Skip to content

Comments

mathtext: add mathnormal and distinguish between normal and italic family#31121

Open
llohse wants to merge 8 commits intomatplotlib:text-overhaulfrom
llohse:feat-mathnormal
Open

mathtext: add mathnormal and distinguish between normal and italic family#31121
llohse wants to merge 8 commits intomatplotlib:text-overhaulfrom
llohse:feat-mathnormal

Conversation

@llohse
Copy link

@llohse llohse commented Feb 9, 2026

PR summary

This adds \mathnormal to the mathtext parser and handles mathit and mathnormal/default differently, to replicate the behaviour of LaTeX. In particular, it produces italic digits, whereas normal produces upright digits.

closes #29253

This is implemented as follows:
For cm, add the cmti (Computer Modern Text Italic) and use it for it. Use cmmi for normal, but map digits to cmr (same as in LaTeX). Technically, LaTeX further distinguishes between normal and the default, where the default uses roman digits and normal uses the digits from cmmi, which are slightly offset.

For stix and dejavu, the font for digits is mapped to either upright or italic for normal and it, respesctively.

To facilitate these features, I have moved the _get_font_constants functionality into the respective font classes instead of a global lookup table. This gets rid of the non-local access to the private _get_fonts from the lookup table. Moreover, this change prepares for reading the constants dynamically, as is necessary with generic OpenType math fonts.
If this change is controversial, I could try to work around it.

@QuLogic I have filed the PR against the text-overhaul branch because of the massive changes therein with respect to main.

Examples

cm stix

PR checklist

Copy link
Member

@QuLogic QuLogic left a comment

Choose a reason for hiding this comment

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

We would need a test here, and I think a What's new note for the new math normal option would be good as well.

Can you link to where you obtained the font files? They do not match what I have in my TexLive installation, but there may be some version mismatch.

@llohse
Copy link
Author

llohse commented Feb 14, 2026

Thanks for your comments.

We would need a test here, and I think a What's new note for the new math normal option would be good as well.

Will do. I did not add tests yet, because a lot of tests where failing with the text-overaul branch at the time.

Can you link to where you obtained the font files? They do not match what I have in my TexLive installation, but there may be some version mismatch.

I got them from https://ctan.org/tex-archive/fonts/cm/ps-type1/bakoma.

@llohse
Copy link
Author

llohse commented Feb 14, 2026

@QuLogic it looks like the tests are still failing with the text-overhaul branch here. Do you know a workaround? It is a bit difficult to pinpoint the tests that I need to update, when everything fails.

@QuLogic
Copy link
Member

QuLogic commented Feb 14, 2026

It's a bit complicated; you would have to fetch from the text-overhaul-figures branch, then test against those, and commit only the new ones, if any. Of course, if you're only adding a new test, then that is simpler as you can just add it here directly. However, at the moment, that branch is a bit out of sync with text-overhaul as I'm preparing to merge #31107.

If you do update the test images yourself, please keep it as a separate commit so that it can be stripped before merging.

@llohse
Copy link
Author

llohse commented Feb 15, 2026

Thanks a lot.
I do expect some existing tests to fail, because the behaviour of \mathit has changed (and even uses an entirely different font for computer modern). Those baseline images would have to be updated in any case. I'll look into that, once you get your branch in sync again. In addition, I would add a small test case for \mathnormal.

@QuLogic
Copy link
Member

QuLogic commented Feb 18, 2026

The branches should be in sync again.

@QuLogic
Copy link
Member

QuLogic commented Feb 18, 2026

Taking a quick look at the results, I see in mathfont_cm_10, the mathbf does now look bolder, and in mathfont_cm_20, the mathtt is now fixed-width. Additionally, items like \mathbb are now upright by default; I would have to check if that matches LaTeX though.

There does seem to be something wrong with \mathit though, as in mathfont_cm_{15,16,17} it appears that DejaVu Sans is being used instead of an italic Computer Modern.

@llohse
Copy link
Author

llohse commented Feb 18, 2026

Taking a quick look at the results, I see in mathfont_cm_10, the mathbf does now look bolder, and in mathfont_cm_20, the mathtt is now fixed-width. Additionally, items like \mathbb are now upright by default; I would have to check if that matches LaTeX though.

There does seem to be something wrong with \mathit though, as in mathfont_cm_{15,16,17} it appears that DejaVu Sans is being used instead of an italic Computer Modern.

Thanks for testing. Previously, the BakomaFonts class would force all digits (no matter what font was selected) to rm (cmr10) via the latex_to_bakoma lookup table. This would only happen for cm and was not consistent with the other font classes and with LaTeX (https://www.overleaf.com/learn/latex/Mathematical_fonts#Other_mathematical_fonts), although I am not entirely sure if this replicates some ancient version of LaTeX.

mathfont_cm_{15,16,17} looks correct on my end. It looks like cmti to me, as it should.

That said, some other tests still fail here, like mathtext_cm_00. The letters are displayed upright instead of italic in mathnormal mode. There seems to be a logic bug in the other classes. I'll do some debugging.

@llohse
Copy link
Author

llohse commented Feb 18, 2026

There does seem to be something wrong with \mathit though, as in mathfont_cm_{15,16,17} it appears that DejaVu Sans is being used instead of an italic Computer Modern.

This is likely due to an outdated local font cache, which does not include cmti10. rm ~/.cache/matplotlib/fontlist* prior to running the tests fixed this for me.

Replace lookup table for font constants based on the family name
by methods in their respective classes.

Removes non-local call of private _get_font method in
_get_font_constant_set.

This simplifies implementing dynamically loaded font constants.
To replicate LaTeX behaviour, distinguish between "italic" and "normal"
math. In particular, digits should be set italic in the \mathit env.

For `cm`, use cmti font for "it"
For general UnicodeFont (stix, DejaVu, ...), maps digits to roman or
italic alphabet, depending on "normal" or "it" environment.
@llohse
Copy link
Author

llohse commented Feb 18, 2026

I fixed all of the regressions that I could find. There are still a large number of the computer modern tests failing, which I cannot quite put my finger on.

@QuLogic
Copy link
Member

QuLogic commented Feb 18, 2026

There does seem to be something wrong with \mathit though, as in mathfont_cm_{15,16,17} it appears that DejaVu Sans is being used instead of an italic Computer Modern.

This is likely due to an outdated local font cache, which does not include cmti10. rm ~/.cache/matplotlib/fontlist* prior to running the tests fixed this for me.

Ah right, I forgot it doesn't update for the internal fonts. You can bump the __version__ in FontManager to force an update.

@llohse
Copy link
Author

llohse commented Feb 18, 2026

Some of the remaining test failures with Computer Modern seem to come from metrics being measured on cmti instead of cmmi. One example is _make_space, which measures the width of the 'm' glyph from the 'it' family (which has changed). There are more subtle places, though.

# We're going into math now, so set font to 'normal'
self.push_state()
self.get_state().font = mpl.rcParams['mathtext.default']
self.get_state().font = "normal"
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like this means that mathtext.default becomes effectively unused, except for the default axis height and xheight metrics (looks like an oversight?), and for the default STIX fallback glyph (probably not worth it). I guess that's fine but should be documented?

Copy link
Author

Choose a reason for hiding this comment

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

Good point. Maybe, the better (and least invasive) change would be to change the default value of mathtext.default to 'normal' (and revert this change here, accordingly)?
Otherwise, I agree there would be no much point in keeping this option at all.

But a discussion to drop this entirely is maybe out of scope for this PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

Changing the default to 'normal' seems to be the easiest solution here indeed.

Copy link
Member

Choose a reason for hiding this comment

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

I agree that changing the default is the best option (so the behavior for the user stays the same if they set it to 'regular' in their code).

One odd catch is that we are keeping it around, but changing its meaning from "math like italics" to "text like italics". My understanding is that this is getting us closer to tex behavior, but produces a silent change to us (for example a user that has expilicty set 'it' in their code.

Do we want to pick a different name and leave the behavior if it alone or just be very loud about it in the API change note?

Copy link
Author

@llohse llohse Feb 21, 2026

Choose a reason for hiding this comment

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

I changed the default to "normal". It took me a while to figure out that stylelib/classic.mplstyle is used in the tests and needed to be updated as well.

@tacaswell: Very good point. Let me know what you decide for. Picking a different name would probably require to rename the mathtext.it parameter for consistency, right?

(If we go forward with the OpenType Math feature, there may be very little reason for users to worry about these parameters in the near future.)

@llohse
Copy link
Author

llohse commented Feb 21, 2026

The tests succeed now against the baseline images from QuLogic's branch, except for those few cases that are expected to fail due to the changed behaviour.

The previous failures were caused by the slanted attribute not being set correctly for fontname = normal font in the BakomaFonts class, causing the kerning do be slightly off.

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

Projects

Status: Ready for Review

Development

Successfully merging this pull request may close these issues.

4 participants