mathtext: add mathnormal and distinguish between normal and italic family#31121
mathtext: add mathnormal and distinguish between normal and italic family#31121llohse wants to merge 8 commits intomatplotlib:text-overhaulfrom
Conversation
QuLogic
left a comment
There was a problem hiding this comment.
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.
95d012c to
c3a69c6
Compare
|
Thanks for your comments.
Will do. I did not add tests yet, because a lot of tests where failing with the text-overaul branch at the time.
I got them from https://ctan.org/tex-archive/fonts/cm/ps-type1/bakoma. |
|
@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. |
3b4303a to
cd3eb9a
Compare
|
It's a bit complicated; you would have to fetch from the If you do update the test images yourself, please keep it as a separate commit so that it can be stripped before merging. |
|
Thanks a lot. |
|
The branches should be in sync again. |
|
Taking a quick look at the results, I see in There does seem to be something wrong with |
Thanks for testing. Previously, the BakomaFonts class would force all digits (no matter what font was selected) to
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. |
This is likely due to an outdated local font cache, which does not include cmti10. |
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.
82f1cc6 to
16fe453
Compare
|
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. |
Ah right, I forgot it doesn't update for the internal fonts. You can bump the |
|
Some of the remaining test failures with Computer Modern seem to come from metrics being measured on cmti instead of cmmi. One example is |
lib/matplotlib/_mathtext.py
Outdated
| # 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" |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Changing the default to 'normal' seems to be the easiest solution here indeed.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.)
|
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 |
PR summary
This adds
\mathnormalto the mathtext parser and handlesmathitandmathnormal/default differently, to replicate the behaviour of LaTeX. In particular,itproduces italic digits, whereasnormalproduces upright digits.closes #29253
This is implemented as follows:
For
cm, add thecmti(Computer Modern Text Italic) and use it forit. Usecmmifornormal, but map digits tocmr(same as in LaTeX). Technically, LaTeX further distinguishes betweennormaland the default, where the default uses roman digits andnormaluses the digits from cmmi, which are slightly offset.For
stixanddejavu, 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
PR checklist