[Tex/LaTex] Absolutely, definitely, preventing page break

page-breaking

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.

Best Answer

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.

Related Question