@@ -2,26 +2,28 @@ Cross-referencing with Sphinx
2
2
=============================
3
3
4
4
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.
6
6
7
7
.. _target to paragraph :
8
8
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.
10
10
This works, but it has some disadvantages:
11
11
12
12
- 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.
16
16
17
17
ReStructuredText has a built-in way to linking to elements,
18
18
and Sphinx extends this to make it even more powerful!
19
19
Some advantages of using reStructuredText's references:
20
20
21
- - Use a name instead of the final link .
21
+ - Use a human-readable name of your choice, instead of a URL .
22
22
- Portable between formats: html, PDF, ePub.
23
23
- 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.
25
27
26
28
.. contents :: Table of contents
27
29
:local:
@@ -33,15 +35,20 @@ Some advantages of using reStructuredText's references:
33
35
Explicit targets
34
36
----------------
35
37
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.
38
42
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 **.
41
44
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:
45
52
46
53
.. code-block :: rst
47
54
@@ -53,7 +60,9 @@ For example, one way of creating a target to a section is:
53
60
Then we can reference the section using ```My target`_ ``,
54
61
that will be rendered as `My target `_.
55
62
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:
57
66
58
67
.. code-block :: rst
59
68
@@ -66,13 +75,14 @@ Then we can reference it using ```target to paragraph`_``,
66
75
that will be rendered as: `target to paragraph `_.
67
76
68
77
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:
70
79
```my custom text <target to paragraph>`_ ``,
71
80
that will be rendered as: `my custom text <target to paragraph >`_.
72
81
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:
76
86
77
87
.. code-block :: rst
78
88
@@ -85,9 +95,12 @@ that will be rendered as: `inline target`_.
85
95
Implicit targets
86
96
----------------
87
97
98
+ You may also reference sections by name without explicitly giving them one by
99
+ using *implicit targets *.
100
+
88
101
When we create a section,
89
102
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`_ ``,
91
104
that will be rendered as: `Explicit targets `_.
92
105
93
106
.. note ::
@@ -113,7 +126,7 @@ Next we will explore the most common ones.
113
126
The ref role
114
127
~~~~~~~~~~~~
115
128
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:
117
130
118
131
.. code-block :: rst
119
132
@@ -149,9 +162,11 @@ that will be rendered as: :ref:`code <target to code>`.
149
162
The doc role
150
163
~~~~~~~~~~~~
151
164
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:
155
170
156
171
.. code-block :: rst
157
172
@@ -167,21 +182,24 @@ That will be rendered as:
167
182
168
183
.. tip ::
169
184
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.
172
187
173
188
The numref role
174
189
~~~~~~~~~~~~~~~
175
190
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:
178
195
179
196
.. code-block :: python
180
197
181
198
# Enable numref
182
199
numfig = True
183
200
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
+
185
203
For example, we can create a target for the next image:
186
204
187
205
.. _target to image :
@@ -204,18 +222,18 @@ For example, we can create a target for the next image:
204
222
205
223
Link me!
206
224
207
- Then we can reference it using ``:numref:`target to image` ``,
225
+ Finally, reference it using ``:numref:`target to image` ``,
208
226
that will be rendered as :numref: `target to image `.
209
227
Sphinx will enumerate the image automatically.
210
228
211
- Auto section label
212
- ------------------
229
+ Automatically label sections
230
+ ----------------------------
213
231
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,
216
234
:doc: `autosectionlabel <sphinx:usage/extensions/autosectionlabel >`.
217
235
218
- Add this to your ``conf.py `` file:
236
+ To activate the `` autosectionlabel `` extension, add this to your ``conf.py `` file:
219
237
220
238
.. _target to code :
221
239
@@ -230,7 +248,7 @@ Add this to your ``conf.py`` file:
230
248
autosectionlabel_prefix_document = True
231
249
232
250
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} `` .
234
252
235
253
For example, we can reference the previous section using:
236
254
@@ -255,24 +273,25 @@ On Read the Docs you can use the :ref:`config-file/v2:sphinx.fail_on_warning` op
255
273
Finding the reference name
256
274
--------------------------
257
275
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.
260
279
261
- Sphinx allows you to explore all targets with:
280
+ List all targets for built documentation with:
262
281
263
282
.. prompt :: bash
264
283
265
284
python -m sphinx.ext.intersphinx <link>
266
285
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
268
287
(``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:
270
289
271
290
.. prompt :: bash
272
291
273
292
python -m sphinx.ext.intersphinx https://docs.readthedocs.io/en/stable/objects.inv
274
293
275
- More
276
- ----
294
+ Cross-referencing targets in other documentation sites
295
+ ------------------------------------------------------
277
296
278
297
You can reference to docs outside your project too! See :doc: `/guides/intersphinx `.
0 commit comments