latexmk add tag
samcarter
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
Circumscribe
I feel like a bit of a peasant because I would just create a separate document for the second version and have it `\input` the first after setting up whatever needs to be set up.
Having a tex file for every compilation mode makes it very clear to future you (or someone else) how different versions of the document can be created, and it's not editor-dependent.

In this case, the first document (which includes both exercises and answers) would look like this:

```
% This is exercises.tex
\documentclass{article}

\usepackage{exercise}

\begin{document}

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

        question text

    \end{Exercise}

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

        solution

    \end{Answer}

\end{document}
```

and the second document would be really short:

```
% This is exercises-students.tex
\PassOptionsToPackage{noanswer}{exercise}
\input{exercises}
```

What I like about `\PassOptionsToPackage` is that no additional code is necessary in the main document for this to work. Also good for this are `\PassOptionsToClass` (e.g. `draft`), `\includeonly{<files>}` and `\AtBeginDocument`.
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

Answer #3
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}
```

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.