【问题标题】:Customize headers and footers in Latex using Sphinx使用 Sphinx 在 Latex 中自定义页眉和页脚
【发布时间】:2017-03-14 13:32:59
【问题描述】:

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

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

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

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

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

sphinx.sty 的以下 sn-p 可能会导致问题:

% 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】:

    目录代码(sphinxmanual.cls)以

    结尾
    \ifdefined\fancyhf\pagestyle{normal}\fi
    

    sphinx.sty 中的评论说:

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

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

    如果您使用\py@HeaderFamily,则需要将整个乳胶包裹在\makeatletter...\makeatother 中。并使用 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
    """,
    }
    

    【讨论】:

    • 如何在 conf.py 中覆盖 \fancypagestyle{normal}??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-30
    • 1970-01-01
    • 1970-01-01
    • 2013-12-03
    • 2012-02-13
    • 2011-12-27
    • 2017-10-18
    相关资源
    最近更新 更多