3

It's a question similar to How to remove the name Chapter with fanchyhdr in Report class. However, it's entirely different case on book class.

I searched every corner of this site for 3 days without getting a working example. It's so frustrated to a beginner. I really want to finish my report.

The challenge is to:

  • Setting up header even for the first page of chapter. book class define the first page of chapter to use plain style.
  • Therefore, we may override plain style by \fancypagestyle{plain}
  • Chapters living in \frontmatter should not show the chapter number since they are 0.
  • Book is oneside. Every chapters is output by \clearpage.

    \documentclass[a4paper,oneside,12pt]{book}
    \usepackage{fancyhdr}
    
    \makeatletter
    \renewcommand{\chaptermark}[1]{\markboth{\textit{Chapter \thechapter}\ #1}{}} % no effort command
    
    \fancypagestyle{plain}{%
        \fancyhf{}%
        % No effort comment from wikibooks: Note the ## here. It's required because \fancypagestyle is making a macro (\ps@fancybook).
        % If I just wrote #1, TeX would think that it's the argument to \ps@fancybook, but
        % \ps@fancybook doesn't take any arguments, so TeX would complain with an error message.
        % You are not expected to understand this.
        % \renewcommand*{\chaptermark}[1]{ \markboth{\thechapter\ \chaptername: ##1}{} }
    
        \fancyhead[R]{\textbf{\leftmark}}
        \fancyfoot[C]{\thepage}
    
        \renewcommand{\plainheadrulewidth}{0.4pt}
    }
    
    \pagestyle{plain}
    \begin{document}
    \frontmatter
    \chapter{Abstract}
    abstract
    \clearpage
    
    \mainmatter
    
    \chapter{Main}
    Show `1 Main` in the header.
    \clearpage
    \end{document}
    
2

It makes absolutely no difference that you are using book rather than report. (Try switching to report to test.)

\renewcommand{\chaptermark}[1]{\markboth{\normalfont\thechapter.\space#1}{}}

But you do need to tell fancyhdr to use the fancy headings

\pagestyle{fancy}

What does make the difference is the use of \frontmatter, \mainmatter etc. which is, it is true, specific to book. More specifically, what matters here is that you want unnumbered chapters in the headers which LaTeX does not do by default in the standard styles. You would see the same complications with unnumbered report chapters, too.

There are several ways to do what you want. The following may not be your best option depending on what other changes you wish to make. However, for the MWE, it is straightforward, at least.

LaTeX's standard classes support starred forms of the sectioning commands for unnumbered sections. This is what you should use prior to \mainmatter. But these are configured differently and do not create the 'marks' for headers by default. To change this, we can redefine the command from book.cls.

First, we create a special command to make 'marks' for starred chapters:

\newcommand{\schaptermark}[1]{\markboth{\normalfont #1}{}}

Then we add this to the definition from book. \makeatletter... \makeatother is required because the code includes the @ symbol which isn't usually allowed in command names in documents.

\makeatletter
% modified from book.cls - don't use \def unless you **know** it is safe!
\def\@schapter#1{\if@twocolumn
  \@topnewpage[\@makeschapterhead{#1}]%
  \else
  \@makeschapterhead{#1}%
  \schaptermark{#1}% <= our addition here
  \@afterheading
  \fi}
\makeatother

Then we set up our page styles:

\fancypagestyle{plain}{%
  \fancyhf{}%
  \fancyhead[R]{\textbf{\leftmark}}%
  \fancyfoot[C]{\thepage}%
  \renewcommand{\plainheadrulewidth}{0.4pt}%
}

Assuming you want the same config for non-first pages:

\fancyhf{}
\fancyhead[R]{\textbf{\leftmark}}
\fancyfoot[C]{\thepage}
\renewcommand{\headrulewidth}{0.4pt}

Then we can write

\frontmatter
\chapter*{Abstract}% a * stops the numbering
abstract

We don't need to say \clearpage. That's handled automatically.

\mainmatter

\chapter{Main}% no * so it is numbered
Show `1 Main` in the header.

to produce

starred and unstarred chapter headers for fancy first pages

Complete code:

\documentclass[a4paper,oneside,12pt]{book}
\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{\normalfont\thechapter.\space#1}{}}
\newcommand{\schaptermark}[1]{\markboth{\normalfont #1}{}}
\makeatletter
% modified from book.cls - don't use \def unless you **know** it is safe!
\def\@schapter#1{\if@twocolumn
  \@topnewpage[\@makeschapterhead{#1}]%
  \else
  \@makeschapterhead{#1}%
  \schaptermark{#1}%
  \@afterheading
  \fi}
\makeatother
\fancypagestyle{plain}{%
  \fancyhf{}%
  \fancyhead[R]{\textbf{\leftmark}}%
  \fancyfoot[C]{\thepage}%
  \renewcommand{\plainheadrulewidth}{0.4pt}%
}
\fancyhf{}
\fancyhead[R]{\textbf{\leftmark}}
\fancyfoot[C]{\thepage}
\renewcommand{\headrulewidth}{0.4pt}
\begin{document}
\frontmatter
\chapter*{Abstract}% a * stops the numbering
abstract
% \clearpage is automatic
\mainmatter

\chapter{Main}% no * so it is numbered
Show `1 Main` in the header.
% \clearpage is automatic
\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.