This is a follow-up question of the answer to Re-displaying section headings after page-breaks
As in the follow-up question, I would like to repeat the section heading after a page break, but only in the case the page break does not correspond to section end.
I tried the code proposed by Werner's answer, but it does not perform good when a long section spreading over the next page should start near the end of a page but LaTeX moves it entirely to the next page, for example because of a long table inserted immediately after the section start, as in the following code:
\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-4]
\section{Second section}
\begin{tabular}{l}
AAAAAAAAAAAAAAAAAAAA \\
AAAAAAAAAAAAAAAAAAAA \\
AAAAAAAAAAAAAAAAAAAA \\
AAAAAAAAAAAAAAAAAAAA \\
AAAAAAAAAAAAAAAAAAAA \\
AAAAAAAAAAAAAAAAAAAA \\
AAAAAAAAAAAAAAAAAAAA \\
\end{tabular}
\par\lipsum[7-14]
\end{document}
In this case, on the second page is wrongly inserted "2 Second section (continued)" before the normal heading "2 Second section":
tabular
at the beginning of the section? If so, have you considered making it a floating object?tabular
environment, so I find out having a lots of unbreakable long tables after sections.\pagebreak
before the offending section.