mpastell / Pweave

Pweave is a scientific report generator and a literate programming tool for Python. It can capture the results and plots from data analysis and works well with numpy, scipy and matplotlib.
http://mpastell.com/pweave
Other
435 stars 64 forks source link

Add proper handling of tikz/pgf files #36

Open reallyasi9 opened 8 years ago

reallyasi9 commented 8 years ago

As of version 1.2, matplotlib has the ability to write pgf files for rendering in LaTeX documents (see http://matplotlib.org/users/whats_new.html#pgf-tikz-backend). This format is useful for scientific documents where the text and measurement used in the plot need to match the text and measurements in the LaTeX document. However, they cannot be included using the standard LaTeX \includegraphics command, and require the \input command instead.

I propose adding a simple check in PwebTexFormatter that checks if figfmt is "pgf", then inserts the appropriate \input statement rather than \includegraphics.

mpastell commented 8 years ago

You can set matplotlib to use pgf as backend for pdf files using a hidden chunk in the start of the document if you want. I will look into adding it as optional format as well. It looks like the currently used "agg" backend doesn't support unicode so in order to get full support one would need to set pgf as backend.

<<echo=False>>=
import matplotlib
from matplotlib.backends.backend_pgf import FigureCanvasPgf
matplotlib.backend_bases.register_backend('pdf', FigureCanvasPgf)

from matplotlib.externals import six
import matplotlib as mpl

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.preamble": [
         "\\usepackage{units}",         # load additional packages
         "\\usepackage{metalogo}",
         "\\usepackage{unicode-math}",  # unicode math setup
         r"\setmathfont{xits-math.otf}",
         r"\setmainfont{DejaVu Serif}", # serif font via preamble
         ]
}
mpl.rcParams.update(pgf_with_custom_preamble)
@