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}

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

---

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}

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}

solution

\end{document}


and the second document would be really short:


% This is exercises-students.tex
\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.
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}

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

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.