Aubrey Anderson

Latex: Conditionally Include Sections from the CLI

When writing my resume recently, I wanted a simple way to hide my address and phone number from the version of my resume I publish here. After digging around various latex package descriptions and StackOverflow posts I wasn’t left with a solution that worked. Most of the answers suggested using special package commands insert packages or having one source file for each version

Requirements

  1. Only one version of the latex source.
  2. No \usepackage{...} directives in the compilation command.

These requirements ensure that maintence is simple and the compilation command differences are short and memorable. #2 in particular requires that the CLI command use only core Latex, but does not prevent the implementation from using any additional packages.

I found that the ifthen package was able to easily accomplish this by switching on whether a command was defined.

The code

Let’s call the command we’re switching on \Switch

\ifthenelse{\isundefined{\Switch}}{
  % Include when the switch is not defined (1)
}{
  % Include when the switch is defined (2)
}

Compile command1 to show (1):

latexmk -g -pdf file.tex

or

pdflatex file.tex

Compile command to show (2):

latexmk -g -pdf -usepretex="\def\Switch{}" file.tex

or

pdflatex '\def\Switch{}\input{file.tex}'

  1. I prefer latexmk because it automatically re-runs if there are still references that need resolving. However, it requires the -g flag to force recompile because it doesn’t recompile when only the -usepretex flag changes. ↩︎