4

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.

4

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}
  • Thanks for your solution. The output is exactly what I want. However, I would like to have the solution immediately following the question in my source code. The reason is that I want to be able to easily move subquestions around. If the answers are in a separate section, I would have to move the answers separately. – ttsc Oct 5 '14 at 15:41
  • @ttsc I've made an edit. Is that what you want? – Nicola Talbot Oct 5 '14 at 15:57
  • Yes. The way you define the QA-command is perfect for me. One more thing: Is it possible to put the questions inside the document environment so that there would be no need for a unique label for each question? – ttsc Oct 5 '14 at 17:36
  • @ttsc I've made another edit. – Nicola Talbot Oct 6 '14 at 10:16
  • Yes! That works for me and it's very nice and clean. Thanks a lot. – ttsc Oct 6 '14 at 16:06
2

Here's a solution using the answers package. You can tinker with the formatting of the answers.

\documentclass[pdftex,12pt]{article}

\usepackage{amsmath}
\usepackage{answers}

\newcommand{\answerFileName}{anexamanswers}
\Newassociation{sol}{Solution}{\answerFileName}
\Opensolutionfile{\answerFileName}

\newenvironment{Exercise}[1]
{\item #1
\begin{enumerate}
}
{
\end{enumerate}
}

\newcommand{\Question}[1]{%
\item #1
}


\begin{document}
\begin{enumerate}

\begin{Exercise}{Calculate the following:}
    \Question{$7+2$}
     \begin{sol}
         $9$
     \end{sol}
    \Question{$9-9$}
      \begin{sol}
          $0$
      \end{sol}
\end{Exercise}

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

\end{enumerate}

\Closesolutionfile{\answerFileName}
\newpage
Answers to Exercises:

\input{\answerFileName}
\end{document}

enter image description here

  • Thanks for answering. Your solution would work for me. If possible, I would prefer to have the answers set up in a way that would not repeat the exercise number with each sub question - so that for example the exercise number 1 would be displayed only once and that would be followed by "a) 9 b) 0" etc... Is it possible to achieve this with the 'answers' package? – ttsc Oct 5 '14 at 15:48
2

Here is a suggestion using the exsheets and tasks packages:

\documentclass{article}
\usepackage{exsheets,tasks}
\SetupExSheets{
  solution/name=Exercise ,
  headings-format=\itshape
}
\begin{document}

\begin{question}
  Calculate the following:
  \begin{tasks}(3)
    \task $7+2$
    \task $9-9$
    \task $5+5+5$
  \end{tasks}
\end{question}
\begin{solution}
  \begin{tasks}(3)
    \task $9$
    \task $0$
    \task $15$
  \end{tasks}
\end{solution}

\begin{question}
  Solve the following equations:
  \begin{tasks}(3)
    \task $x+5=7$
    \task $x-5=9$
    \task $5x=20$
  \end{tasks}
\end{question}
\begin{solution}
  \begin{tasks}(3)
    \task $x=2$
    \task $x=14$
    \task $x=4$
  \end{tasks}
\end{solution}

\section*{Solutions}
\printsolutions

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.