11

I use mdframed for my theorems like this

\mdfdefinestyle{theoremstyle}{%
  %adjustable lengths
  %inner margins
  innertopmargin=\topskip,
  % outter margins
  skipabove= 0.1in,
  skipbelow= 0.1in,
  % colrs
  linecolor=red!60,
  middlelinewidth=2pt,%
  roundcorner=5pt,
  apptotikzsetting={\tikzset{mdfframetitlebackground/.append style={%
  shade,left color=white, right color=blue!20}}},
  % Title
  frametitlefont=\TheoremHeaderFont\bfseries,
  frametitlerule=true,%
  frametitlerulecolor=orange!60,
  frametitlerulewidth=2pt,
  % Miscellaneous
  nobreak=false,
}

Sometimes page break occurs right after the theorem title. How I can prevent to break the frame between title and content?

EDIT BY yori. I'm having the same problem, so here is a MWE:

\documentclass{article}

\usepackage{lipsum}
\usepackage{mdframed}
\usepackage[a4paper,margin=1.75in]{geometry}

\begin{document}
\lipsum[1-5]

\begin{mdframed}[frametitle={The title}]%
\[
x^2+y^2=z^2.
\]
\end{mdframed}

\end{document}
yori
  • 5,681
  • 29
  • 59
itun
  • 305
  • Can you add a minimal, compilable example (MWE) that demonstrates the issue? – Sean Allred Sep 16 '14 at 22:32
  • The issue is that when the frame start near the end of the page and page break occurs between the title and content of the frame. I want to prevent this from happening. – itun Sep 16 '14 at 23:37
  • I know what the issue is, but it'll be a little hard for us to test that it works unless we have an example where it doesn't. – Sean Allred Sep 17 '14 at 03:17
  • Please add a full MWE that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}, not just a snippet. – Aradnix Sep 17 '14 at 05:38
  • @Aradnix: I just added a MWE for itun because I'm having the same problem. I also added a working but slightly unsatisfactory solution. – yori Sep 27 '14 at 11:00
  • One way to solve this problem is to use the needspace option, but this is a bit of an unsatisfactory solution, because it does not really express the intention, and it needs manual tweaking in the case of multi-line titles. – yori Sep 28 '14 at 12:21
  • @Yori a better workaround would be to insert the nobreak option manually per-theorem when needed. Of course this would disallow pagebreaks in the middle of the box content. – Bordaigorl Sep 29 '14 at 15:43
  • @Bordaigorl: this is true but for large documents (I'm working on ~700 pages), this is quite tedious and not very robust. My current best solution is to not use mdframed's title at all, and manually insert the title instead (in my own environment) and put a \nobreak after the title. This works well. – yori Sep 29 '14 at 19:02
  • @Yori What is wrong with the needspace option? If express it globally as needspace=5\baselineskip for the style you use for these boxes, then you will not have to tweak each environment. – Andrew Swann Sep 30 '14 at 11:49
  • @AndrewSwann: The value x for needspace has to be large enough so that the lines of the title + the first line of the contents is covered. On the one hand, I have mdframes with titles that take two lines, and that have contents that sometimes start with (display) equations. On the other hand, I have mdframes which have a title that fits on one line, and contents that fits on another. Setting x too low means breaking the first case, setting x too high leaves too much whitespace on the previous page. In my case, there is no value for x for which neither situation happens. – yori Sep 30 '14 at 15:58

1 Answers1

5

UNSATISFACTORY ANSWERS:

One way to solve this problem is to use the needspace option:

\documentclass{article}

\usepackage{lipsum}
\usepackage{mdframed}
\usepackage[a4paper,margin=1.75in]{geometry}

\begin{document}
\lipsum[1-5]

\begin{mdframed}[needspace=6em, frametitle={The title}]%
\[
x^2+y^2=z^2.
\]
\end{mdframed}

\end{document}

This tells mdframed to place the first page break at least 6em (vertically) into the frame. It is a bit of an unsatisfactory solution though, because it does not really express the intention, and it needs manual tweaking in the case of multi-line titles.

A second approach is to emulate the title, and add a \nobreak after the title:

\documentclass{article}

\usepackage{lipsum}
\usepackage{mdframed}
\usepackage[a4paper,margin=1.75in]{geometry}

\begin{document}
\lipsum[1-5]

\begin{mdframed}
\textbf{The title} \\ \nobreak
\[
x^2+y^2=z^2.
\]
\end{mdframed}

\end{document}

This works better, but still breaks in certain cases...

yori
  • 5,681
  • 29
  • 59