Sphinx: Cross-referencing with MyST Markdown
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 conf.py:1
myst_heading_anchors = 3you can then use this syntax:
# Page 1
This is [a cross-reference to "My section" on page 2](./page_2.md#my-section).# Page 2
## My section
Hello, world!Clicking the link on page_1.md will then take you to “My section” on page_2.md.
Troubleshooting §
myst.xref_missing §
If you get an error such as the following:
C:\path\to\project\source\page_1.md:3: 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.
The number
3is how many headings deep MyST should generate anchors. If you are cross-referencing a##### level 5 headingyou would need to set this to5. ↩︎At first, I tried to solve this by creating explicit targets. This appeared to work, but it turns out the
WARNINGonly shows the first time you runsphinx-buildif you make no changes to the file, so it was masking the fact that the underlying issue was still present. Usingmyst_heading_anchorsis also much nicer because the anchors are generated automatically. ↩︎