Skip to content

Route selected mathtext glyphs to Computer Modern#744

Merged
cvanelteren merged 6 commits into
mainfrom
fix/mathtext-computer-modern-routing
Jul 19, 2026
Merged

Route selected mathtext glyphs to Computer Modern#744
cvanelteren merged 6 commits into
mainfrom
fix/mathtext-computer-modern-routing

Conversation

@cvanelteren

Copy link
Copy Markdown
Collaborator

Closes #717

This patch allows users to use Computer Modern font for certain latex symbols.

test
snippet
import ultraplot as uplt

expr = (
    r"$\mathcal{ABCXYZ}\quad"
    r"\sum_{i=0}^{n}\quad"
    r"\prod_{j=1}^{m}\quad"
    r"\int_a^b\quad"
    r"\oint_C$"
)

fig, axs = uplt.subplots(
    nrows=2,
    refwidth=9,
    refheight=1.7,
    share=False,
    span=False,
)

contexts = (
    (
        "Control: DejaVu math fontset",
        {
            "mathtext.fontset": "dejavusans",
            "mathtext.default": "it",
        },
    ),
    (
        "UltraPlot patched default: selective Computer Modern",
        {},
    ),
)

for ax, (title, rc_kw) in zip(axs, contexts):
    with uplt.rc.context(rc_kw):
        ax.text(0.02, 0.5, expr, transform="axes", fontsize=34, va="center")
    ax.format(title=title, titleloc="left")

axs.format(xlocator="null", ylocator="null", xspineloc="none", yspineloc="none")
uplt.show()

@cvanelteren

Copy link
Copy Markdown
Collaborator Author

This patch adds an opt-in mathtext.cm to the RC settings that leaves the current math rendering unchanged by default. When it is enabled it routes \mathcal through Computer Modern (cmsy10) and selected large operators (\sum, \prod, \coprod, \int, \oint) through Matplotlib's CM/Bakoma while preserving the active UltraPlot font for normal letters and numbers.

@cvanelteren

cvanelteren commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator Author

Can you test this PR @lzhang1994 to see if this would work for you?

@codecov

codecov Bot commented Jun 29, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.20280% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
ultraplot/internals/fonts.py 90.24% 3 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

properly. The following changes are made:

- Rename mathtext.cm to mathtext.cm_symbols to avoid confusion with
  matplotlib's mathtext.fontset 'cm'
- Clear matplotlib's mathtext parse cache when the setting changes so
  toggling takes effect on long-lived parsers
- Load Bakoma fonts lazily and read the rc setting once per parse
- Keep Computer Modern cal when mathtext.cal is set to 'regular'
- Warn once when Bakoma fonts are unavailable on old matplotlib
- Broaden routing to all cmex10 big operators
- Fix docs example: mathtext parses at draw time, so rc.context around
  ax.text had no effect
- Outline the three levels of math font customization: matching the
  document font, Computer Modern symbol routing, and full usetex
- Demonstrate per-text-object math fonts with a multi-row comparison
- Give mathtext.cm_symbols its own linkable subsection with richer
  before/after comparison figures
@cvanelteren

Copy link
Copy Markdown
Collaborator Author

We can merge this as this is opt-in so it won't hurt and gives more options to the end user.

@cvanelteren
cvanelteren merged commit fa26dee into main Jul 19, 2026
18 checks passed
@cvanelteren
cvanelteren deleted the fix/mathtext-computer-modern-routing branch July 19, 2026 11:48
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.

Math font

1 participant