# Using mdframed after a section heading

The following is a very contrived example, but it illustrates a problem I am unable to solve with the mdframed package. When the mdframed environment follows a section heading but there is insufficient room to place the contents environment, the section heading gets orphaned.

Replacing mdframed with minipage or anything else does not result in the orphan, and changing \clubpenalty has no effect.

The same result occurs, for example, if the environment contains only a single line but has a large value for skipabove or innertopmargin; the non-breakable content I use in the MWE below is just for easily demonstrate the result.

\documentclass{article}

\usepackage{mdframed}

\newcommand{\BoxContents}{top\par\vspace*{2in}bottom}

\begin{document}

\vspace*{5in}

\section{Section}

\begin{mdframed}% this orphans the section heading
\BoxContents
\end{mdframed}

\newpage

\vspace*{5in}

\section{Section}

\begin{minipage}[t]{\linewidth}% this does not orphan the section heading
\BoxContents
\end{minipage}

\end{document}

Is there some way I can use this environment following a section heading without manually breaking pages when this occurs?

-
We had a discuss about the issue: chat.stackexchange.com/transcript/message/8856527#8856527 – Marco Daniel Apr 7 '13 at 14:50

In the chatroom David Carlisle and egreg helped me to point out the issue. Thanks for your engagement.

After \section normally you don't have any breaks avoiding orphans. Normally implies we have exceptions.

To allow colour specification you are buying an unwanted breakpoint. To demonstrate this I use the following example:

\documentclass{article}
\begin{document}
\showoutput\setbox0\vbox{%
\section{Section}

\penalty10000
\begin{minipage}[t]{\linewidth}
top\par\vspace*{2in}bottom
\end{minipage}
}\showbox0
\end{document}

In the log file you will find:

.\write1{\@writefile{toc}{\protect \contentsline {section}{\protect \numberline
\ETC.}
.\penalty 10000
.\glue 9.90276 plus 0.86108
.\penalty 10000
.\glue(\parskip) 0.0 plus 1.0
.\glue(\baselineskip) 5.84921

Now we modify the example in this way that we use mdframed.

\documentclass{article}
\usepackage{mdframed}
\begin{document}
\showoutput\setbox0\vbox{%
\section{Section}

\penalty10000

\begingroup\color{red}
\begin{minipage}[t]{\linewidth}
top\par\vspace*{2in}bottom
\end{minipage}
\endgroup
}\showbox0

\showoutput\setbox0\vbox{
\section{Sectionaa}

\penalty10000
\begin{mdframed}% this orphans the section heading
top\par\vspace*{2in}bottom
\end{mdframed}
}\showbox0
\end{document}

The output in the log file is:

.\write1{\@writefile{toc}{\protect \contentsline {section}{\protect \numberline
\ETC.}
.\penalty 10000
.\glue 9.90276 plus 0.86108
.\penalty 10000
.\rule(0.0+0.0)x345.0
.\pdfcolorstack 0 push {0 g 0 G}
.\glue 0.0
.\glue(\parskip) 0.0
.\hbox(0.0+0.0)x345.0, glue set 345.0fil

You can see a glue 0. There the break happens. I can't avoid the glue 0!. Based on your example here the modification of minipage which results in the same issue:

\documentclass{article}
\usepackage{color}
\begin{document}
\vspace*{5in}

\section{Section}
\begingroup\color{red}
\begin{minipage}[t]{\linewidth}
top\par\vspace*{2in}bottom
\end{minipage}
\endgroup

\end{document}

So the issue based on the color usage.

-
Thanks for the walkthrough. I was going through what you went over in chat, and I tried experimenting with some of the definitions in mdframed.sty. In the definition of the environment mdframed, if I comment out the line \color{mdf@fontcolor}, the \pdfcolorstack item disappears from the log. If I comment out the \mdf@trivlist{\mdf@skipabove@length} then the \glue 0.0 disappears. However the page will still break at that point. So unless I am missing something, I don't think it is the \glue 0.0 mentioned in the chat. – grizzilus Apr 7 '13 at 21:41
@grizzilus: Good point. I will discuss this issue further on. – Marco Daniel Apr 8 '13 at 17:19
Was a solution ever found? – Jim Paris Jun 29 '13 at 16:34
@JimParis: Unfortunately not. I tested several other way where \vspace*{2in} is replaced by test\par Test.... and I couldn't reproduce the issue. – Marco Daniel Jun 29 '13 at 16:53
I find it really easy to reproduce: writelatex.com/250587dmfxhc – Jim Paris Jun 29 '13 at 17:43