20

I have a document with different page margins, so I have to change geometry settings after fancyhdr loaded. However, the width of header after geometry is not correct.

\documentclass[12pt]{article}
\usepackage[left=35mm, right=35mm]{geometry}
\usepackage{fancyhdr}
\pagestyle{fancy}
\addtolength{\headheight}{8pt}
\fancyhead[C]{Some header text}

\begin{document}
It's fine here.
\newpage

\newgeometry{left=25mm, right=25mm}
PROBLEM: The line of header is 20mm narrower than expected here.
\end{document}

How can I solve this problem?

2
  • 2
    You have to set the \geometry first, then \fancyhead. So in this instance, you have to reset the header. Have you tried that? Ideally, make a complete, minimal working example (MWE) that one can copy-and-paste to reproduce your problem.
    – Werner
    Dec 25, 2012 at 5:04
  • @Werner Thanks for your advices, and I've added the MWE. Also, how to "reset" the header?
    – Haocheng
    Dec 25, 2012 at 5:39

1 Answer 1

23

Here's a MWE

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{fancyhdr}
%% \fancy@setoffs changed name to \f@nch@setoff at some point.
%% Checking whether \f@nch@setoff exists - if not we define it to be the old \fancy@setoffs:
\makeatletter
  \ifcsname f@nch@setoffs\endcsname\else%
  %% If \fancy@setoffs does not exis, then we assume old version define it to be
  %% fancy@setoffs, which is the old name for it:
  \let\f@nch@setoffs\fancy@setoffs
\fi
\makeatother
\pagestyle{fancy}
\renewcommand{\headrulewidth}{0.4pt}\renewcommand{\footrulewidth}{0.4pt}
\lhead{Hi}\rhead{Bye}
\makeatletter
\newcommand{\resetHeadWidth}{\f@nch@setoffs}
\makeatother

\usepackage{lipsum}
\begin{document}

\lipsum[1-3]

\pagebreak
\newgeometry{margin=1.5in}
\resetHeadWidth

\lipsum[1-3]

\pagebreak
\newgeometry{margin=0.75in}
\resetHeadWidth

\lipsum[1-3]

\end{document}

The key here is the new command \resetHeadWidth which calls the fancyhdr macro for defining the header and footer widths.

2
  • Cool, I also had this issue, and this solution worked out fine for me. (I wonder if fancyhdr maintainers shouldn't mention this in the package documentation.)
    – pedrosanta
    Nov 21, 2016 at 13:13
  • 14
    Worth noting: in the lastest version of the fancyhdr package, the macro \fancy@setoffs has been renamed to \f@nch@setoffs. Jul 30, 2017 at 15:17

You must log in to answer this question.

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