# When does \nopagebreak work?

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?

-

\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}

-
Can you give an example where the output is visually affected by a single \nopagebreak command? – Ari Brodsky Sep 2 '14 at 1:48
@AriBrodsky year and a half late sorry, just noticed your comment and updated the answer. – David Carlisle Jan 13 at 15:56