Skip to content

fix: keep latex script markers - #303

Merged
RivoLink merged 1 commit into
RivoLink:mainfrom
MeJamoLeo:fix/latex-script-fallback
Sep 28, 2026
Merged

RivoLink merged 1 commit into
RivoLink:mainfrom
MeJamoLeo:fix/latex-script-fallback

Conversation

@MeJamoLeo

Copy link
Copy Markdown
Contributor

convert_script maps the body of ^{...} / _{...} to Unicode superscript or subscript characters. When at least one character has no such form — π, ∞, ε, or a nested script — it falls back to ({group}), and the ^ / _ itself is dropped along the way. The exponent then reads as multiplication and an integral bound reads as function application, so the rendering says something different from the source rather than merely looking plainer.

Keeping the marker in the fallback is enough to remove the ambiguity: e^{i\pi} renders as e^(iπ) instead of e(iπ). The conversion rule itself is unchanged, so anything that already had a Unicode form still gets it — n^2, A^T, M^{-1}, x_{max}, n^{abc} and ∑ᵢ₌₁ⁿ are untouched.

Reproduce

$$e^{i\pi} + 1 = 0$$

$$\int_{0}^{\infty} e^{-x^2} dx = \frac{\sqrt{\pi}}{2}$$

leaf --inline=plain --width 50 repro.md

Before:

e(iπ) + 1 = 0

∫₀(∞) e(−x²) dx = √(π)/2

After:

e^(iπ) + 1 = 0

∫₀^(∞) e^(−x²) dx = √(π)/2

Both formulas are already in the repository: demo/sources/demo-latex-render.md shows the Euler–Poisson integral, and TESTING.md shows Euler's identity and the Basel problem. All three render without their markers on main.

Testing

  • Added superscript_fallback_keeps_caret, subscript_fallback_keeps_underscore and nested_script_fallback_keeps_both_markers; all three fail on main and pass with this change. convertible_scripts_stay_unicode guards the paths that already worked and passes on both.
  • cargo test (504 passed), cargo clippy --all-targets --all-features -- -D warnings, cargo fmt --check, cargo build --release.
  • Rendered every *.md in this repo at 11 widths (20–160) in both inline modes with the old and the new binary: 418 comparisons, identical except demo/sources/demo-latex-render.md and TESTING.md, where the only difference is the restored ^ shown above.
  • Checked the recursive paths by hand — \frac, \sqrt, \binom, \begin{cases}, table cells, and scripts nested in scripts (n^{a_{\varepsilon}} → n^(a_(ε)), 2^{2^{2^n}} → 2^(2^(2ⁿ))) — plus degenerate groups such as n^{ }, n^{S} and non-Latin content. Nothing renders worse than before.

Two rough edges are left as they are, since both read correctly and predate this change: a single-character fallback still gets parentheses (n^{\varepsilon} → n^(ε)), and \left(...\right) inside an exponent yields a doubled pair (n^((1−ε))). Happy to tighten either in a follow-up if that is worth doing.

🤖 Generated with Claude Code

@RivoLink
RivoLink force-pushed the fix/latex-script-fallback branch 2 times, most recently from c8c3b9a to 34e7339 Compare September 28, 2026 08:39
@RivoLink
RivoLink force-pushed the fix/latex-script-fallback branch from 34e7339 to 914fe54 Compare September 28, 2026 09:09
@github-actions

Copy link
Copy Markdown

Warning

The head commit isn't GPG-signed.
Maintainers will re-sign it on merge.

@RivoLink
RivoLink force-pushed the fix/latex-script-fallback branch from 914fe54 to 34e832e Compare September 28, 2026 09:54

@RivoLink RivoLink left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,
Thank you so much for this PR!
I just made some minor optimizations and signed the commit.

@RivoLink
RivoLink merged commit cf386d1 into RivoLink:main Sep 28, 2026
1 check passed
@MeJamoLeo

Copy link
Copy Markdown
Contributor Author

Thank you for your better design!!
I’m learning from your code

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.

2 participants