[Tex/LaTex] How to typeset subquestions and the corresponding answers

answersexamexercisespackagesprobsoln

I would like to typeset exercises with subquestions so that in the code I have an answer immediately following each subquestion, but in the output all the answers will be in the end. Also I want both exercises and subquestions to be automatically numbered. I know that there are several packages for typesetting exercises (for example 'answers', 'exercise' and 'probsoln'). I also know that there is a document type called 'exam'.

My question is: Which package would work best for this and what kind of code would produce the result?

I would like to write something like the following:

\begin{ExerciseList}
  \Exercise{}
    Calculate the following:
    \Question{$7+2$}
      \Answer{$9$}
    \Question{$9-9$}
      \Answer{$0$}
    \Question{$5+5+5$}
      \Answer{$15$}
  \Exercise{}
    Solve the following equations:
    \Question{$x+5=7$}
      \Answer{$x=2$}
    \Question{$x-5=9$}
      \Answer{$x=14$}
    \Question{$5x=20$}
      \Answer{$x=4$}
\end{ExerciseList}

And I wan the final document look something like this:

Exercise 1
Calculate the following:
a) 7+2 b) 9-9
c) 5+5+5

Exercise 2
Solve the following equations:
a) x+5=7 b) x-5=9 c)
5x=20

Solutions:

Exercise 1
a) 9 b) 0 c) 15

Exercise 2
a) x+5=7 b) x=14 c) x=4

The code that I have above is just an example. The commands do not have to be exactly as I have written as long as the structure is similar.

Best Answer

With the probsoln package, problems can be defined either within the document or in an external file that can be loaded using a command such as \loadallproblems or \loadrandomproblems.

Here's a simple example with the problems defined in the document:

\documentclass{article}

\usepackage{probsoln}

\begin{defproblem}{prob1}% label
  \begin{onlyproblem}% question
    Calculate the following:%

    \begin{textenum}
    \item $7+2$
    \item $9-9$
    \item $5+5+5$
    \end{textenum}
  \end{onlyproblem}%
  \begin{onlysolution}% solution
    \begin{textenum}
    \item $9$
    \item $0$
    \item $15$
    \end{textenum}
  \end{onlysolution}
\end{defproblem}

\begin{defproblem}{prob2}% label
  \begin{onlyproblem}% question
    Solve the following equations:

    \begin{textenum}
    \item $x+5=7$
    \item $x-5=9$
    \item $5x=20$
    \end{textenum}
  \end{onlyproblem}%
  \begin{onlysolution}% solution
    \begin{textenum}
    \item $x=2$
    \item $x=14$
    \item $x=4$
    \end{textenum}
  \end{onlysolution}
\end{defproblem}

\begin{document}
\renewcommand{\labelenumii}{\theenumii)}

\begin{enumerate}
\foreachproblem{\item\thisproblem}
\end{enumerate}

\section*{Solutions}

\showanswers
\begin{enumerate}
\foreachsolution{\item\thisproblem}
\end{enumerate}

\end{document}

This produces

Image of document showing problems and the solutions

The formatting can be changed. For example, using the enumitem package:

\documentclass{article}

\usepackage{probsoln}
\usepackage{enumitem}

\begin{defproblem}{prob1}% label
  \begin{onlyproblem}% question
    Calculate the following:%

    \begin{textenum}
    \item $7+2$
    \item $9-9$
    \item $5+5+5$
    \end{textenum}
  \end{onlyproblem}%
  \begin{onlysolution}% solution
    \begin{textenum}
    \item $9$
    \item $0$
    \item $15$
    \end{textenum}
  \end{onlysolution}
\end{defproblem}

\begin{defproblem}{prob2}% label
  \begin{onlyproblem}% question
    Solve the following equations:

    \begin{textenum}
    \item $x+5=7$
    \item $x-5=9$
    \item $5x=20$
    \end{textenum}
  \end{onlyproblem}%
  \begin{onlysolution}% solution
    \begin{textenum}
    \item $x=2$
    \item $x=14$
    \item $x=4$
    \end{textenum}
  \end{onlysolution}
\end{defproblem}

\newenvironment{ExerciseList}
{\begin{enumerate}[label={\emph{Exercise \arabic*}},%
   ref={\arabic*},wide]
  \renewcommand{\labelenumii}{\theenumii)}%
}
{\end{enumerate}}

\newcommand{\exercise}{\item\mbox{}\par}

\begin{document}

\begin{ExerciseList}
\foreachproblem{\exercise\thisproblem}
\end{ExerciseList}

\section*{Solutions}

\showanswers
\begin{ExerciseList}
\foreachsolution{\exercise\thisproblem}
\end{ExerciseList}

\end{document}

This produces:

Image of resulting document

Edit:

Here's an alternative where you can write the answer next to the question in the code:

\documentclass{article}

\usepackage{probsoln}
\usepackage{enumitem}

\newcommand{\QA}[2]{%
  \begin{onlyproblem}#1\end{onlyproblem}%
  \begin{onlysolution}#2\end{onlysolution}}

\begin{defproblem}{prob1}% label
  \begin{onlyproblem}% question
    Calculate the following:%

  \end{onlyproblem}%
  \begin{textenum}
    \item \QA{$7+2$}{$9$}
    \item \QA{$9-9$}{$0$}
    \item \QA{$5+5+5$}{$15$}
  \end{textenum}
\end{defproblem}

\begin{defproblem}{prob2}% label
  \begin{onlyproblem}% question
    Solve the following equations:

  \end{onlyproblem}%
  \begin{textenum}
    \item \QA{$x+5=7$}{$x=2$}
    \item \QA{$x-5=9$}{$x=14$}
    \item \QA{$5x=20$}{$x=4$}
  \end{textenum}
\end{defproblem}

\newenvironment{ExerciseList}
{\begin{enumerate}[label={\emph{Exercise \arabic*}},%
   ref={\arabic*},wide]
  \renewcommand{\labelenumii}{\theenumii)}%
}
{\end{enumerate}}

\newcommand{\exercise}{\item\mbox{}\par}

\begin{document}

\begin{ExerciseList}
\foreachproblem{\exercise\thisproblem}
\end{ExerciseList}

\section*{Solutions}

\showanswers
\begin{ExerciseList}
\foreachsolution{\exercise\thisproblem}
\end{ExerciseList}

\end{document}

The result is the same as the previous example.

Edit 2:

Here's a method that defines and displays the problem. The label is generated automatically:

\documentclass{article}

\usepackage{probsoln}
\usepackage{enumitem}

\newcommand{\QA}[2]{%
  \begin{onlyproblem}#1\end{onlyproblem}%
  \begin{onlysolution}#2\end{onlysolution}}

\newenvironment{ExerciseList}
{\begin{enumerate}[label={\emph{Exercise \arabic*}},%
   ref={\arabic*},wide]
  \renewcommand{\labelenumii}{\theenumii)}%
}
{\end{enumerate}}

\newcommand{\exercise}{\item\mbox{}\par}

\newcommand{\Exercise}[2]{\exercise 
 #1\par
 \begin{defproblem}{prob\arabic{enumi}}%
  \begin{textenum}%
  #2%
  \end{textenum}%
 \end{defproblem}%
 \useproblem{prob\arabic{enumi}}%
}

\begin{document}

\begin{ExerciseList}
 \Exercise{Calculate the following:}%
 {%
    \item \QA{$7+2$}{$9$}
    \item \QA{$9-9$}{$0$}
    \item \QA{$5+5+5$}{$15$}
 }%

 \Exercise{Solve the following equations:}
 {%
    \item \QA{$x+5=7$}{$x=2$}
    \item \QA{$x-5=9$}{$x=14$}
    \item \QA{$5x=20$}{$x=4$}
 }
\end{ExerciseList}

\section*{Solutions}

\showanswers
\begin{ExerciseList}
\foreachsolution{\exercise\thisproblem}
\end{ExerciseList}

\end{document}