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}
Here's a bit of a hack using the endnotes
package. I tried to inline some comments.
\documentclass{article}
\usepackage{endnotes}
% Define two new counters for ease of using.
\newcounter{questionnumber}
\newcounter{choicenumber}[questionnumber]
% Formatting the counters, this is where you change how the counters appear.
\renewcommand*\thequestionnumber{\arabic{questionnumber}.}
\renewcommand*\thechoicenumber{\alph{choicenumber})}
% Define the \question and \choice commands to be similar to what is
% given in the OP
\newcommand*\question{\item}
\newcommand*\choice{\item}
% Here's a bit of a hack: \endnotetext stores the \meaning of the its argument
% in the endnotetext file, so the macros aren't expanded. I use \edef to fully
% expand the current \thechoicenumber, and use \expandafter to stuff it into
% the argument for \endnotetext. Suggestions for improvements are welcome!
\newcommand*\entreplace[1]{\endnotetext[\value{questionnumber}]{#1}}
\makeatletter
\newcommand\truechoice[1]{\item \protected@edef\currentcount{\thechoicenumber~~#1} \expandafter\entreplace\expandafter{\currentcount}}
\makeatother
% Define a choices environment, just a list basically.
\newenvironment{choices}{\begin{list}{\thechoicenumber}{\usecounter{choicenumber}}}{\end{list}}
% These are to set up the endnotes. The first makes the endnote marks look
% like the question numbering (as opposed to being in superscript). The second
% sets the endnotes heading to read "Answers".
\renewcommand*\makeenmark{\theenmark.~~}
\renewcommand*\notesname{Answers:}
\begin{document}
\begin{list}{\thequestionnumber}{\usecounter{questionnumber}}
\question Some question
\begin{choices}
\choice False choice
\choice Bad choice
\truechoice{This is true for no good reason} True choice.
\end{choices}
\question Who won the 2012 Presidential Election in US?
\begin{choices}
\choice Mitt Romney
\truechoice{Without even counting Florida} Barack Obama
\end{choices}
\end{list}
% Print the "answers"
\theendnotes
\end{document}
And here's the output:
Edit:
In response to zar's comment below, if we want the input syntax to be
\truechoice{Choice text}{Justification/commentary}
instead of
\truechoice{Justification/commentary} Choice text
one can change the definition to
\newcommand\truechoice[2]{\item #1 \protected@edef\currentcount{\thechoicenumber~~#2} \expandafter\entreplace\expandafter{\currentcount}}
But this way you have to put the choice text in braces, for example, you must write
\truechoice{True Choice!}{That choice was true for no good reason}
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)
\documentclass{article}
\usepackage{endnotes}
% Define two new counters for ease of using.
\newcounter{questionnumber}
\newcounter{choicenumber}[questionnumber]
% Formatting the counters, this is where you change how the counters
% appear.
\renewcommand*\thequestionnumber{\arabic{questionnumber}.}
\renewcommand*\thechoicenumber{\alph{choicenumber})}
% Define the \question and \choice commands to be similar to what is
% given in the OP
\newcommand*\question{\item}
\newcommand*\choice{\item}
% Here's a bit of a hack: \endnotetext stores the \meaning of the its argument
% in the endnotetext file, so the macros aren't expanded. I use \edef to fully
% expand the current \thechoicenumber, and use \expandafter to stuff it into
% the argument for \endnotetext. Suggestions for improvements are welcome!
\newcommand*\entreplace[1]{\endnotetext[\value{questionnumber}]{#1}}
\newcommand\truechoice[2]{\item #1 \edef\tempchoice{\thechoicenumber} \expandafter\def\expandafter\currentcount\expandafter{\tempchoice \ \ #2} \expandafter\entreplace\expandafter{\currentcount}}
% Define a choices environment, just a list basically.
\newenvironment{choices}{\begin{list}{\thechoicenumber}{\usecounter{choicenumber}}}{\end{list}}
% These are to set up the endnotes. The first makes the endnote marks look
% like the question numbering (as opposed to being in superscript). The second
% sets the endnotes heading to read "Answers".
\renewcommand*\makeenmark{\theenmark.~~}
\renewcommand*\notesname{Answers:}
\begin{document}
\begin{list}{\thequestionnumber}{\usecounter{questionnumber}}
\question Some question
\begin{choices}
\choice False choice
\choice Bad choice
\truechoice{True choice}{This is true for no good reason $1+1$. Let me
add more text. There should be four or five lines. Can I put displayed
math in here? \emph{test} \[ E = mc^2\] Some more text.}
\end{choices}
\end{list}
% Print the "answers"
\theendnotes
\end{document}
and this is what it outputs
which as one can see correctly handles the \emph
command and math (both displayed and inline) environments.
Best Answer
Here's one possible solution using a
longtable
environment and the method described by Herbert in his answer to How to programmatically make tabular rows using `\whiledo` ? to iteratively build the rows. I defined a\CheckTable
command with a mandatory argument which builds your table with the number of rows specified in the argument: