Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b1723b6

Browse files
committed
Try to mimic LaTeX's fraction alignment
1 parent f1072e5 commit b1723b6

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

lib/matplotlib/_mathtext.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2751,6 +2751,9 @@ def subsuper(self, s, loc, toks):
27512751

27522752
def _genfrac(self, ldelim, rdelim, rule, style, num, den):
27532753
state = self.get_state()
2754+
cap_height = state.font_output.get_metrics(
2755+
state.font, mpl.rcParams['mathtext.default'],
2756+
'H', state.fontsize, state.dpi).height
27542757
thickness = state.font_output.get_underline_thickness(
27552758
state.font, state.fontsize, state.dpi)
27562759

@@ -2759,17 +2762,28 @@ def _genfrac(self, ldelim, rdelim, rule, style, num, den):
27592762
if style is not self._MathStyle.DISPLAYSTYLE:
27602763
num.shrink()
27612764
den.shrink()
2765+
min_clearance = 0.5 * thickness
2766+
else:
2767+
min_clearance = 1.5 * thickness
27622768
cnum = HCentered([num])
27632769
cden = HCentered([den])
27642770
width = max(num.width, den.width)
27652771
cnum.hpack(width, 'exactly')
27662772
cden.hpack(width, 'exactly')
2767-
vlist = Vlist([cnum, # numerator
2768-
Vbox(0, thickness * 2.0), # space
2769-
Hrule(state, rule), # rule
2770-
Vbox(0, thickness * 2.0), # space
2771-
cden # denominator
2772-
])
2773+
2774+
vlist_builder = []
2775+
if cnum.height < cap_height:
2776+
cnum.height = cap_height
2777+
vlist_builder.append(cnum)
2778+
numerator_padding = max(
2779+
cap_height * 3/5 - cnum.depth, min_clearance) - thickness
2780+
vlist_builder.append(Vbox(0, numerator_padding))
2781+
vlist_builder.append(Hrule(state, rule))
2782+
denominator_padding = max(
2783+
4/3 * cap_height - cden.height, min_clearance) + thickness
2784+
vlist_builder.append(Vbox(0, denominator_padding))
2785+
vlist_builder.append(cden)
2786+
vlist = Vlist(vlist_builder)
27732787

27742788
# Shift so the fraction line sits in the middle of the
27752789
# minus sign
@@ -2780,9 +2794,8 @@ def _genfrac(self, ldelim, rdelim, rule, style, num, den):
27802794
metrics = state.font_output.get_metrics(
27812795
state.font, mpl.rcParams['mathtext.default'],
27822796
minus_sign, state.fontsize, state.dpi)
2783-
shift = (cden.height -
2784-
((metrics.ymax + metrics.ymin) / 2 -
2785-
thickness * 1.5))
2797+
shift = (denominator_padding + cden.height -
2798+
(metrics.ymax + metrics.ymin) / 2 - thickness / 2)
27862799
vlist.shift_amount = shift
27872800

27882801
result = [Hlist([vlist, Hbox(thickness * 2.)])]

0 commit comments

Comments
 (0)