[Tex/LaTex] How to prevent the repeated section titles from duplicating

memoirpage-breakingsectioning

For my document, I wish to have my chapter and section titles repeat when the content continues to a new page. In order to achieve this, I'm using an approach similar to the one given in the answers to this question.

This is working pretty well, except for one situation. If a section finishes at the very end of one page, the heading for the next section will be displayed twice at the top of the next page.

Here is some example code that demonstrates the issue:

\documentclass[twoside]{memoir}
\usepackage[papersize={8.5in,11in}, vmargin=0.5in, outer=1in, inner=0.5in, includehead, includefoot]{geometry}
\usepackage{color}
\usepackage{fontspec}
\usepackage{newunicodechar}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{everyshi}% http://ctan.org/pkg/everyshi
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\usepackage[none]{hyphenat}

\setmainfont{OpenSans}

\makeatletter

\makechapterstyle{mychapter}{
  % Remove 'Chapter 1' from beginning of chapters 
  \renewcommand\@makechapterhead[1]{%
    \chapterheadstart%  \vspace*{50\p@}%
    {%\parindent \z@ \raggedright \normalfont
      %\parskip \z@
      \parindent \z@ \memRTLraggedright \normalfont%
      \printchaptertitle{##1}% \Huge \bfseries #1
      \afterchaptertitle% \par\nobreak \vskip 40\p@
    }
  }

  \setlength{\afterchapskip} {10pt} % Reduce spacing after headings

  % Remove spacing before chapter titles
  \renewcommand\chapterheadstart {}
  %\setlength{\beforechapskip}{0pt} % Doesn't work

  % Allow new chapter to start on right side page
  \renewcommand\clearforchapter{
    \clearpage
  }
}



% Repeating headings (based on https://tex.stackexchange.com/questions/47646/re-displaying-section-headings-after-page-breaks)
\let\@section@title@\relax% Section heading storage

\newcommand{\beginsection}[1]{%
  {\normalfont\secheadstyle #1}
  \par
  \vspace{0.5pt} % Required for vertical alignment, not quite sure why it's necessary
  \gdef\@section@title@{#1}
}

\makepagestyle{headings} {} % Disable default Memoir repeated headings, we'll make our own

\definecolor{grey}{gray}{0.6}

\EveryShipout{%
  \ifdim\pagetotal>\pagegoal% There is content overflow on this page
    \begingroup\let\protect\@typeset@protect
        {\textcolor{grey}{\chaptitlefont\f@rhdr\ (cont'd.)}}% Reprint chapter header
        \afterchaptertitle
        \@afterheading
        \vspace{6pt} % Required for vertical alignment, not quite sure why it's necessary
        {
            \@tempskipa \beforesecskip\relax%
            \textcolor{grey}{\normalfont\secheadstyle \@section@title@\ (cont'd.)}%
        }
    \endgroup
  \fi%
}

\makeatother

%\raggedright

\setlength{\parindent}{0pt}%
\nonzeroparskip

\begin{document}
\chapterstyle{mychapter}
\chapter{Good Chapter}
\beginsection{Section 1}\lipsum[1-2]
\beginsection{Section 2}\lipsum[1-7]
\beginsection{Section 3}\lipsum[1-4]
\chapter{Problem Chapter}
\beginsection{Section 1}\lipsum[7-14]
\beginsection{Section 2}\lipsum[1-6]
\end{document}

And here is a screenshot:

Screenshot

As you can see, Section 2 within 'Problem Chapter' has its heading displayed twice.

How can I prevent this issue? I know the easiest solution would be to have each section start on a new page, but this isn't acceptable to me.

Thanks in advance!

Best Answer

I figured out a way that seems to work, but it adds a bit of unwanted spacing so I'd be interested to see if someone has a cleaner way.

\documentclass[twoside]{memoir}
\usepackage[papersize={8.5in,11in}, vmargin=0.5in, outer=1in, inner=0.5in, includehead, includefoot]{geometry}
\usepackage{color}
\usepackage{fontspec}
\usepackage{newunicodechar}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{everyshi}% http://ctan.org/pkg/everyshi
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\usepackage[none]{hyphenat}

\setmainfont{OpenSans}

\makeatletter

\makechapterstyle{mychapter}{
  % Remove 'Chapter 1' from beginning of chapters 
  \renewcommand\@makechapterhead[1]{%
    \chapterheadstart%  \vspace*{50\p@}%
    {%\parindent \z@ \raggedright \normalfont
      %\parskip \z@
      \parindent \z@ \memRTLraggedright \normalfont%
      \printchaptertitle{##1}% \Huge \bfseries #1
      \afterchaptertitle% \par\nobreak \vskip 40\p@
    }
  }

  \setlength{\afterchapskip} {10pt} % Reduce spacing after headings

  % Remove spacing before chapter titles
  \renewcommand\chapterheadstart {}
  %\setlength{\beforechapskip}{0pt} % Doesn't work

  % Allow new chapter to start on right side page
  \renewcommand\clearforchapter{
    \clearpage
  }
}



% Repeating headings (based on http://tex.stackexchange.com/questions/47646/re-displaying-section-headings-after-page-breaks)
\let\@section@title@\relax% Section heading storage

\newcommand{\beginsection}[1]{%
  \let\@section@title@\relax
  {\normalfont\secheadstyle #1}
  \par
  \vspace{0.5pt} % Required for vertical alignment, not quite sure why it's necessary

  \gdef\@section@title@{#1}
}

\makepagestyle{headings} {} % Disable default Memoir repeated headings, we'll make our own

\definecolor{grey}{gray}{0.6}

\EveryShipout{%
  \ifdim\pagetotal>\pagegoal% There is content overflow on this page
    \begingroup\let\protect\@typeset@protect
        \textcolor{grey}{\chaptitlefont\f@rhdr\ (cont'd.)}% Reprint chapter header
        \afterchaptertitle
        \@afterheading
        \vspace{2pt} % Required for vertical alignment, not quite sure why it's necessary
        \if\relax\@section@title@%
            \relax
        \else%
            {
                \@tempskipa \beforesecskip\relax%
                \textcolor{grey}{\normalfont\secheadstyle \@section@title@\ (cont'd.)}%
            }
        \fi%
    \endgroup
  \fi%
}

\makeatother

%\raggedright

\setlength{\parindent}{0pt}%
\nonzeroparskip

\begin{document}
\chapterstyle{mychapter}
\chapter{Good Chapter}
\beginsection{Section 1}\lipsum[1-2]
\beginsection{Section 2}\lipsum[1-7]
\beginsection{Section 3}\lipsum[1-4]
\chapter{Problem Chapter}
\beginsection{Section 1}\lipsum[7-14]
\beginsection{Section 2}\lipsum[1-6]
\end{document}

Working screenshot

The key change here is that within my \beginsection macro I first unset the section title, disabling the headings. Then I print the title. I then start a new paragraph, which I think is necessary due to how everyshi works (but if there's a way to get rid of it I'd love to hear it!) and then I set the section title to the new value.

EDIT: I'll just add my completely unsubstantiated theory about why adding the new paragraph makes it work. I think that when LaTeX is trying to work out where to put page breaks it approaches the document in chunks. When \beginsection is called without enough room to fit on the current page (so it would overflow to the very top of the next page) it seems like LaTeX first tries to position it on the first page, executing its contents (including any /let or /def statements) which is why the heading on the second page of 'Problem Chapter' was 'Section 2' and not 'Section 1' like I'd expected it to be. The reason I found this surprising was that to my eyes it seemed like Section 2 hadn't started yet as of the top of the second page, but I think the theory I've provided explains this now.

The reason I think that inserting the paragraph break before setting the new value for the section name worked is because this moved the setting of the new value into a different chunk. By the time LaTeX came to render this chunk, it already knew it was on the next page. So this paragraph break seems to allow the first part of my macro to run on the previous page, and the second part to run on the new page.

Anyway, no idea if that's correct or not, I'd be interested to hear from someone more knowledgeable about the inner workings of LaTeX.