Skip to content

boldsymbol support for mathtext #25661

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

Merged
merged 1 commit into from
Jul 11, 2023
Merged
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
14 changes: 14 additions & 0 deletions doc/users/next_whats_new/boldsym_mathtext.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Boldsymbol mathtext command ``\boldsymbol``
-------------------------------------------

Supports using the ``\boldsymbol{}`` command in mathtext:

To change symbols to bold enclose the text in a font command as
shown:

.. code-block::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not fully get this warning, but I guess it must be fixed one way or the other. Maybe better to add a figure in the other PR, like plt.text(...) and then add the alternative version in that figure here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, I am getting these results from the current implementation. Should I add the mathbfit to the other PR, and the boldsymbol to the current one?

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is just missing a blank line.

.. code-block::

   r'$\boldsymbol{a+2+\alpha}$'

(note also that the closing quote is missing anyway)


r'$\boldsymbol{a+2+\alpha}$'

.. math::
\boldsymbol{a+2+\alpha}
36 changes: 36 additions & 0 deletions lib/matplotlib/_mathtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import re
import types
import unicodedata
import string

import numpy as np
from pyparsing import (
Expand Down Expand Up @@ -1811,6 +1812,11 @@ class _MathStyle(enum.Enum):
_right_delims = set(r") ] \} > \rfloor \rangle \rceil".split())
_delims = _left_delims | _right_delims | _ambi_delims

_small_greek = set([unicodedata.name(chr(i)).split()[-1].lower() for i in
range(ord('\N{GREEK SMALL LETTER ALPHA}'),
ord('\N{GREEK SMALL LETTER OMEGA}') + 1)])
_latin_alphabets = set(string.ascii_letters)

def __init__(self):
p = types.SimpleNamespace()

Expand Down Expand Up @@ -1933,6 +1939,9 @@ def csnames(group, names):

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

p.boldsymbol = cmd(
r"\boldsymbol", "{" + ZeroOrMore(p.simple)("value") + "}")

p.placeable <<= (
p.accent # Must be before symbol as all accents are symbols
| p.symbol # Must be second to catch all named symbols and single
Expand All @@ -1949,6 +1958,7 @@ def csnames(group, names):
| p.sqrt
| p.overline
| p.text
| p.boldsymbol
)

p.auto_delim <<= (
Expand Down Expand Up @@ -2597,3 +2607,29 @@ def auto_delim(self, s, loc, toks):
# if "mid" in toks ... can be removed when requiring pyparsing 3.
toks["mid"].asList() if "mid" in toks else [],
toks["right"])

def boldsymbol(self, s, loc, toks):
self.push_state()
state = self.get_state()
hlist = []
name = toks["value"]
for c in name:
if isinstance(c, Hlist):
k = c.children[1]
if isinstance(k, Char):
k.font = "bf"
k._update_metrics()
hlist.append(c)
elif isinstance(c, Char):
c.font = "bf"
if (c.c in self._latin_alphabets or
c.c[1:] in self._small_greek):
c.font = "bfit"
c._update_metrics()
c._update_metrics()
hlist.append(c)
else:
hlist.append(c)
self.pop_state()
Copy link
Contributor

@anntzer anntzer Apr 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't really check, but does that work with nesting? (i.e.\boldsymbol{\mathnormal{foo}} -- the inner one should win)

I'm not actually sure what the semantics of boldsymbol are in TeX, but shouldn't it just be an alias for mathbfit? (and perhaps the test could be based on check_figures_equal too)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check_figures_equal test in the PR currently gives this output:
test_boldsymbol png


return Hlist(hlist)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 7 additions & 1 deletion lib/matplotlib/tests/test_mathtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
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'$\text{text}_{\text{sub}}^{\text{sup}} + \text{\$foo\$} + \frac{\text{num}}{\mathbf{\text{den}}}\text{with space, curly brackets \{\}, and dash -}$',

r'$\boldsymbol{abcde} \boldsymbol{+} \boldsymbol{\Gamma + \Omega} \boldsymbol{01234} \boldsymbol{\alpha * \beta}$',
]

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

fig.draw_without_rendering()


@check_figures_equal(extensions=["png"])
def test_boldsymbol(fig_test, fig_ref):
fig_test.text(0.1, 0.2, r"$\boldsymbol{\mathrm{abc0123\alpha}}$")
fig_ref.text(0.1, 0.2, r"$\mathrm{abc0123\alpha}$")