5

I have a document where from a certain point onward the width of the left margin changes. To do so, I used the \newgeometry command of the geometry package.

However, in the part of the document with the new value of the margin the right footer gets displayed in the wrong place. (Nonetheless, the left footer is placed correctly both before and after the change of the margin.)

Here is an example that reproduces the problem:

\documentclass[a4paper,12pt]{article}

\usepackage[margin=1.125in,top=1in,right=1in,left=2in]{geometry}
\usepackage{lipsum,fancyhdr}

\fancyhead{}
\renewcommand{\headrulewidth}{0pt}
\fancyfoot{}
\rfoot{Rfoot: Page number \thepage{}}
\lfoot{Lfoot: some text}
\pagestyle{fancy}

\begin{document}
Some text
\vfill
\lipsum[1]

\clearpage
\newgeometry{margin=1.125in,top=1in,right=1in,left=1in}
Some other text
\vfill
\lipsum[1]
\end{document}

Here's an example of the correct behavior (before the margin change):

enter image description here

while here is the faulty behavior (after the margin change):

enter image description here

How can I solve this problem?

3
  • I suppose, that fancyhdr uses lengths at the moment of definition of a pagestyle. If the width changes, this won't alter the stored lengths. (untested)
    – user31729
    Mar 4, 2016 at 20:05
  • @ChristianHupfer: I tried to repeat the code \fancyhead{} ... \pagestyle{fancy} after \newgeometry, too, but I still got the error.
    – Pier Paolo
    Mar 4, 2016 at 20:09
  • 1
    Yes, that doesn't work due to the fact that \headwidth isn't updated.
    – user31729
    Mar 4, 2016 at 20:16

1 Answer 1

5

fancyhdr uses the 'command' \headwidth to set the width of the foot and head. This isn't updated by \newgeometry.

\headwidth is in fact a \let\headwidth\fancy@headwidth, i.e. the real length register is \fancy@headwidth.

\documentclass[a4paper,12pt]{article}

\usepackage[margin=1.125in,top=1in,right=1in,left=2in]{geometry}
\usepackage{lipsum,fancyhdr}

\fancyhead{}
\renewcommand{\headrulewidth}{0pt}
\fancyfoot{}
\rfoot{Rfoot: Page number \thepage{}}
\lfoot{Lfoot: some text}
\pagestyle{fancy}

\begin{document}
Old geometry: \verb!\headwith! is \the\headwidth

Some text
\vfill
\lipsum[1]

\clearpage
\newgeometry{margin=1.125in,top=1in,right=1in,left=1in}
new geometry: \verb!\headwith! is still \the\headwidth
\setlength{\headwidth}{\textwidth}


Redefined: \verb!\headwith! is now \the\headwidth



Some other text
\vfill
\lipsum[1]
\end{document}

Update

I have appended some code to \newgeometry that automatically updates \fancy@headwidth. This solves the problem.

\documentclass[a4paper,12pt]{article}


\usepackage[margin=1.125in,top=1in,right=1in,left=2in]{geometry}
\usepackage{lipsum,fancyhdr}

\makeatletter
\let\@geometry@newgeometry\newgeometry

\renewcommand{\newgeometry}[1]{%
  \@geometry@newgeometry{#1}% Call regular \newgeometry first
  \setlength{\fancy@headwidth}{\textwidth}%
}
\makeatother

\fancyhead{}
\renewcommand{\headrulewidth}{0pt}
\fancyfoot{}
\rfoot{Rfoot: Page number \thepage{}}
\lfoot{Lfoot: some text}
\pagestyle{fancy}

\begin{document}
Old geometry: \verb!\headwith! is \the\headwidth

Some text
\vfill
\lipsum[1]

\clearpage
\newgeometry{margin=1.125in,top=1in,right=1in,left=1in}
new geometry: \verb!\headwith! is still \the\headwidth

Redefined: \verb!\headwith! is now \the\headwidth

Some other text
\vfill
\lipsum[1]
\end{document}

enter image description here

1
  • The first answer worked, the second one did not compile on my system. Perhaps geometry has changed since then. Simply inserting \setlength{\headwidth}{\textwidth} after new geometry did solve problem. Hence I'm adding +1 for the original answer, not the update
    – pauljohn32
    Jan 3, 2020 at 19:53

You must log in to answer this question.

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