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
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
---
This approach is not specific to the `exercise` package, but will also work with other classes/packages. For example to automatically compile a second version of a beamer presentation including notes:
```
% !TeX program = latexmk -pdf -interaction=nonstopmode -synctex=1 -jobname=% -pretex="\newif\ifnotes \notesfalse" -usepretex % | latexmk -pdf -interaction=nonstopmode -synctex=1 -jobname=%_notes -pretex="\newif\ifnotes \notestrue" -usepretex %
\documentclass{beamer}
\ifdef{\ifnotes}{
\ifnotes
\setbeameroption{show notes on second screen}
\fi
}{}
\begin{document}
\begin{frame}
test
\note{for notes}
\end{frame}
\end{document}
```
Answer #2
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 #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}
```
Answer #4
Diaa
I would tweak Samcarter's [beautiful answer](https://topanswers.xyz/tex?q=583#a632) and keep what I need in a *.cmd file (if you are working on Windows) to not travel that long through magic comment to edit something.
IMHO, it is totally not advisable to make the solution word appended to the file name since you might not pay enough attention and send the solved exam to the students instead. So, I prefer to prepend it in a clear way.
I would create some CMD files for building the exam and solution files. The following files are expecting `exam` package class.
The following CMD files expect the `*.tex` file in their folder.
## BuildExam.cmd
```
@echo off
cd /d %~dp0
set "myFileName=%~1"
set "myEngine=%~2"
if "%myEngine%"=="lualatex" (^
texfot lualatex -jobname="[Exam] %myFileName%" -synctex=1 -file-line-error -halt-on-error "\AtBeginDocument{\printanswersfalse}\input{%myFileName%}") ^
else (^
latexmk -lualatex -jobname="[Exam] %myFileName%" -silent -synctex=1 -file-line-error -halt-on-error -g -e "$max_repeat=3" -usepretex=\AtBeginDocument{\printanswersfalse} "%myFileName%")
```
## BuildSolution.cmd
```
@echo off
cd /d %~dp0
set "myFileName=%~1"
set "myEngine=%~2"
if "%myEngine%"=="lualatex" (^
texfot lualatex -jobname="[Solution] %myFileName%" -file-line-error -halt-on-error -synctex=1 "\AtBeginDocument{\printanswerstrue}\input{%myFileName%}") ^
else (^
latexmk -lualatex -jobname="[Solution] %myFileName%" -silent -synctex=1 -file-line-error -halt-on-error -g -e "$max_repeat=3" -usepretex=\AtBeginDocument{\printanswerstrue} "%myFileName%")
```
Then, ask texstudio to call either one by defining some commands
![image.png](/image?hash=e68633cc2e4396a1ae152c8cfe994cc589671bdf038c8ee3d08e8fd5efc0161e)
Enjoy building by just hitting the right shortcut
![image.png](/image?hash=e1d408ee8936d7ae2ea7452cc67df02fcc0f5cf8eef2276654595b39d9e571dc)
Then, you will have two files in the working directory:
`[Exam] <tex file name>.pdf`
`[Solution] <tex file name>.pdf`
Answer #5
samcarter
Instead of using TeXStudio's magic comments as in [my previous answer](https://topanswers.xyz/tex?q=583#a632), one could also use the cool TeX automatisation tool arara.
Here a "translated" version of the exercise example:
```
% !TeX program = arara --verbose --max-loops 3 --log % | txs:///view-pdf | txs:///view-log
% arara: latexmk: {
% arara: --> engine: pdflatex,
% arara: --> options: [
% arara: --> '-jobname=@{getBasename(getOriginalReference())}',
% arara: --> '-synctex=1',
% arara: --> '-interaction=nonstopmode',
% arara: --> '-pretex="\\newcommand{\\version}{noanswer}"',
% arara: --> '-usepretex'
% arara: --> ]
% arara: --> }
% arara: latexmk: {
% arara: --> engine: pdflatex,
% arara: --> options: [
% arara: --> '-jobname=@{getBasename(getOriginalReference())}_sol',
% arara: --> '-interaction=nonstopmode',
% arara: --> '-pretex="\\newcommand{\\version}{}"',
% arara: --> '-usepretex'
% arara: --> ]
% arara: --> }
\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}
```
And here the example with beamer note pages:
```
% !TeX program = arara --verbose --max-loops 3 --log % | txs:///view-pdf | txs:///view-log
% arara: latexmk: {
% arara: --> engine: pdflatex,
% arara: --> options: [
% arara: --> '-jobname=@{getBasename(getOriginalReference())}',
% arara: --> '-pdflatex="pdflatex -interaction=nonstopmode"',
% arara: --> '-pretex="\\newif\\ifnotes \\notesfalse"',
% arara: --> '-usepretex'
% arara: --> ]
% arara: --> }
% arara: latexmk: {
% arara: --> engine: pdflatex,
% arara: --> options: [
% arara: --> '-jobname=@{getBasename(getOriginalReference())}_notes',
% arara: --> '-pdflatex="pdflatex -interaction=nonstopmode"',
% arara: --> '-pretex="\\newif\\ifnotes \\notestrue"',
% arara: --> '-usepretex'
% arara: --> ]
% arara: --> }
\documentclass{beamer}
\ifdef{\ifnotes}{
\ifnotes
\setbeameroption{show notes on second screen}
\fi
}{}
\begin{document}
\begin{frame}
test
\note{for notes}
\end{frame}
\end{document}
```