3

I want to make my Table of Contents pages show "Table of Contents" in their headers with package fancyhdr. Right now, according to the MWE below, this is what it shows: enter image description here

And this is what I want it to show: enter image description here The header, in this case, was done via \fancyhead[RE,LO]{Table of Contents}. This affects all other pages though, which is definitely not what I want.

Below is my MWE:

\documentclass[10pt,openany]{book}
\usepackage{fancyhdr}
\pagestyle{fancy}
    \fancyhf{}
    \fancyhead[RE,LO]{Part \thepart\enspace--\enspace Chapter \thechapter}
    \fancyhead[LE,RO]{\thepage}
    \fancyfoot[C]{My Book}

\begin{document}
\tableofcontents
\part{One}
\chapter{1}
\chapter{2}
\part{Two}
\chapter{3}
\chapter{4}
\chapter{5}
\chapter{6}
\chapter{7}
\part{Three}
\chapter{8}
\chapter{9}
\chapter{10}
\chapter{11}
\chapter{12}
\part{Four}
\chapter{13}
\chapter{14}
\part{Five}
\chapter{15}
\chapter{16}
\chapter{17}
\chapter{18}
\chapter{19}
\chapter{20}
\part{Six}
\chapter{21}
\chapter{22}
\chapter{23}
\chapter{24}
\end{document}

EDIT: My environment is MikTeX 2.9 and Command-Line pdflatex (with the help of Neovim) on Windows 10.

MORE EDIT: I want to be able to change the stuff that goes in the header/footer, so even though \pagestyle{fancy} does print TABLE OF CONTENTS for ToC pages, I'd like to avoid that option unless absolutely necessary.

2

2 Answers 2

4
+50

Well, you can for example define two own styles to be used:

\fancypagestyle{styletoc}{% <===========================================
  \fancyhf{}
  \fancyhead[RE,LO]{Table of contents}
  \fancyhead[LE,RO]{\thepage}
  \fancyfoot[C]{My Book}
% \renewcommand{\headrulewidth}{0pt}
}

\fancypagestyle{stylenor}{% <===========================================
  \fancyhf{}
  \fancyhead[RE,LO]{Part \thepart\enspace--\enspace Chapter \thechapter}
  \fancyhead[LE,RO]{\thepage}
  \fancyfoot[C]{My Book}
% \renewcommand{\headrulewidth}{0pt}
}

Style styletoc is then only used for the table of contents part, style stylenor for the other parts. Please sse that you should also define a new style plain:

\fancypagestyle{plain}{% <===========================================
  \fancyhf{}
  \renewcommand{\headrulewidth}{0pt}
} 

to get rid of the headrule without any other datas and the standard page number in the center of the footer ...

With the following mwe

\documentclass[10pt,openany]{book}

\usepackage{blindtext}
\usepackage{fancyhdr}

\fancypagestyle{styletoc}{% <===========================================
  \fancyhf{}
  \fancyhead[RE,LO]{Table of contents}
  \fancyhead[LE,RO]{\thepage}
  \fancyfoot[C]{My Book}
% \renewcommand{\headrulewidth}{0pt}
}

\fancypagestyle{stylenor}{% <===========================================
  \fancyhf{}
  \fancyhead[RE,LO]{Part \thepart\enspace--\enspace Chapter \thechapter}
  \fancyhead[LE,RO]{\thepage}
  \fancyfoot[C]{My Book}
% \renewcommand{\headrulewidth}{0pt}
}

\fancypagestyle{plain}{% <===========================================
  \fancyhf{}
  \renewcommand{\headrulewidth}{0pt}
}


\begin{document}

\pagestyle{styletoc} % <================================================
\tableofcontents

\part{One}
\pagestyle{stylenor} % <================================================
\chapter{1}
\Blindtext
\chapter{2}
\part{Two}
\chapter{3}
\chapter{4}
\chapter{5}
\chapter{6}
\chapter{7}
\part{Three}
\chapter{8}
\chapter{9}
\chapter{10}
\chapter{11}
\chapter{12}
\part{Four}
\chapter{13}
\chapter{14}
\part{Five}
\chapter{15}
\chapter{16}
\chapter{17}
\chapter{18}
\chapter{19}
\chapter{20}
\part{Six}
\chapter{21}
\chapter{22}
\chapter{23}
\chapter{24}
\end{document}

you get the following wished result:

enter image description here

and for chapter 1 etc:

enter image description here

0
0

1st Approach

I think that I did find a solution based on Difficult with fancyhdr and table of contents (different page styles for \frontmatter and \mainmatter).

\documentclass[10pt,openany]{book}
\usepackage{fancyhdr}
\usepackage{etoolbox}
\usepackage{blindtext}

\fancypagestyle{style_A}{
    \fancyhf{}
    \fancyhead[C]{Style A (thin line) -- TOC}
    \renewcommand{\headrulewidth}{0.2pt}
}

\fancypagestyle{style_B}{
    \fancyhf{}
    \fancyhead[C]{Style B (thick line) -- Not TOC}
    \renewcommand{\headrulewidth}{2.0pt}
}

% https://tex.stackexchange.com/questions/167828
\appto\frontmatter{\pagestyle{style_A}}
\appto\mainmatter{\pagestyle{style_B}}

\begin{document}

\frontmatter
\tableofcontents

\mainmatter

\blinddocument
\blinddocument
\blinddocument
\blinddocument
\blinddocument
\blinddocument
\blinddocument
\blinddocument
\blinddocument

\end{document}

enter image description here

2nd Approach

What is wrong with this output?

\documentclass{book}
\usepackage{fancyhdr}
\usepackage{blindtext}
\pagestyle{fancy}

\begin{document}

\tableofcontents

\blinddocument
\blinddocument
\blinddocument
\blinddocument
\blinddocument
\blinddocument
\blinddocument
\blinddocument
\blinddocument

\end{document}

enter image description here

6
  • Thank you for your effort. If, for example, my front matter contains something other than the ToC (for instance, a preface), could I show different headers for those pages using \appto and a different tagline?
    – Paul Kim
    May 30, 2019 at 17:22
  • @K.Paul See update. May 30, 2019 at 17:35
  • I don't see how I can change whatever goes in the header/footer with the second approach. Is that supposed to be the point?
    – Paul Kim
    May 31, 2019 at 0:19
  • @K.Paul You have the title of the TOC in the header, isn't that what you wanted? May 31, 2019 at 0:49
  • Well yes it is, but I want to be able to change what goes in header/footers for other pages, so i was wondering if there were other solutions to the simple vanilla.
    – Paul Kim
    May 31, 2019 at 0:58

You must log in to answer this question.

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