[Tex/LaTex] Header manipulation not working with fancyhdr package

header-footer

The default plain pagestyle makes an empty header and footer except for the bottom page number. I am trying to amend some text in the header of it (since plain is default, this should effect all pages).

I have therefore dug into the fancyhdr package. It's documentation (page 8, top) gives this example code to redefine a pagestyle:

[…] fancyhdr gives you an easier way [to redefine the plain pagestyle] with the \fancypagestyle command. This command can be used to redefine existing pagestyles (like plain) or to define new ones, e.g. if part of your document is to use a different pagestyle. This command has two parameters: one is the name of the pagestyle to be defined, the second consists of commands that change the headers and/or footers, i.e.
fancyhead etc.

\fancypagestyle{plain}{%
\fancyhf{} % clear all header and footer fields
\fancyfoot[C]{\textbf{\thepage}} % except the center
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}}

What I have emphasized in bold in the quote is exactly my goal. I wish to have a specific word in the header on only a part of the document; a certain range of pages.

But I just can't make this work – not even this example code they give. Here is a clean example code (in ShareLatex for a live demo):

\documentclass[11pt,a4paper]{article}

\usepackage[utf8]{inputenc}
\usepackage{fancyhdr}  
\usepackage{lipsum}

\fancypagestyle{plain}{%
\fancyhf{} % clear all header and footer fields
\fancyfoot[C]{\textbf{\thepage}} % except the center
\fancyhead[C]{test text} % added text in header
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}}

\begin{document}

\lipsum 
\lipsum
\lipsum

\end{document}

This code chunk makes no change. I have copy-pasted the documentation code example and only added the line: \fancyhead[C]{test text}. It doesn't work with or without my extra line. No change.

I can't find the place I am misreading in the documentation. The explanation seems pretty simple. Can anyone point out the mistake?

Best Answer

One of the last things article.cls does is to issue

\pagestyle{plain}

and this sets the page style to the current version of plain. Redefining plain in the preamble is therefore not enough, and you need to call \pagestyle again

\fancypagestyle{plain}{%
% WHATEVER
}
\pagestyle{plain}

This isn't mentioned in the documentation of fancyhdr since (I guess) the example is about redefining plain for \chapters, which do call \thispagestyle{empty} anyway.

The warning about \headheight is not related to this, but as Christian has pointed out in his comment you can solve it by using

\usepackage[headheight=14pt]{geometry}
Related Question