Skip to content

Allow "real" LaTeX code for pgf.preamble in matplotlibrc #12674

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

Merged
merged 12 commits into from
Nov 11, 2018
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Allow "real" LaTeX code for ``pgf.preamble`` and ``text.latex.preamble`` in matplotlib rc file
``````````````````````````````````````````````````````````````````````````````````````````````

Previously, the rc file keys ``pgf.preamble`` and ``text.latex.preamble`` were parsed using commmas as separators. This would break valid LaTeX code, such as::

\usepackage[protrusion=true, expansion=false]{microtype}

The parsing has been modified to pass the complete line to the LaTeX system,
keeping all commas.

Passing a list of strings from within a Python script still works as it used to.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Allow "real" LaTeX code for ``pgf.preamble`` and ``text.latex.preamble`` in matplotlib rc file
``````````````````````````````````````````````````````````````````````````````````````````````

Previously, the rc file keys ``pgf.preamble`` and ``text.latex.preamble`` were parsed using commmas as separators. This would break valid LaTeX code, such as::

\usepackage[protrusion=true, expansion=false]{microtype}

The parsing has been modified to pass the complete line to the LaTeX system,
keeping all commas.

Passing a list of strings from within a Python script still works as it used to.
2 changes: 1 addition & 1 deletion lib/matplotlib/backends/backend_pgf.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def get_fontspec():

def get_preamble():
"""Get LaTeX preamble from rc."""
return "\n".join(rcParams["pgf.preamble"])
return rcParams["pgf.preamble"]

###############################################################################

Expand Down
18 changes: 16 additions & 2 deletions lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,20 @@ def validate_string_or_None(s):
raise ValueError('Could not convert "%s" to string' % s)


def _validate_stringlist_or_string(s):
"""convert s to string or raise"""
if s is None or s == 'None':
return ""
try:
if isinstance(s, str):
return s
if isinstance(s, Iterable):
return '\n'.join([str(i) for i in s])
raise ValueError()
except ValueError:
raise ValueError('Could not convert "%s" to string' % s)


def validate_axisbelow(s):
try:
return validate_bool(s)
Expand Down Expand Up @@ -1115,7 +1129,7 @@ def _validate_linestyle(ls):
'text.color': ['black', validate_color],
'text.usetex': [False, validate_bool],
'text.latex.unicode': [True, validate_bool],
'text.latex.preamble': [[], validate_stringlist],
'text.latex.preamble': ['', _validate_stringlist_or_string],
'text.latex.preview': [False, validate_bool],
'text.dvipnghack': [None, validate_bool_maybe_none],
'text.hinting': ['auto', validate_hinting],
Expand Down Expand Up @@ -1391,7 +1405,7 @@ def _validate_linestyle(ls):
# use matplotlib rc settings for font configuration
'pgf.rcfonts': [True, validate_bool],
# provide a custom preamble for the latex process
'pgf.preamble': [[], validate_stringlist],
'pgf.preamble': ['', _validate_stringlist_or_string],

# write raster image data directly into the svg file
'svg.image_inline': [True, validate_bool],
Expand Down
18 changes: 11 additions & 7 deletions matplotlibrc.template
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,18 @@
#text.latex.preamble : ## IMPROPER USE OF THIS FEATURE WILL LEAD TO LATEX FAILURES
## AND IS THEREFORE UNSUPPORTED. PLEASE DO NOT ASK FOR HELP
## IF THIS FEATURE DOES NOT DO WHAT YOU EXPECT IT TO.
## preamble is a comma separated list of LaTeX statements
## that are included in the LaTeX document preamble.
## An example:
## text.latex.preamble : \usepackage{bm},\usepackage{euler}
## text.latex.preamble is a single line of LaTeX code that
## will be passed on to the LaTeX system. It may contain
## any code that is valid for the LaTeX "preamble", i.e.
## between the "\documentclass" and "\begin{document}"
## statements.
## Note that it has to be put on a single line, which may
## become quite long.
## The following packages are always loaded with usetex, so
## beware of package collisions: color, geometry, graphicx,
## type1cm, textcomp. Adobe Postscript (PSSNFS) font packages
## may also be loaded, depending on your font settings
## type1cm, textcomp.
## Adobe Postscript (PSSNFS) font packages may also be
## loaded, depending on your font settings.
#text.latex.preview : False

#text.hinting : auto ## May be one of the following:
Expand Down Expand Up @@ -567,7 +571,7 @@
## instead of uuid4
### pgf parameter
#pgf.rcfonts : True
#pgf.preamble :
#pgf.preamble : ## see text.latex.preamble for documentation
#pgf.texsystem : xelatex

### docstring params
Expand Down