# Avoid pagebreak in mdframed environment

I created a document with two columns which contains almost exclusively mdframed environments. In general, splitting up the frames over the columns or a page is welcomed and works. However, situations occur, where the whole content of the frame is at the bottom of one page (or column) but the bottom line of the frame is moved to the beginning of the next page (or column), which looks rather ugly. As the document is several hundred pages long and has only those frames in it (the document displays a chat history, each message is grouped in a mdframed environment), a manual fix is impossible.

For me it would be alright if the bottom line of the frame was moved to previous page in order to complete the whole frame and in doing so creating a small overlap with the bottom margin. This would look better than a single line on top of the next page. Disabling pagebreaks completely is not an option.

## Edit

Here is a picture of the situation: As you can see, there are several frames on this page but the last one is incomplete: the bottom line of the frame is in the next column. What I want to achieve is that the last frame is completely displayed in the first column. It does not matter that in this case the bottom margin is entered. A minimal example is hard to provide, because it seems like this situation only occurs with certain heights and combinations of frames. In addition, there are some pictures included in the frames which again makes the situation more complicated.

The mdframed package provides the following warning:

Package mdframed Warning: You got a bad break because the last box will be empty you have to change it manually by changing the text, the space or something else on input line 243.

## Edit 2

Here is the code which defines one mdframed environment:

\newmdenv[linecolor=red!50,
rightmargin=1cm,
linewidth=2pt,
roundcorner=5pt,
skipbelow=-1pt,
]{InMessage}


And similarly the definition for the blue frames (rightmargin=1cm changes to leftmargin=1cm).

One frame is build like this:

\begin{OutMessage}
\begin{wrapfigure}[1]{r}[0.35\linewidth]{0.4\linewidth}
\vspace{-21pt}
\hspace{-0.75\linewidth}\footnotesize\bfseries\itshape{09.07.2007}
\end{wrapfigure}
Some text goes in here!
\end{OutMessage}


I know that this wrapfigure hack to get the date on the right hand side of the frame is far from optimum. But this seems to work and is the only solution which came to my mind (What I wanted to achieve: date on the rhs of the frame in the first row, the other text (="chat message") starts also in the first row and continues in the second row which can be used in its entire width). Suggestions to make this more robust are welcomed.

In this way, the whole document is build with alternating InMessage frames and OutMessage frames. The horizontal rules in one frame are created via

\vspace*{-0.5em}
\hphantom{a} \hrule


Again, the \hphantom{a} part is a bad hack, I know.

Maybe I should add, that the document is created from a python script, which uses some text file as an input and reformats it to the latex document. The resulting document is some hundred pages long. I don't know where I should add the \enlargethispage command in order to solve my issue, because I don't know beforehand, on which pages this might be a problem.

• Welcome! If you can post a minimal document we can use to reproduce the problem, you'll encourage people to answer and clarify your question. Maybe needspace would be of interest. – cfr Jul 25 '15 at 22:59
• it's not easy to get this. – touhami Jul 25 '15 at 23:52
• without the code, how mdframeds are defined, we can't see – touhami Jul 26 '15 at 6:23
• i think this is a case where you may need to use \enlargethispage on individual pages. if the pages need not be exactly the same length, and the document is not already divided into chapters or sections that start on a new page, you can "stabilize" things a bit by selecting some reasonable breakpoints and insert \newpage. – barbara beeton Jul 26 '15 at 12:21
• I suggest to try tcolorbox instead of mdframed, it is more robust. – Ulrike Fischer Jul 27 '15 at 14:11