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

问题描述 投票:3回答:1

我们正在探索Sphinx的功能,以便重建我们的遗留手册。我们已将大部分以前的手册移植到Sphinx。现在我正在探索适应我们公司风格的可能性。

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

因此,我在conf.py中使用fancyhdr包中的自定义pagestyle包含以下序言。

latex_elements = {
    'preamble' : '''\
        \\pagestyle{fancy}
        \\fancyhf{}
        \\fancyhead[LE,RO]{My Header}'''
}

不幸的是,标题只在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}
  }
}

可能的解决方法是什么?

latex python-sphinx tex pdflatex
1个回答
4
投票

目录代码(在sphinxmanual.cls中)最终得到

\ifdefined\fancyhf\pagestyle{normal}\fi

sphinx.sty的评论说:

  % Use \pagestyle{normal} as the primary pagestyle for text.

因此,最简单的应该是你的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
""",
}
© www.soinside.com 2019 - 2024. All rights reserved.