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 useplain
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}
Best Answer
It makes absolutely no difference that you are using
book
rather thanreport
. (Try switching toreport
to test.)But you do need to tell
fancyhdr
to use the fancy headingsWhat does make the difference is the use of
\frontmatter
,\mainmatter
etc. which is, it is true, specific tobook
. 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 unnumberedreport
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 frombook.cls
.First, we create a special command to make 'marks' for starred chapters:
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.Then we set up our page styles:
Assuming you want the same config for non-first pages:
Then we can write
We don't need to say
\clearpage
. That's handled automatically.to produce
Complete code: