Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Sphinx-generated index to the sidebar when using Read the Docs theme?

Tags:

I'd like to have a link to the automatically generated index in the sidebar when using sphinx-rtd-theme. I've tried adding it to the toctree:

.. toctree::

    first
    second
    Index <:ref:`genindex`>

but this resulted in

WARNING: toctree contains reference to nonexisting document u':ref:`geinindex`'

from Sphinx and no other effect.

I think I could simply hardcode the index in the theme layout.html file, but perhaps there is some better way, not involving modifying the standard theme?

TIA for any hints!

like image 391
VZ. Avatar asked Aug 11 '14 12:08

VZ.


People also ask

What are the sidebars in the Sphinx HTML theme?

See the Sphinx HTML sidebars documentation for more information. By default, this theme comes with these three theme-specific sidebar elements enabled on all pages: sidebar-logo.html: Displays the logo and site title. search-field.html: A bootstrap-based search bar (from the PyData Sphinx Theme)

How do I use read the Docs in Sphinx?

Open this file in your web browser to see your docs. Your Sphinx project is built. Edit your files and rebuild until you like what you see, then commit your changes and push to your public repository. Once you have Sphinx documentation in a public repository, you can start using Read the Docs by importing your docs.

Can I use Markdown and reStructuredText with Sphinx?

You can use Markdown using MyST and reStructuredText in the same Sphinx project. We support this natively on Read the Docs, and you can do it locally: You can now continue writing your docs in .md files and it will work with Sphinx.

What is the purpose of the Sphinx theme?

This blog postprovides some info about the design, but in short, the theme aims to solve the limitations of Sphinx’s default navigation setup, where only a small portion of your docs were accessible in the sidebar. Our theme is also meant to work well on mobile and tablet devices. Contributing to the theme¶


2 Answers

It's easy if you understand how Sphinx and Jinja work. Unfortunately the Sphinx docs on templating don't give you enough info if you don't. In short, you'll have to override the template:

  • Make sure you have a _templates folder under your sphinx docs folder.
  • Make sure it is listed in your conf.py, e.g. templates_path = ['_templates']
  • Create a file inside the folder called layout.html.
  • Put this snippet inside and save. The exclamation point/mark forces jinja to use the parent template. Don't forget it, or you'll get a recursion error. You only need to override the menu block.

    {% extends "!layout.html" %}
    
      {% block menu %}
        {{ super() }}
        <a href="genindex.html">Index</a>
      {% endblock %}
    
like image 132
Gringo Suave Avatar answered Oct 05 '22 02:10

Gringo Suave


How about:

.. toctree::

    first
    second

* :ref:`genindex`
like image 20
Steven Almeroth Avatar answered Oct 05 '22 03:10

Steven Almeroth