105

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.

0

4 Answers 4

104

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.

11
  • 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. :-) Jan 23, 2013 at 2: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, 2013 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.
    – Paul
    Sep 25, 2013 at 16:26
  • 3
    Thanks for this. A problem is that it doesn't work with \footnote{}, though.
    – nijoakim
    Apr 5, 2014 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 5, 2014 at 20:31
28

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.

2
  • \filbreak it is!
    – PatrickT
    Jun 4, 2022 at 11:48
  • I tried it and it sometimes created page breaks when none where needed.
    – tmalsburg
    Nov 2, 2022 at 11:59
11

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

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).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .