-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[Bug]: bbox and alignment issues with math text #22459
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
In the usetex case, the origin of the shift is, I believe, the following. Remember that usetex+agg is currently implemented by compiling the tex string to a dvi, converting that to png with dvipng and inserting the image "at the right place". In order to determine that place, we must compute the descent of the text, i.e. how much it extends below the baseline. dvipng cannot report this (actually, it has an option to do this using the TeX Two fixes are possible. One is to stop using the tfm files to get glyph metrics, and instead get them from the actual font files with FreeType. Probably a fair bit of code surgery is required, but this seems doable. The other (already suggested elsewhere) is to stop using dvipng and render the glyphs ourselves. This is in fact already the approach used by the vector backends, which is why you don't see the problem e.g. when saving to pdf. This is also the approach used by mplcairo (even for raster output), so you can also use that in the meantime (probably mplcairo master, the last release of mplcairo is too old to work with matplotlib 3.5). For the mathtext case, regarding vertical positioning, I suspect that the problem comes at least in part from the use of "+1/-1" at matplotlib/lib/matplotlib/mathtext.py Line 143 in 0d0799b
Still in the mathtext case, regarding horizontal spacing, the presence of space before the V is (I think, though I haven't checked) just because it is an italic glyph, which gets drawn a bit further to the left from the origin (technically, it has a positive xMin; see the "glyph metrics" figure at https://freetype.org/freetype2/docs/glyphs/glyphs-3.html); this can be checked by typesetting the non-mathtext case in italic (with |
Thanks a lot @anntzer ! Using mplcairo indeed fixes almost everything. It is not 100% perfect for mathtext: as you can see the 'l' extends a little bit over the box in the "$V$ l" case (the bbox is a tiny bit too short). But that's already much better for my use-case ! |
Bug summary
There are several issues with bbox and text alignement when math is used, as evidenced in the mwe below.
In the 'normal' case (1st line), all is ok.
When using mathtext (2nd), the height of the bounding box depends on the text contents while it should not (no fancy super/under script that would extend beyond 'lp'). Also note that the horizontal alignment is different wrt the 1st and 3rd lines (space to the left of the V, no space to the right of the last letter).
When using 'usetex' (3rd line), the bounding box seems correctly placed, but the text position itself in the second case is off by a little bit (see the 'l' being slightly above the line).
Code for reproduction
Actual outcome
Expected outcome
2nd row: the height of the first bbox should be fixed.
3rd row: the vertical positioning of the text in the second case should be fixed.
Additional information
No response
Operating system
No response
Matplotlib Version
3.5.1
Matplotlib Backend
QtAgg
Python version
3.10.2
Jupyter version
No response
Installation
No response
The text was updated successfully, but these errors were encountered: