Repository: matplotlib/matplotlib
File: lib/matplotlib/_mathtext.py (Python)

Code before:
            num.shrink()
            den.shrink()
        cnum = HCentered([num])
        cden = HCentered([den])
        width = max(num.width, den.width)
        cnum.hpack(width, 'exactly')
        cden.hpack(width, 'exactly')

        # Align the fraction with a fraction line (node743, node744 and node746).
        if rule:
            if style is self._MathStyle.DISPLAYSTYLE:
                num_shift_up = consts.num1 * x_height
                den_shift_down = consts.denom1 * x_height
                min_clr = 3 * rule
            else:
                num_shift_up = consts.num2 * x_height
                den_shift_down = consts.denom2 * x_height
                min_clr = rule
            delta = rule / 2
            num_clr = max(num_shift_up - cnum.depth - axis_height - delta, min_clr)
            den_clr = max(axis_height - delta + den_shift_down - cden.height, min_clr)
            # Possible bug in fraction rendering. See GitHub PR 22852 comments.
            vlist = Vlist([cnum,                     # numerator
                           Vbox(0, num_clr - rule),  # space
                           Hrule(state, rule),       # rule
                           Vbox(0, den_clr + rule),  # space
                           cden                      # denominator
                           ])
            vlist.shift_amount = cden.height + den_clr + delta - axis_height

        # Align the fraction without a fraction line (node743, node744 and node745).
        else:
            if style is self._MathStyle.DISPLAYSTYLE:
                num_shift_up = consts.num1 * x_height
                den_shift_down = consts.denom1 * x_height
                min_clr = 7 * thickness
            else:
                num_shift_up = consts.num3 * x_height
                den_shift_down = consts.denom2 * x_height
                min_clr = 3 * thickness
            def_clr = num_shift_up - cnum.depth + den_shift_down - cden.height
            clr = max(def_clr, min_clr)
            vlist = Vlist([cnum,          # numerator
                           Vbox(0, clr),  # space
                           cden           # denominator
                           ])
            vlist.shift_amount = den_shift_down
            if def_clr < min_clr:
                vlist.shift_amount += (min_clr - def_clr) / 2

        result: list[Box | Char | str] = [Hlist([

Review comment (suggestion):
This extra +/- `rule` is odd compared to TeX, but it is explained in the original PR: https://github.com/matplotlib/matplotlib/pull/22852#issuecomment-1100007576

I did not investigate that further as it seems to be working fine for now.

Code after:
            if sub is None:
                x.shift_amount = -shift_up
            else:
                # Align subscript with superscript (node759).
                y = Hlist([Kern(subkern), sub])
                y.shrink()
                if self.is_dropsub(last_char):
                    shift_down = lc_baseline + consts.subdrop * x_height
                else:
                    shift_down = max(shift_down, consts.sub2 * x_height)
                # If the subscript and superscript are too close to each other,
                # move the subscript down.
                clr = (4 * rule_thickness -
                       ((shift_up - x.depth) - (y.height - shift_down)))
                if clr > 0.:
                    shift_down += clr
                    clr = x_height * 4 / 5 - shift_up + x.depth
                    if clr > 0:
                        shift_up += clr
                        shift_down -= clr
                x = Vlist([
                    x,
                    Kern((shift_up - x.depth) - (y.height - shift_down)),
                    y])
                x.shift_amount = shift_down

        if not self.is_dropsub(last_char):
            x.width += consts.script_space * x_height

        # Do we need to add a space after the nucleus?
        # To find out, check the flag set by operatorname
        optional_spacing = ([self._make_space(self._space_widths[r'\,'])]
                            if self._needs_space_after_subsuper else [])
        self._needs_space_after_subsuper = False
        result = Hlist([nucleus, x, *optional_spacing])
        return [result]

    def _genfrac(self, ldelim: str, rdelim: str, rule: float | None, style: _MathStyle,
                 num: Hlist, den: Hlist) -> T.Any:
        state = self.get_state()
        thickness = state.get_current_underline_thickness()

        axis_height = state.fontset.get_axis_height(
            state.font, state.fontsize, state.dpi)
        consts = _get_font_constant_set(state)
        x_height = state.fontset.get_xheight(state.font, state.fontsize, state.dpi)

        for _ in range(style.value):
            x_height *= SHRINK_FACTOR
            num.shrink()
            den.shrink()