Skip to content

Commit e5c4c5c

Browse files
authored
general improvements to cross-references guide (#7388)
1 parent caf990b commit e5c4c5c

File tree

1 file changed

+62
-43
lines changed

1 file changed

+62
-43
lines changed

docs/guides/cross-referencing-with-sphinx.rst

Lines changed: 62 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,28 @@ Cross-referencing with Sphinx
22
=============================
33

44
When writing documentation you often need to link to other pages of your documentation,
5-
or other sections of the current page, or sections from other pages.
5+
other sections of the current page, or sections from other pages.
66

77
.. _target to paragraph:
88

9-
An easy way is just to use the final link of the page/section.
9+
An easy way is just to use the raw URL that Sphinx generates for each page/section.
1010
This works, but it has some disadvantages:
1111

1212
- Links can change, so they are hard to maintain.
13-
- Links can be verbose, not easy to know the page/section they are linking to.
14-
- Not easy way to link to specific sections like paragraphs, figures, or code blocks.
15-
- It only works for the html version of your documentation.
13+
- Links can be verbose and hard to read, so it is unclear what page/section they are linking to.
14+
- There is no easy way to link to specific sections like paragraphs, figures, or code blocks.
15+
- URL links only work for the html version of your documentation.
1616

1717
ReStructuredText has a built-in way to linking to elements,
1818
and Sphinx extends this to make it even more powerful!
1919
Some advantages of using reStructuredText's references:
2020

21-
- Use a name instead of the final link.
21+
- Use a human-readable name of your choice, instead of a URL.
2222
- Portable between formats: html, PDF, ePub.
2323
- Sphinx will warn you of invalid references.
24-
- Cross reference to more than just pages and sections.
24+
- You can cross reference more than just pages and section headers.
25+
26+
This page describes some best-practices for cross-referencing with Sphinx.
2527

2628
.. contents:: Table of contents
2729
:local:
@@ -33,15 +35,20 @@ Some advantages of using reStructuredText's references:
3335
Explicit targets
3436
----------------
3537

36-
If you are not familiar with reStructuredText,
37-
check :doc:`sphinx:usage/restructuredtext/basics` for a quick introduction.
38+
.. note::
39+
40+
If you are not familiar with reStructuredText,
41+
check :doc:`sphinx:usage/restructuredtext/basics` for a quick introduction.
3842

39-
In reStructuredText we have targets and references,
40-
where the target is the place where a reference links to.
43+
Cross referencing in Sphinx uses two components, **targets** and **references**.
4144

42-
We can reference to any part of a document by adding a target
43-
in the line before the element we want to link to.
44-
For example, one way of creating a target to a section is:
45+
- **references** are pointers in your documentation to other parts of your documentation.
46+
- **targets** are the where references can point.
47+
48+
You can manually create a target in any location of your documentation, allowing
49+
you to reference it from other pages. These are called **explicit targets**.
50+
51+
For example, one way of creating an explicit target for a section is:
4552

4653
.. code-block:: rst
4754
@@ -53,7 +60,9 @@ For example, one way of creating a target to a section is:
5360
Then we can reference the section using ```My target`_``,
5461
that will be rendered as `My target`_.
5562

56-
Another example, a target to a paragraph:
63+
You can also add explicit targets before paragraphs (or any other part of a page).
64+
65+
Another example, here we add a target to a paragraph:
5766

5867
.. code-block:: rst
5968
@@ -66,13 +75,14 @@ Then we can reference it using ```target to paragraph`_``,
6675
that will be rendered as: `target to paragraph`_.
6776

6877
The reference displays the name of the target by default,
69-
but we can change that with any text
78+
but we can use any text you want. For example:
7079
```my custom text <target to paragraph>`_``,
7180
that will be rendered as: `my custom text <target to paragraph>`_.
7281

73-
We can also reference to a place *inside* an element,
74-
this can be done with an _`inline target`.
75-
For example, a target inside a paragraph:
82+
We can also create *in-line targets* within an element on your page,
83+
allowing you to, for example, reference text *within* a paragraph.
84+
85+
For example, an in-line target inside a paragraph:
7686

7787
.. code-block:: rst
7888
@@ -85,9 +95,12 @@ that will be rendered as: `inline target`_.
8595
Implicit targets
8696
----------------
8797

98+
You may also reference sections by name without explicitly giving them one by
99+
using *implicit targets*.
100+
88101
When we create a section,
89102
reStructuredText will create a target with the title as the name.
90-
For example, to reference to the previous section we can use ```Explicit targets`_``,
103+
For example, to reference the previous section we can use ```Explicit targets`_``,
91104
that will be rendered as: `Explicit targets`_.
92105

93106
.. note::
@@ -113,7 +126,7 @@ Next we will explore the most common ones.
113126
The ref role
114127
~~~~~~~~~~~~
115128

116-
The ``ref`` role can be used to reference any explicit target.
129+
The ``ref`` role can be used to reference any explicit target. For example:
117130

118131
.. code-block:: rst
119132
@@ -149,9 +162,11 @@ that will be rendered as: :ref:`code <target to code>`.
149162
The doc role
150163
~~~~~~~~~~~~
151164

152-
The `doc` role allow us to link to a page instead of just a section.
153-
The target name can be a relative or absolute path (without the extension),
154-
for example to link to a page in the same directory as this one we can use:
165+
The `doc` role allows us to link to a page instead of just a section.
166+
The target name can be relative to the page where the role exists, or relative
167+
to your documentation's root folder (in both cases, you should omit the extension).
168+
169+
For example, to link to a page in the same directory as this one we can use:
155170

156171
.. code-block:: rst
157172
@@ -167,21 +182,24 @@ That will be rendered as:
167182

168183
.. tip::
169184

170-
Using an absolute path is recommend,
171-
so we avoid changing the target when the section or page are moved.
185+
Using paths relative to your documentation root is recommended,
186+
so we avoid changing the target name if the page is moved.
172187

173188
The numref role
174189
~~~~~~~~~~~~~~~
175190

176-
The ``numref`` role is used to reference tables and images.
177-
Add this to your ``conf.py`` file:
191+
The ``numref`` role is used to reference **numbered** elements of your documentation.
192+
For example, tables and images.
193+
194+
To activate numbered references, add this to your ``conf.py`` file:
178195

179196
.. code-block:: python
180197
181198
# Enable numref
182199
numfig = True
183200
184-
In order to use this role we need to create an explicit target to the element.
201+
Next, ensure that an object you would like to reference has an explicit target.
202+
185203
For example, we can create a target for the next image:
186204

187205
.. _target to image:
@@ -204,18 +222,18 @@ For example, we can create a target for the next image:
204222
205223
Link me!
206224
207-
Then we can reference it using ``:numref:`target to image```,
225+
Finally, reference it using ``:numref:`target to image```,
208226
that will be rendered as :numref:`target to image`.
209227
Sphinx will enumerate the image automatically.
210228

211-
Auto section label
212-
------------------
229+
Automatically label sections
230+
----------------------------
213231

214-
Adding an explicit target on each section and making sure is unique is a big task!
215-
But Sphinx includes an extension to help us with that problem,
232+
Manually adding an explicit target to each section and making sure is unique
233+
is a big task! Fortunately, Sphinx includes an extension to help us with that problem,
216234
:doc:`autosectionlabel <sphinx:usage/extensions/autosectionlabel>`.
217235

218-
Add this to your ``conf.py`` file:
236+
To activate the ``autosectionlabel`` extension, add this to your ``conf.py`` file:
219237

220238
.. _target to code:
221239

@@ -230,7 +248,7 @@ Add this to your ``conf.py`` file:
230248
autosectionlabel_prefix_document = True
231249
232250
Sphinx will create explicit targets for all your sections,
233-
the name of target is the path of the file (without extension) plus the title of the section.
251+
the name of target has the form ``{path/to/page}:{title-of-section}``.
234252

235253
For example, we can reference the previous section using:
236254

@@ -255,24 +273,25 @@ On Read the Docs you can use the :ref:`config-file/v2:sphinx.fail_on_warning` op
255273
Finding the reference name
256274
--------------------------
257275

258-
Using names is more descriptive than using a raw link,
259-
but is still hard to find the correct reference name of an element.
276+
When you build your documentation, Sphinx will generate an inventory of all
277+
explicit and implicit links called ``objects.inv``. You can list all of these targets to
278+
explore what is available for you to reference.
260279

261-
Sphinx allows you to explore all targets with:
280+
List all targets for built documentation with:
262281

263282
.. prompt:: bash
264283

265284
python -m sphinx.ext.intersphinx <link>
266285

267-
Where the link is the link or local path to your inventory file
286+
Where the link is either a URL or a local path that points to your inventory file
268287
(``usually in _build/html/objects.inv``).
269-
For example, to see all target from the Read the Docs documentation:
288+
For example, to see all targets from the Read the Docs documentation:
270289

271290
.. prompt:: bash
272291

273292
python -m sphinx.ext.intersphinx https://docs.readthedocs.io/en/stable/objects.inv
274293

275-
More
276-
----
294+
Cross-referencing targets in other documentation sites
295+
------------------------------------------------------
277296

278297
You can reference to docs outside your project too! See :doc:`/guides/intersphinx`.

0 commit comments

Comments
 (0)