# How to box the correct answer in Exam class?

I have tried using the following code:

\documentclass[12pt]{exam}
\CorrectChoiceEmphasis{\mbox{}}
\begin{document}
\begin{questions}
\question Who is an idot?
\begin{choices}
\choice Not the writer of this post
\CorrectChoiceEmphasis The writter of this post
\end{choices}
\end{questions}
\end{document}

-
By box you mean put a frame around the correct answer? –  cgnieder Nov 8 '12 at 9:54
@cgnieder Frame, means to have a border on all 4 sides like a square around the correct answers, then yes. –  MaoYiyi Nov 8 '12 at 9:56

Here are a couple of methods:

The code:

\documentclass[12pt]{exam}
\newcommand{\mycorrectchoice}[1]{\CorrectChoice \fbox{#1}}

\usepackage{tikz}
\usetikzlibrary{calc,shapes}

\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}

\newcommand{\DrawSmartBox}[1][red]{%
\tikz[overlay,remember picture]{
\draw[#1]
($(bl)+(-1.75em,1em)$) rectangle
($(br)+(0.2em,-0.4em)$);}
}

% \MyCorrectSmartChoice:
% #1 optional argument: aspect customization
% #2 mandatory argument: the answer
\newcommand{\MyCorrectSmartChoice}[2][black]{\CorrectChoice \tikzmark{bl}#2\tikzmark{br}\DrawSmartBox[#1]}

\begin{document}
\begin{questions}
\question Who is an idot?
\begin{choices}
\choice Not the writer of this post
\mycorrectchoice{The writter of this post}
\end{choices}
\question Who is an idot?
\begin{choices}
\choice Not the writer of this post
\MyCorrectSmartChoice[thick,rounded corners,red]{The writter of this post}
\end{choices}
\end{questions}

\end{document}


The result:

Notice that the example requires two compilation runs because of the \tikzmark macro; furthermore, this approach allows to customize the aspect of the box, as did in the example.

-
I got emp.tex:0:No file temp.aux. temp.tex:23:Undefined control sequence. \mycorrectchoice{The writter of this post} temp.tex:29:Undefined control sequence. ...rrectSmartChoice{The writter of this post} temp.tex:0: Label(s) may have changed. Rerun to get cross-references right. –  MaoYiyi Nov 8 '12 at 12:24
@MaoYiyi: you should compile twice because of the tikzmark macro. Does it solve the problem? –  Claudio Fiandrino Nov 8 '12 at 13:16
I just push the button to make a pdf. How does one compile? I use Kile to write my latex. –  MaoYiyi Nov 8 '12 at 13:30
Just push it twice and check that your auxiliary file are not deleted between the two runs. :) –  Claudio Fiandrino Nov 8 '12 at 13:37

Here is a modification of Claudio's answer. With this modification, if you don't use the answers option of the exam class the correct answer is not boxed, you can give the exam to your students, the result is:

If the answers option is used (the syntax is

\documentclass[answers]{exam}


) then the correct answers are boxed. The result is:

The code is

\documentclass[12pt]{exam} %Use the answers option to box correct answers

\usepackage{tikz}
\usetikzlibrary{calc,shapes}

\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}

\newcommand{\DrawSmartBox}[1][red]{%
\tikz[overlay,remember picture]{
\draw[#1]
($(bl)+(-1.75em,1em)$) rectangle
($(br)+(0.2em,-0.4em)$);}
}

%Here is the modification
% \MyCorrectSmartChoice:
% #1 optional argument: aspect customization
% #2 mandatory argument: the answer
\newcommand{\MyCorrectSmartChoice}[2][black]{\CorrectChoice \tikzmark{bl}#2\tikzmark{br}\DrawSmartBox[#1]}
\else
\newcommand{\MyCorrectSmartChoice}[2][black]{\CorrectChoice #1}
\fi

\begin{document}
\begin{questions}
\question Who is an idiot?
\begin{choices}
\choice Not the writer of this post
\MyCorrectSmartChoice[thick,rounded corners,red]{The writer of this post}