From cb6dda683ea6f509fa8640813268739b30ccd8d2 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Fri, 7 Dec 2018 11:27:51 +0100 Subject: [PATCH] Support ~ as nonbreaking space in mathtext. ... consistently with TeX. (AFAICT we never break mathtext over multiple lines so it's just a normal space for us.) See e.g. https://tex.stackexchange.com/a/74354/4101 for reference. --- doc/api/next_api_changes/2018-12-07-AL.rst | 4 ++++ lib/matplotlib/mathtext.py | 1 + lib/matplotlib/tests/test_mathtext.py | 8 +++++++- 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 doc/api/next_api_changes/2018-12-07-AL.rst diff --git a/doc/api/next_api_changes/2018-12-07-AL.rst b/doc/api/next_api_changes/2018-12-07-AL.rst new file mode 100644 index 000000000000..90b49235e4a3 --- /dev/null +++ b/doc/api/next_api_changes/2018-12-07-AL.rst @@ -0,0 +1,4 @@ +``~`` now interpreted as space in mathtext +`````````````````````````````````````````` +In constructs such as ``"$1~2$"``, mathtext now interprets the tilde as a +space, consistently with TeX (this was previously a parse error). diff --git a/lib/matplotlib/mathtext.py b/lib/matplotlib/mathtext.py index f3529a659f1c..0edd28a09a2a 100644 --- a/lib/matplotlib/mathtext.py +++ b/lib/matplotlib/mathtext.py @@ -2696,6 +2696,7 @@ def _make_space(self, percentage): r'\:' : 0.22222, # 4/18 em = 4 mu r'\;' : 0.27778, # 5/18 em = 5 mu r'\ ' : 0.33333, # 6/18 em = 6 mu + r'~' : 0.33333, # 6/18 em = 6 mu, nonbreakable r'\enspace' : 0.5, # 9/18 em = 9 mu r'\quad' : 1, # 1 em = 18 mu r'\qquad' : 2, # 2 em = 36 mu diff --git a/lib/matplotlib/tests/test_mathtext.py b/lib/matplotlib/tests/test_mathtext.py index fd6cebe1fc54..e98f5592019d 100644 --- a/lib/matplotlib/tests/test_mathtext.py +++ b/lib/matplotlib/tests/test_mathtext.py @@ -5,7 +5,7 @@ import pytest import matplotlib -from matplotlib.testing.decorators import image_comparison +from matplotlib.testing.decorators import check_figures_equal, image_comparison import matplotlib.pyplot as plt from matplotlib import mathtext @@ -271,3 +271,9 @@ def test_single_minus_sign(): # If this fails, it would be all white assert not np.all(array == 0xff) + + +@check_figures_equal(extensions=["png"]) +def test_spaces(fig_test, fig_ref): + fig_test.subplots().set_title(r"$1\,2\>3\ 4$") + fig_ref.subplots().set_title(r"$1\/2\:3~4$")