[Tex/LaTex] Latex custom header with fancyhdr

booksconditionalsfancyhdrheader-footersectioning

I would like to customize my headers in the following way in a book:

  • On non-text pages (like for example part titles, or chapter title pages): no header, no footer
  • On text pages
    • On even pages: page number on the left, chapter title on the right, in the format Chapter 1: Title (in bold, grey). If not in a chapter, part title. If not in a part, book title.
    • On odd pages: page number on the right, section title on the left in the format Title (in bold, grey). If not in a section, chapter title. If not in a chapter, part title. If not in a part, book title.

How to achieve this with fancyhdr?

Starting document:

\documentclass[twoside, 10pt]{book}
\usepackage[
    paperwidth=6in,
    paperheight=9in,
    tmargin=0.75in,
    bmargin=0.75in,
    inner=0.75in,
    outer=0.75in
]{geometry}

\usepackage{lipsum}
\usepackage{fancyhdr}
\usepackage{tabularx}
\usepackage[listings,skins,theorems,breakable,most]{tcolorbox}
\edef\restoreparindent{\parindent=\the\parindent\relax}
\usepackage[parfill]{parskip}
\restoreparindent

\pagestyle{fancy}
\fancyhead[RO,LE]{\thepage}
\fancyhead[LO]{\nouppercase\rightmark}
\fancyhead[RE]{\nouppercase\leftmark}
\cfoot{}
\renewcommand{\headrulewidth}{}
\renewcommand{\footrulewidth}{}

\begin{document}
\tableofcontents
\chapter{This is a chapter name}
\chaptertoc
\lipsum
\section{First section}
\subsection{First subsection}
\lipsum
\subsection{Second subsection}
\lipsum
\section{Second section}
\subsection{First subsection}
\lipsum
\subsection{Second subsection}
\lipsum
\section{Second section}
\subsection{First subsection}
\lipsum
\subsection{Second subsection}
\lipsum
\end{document}

Best Answer

You should redefine \headrulewidth to 0pt, not to empty. Beside this you only need to set the correct marks:

\documentclass[twoside, 10pt]{book}
\usepackage[
    paperwidth=6in,
    paperheight=9in,
    tmargin=0.75in,
    bmargin=0.75in,
    inner=0.75in,
    outer=0.75in
]{geometry}

\usepackage{lipsum}
\usepackage{fancyhdr,xcolor}
\usepackage{tabularx}
\usepackage[listings,skins,theorems,breakable,most]{tcolorbox}
\edef\restoreparindent{\parindent=\the\parindent\relax}
\usepackage[parfill]{parskip}
\restoreparindent

\pagestyle{fancy}
\fancyhead[RO,LE]{\thepage}
\fancyhead[LO]{\bfseries\color{gray}\nouppercase\rightmark}
\fancyhead[RE]{\bfseries\color{gray}\nouppercase\leftmark}
\cfoot{}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}


\renewcommand\chaptermark[1]{\markboth{Chapter~\thechapter: #1}{Chapter~\thechapter: #1}}
\renewcommand\sectionmark[1]{\markright{#1}}

\usepackage{xpatch}
%patch \@part as book doesn't know \partname:
\makeatletter
\xpatchcmd\@part{\markboth{}{}}{\markboth{Part~\thepart: #1}{Part~\thepart: #1}}{}{\fail}
\makeatother


\begin{document}
\markboth{Book Title}{Book Title}

\lipsum 
\tableofcontents
\part{Part}
blbllb 
\newpage blblb 


\chapter{This is a chapter name}
%\chaptertoc
\lipsum \lipsum 
\section{First section}
\subsection{First subsection}
\lipsum
\subsection{Second subsection}
\lipsum
\section{Second section}
\subsection{First subsection}
\lipsum
\subsection{Second subsection}
\lipsum
\section{Second section}
\subsection{First subsection}
\lipsum
\subsection{Second subsection}
\lipsum
\end{document}