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

Skip to content

\bf\it in mathtext #19393

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

Closed
beojan opened this issue Jan 28, 2021 · 7 comments · Fixed by #25359
Closed

\bf\it in mathtext #19393

beojan opened this issue Jan 28, 2021 · 7 comments · Fixed by #25359

Comments

@beojan
Copy link

beojan commented Jan 28, 2021

Problem

\bf and \it are both supported in mathtext but if used together, only the inner style is applied.

Proposed Solution

Apply both styles if both \bf and \it are used together. Alternatively, a new \bfit command could be introduced which enables text to be set in bold and italic.

Additional context and prior art

This would be very useful when setting the experiment label in figures for the ATLAS experiment, which requires the name of the experiment in bold-italic followed by text describing the status of that figure in ordinary text. Additional text below this label communicates various experimental conditions that are often useful in interpreting the figure. In the absence of rich text functionality, mathtext (or usetex) is the only way to do this.

I currently handle this in beojan/atlas-mpl by using usetex. However, this requires the user to have a full LaTeX installation available, along with the TeX Gyre fonts in that installation in order to meet the font family requirements of the experiment.

@andrzejnovak handles the same issue in scikit-hep/mplhep by setting the various parts of the label separately, heuristically calculating the offsets needed. This is suboptimal compared to using the text layout algorithms built into the Matplotlib text engine.

@anntzer
Copy link
Contributor

anntzer commented Jan 28, 2021

I have some (no-guarantee) plans to fix that, but that'll likely wait after the deprecation in #18378 elapses (so 3.6 timeframe) as that'll likely require large amounts of surgery and maintaining backcompat on all the internals would be a lot of work...

@oscargus
Copy link
Member

oscargus commented Jan 9, 2022

But that is how TeX works, right? \bf and \it cannot be combined in regular TeX. https://tex.stackexchange.com/questions/41681/correct-way-to-bold-italicize-text

@beojan
Copy link
Author

beojan commented Jan 9, 2022

The textbf / textit commands on the other hand can be nested. The point is to allow bold-italic which currently isn't possible in mathtext.

@anntzer
Copy link
Contributor

anntzer commented Jan 12, 2022

Actually, looking at this again, we don't supply textbf/textit at all, but mathbf/mathit, which cannot be nested. The correct solution seems to be to provide \boldmath and/or \bm. See https://tex.stackexchange.com/questions/3238/bm-package-versus-boldsymbol for a discussion of the differences, but I'd say that these are minor enough that we can just ignore them and pretend the commands are the same.

@anntzer
Copy link
Contributor

anntzer commented Jan 16, 2022

Just doing some preliminary checkings for this... First, let's see what currently works. Because of the way things are currently implemented (font specs get stored as State.font and rm/bf/it get additionally stored as State.font_class), the only combos that can possibly work are one of rm/bf/it together with one of {none,tt,cal,sf,default,bb,frak,scr}. Let's look at all of them (in uppercase, as e.g. cal doesn't cover lowercase at all):

test

We see that the only multicombos that currently work are it+bb, bf+bb, bf+frak, and, for stix only, bf+sf, it+sf. (A quick check with usetex=True and \usepackage{amssymb} (for mathfrak) indicates that none of these stack properly in TeX, which always follows the inner (last) font macro).

Looking at https://ctan.gutenberg.eu.org/fonts/stix/doc/stix.pdf, reasonable additional combos we may want to support are bfit, bfscr, bfsfit, bfbbit, and expand the support for bfsf, sfit for other fonts. https://ctan.gutenberg.eu.org/macros/unicodetex/latex/unicode-math/unicode-math.pdf further mentions bfcal, bfscr, bffrak, although I don't known if we actually have any fonts with such glyphs.

I think the API here that maintains reasonable levels of compatibility with TeX is to actually stop making \mathit{\mathbb{...}} and friends stack properly (reverting to TeX's standard behavior -- fortunately, 1) I guess these usages are rare and 2) this is something on which we can reasonably emit deprecation warnings) and introduce the \mathbbit, \mathbfbb, \mathbffrak, \mathbfit, etc. (using the same names as stix and unicode-math) macros that select the corresponding fonts. (Then we may get away without \bm/\boldmath, but these should be easy enough to add on top of that as well.)


The implementation, for reference:

from matplotlib import pyplot as plt

plt.rcParams["text.latex.preamble"] = r"\usepackage{amssymb}"
fig = plt.figure(figsize=(15, 8))
sfs = fig.subfigures(2, 3)
fontsets = ["dejavusans", "dejavuserif", "cm", "stixsans", "stix"]
styles = ["rm", "it", "bf"]
families = ["", "tt", "cal", "sf", "default", "bb", "frak", "scr"]

for sf, fs in zip(sfs.ravel(), fontsets):
    sf.suptitle(fs, size=16)
    for x, st in enumerate(styles):
        for y, fm in enumerate(families[::-1]):
            # Upper-case text as some fonts, e.g. cal, don't cover lowercase.
            s = ("$\math%s{\math%s{%s+%s}}$" % (st, fm, st.upper(), fm.upper())
                 if fm else
                 "$\math%s{%s}$" % (st, st.upper()))
            sf.text(
                (x+.5)/len(styles), (y+.5)/(len(families)+1), s,
                math_fontfamily=fs, size=16, ha="center")

plt.show()

@oscargus
Copy link
Member

Here is a related issue that probably can be solved simultaneously (and is another twist of the same topic): #22056

@devRD
Copy link
Contributor

devRD commented Feb 19, 2023

Hi, getting bold italics on matplotlib would be interesting, if no one is working on this, I would like to take this issue.

@devRD devRD mentioned this issue Mar 1, 2023
6 tasks
@QuLogic QuLogic added this to the v3.8.0 milestone Apr 21, 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 a pull request may close this issue.

5 participants