1. Georg Brandl
  2. sphinx


sphinx / doc / markup / inline.rst

Inline markup

Sphinx uses interpreted text roles to insert semantic markup into documents.

Variable names are an exception, they should be marked simply with *var*.

For all other roles, you have to write :rolename:`content`.


The default role (`content`) has no special meaning by default. You are free to use it for anything you like.

Cross-referencing syntax

Cross-references are generated by many semantic interpreted text roles. Basically, you only need to write :role:`target`, and a link will be created to the item named target of the type indicated by role. The links's text will be the same as target.

There are some additional facilities, however, that make cross-referencing roles more versatile:

  • You may supply an explicit title and reference target, like in reST direct hyperlinks: :role:`title <target>` will refer to target, but the link text will be title.

  • If you prefix the content with !, no reference/hyperlink will be created.

  • For the Python object roles, if you prefix the content with ~, the link text will only be the last component of the target. For example, :meth:`~Queue.Queue.get` will refer to Queue.Queue.get but only display get as the link text.

    In HTML output, the link's title attribute (that is e.g. shown as a tool-tip on mouse-hover) will always be the full target name.

Cross-referencing Python objects

The following roles refer to objects in modules and are possibly hyperlinked if a matching identifier is found:

The name enclosed in this markup can include a module name and/or a class name. For example, :func:`filter` could refer to a function named filter in the current module, or the built-in function of that name. In contrast, :func:`foo.filter` clearly refers to the filter function in the foo module.

Normally, names in these roles are searched first without any further qualification, then with the current module name prepended, then with the current module and class name (if any) prepended. If you prefix the name with a dot, this order is reversed. For example, in the documentation of Python's :mod:`codecs` module, :func:`open` always refers to the built-in function, while :func:`.open` refers to :func:`codecs.open`.

A similar heuristic is used to determine whether the name is an attribute of the currently documented class.

Cross-referencing C constructs

The following roles create cross-references to C-language constructs if they are defined in the documentation:

Cross-referencing other items of interest

The following roles do possibly create a cross-reference, but do not refer to objects:

The following role creates a cross-reference to the term in the glossary:

Cross-referencing arbitrary locations

To support cross-referencing to arbitrary locations in any document, the standard reST labels are used. For this to work label names must be unique throughout the entire documentation. There are two ways in which you can refer to labels:

  • If you place a label directly before a section title, you can reference to it with :ref:`label-name`. Example:

    .. _my-reference-label:
    Section to cross-reference
    This is the text of the section.
    It refers to the section itself, see :ref:`my-reference-label`.

    The :ref: role would then generate a link to the section, with the link title being "Section to cross-reference". This works just as well when section and reference are in different source files.

    Automatic labels also work with figures: given

    .. _my-figure:
    .. figure:: whatever
       Figure caption

    a reference :ref:`my-figure` would insert a reference to the figure with link text "Figure caption".

  • Labels that aren't placed before a section title can still be referenced to, but you must give the link an explicit title, using this syntax: :ref:`Link title <label-name>`.

Using :role:`ref` is advised over standard reStructuredText links to sections (like `Section title`_) because it works across files, when section headings are changed, and for all builders that support cross-references.

Cross-referencing documents

There is also a way to directly link to documents:

Referencing downloadable files

Other semantic markup

The following roles don't do anything special except formatting the text in a different style:

The following roles generate external links:

Note that there are no special roles for including hyperlinks as you can use the standard reST markup for that purpose.


The documentation system provides three substitutions that are defined by default. They are set in the build configuration file.