The following patch (via etoolbox
) saves the sectional content (number, title and font) in a macro that is re-evaluated at page shipout (as supplied by everyshi
) if there is text overflow. Overflow is established using the condition
\ifdim\pagetotal>\pagegoal
% <do something>
\fi
that checks whether the gathered page content (of height \pagetotal
) extends beyond the allowed total (of height \pagegoal
).
\documentclass{article}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{everyshi}% http://ctan.org/pkg/everyshi
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\makeatletter
\let\@section@title@\relax% Sectional heading storage
\patchcmd{\@sect}% <cmd>
{\@xsect}% <search>
{\gdef\@section@title@{% Store sectional heading
{\noindent#6\@svsec#8\normalfont\ \smash{(continued)}}\par\bigskip}\@xsect}% <replace>
{}{}% <success><failure>
\EveryShipout{%
\ifdim\pagetotal>\pagegoal% There is content overflow on this page
\aftergroup\@section@title@% Reprint/-insert sectional heading
\fi%
}
\makeatother
\begin{document}
\section{A section}\lipsum[1-6]
\subsection{A subsection}\lipsum[7-14]
\end{document}
The sectional content is captured in \@section@title@
and includes the latest sectional title used (anything from level 1 = \section
to level 5 = \paragraph
).
Customizations include using only a specific sectional unit (like \section
, say). Also, to duplicate the spacing after the sectional header when traditionally used, rather than issuing \bigskip
.
The above works in the context of the standard LaTeX document classes (like book
, article
and report
) and may require additional modifications if used in other document classes, or in conjunction with other sectional heading packages.
You can give wrapfigure a bit of a hint that things are not quite the same after a section heading
\section{Section}
\pagedepth\maxdimen
\begin{wrapfigure}[5]{R}{50mm}
seems to work and pushed the section head on to the next page.
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.UPDATE: egreg has written a detailed explanation of
\widowpenalties
and\clubpenalties
.