GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Support {lua,xe}tex as alternative usetex engine. by anntzer · Pull Request #29807 · matplotlib/matplotlib

Conversation

@anntzer

@anntzer anntzer commented Mar 25, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Currently, this PR is mostly a proof of concept; it only implements the
dvi generation and parsing parts, but does not implement rendering in
any of the builtin backends, except svg (under rcParams["svg.fonttype"]
= "none", the default). However, there is a companion branch on the
mplcairo repository, also named "luadvi", which implements support.

Example (requiring both this PR, and mplcairo installed from its luadvi
branch; or try saving to svg with the builtin svg backend):

import matplotlib as mpl; mpl.use("module://mplcairo.qt")
from matplotlib import pyplot as plt

plt.rcParams["text.latex.engine"] = "lualatex"  # or "xelatex"
plt.rcParams["text.latex.preamble"] = (
    # {lua,xe}tex can use any font installed on the system, spec'd using its
    # "normal" name.  Try e.g. DejaVu Sans instead.
    r"\usepackage{fontspec}\setmainfont{TeX Gyre Pagella}")
plt.figtext(.5, .5, r"\textrm{gff\textwon}", usetex=True)
plt.show()

TODO:

  • Fix many likely remaining bugs.
  • Rework font selection in texmanager, which is currently very ad-hoc
    due to the limited number of fonts supported by latex.
  • Implement rendering support in the (other) builtin backends. In
    particular, the Agg (and, if we care, cairo) backend will require
    significant reworking because dvipng, currently used to rasterize dvi
    to png, doesn't support luatex-generated dvi; instead we will need to
    proceed as with the other backends, reading the glyphs one at a time
    from the dvi file and rasterizing them one at a time to the output
    buffer. Working on the other backends is not very high on my priority
    list (as I already have mplcairo as playground...) so it would be nice
    if others showed some interest for it :-)

[Edit: Support for Agg is automatically provided by the new dvipngless
implementation in the text-overhaul branch. Other backends are still TODO.]


See #20262, #22715, #28131.

PR summary

PR checklist

  • "closes #0000" is in the body of the PR description to link the related issue
  • new and changed code is tested
  • Plotting related features are demonstrated in an example
  • New Features and API Changes are noted with a directive and release note
  • Documentation complies with general and docstring guidelines

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@anntzer anntzer added the topic: text/usetex label Mar 25, 2025
@github-actions github-actions Bot added topic: rcparams backend: pdf labels Mar 25, 2025
@anntzer
anntzer force-pushed the luadvi branch from 3866b0e to a2992c8 Compare March 26, 2025 07:42
@github-actions github-actions Bot added the topic: text label Mar 26, 2025
@anntzer
anntzer force-pushed the luadvi branch from a2992c8 to 912d5b7 Compare March 26, 2025 19:12
@anntzer anntzer changed the title Support luatex as alternative usetex engine. Support {lua,xe}tex as alternative usetex engine. Mar 26, 2025
@anntzer
anntzer force-pushed the luadvi branch from 912d5b7 to 00f1e33 Compare March 26, 2025 19:18
@anntzer anntzer mentioned this pull request Mar 26, 2025
[ENH]: Support alternate LaTeX engines #22715
Open
@anntzer
anntzer force-pushed the luadvi branch 2 times, most recently from 4d488f4 to 697d106 Compare March 26, 2025 23:42
@anntzer anntzer mentioned this pull request Mar 27, 2025
Prepare for {xe,lua}tex support in usetex. #29817
Merged
5 tasks
@anntzer
anntzer force-pushed the luadvi branch 3 times, most recently from ff9ecdd to 4f93376 Compare March 27, 2025 22:39
@QuLogic QuLogic added the status: needs rebase label Mar 28, 2025
@github-actions github-actions Bot removed the status: needs rebase label Mar 28, 2025
@anntzer
anntzer force-pushed the luadvi branch 2 times, most recently from 85a73b8 to f1b16f2 Compare March 28, 2025 10:48
This was referenced Mar 29, 2025
Update FreeType to 2.13.3 #29816
Merged
Improve output of dvi debug parsing. #29828
Merged
Rework mapping of dvi glyph indices to freetype indices. #29829
Merged
@anntzer
anntzer force-pushed the luadvi branch from f1b16f2 to 3595fce Compare March 30, 2025 11:54
@github-actions github-actions Bot added the topic: text/fonts label Mar 30, 2025
@anntzer
anntzer force-pushed the luadvi branch 2 times, most recently from c134a34 to ccf927c Compare March 30, 2025 13:49
@github-actions github-actions Bot added the status: needs rebase label Mar 30, 2025
@anntzer
anntzer force-pushed the luadvi branch from ccf927c to 681c78c Compare March 30, 2025 15:26
@github-actions github-actions Bot removed the status: needs rebase label Mar 30, 2025
@anntzer anntzer mentioned this pull request Mar 31, 2025
Switch Tfm metrics to TrueType-compatible API. #29838
Merged
5 tasks
@github-actions github-actions Bot removed the status: needs rebase label Sep 4, 2025
QuLogic
QuLogic reviewed Sep 4, 2025
View reviewed changes
Comment thread lib/matplotlib/texmanager.py
r"\ifpdftex",
r"\usepackage[utf8]{inputenc}",
r"\DeclareUnicodeCharacter{2212}{\ensuremath{-}}",
font_preamble,

@QuLogic QuLogic Sep 4, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving this would be a behaviour change, wouldn't it? I don't recall which usepackage wins though. Also, do we not want to allow a preamble for other engines?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@anntzer anntzer Sep 4, 2025

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think moving the font preamble after inputenc & DeclareUnicodeCharacter would really make a difference for latex, and there's no behaviour change for xe/lualatex as these weren't supported before.

However, it is correct that there's a bit of behavior design that needs to be decided here, which is what is the default font for xe/lualatex? Right now the behavior for latex is: pick one of the few supported fonts (PSNFSS) that more or less matches the rcParams settings, defaulting to computer modern serif (as the default rcParam, DejaVu Sans, is not supported). Note that the Text object's FontProperties is not used (except for the fontsize).

For xel/lualatex, right now this PR does the same thing, and we could very well just document "set rcParams["text.latex.preamble"] = r"\usepackage{fontspec}\setmainfont{...}" to pick whatever font with xe/lualatex". Alternatively, a perhaps better option would be to actually use the Text object's FontProperties (which would need to be threaded down to TexManager, but that seems likely doable) and draw text using the standardly configured font (similarly to rcParams["pgf.rcfonts"] = True for the pgf backend). In that case, usetex with xe/lualatex would not use computer modern, unless the Text's FontProperties say so.

My preference would definitely be for the latter case (just use whatever fonts are configured using the normal mechanism), but I deferred this discussion (and implementation) until the preliminary parts of the work (dvi parsing) got in. Thoughts?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@github-actions github-actions Bot added the status: needs rebase label Mar 13, 2026
@anntzer
anntzer force-pushed the luadvi branch from 9fcdb97 to 7eb0043 Compare March 13, 2026 18:14
@github-actions github-actions Bot added topic: text topic: mpl_toolkit backend: pgf backend: agg topic: path handling backend: ps backend: pdf backend: svg backend: cairo topic: text/fonts topic: text/mathtext topic: transforms and scales topic: styles CI: Run cibuildwheel Run wheel building tests on a PR labels Mar 13, 2026
@anntzer
anntzer changed the base branch from main to text-overhaul March 13, 2026 18:15
@anntzer

anntzer commented Mar 13, 2026

Copy link
Copy Markdown
Contributor Author

I retargeted this to the text-overhaul branch because the new dvipngless codepath in agg directly provides support for xetex/luatex in the agg backend. However I am explicitly not making any request that this blocks 3.11 (it can easily come in 3.12, no baseline images are involved); the only reason I don't target this to main is to avoid pointless rebases atop commits that will not represent the relevant state when this PR gets ultimately merged.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot removed the status: needs rebase label Mar 13, 2026
@github-actions github-actions Bot added the status: needs rebase label Apr 2, 2026
@tacaswell tacaswell added this to the v3.12.0 milestone Apr 3, 2026
@QuLogic

QuLogic commented Apr 10, 2026

Copy link
Copy Markdown
Member

Please rebase on, and re-target to, main now that the text-overhaul branch is merged.

anntzer reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@anntzer
anntzer force-pushed the luadvi branch from 7eb0043 to 044776d Compare April 27, 2026 19:51
@github-actions github-actions Bot removed the status: needs rebase label Apr 27, 2026
@anntzer
anntzer changed the base branch from text-overhaul to main April 27, 2026 19:51
@github-actions github-actions Bot removed topic: text topic: mpl_toolkit labels Apr 27, 2026
@anntzer
Support {lua,xe}tex as alternative usetex engine.
e1ee7d2
Currently, this PR is mostly a proof of concept; only the svg backend
is supported (under rcParams["svg.fonttype"] = "none", the default).
However, there is a companion branch on the mplcairo repository, also
named "luadvi", which implements support for all output formats.

Example (requiring both this PR, and mplcairo installed from its luadvi
branch):
```
import matplotlib as mpl; mpl.use("module://mplcairo.qt")
from matplotlib import pyplot as plt

plt.rcParams["text.latex.engine"] = "lualatex"  # or "xelatex"
plt.rcParams["text.latex.preamble"] = (
    # {lua,xe}tex can use any font installed on the system, spec'd using its
    # "normal" name.  Try e.g. DejaVu Sans instead.
    r"\usepackage{fontspec}\setmainfont{TeX Gyre Pagella}")
plt.figtext(.5, .5, r"\textrm{gff\textwon}", usetex=True)
plt.show()
```
Font effects are supported by mplcairo, e.g.
`\fontspec{DejaVu Sans}[FakeSlant=0.2] abc`.

TODO:

- Fix many likely remaining bugs.
- Rework font selection in texmanager, which is currently very ad-hoc
  due to the limited number of fonts supported by latex.
- Implement rendering support in the (other) builtin backends.  In
  particular, the Agg (and, if we care, cairo) backend will require
  significant reworking because dvipng, currently used to rasterize dvi
  to png, doesn't support luatex-generated dvi; instead we will need to
  proceed as with the other backends, reading the glyphs one at a time
  from the dvi file and rasterizing them one at a time to the output
  buffer.  Working on the other backends is not very high on my priority
  list (as I already have mplcairo as playground...) so it would be nice
  if others showed some interest for it :-)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@QuLogic QuLogic QuLogic left review comments

At least 1 approving review is required to merge this pull request.

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

topic: rcparams topic: text/usetex

Projects

Font and text overhaul
Status: Waiting for other PR

Milestone

v3.12.0

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@anntzer @QuLogic @tacaswell
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.