# Fancyhdr and TOC

I posted a question a couple of weeks ago about my header-footer design and now it looks right on point: thanks for that. I'm having, though, problems with my table of contents, because it has two pages and default style doesn't work for me then (as you can see, this 0 is very unpleasant):

## Photo I

I've taken a look at Changing only the header for Table of Contents pages with package fancyhdr post, and I've worked something like this in my styles.sty file:

\usepackage{extramarks}
\usepackage{fancyhdr}
\fancypagestyle{nor}{% <===========================================
\fancyfoot[RO, LE]{\thepage}
\fancyfoot[CO, CE]{}
}
\fancypagestyle{toc}{% <===========================================
\fancyfoot[RO, LE]{\thepage}
\fancyfoot[CO, CE]{}
}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}\extramarks{}{}}
\renewcommand{\sectionmark}[1]{\extramarks{\thesection}{#1}}


And then, in the .tex file:

\documentclass[a4paper,12pt,twoside]{book}
\usepackage{styles}

\begin{document}
\setlength{\baselineskip}{.55cm}

\pagestyle{nor}
\begin{titlepage}
Some text
\end{titlepage}

\pagestyle{toc}
\tableofcontents

\pagestyle{nor}
\chapter{Els nombres enters}
SOME TEXT.

\end{document}


Which doesn't work for me, because (1) redefining of the chapterMark/sectionMark is lost and so it messes with the style of other pages; (2), by Photo II and Photo III, it seems like it never changes from \pagestyle{toc}; and (3), TOC header doesn't work either, because it shows the same output as Photo I. Photo IV is from a functional header from before these changes I made.

Hope you can help me. Thanks.


\usepackage{extramarks}
\usepackage{fancyhdr}
\fancypagestyle{nor}{% <===========================================
\fancyfoot[RO, LE]{\thepage}
\fancyfoot[CO, CE]{}
}
\fancypagestyle{toc}{% <===========================================
\fancyfoot[RO, LE]{\thepage}
\fancyfoot[CO, CE]{}
}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}\extramarks{}{}}
\renewcommand{\sectionmark}[1]{\extramarks{\thesection}{#1}}


## Photo IV

• Put a \newpage before \pagestyle{nor}. In general it is advised to put \newpage before page style changes, unless you can be sure that a new page was already started. Mar 27 '21 at 9:28
• That is true, but when you issue the \pagestyle{nor}, it is still on the last page of the TOC. So that means that when the last page of the TOC is constructed, nor is the page style that the page builder uses. After that the \chapter starts the new page. Mar 27 '21 at 9:56
• So it seems on page 7 the header (with section title and section number) disppears. I cannot reproduce that, I do get the header. So it may be just coincidental with how your subsections fall on the page. Would it be possible to email a more complete document to me? My email address is in the fancyhdr documentation. Mar 27 '21 at 10:37

In a private communication with @Mario I could solve both issues. For future lookup I will give the solutions to both problems here.

1. Why did the last page of the TOC get the wrong page header?

The pagestyle{nor} command was executed before the last page of the TOC was output. The output of that page occurs when the \newpage that is implied by the next \chapter command is executed. The \tableofcontents itself does not have a \newpage at the end; if you wished, you could add additional text after it on the same page.

Now when the page is output, the new pagestyle has already been set, so that pagestyle is used. The solution is to add a \newpage before the \pagestyle{nor}.

2. The \chaptermark and \sectionmark seem not to be working

It appears Mario was using fancyhdr version 3.10. In all versions of fancyhdr before version 4, the initialisation is done when the first \pagestyle{fancy} (or one of its derived pagestyles) is given, including the initialisation of \chaptermark and \sectionmark. So the first \pagestyle{nor} in the document initialised these and therefore the definitions given in the styles.sty got lost. In fancyhdr version 4 this is no longer the case, so this gives the right results.

Fortunately there is a simple solution, other than switching to version 4: In styles.sty, put \pagestyle{nor} before the \chaptermark definition. Now first the initialisation is done and then the given definitions overwrite the default definitions.

\pagestyle{nor}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}\extramarks{}{}}
\renewcommand{\sectionmark}[1]{\extramarks{\thesection}{#1}}