Header manipulation not working with fancyhdr package

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{\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
\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?

• @campa: Yes, I figured that out too in the meantime :D Thanks. The change of plain is not used unless \pagestyle{plain} is used again
– user31729
Apr 5 '17 at 13:55
• Thanks @campa. It works, but I do get a warning, though, when adding this: "Package Fancyhdr Warning: \headheight is too small (12.0pt): Make it at least 13.59999pt. We now make it that large for the rest of the document. This may cause the page layout to be inconsistent, however." Can this be solved in a quick fix? Apr 5 '17 at 13:56
• @Steeven: \usepackage[headheight=14pt]{geometry}, because you have content in the header that has a larger height than the designed spacing for that.
– user31729
Apr 5 '17 at 13:56
• @campa: Perhaps this is duplicate, if not, you should transfer your comment into an answer, in my point of view
– user31729
Apr 5 '17 at 13:57
• Well that was fast and perfect. Yes, @campa, if you will add the answer, I will accept. Thank you both. Apr 5 '17 at 13:59

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}


As an add-on, I mention fancyhdr is not the only package for (re)defining page styles. You also have titleps, which comes with titlesec, but can be loaded independently, and is simpler to use (it hides the mark mechanism). If you load titlesec, you just have to set the option [pagestyles].

Here's how it goes:

\documentclass[11pt,a4paper]{article}

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

\renewpagestyle{plain}{%
\setfoot{}{\thepage}{}
}%

\pagestyle{plain}

\begin{document}

\lipsum
\lipsum
\lipsum

\end{document}

• Thank you for this extra note. Can this be used to define headers of only certain ranges of pages, as by actual goal was? Apr 5 '17 at 14:45
• I think so, but not specifying a range in the code. It allows to define new page styles, and it's up to you to decide when and where (well, after a chapter or a part, of course!) to apply them. So where you want to apply a new page style, you write \page style{mynicepage style}. Then later you can have a \pagestyle{some-other-page-style}. Apr 5 '17 at 15:12