# aux file being ignored

I'd like to make a document which looks different each time it is built. I thought this would be pretty easy to do using the aux file; however, it seems I am misunderstanding something very basic. For a small proof-of-concept, here is a document that I thought would produce something different on its second run than on its first:

\documentclass{article}
\def\a{never ran}
\begin{document}
\a
\makeatletter\protected@write\@auxout{}{\string\def\string\a{ran}}\makeatother
\end{document}


After running pdflatex test, I can see that test.pdf indeed contains "never ran", and test.aux contains:

\relax
\def\a{ran}


However, running pdflatex test a second time changes neither file. Why not? What should I change so that the document contains ran on the second run?

• Don't define single letter macros - especially not without checking whether they already exist!! – cfr Oct 16 '15 at 0:34
• – egreg Oct 16 '15 at 8:52

You need \gdef as the .aux file is read in inside a group. At least, I think that's why. Certainly, \gdef works.

I changed the name of the command to avoid the complications which ensued from overwriting an existing one. In general, very short macro names - especially single letters - are unsafe and unwise. At the very least, you should ensure that they do not already exist rather than simply redefining them.

\documentclass{article}
\def\abacus{never ran}
\begin{document}
\abacus
\makeatletter
\protected@write\@auxout{}{\string\gdef\string\abacus{ran}}
\makeatother
\end{document}


Make it a global definition:

\documentclass{article}
\def\hasruncheck{never ran}
\begin{document}
\hasruncheck
\makeatletter\protected@write\@auxout{}{\string\gdef\string\hasruncheck{ran}}\makeatother
\end{document}