| 1 |
.. _xref: |
| 2 |
|
| 3 |
================ |
| 4 |
Cross-references |
| 5 |
================ |
| 6 |
|
| 7 |
One of Sphinx's most useful features is creating automatic cross-references |
| 8 |
through semantic cross-referencing roles. |
| 9 |
A cross reference to an object description, such as ``:func:`spam```, |
| 10 |
will create a link to the place where ``spam()`` is documented, |
| 11 |
appropriate to each output format (HTML, PDF, ePUB, etc.). |
| 12 |
|
| 13 |
.. _xref-syntax: |
| 14 |
|
| 15 |
Syntax |
| 16 |
------ |
| 17 |
|
| 18 |
Sphinx supports various cross-referencing roles to create links |
| 19 |
to other elements in the documentation. |
| 20 |
In general, writing ``:role:`target``` creates a link to |
| 21 |
the object called *target* of the type indicated by *role*. |
| 22 |
The link's text depends the role but is often the same as or similar to *target*. |
| 23 |
|
| 24 |
.. _xref-modifiers: |
| 25 |
|
| 26 |
The behavior can be modified in the following ways: |
| 27 |
|
| 28 |
* **Custom link text:** |
| 29 |
You can specify the link text explicitly using the same |
| 30 |
notation as in reStructuredText :ref:`external links <rst-external-links>`: |
| 31 |
``:role:`custom text <target>``` will refer to *target* |
| 32 |
and display *custom text* as the text of the link. |
| 33 |
|
| 34 |
* **Suppressed link:** |
| 35 |
Prefixing with an exclamation mark (``!``) prevents the creation of a link |
| 36 |
but otherwise keeps the visual output of the role. |
| 37 |
|
| 38 |
For example, writing ``:py:func:`!target``` displays :py:func:`!target`, |
| 39 |
with no link generated. |
| 40 |
|
| 41 |
This is helpful for cases in which the link target does not exist; |
| 42 |
e.g. changelog entries that describe removed functionality, |
| 43 |
or third-party libraries that don't support :doc:`intersphinx |
| 44 |
</usage/extensions/intersphinx>`. |
| 45 |
Suppressing the link prevents warnings in :confval:`nitpicky` mode. |
| 46 |
|
| 47 |
* **Modified domain reference:** |
| 48 |
When :ref:`referencing domain objects <ref-objects>`, |
| 49 |
a tilde ``~`` prefix shortens the link text to the last component of the target. |
| 50 |
For example, ``:py:meth:`~queue.Queue.get``` will |
| 51 |
refer to ``queue.Queue.get`` but only display ``get`` as the link text. |
| 52 |
|
| 53 |
In HTML output, the link's ``title`` attribute |
| 54 |
(that is e.g. shown as a tool-tip on mouse-hover) |
| 55 |
will always be the full target name. |
| 56 |
|
| 57 |
|
| 58 |
.. _ref-objects: |
| 59 |
|
| 60 |
Cross-referencing objects |
| 61 |
------------------------- |
| 62 |
|
| 63 |
These roles are described with their respective domains: |
| 64 |
|
| 65 |
* :ref:`C <c-xref-roles>` |
| 66 |
* :ref:`C++ <cpp-xref-roles>` |
| 67 |
* :ref:`JavaScript <js-xref-roles>` |
| 68 |
* :ref:`reStructuredText <rst-xref-roles>` |
| 69 |
* :ref:`Python <python-xref-roles>` |
| 70 |
|
| 71 |
|
| 72 |
.. _ref-role: |
| 73 |
|
| 74 |
Cross-referencing arbitrary locations |
| 75 |
------------------------------------- |
| 76 |
|
| 77 |
.. rst:role:: ref |
| 78 |
|
| 79 |
To support cross-referencing to arbitrary locations in any document, |
| 80 |
the standard reStructuredText labels are used. |
| 81 |
For this to work label names must be unique |
| 82 |
throughout the entire documentation. |
| 83 |
There are two ways in which you can refer to labels: |
| 84 |
|
| 85 |
* If you place a label directly before a section title, you can reference to |
| 86 |
it with ``:ref:`label-name```. For example:: |
| 87 |
|
| 88 |
.. _my-reference-label: |
| 89 |
|
| 90 |
Section to cross-reference |
| 91 |
-------------------------- |
| 92 |
|
| 93 |
This is the text of the section. |
| 94 |
|
| 95 |
It refers to the section itself, see :ref:`my-reference-label`. |
| 96 |
|
| 97 |
The ``:ref:`` role would then generate a link to the section, with the |
| 98 |
link title being "Section to cross-reference". This works just as well |
| 99 |
when section and reference are in different source files. |
| 100 |
|
| 101 |
Automatic labels also work with figures. For example:: |
| 102 |
|
| 103 |
.. _my-figure: |
| 104 |
|
| 105 |
.. figure:: whatever |
| 106 |
|
| 107 |
Figure caption |
| 108 |
|
| 109 |
In this case, a reference ``:ref:`my-figure``` would insert a reference |
| 110 |
to the figure with link text "Figure caption". |
| 111 |
|
| 112 |
The same works for tables that are given an explicit caption using the |
| 113 |
:dudir:`table` directive. |
| 114 |
|
| 115 |
* Labels that aren't placed before a section title can still be referenced, |
| 116 |
but you must give the link an explicit title, using this syntax: |
| 117 |
``:ref:`Link title <label-name>```. |
| 118 |
|
| 119 |
.. note:: |
| 120 |
|
| 121 |
Reference labels must start with an underscore. When referencing a label, |
| 122 |
the underscore must be omitted (see examples above). |
| 123 |
|
| 124 |
Using :rst:role:`ref` is advised over standard reStructuredText links to |
| 125 |
sections (like ```Section title`_``) because it works across files, when |
| 126 |
section headings are changed, will raise warnings if incorrect, and works |
| 127 |
for all builders that support cross-references. |
| 128 |
|
| 129 |
|
| 130 |
Cross-referencing documents |
| 131 |
--------------------------- |
| 132 |
|
| 133 |
.. versionadded:: 0.6 |
| 134 |
|
| 135 |
There is also a way to directly link to documents: |
| 136 |
|
| 137 |
.. rst:role:: doc |
| 138 |
|
| 139 |
Link to the specified document; the document name can be a relative or absolute |
| 140 |
path and is always case-sensitive, even on Windows. For example, if the reference |
| 141 |
``:doc:`parrot``` occurs in the document ``sketches/index``, then the link |
| 142 |
refers to ``sketches/parrot``. If the reference is ``:doc:`/people``` or |
| 143 |
``:doc:`../people```, the link refers to ``people``. |
| 144 |
|
| 145 |
If no explicit link text is given (like usual: ``:doc:`Monty Python members |
| 146 |
</people>```), the link caption will be the title of the given document. |
| 147 |
|
| 148 |
|
| 149 |
Referencing downloadable files |
| 150 |
------------------------------ |
| 151 |
|
| 152 |
.. versionadded:: 0.6 |
| 153 |
|
| 154 |
.. rst:role:: download |
| 155 |
|
| 156 |
This role lets you link to files within your source tree |
| 157 |
that are not reStructuredText documents that can be viewed, |
| 158 |
but files that can be downloaded. |
| 159 |
|
| 160 |
When you use this role, the referenced file is automatically marked for |
| 161 |
inclusion in the output when building (obviously, for HTML output only). |
| 162 |
All downloadable files are put into a ``_downloads/<unique hash>/`` |
| 163 |
subdirectory of the output directory; duplicate filenames are handled. |
| 164 |
|
| 165 |
An example:: |
| 166 |
|
| 167 |
See :download:`this example script <../example.py>`. |
| 168 |
|
| 169 |
The given filename is usually relative to the directory the current source |
| 170 |
file is contained in, but if it absolute (starting with ``/``), it is taken |
| 171 |
as relative to the top source directory. |
| 172 |
|
| 173 |
The ``example.py`` file will be copied to the output directory, and a |
| 174 |
suitable link generated to it. |
| 175 |
|
| 176 |
Not to show unavailable download links, you should wrap whole paragraphs that |
| 177 |
have this role:: |
| 178 |
|
| 179 |
.. only:: builder_html |
| 180 |
|
| 181 |
See :download:`this example script <../example.py>`. |
| 182 |
|
| 183 |
Cross-referencing figures by figure number |
| 184 |
------------------------------------------ |
| 185 |
|
| 186 |
.. versionadded:: 1.3 |
| 187 |
|
| 188 |
.. versionchanged:: 1.5 |
| 189 |
:rst:role:`numref` role can also refer sections. |
| 190 |
And :rst:role:`numref` allows ``{name}`` for the link text. |
| 191 |
|
| 192 |
.. rst:role:: numref |
| 193 |
|
| 194 |
Link to the specified figures, tables, code-blocks and sections; |
| 195 |
the standard reStructuredText labels are used. |
| 196 |
When you use this role, it will insert a reference to |
| 197 |
the figure with link text by its figure number like "Fig. 1.1". |
| 198 |
|
| 199 |
If an explicit link text is given (as usual: ``:numref:`Image of Sphinx (Fig. |
| 200 |
%s) <my-figure>```), the link caption will serve as title of the reference. |
| 201 |
As placeholders, `%s` and `{number}` get replaced by the figure |
| 202 |
number and `{name}` by the figure caption. |
| 203 |
If no explicit link text is given, the :confval:`numfig_format` setting is |
| 204 |
used as fall-back default. |
| 205 |
|
| 206 |
If :confval:`numfig` is ``False``, figures are not numbered, |
| 207 |
so this role inserts not a reference but the label or the link text. |
| 208 |
|
| 209 |
Cross-referencing other items of interest |
| 210 |
----------------------------------------- |
| 211 |
|
| 212 |
The following roles do possibly create a cross-reference, but do not refer to |
| 213 |
objects: |
| 214 |
|
| 215 |
.. rst:role:: confval |
| 216 |
|
| 217 |
A configuration value or setting. |
| 218 |
Index entries are generated. |
| 219 |
Also generates a link to the matching :rst:dir:`confval` directive, |
| 220 |
if it exists. |
| 221 |
|
| 222 |
.. rst:role:: envvar |
| 223 |
|
| 224 |
An environment variable. Index entries are generated. Also generates a link |
| 225 |
to the matching :rst:dir:`envvar` directive, if it exists. |
| 226 |
|
| 227 |
.. rst:role:: token |
| 228 |
|
| 229 |
The name of a grammar token (used to create links between |
| 230 |
:rst:dir:`productionlist` directives). |
| 231 |
|
| 232 |
.. rst:role:: keyword |
| 233 |
|
| 234 |
The name of a keyword in Python. This creates a link to a reference label |
| 235 |
with that name, if it exists. |
| 236 |
|
| 237 |
.. rst:role:: option |
| 238 |
|
| 239 |
A command-line option to an executable program. This generates a link to |
| 240 |
a :rst:dir:`option` directive, if it exists. |
| 241 |
|
| 242 |
|
| 243 |
The following role creates a cross-reference to a term in a |
| 244 |
:ref:`glossary <glossary-directive>`: |
| 245 |
|
| 246 |
.. rst:role:: term |
| 247 |
|
| 248 |
Reference to a term in a glossary. A glossary is created using the |
| 249 |
``glossary`` directive containing a definition list with terms and |
| 250 |
definitions. It does not have to be in the same file as the ``term`` markup, |
| 251 |
for example the Python docs have one global glossary in the ``glossary.rst`` |
| 252 |
file. |
| 253 |
|
| 254 |
If you use a term that's not explained in a glossary, you'll get a warning |
| 255 |
during build. |
| 256 |
|
| 257 |
This role also supports :ref:`custom link text <xref-modifiers>` from the general |
| 258 |
cross-reference syntax. |
| 259 |
|
| 260 |
|
| 261 |
.. _any-role: |
| 262 |
|
| 263 |
Cross-referencing anything |
| 264 |
-------------------------- |
| 265 |
|
| 266 |
.. rst:role:: any |
| 267 |
|
| 268 |
.. versionadded:: 1.3 |
| 269 |
|
| 270 |
This convenience role tries to do its best to find a valid target for its |
| 271 |
reference text. |
| 272 |
|
| 273 |
* First, it tries standard cross-reference targets that would be referenced |
| 274 |
by :rst:role:`doc`, :rst:role:`ref` or :rst:role:`option`. |
| 275 |
|
| 276 |
Custom objects added to the standard domain by extensions (see |
| 277 |
:meth:`.Sphinx.add_object_type`) are also searched. |
| 278 |
|
| 279 |
* Then, it looks for objects (targets) in all loaded domains. It is up to |
| 280 |
the domains how specific a match must be. For example, in the Python |
| 281 |
domain a reference of ``:any:`Builder``` would match the |
| 282 |
``sphinx.builders.Builder`` class. |
| 283 |
|
| 284 |
If none or multiple targets are found, a warning will be emitted. In the |
| 285 |
case of multiple targets, you can change "any" to a specific role. |
| 286 |
|
| 287 |
This role is a good candidate for setting :confval:`default_role`. If you |
| 288 |
do, you can write cross-references without a lot of markup overhead. For |
| 289 |
example, in this Python function documentation:: |
| 290 |
|
| 291 |
.. function:: install() |
| 292 |
|
| 293 |
This function installs a `handler` for every signal known by the |
| 294 |
`signal` module. See the section `about-signals` for more information. |
| 295 |
|
| 296 |
there could be references to a glossary term (usually ``:term:`handler```), a |
| 297 |
Python module (usually ``:py:mod:`signal``` or ``:mod:`signal```) and a |
| 298 |
section (usually ``:ref:`about-signals```). |
| 299 |
|
| 300 |
The :rst:role:`any` role also works together with the |
| 301 |
:mod:`~sphinx.ext.intersphinx` extension: when no local cross-reference is |
| 302 |
found, all object types of intersphinx inventories are also searched. |
| 303 |
|