2

I am currently using Sphinx to produce a latex document. I have some problems with the Bibliography. I would like the Bibliography to appear in the table of contents without a chapter number.

When I include the Bibliography as a separate section, for example using the following reStructured text file:

************
Bibliography
************

.. bibliography:: NullFeaturesInQGIS.bib
   :style: plain

I end up with a numbered chapter called "Bibliography" and then the actual "Bibliography" two pages later.

Copy of the table of contents from the latex pdf file

What I would like to achieve is a table of contents heading of "Bibliography" and that to point to the Bibliography without additional empty pages.

mzjn
  • 48,958
  • 13
  • 128
  • 248
Philip Whitten
  • 293
  • 2
  • 16
  • I presume that you use the sphinxcontrib-bibtex extension. What happens if you simply remove the `Bibliography` section heading? – mzjn Dec 16 '18 at 06:42
  • If I remove the Bibliography section heading I still get two empty pages, and, I don't get any Table of Contents. – Philip Whitten Dec 16 '18 at 10:41
  • For a temporary solution I have separate index.rst files for html and latex output from Sphinx. Within the index.rst file for html I include a Bibliography section with a bibliography directive in the toctree (table of contents), in the index.rst file for latex I have removed both the Bibliography section and the bibliography directive. This gives me the result that I need. – Philip Whitten Dec 28 '18 at 21:03

2 Answers2

2

Two different approaches are shown below that creates a Bibliography section in both the html and latex outputs from Sphinx.

1. Using two distinct "index" restructured text files

One approach that creates a Bibliography section in both the Sphinx html and latex outputs uses two index restructured text files.

For the html output, the index.rst file should be like:

===============
Project Heading
===============

.. toctree::
   :maxdepth: 2
   :caption: Contents:

   section_1
   section_2
   section_3
   bibliography

For the latex output, the index_latex.rst file should be like:

===============
Project Heading
===============

.. toctree::
   :maxdepth: 2
   :caption: Contents:

   section_1
   section_2
   section_3

The bibliography.rst file should be like:

************
Bibliography
************

.. bibliography:: bibtex_filename.bib
   :style: plain

Within the Sphinx configuration file (e.g. conf.py), you need to distinguish between the two different index files. For example:

# The html index document.
master_doc = 'index'

# The latex index document
latex_doc = 'index_latex'

2. Using one index.rst file and using the .. raw:: directive

What follows was adapted from https://github.com/sphinx-doc/sphinx/issues/4775. This approach uses the same index.rst file for both html and latex outputs. The index.rst file should be the same as shown for html output above, and, should include a reference to the bibliography.rst file. The, bibliography.rst file needs to have the .. raw:: directive at the beginning:

.. raw:: latex

   \cleardoublepage
   \begingroup
   \renewcommand\chapter[1]{\endgroup}
   \phantomsection

************
Bibliography
************

.. bibliography:: bibtex_filename.bib
   :style: plain

NOTE

Using the Sphinx .. only:: directive with a single index.rst file, like shown below DOES NOT WORK. Specifically, the latex document will be missing content. This is possibly due to 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
Philip Whitten
  • 293
  • 2
  • 16
  • maybe this [hack](https://github.com/sphinx-doc/sphinx/issues/4775#issuecomment-375906390) using raw latex, suitably adapted, could avoid two separated index.rst files (untested) –  Jan 01 '19 at 09:34
  • Thanks jfbu, it tested the hack that you referenced and it works without error. I have now included it in my answer. – Philip Whitten Jan 06 '19 at 19:08
0

Using the Sphinx .. only:: directive in the bibliography.rst file like this:

.. only:: html

    ************
    Bibliography
    ************

.. bibliography:: bibtex_filename.bib
   :style: plain

and keeping one index.rst file like this:

===============
Project Heading
===============

.. toctree::
   :maxdepth: 2
   :caption: Contents:

   section_1
   section_2
   section_3
   bibliography

solved it for me.

user2549818
  • 199
  • 1
  • 5