9

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":

enter image description here

  • 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. – Robbo Dec 2 '13 at 17:56
1

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
\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>
\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
    }
  \currtabheight=\ht0 \advance\currtabheight by \dp0%
  \pt@saved=\the\pagetotal% store the current pagetotal value
  \advance\pt@saved by \currtabheight% add the height of the current tabular
  \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}

Output

seccont_demo

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. – Ruben Jul 23 '15 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.