2

Is there a way in LaTeX to disable (or heavily penalise) pagebreaking at a specified place?

Here's an example:

\documentclass[]{minimal}

\usepackage[a6paper]{geometry} % demonstration use only

\begin{document}

May break before this.
May break before this.
May break before this.
May break before this.
May break before this.
Don't break before this\\
or before this.

\end{document}
ShreevatsaR
  • 45,428
  • 10
  • 117
  • 149
masu
  • 6,571
  • 3
    Is this specific to using listings and having content immediately after it? – Werner Sep 30 '13 at 17:55
  • It is for me, for now. You supplied an almost perfect answer for one of my questions for which this is an extension. But I don't know any general answers in the topic, so I formulated the question to gain as much knowledge as possible. :) – masu Sep 30 '13 at 18:54
  • I'll have to look into this... – Werner Sep 30 '13 at 20:01
  • The main problem is that \end{lstlisting} adds a feasible break point and undo this effect requires going deep in the innards of the package. On the other hand, a break before "or before this" can be avoided with the standard \\*. – egreg Sep 30 '13 at 20:37
  • I knew I could pose a really good question I try hard enough. 2 accepted answers so far for my 6 questions. :( Thank you @Werner for the effort. – masu Sep 30 '13 at 20:53

2 Answers2

4

You can use the needspace package:

\documentclass[11pt]{article}
\usepackage{lipsum}
\usepackage{needspace}
\begin{document}
\lipsum[1-4]
\needspace{4\baselineskip} % to avoid 3 orphan lines.
\lipsum[5]
\end{document}

Or you can increase the \clubpenalty for {some part}, but like \nopagebreak[n] this is a request, not a demand.

\documentclass[12pt]{article}
\usepackage{lipsum}
\begin{document}
\lipsum[1-4]
{\clubpenalty 10000  % to avoid 1 orphan line 
\lipsum[5]}
\end{document}

Or simply a \vbox:

\documentclass[11pt]{article}
\usepackage{lipsum}
\begin{document}
\lipsum[1-4]
\vbox{\lipsum[5]}
\end{document}
Fran
  • 80,769
0

You can use \nopagebreak[priority] where priority is a integer from 0 to 4. In your example it could be

...
\nopagebreak[4]
Don't break before this
...

to avoid a pagebreak at all.

Ruben
  • 13,448