使用 Sphinx 在 Latex 中自定义页眉和页脚

Ale*_*ph0 6 latex tex pdflatex python-sphinx

我们正在探索 Sphinx 的功能,以重建我们的旧手册。我们已经将大部分以前的手册移植到了 Sphinx。现在我正在探索调整我们公司风格的可能性。

特别是,我们想更改 PDF 手册中页眉和页脚的外观。包括公司徽标和更改偶数页和奇数页的外观。

因此,我在conf.py使用包的自定义页面样式中包含了以下序言fancyhdr

latex_elements = {
    'preamble' : '''\
        \\pagestyle{fancy}
        \\fancyhf{}
        \\fancyhead[LE,RO]{My Header}'''
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,标题只在 之前更改begin{document},之后 Sphinx 样式文件sphinx.sty以某种方式覆盖了我的设置。

以下代码片段sphinx.sty可能会导致问题:

% Redefine the 'normal' header/footer style when using "fancyhdr" package:
\spx@ifundefined{fancyhf}{}{
  % Use \pagestyle{normal} as the primary pagestyle for text.
  \fancypagestyle{normal}{
    \fancyhf{}
    \fancyfoot[LE,RO]{{\py@HeaderFamily\thepage}}
    \fancyfoot[LO]{{\py@HeaderFamily\nouppercase{\rightmark}}}
    \fancyfoot[RE]{{\py@HeaderFamily\nouppercase{\leftmark}}}
    \fancyhead[LE,RO]{{\py@HeaderFamily \@title, \py@release}}
    \renewcommand{\headrulewidth}{0.4pt}
    \renewcommand{\footrulewidth}{0.4pt}
    % define chaptermark with \@chappos when \@chappos is available for Japanese
    \spx@ifundefined{@chappos}{}
      {\def\chaptermark##1{\markboth{\@chapapp\space\thechapter\space\@chappos\space ##1}{}}}
  }
  % Update the plain style so we get the page number & footer line,
  % but not a chapter or section title.  This is to keep the first
  % page of a chapter and the blank page between chapters `clean.'
  \fancypagestyle{plain}{
    \fancyhf{}
    \fancyfoot[LE,RO]{{\py@HeaderFamily\thepage}}
    \renewcommand{\headrulewidth}{0pt}
    \renewcommand{\footrulewidth}{0.4pt}
  }
}
Run Code Online (Sandbox Code Playgroud)

可能的解决方法是什么?

小智 5

目录代码 (in sphinxmanual.cls) 以

\ifdefined\fancyhf\pagestyle{normal}\fi
Run Code Online (Sandbox Code Playgroud)

评论里sphinx.sty说:

  % Use \pagestyle{normal} as the primary pagestyle for text.
Run Code Online (Sandbox Code Playgroud)

因此,最简单的应该是您的conf.py设置覆盖\fancypagestyle{normal},只需根据您的喜好重新发布即可。

您需要包装在整个乳胶\makeatletter...\makeatother,如果你使用\py@HeaderFamily。并使用 Python 原始字符串以避免将所有反斜杠加倍。


详细来说,这里我将原始定义复制到,conf.py以便可以从那里进行自定义

latex_elements = {
  'preamble': """
\makeatletter
  \fancypagestyle{normal}{
    \fancyhf{}
    \fancyfoot[LE,RO]{{\py@HeaderFamily\thepage}}
    \fancyfoot[LO]{{\py@HeaderFamily\nouppercase{\rightmark}}}
    \fancyfoot[RE]{{\py@HeaderFamily\nouppercase{\leftmark}}}
    \fancyhead[LE,RO]{{\py@HeaderFamily \@title, \py@release}}
    \renewcommand{\headrulewidth}{0.4pt}
    \renewcommand{\footrulewidth}{0.4pt}
    % define chaptermark with \@chappos when \@chappos is available for Japanese
    \spx@ifundefined{@chappos}{}
      {\def\chaptermark##1{\markboth{\@chapapp\space\thechapter\space\@chappos\space ##1}{}}}
  }
\makeatother
""",
}
Run Code Online (Sandbox Code Playgroud)

  • 我怎样才能覆盖conf.py中的\fancypagestyle{normal}? (3认同)