[Tex/LaTex] mdframed does not split correctly between pages

mdframedpage-breaking

I use mdframed to make boxes and split them across pages. However, sometimes mdframed splits! very strangely across the pages. In this example he prefers splitting, while there is enough space to put it all.

enter image description here

I would like to know where the conflict is. I am not interested to tweaking this example, because I have hundreds of boxes. I need to understand where the problem is and fix it in the whole book at once.

\documentclass[a4paper,10pt,twocolumn]{book}
\usepackage[english]{babel}
\newcommand{\mytext}{uutien gaaaetlmsl t n.tu aii s liwmuarpulfoaf ealiesnPui sutuacttcuMitai   ibioErertix}
\usepackage[framemethod=tikz]{mdframed}
\tikzstyle{titregris} =[draw=gray, thick, fill=white,
  text=black, rectangle,minimum height=0.7cm]
\mdfdefinestyle{Argumentstyle}{}
\begin{document}
${}$ \vspace{18cm}
\begin{mdframed}[style=Argumentstyle,
firstextra={\node[titregris,xshift=5mm] at (P-|O) {puttetuiroe};},]
\begin {enumerate}
\item \mytext \mytext
\item \mytext \mytext
\item \mytext \mytext
\item \mytext \mytext
\item \mytext \mytext
\end {enumerate}
\end{mdframed}
\end{document} 

UPDATE: I saw that the wrong breakings take place when there is empty space before. mdframed seems to calculate the empty space needed, correctly splits the box such that the first part has the right length, but then it starts WRONGLY, ALWAYS, from the beginning of the next page! Here is a screen shot!

UPDATE 2: I think now that the problem is given by the node that defines the title of the box. Namely, it gives extra height which does not fit into calculations… I changed the present form of the title with the usual one (frametitlerule=true, frametitle={MyTitle}), and the problem dissapeared in all the document 🙂 However, I do like the node of the previous form…

UPDATE 3: It seems to be indeed from the title box, which goes up few millimeters and mdframed does not take into account for the calculation of available space. The only workaround I found until now was to move the title box INSIDE the main box… I checked, with this the whole document is OK.

Best Answer

You can reserve space for the title block with skipabove and topinnermargin. Additionally it turns out that you should hide the size of this title box by setting a bounding box of zero size. This improves breaking across columns:

Sample output

\documentclass[a4paper,10pt,twocolumn]{book}

\usepackage[english]{babel}

\newcommand{\mytext}{uutien gaaaetlmsl t n.tu aii s liwmuarpulfoaf ealiesnPui sutuacttcuMitai   ibioErertix}

\usepackage[framemethod=tikz]{mdframed}
\tikzstyle{titregris}=[draw=gray, thick, fill=white,
  text=black, rectangle, minimum height=0.7cm]
\mdfdefinestyle{Argumentstyle}{}

\begin{document}

\vspace*{18cm}

\begin{mdframed}[style=Argumentstyle,
  skipabove={1.2\baselineskip},
  innertopmargin={1.2\baselineskip},
  firstextra={\useasboundingbox (P) rectangle (P); \node[titregris,xshift=5mm] at (P-|O)  {puttetuiroe};},
  singleextra={\useasboundingbox (P) rectangle (P); \node[titregris,xshift=5mm] at (P-|O)  {puttetuiroe};}]
\begin {enumerate}
\item \mytext \mytext
\item \mytext \mytext
\item \mytext \mytext
\item \mytext \mytext
\item \mytext \mytext
\end {enumerate}
\end{mdframed}

\mytext

\begin{mdframed}[style=Argumentstyle,
  skipabove={1.2\baselineskip},
  innertopmargin={1.2\baselineskip},
  firstextra={\useasboundingbox (P) rectangle (P); \node[titregris,xshift=5mm] at (P-|O)  {puttetuiroe};},
  singleextra={\useasboundingbox (P) rectangle (P); \node[titregris,xshift=5mm] at (P-|O)  {puttetuiroe};}]
\begin {enumerate}
\item \mytext \mytext
\item \mytext \mytext
\item \mytext \mytext
\item \mytext \mytext
\item \mytext \mytext
\end {enumerate}
\end{mdframed}
\end{document} 

Incidentally, note the use of \vspace* rather than ${}$ \vspace. The star * means that the space is not to be discarded at page boundaries etc.

With \vspace*{20cm} the output is:

Second output

Related Question