Neal Becker wrote:
> Objective:
> produce multi-page pdfs using xelatex so I can have advanced latex and stix
> fonts (using xits package)
>
> I've used pdf multipage with the recipe:
>
> import matplotlib as mpl
> mpl.use ('pdf')
> import matplotlib.pyplot as plt
>
> from matplotlib.backends.backend_pdf import PdfPages
> pdf = PdfPages('test_uw3.pdf')
> for page in ...
> fig = plt.figure()
> pdf.savefig (fig)
> plt.close()
> pdf.close()
>
> Now I'm interested in using xelatex (to use stix fonts). So I saw the
> I should use pgf
>
> If I add:
>
> from matplotlib.backends.backend_pgf import FigureCanvasPgf
> matplotlib.backend_bases.register_backend('pdf', FigureCanvasPgf)
>
> as suggested by
> http://matplotlib.org/users/pgf.html
>
> I get an error:
> Traceback (most recent call last):
> File "./read_hist3.py", line 121, in <module>
> pdf.savefig (fig)
> File
> "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_pdf.py",
> line 2258, in savefig
> figure.savefig(self, format='pdf', **kwargs)
> File "/usr/lib64/python2.7/site-packages/matplotlib/figure.py", line 1363,
> in
> savefig
> self.canvas.print_figure(*args, **kwargs)
> File "/usr/lib64/python2.7/site-packages/matplotlib/backend_bases.py", line
> 2093, in print_figure
> **kwargs)
> File "/usr/lib64/python2.7/site-packages/matplotlib/backend_bases.py", line
> 1943, in _print_method
> return print_method(*args, **kwargs)
> File
> "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_pgf.py",
> line 830, in print_pdf
> raise ValueError("filename must be a path or a file-like object")
> ValueError: filename must be a path or a file-like object
>
> Any ideas?
>
The best thing I've come up with so far is this, which will write out each page
to a pdf file, then use subprocess to call 'pdfunite' to join the pdfs.
I like the xits-math + xits - it gives a much more unified look so the text and
math fonts match.
Only problems:
It's extremely slow
The resulting pdf has duplicate embedded fonts
------------------------------------
import matplotlib as mpl
mpl.use ('pgf')
import numpy as np
import matplotlib.pyplot as plt
pgf_with_custom_preamble = {
"font.family": "serif", # use serif/main font for text elements
"text.usetex": True, # use inline math for ticks
"pgf.rcfonts": False, # don't setup fonts from rc parameters
'pgf.texsystem' : 'lualatex',
"pgf.preamble": [
r'\usepackage{fontspec,xunicode}',
r"\usepackage{unicode-math}", # unicode math setup
r"\setmathfont{xits-math.otf}",
r'\usepackage{cancel}',
r'\usepackage{xcolor}',
r'\renewcommand{\CancelColor}{\color{red}}',
r'\setmainfont{xits}', ]
}
mpl.rcParams.update(pgf_with_custom_preamble)
for ...
plt.savefig ('xxx.pdf')
subprocess.call (['pdfunite'] + files + ['test_uw4.pdf'])
|