Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

marked as duplicate by egreg, Mico, Malipivo, Masroor, Werner Jul 18 at 16:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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

1 Answer 1

up vote 5 down vote accepted

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}
}%

\makeatletter

\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}
}%

\makeatletter

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

enter image description here

share|improve this answer
    
I know, that in English language it should be once, twice, three etc. times ;-) –  Christian Hupfer Jul 18 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 at 15:48
    
@egreg: Oh come on, how many people use thrice really :D –  Christian Hupfer Jul 18 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 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 at 15:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.