# Pattern to detect need for recompilation from auxfile

I am using an auxfile to access the results of some computations which I need before they can actually be computed. Since this requires two runs when something changes the result I would like to have a mechanism to detect such changes and print a message to the user.

I vaguley remember a pattern where a hook was installed to compare the value to be written with the previous value (non immediate write hook), and then setting a flag based on which a warning is displayed to the user, informing him that recompilation is necessary.

Unfortunatly I can not find the example again. Does anybody know this pattern and can point me to some (preferably simple) example code?

Despearte googling turned up the example: https://tex.stackexchange.com/a/49035/19326 However I have an issue understanding it (Purpose of AtEndDocument{\def}).

Since this question is currently in a bad spot (not a real question anymore I guess, I wonder whats the best way to carry on, see my comment below)

• Since I found the example I am not sure how to handle this best, delete the question, paste the awnser from the other question, or add a MWE with explanations. I would prefer the last option, however I will have to understand AtEndDocument(\def ... ) first. – ted Nov 24 '14 at 17:48

\usepackage[aux]{rerunfilecheck} issues a warning if the auxfile has changed and asks for another run.

• +1: While this package seems to provide a solution, I am interested in the pattern I described above, rather than how to use a package that does this based on checksums. While the solution might be the best/easiest to use, I am also interested in an alternative for integration in my package without a dependency and for learning purposes. – ted Nov 24 '14 at 12:22
• Some explanations about the benefits of rerunfilecheck can be found here tex.stackexchange.com/a/213789/19326 – ted Nov 24 '14 at 21:34

The code from https://tex.stackexchange.com/a/49035/19326, which is pasted here for reference poses as an example.

\documentclass{article}
\makeatletter
% The command reference writes \newreference into the aux file
\def\createreference#1#2{\@bsphack
\protected@write\@mainaux{}%
{\string\newreference{#1}{#2}}%
\@esphack}
% The command \newreference defines new command named after the first
% argument that produces the second one
\def\newreference#1#2{%
\expandafter\xdef\csname rfr@#1\endcsname{#2}}
% And using the reference
\def\citereference#1{%
\expandafter\ifx\csname rfr@#1\endcsname\relax??\else
\csname rfr@#1\endcsname\fi}
% LaTeX checks at the end of document whether references have
% been changed.  If any reference is changed,
% \tempswa is set to true.  Here we add \newreference commands
% to the list to be checked.
\AtEndDocument{\def\newreference#1#2{%
\edef\reserved@a{#2}%
\expandafter\ifx\csname rfr@#1\endcsname\reserved@a\else
\@tempswatrue\fi}}
\makeatother
\begin{document}
\createreference{john}{John Q. Citizen}
\citereference{john} and \citereference{george}
\end{document}


We notice the following pattern:

1. Create your own def macro yourDefMacro to assign values to a given key
2. Whenever you want to store something in the auxfile write the string\yourDefMacro{key}{value} to the auxfile
3. At the end of the document redefine the def macro to a checker macro that compares the current value of the macro key to the value that will be assigned to it.

Here are the corresponding codeparts:

## 1. Def Macro

\def\newreference#1#2{%
\expandafter\xdef\csname rfr@#1\endcsname{#2}}


Note that this macro actually adds the prefix rfr@ to the actual name being defined. The add is usually not recognized as a character in document, avoiding namespace clutter (This is changed for the definition of the macros by wrapping it in \makeatletter \makeatother).

## 2. Writing to auxfile

\def\createreference#1#2{\@bsphack
\protected@write\@mainaux{}%
{\string\newreference{#1}{#2}}%
\@esphack}


This will lead to the aux files looking something like this:

\relax
\newreference{john}{John Q. Citizen}


The \@bsphack/\@esphack fixes a space issue by removing superfluous spaces. Basically \@bsphack tests for a space in front of the macro it is in and \@esphack looks if there is a space following. If there are spaces on both sides one is removed resulting in only one space.

## 3. Test Macro

\AtEndDocument{%
\def\newreference#1#2{%
\edef\reserved@a{#2}%
\expandafter\ifx\csname rfr@#1\endcsname\reserved@a%
\else%
\@tempswatrue%
\fi}}


This overwrites the definition of \newreference. Before \AtEndDocument causes the aux file to be read again. It defines a macro \reserved@a to the new definition in the newly written auxfile and compares its definition to the definition create by the Def Macro during the initial read of the auxfile latex did at the start of the compile. There is an empty if branch, however if these values differ \@tempswatrue is executed causing Latex to issue a Compile again message at the end of its run.