Skip to content

Commit 4acd3be

Browse files
authored
Correctly read the 'style' argument while processing 'genfrac'. (matplotlib#23034)
* Correctly read the 'style' argument while processing 'genfrac'. * Default to text style if style not specified. * Hard-coded enumerations. Support script_stype and script_script_style. * Automatically convert style number to enumerated type. * Used double quotes for strings. * Verify that genfrac's displaystyle == dfrac. * Use rcParams instead of rcParamsDefault in the test. The test changes the DPI of the figure, but compares images after saving them, so rcParams['savefig.dpi'] is more appropriate.
1 parent 00cdf28 commit 4acd3be

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

lib/matplotlib/_mathtext.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,10 +1634,10 @@ class Parser:
16341634
"""
16351635

16361636
class _MathStyle(enum.Enum):
1637-
DISPLAYSTYLE = enum.auto()
1638-
TEXTSTYLE = enum.auto()
1639-
SCRIPTSTYLE = enum.auto()
1640-
SCRIPTSCRIPTSTYLE = enum.auto()
1637+
DISPLAYSTYLE = 0
1638+
TEXTSTYLE = 1
1639+
SCRIPTSTYLE = 2
1640+
SCRIPTSCRIPTSTYLE = 3
16411641

16421642
_binary_operators = set(
16431643
'+ * - \N{MINUS SIGN}'
@@ -1725,6 +1725,9 @@ def set_names_and_parse_actions():
17251725
p.float_literal = Regex(r"[-+]?([0-9]+\.?[0-9]*|\.[0-9]+)")
17261726
p.space = oneOf(self._space_widths)("space")
17271727

1728+
p.style_literal = oneOf(
1729+
[str(e.value) for e in self._MathStyle])("style_literal")
1730+
17281731
p.single_symbol = Regex(
17291732
r"([a-zA-Z0-9 +\-*/<>=:,.;!\?&'@()\[\]|%s])|(\\[%%${}\[\]_|])" %
17301733
"\U00000080-\U0001ffff" # unicode range
@@ -1799,7 +1802,7 @@ def set_names_and_parse_actions():
17991802
"{" + Optional(p.delim)("ldelim") + "}"
18001803
+ "{" + Optional(p.delim)("rdelim") + "}"
18011804
+ "{" + p.float_literal("rulesize") + "}"
1802-
+ p.optional_group("style")
1805+
+ "{" + Optional(p.style_literal)("style") + "}"
18031806
+ p.required_group("num")
18041807
+ p.required_group("den"))
18051808

@@ -2324,7 +2327,7 @@ def _genfrac(self, ldelim, rdelim, rule, style, num, den):
23242327
state = self.get_state()
23252328
thickness = state.get_current_underline_thickness()
23262329

2327-
if style is not self._MathStyle.DISPLAYSTYLE:
2330+
for _ in range(style.value):
23282331
num.shrink()
23292332
den.shrink()
23302333
cnum = HCentered([num])
@@ -2358,10 +2361,13 @@ def _genfrac(self, ldelim, rdelim, rule, style, num, den):
23582361
return self._auto_sized_delimiter(ldelim, result, rdelim)
23592362
return result
23602363

2364+
def style_literal(self, s, loc, toks):
2365+
return self._MathStyle(int(toks["style_literal"]))
2366+
23612367
def genfrac(self, s, loc, toks):
23622368
return self._genfrac(
23632369
toks.get("ldelim", ""), toks.get("rdelim", ""),
2364-
toks["rulesize"], toks["style"],
2370+
toks["rulesize"], toks.get("style", self._MathStyle.TEXTSTYLE),
23652371
toks["num"], toks["den"])
23662372

23672373
def frac(self, s, loc, toks):

lib/matplotlib/tests/test_mathtext.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,16 @@ def test_inverted_delimiters(fig_test, fig_ref):
367367
fig_ref.text(.5, .5, r"$)($", math_fontfamily="dejavusans")
368368

369369

370+
@check_figures_equal(extensions=["png"])
371+
def test_genfrac_displaystyle(fig_test, fig_ref):
372+
fig_test.text(0.1, 0.1, r"$\dfrac{2x}{3y}$")
373+
374+
thickness = _mathtext.TruetypeFonts.get_underline_thickness(
375+
None, None, fontsize=mpl.rcParams["font.size"],
376+
dpi=mpl.rcParams["savefig.dpi"])
377+
fig_ref.text(0.1, 0.1, r"$\genfrac{}{}{%f}{0}{2x}{3y}$" % thickness)
378+
379+
370380
def test_mathtext_fallback_valid():
371381
for fallback in ['cm', 'stix', 'stixsans', 'None']:
372382
mpl.rcParams['mathtext.fallback'] = fallback

0 commit comments

Comments
 (0)