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:
- Create your own def macro
yourDefMacro to assign values to a given key
- Whenever you want to store something in the auxfile write the string
\yourDefMacro{key}{value} to the auxfile
- 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.
AtEndDocument(\def ... )first. – ted Nov 24 '14 at 17:48