or
samcarter
latexmk
I am making an exercise sheet and need two versions, one for the students with only the questions and one for me with both the questions and the solutions.

How can I automatically compile two versions of the same document without commenting/uncommenting things and renaming pdfs between the compilations?


\documentclass{article}

\usepackage[
noanswer % comment or uncomment here
]{exercise}

\begin{document}

\begin{Exercise}[title={Title},label=ex1]

question text

\end{Exercise}

solution

\end{document}

Skillmon
As a generalized approach, independent on the used editor, one can invoke the compiling binary with something like the following from the command line:

sh
<tex> -jobname="<file>" "<definitions>\\input{<file>}"


sh
pdflatex -jobname="<file>" "\\newcommand*\\version{}\\input{<file>}"


With the same modifications to the file as you did this would create two PDFs, one <file>.pdf and one <file>-noanswer.pdf.

tex
\documentclass{article}

% setting a default value in case it is compiled without the magic comment
\unless\ifdefined\version
\fi

\usepackage[\version]{exercise}

\begin{document}

\begin{Exercise}[title={Title},label=ex1]

question text

\end{Exercise}

solution

\end{document}

samcarter
With texstudio one can use the following magic comment to automatically compile two versions:



\documentclass{article}

% setting a default value in case it is compiled without the magic comment
\unless\ifdefined\version
\fi

\usepackage[\version]{exercise}

\begin{document}

\begin{Exercise}[title={Title},label=ex1]

question text

\end{Exercise}

solution

\end{document}

The magic comment consists of three separate steps:

1. - latexmk creating the student version with latexmk, which will automatically determine the required number of latex runs and other tools
- -pdf telling latexmk to create a pdf
- -pdflatex="pdflatex -synctex=1 -interaction=nonstopmode -shell-escape" passing some options pdflatex, for example in my real world example I need -shell-escape for some diagrams. If you don't need it, better remove this option.
- -jobname=% setting a job name
- -pretex="\newcommand{\version}{noanswer}" passing the noanswer option to the document
- -usepretex % make sure latexmk will use the pretex option which we just set

2. - latexmk creating the version for myself including answers
- -pdf see above
- -pdflatex="pdflatex -synctex=1 -interaction=nonstopmode -shell-escape" see above
- -jobname=%_solution changing the jobname to not overwrite the student version
- -pretex="\newcommand{\version}{}" include the answers
- -usepretex % see above

3. txs:///view-pdf "?am)_solution.pdf" will display the pdf with the solution in the pdf viewer

Compile two versions of a document from the same latex source
:) puh, I still get coffee. When I saw the link, I was afraid that it would lead to something like https://tex.stackexchange.com/questions/79936/coffee-stains-in-latex and you'd say I don't need coffee anymore
the difference is not that big, though :) and if you want more coffee you can always take a look at https://tex.stackexchange.com/questions/122116/how-to-prolong-compilation-time-while-engaging-in-leisure-activities
You're just trying to take my coffee away. If my document compiles too fast, there is not enough time to walk to the coffee maker and back during compilation :)
all this benchmarking I do lately changes me...
this is more or less the same that latexmk does with the -pretex option.
@Skillmon Thanks a lot for your answer! ... and for the hint about \unless!
Note that if you want to negate the result of an \if... test and don't need the true branch of that test (so would do \ifdefined\version\else <stuff>\fi), it is faster to do \unless\if... (in this case \unless\ifdefined\version <stuff>\fi). \unless is part of e-TeX.