Assume I have opened a file myfile and write to it with

 \immediate\write\myfile{My cat is too old}

I have multiple such statements in my document and do calculations in between which influence the outcome of the writing. Up to now, I do not completely understand, when the actual writing is done, especially if the above code is part of the definition of a command.

Can somebody shade light on that issue?

1 Answer 1


Storing a command in a macro does not execute it, so


does nothing until you use \demo:

\demo{My cat is too old}

expands to

\immediate\write\myfile{My cat is too old}

which TeX then starts to execute (as \immediate and \write are non-expandable primitives, not macros). Writing to the file takes place at this point: as soon as TeX reaches the end of the second argument (... old}) it will write to the file.

The \immediate here is used as the alternative

\write\myfile{My cat is too old}

does not write to the file 'now'. Instead, it stores the information, and writes to the file when the next page is shipped out. This is required to get, for example, page numbers correct in the argument, but would give the wrong result if you tried to read back the file before the next shipout.

  • Thank you for making that point clear. Digging deeper, I found out that \immediate\write just appeared to not write immediately because I wrote something based on references which therefore contained the information from the last run. Mar 23, 2013 at 18:22

You must log in to answer this question.

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