2

I've been playing around with a custom "CorrectChoice" command to add referencing to the answers. Specifically, with this setup, I can reference the correct choice and its corresponding answer easily else where in the text:

\documentclass[addpoints,12pt]{exam}
\usepackage{amsmath}

\makeatletter
\newcommand\CC[1]{%
  \def\@currentlabel{#1}\label{anst:\thequestion}%
  \CorrectChoice\label{ans:\thequestion} #1
}
\makeatother


\begin{document}
\begin{questions}
\question This is a question

\begin{choices}
\choice 8
\CC 1
\choice 3
\choice 4
\end{choices}

\end{questions}

The correct answer for question 1 is choice \ref{ans:1}, \ref{anst:1}

\end{document}

The above code works wonderfully. The problem is when the argument given to CC is inline math i.e in the form $...$

For example

...
CC $\frac{3}{2}$
...

I receive a compile error: ! Missing $ inserted. So I figured I am not handling the arguments passed to the newcommand correctly. I came across \tags at one point when doing some research, but couldn't wrap my head around it.

0

1 Answer 1

1

\choice just issues \item and does some checks.

Your call should be \CC{$\frac{3}{2}$} because you want the text to be grabbed as argument to \CC.

In the case of \CC 1, the rules of TeX make it grab 1 as the argument; but \CC 12 would not work.

\documentclass[addpoints,12pt]{exam}
\usepackage{amsmath}

\makeatletter
\newcommand\CC[1]{%
  \def\@currentlabel{#1}\label{anst:\thequestion}%
  \CorrectChoice\label{ans:\thequestion} #1
}
\makeatother


\begin{document}
\begin{questions}
\question This is a question

\begin{choices}
\choice 8
\CC{$\frac{3}{2}$}
\choice 3
\choice 4
\end{choices}

\end{questions}

The correct answer for question 1 is choice \ref{ans:1}, \ref{anst:1}

\end{document}

enter image description here

2
  • That makes complete sense! How does item know how many of the arguments to capture? Does each argument default to one character (thats why 1 is counted, but 12 wouldnt work)? Commented Mar 10, 2020 at 22:15
  • 1
    @JustinDalrymple \item does not grab arguments. It outputs the item number (a letter, in this case) and then does nothing else (well, it does something else, but it's not relevant here); the following text is simply typeset.
    – egreg
    Commented Mar 10, 2020 at 22:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .