You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: Changing text.latex.preamble does not invalidate cached usetex text metrics #32413
With usetex, Matplotlib caches text metrics without the LaTeX preamble in the cache key. After text.latex.preamble changes, redrawing an existing figure keeps the old text widths. On 3.10 and later, a new Text in the same figure also gets the old width. Only a new figure measures the text correctly.
Code for reproduction
importmatplotlibmatplotlib.use("Agg")
importmatplotlibasmplimportmatplotlib.pyplotaspltfig, ax=plt.subplots()
text=ax.text(0.1, 0.5, "Hmx minimum", usetex=True, fontsize=30)
fig.canvas.draw()
print("before:", text.get_window_extent().width)
withmpl.rc_context({"text.latex.preamble": r"\usepackage{helvet}"}):
fig.canvas.draw()
print("after, same Text:", text.get_window_extent().width)
new=ax.text(0.1, 0.2, "Hmx minimum", usetex=True, fontsize=30)
fig.canvas.draw()
print("after, new Text in the same figure:", new.get_window_extent().width)
fig2, ax2=plt.subplots()
fresh=ax2.text(0.1, 0.5, "Hmx minimum", usetex=True, fontsize=30)
fig2.canvas.draw()
print("after, new figure:", fresh.get_window_extent().width)
Actual outcome
On 3.11.2 (3.10.9 gives the same):
before: 240.82454619639245
after, same Text: 240.82454619639245
after, new Text in the same figure: 240.82454619639245
after, new figure: 264.75527647110005
On 3.5.3, the new Text in the same figure measures 264.755 and the same Text keeps 240.825.
Expected outcome
All three "after" widths should be 264.755, the width of the text set in Helvetica.
Additional information
matplotlib.text._get_text_metrics_with_cache keys its cache on the renderer, the text, a copy of the font properties, ismath, and dpi. For ismath="TeX" the metrics also depend on text.latex.preamble, which is not in the key, so a cached Computer Modern measurement is reused for Helvetica text. Before 3.10 the layout cache was per Text, which is why 3.5.3 measures a new Text correctly but still reuses the old width for an existing one. Other rcParamsTexManager reads, such as font.family, are probably affected the same way; I verified only the preamble.
A possible fix: when ismath="TeX", include the TeX font configuration in the key, for example text.latex.preamble and the font settings TexManager reads.
I found this while adding usetex support to curved-text, a library that draws text along curves; its labels inherit the same stale widths.
I'd like to work on this issue. I plan to investigate the text metrics cache and TexManager configuration dependencies, then add a regression test for changing text.latex.preamble
Activity
himanchalkaushale commentedon Oct 1, 2026
Hi! I’ve reproduced the issue locally and would like to investigate and work on a fix. May I take this up?
thiebes commentedon Oct 1, 2026
Please do!
himanchalkaushale commentedon Oct 3, 2026
can you assign this to me
thiebes commentedon Oct 5, 2026
I don't see a way to assign it.
rcomer commentedon Oct 5, 2026
@himanchalkaushale you are welcome to submit a pull request for any issue, but we do not assign them. Please see our contributors’ guide:
https://matplotlib.org/devdocs/devel/index.html
And in particular our AI policy
https://matplotlib.org/devdocs/devel/contribute.html#generative-ai
tanush78 commentedon Oct 11, 2026
I'd like to work on this issue. I plan to investigate the text metrics cache and TexManager configuration dependencies, then add a regression test for changing text.latex.preamble
TEST: Make test_usetex_preamble_cache_invalidation deterministic