2

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

0 doesn't procede there


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}{% <===========================================
    \renewcommand{\headrulewidth}{0.1pt}
    \fancyhead[RE]{\nouppercase{\leftmark}}
    \fancyhead[LO]{\nouppercase{\lastrightxmark}}
    \fancyhead[RO]{\lastleftxmark}
    \fancyhead[LE]{\thechapter}
    \fancyfoot[RO, LE]{\thepage}
    \fancyfoot[CO, CE]{}
    \fancyheadoffset[LE,RO]{+0.025\textwidth}
}
\fancypagestyle{toc}{% <===========================================
    \renewcommand{\headrulewidth}{0.1pt}
    \fancyhead[RE]{\nouppercase{\leftmark}}
    \fancyhead[LO]{\nouppercase{\lastrightxmark}}
    \fancyhead[RO]{\lastleftxmark}
    \fancyhead[LE]{}
    \fancyfoot[RO, LE]{\thepage}
    \fancyfoot[CO, CE]{}
    \fancyheadoffset[]{+0.025\textwidth}
}
\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.

UPDATE: I worked a little bit on my styles.sty code, and this is how it is at the moment. main.tex remains intact. Two things: is there any way to make the code more elegant? It seems a little off. The other, as I commented to Pieter, \renewcommand{\chaptermark} and \renewcommand{\sectionmark} still don't work as expected, output is the same as in Photo II. Furthermore, I tried to put these two commands inside \fancypagestyle{} definition and above them: it hasn't worked in any way.

\usepackage{extramarks}
\usepackage{fancyhdr}
\fancypagestyle{nor}{% <===========================================
    \renewcommand{\headrulewidth}{0.1pt}
    \fancyhead[RE]{\nouppercase{\leftmark}}
    \fancyhead[LO]{\nouppercase{\lastrightxmark}}
    \fancyhead[CE,CO]{}
    \fancyhead[RO]{\lastleftxmark}
    \fancyhead[LE]{\thechapter}
    \fancyfoot[RO, LE]{\thepage}
    \fancyfoot[CO, CE]{}
    \fancyheadoffset[LE,RO]{+0.025\textwidth}
    \fancyheadoffset[RE,LO]{+0.0\textwidth}
}
\fancypagestyle{toc}{% <===========================================
    \renewcommand{\headrulewidth}{0.1pt}
    \fancyhead[RE]{}
    \fancyhead[LO]{}
    \fancyhead[CE,CO]{\textsc{Índex}}
    \fancyfoot[RO, LE]{\thepage}
    \fancyfoot[CO, CE]{}
    \fancyhead[RO]{}
    \fancyhead[LE]{}
    \fancyheadoffset[]{+0.025\textwidth}
}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}\extramarks{}{}}
\renewcommand{\sectionmark}[1]{\extramarks{\thesection}{#1}}

Photo II

Photo III

enter image description here

Photo IV

enter image description here

5
  • 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
  • Hello Pieter, I'm in a book environment, and so I assume chapters always start on a new page. I worked a little bit on my code, but \renewcommand{\chaptermark} and \renewcommand{\sectionmark} are still giving me headaches. I'll update my post.
    – mariovilar
    Mar 27 '21 at 9:43
  • 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
  • It was actually working, only that I had to set \fancyhead[RE,LO,RO,LE] to empty, my fault. Still, \renewcommand{\chaptermark} and \renewcommand{\sectionmark} don't work as expected. A working solution is to put them inside \fancypagestyle, but the following error appears: "Illegal parameter number in definition of \ps@nor"
    – mariovilar
    Mar 27 '21 at 10:16
  • 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
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.