1

Let's imagine I'm teaching a class, and I want to use the exam package to write up a homework assignment. I'm aware that I could compile two different PDF documents -- one containing solutions, using \documentclass[answers]{exam}, and one without solutions, using \documentclass{exam}. (Alternatively, I could make versions with and without the \printanswers directive.)

However, if the students will typeset their own solutions using LaTeX, the ideal approach would be to create two different source versions of the LaTeX file -- one with solutions and one without. Then, I could distribute the no-solution version of the .tex file (perhaps in addition to the no-solution version of the .pdf file), and students wouldn't have to worry about manually re-typing the problem statements.

With that in mind, is there any easy-to-automate way to create a copy of a given LaTeX source file with solutions removed?

alev
  • 121

3 Answers3

1

Here's what I have come up with; I'm curious whether there are any simpler or more robust options.

My solution is UNIX-specific and uses a tool called gpp, although it might be possible to substitute other general-purpose macro processors like m4.

The file input.tex looks like the following:

\documentclass[answers,12pt]{exam}
\begin{document}
\begin{questions}
\question Here is a question
\begin{solution}\hideSolution{
Here is a lengthy solution.

It may contain many paragraphs.
}\end{solution}
\end{questions}
\end{document}

Then, the relevant commands are:

# To generate the with-solutions .tex file:
gpp input.tex -T -o solutions.tex -D'hideSolution(a)=\a'

# To generate the no-solutions .tex file:
gpp input.tex -T -o problems.tex -D'hideSolution(a)='

The -T option tells gpp to use LaTeX-style macros (as opposed to C-style macros).

The main downside to this approach is that you have to remember to add a special command, \hideSolution{...} -- otherwise the solution won't actually be removed. (Because of this, it's a good idea to actually run pdflatex on the no-solution .tex file and preview the results before distributing to students.) Another downside is that input.tex can't be processed by LaTeX without first running it through gpp.

As vonbrand mentioned, a better approach might be to write a Perl or Python script to pre-process particular environments (like \begin{solution}...\end{solution}) such that their contents are removed from the 'no-solution' version. But that takes a fair amount of error-prone custom coding, and it'd be great to reuse someone else's solution if one exists.

alev
  • 121
0

You can e.g. write questions and answers into environments (that you suitably define), and use an external script in e.g. Perl or Python to strip out the answers to give just questions in LaTeX to your students.

Or just use a package like answers to ship out the answers to new files, and typeset the questions and answers separately. Hand your students just the PDF with the questions.

vonbrand
  • 5,473
0

For homework (just a few questions) this is overkill. I just make up a homework handout, with enumerate to number the questions. Each item gets a comment labelling its position, like % 20151h4 for year/term/homework/question (and even a and so on if there are subquestions). The answers I write up in another file, labelled the same way (so they are easy to match up to collect questions and solutions later). Thus I can hand the students the typeset questions and the corresponding full source (for reference, if they need to reproduce notation or so). The full solutions can be published later, and handed to the TAs for grading. Asking students to repeat the questions in their hand ins is pointless, in my opinion.

vonbrand
  • 5,473