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":
Best Answer
The following solution basically patches the
tabular
environment to measure the height of the tabulars and perform more tests in the actual hack to avoid the wrong behaviour. I left more detail about the single steps in the code like in the original solution of @Werner.Code
Output
Remark
If you use other unbreakable material then tabulars, the error could occur again. Those macros would have to be patched similarly to the
tabular
environment. Maybe just leave a comment if this is the case.