I'd like to know when the
auxfile is read. I know it happens after the preamble, but does it come before or after the\AtBeginDocumenthook?I'd like to know when the
auxfile (or any other helper file) is actually written. I tried to trace the writing process by placing an undefined command after something should have been written to theauxfile, and runpdflatexin interactive mode, like so:\documentclass{article} \begin{document} \section{S}\label{s}% write something to the aux file \foo% unknown command \end{document}but to my surprise the
auxfile is still empty whenpdflatexstops at the error. I guess there is some buffering happening, but how would I resolve the synchronization issue? Say, when\foois command that wants to read from theauxfile?
- 19,096
1 Answers
The .aux file is read as part of the \document macro (\begin{document}) but before the \AtBeginDocument hook is used. (You can check this by inserting some 'test code' into the .aux file and the hook.)
Writing to the .aux file takes place both 'immediately' and at shipout. The latter is important to get for example the correct page numbers for cross-references, so things like \label take place in a delayed fashion, using the \protected@write 'wrapper' around the \write primitive. Thus for example
\documentclass{article}
\begin{document}
\makeatletter
\immediate\write\@auxout{\string\foo}
\protected@write\@auxout{}{\string\baz}
\end{document}
only places \foo in the .aux file: \baz is never written as there is no page shipout. In your example, shipout occurs after \foo, so you see nothing in the .aux file at that point although a shipout does occur later.
Note that TeX keeps the .aux file open until the end of the run, so you cannot be sure that any particular \write will appear in the partial .aux file during a run. As such, the only safe time to check on what gets written to the file is after the run completes. In particular, badly-terminated jobs may leave the .aux file in an incomplete state even if the crash occurs after writes 'should' have taken place.
- 259,911
- 34
- 706
- 1,036
\clearpageafter the\label, and pdflatex tells me it shipped out the first page before running into the error, I still don't see anything in theauxfile. Why? – mafp Jun 25 '13 at 10:30\clearpagedoes nothing if there's no material to build a page. – egreg Jun 25 '13 at 10:36\end{document}– Ulrike Fischer Jun 25 '13 at 10:38[1{/var/lib/texmf/fo nts/map/pdftex/updmap/pdftex.map}] ! Undefined control sequence. l.8 \foo, that is the shipout of the first page, no? – mafp Jun 25 '13 at 10:39\makeatletter\immediate\closeout\@mainauxbefore the error it would appear. – Ulrike Fischer Jun 25 '13 at 11:26texhas already invoked the operating system'swrite()syscall, it may well be the case that the file system is buffering the content until the finalclose(). However, this depends on the OS and using another file system could change this behaviour. However, iftexitself does some internal buffering (I don't know the implementation details) and delays thewrite()syscall until the\closeout\@mainaux, I would consider it as asynchronous output bytex. – Daniel Jun 25 '13 at 13:13.auxfile until the very end. as it stands now, unless someone reads @UlrikeFischer's comments, they might still think that they should be able to see a partial.auxfile if their job crashes 99% of the way through a long job, but they can't. – barbara beeton Jun 25 '13 at 13:33\bazto the.auxfile. This is because\protected@writeis quite different from\write.\protected@writetakes 3 args and the 3rd one is subject to\edefbefore\write. One could write\bazwith\protected@write\@auxout{}{\protect\baz}(+ put a box on page). – frougon Apr 14 '19 at 06:31\noexpandas the idea is to show as little difference as possible – Joseph Wright Apr 14 '19 at 06:49\noexpand(i.e., if the page were non-empty,\protected@writewould write the expansion of\bazbut not the\baztoken), but if it's on purpose, okay. :) – frougon Apr 14 '19 at 06:51