83

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.

84

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.

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

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.

11

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

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.