# Storing variables in aux files

Can I use a command to set a variable, then use that variable earlier in the document?

Specifically, I want grab the last revision from the revision history table and put that in my footers.

In my *.tex, I have an environment and commands for a version-history table which I use like this:

\begin{history}
\version{1.0}{1 April 2020}{Initial Release}
\version{1.1}{10 Oct 2021}{Updated chapter 2}
\end{history}


In my *.cls I currently have:

\pagestyle{fancy} % Imports header style
\fancyfoot[L]{\@title}
...
\newcommand{\version}[3]{#1 & #2 & #3\\\hline}
\newenvironment{history}{ ... }{ ... }


And I am thinking of doing this:

\def\@revision{?}

\fancyfoot[L]{\@title Rev\@revision}

\newcommand{\version}[3]{%
\def\@revision{#1}%
#1 & #2 & #3\\\hline%
}
\newenvironment{history}{ ... }{ ... }


But there are two problems:

1. My footers always show ?.
2. Even if \@revision was updated, my version history table is not on the first page. So I imagine it would only set the value for later pages.

Is there a way I can store my revision in *.out or *.aux? Then my footer can use that value at the start of the document, and I'll get a rerunfilecheck if it changed since last run?

I found a promising answer here but it saves counters (which I assume are integer-only). I'm interested in something that can handle [a-zA-Z0-9\.]+

• It would be very helpful if you wrote a small, complete code we can copy/paste instead of censored snippets. Jul 29 at 11:47

You can conditionally set the label if the entry coincides with the last revision number gathered at the previous run.

\documentclass{article}

\makeatletter
\newenvironment{history}
{%
\begin{center}
\begin{tabular}{|c|c|l|}
\hline
Rev & Date & Reason \\ \hline
}
{%
\end{tabular}
\end{center}
\immediate\write\@auxout{\gdef\string\lastrevisionnumber{\@revision}}%
}

\newcommand{\version}[3]{%
#1\gdef\@revision{#1}%
\ifx\@revision\lastrevisionnumber
\def\@currentlabel{#1}\label{lastrevision}%
\fi & #2 & #3 \\ \hline
}
\newcommand{\lastrevisionnumber}{??}
\newcommand{\lastrevision}{\ref{lastrevision}}
\makeatother

\begin{document}

\title{Test}
\author{me}
\maketitle

\begin{history}
\version{1.0}{1 April 2020}{Initial Release}
\version{1.1}{10 Oct 2021}{Updated chapter 2}
\end{history}

\newpage

The last revision is \lastrevision.

\end{document}


You can use \lastrevision anywhere you want.

First I define a command for setting the revision:

\newcommand{\setrev}[1]{%
\protected@write \@auxout {}{\string \newlabel{revision}{{#1}{\thepage}{#1}{revision}{}} }%
\hypertarget{revision}{#1}
}


Then I can access the revision with \ref{revision}:

\pagestyle{fancy} % Imports header style
\fancyfoot[L]{\@title Rev:\ref{revision}}


Then I can set the revision in my document:

\newcommand{\version}[3]{%
\setrev{#1}#1 & #2 & #3\\\hline%
}


I do get the warning:

LaTeX Warning: There were multiply-defined labels.


But maybe I'll have to live with that.

• A safer approach is to use \edef\@currentlabel{#1} and \label. You can also put \string\gdef\string\foo{#1} into \protected@write \@auxout. Jul 29 at 12:19
• That sounds great. I'm trying to find reference manuals for \profected@write \@auxout, \newlabel, \edef, \string, \gdefand \@currentlabel. If you don't mind writing it up as an answer or explaining what it does, I'd be happy to try it out and accept that answer. Jul 29 at 12:33
• \edef, \string and \gdef are TeX commands and can be found in the TeXbook by Knuth. I believe the rest are covered in source2e.pdf (which should already be loaded in the docs folder). Jul 29 at 16:41