0

I trying to figure out how to print the multiple choice answers of my exam paper dynamically using the tikz package.

This is what I have so far:

\documentclass{exam}

\usepackage{tikz}

\newcounter{solcount}
\setcounter{solcount}{1} 
\newcommand{\CC}{\CorrectChoice\label{sol:\arabic{solcount}}\stepcounter{solcount}}
\newcommand\MCanswers{\section*{\centering {\textbf{{\LARGE Multiple Choice Solutions}}}}
    \foreach \x in {1,...,10}{\centering\noindent \x.~\ref{sol:\x}\par }}

\newcommand{\emptybox}[2][\textwidth]{% Empty Box
    \begingroup
    \setlength{\fboxsep}{-\fboxrule}%
    \noindent\hspace*{-5mm}\framebox[#1]{\rule{0pt}{#2}}%
    \endgroup
}

\begin{document}

\begin{questions}
\question[1] What is $1+1$? 
\begin{choices} \choice 1 \CC 2 \choice 3 \choice 4 \end{choices}

\question[1] What is $2+1$? 
\begin{choices} \choice 1 \choice 2 \CC 3 \choice 4 \end{choices}

\question[1] What is $2+2$? 
\begin{choices} \choice 1 \choice 2 \choice 3 \CC 4 \end{choices}


\end{questions}

\MCanswers
\end{document}

It produces the following: Output 1

The problem is, I don't want to see question 4-10 as ??. I need to be more dynamic such that if I have 3 questions, it will print the solutions to my 3 questions and so on.

I know the problem is here:

\foreach \x in {1,...,10}{\centering\noindent \x.~\ref{sol:\x}\par }}

But I'm not sure how to go about making {1,... \numquestions} work?

4
  • 3
    Replace \foreach \x in {1,...,10} by \foreach \x in {1,...,\the\numexpr\value{solcount}-1\relax}.
    – user272921
    Jul 8 at 2:33
  • P.S. Do not use \section*{\centering.... Instead use a package like titlesec to format the sections.
    – user272921
    Jul 8 at 2:33
  • thanks for that. works like a charm!
    – Alan Jones
    Jul 8 at 3:30
  • 1
    @user272921 - I would argue that using \centering on a title is better than inflicting titlesec on it. Jul 8 at 12:53

1 Answer 1

2

You can simplify the code by incrementing solcount before using it instead of after.

In case you are interested, an alternative to using \label and \ref is \csname ...\endscnane and \xdef. The \expandafter is to allow \csname to expand the macro name before running \xdef. While normally one cannot add numbers to macro names, \csname can use almost anything.

The advantage is that it works on the first run. The disadvantage is that you can't put \MCanswers before the questions.

\documentclass{exam}

\usepackage{tikz}

\newcounter{solcount}% set to 0

\newcommand{\CC}{\CorrectChoice\stepcounter{solcount}%
  \expandafter\xdef\csname sol.\thesolcount\endcsname{\thechoice}}
\newcommand\MCanswers{\section*{\centering{\textbf{{\LARGE Multiple Choice Solutions}}}}
    \foreach \x in {1,...,\thesolcount}{\centering \x.~\csname sol.\x\endcsname\par }}

\newcommand{\emptybox}[2][\textwidth]{% Empty Box
    \begingroup
    \setlength{\fboxsep}{-\fboxrule}%
    \noindent\hspace*{-5mm}\framebox[#1]{\rule{0pt}{#2}}%
    \endgroup
}

\begin{document}

\begin{questions}
\question[1] What is $1+1$? 
\begin{choices} \choice 1 \CC 2 \choice 3 \choice 4 \end{choices}

\question[1] What is $2+1$? 
\begin{choices} \choice 1 \choice 2 \CC 3 \choice 4 \end{choices}

\question[1] What is $2+2$? 
\begin{choices} \choice 1 \choice 2 \choice 3 \CC 4 \end{choices}


\end{questions}

\MCanswers
\end{document}

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.