Skip to content

Fix LaTeX labels broken by textwrap in plot utils (#11452)#11476

Open
genrichez wants to merge 1 commit into
pydata:mainfrom
genrichez:fix/latex-label-textwrap
Open

Fix LaTeX labels broken by textwrap in plot utils (#11452)#11476
genrichez wants to merge 1 commit into
pydata:mainfrom
genrichez:fix/latex-label-textwrap

Conversation

@genrichez

Copy link
Copy Markdown

Description

Fix for #11452 — LaTeX axis labels are broken when label_from_attrs() uses textwrap.wrap() to split long strings.

Root cause: The existing code wraps the concatenated name + extra + units string 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

AI Disclosure

  • This PR contains AI-generated content.
    • I have tested any AI-generated content in my PR.
    • I take responsibility for any AI-generated content in my PR.
      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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Latex labels not rendered under very specific conditions

1 participant