I write a lot of exams, quizzes, and homework problems for the course I teach. Whenever I write an evaluation foo.tex, I always want a separate solutions file foo-solutions.tex to be written. Here's an example of my workflow.
I start by writing the evaluation along with the solutions in a file foo-solutions.tex:
\documentclass{exam}
\printanswers
\newcommand{\checkforstudent}[1]{%
\ifcsname#1\endcsname%
\noprintanswers%
\else% ... command '#1' does not exist ...%
\fi%
}
\begin{document}
\checkforstudent{studentmode}
\begin{questions}
\question What is the first sentence of \emph{Moby Dick}?
\begin{solution}
\emph{Call me Ishmael.}
\end{solution}
\end{questions}
\end{document}
The command \checkforstudent tests whether or not the input exists a a command. If so, then the answers are switched off. The first line after \begin{document} uses \checkforstudent to check for a command
Compiling this file generates foo-solutions.pdf:
I also have a file foo.tex:
\newcommand{\studentmode}{}\input foo-solutions
This file defines the command \studentmode and then inputs the entirety of foo-solutions.tex. Compiling foo.tex generates foo.pdf:
Finally, I have these two pdfs generated at once with a bash script write-files.sh
#!/bin/bash
pdflatex foo-solutions.tex
pdflatex foo-solutions.tex
pdflatex foo
pdflatex foo
The script compiles both files twice to make sure all references are properly dealt with.
This workflow works reasonably well. However, there are some annoyances:
- I have to keep track of three files instead of one (
foo-solutions.tex,foo.tex, andwrite-files.sh). - When I write a new evaluation
bar.tex, I copyfoo-solutions.tex,foo.tex, andwrite-files.shto a new directory and replace the instances offoowithbar. This really annoys me. - Many of my colleagues are not comfortable with running bash scripts. So, when I share my documents with them, they have trouble generating both the evaluation and the solutions.
It would be nice if there were a solution to my problem that allowed me to generate foo-solutions.pdf and foo.pdf with just one latex file.
Is this possible?
Note. My current workflow was inspired by this answer to a similar question of mine.


l.18 \askanswers""
– pzorba75 Dec 08 '17 at 03:31\typein. – Skillmon Dec 08 '17 at 11:22