Skip to content
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

Merged
merged 8 commits into from Aug 20, 2012
Merged

PGF backend for XeLaTeX/LuaLaTeX support #1076

merged 8 commits into from Aug 20, 2012

Conversation

pwuertz
Copy link
Contributor

@pwuertz pwuertz commented Aug 12, 2012

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.

import codecs
import subprocess
import warnings
warnings.formatwarning = lambda *args: str(args[0])
Copy link
Member

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.

Copy link
Contributor Author

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.

@pelson
Copy link
Member

pelson commented Aug 12, 2012

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.

@pwuertz
Copy link
Contributor Author

pwuertz commented Aug 12, 2012

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?

@pwuertz
Copy link
Contributor Author

pwuertz commented Aug 13, 2012

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

@mdboom
Copy link
Member

mdboom commented Aug 14, 2012

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 try / except block so if the test fails subsequent tests aren't using the wrong backend. Eventually, it would be nice to have a PR to add PGF backend support to the image comparison tests, but I don't want that to hold this up.

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.

@pwuertz
Copy link
Contributor Author

pwuertz commented Aug 14, 2012

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 knownfailureif makes the tests optional. The complete test suite passes without errors.

@mdboom
Copy link
Member

mdboom commented Aug 14, 2012

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

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?

Copy link
Contributor Author

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?

Copy link
Member

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.

@mdboom
Copy link
Member

mdboom commented Aug 14, 2012

This is a great PR. It would be fantastic to get this in before the freeze on Monday.

@pwuertz
Copy link
Contributor Author

pwuertz commented Aug 14, 2012

Thanks! I'm glad you like it. I'll rebase the commits in this PR to the current master.

@mdboom
Copy link
Member

mdboom commented Aug 14, 2012

@pwuertz wrote

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

@mdboom
Copy link
Member

mdboom commented Aug 14, 2012

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.

@ghost ghost assigned pwuertz Aug 14, 2012
@pwuertz
Copy link
Contributor Author

pwuertz commented Aug 14, 2012

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?

@pelson
Copy link
Member

pelson commented Aug 19, 2012

@mdboom: Happy with this? Shall we merge?

@mdboom
Copy link
Member

mdboom commented Aug 19, 2012

The image files in the examples directory should not be included in the repository.

@mdboom
Copy link
Member

mdboom commented Aug 19, 2012

Other than the extra PNG files, I think this is good to merge.

@pwuertz
Copy link
Contributor Author

pwuertz commented Aug 19, 2012

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

@mdboom
Copy link
Member

mdboom commented Aug 19, 2012

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.

@pwuertz
Copy link
Contributor Author

pwuertz commented Aug 19, 2012

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?

@mdboom
Copy link
Member

mdboom commented Aug 19, 2012

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.

@efiring
Copy link
Member

efiring commented Aug 19, 2012

I agree. Having them with the examples would probably confuse me, and make me think they had been added by accident.

@pwuertz
Copy link
Contributor Author

pwuertz commented Aug 20, 2012

Ok, these files have been moved to doc/_static now.

@mdboom
Copy link
Member

mdboom commented Aug 20, 2012

Ok. This looks good. Merging now.

mdboom added a commit that referenced this pull request Aug 20, 2012
PGF backend for XeLaTeX/LuaLaTeX support
@mdboom mdboom merged commit 97617ff into matplotlib:master Aug 20, 2012
@pelson
Copy link
Member

pelson commented Aug 20, 2012

P.S. Great stuff @pwuertz!

@pwuertz
Copy link
Contributor Author

pwuertz commented Aug 20, 2012

Thank you :)

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

Successfully merging this pull request may close these issues.

None yet

4 participants