Repository navigation
NEW: Reference amsmath environments by their \label - #1215
Open
saimskywalker wants to merge 1 commit into
Open
saimskywalker wants to merge 1 commit into
saimskywalker wants to merge 1 commit into
Conversation
\label\label
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #202
This handles the case from the original issue: a numbered amsmath environment with a single
\label{name}can now be referenced with{eq}`name`.What changed
\label{...}in the environment content, ignoring%comments (but not\%). If it finds exactly one, that label is used for themath_blockinstead of the random uuid. It goes through the sameadd_math_targetpath as$$...$$ (label), so it is registered with the math domain, numbered, and gets anequation-<label>id. Duplicate labels give the usualref.equationwarning. Labels with colons work.\labelis left in the content, as before, so the rendered math in HTML doesn't change. Only the id and the equation number become referenceable.myst.math_labelwarning is emitted, none of the labels are used, and the environment is still numbered as before. Labels in starred environments are ignored.nowrapmath, Sphinx writes\label{equation:doc:label}before the content, so\eqrefwould point at the section counter, not the environment. A smallmath_blockLaTeX visitor override handles amsmath blocks whose label comes from the content: it renames the\labelinside the environment toequation:doc:label, so\eqrefgets the right number. Every other math block goes to the translator's normalvisit_math_block.math_block, the same way$$...$$ (label)is handled there, so[](#name)links work.optional.md, and added an example tomath.md.What's not covered
Per-line labels in multi-line environments like
align, e.g.a &= 1 \label{eq1} \\ b &= 2 \label{eq2}. Sphinx's math domain gives one number permath_block, so this would need per-line numbering in HTML and a different AST representation, which is the larger design question you raised in the issue. This PR warns on that case and does not try to guess.Tests
tests/test_renderers/fixtures/amsmath.md: single label, label plus{eq}reference, label in a comment, label after\%, starred environment, multiple labels with the warning.test_amsmathnow also checks warnings, liketest_link_resolutiondoes.docutil_syntax_extensions.txt: label as a name,[link](#eq:a)resolution, and the multiple-label warning.extended_syntaxesbuild: HTML now has<a href="#equation-eq-labelled">(3)</a>, and a new LaTeX build test checksc=4 \label{equation:index:eq:labelled}and\eqref{equation:index:eq:labelled}.masterand pass on this branch. I ran the full suite on Python 3.14 / Sphinx 9.2 / docutils 0.23 and on Python 3.11 / Sphinx 8.2 / docutils 0.21. ruff and mypy are clean, andsphinx-build -nWon the docs passes..texby hand.