18

After switching from the report class to the book class and starting to use the fancyhdr package to control headers/footers, the page numbers in my footers are no longer centered.

This is my fancyhdr setup:

\usepackage{fancyhdr}
\fancypagestyle{plain}{%
\fancyhf{} % clear all header and footer fields
\fancyfoot[C]{\thepage} % except the center
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}}
%activate the style:
\pagestyle{plain}

\usepackage{geometry}
\geometry{a4paper} %paper format
\geometry{left=27.5mm,right=27.5mm,bottom=27.5mm,top=27.5mm} %margins

With this the page numbers are placed off-center. On even pages they are to the right of the center and on odd ones they are to the left of it. What I want is a perfectly centered (with respect to the page) pagenumber in the footer of even and odd pages alike (like it was when I used the report class and no fancyhdrs).

7
  • The setup for fancyhdr is not sufficient to make a diagnosis; are you also changing the parameters for page building?
    – egreg
    Commented Nov 6, 2011 at 20:56
  • I'm not sure which parameters you mean?
    – Matthias
    Commented Nov 6, 2011 at 20:57
  • If I use that code, the page number goes at the desired place. Do you have some setting to \textwidth or \evensidemargin?
    – egreg
    Commented Nov 6, 2011 at 20:59
  • It's centered in relation to the text, not to the page. You may notice it in twosided layout, when inner and outer margins are different. I suggest keeping centering to the text body, just imagine a full page and the page number below.
    – Stefan Kottwitz
    Commented Nov 6, 2011 at 21:01
  • It's not centered to the page, nor the text here. I will add my page (paper) setup to the question, maybe this is relevant?
    – Matthias
    Commented Nov 6, 2011 at 21:03

2 Answers 2

29

The solution is to load geometry before fancyhdr and apply the settings, then configure fancyhdr, not the other way round.

A working example:

\documentclass{book}
\usepackage[english]{babel}
\usepackage{geometry}
\geometry{a4paper} %paper format
\geometry{left=27.5mm,right=27.5mm,bottom=27.5mm,top=27.5mm} %margins
\usepackage{fancyhdr}
\fancypagestyle{plain}{%
\fancyhf{} % clear all header and footer fields
\fancyfoot[C]{\thepage} % except the center
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}}
%activate the style:
\pagestyle{plain}
\begin{document}
\vspace*{\fill}
\centering\thepage
\end{document}

In this example, if \usepackage{geometry} and the \geometry settings are moved below \pagestyle{plain}, the undesired behavior occurs. I wonder why geometry is not even mentioned in the fancyhdr manual, when I see this dependency.

2
  • Thanks Stefan! In fact, I had just figured it out myself while assembling a proper MWE.
    – Matthias
    Commented Nov 6, 2011 at 21:41
  • 1
    @Matthias: That's the great benefit of MWEs, they're basically like debugging :). Have you considered contacting the author of fancyhdr and asking him if this "conflict" should go into the manual?
    – doncherry
    Commented Jan 14, 2012 at 17:49
0

It happens I redefined a geometry because the document requires it. So my headrule, fancyfoot and everything was not working really well.

\fancypagestyle{custom-fancy}{%
    \newgeometry{
        top=25mm,
        left=30mm,
        right=30mm,
        bottom=25mm,
        includeheadfoot
    }
    \fancyhf{}
    \fancyhead[C]{Just a title}
    \fancyfoot[C]{\thepage}
    \renewcommand{\headrulewidth}{1pt}
    \renewcommand{\footrulewidth}{1pt}
}

enter image description here

As you can see, it overflows and I read in the documentation it was because all the head and foot commands were defined by \headwidth

\headwidth is a length parameter that defines the total width of the headers and footers.

But it is weird, because the package documentation says

enter image description here

So the most practical solution I found was to redefine this value just adding \setlength{\headwidth}{\linewidth} to my fancy style. And voalá.

\fancypagestyle{custom-fancy}{%
    \newgeometry{
        top=25mm,
        left=30mm,
        right=30mm,
        bottom=25mm,
        includeheadfoot
    }
    \fancyhf{}
    \setlength{\headwidth}{\linewidth}
    \fancyhead[C]{Just a title}
    \fancyfoot[C]{\thepage}
    \renewcommand{\headrulewidth}{1pt}
    \renewcommand{\footrulewidth}{1pt}
}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .