Skip to content

texts: keep fontweight on TeX labels, keep lone dollars literal, add Fonts and Labels example - #1

Closed
CSSFrancis wants to merge 2 commits into
ericpre:texts-tex-rotationfrom
CSSFrancis:fix/texts-tex-weight-dollar
Closed

CSSFrancis wants to merge 2 commits into
ericpre:texts-tex-rotationfrom
CSSFrancis:fix/texts-tex-weight-dollar

Conversation

@CSSFrancis

Copy link
Copy Markdown

Follow-up to CSSFrancis#83, based on its branch, so merging this adds two commits to CSSFrancis#83.

Fixes

  1. fontweight was lost on TeX labels. _drawMarkerText called _drawTex without weight, and _drawTex rebuilds ctx.font from opts.weight. So fontweight="bold" with "$\mathrm{Fe}_{K\alpha}$" drew in normal weight. It now passes weight: ms.fontweight.
  2. A single $ changed plain labels. Any $ sent a label to _drawTex, where _texRuns treats each unescaped $ as switching math mode on or off. So "Cost $5" lost its dollar sign, and "$$$" drew nothing. Labels are now typeset only when they have an even, non-zero number of unescaped dollars, matching matplotlib's cbook.is_math_text. Axis labels are unchanged; they always go through _drawTex.

Tests: test_bold_survives_tex_typesetting (1-D and 2-D) and test_odd_dollars_stay_literal ("$$$", "Cost $5"), as screenshot tests. All four fail on CSSFrancis#83 and pass here; test_text_style.py passes 45/45.

Example

Examples/Markers/plot_fonts_and_labels.py, a Fonts and Labels gallery page with one figure per topic:

  • the panel's own title, axis labels and colorbar label with fontsize and TeX;
  • size and weight;
  • an outline (halo) over a busy image;
  • rotation in radians about the anchor;
  • a 3×3 grid of horizontal and vertical alignments, with the anchor points marked;
  • math in markers (sub/superscripts, Greek, \mathrm, bold TeX) next to a plain $5 label;
  • a live set(...) update.

I rendered every figure in headless Chromium to check the layout. One limitation surfaced: the mini-TeX parser doesn't support TeX spacing commands. \, draws a comma and \ drops the space, so the example uses plain spaces.

Assisted-by: claude-code:claude-opus-5-5

_drawTex rebuilds ctx.font from opts.weight, so math labels dropped back
to normal weight.  And any '$' sent a label to the TeX path, where a lone
dollar toggles math mode and vanishes ("Cost $5" lost its dollar).
Mirror matplotlib's is_math_text: typeset only an even, non-zero count of
unescaped dollars.

Assisted-by: claude-code:claude-opus-5-5
Assisted-by: claude-code:claude-opus-5-5
@CSSFrancis

Copy link
Copy Markdown
Author

Superseded by CSSFrancis#85 (same branch, opened against main so CI runs).

@CSSFrancis CSSFrancis closed this Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant