# Writing tutorials with exam document class, with and without solutions

I would like to write some tutorials for my students, and for this I am using the exam document class, and the solution environment.

now the problem is I have a statement

\printanswers


that I need to comment for the tutorial, and uncomment for the solution.

I was hoping if there's a way through which I can compile it a single time, and the source file (say tutorial.tex) creates two pdfs -

1. tutorial.pdf (without solutions)
2. tutorialSolution.pdf (with solutions)

Following is my source code -

\documentclass{exam}
\usepackage[T1]{fontenc}
\usepackage{pslatex}
\usepackage[pdftex]{color}
\usepackage[pdftex]{graphicx}

\begin{document}
\begin{questions}

\vskip 0.5 cm \question Question header \vskip 0.5cm
Question text

\begin{solution}
Solution text
\end{solution}

\end{questions}
\end{document}


• Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. Have you considered either of the following possibilities? (1) Have two 'wrapper' .tex files with different names which call the main file, compiling the two wrappers to produce the two outputs. (2) Using a script rather than running the compilation directly either instead of, or in combination with, (1). – cfr Aug 19 '14 at 22:57
• Hi, I must admit that I am a relative newbie when it comes to latex. Would it be possible for you to give a short example of compiling multiple files from a single main file? thanks in anticipation! – gaurav Aug 19 '14 at 23:33
• Why do you specify the driver for color and graphicx? Usually those packages are quite capable of detecting the right driver themselves... – cgnieder Aug 20 '14 at 9:28

Here is what I had in mind for one method you might use to manage this type of workflow.

You create 3 files. The first is your main file, tutorial.tex, say:

\usepackage[T1]{fontenc}
\usepackage{pslatex}
\usepackage[pdftex]{color}
\usepackage[pdftex]{graphicx}

\begin{document}
\begin{questions}

\vskip 0.5 cm \question Question header \vskip 0.5cm
Question text

\begin{solution}
Solution text
\end{solution}

\end{questions}
\end{document}


The other two are wrappers. For example, these might be tutorialQuestions.tex:

\documentclass{exam}
\input{tutorial}


and tutorialSolutions.tex:

\documentclass{exam}
\input{tutorial}


You can then compile tutorialSolutions.tex and tutorialQuestions.tex separately without overwriting the other version. Or you could use a script to manage this for you. (How to do that depends on your OS.) It is also possible to use various helpers with TeX to do a lot of this and/or have your IDE automate things. However, the above is the basic idea which you then embed in the way that best suits your preferred tools.

• cfr - thanks a lot! works like a charm. created a simple bash script to do the whole work and clean up afterwards too :) – gaurav Aug 20 '14 at 13:23
• @gaurav Excellent! I thought of this because I used it to create slides and an article version of logic exercises and it made it really simple to manage things. (I didn't bother scripting it but that is, of course, the way to do it properly!) – cfr Aug 20 '14 at 15:49

While cfr's answer is very nice and general, I also ran into this problem and wanted to present a specific solution using one IDE, TeXstudio.

To accomplish this, I defined a user command (Preferences > Build > User Commands) as

"/path/to/script/compile-exam.py" %.tex -f | txs:///pdflatex | mv %.pdf %Solutions.pdf | mv %.synctex.gz %Solutions.synctex.gz | "/path/to/script/compile-exam.py" %.tex | txs:///pdflatex | txs:///view-pdf-internal "?m)Solutions.pdf"


where compile-exam.py is a python script I wrote (available here):

from argparse import ArgumentParser
import re
from shutil import copyfile

parser = ArgumentParser()

args = parser.parse_args()
copyfile(args.filename, args.filename+'.bak')

basename = re.match(r".+(?=\.tex)", args.filename).group(0)

if args.forward:
contents = []
with open(args.filename, 'r') as in_file:
for line in in_file:
else:
contents.append(line)
with open(args.filename, 'w') as out_file:
for line in contents:
out_file.write(line)
else:
contents = []
with open(args.filename, 'r') as in_file:
for line in in_file:
else:
contents.append(line)
with open(args.filename, 'w') as out_file:
for line in contents:
out_file.write(line)


From here, I define a keyboard shortcut to run the command (Preferences > Shortcuts > Tools > User)

Using this keyboard shortcut, the compiler will produce exam1.pdf and exam1Solutions.pdf and display the Solutions file in the built-in viewer (still with the ability to scroll to the location of the last edit, etc.).

Also note, you will either need to call python first or make the script executable and add something like #!/usr/bin/python on the first line.

This is obviously not a general solution and does require calling an external program, but it works nicely and in a reasonable amount of time.