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

Skip to content

Commit e56ce6c

Browse files
committed
Add boldsymbol functionality in mathtext
1 parent 088e50b commit e56ce6c

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-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: 28 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 (
@@ -1864,6 +1865,7 @@ def csnames(group, names):
18641865
p.text = Forward()
18651866
p.token = Forward()
18661867
p.underset = Forward()
1868+
p.boldsymbol = Forward()
18671869

18681870
set_names_and_parse_actions() # for mutually recursive definitions.
18691871

@@ -1913,6 +1915,8 @@ def csnames(group, names):
19131915
p.optional_group("annotation") + p.optional_group("body"))
19141916

19151917
p.text <<= cmd(r"\text", QuotedString('{', '\\', endQuoteChar="}"))
1918+
p.boldsymbol <<= cmd(
1919+
r"\boldsymbol", "{" + ZeroOrMore(p.simple)("value") + "}")
19161920

19171921
p.placeable <<= (
19181922
p.accent # Must be before symbol as all accents are symbols
@@ -1930,6 +1934,7 @@ def csnames(group, names):
19301934
| p.sqrt
19311935
| p.overline
19321936
| p.text
1937+
| p.boldsymbol
19331938
)
19341939

19351940
p.simple <<= (
@@ -2599,3 +2604,26 @@ def auto_delim(self, s, loc, toks):
25992604
# if "mid" in toks ... can be removed when requiring pyparsing 3.
26002605
toks["mid"].asList() if "mid" in toks else [],
26012606
toks["right"])
2607+
2608+
def boldsymbol(self, s, loc, toks):
2609+
self.push_state()
2610+
state = self.get_state()
2611+
small_greek = [unicodedata.name(chr(i)).split()[-1].lower() for i in
2612+
range(ord('\N{GREEK SMALL LETTER ALPHA}'),
2613+
ord('\N{GREEK SMALL LETTER OMEGA}') + 1)]
2614+
latin_alphabets = string.ascii_letters
2615+
hlist = []
2616+
name = toks["value"]
2617+
for c in name:
2618+
if isinstance(c, Char):
2619+
c.font = "bf"
2620+
if c.c in latin_alphabets or c.c[1:] in small_greek:
2621+
c.font = "bfit"
2622+
c._update_metrics()
2623+
c._update_metrics()
2624+
hlist.append(c)
2625+
else:
2626+
hlist.append(c)
2627+
self.pop_state()
2628+
2629+
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)