Skip to content

[Bug]: Inconsistent pgf backend results from text.usetex flag #28131

Open

Description

@voidstarstar

Bug summary

The text.usetex rcParam flag can be used to enable TeX rendering on all Text elements instead of Mathtext. When using the pgf backend, I would expect this flag to have no effect and for the Text elements to be rendered identically since pgf also uses TeX to render.

The problem is that the usetex renderer seems to have a different implementation than the pgf renderer.

Code for reproduction

import matplotlib.pyplot as plt
import matplotlib as mpl

mpl.use('pgf')
plt.rcParams.update({
    'figure.dpi': 400,
    'text.usetex': True, # TODO: comment this out to get different results
    'ytick.labelsize': 1,
    })

# Create the figure and axis objects
fig, ax = plt.subplots(figsize=(2, 1.5))

# Plot the data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
ax.plot(x, y)

# Set the title and axis labels
ax.set_title('My Plot')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')

# Save the plot to a file
plt.savefig('my_plot.png')
plt.savefig('my_plot.pgf')

Actual outcome

When text.usetex is True, the tick labels use display math font to render in Tex.
When text.usetex is False, the tick labels use the regular text font to render in Tex.

Expected outcome

When using the pgf backend, I would expect enabling/disabling text.usetex to have no effect on the output.

Additional information

From what I understand, the text.usetex implementation has an unofficially supported rcParam for the preamble named text.latex.preamble. On the other hand, the pgf implementation has an officially supported rcParam for the preamble named pgf.preamble. From the documentation it's unclear to me which renderer/preamble takes precedence when both pgf and usetex are enabled.

The text.usetex renderer also does not support XeLaTeX/LuaLaTeX according to the documentation.

In conclusion:
Is there any reason why these two rendering engines can not (or should not) be unified? I suspect this is also the root cause of a lot of confusion and consistency errors people have.

Related:
#28084

Operating system

Ubuntu

Matplotlib Version

3.7.5

Matplotlib Backend

pgf

Python version

3.8.10

Jupyter version

No response

Installation

pip

Activity

  1. anntzer commented on Apr 25, 2024

    @anntzer
    Contributor

    See #20262 (comment) regarding difficulties of moving to using xe/luatex everywhere.

  2. voidstarstar commented on Apr 25, 2024

    @voidstarstar
    Author

    See #20262 (comment) regarding difficulties of moving to using xe/luatex everywhere.

    Thanks for that info. It sounds like unifying everything using xe/lualatex is desired, but would be a difficult transition.

  3. voidstarstar commented on Apr 25, 2024

    @voidstarstar
    Author

    I think I might understand what's going on now. The documentation was a bit unclear about what text.usetex actually does and how it's behavior changes slightly based on the backend.

    With the agg backend:

    • If text.usetex=True, it will use the latex compiler to render text with the TexManager class.
    • If text.usetex=True, it will use the preamble in text.latex.preamble.
    • If text.usetex=True, it will use TeX math mode to render text.
    • If text.usetex=False, it will use Mathtext to render text.

    With the pgf backend:

    • It will use the pgf.texsystem compiler to render everything.
    • It will use the preamble in pgf.preamble.
    • If text.usetex=True, it will use TeX math mode to render text.
    • If text.usetex=False, it will use TeX text mode to render text.

    In my original post, I had mistakenly assumed that text.usetex=False should never be possible when using the pgf backend. In general, I think most users will want to enable this flag when using pgf.

    (This is because text.usetex changes the behavior of the Formatters, and this behavior is interpreted differently by Mathtext and TeX. For example, ScalarFormatter will output $\\mathdefault{4.0}$ when text.usetex=True and 4.0 when text.usetex=False.)

    References:
    https://matplotlib.org/stable/users/explain/text/mathtext.html
    https://matplotlib.org/stable/users/explain/text/usetex.html
    https://matplotlib.org/stable/users/explain/text/pgf.html

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions