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}

    \begin{Answer}[ref={ex1}]

        solution

    \end{Answer}

\end{document}
```
Top Answer
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>}"
```

So adapted to your use case this would look like

```sh
pdflatex -jobname="<file>-noanswer" "\\newcommand*\\version{noanswer}\\input{<file>}"
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
\def\version{noanswer}
\fi

\usepackage[\version]{exercise}

\begin{document}

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

        question text

    \end{Exercise}

    \begin{Answer}[ref={ex1}]

        solution

    \end{Answer}

\end{document}
```
Answer #2
samcarter
With texstudio one can use the following magic comment to automatically compile two versions:

```
% !TeX program = latexmk -pdf -pdflatex="pdflatex -synctex=1 -interaction=nonstopmode -shell-escape" -jobname=% -pretex="\newcommand{\version}{noanswer}" -usepretex % | latexmk -pdf -pdflatex="pdflatex -synctex=1 -interaction=nonstopmode -shell-escape" -jobname=%_solution -pretex="\newcommand{\version}{}" -usepretex % | txs:///view-pdf "?am)_solution.pdf"

\documentclass{article}

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


\usepackage[\version]{exercise}

\begin{document}

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

        question text

    \end{Exercise}

    \begin{Answer}[ref={ex1}]

        solution

    \end{Answer}

\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
samcarter replying to Skillmon
:) 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
Skillmon replying to samcarter
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
samcarter replying to Skillmon
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 :)
Skillmon replying to samcarter
all this benchmarking I do lately changes me...
Skillmon replying to samcarter
this is more or less the same that `latexmk` does with the `-pretex` option.
samcarter
@Skillmon Thanks a lot for your answer! ... and for the hint about \unless!
Skillmon
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.