ARV
ARV

Reputation: 6897

Creating problem-sets with answers in Latex

I want to typeset Mathematical problem-sets in Latex. My requirements are as follows:

When I type them in, I want the questions and the answers to be next to each other in the source-code so that fixing errors, etc. can be done easily.

However, when the document is typeset, I want the answers to appear in a separate "Answers" section just the way they do in textbooks.

Does anyone know of a way to do this?

Many thanks in advance!

Upvotes: 18

Views: 17383

Answers (3)

Pieter
Pieter

Reputation: 2912

You are looking for deferred printing. There are packages that can handle this problem, for instance exercise. This does exactly what you're looking for.

Upvotes: 23

vy32
vy32

Reputation: 29697

I developed a lot of code to do this. There are two approaches. One is you can have a function that keeps adding the answers to a variable that just gets bigger and bigger. The second is you can have function that writes to a file and then, later, reads the file into your document. Frankly, the variable method is easier, even though it seems grosser.

Upvotes: 0

Peter
Peter

Reputation: 132357

Define a 'question' and a 'solution' environment in your questions.tex file (say). Include questions.tex twice. The first time, include questions only. The second time, include solutions only.

\usepackage{version}

% Include questions but not solutions:
\includeversion{question}\excludeversion{solution}

% Include solutions but not questions:
%\excludeversion{question}\includeversion{solution}

\begin{document}

\begin{enumerate}

\item  % Shared question marker.
\begin{question}
Question goes here.
\end{question}
\begin{solution}
solution goes here
\end{solution}

Upvotes: 4

Related Questions