From 9bf512b52716435337aba29b07a42af28d71c8fe Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Wed, 14 Nov 2018 10:39:56 +0100 Subject: [PATCH] Don't insert spurious newlines by joining tex.preamble. The rc validator already makes it a single string. Also reject non-strings in the validator, as there isn't much use to them anyways in this context. --- ...cparams-pgf.preamble-full-LaTeX-support.rst | 2 ++ lib/matplotlib/rcsetup.py | 18 +++++++++--------- lib/matplotlib/texmanager.py | 2 +- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/doc/api/next_api_changes/2018-10-30-rcparams-pgf.preamble-full-LaTeX-support.rst b/doc/api/next_api_changes/2018-10-30-rcparams-pgf.preamble-full-LaTeX-support.rst index 892130589744..9e27f67c38fd 100644 --- a/doc/api/next_api_changes/2018-10-30-rcparams-pgf.preamble-full-LaTeX-support.rst +++ b/doc/api/next_api_changes/2018-10-30-rcparams-pgf.preamble-full-LaTeX-support.rst @@ -9,3 +9,5 @@ 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. + +Passing a list containing non-strings now fails, instead of coercing the results to strings. diff --git a/lib/matplotlib/rcsetup.py b/lib/matplotlib/rcsetup.py index 488d4304edd1..f054fa9e694f 100644 --- a/lib/matplotlib/rcsetup.py +++ b/lib/matplotlib/rcsetup.py @@ -175,17 +175,17 @@ 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""" +def _validate_tex_preamble(s): 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: + elif isinstance(s, Iterable): + return '\n'.join(s) + else: + raise TypeError + except TypeError: raise ValueError('Could not convert "%s" to string' % s) @@ -412,7 +412,7 @@ def validate_color(s): def validate_string(s): - if isinstance(s, (str, str)): + if isinstance(s, str): # Always leave str as str and unicode as unicode return s else: @@ -1129,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_or_string], + 'text.latex.preamble': ['', _validate_tex_preamble], 'text.latex.preview': [False, validate_bool], 'text.dvipnghack': [None, validate_bool_maybe_none], 'text.hinting': ['auto', validate_hinting], @@ -1405,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_or_string], + 'pgf.preamble': ['', _validate_tex_preamble], # write raster image data directly into the svg file 'svg.image_inline': [True, validate_bool], diff --git a/lib/matplotlib/texmanager.py b/lib/matplotlib/texmanager.py index 0f61dbdca441..3841be4ecad4 100644 --- a/lib/matplotlib/texmanager.py +++ b/lib/matplotlib/texmanager.py @@ -181,7 +181,7 @@ def get_font_preamble(self): def get_custom_preamble(self): """Return a string containing user additions to the tex preamble.""" - return '\n'.join(rcParams['text.latex.preamble']) + return rcParams['text.latex.preamble'] def make_tex(self, tex, fontsize): """