diff --git a/lib/matplotlib/backends/backend_pgf.py b/lib/matplotlib/backends/backend_pgf.py index a36dda160c06..a55c931387a8 100644 --- a/lib/matplotlib/backends/backend_pgf.py +++ b/lib/matplotlib/backends/backend_pgf.py @@ -10,7 +10,6 @@ import re import shutil import subprocess -import sys from tempfile import TemporaryDirectory import weakref @@ -101,6 +100,7 @@ def common_texification(text): # Sometimes, matplotlib adds the unknown command \mathdefault. # Not using \mathnormal instead since this looks odd for the latex cm font. text = _replace_mathdefault(text) + text = text.replace("\N{MINUS SIGN}", r"\ensuremath{-}") # split text into normaltext and inline math parts parts = re_mathsep.split(text) for i, s in enumerate(parts): @@ -792,10 +792,7 @@ def _print_pgf_to_fh(self, fh, *, bbox_inches_restore=None): %% Make sure the required packages are loaded in your preamble %% \\usepackage{pgf} %% -%% and, on pdftex -%% \\usepackage[utf8]{inputenc}\\DeclareUnicodeCharacter{2212}{-} -%% -%% or, on luatex and xetex +%% and on luatex and xetex %% \\usepackage{unicode-math} %% %% Figures using additional raster images can only be included by \\input if diff --git a/lib/matplotlib/tests/test_backend_pgf.py b/lib/matplotlib/tests/test_backend_pgf.py index e089ab4f9c33..e71b6291e4c6 100644 --- a/lib/matplotlib/tests/test_backend_pgf.py +++ b/lib/matplotlib/tests/test_backend_pgf.py @@ -12,8 +12,10 @@ import matplotlib as mpl import matplotlib.pyplot as plt from matplotlib.testing.compare import compare_images, ImageComparisonFailure -from matplotlib.testing.decorators import image_comparison, _image_directories from matplotlib.backends.backend_pgf import PdfPages, common_texification +from matplotlib.testing.decorators import (_image_directories, + check_figures_equal, + image_comparison) baseline_dir, result_dir = _image_directories(lambda: 'dummy func') @@ -338,3 +340,14 @@ def test_unknown_font(caplog): plt.savefig(BytesIO(), format="pgf") assert "Ignoring unknown font: this-font-does-not-exist" in [ r.getMessage() for r in caplog.records] + + +@check_figures_equal(extensions=["pdf"]) +@pytest.mark.parametrize("texsystem", ("pdflatex", "xelatex", "lualatex")) +@pytest.mark.backend("pgf") +def test_minus_signs_with_tex(fig_test, fig_ref, texsystem): + if not check_for(texsystem): + pytest.skip(texsystem + ' + pgf is required') + mpl.rcParams["pgf.texsystem"] = texsystem + fig_test.text(.5, .5, "$-1$") + fig_ref.text(.5, .5, "$\N{MINUS SIGN}1$")