# Memoir class headers listing section/subsection/etc. hierarchy

I'm trying to use the memoir class such that a document with the following structure:

\section{A section}
\subsection{A subsection}
\subsection{Another subsection}
\subsubsection{A subsubsection}
\subsubsection{Another subsubsection}
\subsection{Yet another subsection}
\section{Another section}

1 A section
1 A section >> 1.1 A subsection
1 A section >> 1.2 Another subsection
1 A section >> 1.2 Another subsection >> 1.2.1 A subsubsection
1 A section >> 1.2 Another subsection >> 1.2.2 Another subsubsection
1 A section >> 1.3 Yet another subsection
2 Another section

To this end, I have the following in my preamble:

\def\sectionmark#1{\markright{\thesection \hskip.25cm #1}}
\def\subsectionmark#1{\markright{\rightmark \hskip.25cm >> \hskip.25cm \thesubsection \hskip.25cm #1}}
\def\subsubsectionmark#1{\markright{\rightmark \hskip.25cm >> \hskip.25cm \thesubsubsection \hskip.25cm #1}}

which almost works, but not quite. In particular, it will generate the following headers:

1 A section
1 A section >> 1.1 A subsection
1 A section >> 1.1 A subsection >> 1.2 Another subsection
1 A section >> 1.1 A subsection >> 1.2 Another subsection >> 1.2.1 A subsubsection
1 A section >> 1.1 A subsection >> 1.2 Another subsection >> 1.2.1 A subsubsection >> 1.2.2 Another subsection
1 A section >> 1.1 A subsection >> 1.2 Another subsection >> 1.2.1 A subsubsection >> 1.2.2 Another subsection >> 1.3. Yet another subsection
2 Another section

as the subsectionmark and subsubsectionmark commands nest. What can I do to prevent this?

-

The following MWE redefines the three levels of section marks responsible for filling odd-numbered (or \markright) pages: \sectionmark, \subsectionmark, and \subsubsectionmark. Each <sec> is redefined to capture its contents in \the<sec>mark, allowing it to be re-used elsewhere.

\documentclass{memoir}% http://ctan.org/pkg/memoir
\usepackage[T1]{fontenc}% http://ctan.org/pkg/fontenc
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\setsecnumdepth{subsubsection}% Display division numbers up to \subsubsection
\renewcommand{\thesection}{\arabic{section}}% Remove \chapter number from \section
\newcommand{\sectiondiv}{\guillemotright}%
\makeatletter
\def\sectionmark#1{%
\@setclcnt{section}{@memmarkcntra}
\xdef\thesectionmark{%
\ifnum \c@secnumdepth > \c@@memmarkcntra
\if@mainmatter%
\sectionmarksn{\thesection.~}%
\fi%
\fi%
#1%
}%
\markright{\thesectionmark}}%
\def\subsectionmark#1{%
\@setclcnt{subsection}{@memmarkcntra}
\xdef\thesubsectionmark{%
\ifnum \c@secnumdepth > \c@@memmarkcntra
\if@mainmatter%
\sectionmarksn{\thesubsection.~}%
\fi%
\fi%
#1%
}%
\markright{\thesectionmark~\sectiondiv~\thesubsectionmark}}%
\def\subsubsectionmark#1{%
\@setclcnt{subsubsection}{@memmarkcntra}
\xdef\thesubsubsectionmark{%
\ifnum \c@secnumdepth > \c@@memmarkcntra
\if@mainmatter%
\sectionmarksn{\thesubsubsection.~}%
\fi%
\fi%
#1%
}%
\markright{\thesectionmark~\sectiondiv~\thesubsectionmark~\sectiondiv~\thesubsubsectionmark}}%
\makeatother
\begin{document}
\section{A section}\lipsum[1-15]
\subsection{A subsection}\lipsum[1-15]
\subsection{Another subsection}\lipsum[1-15]
\subsubsection{A subsubsection}\lipsum[1-15]
\subsubsection{Another subsubsection}\lipsum[1-15]
\subsection{Final subsection}\lipsum[1-15]
\end{document}

A slight modification of the traditional memoir header formatting has also been introduced (removing the uppercase format), although it is easy to re-insert this.

The T1 font encoding (via fontenc) was required for \guillemotright. An alternative symbol (set via a redefinition of \sectiondiv) might remove this addition.

-
Thanks for the solution. I feel much better now knowing that it is certainly non-trivial... –  rbobbington Aug 9 '12 at 17:04

Here's a solution using the titleps package and its extramarks option; some conditionals are used to decide whether or not it is necessary to use the separator \guillemotright:

\documentclass{memoir}
\usepackage[T1]{fontenc}
\usepackage[extramarks]{titleps}
\usepackage{lipsum}

\setcounter{secnumdepth}{4}

\settitlemarks*{section,subsection,subsubsection}
\newpagestyle{main}[\small\sffamily]{
\ifnum\value{subsection}>0 \guillemotright\ \else\fi%
\thesubsection\ \subsectiontitle\space%
\ifnum\value{subsubsection}>0 \guillemotright\ \else\fi%
\thesubsubsection\ \subsubsectiontitle}}{}{}
}
\pagestyle{main}

\begin{document}

\chapter{Test}
\section{A section}
\lipsum[1-10]
\subsection{A subsection}
\lipsum[1-10]
\subsection{Another subsection}
\lipsum[1-8]
\subsubsection{A subsubsection}
\lipsum[1-10]
\subsubsection{Another subsubsection}
\lipsum[1-10]
\subsection{Yet another subsection}
\lipsum[1-10]
\section{Another section}
\lipsum[1-10]

\end{document}

An image of the top part of a page showing the header and some text:

There's however an issue with this approach: the marks won't update correctly if the second optional argument of the sectioning commands is used; in this case, some manual work will have to be done.

-
Thanks for the solution. I'm very glad that using an external package means you can have a much simpler and easier to understand bit of code than the mess required to do it all within memoir. –  rbobbington Aug 9 '12 at 17:05