# Running pdflatex the minimally necessary number of times for elsarticle

Running pdflatex ONCE on

\documentclass{elsarticle}%%% version 3.1 from CTAN
\begin{document}
\begin{frontmatter}
\author[1]{Johann Sebastian Bach}
\author[2]{Ludwig van Beethoven\corref{cor2}}
\cortext[cor2]{Corresponding author}%
\end{frontmatter}
\end{document}


produces the following output:

As you see, the labels of both authors are 1, and the superscript star after Mr. Beethoven is missing. You need the second run of pdflatex to correct this issue.

However, running pdflatex ALWAYS at least twice is not what you want: it eats up your time, especially on large papers, whereas running pdflatex once might sometimes suffice if the right .aux files are available from a prior run. I looked into the .log file and the console output in an attempt to discover some hints on whether a second rerun is needed, but I could not find anything usable. My prior approach to checking whether a rerun is needed is saying something like

MESSAGE_FOR_RERUN := '(LaTeX Warning: Label$$s$$ may have changed\. Rerun to get cross\-references right\.)|(LaTeX Warning: There were undefined references\.)|(LaTeX Warning: Citation [^[:cntrl:]]* on page [0-9]* undefined on)|(Package natbib Warning: There were undefined citations\.)|($$mparhack$$ *Rerun to get them right\.)'


in the beginning of a makefile and

for i in $(SOURCES); do \$(PDFLATEX) $$i ; \ done if (egrep (MESSAGE_FOR_RERUN) (OBJECTS)); then \ for i in (SOURCES); do (PDFLATEX)$$i ; done; \
fi


(where the variables are defined appropriately) in a rule of the makefile. (Of course, in a non-MWE, you might also need to run some choice of sed, awk, rm, bibtex, bibtex8, biber, makeglossaries, makeindex, xindy, dvi2ps, zip, chmod or god-knows-what in addition, but such details on other programs are all off-topic here.)

Of course, you can have latexmk or \usepackage[mainaux]{rerunfilecheck} do the job, but I wonder: can you continue doing it the previous way via makefiles, and if so, which string to search for in the log files?

• I'm not sure if I understood your question, since after you compile the 2nd time, that problem disappears. If you need to compile it again it is because you changed or added some material, not because the title part. So, simply type, compile and be happy. Just make sure to run it 2 or 3 times before submit the paper. – Sigur Mar 14 '19 at 1:42
• So, take a look at arara. It has a lot of tools to automate. – Sigur Mar 14 '19 at 2:01
• .log file parsing alone can not be guaranteed to be enough to detect necessary reruns. The code you show here is a prime example of that. The only difference in the .log file between the two runs (on a clean directory) is the absence of the .aux (and .spl, no idea what that does) in the first run. You could detect the relevant message (No file <\jobname>.aux.) and rerun if it is found, that might give you false positives if the .aux file is not used for anything, but you can probably live with that. Now suppose you have run your document once or twice and the .aux is present ... – moewe Mar 14 '19 at 6:48
• ... but then you need to add a third author with their own affiliation/\address. So you add \author[3]{...} and \address[3]{...}. You compile once to get a "1" for the their footnote instead of "c". The .log is identical (except for time and date and memory usage info) to the previous run. But still you need another run for the marker come out as "c". In this situation no amount of .log parsing can help you. It would have helped to compare the .aux files since they differ. IIRC latexmk actually compares all .aux-like files and reruns LaTeX and friends until they are stable... – moewe Mar 14 '19 at 7:07
• I know the algorithm latexmk uses is based on finding all the files the project depends on, and then keeping an eye on them (based on both timestamps, hash value and analysis). It is not as easy as just a simple if then else – daleif Mar 15 '19 at 12:09

Straightforward .log file parsing can not be guaranteed to be sufficient to detect necessary reruns. The rerun warnings in the .log only appear because LaTeX has set up a heuristic to warn you if \labels and citations (and maybe a few other things) look as though they need another LaTeX run. But packages are not obliged to use those monitored mechanisms and may well do their own thing.

The code in the question is a prime example of such code. The relevant labels are written out with the custom \Newlabel command (instead of \newlabel) and there is no code to detect changes with those.

Indeed the only relevant differences between the .log files of the first and second run are

No file <jobname>.aux.


vs

(<jobname>.aux)


and whether or not a file called <jobname>.spl is loaded.

It is now easy to construct an example where the .log files remains the same (except for time and memory information), but the output still needs another LaTeX run.

1. Take the example from the question and compile it twice.
2. Add a third author: \author[3]{Antonín Dvořák} with address \address[3]{Vyšehrader Friedhof}
3. Compile once to see the footnote "1" for Dvořák.
4. Compile again to get "c".

In between steps 3 and 4 the .log file stays the same except for date/time info, memory usage and line numbers for the font infos. None of these changes, however, indicates that another LaTeX run is necessary.

So unless the packages you use have some kind of heuristics built in on the LaTeX side that double-checks if a rerun is needed and warns in the .log, the .log file alone is not enough to decide whether to rerun LaTeX.

That is one of the reasons why latexmk also monitors the auxiliary files for changes. If the auxiliary files are stable between runs it stands to reason that the output should also be stable. Indeed, in the example above \Newlabel{3}{c} gets added to the .aux file in the LaTeX run of step 3. So the .aux is different before and after the run in step 3, but the run in step 4 does not change the .aux file. (Of course there are situations where a rerun is not needed even if the .aux files change, for example because new \labels are not used. So it is not guaranteed that the resulting build cycle is the shortest for a given document. Additionally, there are situations where the .aux file never stabilises: Avoid infinite compiler passes with biblatex ibid citation styles?.)

The package rerunfilecheck, which you mention in the question, makes it possible to monitor the .aux file between LaTeX runs. In particular the hash of the .aux file before and after the current LaTeX run are compared. If the hash changed, that indicates that a rerun might be useful and you get a warning of the following form

Package rerunfilecheck Warning: File <jobname>.aux' has changed. Rerun.

Package rerunfilecheck Info: Checksums for <jobname>.aux':
(rerunfilecheck)             Before: AE406B0DDBF18055292F1343793AC8CB;63
(rerunfilecheck)             After:  ED9C2E0E40AE4680FD0E58AB745EA3BF;80.


If, on the other hand, the .aux is stable, it reports

Package rerunfilecheck Info: File <jobname>.aux' has not changed.
(rerunfilecheck)             Checksum: ED9C2E0E40AE4680FD0E58AB745EA3BF;80.


So with rerunfilecheck your task just reduces to parsing the rerun warnings of that package.

It might be possible to redo what rerunfilecheck does externally in your makefile.

• Hash the current .aux and remember the hash.
• Run LaTeX.
• Hash the new .aux` and compare to the old hash.
• If the hashes are different, rerun.

I doubt that it would be worth the effort, but it might be a nice academic exercise in bash scripting (not that I have any idea how to go about actually achieving this task).