# large contents in mdframed

I know this might not be an intended use case, but I get odd page breaks with the following MWE (tested with todays version 1.6c as well):

\documentclass{article}

\usepackage{mdframed}

\newcommand{\SmallBox}{%
\begin{minipage}[t]{0.9\linewidth}
top

\vspace*{2.5in}

bottom
\end{minipage}%
}

\newcommand{\BigBox}{%
\begin{mdframed}
\SmallBox

\noindent\SmallBox
\end{mdframed}%
}

\begin{document}

\BigBox

\BigBox

\end{document}


As far as why I am doing something like this: I have some items I would like framed, but sometimes they begin with and/or end with a tikz picture and a bit of accompanying text in a minipage. This material can't split, so I often end up with something similar to the example I posted when my item is near the bottom of a page. I have been forcing them to not break in this case, but I hoped there was a way around that (since which items are near the bottom of a page changes when I add content, so I frequently have to modify which boxes are non-breakable.)

Is there a way around having to do this manually? Is this a bug or am I just using this too far out of context.

-
Sorry but I can't understand the question? If the breaking algorithm works why do you think it's a bug? – Marco Daniel Jun 22 '12 at 17:12
@MarcoDaniel I was trying to say that in version 1.6 and up, ths above example breaks a box when it can fit in the available space on the page. I tested this with 1.5 and 1.6c. In 1.5, the second box is not split and its contents is all on the second page. In 1.6c, the second box is split, the first half on page 2, the second half on page 3, even though there is enough room for the entire contents on page 2. I hope that makes the trouble I am having more clear... – grizzilus Jun 22 '12 at 18:04

\makeatletter
\def\mdf@put@frame@i{%Box must be splitted
\mdf@freepagevspace%gives \mdf@freevspace@length
\dimen@=\the\mdf@freevspace@length\relax%
\dimen@i=\mdf@innertopmargin@length\relax%
\ifdimless{\dimen@}{\dimen@i}%
{\hrule \@height\z@ \@width\hsize%
\vfill\eject%
\def\mdf@reserved@a{\mdf@put@frame}%
}%
{%
outerlinewidth,middlelinewidth,innerlinewidth,%
innertopmargin,splitbottomskip}%
\ifbool{mdf@everyline}%
{%
\ifbool{mdf@bottomline}%
{%
}{}%
}{}%
\notbool{mdf@topline}%
{%
}{}%
\ifdimless{\ht\mdf@splitbox@one+\dp\mdf@splitbox@one}{\dimen@}%
because the last box will be empty\MessageBreak
you have to change it manually\MessageBreak
by changing the text, the space\MessageBreak
or something else}%
}{}%
\setbox\mdf@splitbox@save=\vbox{\unvcopy\mdf@splitbox@one}%
\splitmaxdepth\z@ \splittopskip\mdf@splittopskip@length%
\setbox\mdf@splitbox@two\vsplit\mdf@splitbox@one to \dimen@
\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}{\dimen@}%
{%splitted wrong
\mdf@PackageInfo{Box was splittet wrong^^J
starting loop to iterate
the splitting point\MessageBreak}%
\setbox\mdf@splitbox@one=\vbox{\unvcopy\mdf@splitbox@save}%
\dimen@i=\dimen@%\relax
\@tempcnta=\z@\relax
\loop
\ifdim\dimexpr\ht\mdf@splitbox@two+\dp\mdf@splitbox@two\relax>\dimen@
\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}%
\ifnum\@tempcnta>100
\let\iterate\relax
\mdf@PackageWarning{correct box splittet fails^^J
It seems you are using a non splittable
contents\MessageBreak}
\setbox\mdf@splitbox@one=\vbox{\unvcopy\mdf@splitbox@save}%
\setbox\mdf@splitbox@two=\vbox{}%
\def\mdf@reserved@a{\mdf@put@frame}%
\fi
\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}%
\fi%
\ifdim\wd\mdf@splitbox@two=\wd\mdf@splitbox@one\relax
\else%
\mdf@PackageInfo{You first box width is to small^^J
mdframed fixed it\MessageBreak}%
\setbox\mdf@splitbox@two=\vbox%
{%
\hrule \@height\z@ \@width\wd\mdf@splitbox@one\relax
\unvcopy\mdf@splitbox@two%
}%
\fi%
\ifvoid\mdf@splitbox@two\relax%
{\hrule \@height\f@size pt \@width\z@%
\hrule \@height\z@ \@width\hsize}%
\setbox\mdf@splitbox@one=\vbox{\unvcopy\mdf@splitbox@save}%
\def\mdf@reserved@a{\mdf@put@frame}%
\else%
\ifdimequal{\ht\mdf@splitbox@two}{0pt}%
{\hrule \@height\z@ \@width\hsize%
\vfill\eject%
\setbox\mdf@splitbox@one=\vbox{\unvcopy\mdf@splitbox@save}%
\def\mdf@reserved@a{\mdf@put@frame}%
}%
{%
\begingroup\mdf@@setzref\mdf@putbox@first\endgroup%
\hrule \@height\z@ \@width\hsize%
\vfill\eject%
\def\mdf@reserved@a{\mdf@put@frame@ii}%
}%
\fi%
}%
\mdf@reserved@a%
}
\makeatother

-
wow, you are fast. that works great. I tested it on all my other documents too, and it fixed every bad break i was getting with a figure. thanks so much! – grizzilus Jun 22 '12 at 18:26