[Tex/LaTex] exam: How to make the solution box span the whole text width regardless the question level

examwidth

I would like to make any solution box span the whole text width for any question/part/subpart/subsubpart.

\documentclass{exam}
\renewcommand{\solutiontitle}{\noindent\textbf{\underline{Answer:}}\par\noindent}
\printanswers

\begin{document}

\begin{questions}
\question Some question.

  \begin{parts}
  \part some part
    \begin{solution}
    I need this solution box to span the whole text width.
    \end{solution}

    \begin{subparts}
        \subpart another question
        \begin{solution}
            I need this solution box to span the whole text width.
        \end{solution}
    \end{subparts}

  \end{parts}
\end{questions}
\end{document}

enter image description here

Best Answer

In order to make the solution box as wide as the textwidth, you can enclose it in a \fullwidth{<solution>} command or use the EnvFullwidth environment. Details on this are discussed in the exam documentation:

By default, the solution is printed in a box whose width equals that of the text ofthe current question (or part, or subpart, or subsubpart). That is, the indentation at the left of the solution equals the current level of indentation. You can change this byenclosing the solution, solutionorbox, solutionorlines, solutionordottedlines, or solutionorgrid environment in the argument of a \fullwidth or \uplevel command, or inside of a EnvFullwidth or EnvUplevel environment

The following MWE demonstrates the usage of the \fullwidth command as well as the EnvFullwidth environment:

\documentclass{exam}
\renewcommand{\solutiontitle}{\noindent\textbf{\underline{Answer:}}\par\noindent}
\printanswers


\begin{document}

\begin{questions}
\question The \textbf{fullwidth} approach

  \begin{parts}
  \part some part
   \fullwidth{ \begin{solution}
    I need this solution box to span the whole text width.
    \end{solution}}

    \begin{subparts}
        \subpart another question
      \fullwidth{   \begin{solution}
            I need this solution box to span the whole text width.
        \end{solution}}
    \end{subparts}

  \end{parts}
\end{questions}

\begin{questions}
\question The \textbf{EnvFullwidth} approach

  \begin{parts}
  \part some part
  \begin{EnvFullwidth}
     \begin{solution}
        I need this solution box to span the whole text width.
      \end{solution}
   \end{EnvFullwidth}

    \begin{subparts}
        \subpart another question
        \begin{EnvFullwidth}
          \begin{solution}
            I need this solution box to span the whole text width.
          \end{solution}
        \end{EnvFullwidth}
    \end{subparts}

  \end{parts}
\end{questions}
\end{document}

enter image description here


To apply the changes globally, you could define your own solutions environment as follows:

\documentclass{exam}
\renewcommand{\solutiontitle}{\noindent\textbf{\underline{Answer:}}\par\noindent}
\printanswers


\newenvironment{widesolution}{\begin{EnvFullwidth}\begin{solution}}{\end{solution}\end{EnvFullwidth}}

\begin{document}

\begin{questions}
\question The \textbf{new environment} approach

  \begin{parts}
  \part some part
   \begin{widesolution}
    I need this solution box to span the whole text width.
    \end{widesolution}

    \begin{subparts}
        \subpart another question
     \begin{widesolution}
            I need this solution box to span the whole text width.
        \end{widesolution}
    \end{subparts}

  \end{parts}
\end{questions}
\end{document}

To remove the vertical white space that is present in the two above shown examples when commenting out \printanswers we can use command \ifprintanswers as described in section 8.8 of the manual:

8.8 Changes depending on whether or not solutions are being printed

The command \ifprintanswers is provided in case you want to vary what appears onthe exam in ways other than those provided by the solution environments (see section 8), the \CorrectChoice command in multiple choice environments (see section 5.5), and the optional argument to the \answerline command (see section 7.7). You use this command by typing:

\ifprintanswers

Stuff to appear only when answers are being printed.

\else

Stuff to appear only when answers are not being printed.

\fi

Here is a short MWE that introduces a new environment widesolution that is full width only when \printanswers is used. Otherwise the widesolutions environment is equal to the solutions environment and therefore does not cause any additional white space if \printanswers is not used:

\documentclass[cancelspace]{exam}
\renewcommand{\solutiontitle}{\noindent\textbf{\underline{Answer:}}\par\noindent}
%\printanswers


\ifprintanswers
  \newenvironment{widesolution}{\begin{EnvFullwidth}\begin{solution}}{\end{solution}\end{EnvFullwidth}}
\else
    \newenvironment{widesolution}{\begin{solution}}{\end{solution}}
\fi



\begin{document}

\begin{questions}
\question The \textbf{new environment} approach

  \begin{parts}
  \part some part
   \begin{widesolution}
    I need this solution box to span the whole text width.
    \end{widesolution}

    \begin{subparts}
        \subpart another question
     \begin{widesolution}
            I need this solution box to span the whole text width.
        \end{widesolution}
    \end{subparts}

  \end{parts}
\end{questions}

\end{document}

enter image description here

Related Question