我有一个文档,其页边距不同,因此加载后我必须更改几何设置fancyhdr
。但是,几何后的页眉宽度不正确。
\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}
我怎么解决这个问题?
答案1
这是 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}
这里的关键是\resetHeadWidth
调用fancyhdr
宏来定义页眉和页脚宽度的新命令。