I recently found myself frustrated by LaTeX's lack of (straightforward at least) support for conditional compilation.
I was going through the process of resubmitting a revised version of a manuscript to a journal, and needed to compile two versions of the document: a marked up version with certain parts of the text highlighted, and a "clean" one. These needed to otherwise be identical.
I wanted to be able to easily build two versions of the paper without having to change the LaTeX source to eliminate the risk of introducing accidental changes in the text.
As a software engineer, my initial instinct was to attempt to find some way to pass environment variables to pdflatex, which I could then evaluate in the LaTeX source itself and trigger some behavior on their values.
Unfortunately, there doesn't seem to be any simple way to do so.
However, while searching for other solutions, I stumbled upon this StackExchange question and associated answers discussing the \IfFileExists macro.
The \IfFileExists macro triggers conditional behavior at document compilation time based on the presence (or absence) of a file.
It is commonly used to only include certain parts of a document if the source files are present.
However, I realized it's behavior is generic enough that, in combination with a build tool such as make it can also easily be used for the type of conditional compilation I needed.
To achieve this, I added the following to my document preable:
paper.tex% conditional compilation of "review" version
\IfFileExists{./.hlChanges.tmp}{%
\newcommand{\hllinenums}{\linenumbers}%
\newenvironment{highlight}{\color{red}}{\normalcolor}%
}{%
\newcommand{\hllinenums}{}%
\newenvironment{highlight}{}{}%
}
\begin{document}
\hllinenums
This snippet checks for the existence of a temporary and hidden file ./.hlChanges.tmp at compilation time.
If this file is found next to the root .tex file of the document, a \hllinenums macro and a highlight environment are defined to enable line numbers and red text highlighting in the document.
On the other hand, if the file is absent, this command and environment become no-ops, and the call to \hllinenums after \begin{document} does nothing.
With this, I can then wrap any piece of text I want to be highlighted in the "special" version of the document with the highlight environment.
To then compile the highlighted version, I just need to create the dummy ./.hlChanges.tmp before invoking pdflatex.
I automated this process using make:
Makefilepaper.pdf: paper.tex
latexmk -g -pdflatex -synctex=1 -interaction=nonstopmode -file-line-error -pdf $<
paper_hl.pdf: paper.tex
touch ./.hlChanges.tmp
(latexmk -jobname=paper_hl -g -pdflatex -synctex=1 -interaction=nonstopmode -file-line-error -pdf $< \
&& rm -f ./.hlChanges.tmp) || (rm -f ./.hlChanges.tmp && exit 1)
The paper.pdf target compiles the document without highlighting.
The paper_hl.pdf target compiles the same document with highlighting by first creating the dummy file, compiling the document with basically the same options as paper.pdf, and then deleting the dummy file.
Note that the ... && rm ... || rm ... && exit 1 trick is to make sure the file is deleted whether the manuscript compiles successfully or not.
I still wish LaTeX was more modern (or that typst was more widely adopted), but this did the trick for me and made the (always challenging) process of resubmitting a revised version of a manuscript slightly smoother!