# Turning \answerline into a box in exam class

I've been a big fan of the exam class, and use it often to write tests. I'd like to be able to turn \answerline into a box instead, while retaining the ability to use answers to toggle the solutions. Ideally, I'd also be able to set the width and the height globally, while having the option to override it for a given question. Does anyone have any suggestions?

\documentclass{exam}

\begin{document}
\begin{questions}
\question This is a question. \answerline
\end{questions}
\end{document}

• You might be able to use the built in solutionorbox environment \begin{solutionorbox}[3cm] Test \end{solutionorbox} gives a 3cm tall box to write in or displays a box with Solution: Test inside it. The box width depends on the type of question (main or part etc.). – Jon Jun 4 '19 at 16:59
• @Jon I've tried that, however, it always seems that it produces a full width box below the question itself. I'm looking for something that puts it to the right, on the same line as the question itself. – Antoine Ego Jun 4 '19 at 21:42

I basically copied the definition of \answerline. The box is larger due to the \fboxsep gap on all sides, but I figured it was more important to fit the answer into the given space.

\documentclass{exam}

\makeatletter
% One optional argument, the default value of which is empty.
\ifx\@queslevel\ques@ref
\let\ans@l=\questionlabel
\else
\ifx\@queslevel\part@ref
\let\ans@l=\partlabel
\else
\ifx\@queslevel\subpart@ref
\let\ans@l=\subpartlabel
\else
\ifx\@queslevel\subsubpart@ref
\let\ans@l=\subsubpartlabel
\else
% Oops; no question level defined.
% We must be outide of the questions environment.
% Just leave out the label, I guess:
\def\ans@l{}%
\fi
\fi
\fi
\fi
\hfill
% 2016/05/10: Added \color@begingroup and \color@endgroup:
\color@begingroup
\CorrectChoice@Emphasis \hfil #1\hss
\color@endgroup}}%
\else