xelatex with pdf multipage

Neal Becker wrote:

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)

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

I get an error:
Traceback (most recent call last):
  File "./read_hist3.py", line 121, in <module>
    pdf.savefig (fig)
line 2258, in savefig
    figure.savefig(self, format='pdf', **kwargs)
  File "/usr/lib64/python2.7/site-packages/matplotlib/figure.py", line 1363,
    self.canvas.print_figure(*args, **kwargs)
  File "/usr/lib64/python2.7/site-packages/matplotlib/backend_bases.py", line
2093, in print_figure
  File "/usr/lib64/python2.7/site-packages/matplotlib/backend_bases.py", line
1943, in _print_method
    return print_method(*args, **kwargs)
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{unicode-math}", # unicode math setup
        r'\setmainfont{xits}', ]

for ...
   plt.savefig ('xxx.pdf')

subprocess.call (['pdfunite'] + files + ['test_uw4.pdf'])