Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
Loading
from

Conversation

MiniX16
Copy link
Contributor

@MiniX16 MiniX16 commented May 18, 2025

PR summary

This PR adds support for configuring the LaTeX \documentclass{...} used by the PGF backend via a new rcParam: 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 like IEEEtran, acmart, etc.

What problem does it solve?

It allows users to specify the LaTeX document class used during PGF rendering via:

matplotlib.rcParams["pgf.documentclass"] = "IEEEtran"

This provides a clean and documented alternative to the current workaround of modifying the internal _DOCUMENTCLASS variable in backend_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 using mpl.rcParams.get(...).


Doubts:

  • I have not added the new parameter to 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.
  • I'm also not sure whether this feature requires:
    • a documentation note (and where to place it), and/or
    • a dedicated test (beyond verifying it manually and observing that existing PGF tests pass).

Please advise on both.


PR checklist

@oscargus
Copy link
Member

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"
Copy link
Member

Choose a reason for hiding this comment

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

_DOCUMENTCLASS = r"\documentclass{article}"
_DOCUMENTCLASS = (
rf"\documentclass{{{mpl.rcParams.get('pgf.documentclass', 'article')}}}"
)
Copy link
Member

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...

@MiniX16
Copy link
Contributor Author

MiniX16 commented May 18, 2025

Just realized that the test I made doesn't make any sense. I am checking the .lex value, not the .pgf's. I'm trying to remake it but the .gpf generated doesn't have any reference to the documentclass. The article one only differs from the IEEEtrans in the coords, spacins...

The only way I found was:

from matplotlib.backends.backend_pgf import LatexManager

mpl.rcParams["pgf.documentclass"] = "IEEEtran"
header = LatexManager._build_latex_header()
assert r"\documentclass{IEEEtran}" in header

In this case, should I do a rebase to modify the test commit or is It good to make a new commit?

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

Successfully merging this pull request may close these issues.

[Bug]: PGF file produces incorrect whitespace in the legend when using certain documentclass.
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.