115

This is a question that has been asked before in various ways, but I have yet to find a general answer to it. Perhaps I'm misunderstanding something and the solution is actually very simple.

I have a section of a document where page breaks are absolutely not accepted. It consists of a few paragraphs, followed by some formulas. What I want is that the section should be bumped down to the next page if there is not enough space for it on the current page, even if that means that the current page would only contain a single row of text.

I've tried all the usual solutions. When I use \begin{samepage}, I still sometimes get page breaks in the middle of the section.

If I use \begin{minipage}, then the section does not get a page break in the middle of it, but instead it gets pushed to the bottom of the previous page (i.e. it flows beyond the end of the page).

Is there any generic solution for what I want to do? If so, how? And if not, how can I solve this?

One more note: This document is automatically generated, so I can't solve it by manually putting page breaks where they are needed.

4 Answers4

108

Just enclose your text in an unbreakable unit; before it issue a combination of glue and penalties that will fill the page if the unit has to go to the next one.

\documentclass{article}
\usepackage{lipsum}

\newenvironment{absolutelynopagebreak}
  {\par\nobreak\vfil\penalty0\vfilneg
   \vtop\bgroup}
  {\par\xdef\tpd{\the\prevdepth}\egroup
   \prevdepth=\tpd}

\begin{document}
\lipsum[1]

\begin{absolutelynopagebreak}
\lipsum[1-3]\lipsum*[4]
\end{absolutelynopagebreak}

HERE WE RESTART \lipsum[2]
\end{document}

Try changing \lipsum[1-3] into \lipsum[1-2] and the unit will stay in one page.

egreg
  • 1,121,712
  • 1
    Thank you. This does what I needed. I have to say that it certainly wasn't obvious. After looking at it I must say that I still don't fully understand why it works. :-) – Elias Mårtenson Jan 23 '13 at 02:48
  • 1
    @EliasMårtenson A couple of hacks with lower level TeX commands. It's nothing more than enclosing the part to keep together in a box and do some bookkeeping at the end. – egreg Jan 23 '13 at 10:21
  • @egreg There's one more flaw - if defined under a section your environment will leave the section on the previous page, while samepage would force it to swap aswell. – Pawel Sep 25 '13 at 16:26
  • @Paul Just put \section inside the environment. ;-) – egreg Sep 25 '13 at 17:30
  • @egreg more bad news: When I put \section inside and it lands on the next page some unwanted vertical space appears, as in the picture – Pawel Sep 26 '13 at 08:07
  • @Paul Add \@minipagetrue just after \bgroup. – egreg Sep 26 '13 at 09:04
  • 3
    Thanks for this. A problem is that it doesn't work with \footnote{}, though. – nijoakim Apr 05 '14 at 20:28
  • 1
    @nijoakim That's an unfortunate consequence. Such problems are usually solved when doing the final revision, so page breaks can be balanced when the text is in its final form. – egreg Apr 05 '14 at 20:31
  • in my particular case (preserving paragraphs-containing-algorithms) this was much more effective than a \begin{minipage} vector which ruined my text spacing relative to the rest of my document. – Charney Kaye Aug 26 '16 at 20:56
  • I know it's an old post. By the way have to report that putting a negative \vspace after @egreg {absolutelynopagebreak} environment will defeat any resistence for pagebreaking – peppino Mar 15 '18 at 12:43
29

One solution may be to use \filbreak before the section, which is defined in plain TeX as \vfil\penalty-200\vfilneg. This suggests that the line is a favorable break location. It worked for a similar problem of mine, at least.

akvilas
  • 988
  • 6
  • 11
11

You need to not only prevent page breaks (minipage) but also allow the previous page to stretch (most easily by using \raggedbottom)

David Carlisle
  • 757,742
0

On top of the other answers, \nopagebreak can help preventing breaks, but watch out for unintended mixups, such as described in When does \nopagebreak work?.

Also consider that \pagebreak[x] can suggest a pagebreak at a better location.

Also watch out for commands that implicitly suggest a pagebreak, such as \smallbreak, \midbreak, \bigbreak (if your sections keep breaking, read the docs on the commands that you use between parts).

tkruse
  • 111
  • 4