0

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}
  • 1
    Your document does not compile – user31729 Oct 12 '17 at 20:15
  • With Papeeria I have Missing number, treated as zero. [\chapter] /main.tex :29 Illegal unit of measure (pt inserted). [\chapter]. – Sebastiano Oct 13 '17 at 7:16
2

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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.