Sam Hooke

Sphinx: Cross-referencing with MyST

In Sphinx, using the MyST extension enables using Markdown (instead of ReStructuredText). To create a cross-reference, first you must enable auto-generated header anchors by adding this line to your §
myst_heading_anchors = 3

you can then use this syntax: §
# Page 1

This is [a cross-reference to "My section" on page 2](./ §
# Page 2

## My section

Hello, world!

Clicking the link on will then take you to “My section” on

Troubleshooting §

myst.xref_missing §

If you get an error such as the following:

C:\path\to\project\source\ WARNING: local id not found in doc 'page_2': 'my-section' [myst.xref_missing]

This is likely because you need to set myst_heading_anchors in your conf.py2. By default, MyST does not generate heading anchors, and so is unable to create the cross-reference unless if you explicitly enable this setting.

  1. The number 3 is how many headings deep MyST should generate anchors. If you are cross-referencing a ##### level 5 heading you would need to set this to 5↩︎

  2. At first, I tried to solve this by creating explicit targets. This appeared to work, but it turns out the WARNING only shows the first time you run sphinx-build if you make no changes to the file, so it was masking the fact that the underlying issue was still present. Using myst_heading_anchors is also much nicer because the anchors are generated automatically. ↩︎

These are rough notes from whatever I was working on, interested in or thinking about at the time. They vary greatly in quality and length, but prove useful to me, and hopefully to you too!

← Previous: Shipping e-bike batteries from the USA to the UK
Next: GitLab: Enable coverage reporting with pytest →