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

boldsymbol support for mathtext #25661

merged 1 commit into from
Jul 11, 2023

Conversation

devRD
Copy link
Contributor

@devRD devRD commented Apr 11, 2023

PR Summary

Fixes #25643
Fixes #1366
Depends on PR #25359
Add boldsymbol command

PR Checklist

Documentation and Tests

  • Has pytest style unit tests (and pytest passes)
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • New plotting related features are documented with examples.

Release Notes

  • New features are marked with a .. versionadded:: directive in the docstring and documented in doc/users/next_whats_new/
  • API changes are marked with a .. versionchanged:: directive in the docstring and documented in doc/api/next_api_changes/
  • Release notes conform with instructions in next_whats_new/README.rst or next_api_changes/README.rst

Copy link
Member

@oscargus oscargus left a comment

Choose a reason for hiding this comment

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

Looks good! One may consider updating the what's new-note in #25359 in some way instead of having a separate one, but for now this is as good as it can be.

And maybe add something to the documentation, but will be easier once #25359 is in.

To change font to bold and italic 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)

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

@devRD devRD force-pushed the boldsym branch 3 times, most recently from 7371e42 to 1447b6d Compare April 21, 2023 16:11
@ksunden
Copy link
Member

ksunden commented Apr 25, 2023

The PR linked above has been merged, is this still waiting on anything?

@devRD devRD marked this pull request as ready for review April 26, 2023 04:20
@devRD
Copy link
Contributor Author

devRD commented Apr 26, 2023

Hi @ksunden, This PR is not waiting, I opened it for review. Thanks!

fig_test.text(0.1, 0.1, r"$\boldsymbol{abc0123\alpha}$")
fig_test.text(0.1, 0.2, r"$\boldsymbol{\mathrm{abc0123\alpha}}$")

fig_ref.text(0.1, 0.1, r"$\boldsymbol{abc0123\alpha}$")
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should use \mathbfit, no?

@anntzer
Copy link
Contributor

anntzer commented May 2, 2023

I'm sorry for doing this review in a piecemeal fashion, but I think that we still don't know exactly what are the semantics of boldsymbol in tex and should thus figure this out exactly first.
In particular, looking at this again, it seems like \boldsymbol{\mathrm{foo}} prints foo in bold roman (try with text.latex.preamble = "\usepackage{amsmath}" and usetex=True, which suggests to me that boldsymbol is in fact not synonym with mathbfit at all, but perhaps simply toggles the boldness of the font without touching italicness or anything else?

@devRD
Copy link
Contributor Author

devRD commented May 2, 2023

After exploring a bit on boldsymbol, I agree that it slightly differs from mathbfit. From my limited understanding, I suppose the format differs in accordance with various symbols.
For e.g.:
Digits from 0-9 are upright bold
Alphabets A-Za-z are bold+italic
Greek symbols like \alpha and \omega are bold+italic but...
symbols \Gamma and \Omega are just upright bold.

I generated this image for \boldsymbol, \boldsymbol{\mathrm{foo}} and \mathbfit (in order) using text.preamble and text.usetex=True

(some symbols weren't rendered properly, I'm guessing due to LaTeX package differences)
image

This is the link that I used for reference. http://tug.ctan.org/tex-archive/macros/amstex/doc/amsguide.pdf

.lower(),
range(ord('\N{GREEK SMALL LETTER ALPHA}'),
ord('\N{GREEK SMALL LETTER OMEGA}') + 1)))
allLatin = [*lw, *up]
Copy link
Member

Choose a reason for hiding this comment

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

This is equal to string.ascii_letters.

for c in name:
if isinstance(c, Char):
c.font = "bf"
if str(c)[1] in allLatin or str(c)[2:-1] in smGreek:
Copy link
Member

@QuLogic QuLogic Jun 12, 2023

Choose a reason for hiding this comment

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

str(c)[1:-1] is equal to c.c for Char, I think?

Suggested change
if str(c)[1] in allLatin or str(c)[2:-1] in smGreek:
if c.c in string.ascii_letters or c.c[1:] in lower_greek:

Comment on lines 2612 to 2616
smGreek = list(map(lambda a: unicodedata.name(chr(a))
.split()[-1]
.lower(),
range(ord('\N{GREEK SMALL LETTER ALPHA}'),
ord('\N{GREEK SMALL LETTER OMEGA}') + 1)))
Copy link
Member

Choose a reason for hiding this comment

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

Should rename for PEP8, and maybe also prefer list comprehensions

Suggested change
smGreek = list(map(lambda a: unicodedata.name(chr(a))
.split()[-1]
.lower(),
range(ord('\N{GREEK SMALL LETTER ALPHA}'),
ord('\N{GREEK SMALL LETTER OMEGA}') + 1)))
lower_greek = [
unicodedata.name(chr(a)).split()[-1].lower()
for a in range(ord('\N{GREEK SMALL LETTER ALPHA}'),
ord('\N{GREEK SMALL LETTER OMEGA}') + 1))]

@devRD devRD force-pushed the boldsym branch 2 times, most recently from 6e7850e to 3ac9e61 Compare June 13, 2023 09:10
Comment on lines 1 to 2
Boldsymbol mathtext command ``\boldsymbol``
-----------------------------------------------------
Copy link
Member

Choose a reason for hiding this comment

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

Should match text length to underline.

Copy link
Member

@oscargus oscargus left a comment

Choose a reason for hiding this comment

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

Trying the image test code out on normal LaTeX, it seems like the single + should not be bold?

@devRD
Copy link
Contributor Author

devRD commented Jun 23, 2023

I checked the + using an online LaTeX document and with text.usetex=True. It looks like a bold + when I compare it with \mathrm{+}. I got the following figure using text.usetex=True
Figure_1

@oscargus
Copy link
Member

OK! I guess my point really was that the + in \boldsymbol{\Gamma + \Omega} looks different compared to \boldsymbol{+}, but it seems like I draw the wrong conclusion about the reason.

Is it really different or is there just some pixel snapping issue in the test image that makes it look that way?

@devRD
Copy link
Contributor Author

devRD commented Jun 24, 2023

Good catch!
Looking further into this, it is different. The \Gamma + \Omega seems to parse the + in this context as an Hlist and not an instance of Char so the state.font is not applying.

@devRD devRD force-pushed the boldsym branch 4 times, most recently from dd805ef to 10a1ad7 Compare June 26, 2023 08:44
Comment on lines 2640 to 2632
if isinstance(c, Hlist):
k = c.children[1]
if isinstance(k, Char):
k.font = "bf"
k._update_metrics()
if isinstance(c, Char):
c.font = "bf"
if c.c in latin_alphabets or c.c[1:] in small_greek:
c.font = "bfit"
c._update_metrics()
c._update_metrics()
hlist.append(c)
else:
hlist.append(c)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if isinstance(c, Hlist):
k = c.children[1]
if isinstance(k, Char):
k.font = "bf"
k._update_metrics()
if isinstance(c, Char):
c.font = "bf"
if c.c in latin_alphabets or c.c[1:] in small_greek:
c.font = "bfit"
c._update_metrics()
c._update_metrics()
hlist.append(c)
else:
hlist.append(c)
if isinstance(c, Hlist):
k = c.children[1]
if isinstance(k, Char):
k.font = "bf"
k._update_metrics()
elif isinstance(c, Char):
c.font = "bf"
if c.c in latin_alphabets or c.c[1:] in small_greek:
c.font = "bfit"
c._update_metrics()
c._update_metrics()
hlist.append(c)

state = self.get_state()
small_greek = [unicodedata.name(chr(i)).split()[-1].lower() for i in
range(ord('\N{GREEK SMALL LETTER ALPHA}'),
ord('\N{GREEK SMALL LETTER OMEGA}') + 1)]
Copy link
Member

Choose a reason for hiding this comment

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

Maybe one should move this to be a member of the parser class so that it is not recreated every time?

(And considering that it is only used for testing membership, make it a set?)

@oscargus
Copy link
Member

Nice that you got it to work! Just some minor suggestions for clarity and performance.

@devRD devRD force-pushed the boldsym branch 2 times, most recently from 2cfd449 to 05a3261 Compare June 26, 2023 15:42
@ksunden ksunden merged commit fa68f46 into matplotlib:main Jul 11, 2023
@ksunden ksunden added this to the v3.8.0 milestone Jul 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ENH]: Support for \boldsymbol Support \boldsymbol. (Feature request.)
5 participants