5

New edit: The proposed answer by magula, which used to work with version 2.5 of exam.cls no longer works with the current version 2.603. The following was included as a note for version 2.601: "We changed command and environment names in the code from framed.sty that's included (slightly modified) in exam.cls so that the user can say \usepackage{framed} without creating conflicts. This also allows the user to use packages, such as minted.sty, that load framed.sty." I'm not sure if the changes made now render the current given answer/hack useless, but it simply deosn't work anymore.

Question: Can TheSolution environment be renewed in such a way that the framed box provided for the solution does not break into separate boxes on different pages as also illustrated in this question.

The code

\documentclass[answers]{exam}
\usepackage{lipsum}
\usepackage{mdframed}

\def\MakeFramed#1{\begin{mdframed}}
\def\endMakeFramed{\end{mdframed}}

\begin{document}

\begin{questions}

\question A short question.
\begin{solution}
A short solution.
\end{solution}

\question A question with a long solution.

\begin{solution}
The long solution continues onto another page: \lipsum[1-8]
\end{solution}

\end{questions}
\end{document}

produces a page break that looks like

enter image description here

instead of the following desired look:

enter image description here

In the manual, on page 74, it mentions that one could use \renewenvironment to completely customize the solution environment by changing the definition of TheSolution. My question is how I could do this to make the frame extend onto the next page in the desired way indicated above and the linked to question at the beginning of this question.

5
  • @AGoldMan I'm using MiKTeX 2.9 if that helps. Also, I assume you have the latest version of exam.cls installed? It was rather recently updated. It could very easily have something to do with my distribution I suppose, but I find that odd. Commented Jan 28, 2018 at 12:28
  • Apologies, didn't realize I had an old version. New solution is up.
    – A Gold Man
    Commented Jan 28, 2018 at 14:25
  • @AGoldMan Ah, yes, that would explain it--still have a problem concerning vertical spacing between questions and solution boxes though. It shrinks drastically with the proposed modification. Commented Jan 28, 2018 at 14:29
  • Give me some more time, i'll give you a nicer implementation
    – A Gold Man
    Commented Jan 28, 2018 at 17:56
  • @AGoldMan The spacing is fine now, I think. Is that what you were referring to? Commented Jan 28, 2018 at 18:02

3 Answers 3

1
+50

With the new version, the commands you need to change to remimplement the original solution are exam@MakeFramed and endexam@MakeFramed

So you should change

\def\MakeFramed#1{\begin{mdframed}}
\def\endMakeFramed{\end{mdframed}}

to

\makeatletter
\def\exam@MakeFramed#1{\begin{mdframed}}
\def\endexam@MakeFramed{\end{mdframed}}
\makeatother

This is a classic example of namespacing in TeX, where private macros have their name prefixed with the name of the package they belong to.

1
  • Oh how infuriating! But also encouraging...I read through the exam.cls code and tried exactly what you provided in your answer, but I forgot the all important \makeatletter and makeatother commands. :( I mean hey...I'm learning from this though! Also useful for anyone else who stumbles across this answer...the frames look a little messed up when viewed zoomed out in Adobe, but \usepackage[framemethod=tikz]{mdframed} fixes this as noted in Heiko's answer here. Thanks for the answer and impressing upon me the need to remember make... Commented Jan 28, 2018 at 14:46
5

Just redefining TheSolution environment to be mdframed seems to work. You can add the heading "Solution:" and provide extra vertical spacing to match the original style too:

Sample output

with page breaks like

Sample break

\documentclass[answers]{exam}
\usepackage{lipsum}
\usepackage{mdframed}

\renewenvironment{TheSolution}{\begin{mdframed}[skipabove=\baselineskip,innertopmargin=\baselineskip,innerbottommargin=\baselineskip]
  \textbf{Solution:}\enspace\ignorespaces}{\end{mdframed}}

\begin{document}

\begin{questions}

\question A short question.
\begin{solution}
A short solution.
\end{solution}

\question A question with a long solution.

\begin{solution}
The long solution continues onto another page: \lipsum[1-8]
\end{solution}

\end{questions}
\end{document}
2
  • I have now added some code to adjust the styling. Commented Jan 28, 2018 at 14:49
  • Thanks a lot for this! It's nice to know another approach for going about fixing this perceived problem. (At least I don't think multiple boxes across several pages is good.) Thanks again! Commented Jan 28, 2018 at 14:50
2
\usepackage{mdframed}

\def\MakeFramed#1{\begin{mdframed}}
\def\endMakeFramed{\end{mdframed}}

This simply replaces the commands TheSolution uses at start and end of the content with the mdframed ones.

3
  • See tex.stackexchange.com/questions/124539/…. Using \usepackage[framemethod=tikz]{mdframed} doesn't remove the problem, but might make it lesser. If that's fine, you could also try just making the lines thicker. (e.g. \begin{mdframed}[linewidth=1pt]). Both together should render a good result.
    – magula
    Commented Sep 14, 2017 at 11:25
  • You could also try to find out how fbox draws the lines and why that doesn't render this problem.
    – magula
    Commented Sep 14, 2017 at 11:29
  • Months ago I see a setting in Acrobat Reader that changes the width of thin lines. Another solution might be to use wider lines for the frames. The same thing happens with tables rules.
    – djnavas
    Commented Nov 14, 2017 at 8:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .