# Is there a way to know how often latex has already run? [duplicate]

A few days ago, I have seen an example of a latex file, that did conditional \defs on subsequent runs but I can't find that one again.

I still remember that it was by looking into the \jobname.aux do get the number of previous builds.

In my current situation, I'd like to not use package minted but define its commands to simply use verbatim-environment during the first two builds and using minted and its built-in macros for syntaxhighlight in the third run.

How exactly can I get the number of previous latex-runs?

-

## marked as duplicate by egreg, Mico, Malipivo, Masroor, WernerJul 18 '14 at 16:44

The traditional way is to write a counter value to the .aux file – Christian Hupfer Jul 18 '14 at 15:36

This .tex code uses a counter called NumberOfRuns and writes its value to the .aux file at the end of the document (\AtEndDocument). In the beginning of the document, after the .aux file is read, the counter value is known and then increased.

It is easy then to react to the current counter value via \ifnum or \ifnumexpr commands, for example from the etoolbox package.

Note: If the .aux file is deleted, the information is lost.

\documentclass{scrartcl}

\newcounter{NumberOfRuns}

\makeatletter
\AtEndDocument{%
\immediate\write\@auxout{%
\string\setcounter{NumberOfRuns}{\number\value{NumberOfRuns}}
}
}%

\AtBeginDocument{%
\refstepcounter{NumberOfRuns}
}%

\makeatother

\begin{document}

This document was compiled \theNumberOfRuns~times so far!

\end{document}


Improved version, which shows the query for certain values

\documentclass{scrartcl}

\usepackage{etoolbox}

\newcounter{NumberOfRuns}

\makeatletter
\AtEndDocument{%
\immediate\write\@auxout{%
\string\setcounter{NumberOfRuns}{\number\value{NumberOfRuns}}
}
}%

\AtBeginDocument{%
\refstepcounter{NumberOfRuns}
}%

\makeatother

\newcommand{\prettyoutput}[1]{%
\ifnumequal{\number\value{#1}}{1}{once}{%
\ifnumequal{\number\value{#1}}{2}{twice}{%
\ifnumequal{\number\value{#1}}{3}{thrice}{%
\number\value{#1}~times%
}%
}%
}%
}%

\begin{document}

This document was compiled \prettyoutput{NumberOfRuns} so far!

\end{document}


-
I know, that in English language it should be once, twice, three etc. times ;-) – Christian Hupfer Jul 18 '14 at 15:43
I thrice presented him a kingly crown, // Which he did thrice refuse: was this ambition? (Julius Caesar, Act 3, Scene 2). ;-) – egreg Jul 18 '14 at 15:48
@egreg: Oh come on, how many people use thrice really :D – Christian Hupfer Jul 18 '14 at 15:48
Yeah, that every information in the .aux-file is lost when you delete it is quite obvious ;) As far as I can see, during the first run, the counter is 0 until the \begin{document}? Or better said, if $n$ is the number of runs, then the counter is $n-1$ in the preambel? – NobbZ Jul 18 '14 at 15:48
@NobbZ: No, as the code is written, it is initially set to 1, since it is \refstepped within \AtBeginDocument. When a new counter is defined, how ever, its initial value is 0, that's true – Christian Hupfer Jul 18 '14 at 15:50