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:

And this is what I want it to show: 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{}
\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.

• Is this related? tex.stackexchange.com/questions/91219/… May 30 '19 at 16:38
• @Dr.ManuelKuehner yes, but I tried the solution and it printed the header and footer even for pages that ought not to have them, like the Chapter, Page, and ToC starting pages. May 30 '19 at 16:42

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

\fancypagestyle{styletoc}{% <===========================================
\fancyhf{}
\fancyfoot[C]{My Book}
}

\fancypagestyle{stylenor}{% <===========================================
\fancyhf{}
\fancyfoot[C]{My Book}
}


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{}
}


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{}
\fancyfoot[C]{My Book}
}

\fancypagestyle{stylenor}{% <===========================================
\fancyhf{}
\fancyfoot[C]{My Book}
}

\fancypagestyle{plain}{% <===========================================
\fancyhf{}
}

\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:

and for chapter 1 etc:

## 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}
}

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

% 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}


## 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}


• 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? May 30 '19 at 17:22
• @K.Paul See update. May 30 '19 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? May 31 '19 at 0:19
• @K.Paul You have the title of the TOC in the header, isn't that what you wanted? May 31 '19 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. May 31 '19 at 0:58