2

In the following code, how to prevent a page break between the colored box and its subsequent text?

\documentclass[a4paper]{book}
\usepackage{lipsum}
\usepackage[showframe]{geometry}
\usepackage[framemethod=TikZ]{mdframed}
\mdfsetup{skipbelow=2em,skipabove=2em}
\setlength{\parskip}{0pt}
\begin{document}
\lipsum[1-6]
\begin{mdframed}[innertopmargin=9pt,innerbottommargin=9pt,usetwoside=false,backgroundcolor=yellow]
\normalfont\Large\bfseries Notes
\end{mdframed}
\noindent Note 1: Bla bla\\
Note 2: Foo Bar\\
And other notes which can span several pages...
\end{document}

Please note that I want at least three lines of notes to be always under the box on a same page, otherwise both the box and its notes should go to the next page.

  • If you know how large the frame is in advance (in this case, it is quite short vertically), you can \usepackage{needspace} and then "require" the space before the frame, as in \needspace{2\baselineskip}\begin{mdframed}.... Obviously, this approach will not work for frames that you wish to break across pages. – Steven B. Segletes Apr 7 '15 at 12:01
  • tcolorbox provides for breakable boxes definitely – user31729 Apr 7 '15 at 12:03
  • @StevenB.Segletes: Thanks, but I'd prefer not to use additional package. Also the height of my boxes is fixed. – Sisabe Apr 7 '15 at 12:03
  • @ChristianHupfer I think the OP wants to avoid a break not only within the box, but also after it. – Steven B. Segletes Apr 7 '15 at 12:04
  • Then how about this?: \setbox0=\vbox{\begin{mdframed}[innertopmargin=9pt,innerbottommargin=9pt,usetwoside=false,backgroundcolor=yellow] \normalfont\Large\bfseries Notes \end{mdframed} \noindent Note 1: Bla bla\\ Note 2: Foo Bar\\ And other notes which can span several pages... } \box0. Of course, this will also not allow breaking within the box. – Steven B. Segletes Apr 7 '15 at 12:11
1

While I originally suggested a needspace approach to the OP by way of a comment (and still think it the best approach), the OP prefers not to load additional packages. So here is an alternative.

After some discussion in the comments, this approach (I think) remedies the two issues noted by the OP. The vertical space at the beginning of the page is remedied by issuing a \vspace{} before the \box and issuing its negative counterpart as the first item within the box. This will not affect the result in the middle of a page, but will apply the negative \vspace only if it occurs at the beginning of the page (to counteract the top-skip of the mdframe).

As to the second issue. One need not include the multiple pages of notes inside the box... only those line that one wishes to remain unbroken with the mdframe header.

At the OP's request, I have wrapped it into a macro similar to \section. The syntax is \mysection{Section name}{after text} where the "after text" will be guaranteed not to break from the header.

\documentclass[a4paper]{book}
\usepackage{lipsum}
\usepackage[showframe]{geometry}
\usepackage[framemethod=TikZ]{mdframed}
\mdfsetup{skipbelow=2em,skipabove=2em}
\setlength{\parskip}{0pt}
\newcommand\mysection[2]{%
 \setbox0=\vbox{\vspace{-2em}%
  \begin{mdframed}[innertopmargin=9pt,innerbottommargin=9pt,usetwoside=false,backgroundcolor=yellow]
    \normalfont\Large\bfseries #1
  \end{mdframed}
  \noindent #2%
 }
 \vspace{2em}\box0\noindent\ignorespaces%
}
\begin{document}
\lipsum[1-3]
\mysection{Notes}{%
Note 1: Bla bla\\
Note 2: Foo Bar
}
And other notes which can span several pages...\par
\lipsum[1]
\mysection{Notes}{%
Note 1: Bla bla\\
Note 2: Foo Bar
}
And other notes which can span several pages...\par
\end{document}

enter image description here

enter image description here

| improve this answer | |
  • Thanks for your solution, but isn't there really an automatic way like the \section{...} mechanism? – Sisabe Apr 7 '15 at 12:30
  • @Sisabe I have turned it into a macro for you. – Steven B. Segletes Apr 7 '15 at 12:39
1

If tcolorbox could be a solution following code could be a starting point. It defines a notes environment which includes a default "Notes" title with all notes. This box is breakable between pages, but at least 3 notes lines are included before a page break, otherwise the whole box starts in a new page. If for any reason the box should not be breakables, option breakable=false can be used:

\documentclass[a4paper]{book}
\usepackage{lipsum}
\usepackage[most]{tcolorbox}
\usepackage[showframe]{geometry}
\setlength{\parskip}{0pt}

\newtcolorbox{notes}[1][]{title=Notes, left=0pt, right=0pt, lefttitle=4mm, toptitle=3mm, bottomtitle=3mm, boxrule=0mm, sharp corners, fonttitle=\normalfont\Large\bfseries, colbacktitle=yellow, coltitle=black, colback=white, lines before break=3, breakable, enhanced,#1}

\begin{document}
\lipsum[1-5]
\begin{notes}[breakable=false]
\noindent Note 1: Bla bla\\
Note 2: Foo Bar\\
\lipsum[1]
\end{notes}
\end{document}

enter image description here

| improve this answer | |

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.