# How to generate a "<section> continued" heading after page-break?

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

\patchcmd{\@sect}% <cmd>
{\@xsect}% <search>
{\noindent#6\@svsec#8\normalfont\ \smash{(continued)}}\par\bigskip}\@xsect}% <replace>
{}{}% <success><failure>

\EveryShipout{%
\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":

• The problem is that the section immediatelly starts with a large block. Do you really have a tabular at the beginning of the section? If so, have you considered making it a floating object?
– yo'
Dec 2 '13 at 16:45
• @tohecz : Yes, I am writing my resume using widely tabular environment, so I find out having a lots of unbreakable long tables after sections. Dec 2 '13 at 17:56

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

\documentclass{article}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{everyshi}% http://ctan.org/pkg/everyshi
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\usepackage{environ}% http://www.ctan.org/pkg/environ

\makeatletter
\patchcmd{\@sect}% <cmd>
{\@xsect}% <search>
{\noindent#6\@svsec#8\normalfont\ \smash{(continued)}}\par\bigskip}\@xsect}% <replace>
{}{}% <success><failure>
\newif\if@sectionpage %Conditional to see if we are on the page where a section has been started
\newif\if@tab@pagebreak %Conditional to check if a table will cause a pagebreak
\newdimen\currtabheight
\newdimen\pt@saved
\let\ltx@tabular\tabular
\let\ltx@endtabular\endtabular
\providecommand{\env@tabular@save@env}{}
\providecommand{\env@tabular@process}{}
%Patching the tabular envionment
\RenewEnviron{tabular}[1]{%
\setbox0=\hbox{% box the tabular to measure the height later
\ltx@tabular{#1}
\BODY
\ltx@endtabular
}
\pt@saved=\the\pagetotal% store the current pagetotal value
\ifdim\pt@saved>\pagegoal% the tabular will cause a pagebreak
\global\@tab@pagebreaktrue
\fi
\ltx@tabular{#1}% printing the tabular
\BODY
\ltx@endtabular
}
%Doing the old shipout trick with the addtional declared conditionals
\EveryShipout{%
\ifdim\pagetotal>\pagegoal
\if@sectionpage
\if@tab@pagebreak\else\aftergroup\@section@title@\fi
\else
\aftergroup\@section@title@
\fi
\fi
\global\@sectionpagefalse
\global\@tab@pagebreakfalse}
%redefinition to make the sectionpage-switch work
\let\ltx@section=\section
\renewcommand{\section}{\@sectionpagetrue\ltx@section}
\makeatother

\begin{document}
\section{A section}\lipsum[1-4]
\section{Second section}

\begin{tabular}{l}
AAAAAAAAAAAAAAAAAAAA \\
AAAAAAAAAAAAAAAAAAAA \\
AAAAAAAAAAAAAAAAAAAA \\
AAAAAAAAAAAAAAAAAAAA \\
AAAAAAAAAAAAAAAAAAAA \\
AAAAAAAAAAAAAAAAAAAA
\end{tabular}

\lipsum[7-14]
\end{document}


## 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.

• @Robbo -- Does the above solution help to solve the problem? If not, please leave a comment what else is needed. Jul 23 '15 at 17:20