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

Code before:
                    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()

        # The fraction line (if present) must be aligned with the minus sign. Therefore,
        # the height of the latter from the baseline is the axis height.
        metrics = state.fontset.get_metrics(
            state.font, mpl.rcParams['mathtext.default'],
            '\u2212', state.fontsize, state.dpi)
        axis_height = (metrics.ymax + metrics.ymin) / 2
        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()
        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)

Review comment (suggestion):
axis_height could (like x_height) be read from the tfm file (as params(22) for computer modern, but I guess either way is fine (essentially, this is the same comment as regarding x_height).  Again, if we diverge from "TeX the program", a comment would be nice.

Code after:
                state.font, state.fontsize * SHRINK_FACTOR, state.dpi)
            shift_up = nucleus.height - consts.supdrop * shrunk_x_height
            shift_down = nucleus.depth + consts.subdrop * shrunk_x_height

        x: List
        if super is None:
            # Align subscript without superscript (node757).
            # Note: One of super or sub must be a Node if we're in this function, but
            # mypy can't know this, since it can't interpret pyparsing expressions,
            # hence the cast.
            x = Hlist([Kern(subkern), T.cast(Node, sub)])
            x.shrink()
            if self.is_dropsub(last_char):
                shift_down = lc_baseline + consts.subdrop * x_height
            else:
                shift_down = max(shift_down, consts.sub1 * x_height,
                                 x.height - x_height * 4 / 5)
            x.shift_amount = shift_down
        else:
            # Align superscript (node758).
            x = Hlist([Kern(superkern), super])
            x.shrink()
            if self.is_dropsub(last_char):
                shift_up = lc_height - consts.subdrop * x_height
            else:
                shift_up = max(shift_up, consts.sup1 * x_height, x.depth + x_height / 4)
            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