Skip to content

Improve TeX spacing and bounds#165

Merged
Kolaru merged 8 commits into
Kolaru:masterfrom
AshtonSBradley:asb/fix-sqrt-bbox
May 29, 2026
Merged

Improve TeX spacing and bounds#165
Kolaru merged 8 commits into
Kolaru:masterfrom
AshtonSBradley:asb/fix-sqrt-bbox

Conversation

@AshtonSBradley
Copy link
Copy Markdown
Contributor

@AshtonSBradley AshtonSBradley commented May 26, 2026

Summary

Improves TeX spacing/bounds and square-root layout without changing the public API.

The main visible changes are:

  • Keeps Space out of visual ink bounds while preserving horizontal advance, so padding does not inflate labels or rotated text extents.
  • Tightens italic-correction behavior for digit, punctuation, Latin-italic, and Greek-adjacent cases, avoiding cramped labels such as 2ab, over-wide Latin pairs such as ab, and punctuation regressions such as W(\alpha,\alpha^*).
  • Classifies \gtrsim and \lesssim as relation commands, so they receive the same relation spacing as , , and \geq.
  • Refines fraction bounds so padded vincula stay within the expression advance box.
  • Reworks square-root selection so simple short roots use a compact native root, while tall/fraction roots use sized radical variants or scaled fallbacks.
  • Tightens the glyph ascender to its ink top, preventing empty root-glyph ascender space from inflating rotated/y-axis label bounds.
  • Adds regressions for simple roots, fraction roots, short-root descenders, relation spacing, italic spacing, and bbox-sensitive layouts.

No public API changes are intended.

Explanation

The sqrt path now separates base-root and constructed-root use cases. Ordinary glyphs are allowed for simple, short roots where they produce the expected compact shape, but they are not treated as stretchable radicals for fraction or tall roots. Those cases continue through radical.v*/sqrt.v* candidates, with NewComputerModern only used as a missing-glyph fallback. A small descent cap handles fonts whose compact root glyph otherwise hangs too far below short content such as \sqrt{x}.

The bounds changes make visual bounds better match actual ink/rule geometry. This helps labels, rotated TeX, and fraction/radical expressions avoid whitespace or anchoring artifacts from non-ink padding and from font-level ascenders on compact sqrt glyphs.

The spacing changes keep the existing collision-safe Greek behavior, but add a separate compact path for adjacent Latin italic variables and digit-to-Latin implicit multiplication. Relation command classification now also includes \gtrsim and \lesssim, matching their unicode equivalents.

Tests

  • git diff --check
  • Result: passed with no whitespace errors.
  • julia --project=@runic -m Runic --check -d src/engine/layout.jl src/engine/texelements.jl test/layout.jl
  • Result: passed with no formatting diff.
  • julia --project=/Users/braas09p/Dropbox/Julia/Dev/MathTeXEngine.jl test/runtests.jl
  • Result: TeXExpr 1/1, Parser 128/128, Fonts 5/5, Layout 201/201, Generate elements 4/4.

I also regenerated local visual diagnostics for the full radicals/font sheet, the Makie latex_strings sheet, the rotated fraction bbox/vinculum intercept check, and a focused \gtrsim relation-spacing sheet.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 26, 2026

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 88.67925% with 18 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.74%. Comparing base (efdbaab) to head (b1674a3).
⚠️ Report is 37 commits behind head on master.

Files with missing lines Patch % Lines
src/engine/layout.jl 92.00% 10 Missing ⚠️
src/engine/texelements.jl 76.47% 8 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #165      +/-   ##
==========================================
+ Coverage   79.93%   84.74%   +4.80%     
==========================================
  Files          10       10              
  Lines         658     1029     +371     
==========================================
+ Hits          526      872     +346     
- Misses        132      157      +25     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ffreyer
Copy link
Copy Markdown
Contributor

ffreyer commented May 26, 2026

I did some comparing between different versions:

MathTexEngine 0.6.7 MathTexEngine 0.6.8 both prs
Screenshot from 2026-05-26 16-11-09 Screenshot from 2026-05-26 16-13-00 Screenshot from 2026-05-26 16-19-38

It's not actually the ascender that is different but the position returned by generate_tex_elements as well as the descender. And this is specifically because the picked sqrt character is different. It used to be index 5946 and now is 861. This is specifically because of this added characters:

radicals = TeXElement[TeXChar('', state, :symbol)]

Forcing the old choice results in these:

MathTexEngine 0.6.8 both prs
image Screenshot from 2026-05-26 16-53-11

Without the prs the sqrt rendering is an exact match if I pick 5946.

@ffreyer
Copy link
Copy Markdown
Contributor

ffreyer commented May 26, 2026

Oh and just to be clear, I don't care much about the little details like the spacing in 2ab, how long the line in sqrt is, where exactly the 1 is placed and so on. I assume you looked into this and know better than me

@ffreyer
Copy link
Copy Markdown
Contributor

ffreyer commented May 26, 2026

These are the different sqrt options:

image

Is there a good reason to not just remove the first option? It's the only one that's weird and it wasn't there before #156

@AshtonSBradley
Copy link
Copy Markdown
Contributor Author

AshtonSBradley commented May 26, 2026

These are the different sqrt options:

image Is there a good reason to not just remove the first option? It's the only one that's weird and it wasn't there before #156

You are right. I have removed this glyph! Current PR also aims to fix the whitespace issue you identified (and the small a*b spacing, and I noticed multi-argument functions where slightly cramped in argument formatting). I hope this fixes all of those things. The Makie PR has been removed (not necessary once glyph excised).

@AshtonSBradley
Copy link
Copy Markdown
Contributor Author

Short visual update after the font-capability sqrt refactor and fraction-origin tweak.

The square-root reference now keeps the NCM-style radical for simple/non-rule roots, while allowing native sized radicals for fraction/tall roots where the font provides compatible variants. The last row also shows the + \frac{A+B}{J+U} spacing after shifting padded fraction rules so they do not extend left of their own origin.

square_roots_font_resolution

I also refreshed the Makie-style rotation/anchor check; this remains a MathTeXEngine-side fix, with no Makie code change required.

latex_axis_scene_bbox

@ffreyer
Copy link
Copy Markdown
Contributor

ffreyer commented May 26, 2026

Bounding boxes look good to me without the 861 sqrt.

There are a couple more noticeable changes in Makie refimages. Github doesn't let me upload images atm, so I'm just going to list them:

  • sqrt glyphs extends lower (see "new text bounding boxes" Makie refimage tests)
  • Expressions are still more cramped (see "latex strings", "latex ticks")

Not sure if these are improvements or regressions. I wouldn't mind just updating the refimages for them

@AshtonSBradley
Copy link
Copy Markdown
Contributor Author

Bounding boxes look good to me without the 861 sqrt.

There are a couple more noticeable changes in Makie refimages. Github doesn't let me upload images atm, so I'm just going to list them:

  • sqrt glyphs extends lower (see "new text bounding boxes" Makie refimage tests)
  • Expressions are still more cramped (see "latex strings", "latex ticks")

Not sure if these are improvements or regressions. I wouldn't mind just updating the refimages for them

Im just working on the "sqrt glyphs extends lower" problem

@ffreyer ffreyer mentioned this pull request May 27, 2026
3 tasks
@ffreyer
Copy link
Copy Markdown
Contributor

ffreyer commented May 28, 2026

Are you done with the pr?

@AshtonSBradley
Copy link
Copy Markdown
Contributor Author

Thanks again for the careful checks. I pushed one more compact update (d8ec354) addressing the latest spacing/root details:

  • \gtrsim and \lesssim are now classified as relation commands, so U\gtrsim\mu spaces like the unicode U≳\mu and the existing \geq case.
  • Adjacent Latin italic variables now get a small TeX-like tuck (ab, 2ab) without reusing that rule for Greek-adjacent cases such as k\xi, and punctuation boundaries such as W(\alpha,\alpha^*) remain protected.
  • The compact glyph reports its ink-top ascender rather than the full math-font ascender, which removes the empty bbox slack that was showing up as extra whitespace on rotated/y-axis labels.
  • Added focused regression coverage for those cases and reran the local reference sheets.

Local checks: git diff --check, Runic on the touched engine/test files, and test/runtests.jl all pass (Layout 201/201, with the other package test groups passing as well).

@AshtonSBradley
Copy link
Copy Markdown
Contributor Author

This is the current state as regards fonts, sqrt and rotation, and latex strings. Happy to iterate further if needed, but hopefully this has solved it. It might still need reference image updates to merge.

Fontwise, previously several had a lot of breakages on this sheet. I took the view that NCM is the main target, and hopefully we fix some of the others along the way. Not perfect, but I think NCM is solid now, and the others are much improved.

latex strings sheet bbox intercept sheet radicals sheet

@SimonDanisch
Copy link
Copy Markdown
Contributor

Would be nice if we could merge & tag that somewhat quickly. Let us know if we can help with anything! Thank you all!

@ffreyer
Copy link
Copy Markdown
Contributor

ffreyer commented May 29, 2026

Makie refimages look fine to me

@AshtonSBradley
Copy link
Copy Markdown
Contributor Author

@Kolaru let me know what else I should do to get this merged

@Kolaru
Copy link
Copy Markdown
Owner

Kolaru commented May 29, 2026

It all looks good to me, thanks to everyone involved! I'll tag a new version now.

@Kolaru Kolaru merged commit 860f46a into Kolaru:master May 29, 2026
6 of 8 checks passed
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.

5 participants