8

What is the difference between \write and \immediate\write commands when writing to a text file?

\write command seems to write nothing on the text file...

Here a MWE:

\documentclass{article}
\texttowrite

\newwrite\mytextfile
\immediate\openout\mytextfile=\jobname.txt

\newcommand*{\texttowrite}{Some text}

\begin{document}

\write\mytextfile{\texttowrite}
\immediate\write\mytextfile{\texttowrite}

\immediate\closeout\mytextfile
\end{document}

EDIT (after egreg's answer and comment): one more exemple for beginners (like me) : I allow myself to share this small test which illustrates an aspect of the operation of \write and \immediate\write:

\documentclass{article}
\newwrite\mytextfile
\immediate\openout\mytextfile=\jobname.txt
\newcommand*{\texttowrite}{Some text}
\begin{document}
\texttowrite

\write\mytextfile{\texttowrite 1}
\immediate\write\mytextfile{\texttowrite 2}
\write\mytextfile{\texttowrite 3}
\immediate\write\mytextfile{\texttowrite 4}

\closeout\mytextfile
\end{document}

Content of \jobname.txt:

enter image description here

2
  • 3
    immediate is written immediatly, without it the write happens when the page is shipped out. Your \write doesn't write as you closed the file to early with the \immediate\closeout. Feb 18, 2022 at 11:55
  • @Ulrike Ok I understand your comment. After little tests it apears that if I replace \immediate\closeout by just \closeout the \write command "works". So I definitly don't understand what the \immediate command do...
    – zetyty
    Feb 18, 2022 at 12:09

1 Answer 1

7

The main usage of \write is to record information such as page numbers when preparing a table of contents or for cross-references.

Thus \write stores the text (without macro expansion) and writes it (with macro expansion) after a page is shipped out.

In some cases, such delay is not needed or wanted, so you can prefix \write with \immediate and, in this case the \write operation (with macro expansion) is carried out immediately.

If you have a \write operation that happens to be executed after a file has been closed, the tokens will be shown on the terminal and, of course, not written out. This is your case: you close the output file before the last \write has a chance to be performed.

Even

\documentclass{article}

\newwrite\mytextfile
\immediate\openout\mytextfile=\jobname.txt

\begin{document}

\write\mytextfile{some text}

\end{document}

would write nothing on the specified file, because no page is shipped out.

4
  • Thanks for your answer. So, the \write command is only "working" if there is no \immediate\closeout in the entire document? If yes, I don't understand the need of \immediate\closeout instead of \closeout... Do you think I should ask a question about \closeout and \immediate\closeout too?
    – zetyty
    Feb 18, 2022 at 19:34
  • @SylvainRigal Also \closeout follows the same rule as \write
    – egreg
    Feb 18, 2022 at 20:15
  • I don't fully understand the need of \immediate\closeout instead of just \closeout, but it's another question... Thanks again!
    – zetyty
    Feb 19, 2022 at 19:40
  • @SylvainRigal In my last comment I said thay you don't want to do \immediate\closeout, but just \closeout.
    – egreg
    Feb 19, 2022 at 20:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .