Skip to content

Correctly read the 'style' argument while processing 'genfrac'. #23034

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 7 commits into from
Jun 16, 2022
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
20 changes: 13 additions & 7 deletions lib/matplotlib/_mathtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -1634,10 +1634,10 @@ class Parser:
"""

class _MathStyle(enum.Enum):
DISPLAYSTYLE = enum.auto()
TEXTSTYLE = enum.auto()
SCRIPTSTYLE = enum.auto()
SCRIPTSCRIPTSTYLE = enum.auto()
DISPLAYSTYLE = 0
TEXTSTYLE = 1
SCRIPTSTYLE = 2
SCRIPTSCRIPTSTYLE = 3

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

p.style_literal = oneOf(
[str(e.value) for e in self._MathStyle])("style_literal")

p.single_symbol = Regex(
r"([a-zA-Z0-9 +\-*/<>=:,.;!\?&'@()\[\]|%s])|(\\[%%${}\[\]_|])" %
"\U00000080-\U0001ffff" # unicode range
Expand Down Expand Up @@ -1799,7 +1802,7 @@ def set_names_and_parse_actions():
"{" + Optional(p.delim)("ldelim") + "}"
+ "{" + Optional(p.delim)("rdelim") + "}"
+ "{" + p.float_literal("rulesize") + "}"
+ p.optional_group("style")
+ "{" + Optional(p.style_literal)("style") + "}"
+ p.required_group("num")
+ p.required_group("den"))

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

if style is not self._MathStyle.DISPLAYSTYLE:
for _ in range(style.value):
num.shrink()
den.shrink()
cnum = HCentered([num])
Expand Down Expand Up @@ -2358,10 +2361,13 @@ def _genfrac(self, ldelim, rdelim, rule, style, num, den):
return self._auto_sized_delimiter(ldelim, result, rdelim)
return result

def style_literal(self, s, loc, toks):
return self._MathStyle(int(toks["style_literal"]))

def genfrac(self, s, loc, toks):
return self._genfrac(
toks.get("ldelim", ""), toks.get("rdelim", ""),
toks["rulesize"], toks["style"],
toks["rulesize"], toks.get("style", self._MathStyle.TEXTSTYLE),
toks["num"], toks["den"])

def frac(self, s, loc, toks):
Expand Down
10 changes: 10 additions & 0 deletions lib/matplotlib/tests/test_mathtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,16 @@ def test_inverted_delimiters(fig_test, fig_ref):
fig_ref.text(.5, .5, r"$)($", math_fontfamily="dejavusans")


@check_figures_equal(extensions=["png"])
def test_genfrac_displaystyle(fig_test, fig_ref):
fig_test.text(0.1, 0.1, r"$\dfrac{2x}{3y}$")

thickness = _mathtext.TruetypeFonts.get_underline_thickness(
None, None, fontsize=mpl.rcParams["font.size"],
dpi=mpl.rcParams["savefig.dpi"])
fig_ref.text(0.1, 0.1, r"$\genfrac{}{}{%f}{0}{2x}{3y}$" % thickness)


def test_mathtext_fallback_valid():
for fallback in ['cm', 'stix', 'stixsans', 'None']:
mpl.rcParams['mathtext.fallback'] = fallback
Expand Down