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'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}

gives headers such as

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?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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.

enter image description here

\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}
  \advance\c@@memmarkcntra\m@ne
  \xdef\thesectionmark{%
    \ifnum \c@secnumdepth > \c@@memmarkcntra
      \if@mainmatter%
        \sectionmarksn{\thesection.~}%
      \fi%
    \fi%
    #1%
  }%
  \markright{\thesectionmark}}%
\def\subsectionmark#1{%
  \@setclcnt{subsection}{@memmarkcntra}
  \advance\c@@memmarkcntra\m@ne
  \xdef\thesubsectionmark{%
    \ifnum \c@secnumdepth > \c@@memmarkcntra
      \if@mainmatter%
        \sectionmarksn{\thesubsection.~}%
      \fi%
    \fi%
    #1%
  }%
  \markright{\thesectionmark~\sectiondiv~\thesubsectionmark}}%
\def\subsubsectionmark#1{%
  \@setclcnt{subsubsection}{@memmarkcntra}
  \advance\c@@memmarkcntra\m@ne
  \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.

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

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]{
  \sethead{\textsl{\thesection\ \sectiontitle\space%
     \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:

enter image description here

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.

share|improve this answer
    
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
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.