Skip to content

Commit 1de8ffb

Browse files
committed
Add boldsymbol functionality in mathtext
1 parent 5ddf9c6 commit 1de8ffb

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed
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

+36
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 (
@@ -1811,6 +1812,11 @@ class _MathStyle(enum.Enum):
18111812
_right_delims = set(r") ] \} > \rfloor \rangle \rceil".split())
18121813
_delims = _left_delims | _right_delims | _ambi_delims
18131814

1815+
_small_greek = set([unicodedata.name(chr(i)).split()[-1].lower() for i in
1816+
range(ord('\N{GREEK SMALL LETTER ALPHA}'),
1817+
ord('\N{GREEK SMALL LETTER OMEGA}') + 1)])
1818+
_latin_alphabets = set(string.ascii_letters)
1819+
18141820
def __init__(self):
18151821
p = types.SimpleNamespace()
18161822

@@ -1933,6 +1939,9 @@ def csnames(group, names):
19331939

19341940
p.operatorname = cmd(r"\operatorname", "{" + ZeroOrMore(p.simple)("name") + "}")
19351941

1942+
p.boldsymbol = cmd(
1943+
r"\boldsymbol", "{" + ZeroOrMore(p.simple)("value") + "}")
1944+
19361945
p.placeable <<= (
19371946
p.accent # Must be before symbol as all accents are symbols
19381947
| p.symbol # Must be second to catch all named symbols and single
@@ -1949,6 +1958,7 @@ def csnames(group, names):
19491958
| p.sqrt
19501959
| p.overline
19511960
| p.text
1961+
| p.boldsymbol
19521962
)
19531963

19541964
p.auto_delim <<= (
@@ -2597,3 +2607,29 @@ def auto_delim(self, s, loc, toks):
25972607
# if "mid" in toks ... can be removed when requiring pyparsing 3.
25982608
toks["mid"].asList() if "mid" in toks else [],
25992609
toks["right"])
2610+
2611+
def boldsymbol(self, s, loc, toks):
2612+
self.push_state()
2613+
state = self.get_state()
2614+
hlist = []
2615+
name = toks["value"]
2616+
for c in name:
2617+
if isinstance(c, Hlist):
2618+
k = c.children[1]
2619+
if isinstance(k, Char):
2620+
k.font = "bf"
2621+
k._update_metrics()
2622+
hlist.append(c)
2623+
elif isinstance(c, Char):
2624+
c.font = "bf"
2625+
if (c.c in self._latin_alphabets or
2626+
c.c[1:] in self._small_greek):
2627+
c.font = "bfit"
2628+
c._update_metrics()
2629+
c._update_metrics()
2630+
hlist.append(c)
2631+
else:
2632+
hlist.append(c)
2633+
self.pop_state()
2634+
2635+
return Hlist(hlist)

lib/matplotlib/tests/test_mathtext.py

+7-1
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)