Here's a variation on my earlier answer. In this version, the choices aren't part of any environment; you just start listing \choice
s and \correctchoice
s after the question.
Here's a complete LaTeX file that illustrates it.
\documentclass{article}
%--------------------------------------------------------------------
\newcounter{question}
\newif\ifinchoices
\inchoicesfalse
\newenvironment{questions}{%
\list{\thequestion.}%
{%
\usecounter{question}%
\def\question{\inchoicesfalse\item}%
\settowidth{\leftmargin}{10.\hskip\labelsep}%
\labelwidth\leftmargin\advance\labelwidth-\labelsep
}%
}
{%
\endlist
}%
\newcounter{choice}
\renewcommand\thechoice{\Alph{choice}}
\newcommand\choicelabel{\thechoice.}
\def\choice{%
\ifinchoices
% Do nothing
\else
\startchoices
\fi
\refstepcounter{choice}%
\ifnum\value{choice}>1\relax
\penalty -50\hskip 1em plus 1em\relax
\fi
\choicelabel
\nobreak\enskip
}% choice
\def\CorrectChoice{%
\choice
\addanswer{\thequestion}{\thechoice}%
}
\let\correctchoice\CorrectChoice
\newcommand{\startchoices}{%
\inchoicestrue
\setcounter{choice}{0}%
% \par % Uncomment this to have choices always start a new line
% If we're continuing the paragraph containing the question,
% then leave a bit of space before the first choice:
\ifvmode\else\enskip\fi
}%
\newbox\allanswers
\setbox\allanswers=\hbox{}
\newcommand{\addanswer}[2]{%
\global\setbox\allanswers=\hbox{\unhbox\allanswers #1.~#2\quad}%
}
\newcommand{\showanswers}{%
\vfill
\begin{center}
Answers
\end{center}
\noindent\unhbox\allanswers
}
%--------------------------------------------------------------------
\begin{document}
\begin{questions}
\question What was the color of George Washington's white horse?
\choice Blue
\choice Yellow
\correctchoice White
\choice Black
\question Which of these things doesn't fit in?
\choice John
\choice Paul
\choice George
\choice Ringo
\correctchoice Socrates
\question Who's buried in Grant's tomb?
\choice Washington
\choice Mickey Mouse
\correctchoice Grant
\end{questions}
\showanswers
\newpage
\begin{questions}
\question $1+1= \mathord{?}$
\choice $0$ \choice $1$ \correctchoice $2$ \choice $3$ \choice $4$
\question $\sin \frac{\pi}{2} = \mathord{?}$
\choice $0$ \correctchoice $1$ \choice $2$ \choice $\frac{\pi}{2}$
\question What was the color of the bus driver's eyes?
\choice Green \choice Yellow \correctchoice Blue \choice Red
\end{questions}
\showanswers
\end{document}
Your question is a bit confusing: you refer to the exercise
package but are using commands in your code snippets that are defined by the exam
class... Then you're talking about a book with a separate answers section but also say
In the answer sheet, I like to [...]
Since I now don't know what package/class you want to use and you never really answered my comment if you're bound to a specific package, and since it is also not really clear if you want to design a book or a exercise sheet/answer sheet combination (or maybe both?) I'll give a solution for the exam
class creating sheets and one for the exsheets
package for use in a textbook.
Using the exam
class
The exam
class obviously is not meant to create a textbook. But exercise/answer sheets can be created with it. Remove or add the class option answers
to see the difference:
\documentclass[answers]{exam}
\begin{document}
\begin{questions}
\question This is the first question.
\ifprintanswers\emph{Info:} Correct choices are marked bold.\fi
\begin{checkboxes}
\choice Wrong answer
\correctchoice This is a correct answer
\correctchoice This is another answer
\choice Wrong answer
\end{checkboxes}
\question This is the second question.
\ifprintanswers\emph{Info:} Correct choices are marked bold.\fi
\begin{oneparcheckboxes}
\correctchoice True
\choice False
\correctchoice True
\choice false
\end{oneparcheckboxes}
\end{questions}
\end{document}
Using the exsheets
package
The exsheets
package can be used to create exams, exercise/answer sheets or exercises in textbooks. The following example will show how to add exercises and solutions and print the solutions in a separate section or chapter.
Using enumitem
\documentclass{article}
\usepackage{exsheets}
\SetupExSheets{counter-format=se.qu}
% due to a bug in versions >0.3a:
\providecommand*\checkedchoicebox{\ckeckedchoicebox}
\usepackage[inline]{enumitem}
\newlist{choices}{itemize}{1}
\newlist{choices*}{itemize*}{1}
\setlist[choices*]{itemjoin=\qquad}
\newcommand*\choice{\item[\choicebox]}
\newcommand*\correctchoice{\PrintSolutionsTF{\item[\checkedchoicebox]}{\item[\choicebox]}}
\begin{document}
\section{Exercises}
\begin{question}\label{qu:one}
This is the first question.
\begin{choices}
\choice Wrong answer
\correctchoice This is a correct answer
\correctchoice This is another answer
\choice Wrong answer
\end{choices}
\end{question}
\begin{solution}
This is the first question.
\begin{choices}
\choice Wrong answer
\correctchoice This is a correct answer
\correctchoice This is another answer
\choice Wrong answer
\end{choices}
You find the question on page~\pageref{qu:one}.
\end{solution}
\begin{question}\label{qu:two}
This is the second question.
\begin{choices*}
\correctchoice True
\choice False
\correctchoice True
\choice false
\end{choices*}
\end{question}
\begin{solution}
This is the second question.
\begin{choices*}
\correctchoice True
\choice False
\correctchoice True
\choice false
\end{choices*}
\noindent You find the question on page~\pageref{qu:two}.
\end{solution}
\section{Solutions}
\printsolutions
\end{document}
With >v0.3a of exsheets
The next update of exsheets
will not only fix a few issues such as the \ckeckedchoicebox
but also provide a possibility without enumitem
. A working version can be downloaded here, the update to CTAN will have to wait a few days, though.
\documentclass{article}
\usepackage{exsheets}
\SetupExSheets{counter-format=se.qu}
\NewTasks[style=multiplechoice]{choices}[\choice]
\newcommand*\correct{\checkedchoicebox}
\begin{document}
\section{Exercises}
\begin{question}\label{qu:one}
This is the first question.
\begin{choices}
\choice Wrong answer
\choice This is a correct answer
\choice This is another answer
\choice Wrong answer
\end{choices}
\end{question}
\begin{solution}
This is the first question.
\begin{choices}
\choice Wrong answer
\choice[\correct] This is a correct answer
\choice[\correct] This is another answer
\choice Wrong answer
\end{choices}
You find the question on page~\pageref{qu:one}.
\end{solution}
\begin{question}\label{qu:two}
This is the second question.
\begin{choices}{4}
\choice True
\choice False
\choice True
\choice false
\end{choices}
\end{question}
\begin{solution}
This is the second question.
\begin{choices}{4}
\choice[\correct] True
\choice False
\choice[\correct] True
\choice false
\end{choices}
\noindent You find the question on page~\pageref{qu:two}.
\end{solution}
\section{Solutions}
\printsolutions
\end{document}
Best Answer
Here's a bit of a hack using the
endnotes
package. I tried to inline some comments.And here's the output:
Edit:
In response to zar's comment below, if we want the input syntax to be
instead of
one can change the definition to
But this way you have to put the choice text in braces, for example, you must write
Edit 2: S.G. and zar noticed some problems with my abuse of the
\edef
function. A more complicated, but functionally better hack is the following (I'm including the full file below for easy of copy-pasting)and this is what it outputs
which as one can see correctly handles the
\emph
command and math (both displayed and inline) environments.