0

I'm using the geometry package for settings like textwidth and margins. fancyhdr uses the initial text geometry setting. Through my document I also change the geometry of the text layout (i.e. not the page size/orientation) multiple times using \newgeometry{} (because my text is set relative to different background images in the document).

(If I change the geometry midways in the document, There is a hack so fancyhdr picks up and adapts to the new textwidth).

How can I set up the footer and header width for fancyhdr so that it is independent of the text geometry and never changes? So it keeps the width and never adapts to the geometry of the text. My specific case is that I want the width of the footer to fill up (almost) the whole page width and keep it so on every page where this page style applies.

Here is a small example that shows that the footer/header width is set from the original geometry and never changes (however, on each page it aligns to where the text begins):

\documentclass[8pt,oneside]{report}
\usepackage[a3paper,landscape,left=12.cm,right=12.cm]{geometry}
\usepackage{fancyhdr}
\usepackage{kantlipsum}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% setup fancyhdr style "fancy"

\pagestyle{fancy}

% clear all
\fancyhf{} 

% header content
\fancyhf[HL]{Left eye}
\fancyhf[HC]{Nose}
\fancyhf[HR]{Right eye}

% footer content
\fancyhf[FL]{Left foot}
\fancyhf[FC]{\thepage}
\fancyhf[FR]{Right foot}

% show header and footer lines
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}

% TODO: here I would like some command/setting that sets up the header and 
%       footer width and keeps it through the whole document (unless changed), 
%       like \fancywidth{0.9\pagewidth} or \fancyfill{marginL}{marginR}. 
%
%       even better, this constant width setting should be possible to define 
%       for every \fancypagestyle (and if not defined, use the current behaviour).
%       this way we could switch easily between different setups.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\section*{geometry}
\kant[1-4]
\newpage

\newgeometry{left=1cm,right=1cm}
\section*{newgeometry\{left=1cm,right=1cm\}}
\kant[5-9]
\newpage

\newgeometry{left=20cm,right=11cm}
\section*{newgeometry\{left=20cm,right=11cm\}}
\kant[10-12]
\newpage

\restoregeometry
\section*{restoregeometry}
\kant[14-17]
\newpage

\end{document}
3
  • A different approach is to set up an initial, fixed geometry that gives correct header/footer width, and then use minipages on every page to set my desired text geometry. But minipages creates other problems like footnotes.
    – telephone
    Commented Mar 13 at 2:19
  • What about using changepage and adjustwidth? Then you can alter the width of the text block without changing the actual layout. Otherwise, you are going to need to calculate appropriate offsets for the header/footer to compensate.
    – cfr
    Commented Mar 13 at 3:33
  • 1
    Off-topic: 8pt is not a valid option. You are getting 10pt.
    – cfr
    Commented Mar 13 at 5:22

1 Answer 1

1

Normally, headers and footers are connected to the \textwidth (this is standard typographical practice). So you must now fool fancyhdr to compensate for your changes in the margins and \textwidth.

If you look at figure 1 in the fancyhdr documentation, you will see that the distance from the left edge of the paper to the left edge of the text is 1in+\hoffset+\oddsidemargin So to get the header/footer on a fixed space from the left edge of the paper (say 1cm), we have to shift the header by -1in-\hoffset-\oddsidemargin+1cm

In order to keep a fixed width for the header/footer we should not use \fancy..offset, but rather set \headwidth to our desired value. In the example below I set it to \paperwidth-2cm, so that we get a margin of 1cm at both ends, for the header and the footer, independent of the margins of the text. At least that is how I interpret you wishes.

\setlength{\headwidth}{\dimexpr\paperwidth-2cm}

\fancyhfinit{%
  \hspace{\dimexpr-1in-\hoffset-\oddsidemargin+1cm}%
}

enter image description here

enter image description here

enter image description here

enter image description here

Here is the complete document.

\documentclass[8pt,oneside]{report}
\usepackage[a3paper,landscape,left=12.cm,right=12.cm]{geometry}
\usepackage{fancyhdr}
\usepackage{kantlipsum}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% setup fancyhdr style "fancy"

\pagestyle{fancy}

\setlength{\headwidth}{\dimexpr\paperwidth-2cm}
\newlength{\fancyLM}
\fancyhfinit{%
  \hspace{\dimexpr-1in-\hoffset-\oddsidemargin+1cm}%
}

% clear all
\fancyhf{} 

% header content
\fancyhf[HL]{Left eye}
\fancyhf[HC]{Nose}
\fancyhf[HR]{Right eye}

% footer content
\fancyhf[FL]{Left foot}
\fancyhf[FC]{\thepage}
\fancyhf[FR]{Right foot}

% show header and footer lines
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}

% TODO: here I would like some command/setting that sets up the header and 
%       footer width and keeps it through the whole document (unless changed), 
%       like \fancywidth{0.9\pagewidth} or \fancyfill{marginL}{marginR}. 
%
%       even better, this constant width setting should be possible to define 
%       for every \fancypagestyle (and if not defined, use the current behaviour).
%       this way we could switch easily between different setups.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\section*{geometry}
\kant[1-4]
\newpage

\newgeometry{left=1cm,right=1cm}
\section*{newgeometry\{left=1cm,right=1cm\}}
\kant[5-9]
\newpage

\newgeometry{left=20cm,right=11cm}
\section*{newgeometry\{left=20cm,right=11cm\}}
\kant[10-12]
\newpage

\restoregeometry
\section*{restoregeometry}
\kant[14-17]
\newpage

\end{document}

By the way, there are other ways of putting text/images at a fixed place on the page, without using fancyhdr, like the eso-pic packages, or the LateX hooks for shipout.

3
  • Thank you, this is exactly what I need :) A simple and good solution. It's also easy to wrap it into a custom command \newcommand{\fancyfill}[1]{ ... } with the argument defining the margins.
    – telephone
    Commented Mar 13 at 13:53
  • Worth to mention is that \reversemarginpar creates problem with this solution. I couldn't get things working in my real document, and I later found this inference. I just removed that setting because it was not relevant in my document.
    – telephone
    Commented Mar 13 at 16:40
  • Thanks for reporting this. Probably in that case the marginpar width must also be taken into account, and maybe there is something with odd/even pages too. I will have a look and try to get it as complete as possible. Commented Mar 13 at 21:51

You must log in to answer this question.

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