[Tex/LaTex] Displaying the range of sections on the page in the header

header-footermemoirpage-breakingsectioning

I would like to have on each page the range of sections covered by it displayed, opposite of the page number and accompanied by the name of the first new section (if there is a new one) starting on that page. Chapters may start on odd or even pages. I also would like the first page of a chapter to have the exact same style: with the section range next to the the inner header margin and the page number next to the outer margin. In case there is no "first" section on the first (couple of) pages of a chapter, only the page number should display in the header.

This question is closely related to this question about correctly displaying section numbers in a header; I think that a correct solution might be dependent on the information given in the answers there.

I always use the memoir-package.


Format example: With the following code

\documentclass[openany]{memoir}
\usepackage{lipsum}

\begin{document}

\chapter{The only chapter} \lipsum[1]
\section{Section one} \lipsum[1]
\section{Section two} \lipsum[1-2]\lipsum[4]\lipsum[4]\lipsum[4]
\section{Section three} \lipsum[1]\lipsum[1]\lipsum[1]
\section{Section four} \lipsum[1]\lipsum[1]\lipsum[1]
\section{Section five} \lipsum[2]
\section{Section six} \lipsum[1]
\section{Section seven} \lipsum[1]
\section{Section eight} \lipsum[1]
\section{Section nine} \lipsum[1-5]
\section{Section ten} \lipsum[1]
\section{Section eleven} \lipsum[1-8]

\chapter{The second chapter} \lipsum[1-10]
\section{Section one} \lipsum[1]

\chapter{The third chapter}
\section{Section one} \lipsum[4]
\section{Section two} \lipsum[4]
\section{Section three} \lipsum[4]

\end{document}

chapter 2 starts on page 10 (not page 11, thanks to the openany option), chapter 3 starts on page 13 (not page 15, for the same reason), and the headers should be as follows:

  1. 1.1 -- Section one1
  2. 2Section two -- 1.2
  3. 1.3--1.4 -- Section three3
  4. 4Section five -- 1.4--1.5
  5. 1.6--1.8 -- Section six5
  6. 6Section nine -- 1.9
  7. 1.9--1.11 -- Section ten7
  8. 81.11
  9. 1.119
  10. 10
  11. 11
  12. 12Section one -- 2.1
  13. 3.1--3.3 -- Section one13

A slight variation would be to have chapter-initial pre-section pages display only the chapter number. That would change pages 10 and 11 to the following:

[10.] 102 (German style: 102.)

[11.] 211 (German style: 2.11)

Another (very reasonable and arguably better) variation is to have a section's name be mentioned even if there is no new section on a particular page; this is what Heiko Oberdiek's present solution does:

[8.] 8Section eleven -- 1.11

[9.] 1.11 -- Section eleven9


Two very friendly notes for the inevitable person wanting to tell me that first pages of chapters "shouldn't" have headers:

  1. Written material contains so many redundancies (page numbers, tables of contents, indexes, content redundancies in the material in the body text, …) and headers are themselves technically redundant. Consistency is my personal design choice.
  2. There are genres where aesthetic considerations are very much subordinate to stylistic uniformity, such as certain legal documents. (But then I don't think that a within-chapter header and footer style for a chapter's first page is unaesthetic in the first place.)

Best Answer

Since class memoir is used, starting a chapter on any page is easy:

\renewcommand*{\clearforchapter}{\clearpage}

Getting the headers right is much more complicate. Having page style plain for the first page of chapters would simplify this task a lot, because then the section values in the mark registers can be reset easily. The example below uses lots of additional mark registers instead (using eTeX and package etex) to remember section titles, section names and chapter numbers.

Remarks:

  • The section title is remembered in \MarkSectionTitle. Its chapter number is put in \MarkSectionChapterNumber. The title is only printed, if this chapter number matches the current chapter number to avoid printing the title from the previous chapter at the starting pages of a new chapter.

  • Unnumbered chapters/sections are unsupported/poorly supported.

  • The numbering stuff has prototype quality only. There might be some missing cases/bugs.

  • \MarkSectionEarlyNumber was added to catch the case where a section starts a new page. That means, the previous section also ends on that page, but the first number of the header should be the new section on the page.

The example file:

\documentclass{memoir}
\usepackage{lipsum}

\usepackage{etex}

\newmarks\MarkSectionTitle
\newmarks\MarkSectionChapterNumber
\newmarks\MarkSectionNumber
\newmarks\MarkSectionEarlyNumber
\newmarks\MarkChapterNumber

\makeatletter
\newcommand*{\AddMarks}[2]{%
  \begingroup
    \let\label\relax
    \let\index\relax
    \let\glossary\relax
    \protected@edef\x{#2}%
    \toks@\expandafter{\x}%
    \marks#1\expandafter{\the\toks@}%
  \endgroup
}
\newcommand*{\MarksNoBreak}{%
  \if@nobreak\ifvmode\nobreak\fi\fi
}
\newcommand*{\OriginalSection}{}
\let\OriginalSection\section
\renewcommand*{\section}{%
  \@ifstar{\OriginalSection*}{%
    \stepcounter{section}%
    \AddMarks\MarkSectionEarlyNumber{\thesection}%
    \addtocounter{section}{-1}%
    \OriginalSection
  }%
}

\makepagestyle{secheadings}
\makepsmarks{secheadings}{%
  \def\chaptermark##1{%
    \AddMarks\MarkChapterNumber{\thechapter}%
    \marks\MarkSectionEarlyNumber{}%
    \MarksNoBreak
  }%
  \def\sectionmark##1{%
    \ifnum\value{secnumdepth}>0 %
      \AddMarks\MarkSectionNumber{\thesection}%
    \fi
    \AddMarks\MarkSectionTitle{##1}%
    \AddMarks\MarkSectionChapterNumber{\thechapter}%
    \MarksNoBreak
  }%
  \def\tocmark{}%
  \def\lofmark{}%
  \def\lotmark{}%
  \def\bibmark{}%
  \def\indexmark{}%
  \def\glossarymark{}%
}
\newcommand*{\SecHeadingsSetup}{%
  \protected@edef\SecHeadingsTheChapter{\thechapter}%
  \edef\SecHeadingsSectionChapterNumber{%
    \firstmarks\MarkSectionChapterNumber 
  }%
  \ifx\SecHeadingsTheChapter\SecHeadingsSectionChapterNumber
    \edef\SecHeadingsFirstTitle{\firstmarks\MarkSectionTitle}%
    \edef\SecHeadingsPreviousChapterNumber{\topmarks\MarkChapterNumber}%
    \edef\SecHeadingsThisChapterNumber{\firstmarks\MarkChapterNumber}%  
    \edef\SecHeadingsLastNumber{\botmarks\MarkSectionNumber}% 
    \let\SecHeadingsFirstNumber\@empty
    \ifx\SecHeadingsPreviousChapterNumber\SecHeadingsThisChapterNumber
      \edef\SecHeadingsFirstNumber{\topmarks\MarkSectionEarlyNumber}% 
    \fi
    \ifx\SecHeadingsFirstNumber\@empty
      \edef\SecHeadingsFirstNumber{\firstmarks\MarkSectionNumber}%
    \fi
    \ifx\SecHeadingsFirstNumber\@empty
      \let\SecHeadingsNumbers\@empty  
    \else
      \ifx\SecHeadingsFirstNumber\SecHeadingsLastNumber
        \let\SecHeadingsNumbers\SecHeadingsFirstNumber 
      \else
        \def\SecHeadingsNumbers{%
          \SecHeadingsFirstNumber--\SecHeadingsLastNumber
        }%
      \fi 
    \fi   
  \else   
    \let\SecHeadingsFirstTitle\@empty
    \let\SecHeadingsNumbers\@empty
  \fi
}
\newcommand*{\SecHeadingsPrint}[2]{%
  \ifx#1\@empty
    \ifx#2\@empty
      \SecHeadingsOut{}%
    \else
      \SecHeadingsOut{#2}%
    \fi
  \else
    \ifx#2\@empty
      \SecHeadingsOut{#1}%
    \else
      \SecHeadingsOut{#1 -- #2}%
    \fi
  \fi  
}
\newcommand*{\SecHeadingsOut}[1]{%
  #1%
  \protected@edef\SecHeadingsTemp{#1}%
  \@onelevel@sanitize\SecHeadingsTemp 
  \typeout{Header(\thepage): \SecHeadingsTemp}%
}
\makeevenhead{secheadings}{\thepage}{}{%
  \SecHeadingsSetup
  \SecHeadingsPrint\SecHeadingsFirstTitle\SecHeadingsNumbers
}
\makeoddhead{secheadings}{%
  \SecHeadingsSetup
  \SecHeadingsPrint\SecHeadingsNumbers\SecHeadingsFirstTitle
}{}{\thepage}
\makeatother 

\aliaspagestyle{chapter}{secheadings}
\pagestyle{secheadings}

\renewcommand*{\clearforchapter}{\clearpage}

\begin{document}

\chapter{The only chapter} \lipsum[1]
\section{Section one} \lipsum[1]
\section{Section two} \lipsum[1-2]\lipsum[4]\lipsum[4]\lipsum[4]
\section{Section three} \lipsum[1]\lipsum[1]\lipsum[1]
\section{Section four} \lipsum[1]\lipsum[1]\lipsum[1] 
\section{Section five} \lipsum[2]
\section{Section six} \lipsum[1] 
\section{Section seven} \lipsum[1]
\section{Section eight} \lipsum[1]
\section{Section nine} \lipsum[1-5]
\section{Section ten} \lipsum[1]   
\section{Section eleven} \lipsum[1-8]

\chapter{The second chapter} \lipsum[1-10]
\section{Section one} \lipsum[1]

\chapter{The third chapter}
\section{Section one} \lipsum[4]
\section{Section two} \lipsum[4]
\section{Section three} \lipsum[4]

\end{document}

Result:

Header(1): 1.1 -- Section one
Header(2): Section two -- 1.2
Header(3): 1.3--1.4 -- Section three
Header(4): Section five -- 1.4--1.5
Header(5): 1.6--1.8 -- Section six
Header(6): Section nine -- 1.9
Header(7): 1.9--1.11 -- Section ten
Header(8): Section eleven -- 1.11
Header(9): 1.11 -- Section eleven
Header(10): 
Header(11): 
Header(12): Section one -- 2.1
Header(13): 3.1--3.3 -- Section one
Related Question