Internal Documentation Resources¶
This is a guide for formatting your content with reStructuredText in Sphinx. A textual data format developed by the Docutils project as a documentation tool for Python. The installation of this documentation site can be found in the project’s readme.md file, this page is solely intended for teaching the basics of Sphinx and reStructuredText and provide you with links for further documentation and guides for the more complex stuff.
Creating Python extensions to extend, modify or disable any function of the rST pre-processor is out of scope for this guide, we will only cover the basic configuration parameters for modifying the content of the base furo template (such as title, logo, copyright etc.) and where else to replace them in the static files where reading those values is not possible due to Sphinx’s internal rendering logic (404 error page etc.) and the content-tree and formatting within Sphinx itself.
This guide will refer to blocks of content either as “block(s)“ or “node(s)“ depending on the context, they refer to any type of content found in reStructuredText that follow the same syntax.
The guide will also use the words “parent(s)“ and “child(ren)“ to refer to content nodes/blocks embedded into each other.
Now: Let’s get through the most important bits
White spaces and Indentation¶
It’s a Python based framework after all…
Important
Indentation and spacing is critical for reStructuredText documents. Unless otherwise stated, always use two spaces for indentation, and always leave an empty line between content blocks.
For inline formatting, such as **bold** or *italic* text, `links <...>`_, note that they cannot be combined, you cannot emphasize a link or part of a link etc. And you must leave white space around them. If you want to remove the white space around an element, you must escape them with dashes:
This is a re\ **Structured**\ Text example
Result: This is a reStructuredText example.
Otherwise they won’t render as intended.
Directives and the TOC-tree¶
One of Sphinx’s main feature is the dynamically generated document-tree, which collects data from Sphinx’s toctree directive, and builds the links based on them. The directory and link structure is completely ignored in the process, you need to add the main links to your index.rst document in the root of your source page. And it will also serve as the landing page of the documentation site.
Note
This is that page.
Another compelling feature of reStructuredText is that you can define “directives“ via plugins and extensions on top of Sphinx’s main ones, which can handle content for you in various different ways. We will cover the most important default ones and the general directives syntax in this guide.
TOC-tree¶
As mentioned you need to add the toctree to your index.rst document, it can be placed anywhere you’d be happy to render it within the page. You can add it to your document after the title, or the end of it, or anything in-between.
Caution
The document title has to be the first thing in your document after the optional meta tags, or the page won’t be rendered and added to your TOC-tree.
The syntax of the toctree directive is as follows:
.. toctree::
:maxdepth: N
:caption: Optional Title for your TOC
pages-in-the-order
youd-like-to
show-them
The :caption: variable is optional, it will be shown as the heading of the contents. The :maxdepth: variable will tell Sphinx how deep the list should be rendered out, if it is shown in the content.
The :maxdepth: variable is ignored in the side-bar, it will always show every added item in the side-bar menu. It is only relevant to the page’s rendered content.
Alternatively, you can also add the :hidden: variable to the TOC-tree, so that it won’t render on the page itself.
.. toctree::
:hidden:
pages-to-add
to-the-menu
Important
The content of the toctree directive takes both relative and absolute paths (using the source folder as root), watch out for those slashes at the beginning of the file names.
The best practice is to have your directroy structure follow your intended menu layout and add only the files in the folder named after your page. This will avoid confusion and recursion errors.
Important
Once again, the :maxdepth: and :hidden: variables don’t affect what is shown in the side-bar. The only way to hide a page from the menu is to not add it to the DOC-tree at all, and link to it via the :doc: role instead.
Warning
DO NOT CROSS-REFERENCE IN THE TOC-TREE! AND NEVER ADD THE INDEX FILE ITSELF TO IT!
It will create an infinite loop resulting in a recursion error and break the page rendering, as Sphinx offers no protection against it other than stopping it completely. NEVER DO SOMETHING LIKE THIS:
tools.rst
.. toctree::
:hidden:
tools/backup
tools/recovery
tutorials
tutorials.rst
.. toctree::
:hidden:
tutorials/backup
tutorials/recovery
tools
Adding the same entry to multiple paths, even if it doesn’t cause an error, it will still cause the side-bar to treat the first occuring entry as the highlighted menu entry, regardless of the path taken to open it. It will also open up all other sub-menus that contain the item.
Directives and Syntax¶
Directives are the main way you can use Sphinx’s more advanced content formattings and interactive content.
Every directive is hard-coded into Sphinx (or its plugins and extensions), and they follow the same format:
.. DIRECTIVE::
:VARIABLE: VALUE(S)
:VARIABLE: VALUE(S)
CONTENT
Alternatively, if the directive takes a single variable, only its values need to be defined:
.. DIRECTIVE:: VALUE(S)
CONTENT
As each directive needs to be properly indented, all of them are handled as blocks, so in order to use them inline (such as a custom image that you want to use as an icon) you must create a reference point at the bottom of your document, and embed the reference in the content. Similarly how footnotes and referenced links work in Markdown.
You can read about them in the following section.
Internal links and References¶
Internal links¶
The main advantage of rST is the ability to cross-link between pages and link thier contents easily. You can use both absolute (again, the source directory being its root) and relative links to do that.
In order to reference internal links you have to use the :doc: role. The syntax of roles is as follow:
:ROLE:`CONTENT <OPTIONAL-DATA>`
Therefore you can embed internal links like this:
:doc:`Link text </link-to/absolute-path>`
:doc:`Link text <link-to/relative-path>`
Best practices would be to link to child pages with relative paths, and any other pages with absolute paths. If the absolute paths are too long, you might consider relative links as well, but bear in mind that stepping up multiple folders can become confusing:
/absolute/path/to/the/very/deeply/nested/page-20
:doc:`Link to Page 10 </absolute/path/to/page-10>`
/absolute/path/to/the/very/deeply/nested/page-20
:doc:`Link to Page 10 <../../../../page-10>`
I’ll let you figure out whether I messed up the double-dot counts.
Internal references¶
You can create reference point inside a document using the following syntax:
.. _REFERENCE-POINT:
You can then reference this point from either the same or any other document using the ref role:
:ref:`Link text <REFERENCE-POINT>`
Important
Note the leading underscore in the reference point, and the lack of it in the ref role. This is because you need to tell Sphinx (via the underscore) that you are defining a new value, and not trying to load a non-existent directive.
Also, by creating a reference-point just before headers, you will automatically assign the header text as the default text for the reference.
So creating a reference point such as:
.. _reference-point:
Document header
---------------
Now if you use the reference role on your pages, :ref:`reference-point` will automatically show the text Document header. Unless of course you want to provide your own text, :ref:`Link text <reference-point>`, like so.
Caution
These reference points function as unique IDs, they need to be unique across the whole website, and they must be valid HTML/CSS IDs[1].
For best practices use the absolute paths of the file they’re in, replacing the slashes with double-dashes:
.. _absolute--path--reference-point:
This way you can be sure that no reference-points are the same across multiple pages.
Footnotes and annotations¶
You can also include annotations in your document, there are two default styling to do so: [*]_ and [#]_
The first annotation will use the star character and derivatives (cross, double cross etc.) to mark the footnotes. While using the pound character will give you a numeric list.
These references then can be added to your document as blocks (with the rubric directive optionally to give it a title):
.. rubric:: Footnotes
.. [#] Footnote explanation 1
.. [#] Footnote explanation 2
.. rubric:: Footnotes
.. [*] Footnote explanation 1
.. [*] Footnote explanation 2
The footnotes will be linked automatically in the order they occur. But you can also manually assign the numbers to them:
.. [1] Footnote explanation 1
.. [2] Footnote explanation 2
Which is useful if you intend to refer multiple parts of the content to the same explanation ([1]_, [2]_ etc.).
External links and references¶
You can, of course, also use just hyperlinks to link to content. The syntax for them is the following:
`Link text <link.url>`_
If there’s a link that you need to include more frequently, however, you might want to define it like a reference-point, with one notable difference being the link following it:
.. _link: link.url
You can then reference it as such:
`link`_
With this, the basics are covered now. You can follow along this guide, or alternatively, visit the Sphinx rST documentation regarding reStructuredText for a more accurate and thorough guide. We will still go over some further basic info and the most commonly used stylings:
Further information on Sphinx and rST¶
This covers the basic information in the following topics:
Footnotes

Amelabs Docs