Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.)
share|improve this question
1  
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
add comment

1 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
share|improve this answer
    
(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
    
Ad (1) I had understood it differently. Probably it can be fixed by defining a macro for each section title that appears in the headers and to suppress the section title the next time if the macro is defined. Ad (2) Also unnumbered chapters, sections are not supported. Ad (1)-(3) Sorry, no time/too localized for me. –  Heiko Oberdiek Nov 20 '12 at 19:06
    
Okay, (2) (like (1)) isn't technically about unnumbered chapters/sections, but anyways, thanks for your work! I think I'll open a bounty. // Hmm, I'm wondering about the right Stack Exchange type for the bounty. –  Lover of Structure Nov 20 '12 at 20:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.