References and cross-references#
Because jupyter-book
is built on top of Sphinx,
there are many ways to reference content within your book (or even across other books, or Sphinx websites).
Referencing is accomplished with roles or with markdown link syntax, depending on your use-case. There are a few ways to reference content from your book, depending on what kind of content you’d like to reference.
See also
If you’re getting started, check out Get started with references for more information.
Reference section labels#
Labels are a way to add tags to parts of your content so that you can reference them later on. This is helpful if you want to quickly insert links to other parts of your book. Labels can be added before major elements of a page, such as titles or figures.
To add a label, use the following pattern before the element you wish to label:
(my-label)=
# The thing to label
For example, we’ve added the following label above the header for this section with:
(content:references:labels)=
## Reference section labels
You can insert cross-references to labels in your content with two kinds of syntax:
{ref}`label-text`
[](label-text)
For example, the syntax {ref}`content:references:labels`
or [](content:references:labels)
results in a link to this section like so: Reference section labels.
Reference figures#
To reference a figure in your book, first add a figure and ensure that it has both a name
as well as a caption associated with it:
```{figure} ../images/cool.jpg
:name: my-fig-ref
My figure title.
```
Then, reference the figure by its :name:
value. For example:
source |
result |
---|---|
|
Here is My figure title. |
|
Here is My cool fig |
|
Here is My figure title. |
|
Here is My cool fig |
|
Here is Fig. 1 |
|
Here is Custom Figure 1 text |
Reference tables#
To reference a table, first create a table and ensure that it has a :name:
and a title:
```{table} My table title
:name: my-table-ref
| header 1 | header 2 |
|---|---|
| 3 | 4 |
```
header 1 |
header 2 |
---|---|
3 |
4 |
Here are several ways to reference this content:
source |
result |
---|---|
|
Here is My table title |
|
Here is My cool table |
|
Here is My table title |
|
Here is My cool table |
|
Here is Table 1 |
|
Here is Custom Table 1 text |
Reference content files#
To reference other files of book content, use the {doc}
role, or link directly to another file with Markdown link syntax. For example:
source |
result |
---|---|
|
|
|
Here is A different page |
|
|
|
Here is A different page |
Reference equations#
To reference equations, first insert an equation with a label like so:
```{math}
:label: my-math-ref
w_{t+1} = (1 + r_{t+1}) s(w_t) + y_{t+1}
```
To reference equations, use the {eq}
role. It will automatically insert the number of the equation.
Note that you cannot modify the text of equation links.
For example:
Choose the reference text#
If you’d like to choose the text of the rendered reference link, use the following pattern:
{someref}`your text here <reference-target>`
Above, reference-target
is the reference to which you are referring, and your text here
will be the displayed text on the page.
For example, see the following references:
- {ref}`Here's another references section <content:references:labels>`
- {doc}`Here's the code outputs section <code-outputs>`
Number your references#
You can add numbered references to tables, figures, or sections.
To add a numbered reference to a table or figure, use the {numref}
role.
Use custom text with numbered references#
If you are using custom text with your references, use %s
as a placeholder for the number.
Here's {numref}`Custom Table %s text <my-table-ref>`.
Here’s Custom Table 1 text.
See more examples in the sections linked above.
Reference numbered sections#
To reference numbered sections, you should first enable numbered sections in your Table of Contents.
Then, you may use the {numref}
role in the same way that you use it for Figures or Tables.
Reference with markdown link syntax#
If you wish to use Markdown style syntax, then MyST Markdown will try to find a reference from any of the above reference types (and more!).
This has an advantage, in that you can use nested markdown syntax in your text, for example:
- [A **bolded _reference_** to a page](./myst.md)
- [A reference to a header](content:references:labels)
Leaving the title empty will mean the reference uses the target as text, for example:
[](./myst.md)
Internal vs. External URLs
You can control how MyST Markdown distinguishes between internal references and external URLs in your _config.yml
.
For example,
parse:
myst_url_schemes: [mailto, http, https]
means that [Jupyter Book](https://jupyterbook.org)
will be recognised as a URL, but [Citations](content:citations)
will not:
Check for missing references#
You can check for missing references when building a Jupyter Book. To do so, use the following options:
jupyter-book build -W -n --keep-going docs/
This will check for missing references (-n
) and turn them into errors (-W
),
but will still attempt to run the full build (--keep-going
),
so that you can see all errors in one run.