[Tex/LaTex] geometry, fancyhdr: \fancyfoot[C]{\thepage} is not really centered

fancyhdrgeometryheader-footerincompatibilitymargins

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).

Best Answer

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.