fancyhdr – Automatically Switching Back to Default Plain Page Style in LaTeX

fancyhdrpagestyle

I try to write a document using the report document class. I used fancypagestyle to redefine the plain page style to have custom header and footers.

Now I try to fill the document with its content, using \chapter, \section, and \subsection. The effect, that I see and which I do not expect is, that when I have enough text, that a page that started with a \chapter overflows to the next page, the page style changes to a style without header and footer. To me, it seems that the original plain page style is used again (I can see a page number at the bottom of the page).

I can workaround that, by adding \thispagestyle{plain} after \section or \subsection.

Is there some kind of mechanic involved, that switches to a different page style? What else could cause this change in the page style?

Edit: Here is a very reduced document, that shows the problem. The first page is using the plain page style, but on page 2 and the following pages, an other page style seems to be used (I think that might be the old plain style).

\documentclass[a4paper]{report}

\usepackage{titling}

\title{Test}
\author{Me}
\date{\today}
\let\documenttitle\thetitle

\usepackage[T1]{fontenc}
\usepackage{fontspec}
\usepackage{titlesec}
\usepackage{fancyhdr}
\usepackage{blindtext}

\setlength{\voffset}{-2.540cm}
\setlength{\hoffset}{-2.540cm}

\setlength{\topmargin}{2.0cm}
\setlength{\headsep}{1cm}
\setlength{\headheight}{0.5cm}

\setlength{\oddsidemargin}{2.5cm}
\setlength{\footskip}{2.9cm}

\setlength{\textheight}{23.5cm}
\setlength{\textwidth}{16.0cm}

\setlength{\marginparwidth}{0cm}
\setlength{\marginparpush}{0cm}
\setlength{\marginparsep}{0cm}

\fancypagestyle{plain}{%
    \fancyhf{}%

    \rhead{\fontsize{8.5pt}{11.5pt}\fontseries{m}\selectfont\thepage/X}%
    \lhead{\fontsize{8.5pt}{11.5pt}\fontseries{m}\selectfont\MakeUppercase{%
        \documenttitle%
    }}%
}

\begin{document}
    \blinddocument
\end{document}

This is the command line, that I used to generate a pdf file:

% xelatex --shell-escape  rep.tex && open rep.pdf

Best Answer

I believe there are two misunderstandings causing you to be confused.

Why is does the page style change when there are several pages?

It is a common typographical convention not to use headers on pages where a chapter starts. This let's the chapter heading be the first thing on the page without capping it with a header (that would usually just contain the chapter heading again).

In order to accommodate for this, LaTeX has the command \thispagestyle which changes the page style for this page only, i.e. it will reset to the default before the next page (as opposed to \pagestyle which changes the page style globally). By default, when you use \chapter, somewhere in the code there is a \thispagestyle{plain} that sets the page style for the page where the chapter starts to plain.

By default, the plain style has the page number in the footer and nothing in the header (this is also the default page style for the whole document). You changed that using fancyhdr which is why you see your custom style on the chapter heading page.

So why does my page style not work on the rest of the document? I changed the default, didn't I?

and here's the second misunderstanding. What \pagestyle{plain} does is not actually telling LaTeX "use the page style plain from now on". Instead, it says "from now on, put the page number in the footer and nothing in the header". That is, it sets the style according to plain, the fact that these settings came from plain is immediately forgotten.

Because of this, just changing the definition of a page style will never take effect in your document, even if you have activated it previously using \pagestyle. It only takes effect when you activate it again. In your example, this is done for the first page by \chapter (using \thispagestyle{plain}) but not for any other pages. That is why the default definitions are still in effect for those pages.

What all this means is that all you have to do to get what you expect is say

\pagestyle{plain}

after your \fancypagestyle definition. This will activate the settings you just defined and make them the document default.

Related Question