# Asking for code review: Xelatex / PGF backend

Hi!
I would like to ask for a code review for a new backend I wrote for creating
figures with Xelatex/Lualatex. It uses the PGF (Tikz) Package for all
drawing operations and enables full unicode support and typesetting of
texts/formulas using Latex. This way, the figures created fit perfectly in
Latex documents. Furthermore, Xelatex/Lualatex is able to use the fonts
installed on your operating system. The drawing commands of the PGF pictures
can be included in Latex documents or can be directly compiled to PDF by the
backend.

Github project for hosting the code, usage instructions and examples:
https://github.com/pwuertz/matplotlib-backend-pgf

A document demonstrating the benefits of using Xelatex/PGF:
https://github.com/pwuertz/matplotlib-backend-pgf/raw/master/demo/demo.pdf

Gallery of the matplotlib examples processed with backend_pgf:
https://github.com/pwuertz/matplotlib-backend-pgf/wiki/Examples%20Gallery
A few exceptions are known to fail due to Latex incompatible math-text.

This is a matplotlib branch set up as suggested in the matplotlib developer
wiki. It includes the code from above and adds new rc-parameters and the
'.pgf' file type.
https://github.com/pwuertz/matplotlib/compare/master...pgf-backend

Discussions are usually taking place at the github diff, right? I hope
you'll find this an interesting option for creating figures with matplotlib.

Cheers,
Peter

···

View this message in context: http://old.nabble.com/Asking-for-code-review%3A-Xelatex---PGF-backend-tp34263853p34263853.html
Sent from the matplotlib - devel mailing list archive at Nabble.com.

Le 07/08/12 01:17, Peter W�rtz a �crit :

Hi!
I would like to ask for a code review for a new backend I wrote for creating
figures with Xelatex/Lualatex. It uses the PGF (Tikz) Package for all
drawing operations and enables full unicode support and typesetting of
texts/formulas using Latex. This way, the figures created fit perfectly in
Latex documents. Furthermore, Xelatex/Lualatex is able to use the fonts
installed on your operating system. The drawing commands of the PGF pictures
can be included in Latex documents or can be directly compiled to PDF by the
backend.

Github project for hosting the code, usage instructions and examples:
https://github.com/pwuertz/matplotlib-backend-pgf

A document demonstrating the benefits of using Xelatex/PGF:
https://github.com/pwuertz/matplotlib-backend-pgf/raw/master/demo/demo.pdf

Gallery of the matplotlib examples processed with backend_pgf:
https://github.com/pwuertz/matplotlib-backend-pgf/wiki/Examples%20Gallery
A few exceptions are known to fail due to Latex incompatible math-text.

This is a matplotlib branch set up as suggested in the matplotlib developer
wiki. It includes the code from above and adds new rc-parameters and the
'.pgf' file type.
https://github.com/pwuertz/matplotlib/compare/master...pgf-backend

Discussions are usually taking place at the github diff, right? I hope
you'll find this an interesting option for creating figures with matplotlib.

Cheers,
Peter

Very interesting work. Thanks.

Is there any reason that the generated figure could not
be used with pdflatex ?

I ask the question before giving it a try (I use pdflatex).

Regards,

David

David Trémouilles wrote:

Is there any reason that the generated figure could not be used with
pdflatex ?

The PGF pictures should work with pdflatex, xelatex and lualatex alike.
Xelatex (or lualatex) must be installed though because I use it for
obtaining the font metrics when the figure is created. In principle I could
use pdflatex for this as well, I just saw no reason to use it anymore when I

So in order to get the font metrics right you should tell the backend which
fonts you are going to use later. You need to disable the default font
configuration that uses the system fonts specified in the matplotlib rc
paramteres and fully configure the latex environment using your own
preamble. This can be done using the rc parameters:

matplotlib.rcParams.update({
"pgf.rcfonts": False,
"pgf.preamble": [r"\usepackage{siunitx}",
r"\usepackage{somefontpackage}"],
})

I never thought about it, but if there interest in using this with pdflatex
I could add this as an option. It might just be a little bit confusing since
the font setup will be different when switching from xelatex to pdflatex due
to its limitations.

···

View this message in context: http://old.nabble.com/Asking-for-code-review%3A-Xelatex---PGF-backend-tp34263853p34270497.html
Sent from the matplotlib - devel mailing list archive at Nabble.com.

Peter Würtz wrote:

David Trémouilles wrote:

Is there any reason that the generated figure could not be used with
pdflatex ?

The PGF pictures should work with pdflatex, xelatex and lualatex alike.
Xelatex (or lualatex) must be installed though because I use it for
obtaining the font metrics when the figure is created. In principle I
could use pdflatex for this as well, I just saw no reason to use it

Ok, the latest commit now allows you to rely on pdflatex only. Xelatex is
still the default, but you can change that using the rc parameters:

matplotlib.rcParams.update({
"font.family": "serif",
"pgf.texsystem": "pdflatex",
"pgf.preamble": [r"\usepackage{siunitx}",
r"\usepackage{somefontpackage}"],
})

System fonts specified in the rc parameters will be ignored since pdflatex
cannot use them. Font metrics will be obtained from a pdflatex process. If
you save the figure as pdf, pdflatex will be used to compile the figure.

Hope this helps.

···

View this message in context: http://old.nabble.com/Asking-for-code-review%3A-Xelatex---PGF-backend-tp34263853p34271056.html
Sent from the matplotlib - devel mailing list archive at Nabble.com.

Thank you very much Peter
I'm going to test...

David

···

Le 08/08/12 12:59, Peter Würtz a écrit :

Peter Würtz wrote:

David Trémouilles wrote:

Is there any reason that the generated figure could not be used with
pdflatex ?

The PGF pictures should work with pdflatex, xelatex and lualatex alike.
Xelatex (or lualatex) must be installed though because I use it for
obtaining the font metrics when the figure is created. In principle I
could use pdflatex for this as well, I just saw no reason to use it

Ok, the latest commit now allows you to rely on pdflatex only. Xelatex is
still the default, but you can change that using the rc parameters:

matplotlib.rcParams.update({
"font.family": "serif",
"pgf.texsystem": "pdflatex",
"pgf.preamble": [r"\usepackage{siunitx}",
r"\usepackage{somefontpackage}"],
})

System fonts specified in the rc parameters will be ignored since pdflatex
cannot use them. Font metrics will be obtained from a pdflatex process. If
you save the figure as pdf, pdflatex will be used to compile the figure.

Hope this helps.