Title: [Bug]: Changing text.latex.preamble does not invalidate cached usetex text metrics · Issue #32413 · matplotlib/matplotlib · GitHub
Open Graph Title: [Bug]: Changing text.latex.preamble does not invalidate cached usetex text metrics · Issue #32413 · matplotlib/matplotlib
X Title: [Bug]: Changing text.latex.preamble does not invalidate cached usetex text metrics · Issue #32413 · matplotlib/matplotlib
Description: Bug summary 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 fig...
Open Graph Description: Bug summary 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....
X Description: Bug summary 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....
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/32413
X: @github
Domain: github.com
IPv4 address: 140.82.114.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Bug]: Changing text.latex.preamble does not invalidate cached usetex text metrics","articleBody":"### Bug summary\n\nWith 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.\n\n### Code for reproduction\n\n```Python\nimport matplotlib\nmatplotlib.use(\"Agg\")\nimport matplotlib as mpl\nimport matplotlib.pyplot as plt\n\nfig, ax = plt.subplots()\ntext = ax.text(0.1, 0.5, \"Hmx minimum\", usetex=True, fontsize=30)\nfig.canvas.draw()\nprint(\"before:\", text.get_window_extent().width)\n\nwith mpl.rc_context({\"text.latex.preamble\": r\"\\usepackage{helvet}\"}):\n fig.canvas.draw()\n print(\"after, same Text:\", text.get_window_extent().width)\n\n new = ax.text(0.1, 0.2, \"Hmx minimum\", usetex=True, fontsize=30)\n fig.canvas.draw()\n print(\"after, new Text in the same figure:\", new.get_window_extent().width)\n\n fig2, ax2 = plt.subplots()\n fresh = ax2.text(0.1, 0.5, \"Hmx minimum\", usetex=True, fontsize=30)\n fig2.canvas.draw()\n print(\"after, new figure:\", fresh.get_window_extent().width)\n```\n\n### Actual outcome\n\nOn 3.11.2 (3.10.9 gives the same):\n\n- before: 240.82454619639245\n- after, same Text: 240.82454619639245\n- after, new Text in the same figure: 240.82454619639245\n- after, new figure: 264.75527647110005\n\nOn 3.5.3, the new Text in the same figure measures 264.755 and the same Text keeps 240.825.\n\n### Expected outcome\n\nAll three \"after\" widths should be 264.755, the width of the text set in Helvetica.\n\n### Additional information\n\n`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 `rcParams` `TexManager` reads, such as `font.family`, are probably affected the same way; I verified only the preamble.\n\nA possible fix: when `ismath=\"TeX\"`, include the TeX font configuration in the key, for example `text.latex.preamble` and the font settings `TexManager` reads.\n\nI found this while adding usetex support to [curved-text](https://github.com/thiebes/curved-text), a library that draws text along curves; its labels inherit the same stale widths.\n\n### Operating system\n\nWindows 11\n\n### Matplotlib Version\n\n3.11.2 (also 3.10.9 and 3.5.3)\n\n### Matplotlib Backend\n\nAgg\n\n### Python version\n\n3.13.3\n\n### Jupyter version\n\nnone\n\n### Installation\n\npip","author":{"url":"https://github.com/thiebes","@type":"Person","name":"thiebes"},"datePublished":"2026-10-01T08:35:55Z","url":"https://github.com/matplotlib/matplotlib/issues/32413"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:6ff27164-7f77-7d5b-f3f0-5fab06862d9a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D474:1139E2:EC6D65:13C8D1A:6B064EA3 |
| html-safe-nonce | 5d9ad0c07783ce65fe5a99a15bcc3674501fe8d4cf6d1600cf9be81d645d0d8a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENDc0OjExMzlFMjpFQzZENjU6MTNDOEQxQTo2QjA2NEVBMyIsInZpc2l0b3JfaWQiOiIxMjk3MDU2NDE2OTY4MTAxNTM5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 94608d7c1ebc4f2faf84fe157cc738b00aebc8a5e54946fdd2bd334119c47d4d |
| hovercard-subject-tag | issue:5659364814 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/matplotlib/matplotlib/32413/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d65cad45c524793b1c3ae7975dd2048a68a7498a45826b05138ad22e26ad8266/matplotlib/matplotlib/issues/32413 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d65cad45c524793b1c3ae7975dd2048a68a7498a45826b05138ad22e26ad8266/matplotlib/matplotlib/issues/32413 |
| og:image:alt | Bug summary 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.... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | thiebes |
| hostname | github.com |
| expected-hostname | github.com |
| None | 772b6997ca4daa0428dd49a25d76052e765555ae560fe9c88517eb828bbd861e |
| turbo-cache-control | no-preview |
| go-import | github.com/matplotlib/matplotlib git https://github.com/matplotlib/matplotlib.git |
| octolytics-dimension-user_id | 215947 |
| octolytics-dimension-user_login | matplotlib |
| octolytics-dimension-repository_id | 1385122 |
| octolytics-dimension-repository_nwo | matplotlib/matplotlib |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1385122 |
| octolytics-dimension-repository_network_root_nwo | matplotlib/matplotlib |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f1f9a08f7c769a37f1dfc18ad43d463c222a7eea |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width