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:
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!
section 2
is displayed on the very bottom of page 3.Section 1
in order to lineSection 2
up with the top of the next page.