Fix LaTeX labels broken by textwrap in plot utils (#11452)#11476
Open
genrichez wants to merge 1 commit into
Open
Fix LaTeX labels broken by textwrap in plot utils (#11452)#11476genrichez wants to merge 1 commit into
genrichez wants to merge 1 commit into
Conversation
textwrap.wrap() can split the combined label string at positions between adjacent $...$ blocks, producing invalid $$ sequences that matplotlib's mathtext parser cannot render. Since the rendered width of LaTeX is typically much shorter than the source string length, wrapping based on character count is misleading anyway. Skip wrapping entirely for LaTeX-detected labels (those starting with $ and having an even number of $ characters) and return the concatenated name + extra + units directly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Fix for #11452 — LaTeX axis labels are broken when
label_from_attrs()usestextwrap.wrap()to split long strings.Root cause: The existing code wraps the concatenated
name + extra + unitsstring at 60 characters and rejoins with"$\n$". When the wrap point falls between two adjacent$...$blocks (e.g.,$\frac{x}{A}$followed by[$\mathrm{m}$]), the rejoin produces$$sequences that matplotlib's mathtext parser cannot render.Fix: Skip
textwrap.wrap()entirely for LaTeX-detected labels and return the concatenated string directly. The rendered width of LaTeX is typically much shorter than the source string length, so character-count-based wrapping is misleading for these labels anyway.LaTeX detection heuristic (unchanged from existing code):
name.startswith("$") and name.count("$") % 2 == 0.Checklist
whats-new.rstapi.rstAI Disclosure
Tools: Claude (Anthropic) — used for root cause analysis, fix implementation, and test generation. All code was reviewed, tested (565 existing tests pass, 36 additional edge case tests pass), and validated manually.