[Tex/LaTex] Remove `CHAPTER 0.’ from header in frontmatter chapters with fancyhdr

fancyhdrheader-footer

As the title says. The heading on the page following the Abstract looks like this:

CHAPTER 0. ABSTRACT

How can I remove the unnecessary Chapter number from the headings in the frontmatter?

\documentclass{book}
\usepackage{fancyhdr}
\begin{document}
\frontmatter
\pagestyle{fancy}
\chapter{Abstract}
Why does it say ``Chapter 0.'' on the next page?
\tableofcontents
\mainmatter
\chapter{First}
\end{document}

Best Answer

The fancyhdr package provides, by default, the definition of \chaptermark used in report.cls when \pagestyle{headings} is in force.

The book class instead has a check for \if@mainmatter:

  \def\ps@headings{%
      \let\@oddfoot\@empty\let\@evenfoot\@empty
      \def\@evenhead{\thepage\hfil\slshape\leftmark}%
      \def\@oddhead{{\slshape\rightmark}\hfil\thepage}%
      \let\@mkboth\markboth
    \def\chaptermark##1{%
      \markboth {\MakeUppercase{%
        \ifnum \c@secnumdepth >\m@ne
          \if@mainmatter
            \@chapapp\ \thechapter. \ %
          \fi
        \fi
        ##1}}{}}%
    \def\sectionmark##1{%
      \markright {\MakeUppercase{%
        \ifnum \c@secnumdepth >\z@
          \thesection. \ %
        \fi
        ##1}}}}

So you should supplement your code with the right definition. There's still a small problem, in that the pages of the table of contents will have two “CONTENTS” in the header, because \tableofcontents uses \@mkboth; you can fix it by redefining it.

\documentclass{book}
\usepackage{fancyhdr}
\begin{document}
\pagestyle{fancy}
\makeatletter
\renewcommand\chaptermark[1]{%
  \markboth{\MakeUppercase{%
    \ifnum \c@secnumdepth >\m@ne
      \if@mainmatter
        \@chapapp\ \thechapter. \ %
      \fi
    \fi
    #1}}{}%
}
\renewcommand\@mkboth[2]{\markboth{#1}{}}
\makeatother

\frontmatter
\chapter{Abstract}
Why does it say ``Chapter 0.'' on the next page?
\tableofcontents
\mainmatter
\chapter{First}
\end{document}