# Constant Solution Spacing in the Exam Documentclass

I'm using the exam documentclass and I would like to keep the spacing between questions the same whether or not the solutions are printed using \printanswers (so it has the effect if the solutions being "written on" the original exam). I've seen these similar questions on this site, but the answers they provide either directly relate to tikz (which I'm not using), or feel contrived and seem to circumvent the correct usage of the solution environment:

The exam class documentation (under section 8.3.3 Advanced customizations) seems to suggest that the only way to maintain the spacing is to edit the TheSolution environment in the exam.cls file (search for \newenvironment{TheSolution}), but this is a bit beyond my abilities. Is there an easy way to maintain the spacing that I am missing? Or is there a simple way to edit the TheSolution environment to make this happen?

Obligatory MWE. The \stretch{1} spacing is eliminated when \printanswers is uncommented.

\documentclass[12pt]{exam}
\usepackage[margin=1in]{geometry}
\unframedsolutions
\renewcommand{\solutiontitle}{}

\begin{document}
\begin{questions}

\question
\begin{solution}[\stretch{1}]
\end{solution}

\question
\begin{solution}[\stretch{1}]
I'm way up on the page if there are solutions.
\end{solution}

\end{questions}
\end{document}

• So it appears that putting \ifprintanswers\vspace{\stretch{1}}\fi after each \end{solution} will work, but there should still be a cleaner solution that this. Oct 3 '15 at 15:37
• There's probably an even cleaner solution, but look at etoolbox to automate yours: \AtEndEnvironment{solution}{\ifprintanswers...\fi}. Oct 3 '15 at 17:34
• @SeanAllred, thanks. That actually helps quite a bit because it can go in the preamble and leave the document body uncluttered. Oct 3 '15 at 17:40

In lieu of other answers, I'll convert my comment into one:

You can use etoolbox to automate your hack:

\AfterEndEnvironment{solution}{\ifprintanswers\vspace{\stretch{1}}\fi}


The answer by @SeanAllred is useful but (I think) limits your questions to the same spacing between all questions. If you want to provide more space for some questions and less space for others, then that solution will not work. For example,

\begin{solution}[\stretch{2}]
\end{solution}

\question
\begin{solution}[\stretch{1}]
I'm way up on the page if there are solutions.
\end{solution}


The first question will have twice the space for an answer on the test than the second question but when \ifprintanswers is true, they will have equal spacing. The questions will not longer align between the test and the answer key.

Here is a solution I use to avoid that problem. It adds the \vspace*{\stretch{1}} at the end of each question but allows you to specify a set height for the space. \vspace will then divide any extra page space among the questions.

I created two options to add space for a question. The first option is a new command called \AnswerBox. It uses a \parbox for answers that do not require paragraph elements. Pass the height dimension to \AnswerBox in the first set of braces, then the answer in the second set. The second option is called \AnswerPage. It is an environment that uses a minipage so you can include paragraph elements. As above, you specify the height in braces after you specify \begin{AnswerPage}. Examples of each are included in the MWE below. Use one or both (or none!) as suits your needs.

Optionally, you can also define a length that will be establish a base minimum height for your answers, here called \basespace. You can then define spaces for answers in multiples of \basespace, such as 2\basespace. Here's the MWE. I welcome suggestions for improving the code.

\documentclass[11pt]{exam}

%% Remove comment %% to print answer key.
\unframedsolutions
\renewcommand{\solutiontitle}{}
\SolutionEmphasis{\bfseries}

\parbox[t][#1]{0.95\textwidth}{%
\begin{solution}#2\end{solution}}
\vspace*{\stretch{1}}
}

{\begin{minipage}[t][#1]{\textwidth}%
\begin{solution}}
{\end{solution}\end{minipage}
\vspace*{\stretch{1}}}

\newlength{\basespace}
\setlength{\basespace}{5\baselineskip}

\begin{document}

\begin{questions}

\question[4]
Here is a question.

}

\question[2]
Write a math formula.

$y = mx + b$
}

\question[4]

Music in the soul can he heard by the universe.
}

\question[4]
Write a haiku.