summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/generic/FAQ-en/html/FAQ-write.html
blob: 68fc5cc9dd703f09917937d6d578dda047244f0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<head>
<title>UK TeX FAQ -- question label write</title>
</head><body>
<h3>Writing (text) files from TeX</h3>
<p/>TeX allows you to write to output files from within your document.
The facility is handy in many circumstances, but it is vital for
several of the things LaTeX (and indeed almost any higher-level
TeX-based macro package) does for you.
<p/>The basic uses of writing to an external file are &#8220;obvious&#8221; &#8212;
remembering titles of sections for a table of contents, remembering
label names and corresponding section or figure numbers, all for a
later run of your document.  However, the &#8220;non-obvious&#8221; thing is
easy to forget: that page numbers, in TeX, are slippery beasts, and
have to be captured with some care.  The trick is that <code>\</code><code>write</code>
operations are only executed as the page is sent to the DVI
or PDF file.  Thus, if you arrange that your page-number macro
(<code>\</code><code>thepage</code>, 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.
<p/>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 <code>\</code><code>immediate</code>:
<blockquote>
<pre>
\immediate\write\terminal{I'm waiting...}
</pre>
</blockquote><p>
writes a &#8220;computer-irritates-user&#8221; message, to the terminal.
<p/>Which brings us to the reason for that <code>\</code><code>terminal</code>.  TeX can
&#8220;<code>\</code><code>write</code>&#8221; up to 16 streams simultaneously, and that argument to
<code>\</code><code>write</code> says which is to be used.  Macro packages provide the
means of allocating streams for your use: Plain TeX provides a macro
<code>\</code><code>newwrite</code><code>\</code><code>streamname</code>, which sets <code>\</code><code>streamname</code> as
the stream number.  In fact, <code>\</code><code>terminal</code> (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 <em>any</em>
stream that isn&#8217;t attached to a file it will send the output to the
terminal (and the log).
<p/><p>This question on the Web: <a href="http://www.tex.ac.uk/cgi-bin/texfaq2html?label=write">http://www.tex.ac.uk/cgi-bin/texfaq2html?label=write</a>
</body>