From a973b75f650b771f4f04b0e74316ccda349f7f8a Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 18 Jun 2019 11:49:37 +0200 Subject: [PATCH] Fix unicode_minus + usetex. ... by telling TeX to treat \u2212 (unicode minus) as a minus sign. This also uncovered a bug in _parse_enc (basically, there can be multiple character entries per line, so one needs to use `.split()` instead of `.split("\n")`. --- lib/matplotlib/dviread.py | 2 +- lib/matplotlib/tests/test_usetex.py | 24 +++++++++++++----------- lib/matplotlib/texmanager.py | 18 ++++++++---------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/lib/matplotlib/dviread.py b/lib/matplotlib/dviread.py index 611218d6236c..dbb7d2602d38 100644 --- a/lib/matplotlib/dviread.py +++ b/lib/matplotlib/dviread.py @@ -989,7 +989,7 @@ def _parse_enc(path): with open(path, encoding="ascii") as file: no_comments = "\n".join(line.split("%")[0].rstrip() for line in file) array = re.search(r"(?s)\[(.*)\]", no_comments).group(1) - lines = [line for line in array.split("\n") if line] + lines = [line for line in array.split() if line] if all(line.startswith("/") for line in lines): return [line[1:] for line in lines] else: diff --git a/lib/matplotlib/tests/test_usetex.py b/lib/matplotlib/tests/test_usetex.py index 84af0fb790c9..d200c7792b39 100644 --- a/lib/matplotlib/tests/test_usetex.py +++ b/lib/matplotlib/tests/test_usetex.py @@ -1,26 +1,22 @@ -import warnings - import pytest -import matplotlib -from matplotlib.testing.decorators import image_comparison +import matplotlib as mpl +from matplotlib.testing.decorators import check_figures_equal, image_comparison import matplotlib.pyplot as plt from matplotlib.ticker import EngFormatter -with warnings.catch_warnings(): - warnings.simplefilter('ignore') - needs_usetex = pytest.mark.skipif( - not matplotlib.checkdep_usetex(True), - reason='Missing TeX of Ghostscript or dvipng') +@pytest.fixture(autouse=True) # All tests in this module use usetex. +def usetex(): + if not mpl.checkdep_usetex(True): + pytest.skip('Missing TeX of Ghostscript or dvipng') + mpl.rcParams['text.usetex'] = True -@needs_usetex @image_comparison(baseline_images=['test_usetex'], extensions=['pdf', 'png'], tol=0.3) def test_usetex(): - matplotlib.rcParams['text.usetex'] = True fig = plt.figure() ax = fig.add_subplot(111) ax.text(0.1, 0.2, @@ -32,3 +28,9 @@ def test_usetex(): fontsize=24) ax.set_xticks([]) ax.set_yticks([]) + + +@check_figures_equal() +def test_unicode_minus(fig_test, fig_ref): + fig_test.text(.5, .5, "$-$") + fig_ref.text(.5, .5, "\N{MINUS SIGN}") diff --git a/lib/matplotlib/texmanager.py b/lib/matplotlib/texmanager.py index 8a4b58331859..f2470ff038ec 100644 --- a/lib/matplotlib/texmanager.py +++ b/lib/matplotlib/texmanager.py @@ -206,11 +206,10 @@ def make_tex(self, tex, fontsize): r'{\rmfamily %s}') tex = fontcmd % tex - if rcParams['text.latex.unicode']: - unicode_preamble = r""" -\usepackage[utf8]{inputenc}""" - else: - unicode_preamble = '' + unicode_preamble = "\n".join([ + r"\usepackage[utf8]{inputenc}", + r"\DeclareUnicodeCharacter{2212}{\ensuremath{-}}", + ]) if rcParams["text.latex.unicode"] else "" s = r""" \documentclass{article} @@ -257,11 +256,10 @@ def make_tex_preview(self, tex, fontsize): r'{\rmfamily %s}') tex = fontcmd % tex - if rcParams['text.latex.unicode']: - unicode_preamble = r""" -\usepackage[utf8]{inputenc}""" - else: - unicode_preamble = '' + unicode_preamble = "\n".join([ + r"\usepackage[utf8]{inputenc}", + r"\DeclareUnicodeCharacter{2212}{\ensuremath{-}}", + ]) if rcParams["text.latex.unicode"] else "" # newbox, setbox, immediate, etc. are used to find the box # extent of the rendered text.