[Tex/LaTex] When does \nopagebreak work

page-breaking

There are many examples where \nopagebreak doesn't work as expected:

What is an example case where \nopagebreak does have an effect? And what are the rules determining whether or not \nopagebreak will work?

Best Answer

\nopagebreak makes it infinitely bad to break at that point. So TeX will not break at that node However it is possible that it breaks at that point visually for example

 blah blah

 \nopagebreak
 \hrule height 0pt
 \pagebreak[2]


 blah blah

Then TeX will not break above the invisible rule, but may break after the invisible rule. So it may seem that adding \nopagebreak has not worked.

In complicated macros like section headings that are adding multiple penalties and spaces, each of which separately introduces a potential breakpoint it can be quite hard to prevent page breaking with a single \nopagebreak but the details depend on the exact details of the macro, and where it is inserting vertical space and penalties.


In answer to the question in comments, usually a single \nopagebreak will prevent a page break as long as there is another feasible break point on that page, for example if you uncomment the \pagebreak here The break at that point is avoided by taking two extra lines over to the next page.

\documentclass{article}

\setlength\textwidth{5cm}
\setlength\textheight{16cm}
\begin{document}

\section{zzz}
abc\par one two three four one two three four one two three four
abc\par one two three four one two three four one two three four
abc\par one two three four one two three four one two three four
abc\par one two three four one two three four one two three four
abc\par one two three four one two three four one two three four
abc\par one two three four one two three four one two three four

\section{zzz}
abc\par one two three four one two three four one two three four
abc\par one two three four one two three four one two three four
abc\par one two three four one two three four one two three four
abc\par one two three four one two three four one two three four
abc\par one two three four one two three four one two three four
abc\par one two three four one two three four one two three four

\section{zzz}
abc\par one two three four one two three four one two three four
abc\par%\nopagebreak
one two three four one two three four one two three four
abc\par one two three four one two three four one two three four
abc\par one two three four one two three four one two three four
abc\par one two three four one two three four one two three four
abc\par one two three four one two three four one two three four


\end{document}