3

I'm using the fancyhdr package in an article with the following code

\fancyhf{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[RE]{\textit{\nouppercase{\leftmark}}}
\fancyhead[LO]{\textit{\nouppercase{\rightmark}}}

At the moment \leftmark and \rightmark give the section and subsection titles. My subsections are pretty short and on a two-page spread there's a big chance the subsection title is actually on the page, so I don't need it in the header to refer to it. But I do have several part*s to my document.

I would like to use the part name (on the even pages) and the section name (on the odd pages) like so:

|  15       Third part* title      |      §2 2nd section title    16  |
|                                  |                                  |
|  Content of the 15th page,       |      Content of the 16th page,   |
|  continuing to the next          |      continuing to the next      |
|  line.                           |      line.                       |
|                                  |                                  |
|                                  |                                  |
|                                  |                                  |
|                                  |                                  |
|                                  |                                  |
|                                  |                                  |
|                                  |                                  |

I'm pasting a MWE for testing purposes

\documentclass[twoside]{article}

\usepackage{fancyhdr}
\pagestyle{fancy}
\setlength{\headheight}{16pt}

\fancyhf{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[RE]{\textit{\nouppercase{\leftmark}}}
\fancyhead[LO]{\textit{\nouppercase{\rightmark}}}

\begin{document}

\part*{First part title}

\section{First section title}

Abc

\newpage

\subsection{First subsection title}

Def

\newpage

\subsection{Second subsection title}

Ghi

\newpage

\part*{Second part title}

\section{Second section title}

Jkl

\newpage

Mno

\end{document}
Earthliŋ
  • 1,140

1 Answers1

3

Normally, starred sectioning commands do not automatically set the headers in any case. From your MWE, however, I assume that you would like them to. This code uses etoolbox to patch both the unstarred and starred commands to mark the headers. The section and subsectioning commands are redefined using modified code from fancyhdr's documentation.

\documentclass[twoside]{article}

\usepackage{fancyhdr,etoolbox}
\pagestyle{fancy}
\setlength{\headheight}{16pt}

\fancyhf{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[RE]{\textit{\nouppercase{\leftmark}}}
\fancyhead[LO]{\textit{\nouppercase{\rightmark}}}
\renewcommand\sectionmark[1]{\markright{#1}}
\renewcommand\subsectionmark[1]{\relax}
\makeatletter
\patchcmd{\@part}{\markboth{}{}}{\markboth{#1}{}}{\typeout{done patching part!}}{\typeout{oh dear! could not patch part command...}}
\patchcmd{\@spart}{\nobreak}{\nobreak\markboth{#1}{}}{\typeout{done patching starred part!}}{\typeout{oh dear! could not patch starred part command...}}
\makeatother
\begin{document}

  \part*{First part title}

  \section{First section title}

  Abc

  \newpage

  \subsection{First subsection title}

  Def

  \newpage

  \subsection{Second subsection title}

  Ghi

  \newpage

  \part*{Second part title}

  \section{Second section title}

  Jkl

  \newpage

  Mno

\end{document}

modified headers

cfr
  • 198,882
  • I'm using \pagestyle{empty} for the title and contents pages (like \pagestyle{empty} \maketitle \newpage \tableofcontents \clearpage \pagestyle{fancy}). In combination with your code, I'm getting "oh dear! could not patch [starred] part command..." and the corresponding header remains empty. What can I do to fix this? – Earthliŋ Dec 28 '14 at 23:14
  • @Earthliŋ I added that code to my example, added a title etc. in the preamble, and it recompiled fine with the title and contents pages. Can you update your question with an MWE I can use to reproduce the problem? – cfr Dec 28 '14 at 23:19
  • The problem was with using the hyperref package, which should be loaded after your code to work properly. Thank you! – Earthliŋ Dec 28 '14 at 23:39
  • @Earthliŋ Ah, yes. That makes sense. Since hyperref will be changing those commands, too. (And hyperref likes being loaded late, in most cases.) Glad you figured it out! – cfr Dec 28 '14 at 23:46