[Tex/LaTex] Avoiding page breaks shortly after section/subsection headings

e-texpage-breakingsectioningsections-paragraphs

By default, LaTeX allows a page break after the first two lines of paragraphs following section/subsection headings. How can I enlarge this minimum to, say, three lines or 10 percent of the value of \textheight?

Best Answer

There may be a generalisable mechanism: Using e-TeX and its \clubpenalties command. In the following example, patching \@afterheading seems to do the trick.

\documentclass{article}

\usepackage{etex}

\usepackage{etoolbox}
\makeatletter
\patchcmd{\@afterheading}%
    {\clubpenalty \@M}{\clubpenalties 3 \@M \@M 0}{}{}
\patchcmd{\@afterheading}%
    {\clubpenalty \@clubpenalty}{\clubpenalties 2 \@clubpenalty 0}{}{}
\makeatother

\usepackage{blindtext}

\textheight 480pt

\begin{document}

\section{bla}

\blindtext[3]

\section{blubb}

\blindtext

\clearpage

\section{foo}

\blindtext[3]

An extra line.

\section{bar}

\blindtext

\end{document}

UPDATE: egreg has written a detailed explanation of \widowpenalties and \clubpenalties.