# Absolutely, definitely, preventing page break

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.

-

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.

-
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 2:48
@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 I like your answer, however if absolutelynopagebreak doesn't jump to the next page it creates some bad looking blank space over the environment. To see what I mean just change \lipsum[1-3] to \lipsum[1]. – Paul Sep 25 '13 at 15:00
@Paul You're right, there was an l too many. – egreg Sep 25 '13 at 15:19
@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 '14 at 20:31

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

-

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.

-