You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: Inconsistent pgf backend results from text.usetex flag #28131
The problem is that the usetex renderer seems to have a different implementation than the pgf renderer.
Code for reproduction
importmatplotlib.pyplotaspltimportmatplotlibasmplmpl.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 objectsfig, ax=plt.subplots(figsize=(2, 1.5))
# Plot the datax= [1, 2, 3, 4, 5]
y= [2, 4, 6, 8, 10]
ax.plot(x, y)
# Set the title and axis labelsax.set_title('My Plot')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
# Save the plot to a fileplt.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.
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.)
Activity
anntzer commentedon Apr 25, 2024
See #20262 (comment) regarding difficulties of moving to using xe/luatex everywhere.
voidstarstar commentedon Apr 25, 2024
Thanks for that info. It sounds like unifying everything using xe/lualatex is desired, but would be a difficult transition.
voidstarstar commentedon Apr 25, 2024
I think I might understand what's going on now. The documentation was a bit unclear about what
text.usetexactually does and how it's behavior changes slightly based on the backend.With the agg backend:
text.usetex=True, it will use the latex compiler to render text with theTexManagerclass.text.usetex=True, it will use the preamble intext.latex.preamble.text.usetex=True, it will use TeX math mode to render text.text.usetex=False, it will use Mathtext to render text.With the pgf backend:
pgf.texsystemcompiler to render everything.pgf.preamble.text.usetex=True, it will use TeX math mode to render text.text.usetex=False, it will use TeX text mode to render text.In my original post, I had mistakenly assumed that
text.usetex=Falseshould 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.usetexchanges the behavior of theFormatters, and this behavior is interpreted differently by Mathtext and TeX. For example,ScalarFormatterwill output$\\mathdefault{4.0}$whentext.usetex=Trueand4.0whentext.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