-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Description
Problem
When rendering mathematical equations, matplotlib currently only supports a hard-coded number of fonts (configured via mathtext.fontset). While it is possible to configure custom fonts (via mathtext.fontset: custom), this requires to set separate fonts for upright, italic, fraktur, double-struck, etc. variants -- which is fundamentally incompatible with the way modern OpenType mathematics fonts are designed.
The current approach has several shortcomings:
- It is not generally possible to use consistent fonts for text and mathematics (except for special cases covered by the hard-coded options). Think of matching the fonts for a textbook (using STIX Two,Minion, ...) or in a slide show (Cambria)
- The UX of configuring fonts is inconsistent and somewhat counter intuitive.
- (The mathtext codebase and font selection logic is relatively complex and inaccessible, with all the hardcoded fonts, fixes for their quirks, and fallbacks for missing symbols).
Unicode defines mathematical alphanumeric symbols as unique codepoints (see https://en.wikipedia.org/wiki/Mathematical_Alphanumeric_Symbols), in contrast to different fonts all defining different styles for the same ASCII characters/codepoints.
Mathematical formulas can be rendered with OpenType mathematics fonts such as STIX Two Math, Cambria Math, or Latin Modern Math, etc.. For modern LaTeX (LuaTeX and XeTeX), this is implemented in the unicode-math package.
Instead of using different fonts based on the style to render the same codepoints (as it is currently implemented in matplotlib), this maps alphanumeric characters to different codepoints based on the style, and render them from a single font.
Since there is a major overhaul of matplotlib's text rendering currently under way (#30161), I thought now is a good time to bring this up.
Proposed solution
I propose to add support for OpenType mathematics fonts. Basic support is introduced in PR #31064 . The implementation is inspired by the unicode-math package for LaTeX.
This enables to configure and use arbitrary OpenType mathematics fonts to
- match text and math fonts with the surrounding document
- makes the UX more consistent
What do you think of this idea in general?
There are some open design questions:
- How should the fonts be configured?
- How to do unit tests? This may require vendoring at least one OpenType mathematics font. Currently, I have added STIX Two Math to the test data.
The next logical steps would be (i) to make use of the font metrics from the MATH tables instead of using default values (ii) support scaled variants of symbols.