修改现有标头的内容

修改现有标头的内容

我正在使用一个设置一些标题的文档类(使用 fancyhdr),我想修改(而不是覆盖)这些标题。例如:

文档类创建了一个右侧标题,上面写着“默认文本”。我想将其更改为“默认文本。我的文本”。

在我的 tex 文件中,如果我写入:

\fancyhead[R]{My Text.}

那么先前包含在右侧标题中的文本将被删除,并且仅出现“我的文本。”。

是否有某种方法可以检索标头的内容,以便我可以在修改后的标头中重用该内容?例如:

\fancyhead[R]{\fetchRightHeader My Text.}

理想情况下,这将导致右侧标题显示“默认文本。我的文本”。

以防万一:我需要修改偶数页的右侧页眉和奇数页的左侧页眉。文档类在左侧和右侧包含不同的文本。

附加问题:有没有办法修改由文档类定义的 fancypagestyle?文档类为首页设置了不同的样式,以便为该页使用不同的页眉。我也想修改这些首页页眉。

谢谢!

编辑:

根据建议安德鲁,这是一个最小的工作示例。

\documentclass{article}

\usepackage{fancyhdr}
\usepackage{filecontents}

% Another file defines a header, which I want to modify.
\begin{filecontents*}{style.cls}
  \pagestyle{fancy}
  \fancyhead[R]{Default Text.}
\end{filecontents*}
\input{style.cls}

% This line overwrites the previously defined header,
% whereas I would like to keep the previous contents
% and also add my own.
\fancyhead[R]{My Text.}

\begin{document}
\section{Section 1}
Foo bar baz.
\end{document}

以下是上述示例生成的 PDF 输出:

示例的实际输出

作为参考,这是我想要的输出:

期望输出

答案1

对我来说做

\pagestyle{fancy}
\fancyhf[H]
{
 % header contents
}
\fancypagestyle{styleName}
{
 % specific page header content
}

为了澄清起见,我使用创建了一个通用标题\fancyhf[H]{},然后使用创建了一个特定的页面标题\fancypagestyle{}{},我无法说出此功能何时实现以及这个答案是否有任何价值,但我会把它留在这里,以便那些可能像我一样偶然发现这里的人。

答案2

您也可以将内容保存在宏中,然后使用其他宏进行修改:

\documentclass{article}
\usepackage[twoside]{fancyhdr}
\edef\versoheadtext{Starting Verso Header}
\def\changeversohead#1{
  \edef\tempstring{\versoheadtext}
  \xdef\versoheadtext{\tempstring\space#1}
}
\edef\rectoheadtext{Starting Recto Header}
\def\changerectohead#1{
  \edef\tempstring{\rectoheadtext}
  \xdef\rectoheadtext{\tempstring\space#1}
}
\pagestyle{fancy}
\fancyhead[CE]{\versoheadtext}
\fancyhead[CO]{\rectoheadtext}
\begin{document}
It\par\clearpage
was\par\clearpage
\changerectohead{ ..darker}
\changeversohead{ ..stormier}
a\par\clearpage
dark\par\clearpage
and\par\clearpage
stormy\par\clearpage
night.\par\clearpage
\end{document}

相关内容