Skip to content

[ENH]: Use \genfrac in display style? #23008

Closed

Description

@tfpf

Problem

Is it possible to render fractions in display style using \genfrac?

The style argument of the _genfrac function in _mathtext.py is received as an Hlist. As a result, fractions will never use display style (since style and self._MathStyle.DISPLAYSTYLE will always compare unequal).

import matplotlib.pyplot as plt

fig = plt.figure()
fig.text(.2, .5, r'$\genfrac{}{}{0}{0}{1}{2}$')
fig.text(.3, .5, r'$\genfrac{}{}{1}{1}{1}{2}$')
plt.show()

Proposed solution

Change

        p.genfrac <<= r"\genfrac" - (
            "{" + Optional(p.ambi_delim | p.left_delim)("ldelim") + "}"
            + "{" + Optional(p.ambi_delim | p.right_delim)("rdelim") + "}"
            + "{" + p.float_literal("rulesize") + "}"
            + p.simple_group("style")                    # <- change this line
            + p.required_group("num")
            + p.required_group("den")
            | Error("Expected "
                    r"\genfrac{ldelim}{rdelim}{rulesize}{style}{num}{den}"))

to

        p.genfrac <<= r"\genfrac" - (
            "{" + Optional(p.ambi_delim | p.left_delim)("ldelim") + "}"
            + "{" + Optional(p.ambi_delim | p.right_delim)("rdelim") + "}"
            + "{" + p.float_literal("rulesize") + "}"
            + "{" + p.int_literal("style") + "}"                    # <- changed this line
            + p.required_group("num")
            + p.required_group("den")
            | Error("Expected "
                    r"\genfrac{ldelim}{rdelim}{rulesize}{style}{num}{den}"))

with p.int_literal defined using a regex (just like how p.float_literal is defined), following which, the _genfrac function should obtain the enumeration variable from the value. We can hard-code the enumerations

display_style = 0
text_style = 2
script_style = 4
script_script_style = 6

since those are the values specified in TeX: The Program.

I could try to create a PR for this, but my knowledge of language parsing is very limited.

Activity

  1. anntzer commented on May 8, 2022

    @anntzer
    Contributor

    Seems reasonable to me, but the numeric values I see in the documentation for genfrac (e.g. in "User's guide for the amsmath package (version 2.1)) are 0/1/2/3, not 0/2/4/6?
    (Also, if using scriptstyle or scriptscriptstyle, I guess the _genfrac definition needs to shrink() the nodes once or twice more.)

  2. oscargus commented on May 8, 2022

    @oscargus
    Member

    How should the line height behave as consequence of this? #22171

  3. tfpf commented on May 8, 2022

    @tfpf
    ContributorAuthor

    That's fair; using 0, 1, 2 and 3 sounds good. (Worth noting that those were the numbers originally used in #8151.)

    How should the line height behave as consequence of this?

    Correct me if I'm wrong, but will that really affect anything? That is for rule argument, while this for the style argument. (A rebase may be required once that PR is merged, but … other than that?)

  4. oscargus commented on May 8, 2022

    @oscargus
    Member

    Correct me if I'm wrong

    Ah, I thought this way about "automatic" rescaling of the text. My bad.

  5. added this to the v3.6.0 milestone on Jun 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions