[Tex/LaTex] Change fancyhdr settings in preamble after they have already been set

fancyhdrheader-footermargins

I have a "standard" preamble (portion in the code below that I indicated that I really don't want to change) that I am trying to reuse. But, I want to adjust the right margin for one particular document. There is no problem in changing the geometry, nor a problem in modifying the text that is placed in the header — that works just fine. But, how do I change the location of where the header text is placed?

I want the "README" text to be right aligned with the text margin — similar to what do not adjust the right margin — by commenting out the line: \geometry{right=1.0in}

When I attempt to adjust the right margin I get the header on the right and footer page number still at the old margin location:

enter image description here

Notes:

  • This is not a case of needing a newgeometry mid way in a document. This entire document needs the slightly tweaked settings.
  • This question Problem with the header/ footer width seems to imply that fancyhdr must be loaded after the geometry settings, but that does not appear to be the case as the MWE below shows that the geometry settings after loading the fancyhdr package.
  • An obvious solution is to just copy the preamble into a new file and change that, or add a \def before \begin{document} and only set thr right margin once, but I'd prefer to avoid those types of solutions if at all possible.

Code:

\documentclass{article}
\usepackage[showframe]{geometry}%            
\usepackage{fancyhdr}
\usepackage{lipsum}


% ------------- Do not want to change this from here [------
\makeatletter%
    \geometry{right=0.25in}%
    \geometry{paperheight=3.5in}% 
    \geometry{paperwidth=8.5in}%
    \geometry{showframe=true}% 
    %
    \geometry{headsep=0.25in}
    \geometry{headheight=14pt}
    \let\ps@plain\ps@fancy%
    %
    \pagestyle{fancy}% <--- Removing this gives the desired output.
    \fancyhead[R]{Don't READ}%
    \fancyfoot[R]{Page \thepage}%
\makeatother%
% ------------- Do not want to change this to here ------]

% ------------- Only want to change below here
\makeatletter%
    \geometry{right=1.0in}%
    \let\ps@plain\ps@fancy%
    %
    \pagestyle{fancy}%
    \fancyhead[R]{README}%
    \fancyfoot[R]{Page \thepage}%
\makeatother%

\begin{document}
\lipsum[1]
\end{document}

Best Answer

You can use \fancyheadoffset:

\documentclass{article}
\usepackage[showframe]{geometry}%            
\usepackage{fancyhdr}
\usepackage{lipsum}


% ------------- Do not want to change this from here [------
\makeatletter%
    \geometry{right=0.25in}%
    \geometry{paperheight=3.5in}% 
    \geometry{paperwidth=8.5in}%
    \geometry{showframe=true}% 
    %
    \geometry{headsep=0.25in}
    \geometry{headheight=14pt}
    \let\ps@plain\ps@fancy%
    %
    \pagestyle{fancy}% <--- Removing this gives the desired output.
    \fancyhead[R]{Don't READ}%
    \fancyfoot[R]{Page \thepage}%
\makeatother%
% ------------- Do not want to change this to here ------]

% ------------- Only want to change below here
\makeatletter%
    \geometry{right=1.0in}%
    \let\ps@plain\ps@fancy%
    %
    \pagestyle{fancy}%
    \fancyheadoffset{0cm}
    \fancyhead[R]{README}%
    \fancyfoot[R]{Page \thepage}%
\makeatother%

\begin{document}
\lipsum[1]
\end{document}

enter image description here

Related Question