# Conditional compilation of documents using command line options for pdflatex

Is it possible to compile two different pdf outputs from the same .tex file?

For example, let's say I have a document main.tex which contains questions for students, and answers only for teachers, which I want to hide from the students. Is it possible to compile the student and teacher versions of the document from the command line with different options?

pdflatex '\documentversion{student}' main -o student.pdf # output is student.pdf
pdflatex '\documentversion{teacher}' main -o teacher.pdf # output is teacher.pdf

• The linked question (and its answer) partially answers this question. I vote to reopen this question and provide a complete answer... Dec 31, 2014 at 0:16
• @PaulGaborit It's reopened now. Dec 31, 2014 at 0:58

Here is an example.

You must compile this document with this two commands to get the two versions (students.pdf and teachers.pdf):

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


(The -jobname option defines the name of the produced PDF document. Ex: with -jobname=students option, the command produces students.pdf.)

The main.tex document (using etoolbox to simplify the test):

\documentclass{beamer}
\usepackage{etoolbox}
\begin{document}
\begin{frame}
\frametitle{Title}
\ifdef{\teachers}
{Content for teachers}
{Content for students}
\end{frame}
\end{document}

• Could you explain the use of jobname? Jul 4, 2016 at 12:53
• @Praxeolitic Done... Jul 4, 2016 at 13:00
• Looks like that was a silly question. I was confused and had thought it was part of getting the conditional to work. Jul 4, 2016 at 13:04
• How would multiple variables be accomplished? Feb 4, 2020 at 21:04
• @notlesh I do not understand your question: why do you want multiple "variables"? to produce multiple documents? Feb 4, 2020 at 22:59