/ LaTeX

How I Created a Race Condition in LaTeX

LaTeX is a powerful typsetting language. It's generally used for documents, particularly those with complicated mathematical symbols and expressions, as LaTeX can render them with ease. However it is capable of producing more than just documents; you can create Powerpoint style presentations as well.

Presentations in LaTeX: Beamer

The beamer package can create slides for presentations in PDF form. If you have animation steps in a slide, such as revealing one bullet at a time, then beamer will output a PDF with multiple pages per slide, with each slide adding another bullet. This is known as presentation mode.

Additionally, beamer can render the presentation in handout mode, where the animation steps are removed, and there's only one PDF page per slide. This is generally intended for handing out copies of the presentation. While I don't generally hand out copies of my presentations, I do use the handout mode because it is much faster to scroll through on the computer than presentation mode.

The Race Condition

The standard compiler of choice for rendering PDF files of LaTeX code is pdflatex. One of the disadvantages of writing in LaTeX is that you do not get to see the changes to your LaTeX code in real time; the document must be recompiled after any change.

To solve this problem, there is a perl script called latexmk which has a continuous mode: it automatically recompiles the document whenever it detects a change to the file (or set of files that affects the final document).

I wanted to compile both presentation and handout modes of my slides simultaneously without having two copies of the same slides. So I created three separate files.






<slides content>

This way I can edit just one file, Slides.tex, while compiling Presentation.tex in presentation mode and Handout.tex in handout mode into two separate files, Presentation.pdf and Handout.pdf.

However, if you do this with two instances of latexmk, one for Presentation.tex and one for Handout.tex, you create a race condition with the aux files that get generated.

LaTeX was designed in the early days of computing when far less RAM was available than today. Hence, during the compilation process, instead of holding temporary information in RAM, many temporary files (including aux files) get written and then read. If it were redesigned today, these aux files wouldn't be necessary.

latexmk will not only recompile because of a change in the tex files, but also the aux files, which is problematic.

Suppose you have the three files set up as above, and two instances of latexmk running in continuous compilation mode: one for Presentation.tex and one for Handout.tex. Once you make a change to Slides.tex, both instances of latexmk will start recompiling. Let's suppose without loss of generality that the latexmk instance compiling Presentation.tex finishes first. It writes a change to Slides.aux. Unfortunately, now the latexmk instance for Handout.tex detects this change in Slides.aux, and then recompiles and writes a change to Slides.aux. This then causes the latexmk instance for Presentation.tex to recompile, and so on.

A Solution

One way to solve this is just create two separate copies of Slides.tex. However, this would make editing a pain.

A better solution is the use of symlinks. Symlinks are essentially file pointers; they redirect any program that wants to write to the symlink to actually write to the file the symlink points to.

In reality, my Slides.tex is

% Change settings based on handout vs not handout mode
% Handout mode
% Not Handout mode



This code defines the command \mysuf as -handout if the document being compiled is in handout mode, and as an empty string if it is not. Then instead of putting all my content in Slides.tex, I separate out the content into multiple files such as Title.tex, Introduction.tex, etc. And crucially, I create the symlinks Title-handout.tex, Introduction-handout.tex, etc. that all point to their non -handout counterparts.

By doing so, the latexmk instances stop writing over each other; the latexmk instance compiling Presentation.tex generates Title.aux, Introduction.aux, etc. while the latexmk instance compiling Handout.tex generates Title-handout.aux, Introduction-handout.aux, etc.

How I Created a Race Condition in LaTeX
Share this

Subscribe to Seonwoo's Musings