1

I just finished writing my Master's thesis using Latex.

When I was executing my last builds, I was thinking that it would have been quite interesting to count the total number of builds it had taken me to get so far.

Is it possible to somehow count the number of builds you do on a single document? Can I define some sort of counter that is going to increment by 1 with every build (maybe using an external file to store and read the current number)?

Marco7757
  • 235
  • Yes, you can, given the external file is never deleted. Have you seen https://tex.stackexchange.com/questions/282589/include-build-number-inside-pdf? – TeXnician Jul 31 '18 at 09:04
  • Would this also count the compile runs aborted due to some silly error? – Johannes_B Jul 31 '18 at 09:06
  • I would rather do git init in the repertory and use git commit when making changes. You can then do git log --oneline initial..HEAD | wc -l to get the number of commits since initial one (tagged "initial") (or perhaps there is even better git-way to count commits since initial one, did not check). Indeed number of changesets to your Thesis looks more significant than number of LaTeX builds... (add *pdf and *log and *aux and *toc etc.. to your .gitignore, only the tex sources need version-controlling) –  Dec 30 '18 at 09:38

2 Answers2

2

The zref package provides the runs module, which stores the number of runs to the .aux file.

(Side note: The overview of user modules on page 9 in the zref manual is incomplete, the runs module is mentioned on page 16, however)

The number can be retrieved by \zruns, which is an expandable macro.

Please note, that

  • runs where \nofiles is active, are not counted.
  • the number of runs is lost when the .aux file is deleted.

\documentclass{book}

\usepackage{blindtext}

\usepackage[runs]{zref}


\begin{document}
\typeout{This document has been built \zruns\ times}
\blindtext[10]
\end{document}
1

Here is a user-made approach. Unfortunately I don't seem able to find a way to test if TeX has encountered an error in the past (and continued in \nonstopmode for example), so this will count all runs, except if you run in an interactive way and stop the compilation on an error manually (by X).

The idea here is to use a special dedicated auxiliary file, because one may legitimately delete the main .aux file in certain circumstances to recover from some situations.

\newcounter{nbofruns}
\InputIfFileExists{\jobname-nbofruns.txt}{}{}
\stepcounter{nbofruns}

\documentclass{article}
\usepackage{atveryend}
\AtVeryVeryEnd
{%
  \newwrite\runs
  \immediate\openout\runs=\jobname-nbofruns.txt
  \immediate\write\runs
     {\string\setcounter{nbofruns}{\the\value{nbofruns}}}%
}
\begin{document}
This document has been built \arabic{nbofruns} times.
\end{document}
  • the usage of \newwrite appears sub-optimal (it could trigger a "no room for more writes error" in some cases), certainly I could use some existing write stream at this "very very end" location, but I was too lazy to go check in LaTeX2e code and see if I could safely do for example \openout7 safely. –  Dec 30 '18 at 09:32