Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bd7829d

Browse files
sxntxnjklymak
authored andcommitted
Allow "real" LaTeX code for pgf.preamble in matplotlibrc (#12674)
* Allow "real" LaTeX code for pgf.preamble in matplotlibrc This allows to use the rc key "pgf.preamble" to contain "real" LaTeX code. As of before this patch, commas are used as parser separators, causing the PGF backend call to latex to fail, for example at loading packages which contain options such as: \usepackage[protrusion=true, expansion=false]{microtype} Passing a list of strings from within a Python script works fine, and with this patch it stays this way. * Python3 only: remove six * Flake8: add second blank line * Return "" instead of str() As per review by anntzer * Raise ValueError directly As per review by anntzer * Add API changes note * Rename function validate_stringlist_or_string ... to _validate_stringlist_or_string As per review by anntzer * Join on '\n' As per review by tacaswell * Also apply to text.latex.preamble * Change documentation in matplotlibrc.template Update documentation of text.latex.preamble. Add reference to text.latex.preamble for pgf.preamble. * Add "What's New" entry Actually a copy of doc/api/next_api_changes/2018-10-30-rcparams-pgf.preamble-full-LaTeX-support.rst * Check for Iterable instead of list ... in order to also accept tuples as input, not only lists.
1 parent ad0009f commit bd7829d

File tree

5 files changed

+50
-10
lines changed

5 files changed

+50
-10
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Allow "real" LaTeX code for ``pgf.preamble`` and ``text.latex.preamble`` in matplotlib rc file
2+
``````````````````````````````````````````````````````````````````````````````````````````````
3+
4+
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::
5+
6+
\usepackage[protrusion=true, expansion=false]{microtype}
7+
8+
The parsing has been modified to pass the complete line to the LaTeX system,
9+
keeping all commas.
10+
11+
Passing a list of strings from within a Python script still works as it used to.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Allow "real" LaTeX code for ``pgf.preamble`` and ``text.latex.preamble`` in matplotlib rc file
2+
``````````````````````````````````````````````````````````````````````````````````````````````
3+
4+
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::
5+
6+
\usepackage[protrusion=true, expansion=false]{microtype}
7+
8+
The parsing has been modified to pass the complete line to the LaTeX system,
9+
keeping all commas.
10+
11+
Passing a list of strings from within a Python script still works as it used to.

lib/matplotlib/backends/backend_pgf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def get_fontspec():
5959

6060
def get_preamble():
6161
"""Get LaTeX preamble from rc."""
62-
return "\n".join(rcParams["pgf.preamble"])
62+
return rcParams["pgf.preamble"]
6363

6464
###############################################################################
6565

lib/matplotlib/rcsetup.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,20 @@ def validate_string_or_None(s):
175175
raise ValueError('Could not convert "%s" to string' % s)
176176

177177

178+
def _validate_stringlist_or_string(s):
179+
"""convert s to string or raise"""
180+
if s is None or s == 'None':
181+
return ""
182+
try:
183+
if isinstance(s, str):
184+
return s
185+
if isinstance(s, Iterable):
186+
return '\n'.join([str(i) for i in s])
187+
raise ValueError()
188+
except ValueError:
189+
raise ValueError('Could not convert "%s" to string' % s)
190+
191+
178192
def validate_axisbelow(s):
179193
try:
180194
return validate_bool(s)
@@ -1115,7 +1129,7 @@ def _validate_linestyle(ls):
11151129
'text.color': ['black', validate_color],
11161130
'text.usetex': [False, validate_bool],
11171131
'text.latex.unicode': [True, validate_bool],
1118-
'text.latex.preamble': [[], validate_stringlist],
1132+
'text.latex.preamble': ['', _validate_stringlist_or_string],
11191133
'text.latex.preview': [False, validate_bool],
11201134
'text.dvipnghack': [None, validate_bool_maybe_none],
11211135
'text.hinting': ['auto', validate_hinting],
@@ -1391,7 +1405,7 @@ def _validate_linestyle(ls):
13911405
# use matplotlib rc settings for font configuration
13921406
'pgf.rcfonts': [True, validate_bool],
13931407
# provide a custom preamble for the latex process
1394-
'pgf.preamble': [[], validate_stringlist],
1408+
'pgf.preamble': ['', _validate_stringlist_or_string],
13951409

13961410
# write raster image data directly into the svg file
13971411
'svg.image_inline': [True, validate_bool],

matplotlibrc.template

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -229,14 +229,18 @@
229229
#text.latex.preamble : ## IMPROPER USE OF THIS FEATURE WILL LEAD TO LATEX FAILURES
230230
## AND IS THEREFORE UNSUPPORTED. PLEASE DO NOT ASK FOR HELP
231231
## IF THIS FEATURE DOES NOT DO WHAT YOU EXPECT IT TO.
232-
## preamble is a comma separated list of LaTeX statements
233-
## that are included in the LaTeX document preamble.
234-
## An example:
235-
## text.latex.preamble : \usepackage{bm},\usepackage{euler}
232+
## text.latex.preamble is a single line of LaTeX code that
233+
## will be passed on to the LaTeX system. It may contain
234+
## any code that is valid for the LaTeX "preamble", i.e.
235+
## between the "\documentclass" and "\begin{document}"
236+
## statements.
237+
## Note that it has to be put on a single line, which may
238+
## become quite long.
236239
## The following packages are always loaded with usetex, so
237240
## beware of package collisions: color, geometry, graphicx,
238-
## type1cm, textcomp. Adobe Postscript (PSSNFS) font packages
239-
## may also be loaded, depending on your font settings
241+
## type1cm, textcomp.
242+
## Adobe Postscript (PSSNFS) font packages may also be
243+
## loaded, depending on your font settings.
240244
#text.latex.preview : False
241245

242246
#text.hinting : auto ## May be one of the following:
@@ -567,7 +571,7 @@
567571
## instead of uuid4
568572
### pgf parameter
569573
#pgf.rcfonts : True
570-
#pgf.preamble :
574+
#pgf.preamble : ## see text.latex.preamble for documentation
571575
#pgf.texsystem : xelatex
572576

573577
### docstring params

0 commit comments

Comments
 (0)