5

I use the fancyhdr package, so as to include the name and number of the relevant chapter in the header. This is my code:

\documentclass{report}

\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{#1}{#1}}
\fancyhead[R]{}
\fancyhead[L]{\chaptername\ \thechapter\ --\ \leftmark}

\begin{document}

\tableofcontents

\chapter*{Introduction}
\addcontentsline{toc}{chapter}{Introduction}
Text
\newpage
More text

\chapter{The first chapter}

\end{document}

As you perceive, the introduction is not enumerated.

With the code above, the second page of the introduction is given a header with the text "Chapter 0 - CONTENTS".

The same thing happens if the table of contents goes beyond the first page. Subsequent pages are all given the same header.

How could I adjust the above code, so that the headers are only introduced with the first chapter, and the table of contents and introduction are without a header?

2 Answers 2

3

I'd use a different approach, namely to set the header in \chaptermark.

\documentclass{report}

\usepackage{fancyhdr}

\pagestyle{fancy}
\fancyhead{} % clear the headers
\fancyhead[L]{\nouppercase{\leftmark}}
\renewcommand{\chaptermark}[1]{\markboth{\chaptername\ \thechapter\ -- #1}{}}

\newcommand{\uchapter}[1]{%
  \chapter*{#1}%
  \markboth{#1}{}%
  \addcontentsline{toc}{chapter}{#1}%
}

\begin{document}

\tableofcontents

\uchapter{Introduction}

Text
\newpage
More text

\chapter{The first chapter}

Text
\newpage
More text

% check the header of a possible second page in the TOC
\addtocontents{toc}{\protect\newpage\protect\mbox{}}

\end{document}

The \nouppercase is needed because report adds \MakeUppercase to the arguments to \markboth for special chapters such as the TOC, the lists of figures and tables and the bibliography.

I introduced a \uchapter command to avoid heavy code in the document body.

2
  • Delete my answer and I upvoted your.
    – Sebastiano
    Mar 15, 2020 at 13:14
  • (offtopic) is possible to find on StackExchange the same solution for article? Dec 18, 2021 at 22:25
3

Change the \fancyhead[L]{...} to

\makeatletter
\fancyhead[L]{%
  % thank egreg for his help in comment
  \ifnum\value{chapter}=0\else\chaptername\ \thechapter\ --\ \fi\leftmark
}
\makeatother

and add

\chaptermark{Introduction}

after \chapter*{Introduction} help.

With this change,

  • the second page of unnumbered chapter "Introduction" will have its left header showing "Introduction", and
  • the second page of table of contents will have its left header showing "CONTENTS".

A full example

\documentclass{report}

\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{#1}{#1}}
\fancyhead[R]{}

\makeatletter
\fancyhead[L]{%
  % thank egreg for his help in comment
  \ifnum\value{chapter}=0\else\chaptername\ \thechapter\ --\ \fi\leftmark
}
\makeatother

\begin{document}

\tableofcontents

\chapter*{Introduction}
\chaptermark{Introduction}
\addcontentsline{toc}{chapter}{Introduction}
Text
\newpage
More text

\chapter{The first chapter}
\newpage
text

\end{document}
6
  • \ifnum\c@chapter is better; even more so \ifnum\value{chapter}; \the is innocuous in that position, it could be harmful in different cases.
    – egreg
    Mar 14, 2020 at 23:42
  • @egreg Thank you. Answer is updated to use \ifnum\value{chapter} instead of \ifnum\the\c@chapter. Mar 15, 2020 at 13:54
  • Many thanks. This works very well for the sections preceding the first chapter. However, I still have one problem: The sections that follow the final chapter all retain the header of the final chapter; e.g. if we add: \chapter*{Conclusion} Concluding text \newpage Conclusively concluding text. The second page of the conclusion has the same header: "Chapter 1 - The first chapter". How could one change this? Mar 31, 2020 at 17:21
  • 1
    @HeinrichWagner Use another \chaptermark to set the text in header. Apr 1, 2020 at 2:58
  • 1
    @HeinrichWagner egreg's answer automatically meets your requirement. My answer is too special to be suitable for general use. Apr 2, 2020 at 0:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .