# Environment color within mdframed change after page break

I am creating teaching notes with examples and solutions. At the begining of the course, I provide to the students the lecture notes with the examples but blank solutions so they can fill the box during class as I do the example in class. After the class I upload the examples with solutions.

I am planing to do that with the mdframed package. I define my mdframed and write the exercise question, then I add a solution (exsolution) environment within the mdframed which I set the color to be white when I do not want the students to see the solutions or black when I want them to see the solutions.

The problem comes with page breaks where the color in the solution does not follow after the page break. Any feedback?

I am adding an example when I set the solution color \ColourOfSolutions to be white:


\documentclass[12pt]{report}

\usepackage[utf8]{inputenc}
\usepackage[framemethod=TikZ]{mdframed}
\setlength\parskip{\baselineskip}

\usepackage{lipsum}% just to generate text for the example

\newcounter{example}
\renewcommand{\theexample}{\arabic{example}}
\newcommand{\ColourOfSolutions}{white}

\mdfdefinestyle{example}{%
linecolor=black,
outerlinewidth=1.5pt,
%bottomline=false,
%leftline=false,rightline=false,
skipabove=\baselineskip,
skipbelow=\baselineskip,
frametitle=\mbox{},
fontcolor=black
}

\newmdenv[%
style=example,
settings={\global\refstepcounter{example}},
]{example}

\newenvironment{exsolution}{\color{\ColourOfSolutions}}

\begin{document}
\lipsum[1]

\begin{example}
\lipsum[3]

\begin{exsolution}
\lipsum[4]
\lipsum[5]
\end{exsolution}

\end{example}

\end{document}


Please find here the image of what I get. Please check that the solution is white, does not appear, in the first page but appears again (the last part of the solution) in the second page. changing the \ColourOfSolutions to black would illustrate this better:

I have seen many cases of people working with examples and solutions but I have not seen anything similar.

• If you are open to use lualatex, you could use \usepackage{luacolor} and the colour stack will be maintained during pagebreakings – user31729 Sep 22 '15 at 15:18
• There's also pdfcol package, but it does not work (I'll try to figure out) – user31729 Sep 22 '15 at 15:27
• A solution without lualatex would also be nice – Denis Sep 22 '15 at 16:07
• I was using PDFLatex. This is part of a biger document and Yes i can run the whole document it with LuaTex but need to clean it and modify thing that seems not to work nicely with LuaTex. However yes a solution without lualatex would be very nice. – Jose Sep 22 '15 at 17:00
• Are there still no solutions for mdframed? – 71GA Dec 3 '16 at 18:11

Although the question was about mdframed, may be you can consider tcolorbox as an alternative solution. Next code doesn't show an example environment as complete as your one (see update), but a partial example which shows that the color problem doesn't appear with breakable tcolorboxes.

In this case, the example form the upperbox while solutions are the lowerbox. With parameter lowerbox=visible|invisible you can decide if lower box is printed or not.

You don't need to change to lualatex.

\documentclass[12pt]{report}

\usepackage[utf8]{inputenc}
\usepackage[most]{tcolorbox}
\setlength\parskip{\baselineskip}

\usepackage{lipsum}% just to generate text for the example

\newcounter{example}
\renewcommand{\theexample}{\arabic{example}}
\newcommand{\ColourOfSolutions}{white}

\newtcolorbox{example}{enhanced,breakable,lowerbox=invisible,collower=blue}

\begin{document}
\lipsum[1]

\begin{example}
\lipsum[3]

%All solutions are into the lowerbox
\tcblower

\lipsum[4]
\lipsum[5]
\end{example}

\end{document}


With an invisible lower box:

and with a visible one:

Update: Format similar to OP's box

Just to show how to reproduce a format similar to what OP posted with tcolorbox. The code, adapted from example in pages 55-56 in tcolorbox documentation, could be something like:

\documentclass[12pt]{report}

\usepackage[utf8]{inputenc}
\usepackage[most]{tcolorbox}
\setlength\parskip{\baselineskip}

\usepackage{lipsum}% just to generate text for the example

\newcounter{example}
\newtcolorbox[use counter=example]{example}{%
empty,
title={Example \thetcbcounter},
attach boxed title to top left,
boxed title style={empty, size=minimal, left=3mm, top=2mm},
coltitle=black,
fonttitle=\bfseries,
before=\par\smallskip\noindent,
parbox=false,
boxsep=0pt,
left=3mm,
right=3mm,
top=4pt,
breakable,
overlay unbroken={%
\draw[line width=2pt] ([yshift=1pt]title.north west) rectangle
(frame.south east);},
overlay first={%
\draw[line width=2pt]([xshift=1pt]frame.south west) |-
([yshift=1pt]title.north)-|([xshift=-1pt]frame.south east);},
overlay middle={%
\draw[line width=2pt]([xshift=1pt]frame.south west) --
([xshift=1pt]frame.north west);
\draw[line width=2pt]([xshift=-1pt]frame.south east) --
([xshift=-1pt]frame.north east);},
overlay last={%
\draw[line width=2pt]([xshift=1pt]frame.north west) |-
(frame.south)-|([xshift=-1pt]frame.north east);},
vfill before first,
lowerbox=visible,
collower=blue,
}

\begin{document}
\lipsum[4]

\begin{example}
\lipsum[3]
%All solutions are into the lowerbox
\tcblower
\lipsum[4]
\end{example}

\begin{example}
\lipsum[3]
%All solutions are into the lowerbox
\tcblower
\lipsum[4]
\end{example}

\end{document}


• This is cheating ;-) Using a different setup where the colour stack isn't affected ;-) (+1) – user31729 Sep 22 '15 at 19:41
• Many thanks Ignasi! It does work quite nicely and I did adopt it already. I was adapting your initial post to my format before replying... but then I see that you have done it already. Thank you! tcolorbox provides a nice solution in this case. Thanks again – Jose Sep 23 '15 at 15:57

If lualatex is no obstacle \usepackage{luacolor} will preserve the colour stacks. (I changed the colour however to blue to make it more outstanding for this example)

\documentclass[12pt]{report}

\usepackage{luacolor}

\usepackage[utf8]{inputenc}
\usepackage[framemethod=TikZ]{mdframed}
\setlength\parskip{\baselineskip}

\usepackage{lipsum}% just to generate text for the example

\newcounter{example}
\renewcommand{\theexample}{\arabic{example}}
\newcommand{\ColourOfSolutions}{blue}

\mdfdefinestyle{example}{%
linecolor=black,
outerlinewidth=1.5pt,
%bottomline=false,
%leftline=false,rightline=false,
skipabove=\baselineskip,
skipbelow=\baselineskip,
frametitle=\mbox{},
fontcolor=black
}

\newmdenv[%
style=example,
settings={\global\refstepcounter{example}},
]{example}

\newenvironment{exsolution}{\color{\ColourOfSolutions}}

\begin{document}
\lipsum[1]

\begin{example}
\lipsum[3]

\begin{exsolution}
\lipsum[4]
\lipsum[5]
\end{exsolution}

\end{example}

\end{document}