# How to prevent mdframed from typesetting a widow line that contains just a bit vspace with colored background?

The code below produces a nice box around an equation or a few lines of text but continues the boxes on the following page with an absurd horizontal rule. Of course I can get rid of this outrageous page break by adding

nobreak=true,


to \newmdenv; but for other, larger boxes I will need page breaks. Therefore I need another solution to fix this undesirable behavior of mdframed.

Here the code example, about as minimal as I could get it:

\documentclass[a4paper,11pt,fleqn]{report}

\setlength{\textheight}{430pt}

\renewcommand{\arraystretch}{1.3}

\usepackage{amsmath}
\usepackage[bold-style=ISO]{unicode-math} % must come after ams and symbols

\usepackage{xcolor}
\usepackage{mdframed}

\newmdenv[
usetwoside=false,
skip$$$$$$$$ab$$$$$$$$ove=3pt minus 1pt plus 3pt,
skipbelow=3pt minus 1pt plus 3pt,
leftmargin=-4pt,
rightmargin=-4pt,
innerleftmargin=2pt,
innerrightmargin=2pt,
innertopmargin=4pt,
innerbottommargin=4pt,
backgroundcolor=\boxcolor!20,
topline=false,
bottomline=false,
linecolor=\boxcolor,
linewidth=2pt,
]{aux*}

\newenvironment{mybox} {\begin{aux*}} {\end{aux*}}

\begin{document}

Mix text and equation to insert lots of glue
$$$$$$$$$$$$$$$$$$$$$$\frac{a}{b}$$$$$$$$$$$$$$$$$$$$$$
Mix text and equation to insert lots of glue
$$$$$$$$$$$$$$$$$$$$$$\frac{a}{b}$$$$$$$$$$$$$$$$$$$$$$
Mix text and equation to insert lots of glue
$$$$$$$$$$$$$$$$$$$$$$\frac{a}{b}$$$$$$$$$$$$$$$$$$$$$$
Mix text and equation to insert lots of glue
$$$$$$$$$$$$$$$$$$$$$$\frac{a}{b}$$$$$$$$$$$$$$$$$$$$$$
Mix text and equation to insert lots of glue
$$$$$$$$$$$$$$$$$$$$$$\frac{a}{b}$$$$$$$$$$$$$$$$$$$$$$
Mix text and equation to insert lots of glue
$$$$$$$$$$$$$$$$$$$$$$\frac{a}{b}$$$$$$$$$$$$$$$$$$$$$$
Mix text and equation to insert lots of glue
$$$$$$$$$$$$$$$$$$$$$$\frac{a}{b}$$$$$$$$$$$$$$$$$$$$$$
and now the problematic box
\def\boxcolor{green}
\begin{mybox}%
$$\l$$$$$$$$ab$$$$$$$$el{Edwba} \sum$$%
\end{mybox}
See just $$$$$$$$ab$$$$$$$$ove the nonsensical line produced by mdframed!
$$$$$$$$ab$$$$$$$$
Mix text and equation to insert lots of glue
$$$$$$$$$$$$$$$$$$$$$$\frac{a}{b}$$$$$$$$$$$$$$$$$$$$$$
Mix text and equation to insert lots of glue
$$$$$$$$$$$$$$$$$$$$$$\frac{a}{b}$$$$$$$$$$$$$$$$$$$$$$
Mix text and equation to insert lots of glue
$$$$$$$$$$$$$$$$$$$$$$\frac{a}{b}$$$$$$$$$$$$$$$$$$$$$$
Mix text and equation to insert lots of glue
$$$$$$$$$$$$$$$$$$$$$$\frac{a}{b}$$$$$$$$$$$$$$$$$$$$$$
Mix text and equation to insert lots of glue
$$$$$$$$ab$$$$$$$$
Mix text and equation to insert lots of glue
$$$$$$$$ab$$$$$$$$
$$$$$$$$ab$$$$$$$$

\def\boxcolor{red}
\begin{mybox}%
Here comes an example with three lines of text in the box.
Three lines is a lot of text bla bla bla bla bla bla blaaa.
Three lines is a lot of text bla bla bla bla bla bla blaaa.
\end{mybox}

See again the scandalous page break.

\end{document}


And here the screenshots of the generated three pages:

--

--

• I guess that the problem is that after equation a new line starts. Can you use the box inside equation? May 1 '15 at 10:59
• The same problem occurs if mybox contains text. Hence the equation environment definitively has nothing to do with the problem. Maybe I should have chosen a text example to present my question in simpler form. May 1 '15 at 11:17
• OK, you can edit the post to add a minimal full code reproducing the problem so we can compile it. May 1 '15 at 11:19
• Please fix the example so it can be run locally to show the problem. Hard to debug otherwise. May 1 '15 at 12:00
• OK, now the question comes with a working example. May 1 '15 at 12:37

The package detects this and warns that the final frame segment at the page break is empty. It does however output the segment, otherwise you would not get the final border of the frame.

here though you only have side borders so (I think, I don't know this code that well) it is safe just to discard the final segment. It seems to work here at least. (See the two places marked CHANGE)

\documentclass[a4paper,11pt,fleqn]{report}

\setlength{\textheight}{430pt}

\renewcommand{\arraystretch}{1.3}

\usepackage{amsmath}
\usepackage[bold-style=ISO]{unicode-math} % must come after ams and symbols

\usepackage{xcolor}
\usepackage{mdframed}

\makeatletter

\def\mdf@put@frame@ii{%
\setlength{\mdf@freevspace@length}{\vsize}%
\ifbool{mdf@repeatframetitle}%
{%repeating title -- more robust using hooks.
\toggletrue{mdf@notfirstframetitle}%
\splitmaxdepth\z@ \splittopskip\z@%
\setbox\mdf@splitbox@one=\vbox{\break\unvbox\mdf@splitbox@one}%
\setbox0=\vsplit\mdf@splitbox@one to \z@\relax%
\setbox\mdf@splitbox@one=\vbox{\unvbox\mdf@splitbox@one}%
\setbox\mdf@splitbox@one\vbox%
{%
\parskip\z@\relax%
\parindent\z@\relax%
\vskip\mdf@frametitle$$$$$$$$ab$$$$$$$$oveskip@length%
\unvcopy\mdf@frametitlebox\relax%
\mdf@@frametitlerule\relax%
\unvbox\mdf@splitbox@one\relax%
}%
\setbox\mdf@splitbox@one=\vbox{\unvbox\mdf@splitbox@one}%
}{}%
\setlength{\dimen@}{\dimexpr\ht\mdf@splitbox@one+\dp\mdf@splitbox@one\relax}%
{innerbottommargin,innerlinewidth,middlelinewidth,outerlinewidth}%
\ifboolexpr { bool {mdf@everyline} and bool {mdf@topline} }%
{%
{innerlinewidth,middlelinewidth,outerlinewidth}%
}{}%
\notbool{mdf@bottomline}%remove bottom if neccessary
{%
{innerlinewidth,middlelinewidth,outerlinewidth}%
}{}%
\ifdimgreater{\dimen@}{\mdf@freevspace@length}%
{%have a middle box
\ifbool{mdf@everyline}%regard top and bottom if everyline is used
{%
\ifbool{mdf@topline}%
{%
{innerlinewidth,middlelinewidth,outerlinewidth}%
}{}%
\ifbool{mdf@bottomline}%
{%
{innerlinewidth,middlelinewidth,outerlinewidth}%
}{}%
}{}%
\setbox\mdf@splitbox@save=\vbox{\unvcopy\mdf@splitbox@one}%
\splitmaxdepth\z@ \splittopskip\mdf@splittopskip@length%
\setbox\mdf@splitbox@two\vsplit\mdf@splitbox@one to \mdf@freevspace@length%
\setbox\mdf@splitbox@two\vbox{\unvbox\mdf@splitbox@two}%
\setbox\mdf@splitbox@one\vbox{\unvbox\mdf@splitbox@one}%
\ifdimgreater{\ht\mdf@splitbox@two+\dp\mdf@splitbox@two}%
{\mdf@freevspace@length}%
{%splitted wrong
\mdf@PackageInfo{Box was splittet wrong^^J
starting loop to iterate
the splitting point\MessageBreak}%
\dimen@i=\mdf@freevspace@length%\relax
\@tempcnta=\z@\relax
\loop
\ifdim\dimexpr\ht\mdf@splitbox@two+\dp\mdf@splitbox@two\relax>%
\mdf@freevspace@length\relax
\ifnum\@tempcnta>100
\let\iterate\relax%
\mdf@PackageWarning{correct box splittet fails^^J
It seems you are using a non splitt$$$$$$$$ab$$$$$$$$le
contents\MessageBreak}%
\fi
\setbox\mdf@splitbox@one=\vbox{\unvcopy\mdf@splitbox@save}%
\splitmaxdepth\z@ \splittopskip\mdf@splittopskip@length%
\setbox\mdf@splitbox@two\vsplit\mdf@splitbox@one to \dimen@i\relax%
\setbox\mdf@splitbox@two\vbox{\unvbox\mdf@splitbox@two}%
\setbox\mdf@splitbox@one\vbox{\unvbox\mdf@splitbox@one}%
\repeat%
}{}%
\ifvoid\mdf@splitbox@one\relax%
\mdf@PackageWarning{You got a bad break because the splittet box is
empty^^J
You have to change the page settings^^J
like enlargethispage or something else^^J
the package increases do
\enlargethispage{\baselineskip}\MessageBreak}%
\setbox\mdf@splitbox@one=\vbox{\unvcopy\mdf@splitbox@save}%
\enlargethispage{\baselineskip}%
\def\mdf@reserved@a{\mdf@put@frame@ii}%
\else
\setbox\mdf@splitbox@two=\vbox%
{%
\hrule \@height\z@ \@width\mdfboundingboxwidth\relax%
\unvcopy\mdf@splitbox@two%
}%
\begingroup%
\mdf@@setzref%
\booltrue{mdfmiddleframe}%
\mdf@beforebreak%
\mdf@putbox@middle%
\mdf@afterbreak%
\endgroup%
\hrule \@height\z@ \@width\hsize%
\vfill\eject%
\def\mdf@reserved@a{\mdf@put@frame@ii}%
\fi
}%End middle box case
{%start last box case
\ifvoid\mdf@splitbox@one
\mdf@PackageWarning{You got a bad break\MessageBreak
because the last split box is empty\MessageBreak
You have to change the settings1}%%
%CHANGE:  leave final box as void
\iffalse
\setbox\mdf@splitbox@one=\vbox%
{%
\unvbox\mdf@splitbox@one%
\hrule \@height\z@ \@width\mdfboundingboxwidth
}%
\fi
%end CHANGE
\fi%
\ifdimless{\ht\mdf@splitbox@one}{1sp}%
{%
\mdf@PackageWarning{You got a bad break\MessageBreak
because the last split box is empty\MessageBreak
You have to change the settings2}%

\let\mdf@reserved@a\relax%
\iffalse
\setbox\mdf@splitbox@one=\vbox%
{%
\unvbox\mdf@splitbox@one%
\hrule \@height\z@ \@width\mdfboundingboxwidth
}%
\fi
%END CHANGE
\setbox\mdf@splitbox@one\box\voidb@x
}{}%
\begingroup%
\toggletrue{mdflastframe}%
\mdf@beforebreak%
\begingroup%
\mdf@@setzref%
\booltrue{mdflastframe}%
\mdf@beforelastframe%
\mdf@putbox@second%
\mdf@afterlastframe%
\endgroup%
\hrule \@height\z@ \@width\hsize%
\mdf@afterbreak%
\endgroup%
\let\mdf@reserved@a\relax%
}%
\mdf@reserved@a%
}

\makeatother

\newmdenv[
usetwoside=false,
skip$$$$$$$$ab$$$$$$$$ove=3pt minus 1pt plus 3pt,
skipbelow=3pt minus 1pt plus 3pt,
leftmargin=-4pt,
rightmargin=-4pt,
innerleftmargin=2pt,
innerrightmargin=2pt,
innertopmargin=4pt,
innerbottommargin=4pt,
backgroundcolor=\boxcolor!20,
topline=false,
bottomline=false,
linecolor=\boxcolor,
linewidth=2pt,
]{aux*}

\newenvironment{mybox} {\begin{aux*}} {\end{aux*}}

\begin{document}

Mix text and equation to insert lots of glue
$$$$$$$$$$$$$$$$$$$$$$\frac{a}{b}$$$$$$$$$$$$$$$$$$$$$$
Mix text and equation to insert lots of glue
$$$$$$$$$$$$$$$$$$$$$$\frac{a}{b}$$$$$$$$$$$$$$$$$$$$$$
Mix text and equation to insert lots of glue
$$$$$$$$$$$$$$$$$$$$$$\frac{a}{b}$$$$$$$$$$$$$$$$$$$$$$
Mix text and equation to insert lots of glue
$$$$$$$$$$$$$$$$$$$$$$\frac{a}{b}$$$$$$$$$$$$$$$$$$$$$$
Mix text and equation to insert lots of glue
$$$$$$$$$$$$$$$$$$$$$$\frac{a}{b}$$$$$$$$$$$$$$$$$$$$$$
Mix text and equation to insert lots of glue
$$$$$$$$$$$$$$$$$$$$$$\frac{a}{b}$$$$$$$$$$$$$$$$$$$$$$
Mix text and equation to insert lots of glue
$$$$$$$$$$$$$$$$$$$$$$\frac{a}{b}$$$$$$$$$$$$$$$$$$$$$$
and now the problematic box
\def\boxcolor{green}
\begin{mybox}%
$$\l$$$$$$$$ab$$$$$$$$el{Edwba} \sum$$%
\end{mybox}
See just $$$$$$$$ab$$$$$$$$ove the nonsensical line produced by mdframed!
$$$$$$$$ab$$$$$$$$
Mix text and equation to insert lots of glue
$$$$$$$$$$$$$$$$$$$$$$\frac{a}{b}$$$$$$$$$$$$$$$$$$$$$$
Mix text and equation to insert lots of glue
$$$$$$$$$$$$$$$$$$$$$$\frac{a}{b}$$$$$$$$$$$$$$$$$$$$$$
Mix text and equation to insert lots of glue
$$$$$$$$$$$$$$$$$$$$$$\frac{a}{b}$$$$$$$$$$$$$$$$$$$$$$
Mix text and equation to insert lots of glue
$$$$$$$$$$$$$$$$$$$$$$\frac{a}{b}$$$$$$$$$$$$$$$$$$$$$$
Mix text and equation to insert lots of glue
$$$$$$$$ab$$$$$$$$
Mix text and equation to insert lots of glue
$$$$$$$$ab$$$$$$$$
$$$$$$$$ab$$$$$$$$

\def\boxcolor{red}
\begin{mybox}%
Here comes an example with three lines of text in the box.
Three lines is a lot of text bla bla bla bla bla bla blaaa.
Three lines is a lot of text bla bla bla bla bla bla blaaa.
\end{mybox}

See again the scandalous page break.

\end{document}