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

Skip to content

Commit 57f697c

Browse files
authored
Implement underline logic as in TeX (#23616)
2 parents cf3aacf + 44a6337 commit 57f697c

4 files changed

Lines changed: 31 additions & 0 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Underlining text while using Mathtext
2+
-------------------------------------
3+
4+
Mathtext now supports the ``\underline`` command.
5+
6+
.. code-block:: python
7+
8+
import matplotlib.pyplot as plt
9+
10+
plt.text(0.4, 0.7, r'This is $\underline{underlined}$ text.')
11+
plt.text(0.4, 0.3, r'So is $\underline{\mathrm{this}}$.')
12+
plt.show()

lib/matplotlib/_mathtext.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2207,6 +2207,8 @@ def csnames(group: str, names: Iterable[str]) -> Regex:
22072207

22082208
p.overline = cmd(r"\overline", p.required_group("body"))
22092209

2210+
p.underline = cmd(r"\underline", p.required_group("body"))
2211+
22102212
p.overset = cmd(
22112213
r"\overset",
22122214
p.optional_group("annotation") + p.optional_group("body"))
@@ -2259,6 +2261,7 @@ def csnames(group: str, names: Iterable[str]) -> Regex:
22592261
| p.underset
22602262
| p.sqrt
22612263
| p.overline
2264+
| p.underline
22622265
| p.text
22632266
| p.boldsymbol
22642267
| p.substack
@@ -2945,6 +2948,21 @@ def overline(self, toks: ParseResults) -> T.Any:
29452948
hlist = Hlist([rightside])
29462949
return [hlist]
29472950

2951+
def underline(self, toks: ParseResults) -> T.Any:
2952+
body = toks["body"]
2953+
state = self.get_state()
2954+
thickness = state.get_current_underline_thickness()
2955+
# Place the underline below `body` (node735).
2956+
vlist = Vlist([
2957+
Hlist([body]),
2958+
Kern(3 * thickness),
2959+
Hrule(state, thickness),
2960+
])
2961+
delta = vlist.height + vlist.depth + thickness
2962+
vlist.height = body.height
2963+
vlist.depth = delta - vlist.height
2964+
return [Hlist([vlist])]
2965+
29482966
def _auto_sized_delimiter(self, front: str,
29492967
middle: list[Box | Char | str],
29502968
back: str) -> T.Any:
2.49 KB
Loading

lib/matplotlib/tests/test_mathtext.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@
154154
r'$\left\lbrace\frac{\left\lbrack A^b_c\right\rbrace}{\left\leftbrace D^e_f \right\rbrack}\right\rightbrace\ \left\leftparen\max_{x} \left\lgroup \frac{A}{B}\right\rgroup \right\rightparen$',
155155
r'$\left( a\middle. b \right)$ $\left( \frac{a}{b} \middle\vert x_i \in P^S \right)$ $\left[ 1 - \middle| a\middle| + \left( x - \left\lfloor \dfrac{a}{b}\right\rfloor \right) \right]$',
156156
r'$\sum_{\substack{k = 1\\ k \neq \lfloor n/2\rfloor}}^{n}P(i,j) \sum_{\substack{i \neq 0\\ -1 \leq i \leq 3\\ 1 \leq j \leq 5}} F^i(x,y) \sum_{\substack{\left \lfloor \frac{n}{2} \right\rfloor}} F(n)$',
157+
' '.join(f'${c}\\underline{{{c}}}$' for c in 'abfghy') + r' $\underline{\left(\dfrac{num}{\underline{den}}\right)}^{\underline{p_1}}$',
157158
]
158159

159160
digits = "0123456789"

0 commit comments

Comments
 (0)