# How to show multi-choice answer key at the end of the document?

I want to show the answer keys at the end of the (multiple-choice) exam page. I have a bank of questions and answers (always the first answer in the bank is assumed to be the right choice), and some macros to shuffle the questions and their answers. All files are attached (here). The output is also shown bellow. the problem is that I cannot show the correct answers at the end of the page.

• What have you done so far? Could you show us a minimal working code to demonstrate? – Steven B. Segletes Dec 22 '14 at 18:51
• I have shared my codes. I can not fill the final answer key. – remo Dec 22 '14 at 19:14
• I would highly recommend against some of the naming conventions you've adopted. For example, you have quite a number of macros named \pgf.... This makes it very hard to figure out what going on since naming in this manner suggests that these are macros which come with the pgf suite. Such personalized macros should be named in a manner to indicate that they are your own. Also, it's just a lot of work for someone else to read through and try to figure out what you're doing. – A.Ellett Dec 22 '14 at 22:55

## 1 Answer

There are various packages out there to help you build exams and quizzes. I'm not really familiar with them because I've built my own. Here's a very simplified version of what I use:

\documentclass{article}

\makeatletter

\newcounter{ae@prob@cnt}
\let\ae@answer@key\relax

\newcommand\problem[1][a]{%%
\stepcounter{ae@prob@cnt}%%
\edef\ae@tmp{\theae@prob@cnt/#1}%%
\ifx\relax\ae@answer@key
\edef\ae@answer@key{\ae@tmp}%%
\else
\edef\ae@answer@key{\ae@answer@key,\ae@tmp}%%
\fi
Problem \theae@prob@cnt\hspace{1em}%%%
}

\newcommand\presentanswerkey{%%
\expandafter\ae@present@answers\ae@answer@key,\relax\@nil}

\def\ae@present@answers#1,#2\@nil{%%
\def\ae@continue{}%%
\ae@parse@pair#1\@nil
\ifx\relax#2
\else
\def\ae@continue{\ae@present@answers#2\@nil}%%
\fi
\ae@continue}

\def\ae@parse@pair#1/#2\@nil{%%
#1\hspace{0.25cm}#2\par}

\makeatother

\begin{document}

\problem[a] This is a question

\problem[b] This is another question

\problem[d] This is the penultimate question

\problem[c] My favorite question.

\presentanswerkey

\end{document}


UPDATE

Here's a modified version of the above:

\makeatletter
\let\ae@answer@key\relax
\newcommand\buildanswerkey[2]{%%
\typeout{===>#1:#2}%%
\edef\ae@tmp{#1/#2}%%
\ifx\relax\ae@answer@key
\xdef\ae@answer@key{\ae@tmp}%%
\else
\xdef\ae@answer@key{\ae@answer@key,\ae@tmp}%%
\fi}

\newcommand\presentanswerkey{%%
\typeout{===>\detokenize\expandafter{\ae@answer@key}}%%
\expandafter\ae@present@answers\ae@answer@key,\relax\@nil}

\def\ae@present@answers#1,#2\@nil{%%
\def\ae@continue{}%%
\ae@parse@pair#1\@nil
\ifx\relax#2
\else
\def\ae@continue{\ae@present@answers#2\@nil}%%
\fi
\ae@continue}

\def\ae@parse@pair#1/#2\@nil{%%
#1\hspace{0.25cm}#2\par}

\makeatother


Save it in a file called buildanswerkey.tex and load it in your commandsPerm.tex file.

In commandsPerm.tex, I've rewritten a few of the lines as follows:

%%-----------------------------------------
\pgfmathrandomitemwithoutreplacement\j{tmp}
\sbox\answera{\ifx\j\correctAnswer * \expandafter\buildanswerkey\expandafter{\thequestion}{a}\fi\ifcase\j\relax\or#1\or#2\or#3\or#4\fi}%
\ifx\j\javab \questionhaspoints{\thequestion = 1} \fi
%%-----------------------------------------
\pgfmathrandomitemwithoutreplacement\j{tmp}
\sbox\answerb{\ifx\j\correctAnswer * \expandafter\buildanswerkey\expandafter{\thequestion}{b}\fi\ifcase\j\relax\or#1\or#2\or#3\or#4\fi}%
\ifx\j\javab \questionhaspoints{\thequestion = 2} \fi
%%-----------------------------------------
\pgfmathrandomitemwithoutreplacement\j{tmp}
\sbox\answerc{\ifx\j\correctAnswer * \expandafter\buildanswerkey\expandafter{\thequestion}{c}\fi\ifcase\j\relax\or#1\or#2\or#3\or#4\fi}%
\ifx\j\javab \questionhaspoints{\thequestion = 3} \fi
%%-----------------------------------------
\pgfmathrandomitemwithoutreplacement\j{tmp}
\sbox\answerd{\ifx\j\correctAnswer * \expandafter\buildanswerkey\expandafter{\thequestion}{d}\fi\ifcase\j\relax\or#1\or#2\or#3\or#4\fi}%
\ifx\j\javab \questionhaspoints{\thequestion = 4} \fi


Then when you compile your file, you get an answer key. I didn't bother to figure out how you intended to create your answer key, but by calling \presentanswerkey before exiting your mcquestions environment, then you'll get a correct key:

• My solution produces the questions from a provided bank and shuffles them. – remo Dec 22 '14 at 19:15
• I have attached my codes. Please check the link. – remo Dec 22 '14 at 19:45
• Unfortunately, it's a lot of code to read through and certainly not all necessary to create a desirable MWE. I'd suggest you pair it all down a bit first to the bare essentials (for example, you don't need the problems to be read from the problem bank). But, it's unclear to me reading through your files how you know what the correct answers is for any given problem. – A.Ellett Dec 22 '14 at 19:49
• I think the problem can be solved within "commandsPerm.tex" lines 144-161. – remo Dec 22 '14 at 19:52
• I assume always the first answer is correct. they are shuffled later. – remo Dec 22 '14 at 19:52