Render escaped percent signs in TUI math (#57501)

## What changed

Recognize `\%` as a literal `%` in inline and display math, so expressions such as `$3.8\%$` render as `3.8%`.

## Testing

Extend inline and display math snapshots to cover escaped percent signs, including an inline expression with multiplication and a fraction.

GitOrigin-RevId: a0f9935dc8492fe1cd5a0d33d62930c48c5433b8
This commit is contained in:
Eric Traut
2026-10-27 16:20:45 +00:00
committed by copyberry
parent 2edc8dcb0c
commit b07a347381
5 changed files with 10 additions and 1 deletions
@@ -232,6 +232,7 @@ impl MathParser<'_> {
return match self.take()? {
',' | ';' | ':' | ' ' => Some(Layout::text(" ")),
'!' => Some(Layout::text("")),
'%' => Some(Layout::text("%")),
'{' => Some(Layout::text("{")),
'}' => Some(Layout::text("}")),
'|' => Some(Layout::text("‖")),
@@ -29,6 +29,10 @@ $$
$$\frac{1}{2}\frac{3}{4}$$
$$
3.8\%
$$
\[
\int_0^1 x^2\,dx=\frac{1}{3}
\]
@@ -17,6 +17,7 @@ fn unicode_math_inline_snapshot() {
insta::assert_snapshot!(plain(
r"Inline $\alpha^2 + \beta_{10}$ on $\mathbb{R}^n$.
Root $\sqrt{x^2+y^2}$ and fraction $a/\frac{b}{c}$.
Percent: $3.8\%$ and $100000 \times 3.8\% \times \frac{360}{365} \approx 3747.95$.
Unsupported: $\unknown{x_y}$ and $\frac{a}{b}^2$.
Angles: <$x$>, <\(\alpha\)>, and <$\unknown{x}$>.
Code: `$\alpha$`; money: $5 and $10; shell: $HOME.",
@@ -12,6 +12,8 @@ x=────────────
─ ─
2 4
3.8%
1 1
∫ x² dx= ─
0 3
@@ -1,10 +1,11 @@
---
source: tui/src/markdown_render/math_tests.rs
assertion_line: 16
expression: "plain(r\"Inline $\\alpha^2 + \\beta_{10}$ on $\\mathbb{R}^n$.\nRoot $\\sqrt{x^2+y^2}$ and fraction $a/\\frac{b}{c}$.\nUnsupported: $\\unknown{x_y}$ and $\\frac{a}{b}^2$.\nAngles: <$x$>, <\\(\\alpha\\)>, and <$\\unknown{x}$>.\nCode: `$\\alpha$`; money: $5 and $10; shell: $HOME.\",\n80)"
expression: "plain(r\"Inline $\\alpha^2 + \\beta_{10}$ on $\\mathbb{R}^n$.\nRoot $\\sqrt{x^2+y^2}$ and fraction $a/\\frac{b}{c}$.\nPercent: $3.8\\%$ and $100000 \\times 3.8\\% \\times \\frac{360}{365} \\approx 3747.95$.\nUnsupported: $\\unknown{x_y}$ and $\\frac{a}{b}^2$.\nAngles: <$x$>, <\\(\\alpha\\)>, and <$\\unknown{x}$>.\nCode: `$\\alpha$`; money: $5 and $10; shell: $HOME.\",\n80)"
---
Inline α² + β₁₀ on ℝⁿ.
Root √(x²+y²) and fraction a/((b)/(c)).
Percent: 3.8% and 100000 × 3.8% × ((360)/(365)) ≈ 3747.95.
Unsupported: $\unknown{x_y}$ and $\frac{a}{b}^2$.
Angles: <x>, <α>, and <$\unknown{x}$>.
Code: $\alpha$; money: $5 and $10; shell: $HOME.