0

I'm creating a template for a project. In it, I've created a command to create a summary page, but I need to change the geometry to be wider than the rest of the document. I've used the geometry package with the commands \newgeometry and \restoregeometry. This part works well.

Now I'm looking for a way to create the same header and footer across pages (this includes the same margin for all the different geometries). To do this, I've tried calculating the left and right margins to add the margin value with the command \fancyhfoffset like this:

\newcommand{\leftMargin}{%
    \dimexpr% 
      1in + %
    \ifoddpage%
      \oddsidemargin%
    \else%
      \evensidemargin%
    \fi%
  \relax%
}

\newcommand{\rightMargin}{%
  \dimexpr%
    \paperwidth - \textwidth - \leftMargin%
  \relax%
}

\fancypagestyle{main}{%
  % Reset
  \fancyhf{}%
  \renewcommand{\headrulewidth}{0pt}%
  \renewcommand{\footrulewidth}{0pt}%
  % Compute header and footer margins
  \fancyhfoffset[L]{\leftMargin - 2cm}%
  \fancyhfoffset[R]{\rightMargin - 2cm}%
  % Header
  \fancyhead[R]{\myTitle}%
  % Footer
  \fancyfoot[R]{\myLogo}%
  \fancyfoot[L]{\thepage}%
}

But this solution doesn't work : The margins are the same than these of the previous geometry... And if I use the same solution for the style plain (\fancypagestyle{plain}), when the style change (for example, the next page after a chapter start), the margins are reset and now they follow the text width. I think it's not possible to use the fancyhfoffset inside a fancypagestyle declaration but I don't know how else to do it.

Thanks in advance for your help !

2
  • Welcome to TeX.SE! Could you extend your code fragment into a small example we can compile please? Starting with \documentclass{} and ending with \end{document}
    – JamesT
    May 11, 2023 at 14:11
  • It is a relative offset. Use \fancyhfoffset[L]{-2cm} and \fancyhfoffset[R]{-2cm}. May 11, 2023 at 18:27

1 Answer 1

0

Geometry uses odd page margins as input, then computes the even page margins. \fancyhfoffset is relative to the current margins, which obviously change with \newgeometry.

\documentclass[twoside]{article}
\usepackage[showframe]{geometry}
\usepackage{fancyhdr}

\usepackage{lipsum}% MWE onlu

\edef\leftMargin{% odd page
    \dimexpr
      1in + \oddsidemargin
    \relax
}
\edef\rightMargin{%
  \dimexpr%
    \paperwidth - \textwidth - \leftMargin% or \evensidemargin - 1in
  \relax%
}

\fancyhf{}%
\renewcommand{\headrulewidth}{0pt}%
\renewcommand{\footrulewidth}{0pt}%
% Header
\fancyhead[R]{myTitle}%
% Footer
\fancyfoot[R]{myLogo}%
\fancyfoot[L]{\thepage}%
\pagestyle{fancy}

\fancypagestyle{main}{%
  % Reset
  \fancyhf{}%
  \renewcommand{\headrulewidth}{0pt}%
  \renewcommand{\footrulewidth}{0pt}%
  % Compute header and footer margins
  \fancyhfoffset[L]{-1cm}%
  \fancyhfoffset[R]{-1cm}%
  % Header
  \fancyhead[R]{myTitle}%
  % Footer
  \fancyfoot[R]{myLogo}%
  \fancyfoot[L]{\thepage}%
}

\begin{document}
\lipsum[1-8]

\newgeometry{left={\dimexpr \leftMargin-1cm},right={\dimexpr \rightMargin-1cm}}
\pagestyle{main}

\lipsum[1-12]

\clearpage% print header/footer for last page
\restoregeometry
\end{document}

You must log in to answer this question.

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