0

I have a problem with the header which I can't seem to solve. I am using the book class, and I want the part name and the chapter name in the header. I have tried using fancyhdr.

A MWE:

\documentclass[a4paper]{book}

\usepackage{fancyhdr}
\usepackage{emptypage}
\usepackage{lipsum}

\pagestyle{fancy}
\fancyhf{}
\fancyhead[RO,LE]{\thepage}
\fancyhead[RE]{\rightmark}
\fancyhead[LO]{\leftmark}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{#1}}

\begin{document}

\frontmatter

\tableofcontents

\mainmatter

\part{First part}
\chapter{First chapter}
\lipsum
\section{First section}
\lipsum

\part{Second part}
\chapter{Second chapter}
\lipsum
\section{Second section}
\lipsum

\backmatter

\end{document}

But instead of the section name on even pages I want the part name, i.e., instead of "First section" I want "First part".

2
  • I think the solution is here tex.stackexchange.com/a/219542/106804
    – oliversm
    Commented May 6, 2020 at 14:32
  • Thanks! I had not seen that. It's a quite complicated example, but I I found a solution in the code. However, it seems to rely on "redefining" \part using \titleformat and I really couldn't understand why it worked, but when trying that example out I found another way to solve my problem. Commented May 6, 2020 at 18:59

1 Answer 1

0

Edit: Found the solution here in a thread that I somehow had missed: Header and footer for a book using: part on even page and chapter on odd page

This solves my problem:

\documentclass[a4paper]{book}

\usepackage{fancyhdr}
\usepackage{emptypage}
\usepackage{lipsum}

\newcommand*\parttitle{}
\let\origpart\part
\renewcommand*{\part}[2][]{%
\ifx\\#1\\% optional argument not present?
  \origpart{#2}%
  \renewcommand*\parttitle{#2}%
\else
  \origpart[#1]{#2}%
  \renewcommand*\parttitle{#1}%
\fi
}

\pagestyle{fancy}
\fancyhf{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[LO]{\leftmark}
\fancyhead[RE]{\parttitle}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\headrulewidth}{0pt}

\begin{document}

\frontmatter

\tableofcontents

\mainmatter

\part{First part}
\chapter{First chapter}
\lipsum
\section{First section}
\lipsum

\part{Second part}
\chapter{Second chapter}
\lipsum
\section{Second section}
\lipsum

\backmatter

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .