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
PGF backend for XeLaTeX/LuaLaTeX support #1076
Conversation
import codecs | ||
import subprocess | ||
import warnings | ||
warnings.formatwarning = lambda *args: str(args[0]) |
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.
Presumably this is going to have a global effect, which isn't that desirable. I don't really have a workaround for this, its merely an observation.
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.
It's not that i really need the warnings. I just removed everything in the latest commit.
Thanks for submitting this. I expect this could be a very well used feature. I haven't run this code, but I have skimmed it and it all appears well written, is fairly clear and broadly PEP8 compliant. Even with the most thorough review, it is inevitable that other users will find problems, so I was wondering if you are in a position to provide a little support should any issues arise when it is out in the wild? Given that one of the best ways for this kind of functionality is to get users using it, I am in favour of merging this before the 1.2/2.0 freeze deadline, though I am sure there will be a couple of actions to be done before the merge actually takes place. |
Thanks for your input! Concerning support: At https://github.com/pwuertz/matplotlib-backend-pgf I already received some really useful feedback from users. This is my first github project and I really like how easy issues and fixes can be handled. I don't think that I'll have a lot of time for regular checking and participating in matplotlib mailing list discussions, but I'll gladly provide support when someone notifies me about an issue. Is this possible via github as well? Like being assigned to an issue if the problem is pgf related? |
I would also like to add some tests to matplotlib.tests but doesn't seem to work right out of the box. A test would require a switch of backends, which means that every test must ensure that the expected backend is currently selected. Probably the easiest way to do this is to patch the image_comparison decorator and ImageComparisonTest to make use of plt.switch_backend. Shall include this new functionality to this pull request or should I just forget about adding tests? Also, it would be nice to support backend switching for the sphinx plot extension too, since I had to do everything manually when writing the documentation :/. |
vis a vis testing: how about this...? Write a couple simple tests that use switch_backend to generate some pgf output, and just compare it textually to some known good value (assuming the pgf output is deterministic). Make sure the switch_backend is in a I also think it's a great idea to add support to the plot extension -- particularly when generating LaTeX output from sphinx -- but that can be a separate PR. |
Considering that any differences in evaluated floating point values would break a textual comparison I just implemented a simple image comparison test based on matplotlib.testing.compare. The tests are decorated with a function for reliable backend switching. The decorator |
Your solution to testing looks great -- particularly because it localizes the changes necessary to a single PGF-specific file. Nice. |
@@ -574,6 +574,12 @@ def __call__(self, s): | |||
'pdf.use14corefonts' : [False, validate_bool], # use only the 14 PDF core fonts | |||
# embedded in every PDF viewing application | |||
'pdf.fonttype' : [3, validate_fonttype], # 3 (Type3) or 42 (Truetype) | |||
|
|||
'pgf.debug' : [False, validate_bool], # output debug information | |||
'pgf.texsystem' : ['xelatex', str], # choose latex application for creating pdf files (xelatex/lualatex) |
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.
Should this parameter be a choice between 'xelatex', 'lualatex' and 'pdflatex' so that the rc parser will find errors early?
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'm already checking this when reading the 'pgf.texsystem' parameter. Would you feel better with an additional check at rcsetup.py?
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 difference is that a check here would warn on import of matplotlib, not just upon using the PGF backend. Also, the check in the PGF backend falls back on xelatex if the value is invalid -- I'm worried that if someone sets this to "latex" (trying desperately to get this to work with classic latex, which of course it won't), they may become frustrated this it isn't "doing what was asked", rather than getting a message that what they want to do isn't supported.
This is a great PR. It would be fantastic to get this in before the freeze on Monday. |
Thanks! I'm glad you like it. I'll rebase the commits in this PR to the current master. |
@pwuertz wrote
I'll add you as a developer to the project so that issues can be assigned to you, which should trigger an e-mail notification etc. Thanks. |
I want to add also that I like that this is based on the PGF (low-level) layer, rather than TikZ as some earlier proposals have done. I think this gives greater consistency with the rest of matplotlib and makes this backend very much on par with the others. |
Ok, having a few lines of debug info in the produced pgf files makes me feel better when this goes into the wild ^^. Hm, any tasks left? |
@mdboom: Happy with this? Shall we merge? |
The image files in the examples directory should not be included in the repository. |
Other than the extra PNG files, I think this is good to merge. |
I had to include pre-processed plots for the documentation because the matplotlib sphinx extension does not provide a way for switching the rendering backend. Also, the latex unicode-math setup seen in one example doesn't work out of the box with current latex installations and requires downloading and installing special math fonts. Since matplotlib isn't focused on latex I'm not sure if setting up a unicode math environment should be a made a requirement for building the documentation.. |
Ah, I see. That makes sense. I wonder if there's a way to include them in the documentation tree, instead, just so that's more obvious. |
I think in case someone wants to update the images it's more clear if they are located right next to the plotting script. Maybe I could just add the explanation for doing this as a comment in the scripts, or do you want me to move the images somewhere else or to create new directories? Or fix this up later when the documentation is re-structured? |
There are other example images that fit into this category -- being pregenerated because they have unusual requirements -- and they are in doc/_static. I think these should go there, too. I don't think we should have images checked into git alongside the examples. |
I agree. Having them with the examples would probably confuse me, and make me think they had been added by accident. |
Ok, these files have been moved to doc/_static now. |
Ok. This looks good. Merging now. |
PGF backend for XeLaTeX/LuaLaTeX support
P.S. Great stuff @pwuertz! |
Thank you :) |
This pull request contains the implementation and documentation of a backend that allows matplotlib to export figures as pdflatex, xelatex or lualatex processable drawing commands. It uses the PGF (Tikz) Package for all drawing operations and enables full unicode support and typesetting of texts/formulas using LaTeX. The drawing commands of the PGF pictures can be included in LaTeX documents or can be directly compiled to PDF. It should solve the issues #127 and #319.