Registry Mirror
Skip to content

Conversation

@genrichez

Copy link
Copy Markdown
Contributor

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.
@dcherian
dcherian enabled auto-merge (squash) August 13, 2026 14:47
@dcherian
dcherian disabled auto-merge August 19, 2026 20:46
@dcherian
dcherian merged commit 63b61d3 into pydata:main Aug 19, 2026
42 checks passed
@welcome

welcome Bot commented Aug 19, 2026

Copy link
Copy Markdown

Congratulations on completing your first pull request! Welcome to Xarray! We are proud of you, and hope to see you again! celebration gif

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

3 participants