\
write
operations are only executed as the page is sent to the DVI
or PDF file. Thus, if you arrange that your page-number macro
(\
thepage
, in LaTeX) is not expanded until the page is
written, then the number written is correct, since that time is where
TeX guarantees the page number tallies with the page being sent
out.
Now, there are times when you want to write something straight away:
for example, to interact with the user. TeX captures that
requirement, too, with the primitive command \
immediate
:
writes a “computer-irritates-user” message, to the terminal. Which brings us to the reason for that\immediate\write\terminal{I'm waiting...}
\
terminal
. TeX can
“\
write
” up to 16 streams simultaneously, and that argument to
\
write
says which is to be used. Macro packages provide the
means of allocating streams for your use: Plain TeX provides a macro
\
newwrite
(used as “\
newwrite
\
streamname
”, which sets
\
streamname
as the stream number). In fact, \
terminal
(or
its equivalent) is the first output stream ever set up (in most macro
packages): it is never attached to a file, and if TeX is asked to
write to any stream that isn’t attached to a file it will send
the output to the terminal (and the log).
This answer last edited: 2011-10-15
This question on the Web: http://www.tex.ac.uk/cgi-bin/texfaq2html?label=write