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

Skip to content

Commit ebbd6be

Browse files
committed
Add boldsymbol functionality in mathtext
1 parent e528649 commit ebbd6be

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Boldsymbol mathtext command ``\boldsymbol``
2+
-------------------------------------------
3+
4+
Supports using the ``\boldsymbol{}`` command in mathtext:
5+
6+
To change symbols to bold enclose the text in a font command as
7+
shown:
8+
9+
.. code-block::
10+
11+
r'$\boldsymbol{a+2+\alpha}$'
12+
13+
.. math::
14+
\boldsymbol{a+2+\alpha}

lib/matplotlib/_mathtext.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import re
1212
import types
1313
import unicodedata
14+
import string
1415

1516
import numpy as np
1617
from pyparsing import (
@@ -1886,6 +1887,7 @@ def csnames(group, names):
18861887
p.text = Forward()
18871888
p.token = Forward()
18881889
p.underset = Forward()
1890+
p.boldsymbol = Forward()
18891891

18901892
set_names_and_parse_actions() # for mutually recursive definitions.
18911893

@@ -1935,6 +1937,8 @@ def csnames(group, names):
19351937
p.optional_group("annotation") + p.optional_group("body"))
19361938

19371939
p.text <<= cmd(r"\text", QuotedString('{', '\\', endQuoteChar="}"))
1940+
p.boldsymbol <<= cmd(
1941+
r"\boldsymbol", "{" + ZeroOrMore(p.simple)("value") + "}")
19381942

19391943
p.placeable <<= (
19401944
p.accent # Must be before symbol as all accents are symbols
@@ -1952,6 +1956,7 @@ def csnames(group, names):
19521956
| p.sqrt
19531957
| p.overline
19541958
| p.text
1959+
| p.boldsymbol
19551960
)
19561961

19571962
p.simple <<= (
@@ -2621,3 +2626,31 @@ def auto_delim(self, s, loc, toks):
26212626
# if "mid" in toks ... can be removed when requiring pyparsing 3.
26222627
toks["mid"].asList() if "mid" in toks else [],
26232628
toks["right"])
2629+
2630+
def boldsymbol(self, s, loc, toks):
2631+
self.push_state()
2632+
state = self.get_state()
2633+
small_greek = [unicodedata.name(chr(i)).split()[-1].lower() for i in
2634+
range(ord('\N{GREEK SMALL LETTER ALPHA}'),
2635+
ord('\N{GREEK SMALL LETTER OMEGA}') + 1)]
2636+
latin_alphabets = string.ascii_letters
2637+
hlist = []
2638+
name = toks["value"]
2639+
for c in name:
2640+
if isinstance(c, Hlist):
2641+
k = c.children[1]
2642+
if isinstance(k, Char):
2643+
k.font = "bf"
2644+
k._update_metrics()
2645+
if isinstance(c, Char):
2646+
c.font = "bf"
2647+
if c.c in latin_alphabets or c.c[1:] in small_greek:
2648+
c.font = "bfit"
2649+
c._update_metrics()
2650+
c._update_metrics()
2651+
hlist.append(c)
2652+
else:
2653+
hlist.append(c)
2654+
self.pop_state()
2655+
2656+
return Hlist(hlist)

lib/matplotlib/tests/test_mathtext.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@
134134
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
135135
r'$1.$ $2.$ $19680801.$ $a.$ $b.$ $mpl.$',
136136
r'$\text{text}_{\text{sub}}^{\text{sup}} + \text{\$foo\$} + \frac{\text{num}}{\mathbf{\text{den}}}\text{with space, curly brackets \{\}, and dash -}$',
137-
137+
r'$\boldsymbol{abcde} \boldsymbol{+} \boldsymbol{\Gamma + \Omega} \boldsymbol{01234} \boldsymbol{\alpha * \beta}$',
138138
]
139139

140140
digits = "0123456789"
@@ -538,3 +538,9 @@ def test_mathtext_operators():
538538
fig.text(0.5, (x + 0.5)/len(test_str), r'${%s}$' % i)
539539

540540
fig.draw_without_rendering()
541+
542+
543+
@check_figures_equal(extensions=["png"])
544+
def test_boldsymbol(fig_test, fig_ref):
545+
fig_test.text(0.1, 0.2, r"$\boldsymbol{\mathrm{abc0123\alpha}}$")
546+
fig_ref.text(0.1, 0.2, r"$\mathrm{abc0123\alpha}$")

0 commit comments

Comments
 (0)