Title: [Bug]: usetex text drawn as paths (SVG, path effects) is narrower than its layout · Issue #32389 · matplotlib/matplotlib · GitHub
Open Graph Title: [Bug]: usetex text drawn as paths (SVG, path effects) is narrower than its layout · Issue #32389 · matplotlib/matplotlib
X Title: [Bug]: usetex text drawn as paths (SVG, path effects) is narrower than its layout · Issue #32389 · matplotlib/matplotlib
Description: Bug summary TextToPath.get_glyphs_tex always runs LaTeX at the fixed FONT_SCALE of 100 pt and ignores the size in prop. TeX fonts have a separate design for each size, so all usetex text drawn as paths (e.g., all SVG output, and text wit...
Open Graph Description: Bug summary TextToPath.get_glyphs_tex always runs LaTeX at the fixed FONT_SCALE of 100 pt and ignores the size in prop. TeX fonts have a separate design for each size, so all usetex text drawn as p...
X Description: Bug summary TextToPath.get_glyphs_tex always runs LaTeX at the fixed FONT_SCALE of 100 pt and ignores the size in prop. TeX fonts have a separate design for each size, so all usetex text drawn as p...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/32389
X: @github
Domain: github.com
IPv4 address: 140.82.114.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Bug]: usetex text drawn as paths (SVG, path effects) is narrower than its layout","articleBody":"### Bug summary\n\n`TextToPath.get_glyphs_tex` always runs LaTeX at the fixed `FONT_SCALE` of 100 pt and ignores the size in `prop`. TeX fonts have a separate design for each size, so all usetex text drawn as paths (e.g., all SVG output, and text with `path_effects` on any backend) uses the 17 pt design. At 6 to 12 pt the resulting text is 5-12% narrower than its layout and has lighter strokes. PNG and PDF output are correct.\n\n### Code for reproduction\n\n```Python\nimport re\n\nimport matplotlib as mpl\nimport matplotlib.patheffects as pe\nimport matplotlib.pyplot as plt\n\nmpl.rcParams[\"text.usetex\"] = True\ns = \"Hamburgefonstiv\"\n\n# The same 6 pt text saved as SVG and as PDF.\nfig = plt.figure(figsize=(2, 1))\nfig.text(0.1, 0.5, s, fontsize=6)\nfig.savefig(\"usetex.svg\")\nfig.savefig(\"usetex.pdf\")\nwith open(\"usetex.svg\", encoding=\"utf-8\") as f:\n svg = f.read()\nwith open(\"usetex.pdf\", \"rb\") as f:\n pdf = f.read()\nprint(\"SVG fonts:\", sorted(set(re.findall(r'id=\"(CM[A-Z]+\\d+)-', svg))))\nprint(\"PDF fonts:\", sorted({m.decode() for m in\n re.findall(rb\"/BaseFont\\s*/(?:[A-Z]{6}\\+)?(\\w+)\", pdf)}))\n\n# Right-aligned 8 pt text on Agg, without and with path effects.\nfig = plt.figure(figsize=(1.6, 0.5), dpi=600)\nfig.add_artist(plt.Line2D([0.93, 0.93], [0, 1], lw=0.3, color=\"tab:red\"))\nfig.text(0.93, 0.62, s, fontsize=8, ha=\"right\", va=\"center\")\nfig.text(0.93, 0.25, s, fontsize=8, ha=\"right\", va=\"center\",\n path_effects=[pe.Normal()])\nfig.savefig(\"usetex_path_effects.png\")\n```\n\n### Actual outcome\n\n```text\nSVG fonts: ['CMSS17']\nPDF fonts: ['CMSS8']\n```\nThe same 6 pt text is drawn with the 17 pt design of Computer Modern Sans (CMSS17) in the SVG file, and with CMSS8, the smallest design, in the PDF file.\n\u003cimg width=\"960\" height=\"300\" alt=\"Two lines of usetex text, right-aligned on a red line; the lower line, with path effects, is narrower and ends short of the line\" src=\"https://github.com/user-attachments/assets/e89a46b5-8f54-41e2-a178-0028ab33c141\" />\n - both lines are right-aligned on the red line, 8 pt\n - the lower line has `path_effects=[pe.Normal()]`, so it is drawn through `get_glyphs_tex`.\n - the lower line narrower is narrower and lighter, and it ends short of the red line\n\nThe table below compares the width matplotlib lays the text out with (`TexManager.get_text_width_height_descent`) with the ink width as drawn by `get_glyphs_tex`, and with the ink width when LaTeX runs at the text size.\n| Size | Layout width | Ink width, drawn (SVG, path effects)| Ink width, LaTeX at the text size |\n| ------- | ------- | ------- | ------- |\n| 6 pt | 44.87 pt | 39.56 pt | 44.76 pt |\n| 10 pt | 70.43 pt | 65.93 pt | 70.27 pt |\n| 24 pt | 158.55 pt | 158.22 pt | 158.22 pt |\n\nFor 17 pt and larger CMSS17 is the correct design, so larger text is not affected. The layout width includes the side bearings of the first and last glyph, so it is slightly larger than the ink width even when both use the same design. The bug is the difference between the two ink columns, which closes at 17 pt and above.\n\n### Expected outcome\n\nusetex text drawn as paths matches PNG and PDF output, with the design TeX selects for the text's size:\n\n- The SVG file uses CMSS8 for the 6 pt text, as the PDF file does.\nIn the image, the lower line matches the upper line and ends at the red line.\n- In the table, the drawn ink width equals the ink width with LaTeX at the text size, at every size.\n\n### Additional information\n\n### Likely root cause:\n`get_glyphs_tex` typesets the string with `TexManager().make_dvi(s, self.FONT_SCALE`), so LaTeX always runs at 100 pt, and its callers scale the result down to the text size. Everywhere else, LaTeX runs at the text's own size. *i.e.*, in the `draw_tex` methods of the PDF, PS, and PGF backends (`prop.get_size_in_points()`), in Agg through `dvipng`, and in the layout measurement (`TexManager.get_text_width_height_descent`). Because TeX selects a font design by size, only the paths through `get_glyphs_tex` draw with a different design from the one matplotlib measured.\n\n### Affected paths:\nThe SVG backend does not override `draw_tex`, so `RendererBase.draw_tex` draws all usetex text through `_draw_text_as_path` and `get_glyphs_tex`, for any `svg.fonttype. PathEffectRenderer` inherits the same `draw_tex`, so usetex text with path effects is affected on every backend, including Agg. `TextPath(..., usetex=True)` also calls `get_glyphs_tex`. The Cairo backend inherits `RendererBase.draw_tex` as well.\n\n### Possible fix\n```diff\n- dvifile = TexManager().make_dvi(s, self.FONT_SCALE)\n+ size = prop.get_size_in_points()\n+ scale = self.FONT_SCALE / size\n+ dvifile = TexManager().make_dvi(s, size)\n ...\n- xpositions.append(text.x)\n- ypositions.append(text.y)\n- sizes.append(text.font_size / self.FONT_SCALE)\n+ xpositions.append(text.x * scale)\n+ ypositions.append(text.y * scale)\n+ sizes.append(text.font_size / size)\n ...\n for ox, oy, h, w in page.boxes:\n+ ox, oy, h, w = ox * scale, oy * scale, h * scale, w * scale\n```\n\nThis is a safe solution because glyph outlines are still loaded at `FONT_SCALE`, and glyph keys include the font name, so the shared `glyph_map` of the SVG backend keeps separate entries for CMSS8 and CMSS17 when a figure has mixed sizes. The LaTeX run is the one where the layout measurement is already cached, so the change adds no extra LaTeX runs.\n\nTested so far as a local patch on 3.11.2:\n\n#### Limitation for `TextPath`:\nThe fix takes the text size from `prop`, which is correct for the SVG backend and path effects, because they pass the font properties of the text. `TextPath` accepts its size separately and passes `prop` to `get_text_path` unchanged. So `TextPath((0, 0), s, size=12, usetex=True`) would run LaTeX at the default 10 pt in `prop` instead of at 12 pt. Passing a copy of `prop` with its size set to `size` in `TextPath.__init__` would fix that.\n\n### Context\nI maintain [curved-text](https://github.com/thiebes/curved-text), a package listed in the third-party registry that draws text along curves from `get_glyphs_tex` outlines. I found this while adding usetex support to it. For now it works around the bug by setting `FONT_SCALE` to the text size on its own `TextToPath` instance, which fixes curved-text but not matplotlib's own SVG and path-effects output. I would be glad to open a PR with the change above and a test, if this direction is acceptable.\n\n### AI disclosure\nI used an AI assistant (Claude) to trace the code paths, measure the widths, and test the patch. I ran the reproduction and the measurements myself and checked the results, and I reviewed the proposed change line by line.\n\n### Operating system\n\nWindows 11\n\n### Matplotlib Version\n\n3.11.2\n\n### Matplotlib Backend\n\nAgg (image), SVG and PDF via `savefig`\n\n### Python version\n\n3.13.3\n\n### Jupyter version\n\n_No response_\n\n### Installation\n\npip","author":{"url":"https://github.com/thiebes","@type":"Person","name":"thiebes"},"datePublished":"2026-09-24T10:44:11Z","url":"https://github.com/matplotlib/matplotlib/issues/32389"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:cf5d4208-6f80-74d4-30c5-e38b8ad0eb59 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CD48:1759AA:D143C3:11D2D38:6B064EA0 |
| html-safe-nonce | 37960b3df382f09b220a7470647f0db7ea51e21e7cef16341e2b0aa3de16329f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRDQ4OjE3NTlBQTpEMTQzQzM6MTFEMkQzODo2QjA2NEVBMCIsInZpc2l0b3JfaWQiOiI3MDIwMTIxMTg1NjM5NjY2MjQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | f22d0c70a4868e8a35915a6753c9fe82751f7e6df28136afc307d07c63b86176 |
| hovercard-subject-tag | issue:5567087203 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/matplotlib/matplotlib/32389/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b96c4764dbbbb747ff798e6f52c17275030658e39de21536719900f2ea23a0a5/matplotlib/matplotlib/issues/32389 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b96c4764dbbbb747ff798e6f52c17275030658e39de21536719900f2ea23a0a5/matplotlib/matplotlib/issues/32389 |
| og:image:alt | Bug summary TextToPath.get_glyphs_tex always runs LaTeX at the fixed FONT_SCALE of 100 pt and ignores the size in prop. TeX fonts have a separate design for each size, so all usetex text drawn as p... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | thiebes |
| hostname | github.com |
| expected-hostname | github.com |
| None | 772b6997ca4daa0428dd49a25d76052e765555ae560fe9c88517eb828bbd861e |
| turbo-cache-control | no-preview |
| go-import | github.com/matplotlib/matplotlib git https://github.com/matplotlib/matplotlib.git |
| octolytics-dimension-user_id | 215947 |
| octolytics-dimension-user_login | matplotlib |
| octolytics-dimension-repository_id | 1385122 |
| octolytics-dimension-repository_nwo | matplotlib/matplotlib |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1385122 |
| octolytics-dimension-repository_network_root_nwo | matplotlib/matplotlib |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f1f9a08f7c769a37f1dfc18ad43d463c222a7eea |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width