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

时间:2017-03-14 13:33:00

标签: latex python-sphinx tex pdflatex

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

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

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

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}
  }
}

可能的解决方法是什么?

1 个答案:

答案 0 :(得分: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
""",
}