1

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
\fancyhf{} % Clear header/footer
\fancyfoot[L]{\@title}
...
\newcommand{\version}[3]{#1 & #2 & #3\\\hline}
\newenvironment{history}{ ... }{ ... }

And I am thinking of doing this:

\def\@revision{?}

\pagestyle{fancy} % Imports header style \fancyhf{} % Clear header/footer \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\.]+

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

2 Answers2

1

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

\documentclass{article}
\usepackage[raiselinks]{hyperref}

\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.

egreg
  • 1,121,712
0

I managed to adapt an answer from this solution: https://tex.stackexchange.com/a/160035/205379

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
\fancyhf{} % Clear header/footer
\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.

Stewart
  • 703
  • A safer approach is to use \edef\@currentlabel{#1} and \label. You can also put \string\gdef\string\foo{#1} into \protected@write \@auxout. – John Kormylo Jul 29 '22 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. – Stewart Jul 29 '22 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). – John Kormylo Jul 29 '22 at 16:41