# Toggle between dotted lines/solution/blank space

I want to learn how to create a command such that I can easily toggle between having dotted lines or solution or even blank space (no space) under my questions.

I know exam class has the answer argument that allows you to display your solution under the question but that only does half the job as it also displays the dotted lines above.

My MWE:

\documentclass[a4paper,answers,12pt]{exam}

\begin{document}
\begin{questions}
\question[1] $1+1$?
\begin{choices} \choice 1\correctchoice 2\choice 3\choice 4\end{choices}
\question[1]
What is $1+1$?
\fillwithdottedlines{2cm}
\begin{solution}
$2$
\end{solution}
\begin{parts}
\part[1] What is $1-1$?
\fillwithdottedlines{2cm}
\begin{solution}
$2$
\end{solution}
\end{parts}
\end{questions}
\end{document}


Ideally, I want to learn how to create a command that can easily toggle between these three states.

• To clarify, the toggle can be applied "globally" (e.g. in the preamble)? Or do you want to be able to toggle each question individually? Jan 26, 2021 at 14:57

Here is one approach.

We define a new " if " dotsolution, using the ifthen package which exam already loads. Then, we store the old definition of \solution (what happens at \begin{solution), and add stuff to it. The stuff we add checks both \ifdotsolution and \ifprintanswers. If \ifprintanswers is true, we skip straight to the solution. If not, we check \dotsolution; if that is true, we print the dots, else we do nothing.

So, to toggle the 3 cases you can: a) use the answers option, b) don't use answers and set \dotsolutionfalse, or c) don't use answers and set \dotsolutiontrue.

Bonus: We add an argument to \begin{solution} for the height of \fillwithdottedlines. But of course, you can remove it and hardcode a value if you prefer.

\documentclass[a4paper,12pt]{exam}

\newif\ifdotsolution
\dotsolutiontrue
\let\osolution\solution
\def\solution#1{%
\ifdotsolution%
\fillwithdottedlines{#1}%
\fi%
\fi%
\osolution%
}

\begin{document}
\begin{questions}
\question[1] $1+1$?
\begin{choices}
\choice 1
\correctchoice 2
\choice 3
\choice 4
\end{choices}
\question[1] What is $1+1$?
\begin{solution}{2cm}
$2$
\end{solution}
\begin{parts}
\part[1] What is $1-1$?
\begin{solution}{2cm}
$2$
\end{solution}
\end{parts}
\end{questions}
\end{document}


• I like the way you modified the \begin{solution} to add \begin{solution}{2cm}. How would i go about modifying the \question[2] to \question[2]{3cm} so that it adds lines at the bottom of that question. Jan 27, 2021 at 3:16
• @AlanJones Can you clarify the purpose of the lines in that case? Based on the current problem, I figured they were space to write the solution. If we attach them to \question, then it will require a different approach. Jan 27, 2021 at 15:28
• See how you modified the the solution environment to include {2cm} at the end which adds the dotted line. I was wondering if we can also modify the \question command to include something like \question[1]{2cm} that will display the mark as well as the dotted lines at the end of the question. I hope that makes sense. Jan 27, 2021 at 20:00
• In effect rather than having to manually type \fillwithdottedlines{2cm} i can simply write \question[2]{2cm} which will display the mark as well as the dottedlines at the end. Jan 27, 2021 at 20:01
• I see. In the current solution, you don't need to type \fillwithdottedlines{2cm}, since it is part of the solution environment. But you want the behaviour without the environment? Since the \question command is actually like an \item, I don't know if there is a hook to print material after the question text. You can consider one of the solutions here that could help achieve this. Jan 27, 2021 at 20:16