Search code examples
latexpython-sphinxrestructuredtextbibliography

Use Sphinx to make a bibliography in a latex document


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.


Solution

  • 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