-
-
Notifications
You must be signed in to change notification settings - Fork 7.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 our terms of service and privacy 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?
Changes from all commits
23e0b0c
e8f44bc
1bf0be7
89f6d80
6d48325
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
PGF backend: new rcParam :rc:`pgf.documentclass` | ||
------------------------------------------------ | ||
|
||
A new rcParam :rc:`pgf.documentclass` has been added to allow users to override | ||
the default LaTeX document class (``article``) used by the PGF backend. | ||
This enables better compatibility when including PGF figures in documents that | ||
use custom LaTeX classes like ``IEEEtran`` or others, avoiding layout | ||
issues like incorrect font sizes or spacing mismatches. |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -48,6 +48,7 @@ | |||
pgf.preamble Lines to be included in the LaTeX preamble | ||||
pgf.rcfonts Setup fonts from rc params using the fontspec package | ||||
pgf.texsystem Either "xelatex" (default), "lualatex" or "pdflatex" | ||||
pgf.documentclass The LaTeX document class to use (e.g., "article", "IEEEtran") | ||||
================= ===================================================== | ||||
|
||||
.. note:: | ||||
|
@@ -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`. | ||||
|
||||
|
||||
.. code-block:: python | ||||
|
||||
import matplotlib as mpl | ||||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
|
||||
import matplotlib.pyplot as plt | ||||
plt.rcParams["pgf.documentclass"] = "IEEEtran" | ||||
|
||||
This is useful when including PGF figures into LaTeX documents using | ||||
custom classes such as ``IEEEtran``, to avoid layout | ||||
mismatches in font size or spacing. | ||||
|
||||
.. redirect-from:: /gallery/userdemo/pgf_texsystem | ||||
|
||||
.. _pgf-texsystem: | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. The default value should go in https://github.com/matplotlib/matplotlib/blob/main/lib/matplotlib/mpl-data/matplotlibrc There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This hasn't been removed. |
||
rcParamsDefault._update_raw(rcsetup._hardcoded_defaults) | ||
rcParamsDefault._ensure_has_backend() | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,9 +29,6 @@ | |
_log = logging.getLogger(__name__) | ||
|
||
|
||
_DOCUMENTCLASS = r"\documentclass{article}" | ||
|
||
|
||
# Note: When formatting floating point values, it is important to use the | ||
# %f/{:f} format rather than %s/{} to avoid triggering scientific notation, | ||
# which is not recognized by TeX. | ||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. You don't need to use |
||
# Include TeX program name as a comment for cache invalidation. | ||
# TeX does not allow this to be the first line. | ||
rf"% !TeX program = {mpl.rcParams['pgf.texsystem']}", | ||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. |
||
self.print_pgf(tmppath / "figure.pgf", **kwargs) | ||
(tmppath / "figure.tex").write_text( | ||
"\n".join([ | ||
_DOCUMENTCLASS, | ||
rf"\documentclass{{{docclass}}}" | ||
r"\usepackage[pdfinfo={%s}]{hyperref}" % pdfinfo, | ||
r"\usepackage[papersize={%fin,%fin}, margin=0in]{geometry}" | ||
% (w, h), | ||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. |
||
r"\usepackage[pdfinfo={%s}]{hyperref}" % pdfinfo, | ||
r"\usepackage[papersize={%fin,%fin}, margin=0in]{geometry}" | ||
% (width_inches, height_inches), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 commentThe 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. |
||
def test_pgf_documentclass_written(): | ||
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 commentThe 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 outputting |
||
assert r"\documentclass{IEEEtran}" in 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.
Extra change.