I cannot figure out the reason for this weird occurrence. If I declare my exam to print answers, I get an error that
! LaTeX Error: \begin{choices} on input line 21 ended by \end{questions}
! LaTeX Error: \begin{questions} on input line 20 ended by \end{document}.
This is fixed, if I simply add a second line of
\end{choices}
To my newcommand. If I am not printing answers, the second \end{choices}
is not required.
What am I writing improperly for the loop that is making it ignore the first \end{choices}
?
I have included all the lines needed in my MWE, but I have commented out the print answers part and the 2nd \end{choices}
so you can see it compile and work properly as well.
% to see the error in action, comment this first documentclass command, and uncomment the one with [answers]
% to resolve the error generated when enabling the answers, uncomment the second \end{choices} command down in \newcommand{\newmpchoice}
\documentclass{exam}
%\documentclass[answers]{exam}
\usepackage{pgffor}
\newcommand{\printchoice}[3]{\ifnum\numexpr#1\relax=\numexpr#2\relax \CorrectChoice #3 \else \choice #3\fi}
\newcommand{\newmpchoice}[4]{
\question #1
\begin{choices}
\foreach \x[count=\xi] in {#3}
{ \printchoice{#2}{\xi}{\x}};
\end{choices}
%\end{choices}
\begin{solution}
#4
\end{solution}
}
\begin{document}
\begin{questions}
\newmpchoice{Test question}{1}{{Answer 1},{Answer 2},{Answer 3},{Answer 4},{Answer 5}}{Solution}
\end{questions}
\end{document}
\end{choice}
works fine here. I get the error you describe if I add two of them. Unless I am misinterpreting something here.\end{choice}
in it. I've added a comment to the top of the code to make that more cleartexdoc exam
, or ctan.org/pkg/exam ), section **3.2 The option answers ** which specifies some changes. The problem has something to to with\CorrectChoice
, but I cannot look into it further right now.