GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix stale usetex text metrics when text.latex.preamble changes by himanchalkaushale · Pull Request #32473 · matplotlib/matplotlib

Conversation

@himanchalkaushale

@himanchalkaushale himanchalkaushale commented Oct 13, 2026

Copy link
Copy Markdown

Closes #32413

Summary

Fixes stale text metrics in usetex mode when text.latex.preamble changes.

_get_text_metrics_with_cache caches the results of renderer.get_text_width_height_descent using the text, font properties, math mode, and DPI as cache keys. However, in usetex mode, the rendered dimensions also depend on text.latex.preamble, which was not included in the cache key.

As a result, changing the preamble could leave existing Text objects with stale metrics. Since Matplotlib 3.10 moved the cache from individual Text objects to the renderer, newly created Text objects in the same figure could also reuse stale metrics. Creating a new figure avoided the issue because it created a new renderer with a fresh cache.

This PR addresses the issue by including the LaTeX preamble in the cache key when ismath == "TeX". For non-TeX text, the preamble remains None, preserving the existing cache-key behavior.

The change is intentionally limited to the preamble. Other TexManager configuration options, such as font settings, are left unchanged because they are outside the scope of this issue.

Tests

Adds a regression test that:

  1. Renders a string with usetex enabled.
  2. Changes the preamble to \usepackage{helvet}.
  3. Verifies that the existing Text object, a newly created Text object in the same figure, and a Text object in a new figure all report consistent text widths.

The test is skipped when the required helvet LaTeX package is unavailable.

The existing test_metrics_cache and test_metrics_cache2 assertions continue to pass.

AI Disclosure

AI assistance was used to draft the regression test cases. The implementation and final test changes were reviewed by me.

PR Checklist

  • Added a regression test for the reported issue.
  • Verified the existing text metrics cache tests.
  • [N/A] Plotting example required.
  • [N/A] Release notes required.
  • [N/A] Documentation changes required.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@himanchalkaushale
Fix stale usetex text metrics when text.latex.preamble changes
c944f59
Closes matplotlib#32413
@github-actions github-actions Bot added the topic: text label Oct 13, 2026
@github-actions

github-actions Bot commented Oct 13, 2026

Copy link
Copy Markdown

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks. We also ask that you please finish addressing any review comments on this PR and wait for it to be merged (or closed) before opening a new one, as it can be a valuable learning experience to go through the review process.

You can also join us on discourse chat for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide.
Please let us know if (and how) you use AI, it will help us give you better feedback on your PR.

We strive to be a welcoming and open project. Please follow our Code of Conduct.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot added the first-contribution label Oct 13, 2026
@himanchalkaushale
Select a sans-serif family in test_usetex_preamble_cache_invalidation
148b77b
\usepackage{helvet} only redefines the sans-serif default (\sfdefault); with a serif family the text is typeset with \rmfamily and loading helvet does not change its font, so the widths never differed and the test could not detect stale metrics.  Set font.family to sans-serif so the text is typeset with \sffamily and the preamble change provably switches the measured font.
lucaznch
lucaznch reviewed Oct 13, 2026
View reviewed changes

@lucaznch lucaznch left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the contribution. The PR currently has some failing CI checks that seem related and worth investigating.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

melissawm reacted with heart emoji
All reactions
  • ❤️ 1 reaction
Comment thread lib/matplotlib/tests/test_usetex.py Outdated

# The Helvetica metrics differ from the previously cached Computer Modern
# ones, i.e. the stale metrics are not reused ...
assert all(width != cm_width for width in widths)

@lucaznch lucaznch Oct 13, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assertion is failing in all six Ubuntu test runs.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@himanchalkaushale himanchalkaushale Oct 14, 2026

Copy link
Copy Markdown
Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing this out. All ubuntu checks are now passed in 3fa0b90 commit, but AppVeyor hit the 60-minute limit at 98% with no test failure.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@lucaznch lucaznch Oct 15, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The appveyor check is known to time out, so I wouldn't worry too much about that one.

In general, make sure all CI checks pass on your pull request. It's also best to run the relevant tests locally before pushing your changes rather than relying on CI alone. See the testing guide for more information.

With the checks passing now, the PR should be in a better position for a maintainer to review.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@himanchalkaushale
TEST: Make test_usetex_preamble_cache_invalidation deterministic
3fa0b90
Replace unreliable helvet/sans-serif setup with times/serif which produces a
large, deterministic metrics change in all TeX Live environments.

The original test used \usepackage{helvet} with font.family=sans-serif, but
in Ubuntu CI the cmss vs Helvetica metrics for alphanumeric text were
nearly identical, causing false failures.

The new test uses the default serif family (Computer Modern Roman) and
\usepackage{times} (mathptmx), which is the standard way to change the
serif font in LaTeX and produces a dramatic, reliable width difference.

Verifies cache invalidation by checking:
1. existing_width != cm_width (preamble change affects rendering)
2. existing_width == same_fig_width == new_fig_width (all three measurements
   agree, proving stale cache is not reused)

Closes matplotlib#32413.
@himanchalkaushale
himanchalkaushale requested a review from lucaznch October 15, 2026 05:04
@himanchalkaushale

himanchalkaushale commented Nov 4, 2026

Copy link
Copy Markdown
Author

All other CI checks are passing. AppVeyor is still timing out, as mentioned in the discussion. No additional code changes seem necessary from my side. Thanks!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@lucaznch lucaznch Awaiting requested review from lucaznch

At least 1 approving review is required to merge this pull request.

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

first-contribution topic: text

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

[Bug]: Changing text.latex.preamble does not invalidate cached usetex text metrics

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@himanchalkaushale @lucaznch
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.