Skip to content

Conversation

@Leedehai
Copy link
Contributor

This PR adds a new math element math.number with the introduction of struct NumberElem, so that number (e.g. $2.1$) and number-like text (e.g. $"2.1"$) can be differentiated. A number can contain multiple digits and a decimal point in the middle, as controlled by the lexer's math_text().

This way, as demonstrated in the new ref image math-number-parsed.png, we can apply a different rendering procedure to the numbers (see the changes in this PR's math/text.rs).

This PR fixes #7574 (see the new ref image issue-7574-footnote-inside-math.png). The footnote notation (e.g. 1 and note 2) doesn't really have the semantics of a number in math, and thus should be rendered as text. See comments this and this on that issue.

This PR changed the ref image enum-numbering-closure-nested-complex.png, because a numbered list isn't in a math context, and therefore the bullet point indexes should be rendered as text.

This PR changed the code snippet of test math-attach-nested-base in attach.typ, because [0] is a content block holding a text of zero, while [$0$] forces the zero to be a number in math context. This difference is also specifically tested in the new test math-number-parsed (see a0 and b0 there).

CC @mkorje

@MDLC01
Copy link
Collaborator

MDLC01 commented Dec 14, 2025

Would this PR make it possible to change the math class of the period back to Punctuation without affecting numbers (#5700)?

@Leedehai
Copy link
Contributor Author

Leedehai commented Dec 14, 2025

@MDLC01 this can be done with ease 🎊 by adding these diffs on top of this PR:

--- a/crates/typst-utils/src/lib.rs
+++ b/crates/typst-utils/src/lib.rs
@@ -373,7 +373,9 @@ pub fn default_math_class(c: char) -> Option<MathClass> {
 
         // Better spacing.
         // https://github.com/typst/typst/pull/1855
-        '.' | '/' => Some(MathClass::Normal),
+        '/' => Some(MathClass::Normal),
+
+        '.' => Some(MathClass::Punctuation),

--- a/crates/typst-layout/src/math/text.rs
+++ b/crates/typst-layout/src/math/text.rs
@@ -126,7 +126,10 @@ pub fn layout_number(
 
         // This won't panic as ASCII digits and '.' will never end up as
         // nothing after shaping.
-        let glyph = GlyphFragment::new_char(ctx, styles, c, span).unwrap();
+        let mut glyph = GlyphFragment::new_char(ctx, styles, c, span).unwrap();
+        if c == '.' {
+            glyph.class = MathClass::Normal;
+        }
         fragments.push(glyph.into());
     }
     let frame = MathRun::new(fragments).into_frame(styles);

and I can verify it works as intended :) However, I'd say it's worth opening a separate PR after this current one's acceptance, because this additional change will break these tests and their fix needs more thinking:

  • math-class-exceptions, especially what to do with d_0.d_1d_2. We may want to do d_0\.d_1d_2.
  • math-mat-align-explicit-mixed, which I'm not sure how to modify, because 18&&.02 and the like is not parsed by the the lexer's math_text() as one number due to alignment point && in the middle, and thus the dot (as a MathClass::Punctuation) will introduce a space there. We might need to modify the lexer to make it right (or resign ourselves to this fact).
  • issue-2044-invalid-parsed-ident.

@Mc-Zen
Copy link
Contributor

Mc-Zen commented Dec 19, 2025

I wonder what the consequences would be for numerous packages that generate numbers in equations from strings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Footnotes in math are not superscript

3 participants