下面显示了两种不同的方法,它们在 Sphinx 的 html 和 latex 输出中创建参考书目部分。
1.使用两个不同的“索引”重组文本文件
在 Sphinx html 和 latex 输出中创建书目部分的一种方法使用两个 index 重组文本文件。
对于 html 输出,index.rst 文件应该是这样的:
===============
Project Heading
===============
.. toctree::
:maxdepth: 2
:caption: Contents:
section_1
section_2
section_3
bibliography
对于 latex 输出,index_latex.rst 文件应如下所示:
===============
Project Heading
===============
.. toctree::
:maxdepth: 2
:caption: Contents:
section_1
section_2
section_3
bibliography.rst 文件应该是这样的:
************
Bibliography
************
.. bibliography:: bibtex_filename.bib
:style: plain
在 Sphinx 配置文件中(例如conf.py),您需要区分两个不同的索引文件。例如:
# The html index document.
master_doc = 'index'
# The latex index document
latex_doc = 'index_latex'
2。使用一个index.rst 文件并使用.. raw:: 指令
以下内容改编自https://github.com/sphinx-doc/sphinx/issues/4775。此方法对 html 和 latex 输出使用相同的 index.rst 文件。 index.rst 文件应该与上面 html 输出中显示的相同,并且应该包含对 bibliography.rst 文件的引用。 bibliography.rst 文件需要以.. raw:: 指令开头:
.. raw:: latex
\cleardoublepage
\begingroup
\renewcommand\chapter[1]{\endgroup}
\phantomsection
************
Bibliography
************
.. bibliography:: bibtex_filename.bib
:style: plain
注意
将 Sphinx .. only:: 指令与单个 index.rst 文件一起使用,如下所示不起作用。具体来说,latex 文档将缺少内容。这可能是由于problems with the .. only:: directive.
===============
Project Heading
===============
.. only:: html
.. toctree::
:maxdepth: 2
:caption: Contents:
section_1
section_2
section_3
bibliography
.. only:: latex
.. toctree::
:maxdepth: 2
:caption: Contents:
section_1
section_2
section_3