10

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

3
  • 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, 2013 at 16:45
  • 1
    @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, 2013 at 17:56
  • Simple but very adhoc solution: use \pagebreak before the offending section. Oct 27 at 10:25

1 Answer 1

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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .