From ab331b0fb817854a88970af94025aa7cab604e11 Mon Sep 17 00:00:00 2001 From: tfpf Date: Wed, 11 May 2022 17:34:53 +0530 Subject: [PATCH 1/7] Correctly read the 'style' argument while processing 'genfrac'. --- lib/matplotlib/_mathtext.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/_mathtext.py b/lib/matplotlib/_mathtext.py index 8869b00769e5..1c0cbef47d0e 100644 --- a/lib/matplotlib/_mathtext.py +++ b/lib/matplotlib/_mathtext.py @@ -1723,6 +1723,7 @@ def set_names_and_parse_actions(): # Root definitions. p.float_literal = Regex(r"[-+]?([0-9]+\.?[0-9]*|\.[0-9]+)") + p.style_literal = oneOf([str(e.value) for e in self._MathStyle]) p.space = oneOf(self._space_widths)("space") p.single_symbol = Regex( @@ -1799,7 +1800,7 @@ def set_names_and_parse_actions(): "{" + Optional(p.delim)("ldelim") + "}" + "{" + Optional(p.delim)("rdelim") + "}" + "{" + p.float_literal("rulesize") + "}" - + p.optional_group("style") + + "{" + p.style_literal("style") + "}" + p.required_group("num") + p.required_group("den")) @@ -1935,6 +1936,7 @@ def non_math(self, s, loc, toks): return [hlist] float_literal = staticmethod(pyparsing_common.convertToFloat) + style_literal = staticmethod(pyparsing_common.convertToInteger) def _make_space(self, percentage): # All spaces are relative to em width @@ -2361,7 +2363,7 @@ def _genfrac(self, ldelim, rdelim, rule, style, num, den): def genfrac(self, s, loc, toks): return self._genfrac( toks.get("ldelim", ""), toks.get("rdelim", ""), - toks["rulesize"], toks["style"], + toks["rulesize"], self._MathStyle(toks["style"]), toks["num"], toks["den"]) def frac(self, s, loc, toks): From 825aa6cfd8d7bb4e277690231ac9db73db288098 Mon Sep 17 00:00:00 2001 From: tfpf Date: Wed, 11 May 2022 19:16:27 +0530 Subject: [PATCH 2/7] Default to text style if style not specified. --- lib/matplotlib/_mathtext.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/_mathtext.py b/lib/matplotlib/_mathtext.py index 1c0cbef47d0e..4702cb6500c6 100644 --- a/lib/matplotlib/_mathtext.py +++ b/lib/matplotlib/_mathtext.py @@ -1800,7 +1800,7 @@ def set_names_and_parse_actions(): "{" + Optional(p.delim)("ldelim") + "}" + "{" + Optional(p.delim)("rdelim") + "}" + "{" + p.float_literal("rulesize") + "}" - + "{" + p.style_literal("style") + "}" + + "{" + Optional(p.style_literal)("style") + "}" + p.required_group("num") + p.required_group("den")) @@ -2363,7 +2363,8 @@ def _genfrac(self, ldelim, rdelim, rule, style, num, den): def genfrac(self, s, loc, toks): return self._genfrac( toks.get("ldelim", ""), toks.get("rdelim", ""), - toks["rulesize"], self._MathStyle(toks["style"]), + toks["rulesize"], self._MathStyle( + toks.get("style", self._MathStyle.TEXTSTYLE.value)), toks["num"], toks["den"]) def frac(self, s, loc, toks): From 96f7f9adfff0665f2d58f4c2a0e18cc138469c0e Mon Sep 17 00:00:00 2001 From: tfpf Date: Thu, 12 May 2022 13:37:23 +0530 Subject: [PATCH 3/7] Hard-coded enumerations. Support script_stype and script_script_style. --- lib/matplotlib/_mathtext.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/_mathtext.py b/lib/matplotlib/_mathtext.py index 4702cb6500c6..31f1dbf6731c 100644 --- a/lib/matplotlib/_mathtext.py +++ b/lib/matplotlib/_mathtext.py @@ -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}' @@ -2326,7 +2326,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]) From f13788b74e0383ce4b77e5bac7d4c7c04ec9eec8 Mon Sep 17 00:00:00 2001 From: tfpf Date: Thu, 12 May 2022 18:24:17 +0530 Subject: [PATCH 4/7] Automatically convert style number to enumerated type. --- lib/matplotlib/_mathtext.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/_mathtext.py b/lib/matplotlib/_mathtext.py index 31f1dbf6731c..d290d0455fde 100644 --- a/lib/matplotlib/_mathtext.py +++ b/lib/matplotlib/_mathtext.py @@ -1723,9 +1723,11 @@ def set_names_and_parse_actions(): # Root definitions. p.float_literal = Regex(r"[-+]?([0-9]+\.?[0-9]*|\.[0-9]+)") - p.style_literal = oneOf([str(e.value) for e in self._MathStyle]) 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 @@ -1936,7 +1938,6 @@ def non_math(self, s, loc, toks): return [hlist] float_literal = staticmethod(pyparsing_common.convertToFloat) - style_literal = staticmethod(pyparsing_common.convertToInteger) def _make_space(self, percentage): # All spaces are relative to em width @@ -2360,11 +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"], self._MathStyle( - toks.get("style", self._MathStyle.TEXTSTYLE.value)), + toks["rulesize"], toks.get("style", self._MathStyle.TEXTSTYLE), toks["num"], toks["den"]) def frac(self, s, loc, toks): From 0ad1392365d30c2fe6ae82fe22690cf7ba0d9342 Mon Sep 17 00:00:00 2001 From: tfpf Date: Thu, 12 May 2022 18:26:26 +0530 Subject: [PATCH 5/7] Used double quotes for strings. --- lib/matplotlib/_mathtext.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/_mathtext.py b/lib/matplotlib/_mathtext.py index d290d0455fde..27c4836a2673 100644 --- a/lib/matplotlib/_mathtext.py +++ b/lib/matplotlib/_mathtext.py @@ -1726,7 +1726,7 @@ def set_names_and_parse_actions(): p.space = oneOf(self._space_widths)("space") p.style_literal = oneOf( - [str(e.value) for e in self._MathStyle])('style_literal') + [str(e.value) for e in self._MathStyle])("style_literal") p.single_symbol = Regex( r"([a-zA-Z0-9 +\-*/<>=:,.;!\?&'@()\[\]|%s])|(\\[%%${}\[\]_|])" % From 2fccd623a09f15d9c5c9ba74995ab23abcecf26f Mon Sep 17 00:00:00 2001 From: tfpf Date: Sat, 21 May 2022 20:43:49 +0530 Subject: [PATCH 6/7] Verify that genfrac's displaystyle == dfrac. --- lib/matplotlib/tests/test_mathtext.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/matplotlib/tests/test_mathtext.py b/lib/matplotlib/tests/test_mathtext.py index 1bb1131cb88a..b533cffa539e 100644 --- a/lib/matplotlib/tests/test_mathtext.py +++ b/lib/matplotlib/tests/test_mathtext.py @@ -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=12, dpi=mpl.rcParamsDefault['figure.dpi']) + fig_ref.text( + 0.1, 0.1, fr"$\genfrac{{}}{{}}{{{thickness}}}{{0}}{{2x}}{{3y}}$") + + def test_mathtext_fallback_valid(): for fallback in ['cm', 'stix', 'stixsans', 'None']: mpl.rcParams['mathtext.fallback'] = fallback From ab37e847c9c4f35f30cc5134217b26b02ac658b9 Mon Sep 17 00:00:00 2001 From: tfpf Date: Sun, 22 May 2022 10:25:45 +0530 Subject: [PATCH 7/7] 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. --- lib/matplotlib/tests/test_mathtext.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/tests/test_mathtext.py b/lib/matplotlib/tests/test_mathtext.py index b533cffa539e..d6e16d247f1b 100644 --- a/lib/matplotlib/tests/test_mathtext.py +++ b/lib/matplotlib/tests/test_mathtext.py @@ -372,9 +372,9 @@ 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=12, dpi=mpl.rcParamsDefault['figure.dpi']) - fig_ref.text( - 0.1, 0.1, fr"$\genfrac{{}}{{}}{{{thickness}}}{{0}}{{2x}}{{3y}}$") + 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():