Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Add support for pgf.documentclass rcParam#30063
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Conversation
Thanks! It would be nice with a bit of documentation and some test for this. Docs:https://matplotlib.org/stable/users/explain/text/pgf.html#custom-preamble would be a place to mention it. And it can be worth having a release note that this is a new feature since more people can be interested in it. Test: I quickly browsed the available tests for some inspiration, but I think the best way, if possible, is to add a test where the resulting PGF-file is checked to contain the correct documentclass. |
@@ -958,6 +958,7 @@ def rc_params_from_file(fname, fail_on_error=False, use_default_template=True): | |||
# Strip leading comment. | |||
transform=lambda line: line[1:] if line.startswith("#") else line, | |||
fail_on_error=True) | |||
rcParamsDefault["pgf.documentclass"] = "article" |
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.
The default value should go inhttps://github.com/matplotlib/matplotlib/blob/main/lib/matplotlib/mpl-data/matplotlibrc
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.
This hasn't been removed.
_DOCUMENTCLASS = r"\documentclass{article}" | ||
_DOCUMENTCLASS = ( | ||
rf"\documentclass{{{mpl.rcParams.get('pgf.documentclass', 'article')}}}" | ||
) |
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 think the reason that this is a variable is that it should be possible to override the document class. (Although in an undocumented way.)
Since it is now possible to do that in a more regular way, it would make sense to rewrite the code to not use the variable any more. It is used in three locations though, so maybe one should simply try to factor out the pre-amble code in some way. Which may lead to that your current solution can stay...
baa646d
to33e34c3
Compare33e34c3
to6d48325
CompareHi@oscargus, could you take a look at this PR when you have a moment? It's been open for a while. Thanks! |
@@ -958,6 +958,7 @@ def rc_params_from_file(fname, fail_on_error=False, use_default_template=True): | |||
# Strip leading comment. | |||
transform=lambda line: line[1:] if line.startswith("#") else line, | |||
fail_on_error=True) | |||
rcParamsDefault["pgf.documentclass"] = "article" |
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.
This hasn't been removed.
@@ -144,6 +145,7 @@ | |||
if you want to do the font configuration yourself instead of using the fonts | |||
specified in the rc parameters, make sure to disable :rc:`pgf.rcfonts`. | |||
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.
Extra change.
@@ -170,6 +172,17 @@ | |||
ax.set_ylabel(r"\url{https://matplotlib.org}") | |||
ax.legend(["unicode math: $λ=∑_i^∞ μ_i^2$"]) | |||
You can also change the LaTeX document class used when generating PGF figures. | |||
By default, Matplotlib uses ``article``, but you can override this using | |||
the ``pgf.documentclass`` rcParam:: |
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.
the``pgf.documentclass``rcParam:: | |
:rc:`pgf.documentclass`: |
@@ -205,7 +202,7 @@ class LatexManager: | |||
@staticmethod | |||
def _build_latex_header(): | |||
latex_header = [ | |||
_DOCUMENTCLASS, | |||
rf"\documentclass{{{mpl.rcParams.get('pgf.documentclass', 'article')}}}", |
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.
You don't need to use.get
here (see howpgf.texsystem
below is unconditional.)
@@ -831,10 +828,11 @@ def print_pdf(self, fname_or_fh, *, metadata=None, **kwargs): | |||
# print figure to pgf and compile it with latex | |||
with TemporaryDirectory() as tmpdir: | |||
tmppath = pathlib.Path(tmpdir) | |||
docclass = mpl.rcParams.get("pgf.documentclass", "article") |
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.
Ditto.
@@ -931,7 +929,7 @@ def _write_header(self, width_inches, height_inches): | |||
pdfinfo = ','.join( | |||
_metadata_to_str(k, v) for k, v in self._info_dict.items()) | |||
latex_header = "\n".join([ | |||
_DOCUMENTCLASS, | |||
rf"\documentclass{{{mpl.rcParams.get('pgf.documentclass', 'article')}}}", |
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.
Ditto.
@@ -400,3 +400,12 @@ def test_document_font_size(): | |||
label=r'\normalsize the document font size is \the\fontdimen6\font' | |||
) | |||
plt.legend() | |||
@pytest.mark.backend("pgf") |
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.
This test doesn't create any plots, so it doesn't need a specific backend.
from matplotlib.backends.backend_pgf import LatexManager | ||
mpl.rcParams["pgf.documentclass"] = "IEEEtran" | ||
header = LatexManager._build_latex_header() |
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.
This test only really checks one code path; there should at least be one for outputtingpgf
from a figure.
Uh oh!
There was an error while loading.Please reload this page.
PR summary
This PR adds support for configuring the LaTeX
\documentclass{...}
used by the PGF backend via a newrcParam
:pgf.documentclass
.Why is this change necessary?
Currently, the PGF backend uses a hardcoded LaTeX document class (
article
) when rendering PGF figures. This causes layout inconsistencies (such as in#29978) when the.pgf
file is included in documents using other classes likeIEEEtran
,acmart
, etc.What problem does it solve?
It allows users to specify the LaTeX document class used during PGF rendering via:
This provides a clean and documented alternative to the current workaround of modifying the internal
_DOCUMENTCLASS
variable inbackend_pgf
showed in#29978.What is the reasoning for this implementation?
The new rcParam follows Matplotlib’s configuration pattern and is registered via
rcsetup.py
. It defaults to'article'
, ensuring backward compatibility. The PGF backend retrieves the class usingmpl.rcParams.get(...)
.Doubts:
matplotlibrc
because I'm unsure of its exact placement or formatting style (or even if Ia should add it). I'd appreciate feedback on whether it should be included there explicitly.Please advise on both.
PR checklist