# Displaying the range of sections on the page in the header

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 one - 1
2. 2 - Section two -- 1.2
3. 1.3--1.4 -- Section three - 3
4. 4 - Section five -- 1.4--1.5
5. 1.6--1.8 -- Section six - 5
6. 6 - Section nine -- 1.9
7. 1.9--1.11 -- Section ten - 7
8. 8 - 1.11
9. 1.11 - 9
10. 10 -
11. - 11
12. 12 - Section one -- 2.1
13. 3.1--3.3 -- Section one - 13

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.] 10 - 2 (German style: 10 - 2.)

[11.] 2 - 11 (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.] 8 - Section eleven -- 1.11

[9.] 1.11 -- Section eleven - 9

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.)
-
openany as an argument to the class will let chapters start on either odd or even pages. Can't help with the rest, I'm afraid. –  Torbjørn T. Oct 7 '12 at 20:36
@TorbjørnT. Thanks, I've incorporated this into my question statement. –  Lover of Structure Feb 23 '13 at 1:28

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
\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}%
\OriginalSection
}%
}

\def\chaptermark##1{%
\marks\MarkSectionEarlyNumber{}%
\MarksNoBreak
}%
\def\sectionmark##1{%
\ifnum\value{secnumdepth}>0 %
\fi
\MarksNoBreak
}%
\def\tocmark{}%
\def\lofmark{}%
\def\lotmark{}%
\def\bibmark{}%
\def\indexmark{}%
\def\glossarymark{}%
}
\firstmarks\MarkSectionChapterNumber
}%
\fi
\fi
\else
\else
}%
\fi
\fi
\else
\fi
}
\ifx#1\@empty
\ifx#2\@empty
\else
\fi
\else
\ifx#2\@empty
\else
\fi
\fi
}
#1%
}
}
}{}{\thepage}
\makeatother

\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

(1) Okay, this conforms to my format except for pages 8 and 9. (2) Your code does not give the right numbering for the last page if I add something like \appendix \chapter{The next chapter} \lipsum[1] afterwards. (3) See my edit to the question statement. –  Lover of Structure Nov 20 '12 at 18:48