10

Question

I would like to prevent mdframed to split a box too early or too late. By this, I mean that I would like the remaining part of the box to always be greater than a fixed length, given before.

It seems to be impossible with the normal options of mdframed.

Does anyone know how to modify the mdframed macros so that it has this feature?

Picture and code

enter image description here

\documentclass[a5paper]{article}
\usepackage{mdframed}
\begin{document}

\begin{mdframed}[
innerrightmargin = 0.5cm, 
innertopmargin = 0.5cm, 
innerbottommargin = 0.5cm, 
innerleftmargin = 0.5cm, 
skipbelow = 5mm, 
skipabove = 5mm, 
userdefinedwidth = 5cm, 
splitbottomskip = 5mm, 
splittopskip = 5mm,
backgroundcolor = red,  
roundcorner = 5pt,
align = center]


Vestibulum lectus metus, tincidunt at fermentum non, pellentesque at lorem. Vivamus nisl sem, tempor ac mi et, elementum feugiat justo. Pellentesque tristique consequat molestie. Donec non porttitor risus. Praesent adipiscing elementum tortor, commodo ullamcorper justo consequat eget. Aenean eget nulla enim. Nam purus massa, hendrerit quis faucibus eget, consectetur ac dui. Curabitur ornare non ipsum elementum suscipit. Sed a dignissim ligula, nec vestibulum dui. Fusce sed varius tellus.

Vestibulum lectus metus, tincidunt at fermentum non, pellentesque at lorem. Vivamus nisl sem, tempor ac mi et, elementum feugiat justo.

Vestibulum lectus metus.

\end{mdframed}

This is after the \verb+mdframed+ environment.
\end{document}
1
  • This might be problematic with multiple page mdframed environments, but in this case, adding \enlargethispage{4pt} before \begin{mdframed} avoids the problem.
    – egreg
    Apr 15 '14 at 14:17
8

I introduce \widow which uses the \widowpenalty to prevent a hanging single line (unless the whole paragraph only took a single line). You insert \widow as the last word of the paragraph. In this way, it doesn't apply to your whole document, but is applied locally to resolve a local problem.

This same approach applies outside of mdframed, as well.

\documentclass[a5paper]{article}
\usepackage{mdframed}
\newcommand\widow{%
  \widowpenalty=10000

  \widowpenalty=150}
\begin{document}

\begin{mdframed}[
innerrightmargin = 0.5cm, 
innertopmargin = 0.5cm, 
innerbottommargin = 0.5cm, 
innerleftmargin = 0.5cm, 
skipbelow = 5mm, 
skipabove = 5mm, 
userdefinedwidth = 5cm, 
splitbottomskip = 5mm, 
splittopskip = 5mm,
backgroundcolor = red,  
roundcorner = 5pt,
align = center]


Vestibulum lectus metus, tincidunt at fermentum non, pellentesque at lorem. Vivamus nisl sem, tempor ac mi et, elementum feugiat justo. Pellentesque tristique consequat molestie. Donec non porttitor risus. Praesent adipiscing elementum tortor, commodo ullamcorper justo consequat eget. Aenean eget nulla enim. Nam purus massa, hendrerit quis faucibus eget, consectetur ac dui. Curabitur ornare non ipsum elementum suscipit. Sed a dignissim ligula, nec vestibulum dui. Fusce sed varius tellus.

Vestibulum lectus metus, tincidunt at fermentum non, pellentesque at lorem. Vivamus nisl sem, tempor ac mi et, elementum feugiat justo.

Vestibulum lectus metus.\widow

\end{mdframed}

This is after the \verb+mdframed+ environment.
\end{document}

enter image description here


To make the answer symmetric, I have a comparable solution for orphans at the beginning of a paragraph, to be applied as \orphan at the beginning of a paragraph.

\usepackage{needspace}
\newcommand\needlines[1]{\needspace{#1\baselineskip}}
\newcommand\orphan{%
  \needlines{2}%
}
4
  • Thanks for your answer. Does the end of your code useful? I think you forgot to delete it, doesn't you?
    – Colas
    Apr 15 '14 at 13:28
  • @Colas LOL! Thanks. It's how I multi-task, by having several source codes lined up at once. Fixed. Apr 15 '14 at 13:31
  • I don't understand why you set \widowpenalty to different values, 10000 and 15. Do you prefer using \widow of the needspace package?
    – Colas
    Apr 15 '14 at 13:36
  • 2
    @Colas The large value, 10000, will apply to the current paragraph. The blank line that follows in the definition is essentially an indicator to start a new paragraph. Once in the new paragraph, the value is reset to the default value of 150, so that it doesn't affect any subsequent text. Apr 15 '14 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.