A broad knowledge of how page breaking works can help. TeX accumulates material in the main vertical list, mostly lines after having split a paragraph, but also the “glue” it inserts between lines. Other material can be present on this vertical list, for instance the glue inserted before and after sectional titles for vertically spacing them. There are other things, but a complete description would take too long; the other main ingredient for page breaking are penalties.
A penalty can be inserted either automatically (by TeX the program) or manually (usually embedded in macros, so the user doesn't need to be aware of them). An automatic penalty is inserted after the first line of a paragraph (in the amount specified by the parameter
\clubpenalty) and after the last but one line (
\widowpenalty). A typical manual penalty is inserted by
Such penalties tell TeX the desirability for a page break at that point (always between lines, because paragraphing has already been performed when TeX looks for a page break point). If the penalty is positive, the break is undesirable, if negative it is desirable; the range is
-10000..10000: a –10000 penalty forces a page break, a penalty of 10000 disallows it.
Page breaks can take place at glue or at penalties (not the complete truth, actually, but quite near it). No page break can happen at glue that's immediately preceded by a penalty or other glue, but a penalty is always considered for a page break.
When TeX accumulates material, it keeps track of the cost for breaking a page at every feasible break point. The precise computations of this cost are explained in the TeXbook (chapter 15). A 10000 penalty makes for a “maximum cost” (100000). This cost depends also on the height of the accumulated material, so when the page is still almost empty, the cost will be this maximum value, but when the accumulated material is higher than the space reserved for the page it becomes infinite. The space reserved for the page is contained in the internal parameter
\pagegoal that is equal to
\textwidth (in LaTeXspeak,
\vsize is the primitive parameter) when a new page is started (more precisely, when the first item is appended to the main vertical list after a page has been ejected), but it can decrease if footnotes or floats are found during processing.
\pagebreak \pagebreak \pagebreak \pagebreak \pagebreak
insert a penalty at the spot. If the command is issued between paragraphs it will be precisely at the spot, if it's issued in a paragraph, the penalty will be inserted just after the line in which it happens to fall, before the interline glue. The amounts are
0 \@lowpenalty \@medpenalty \@highpenalty 10000
(their opposite are inserted by the similar command
\nopagebreak). It's the document class that fixes the intermediate values; for the standard classes they are
\@lowpenalty = 51
\@medpenalty = 151
\@highpenalty = 301
TeX keeps track of the costs, marking the “best so far” and, when it finds the cost for a page break is infinite, it backs up and splits the page at the most recent “best so far” cost. When a break point is superseded by another “best so far”, the penalties associated to that break (there may be none, of course) are discarded and they'll no more influence the computations.
So, if one issues
\pagebreak at a point that's sufficiently far from the next page break, in the sense that more material fits in the page after it, the penalty will be forgotten. Conversely, a
\pagebreak (or, equivalently, simply
\pagebreak) will produce a cost of –10000 that, by rule, forces a page break, even if the accumulated material is still far from
The sectional commands of LaTeX take care that the first paragraph started after them is preceded by a 10000 penalty, so no page break can occur between the title and the first paragraph or an immediately following sectional title. The next feasible break point is thus after the second line of the next paragraph (because the macros also temporarily set
\clubpenalty to 10000).
This behavior is modifiable, because the current value of
\pagegoal and the “height so far” can be examined at any time and the macro responsible for typesetting a section title can decide whether the available space is below some threshold and, in case, issue a
needspace provides a user interface for deciding whether prematurely breaking a page depending on the available space. The package
titlesec, with the
nobottomtitles* option does a similar thing (the inserted penalty is, however, 9999 and not 10000).
A final note: the penalties or glue where the page break takes place are moved to the next page, where they will disappear, because TeX always discards these items when it starts building a new page. Some clever output routine can use this fact, performing some actions before the items are discarded. Indeed, the LaTeX output routine does this, in order to accommodate page floats if so it's needed: the penalty issued by
\clearpage is –10001, so the output routine can know that the page break has been a consequence of
\clearpage and it will flush the float queues.