Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Support \underline in Mathtext. #23616

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions doc/users/next_whats_new/mathtext_underline.rst
Original file line number Diff line number Diff line change
@@ -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()
17 changes: 17 additions & 0 deletions lib/matplotlib/_mathtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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",
Expand All @@ -1843,6 +1845,7 @@ def csnames(group, names):
| p.underset
| p.sqrt
| p.overline
| p.underline
)

p.simple <<= (
Expand Down Expand Up @@ -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):
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions lib/matplotlib/tests/test_mathtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down