Jérôme Belleman
Home  •  Tools  •  Posts  •  Talks  •  Travels  •  Graphics  •  About Me

Effective Ways to Compile LaTeX Files

18 Oct 2017

As I was hurriedly typing a live report of the HEPiX Fall 2017 workshop, I reflected upon the different ways to compile LaTeX documents as you go along.

1 In the Beginning...

I seem to remember it was in 2003 when back in the days I seriously started using LATEX and I learned then that one had to run the following sequence of commands:

latex foo.tex # Generates a DVI file
dvips foo.dvi # Generates a PostScript file
ps2pdf foo.ps # Generates a PDF file

A rather tedious process. What's more, the output PDF quality could under some circumstances be underwhelming, to say the least. When playing with font encodings, character rendering felt it was subject to some low resolution. In addition to this, this chain prevented you from importing PDFs with the \includegraphics command. You could do so with PostScript files, however. Truth be told, it enabled me to work with the PSTricks package for making drawings, which I enormously enjoyed.

2 Only PDFs

Very soon, though, I felt that working with PDFs end to end was the way to go. I stumbled upon the pdfTEX typesetter which short-cuts the dvips and ps2pdf steps:

pdflatex foo.tex # Generates a PDF file

That's all there was to it. And it solved any output PDF quality that I could have had with the previous approach. And since it's PDF end to end, importing PDF files with the \includegraphics command became possible, even if it's importing PostScript files which stopped working. This has never been a problem, all I had to do was give those few PostScript files that I wanted to import to ps2pdf.

What I found more upsetting, however, was that it became impossible to use PSTricks, all the more disappointing because I really became used to this excellent package. But it got me to discover an even better one: Till Tantau's truly astonishing TikZ system. I never looked back. I took particular satisfaction in seeing that I could draw diagrams several times faster with TikZ than all my colleagues possibly could with OpenOffice Draw.

3 Make a la LaTeX

And yet it all still felt a little baroque. Why do I have to compile my source files so many times before all cross references are satisfied? Certainly, I could get away with it by writing Makefiles but it has never really been conclusive and there was always that resident fear that one more compilation might still be necessary. And then I found the latexmk command:

latexmk -pdf foo.tex

The latexmk command also provides the particularly useful -c switch which cleans your directory of any intermediate files such as *.log and *.aux, and the even stronger -C option which also removes the resulting output file. Interestingly, there is no need to pass them the name of the source file, they'll work that out by themselves.

4 Involving Inotify

When I get to write exhaustive reports of conferences such as HEPiX that last the best part of 8 hours of presentations each day, I sometimes find myself nervously compiling the document every few sentences, even when it's not really necessary. This ends up happening so many times a day that most of my battery goes to that.

Why not leave the compiling process to a while loop which I know will do the work for me every 5 minutes, or so? Using latexmk would additionally ensure that the compilation takes place only if the file was changed since then, not necessary each and every 5 minutes. Rather foolishly, though, I got it in my head that there was no such thing as the xelatex equivalent to latexmk, so I decided to involve inotify. There was no need to write any tool based on the API as it already comes with a number of utilities which can be used out of the box, such as inotifywait:

while true; do
  inotifywait foo.tex && xelatex foo.tex && sleep 5m
done

This causes inotifywait to watch the foo.tex file until it changes, at which point it'll exit. Of course, if I'd spent the time reading the latexmk man page more carefully, I would have seen that it comes with the -xelatex switch. This approach could have been a more elegant implemented:

while true; do
  latexmk -xelatex foo.tex && sleep 5m
done

5 References