28.2 Command line input

As part of the command line invocation

latex-engine options argument

you can specify arbitrary LaTeX input by starting argument with a backslash. (All the engines support this.) This allows you to do some special effects.

For example, this file (which uses the hyperref package for hyperlinks) can produce two kinds of output, one to be read on physical paper and one to be read online.

\ifdefined\paperversion        % in preamble
\newcommand{\urlcolor}{black}
\else
\newcommand{\urlcolor}{blue}
\fi
\usepackage[colorlinks=true,urlcolor=\urlcolor]{hyperref}
  ...
\href{https://www.ctan.org}{CTAN}  % in body
  ...

Compiling this document book.tex with the command line pdflatex book will give the ‘CTAN’ link in blue. But compiling it with

pdflatex "\def\paperversion{}\input book.tex"

has the link in black. We use double quotes to prevent interpretation of the symbols by the command line shell. (This usually works on both Unix and Windows systems, but there are many peculiarities to shell quoting, so read your system documentation if need be.)

In a similar way, from the single file main.tex you can compile two different versions.

pdflatex -jobname=students "\def\student{}\input{main}"
pdflatex -jobname=teachers "\def\teachers{}\input{main}"

The jobname option is there because otherwise both files would be called main.pdf and the second would overwrite the first (see Jobname).

In this example we use the command line to select which parts of a document to include. For a book named mybook.tex and structured like this.

\documentclass{book}
\begin{document}
   ...
\include{chap1} 
\include{chap2}
  ...
\end{document}

the command line

pdflatex "\includeonly{chap1}\input{mybook}"

will give output that has the first chapter but no other chapter. See Splitting the input.


Unofficial LaTeX2e reference manual