diff --git a/doc/users/next_whats_new/mathtext_underline.rst b/doc/users/next_whats_new/mathtext_underline.rst new file mode 100644 index 000000000000..118e5cd8f9e7 --- /dev/null +++ b/doc/users/next_whats_new/mathtext_underline.rst @@ -0,0 +1,11 @@ +Underlining text while using Mathtext +------------------------------------- + +Mathtext now supports the ``\underline`` command. + +.. code-block:: python + + import matplotlib.pyplot as plt + plt.text(0.4, 0.7, r'This is $\underline{underlined}$ text.') + plt.text(0.4, 0.3, r'So is $\underline{\mathrm{this}}$.') + plt.show() diff --git a/lib/matplotlib/_mathtext.py b/lib/matplotlib/_mathtext.py index 0aa7bec57418..aae766263307 100644 --- a/lib/matplotlib/_mathtext.py +++ b/lib/matplotlib/_mathtext.py @@ -1770,6 +1770,7 @@ def csnames(group, names): p.group = Forward() p.operatorname = Forward() p.overline = Forward() + p.underline = Forward() p.overset = Forward() p.placeable = Forward() p.required_group = Forward() @@ -1820,6 +1821,7 @@ def csnames(group, names): + p.required_group("value")) p.overline <<= cmd(r"\overline", p.required_group("body")) + p.underline <<= cmd(r"\underline", p.required_group("body")) p.overset <<= cmd( r"\overset", @@ -1843,6 +1845,7 @@ def csnames(group, names): | p.underset | p.sqrt | p.overline + | p.underline ) p.simple <<= ( @@ -2478,6 +2481,20 @@ def overline(self, s, loc, toks): hlist = Hlist([rightside]) return [hlist] + def underline(self, s, loc, toks): + body = toks["body"] + + state = self.get_state() + thickness = state.get_current_underline_thickness() + + # Place underline below body (loosely based on node735). + kern = 3 * thickness + vlist = Vlist([Hlist([body]), Kern(kern), Hrule(state, thickness)]) + vlist.shift_amount = kern + thickness / 2 + body.depth + + hlist = Hlist([vlist]) + return [hlist] + def _auto_sized_delimiter(self, front, middle, back): state = self.get_state() if len(middle): diff --git a/lib/matplotlib/tests/baseline_images/test_mathtext/mathtext1_dejavusans_04.png b/lib/matplotlib/tests/baseline_images/test_mathtext/mathtext1_dejavusans_04.png new file mode 100644 index 000000000000..cc0a7c0a8cb2 Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_mathtext/mathtext1_dejavusans_04.png differ diff --git a/lib/matplotlib/tests/test_mathtext.py b/lib/matplotlib/tests/test_mathtext.py index 0c0057c9ea85..dde741d3af30 100644 --- a/lib/matplotlib/tests/test_mathtext.py +++ b/lib/matplotlib/tests/test_mathtext.py @@ -129,6 +129,7 @@ r'$x \overset{f}{\rightarrow} \overset{f}{x} \underset{xx}{ff} \overset{xx}{ff} \underset{f}{x} \underset{f}{\leftarrow} x$', # github issue #18241 r'$\sum x\quad\sum^nx\quad\sum_nx\quad\sum_n^nx\quad\prod x\quad\prod^nx\quad\prod_nx\quad\prod_n^nx$', # GitHub issue 18085 r'$1.$ $2.$ $19680801.$ $a.$ $b.$ $mpl.$', + r'Matplotlib: $\underline{Plotting}$ $\underline{\mathrm{with}}$ Python', # GitHub issue 14235 ] digits = "0123456789"