Cross-references
References refer to labeled content (e.g. a figure, document or table) and automatically generates links and extra information, like numbering. This page covers the basics of setting up references to content and shows examples for sections, figures, tables and equations.
#1Directive Targets
Targets are custom anchors that you can refer to elsewhere, for example, a figure, section, table, program, or proof. To be referenceable, they must have a label
/identifier
pair in the AST. These can be created by setting the label
option in many directives. For example, to label and reference a figure, use the following syntax:
```{figure} https://source.unsplash.com/random/500x200/?mountain :name: my-fig :align: center My **bold** mountain šš . ``` Check out [](#my-fig)!!
#2Referencing using Links
Cross-referencing content is accomplished with markdown link syntax ([text](#target)
) where #target
is the target label[1], like the figure, equation or section header that you are referencing. If you leave the text empty, MyST will fill in the link with the title, caption, document name, or equation number as appropriate (e.g. "Figure 1" or "Section 1.3.7"). If you do supply text, you can control what is displayed in the reference, as well as have access to placing the name and enumerator of the target, using {name}
and {number}
, respectively[2].
MyST Syntax | Rendered |
---|---|
| Section 3 |
| Sec. 3 |
| Sec. 3 |
| Section "Header Targets" |
| bold reference |
| Citations and bibliography |
| _toc.yml |
#3Header Targets
To add labels to a header use (my-section)=
before the header, these can then be used in markdown links and {ref}
roles. This is helpful if you want to quickly insert links to other parts of your book. Referencing a heading will show the heading and the subsequent two pieces of content[3], unless a header is encountered.
(my-section)= #### Header _Targets_ Use `(label)=` before the element that you want to target, then reference content with: * [](#my-section)
#4Header Numbering
By default section numbering for headers is turned off with numbering for figure and table numbering enabled.
To turn on numbering
for headers, you can can change the frontmatter in the document or project.
--- numbering: heading_2: true heading_3: true --- (my-chapter)= ## My Chapter (my-section)= ### My Section (my-section2)= ### My Second Section * [](#my-chapter) * [](#my-section) * [](#my-section2)
#5Equations Targets
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.
```{math} :label: my-math-label e=mc^2 ``` See [](#my-math-label) for an equation!
#6Notebook Cell Targets
You can label notebook cells using a comment at the top of the cell, using a #| label:
syntax, or have this added directly in the notebook metadata for the cell.
#| label: my-cell
print('hello world')
The cell output or the entire cell can be embedded or referred to using the image or link syntax.
[](#my-cell) - This is a cross-reference to a notebook cell
 - This will embed the output of a notebook cell
or as a figure
directive, where you can then add a caption. If you are referring to that figure in a further cross reference that figure (i.e. not the original cell), give it a new name
.
```{figure} #my-cell
:name: fig-my-cell
```
#7Label Anything
It is possible to label any document node by adding (my-label)=
before any other block of content. These can be referenced using the {ref}
role, but by default will not be enumerated, so you cannot use %s
or {number}
in the content.
(my-paragraph)= This is just a paragraph! (my-points)= * Bullet * points Please see [this paragraph](#my-paragraph) and [these points](#my-points).
#8Referencing using Roles
- ref
- The
{ref}
role can be used to bring the title or caption directly in line, the role can take a single argument which is the label, for example,{ref}`reference-target`
- You can also choose the reference text directly (not taking from the title or caption) by using,
{ref}`your text here <reference-target>`
.
- numref
- The
{numref}
role is exactly the same as the above{ref}
role, but also allows you to use a%s
in place of the number, which will get filled in when the content is rendered. For example,{numref}`Custom Table %s text <my-table-ref>`.
will becomeCustom Table 3 text
.
- eq
- The
{eq}`my-equation`
syntax creates a numbered link to the equation, which is equivalent to[](#my-equation)
as there is no text content to fill in a title or caption.
- doc
- The
{doc}`./my-file.md`
syntax creates a link to the document, which is equivalent to[](./my-file.md)
.