Skip to content

Commit e437df8

Browse files
committed
Make mathtext's fraction alignment resemble LaTeX's
1 parent 5f0e6df commit e437df8

File tree

302 files changed

+21007
-19078
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

302 files changed

+21007
-19078
lines changed

lib/matplotlib/_mathtext.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2758,6 +2758,9 @@ def subsuper(self, s, loc, toks):
27582758

27592759
def _genfrac(self, ldelim, rdelim, rule, style, num, den):
27602760
state = self.get_state()
2761+
cap_height = state.font_output.get_metrics(
2762+
state.font, mpl.rcParams['mathtext.default'],
2763+
'H', state.fontsize, state.dpi).height
27612764
thickness = state.font_output.get_underline_thickness(
27622765
state.font, state.fontsize, state.dpi)
27632766

@@ -2766,26 +2769,38 @@ def _genfrac(self, ldelim, rdelim, rule, style, num, den):
27662769
if style is not self._MathStyle.DISPLAYSTYLE:
27672770
num.shrink()
27682771
den.shrink()
2772+
min_clearance = np.ceil(0.5 * thickness)
2773+
cap_height *= 0.7
2774+
else:
2775+
min_clearance = 1.5 * thickness
27692776
cnum = HCentered([num])
27702777
cden = HCentered([den])
27712778
width = max(num.width, den.width)
27722779
cnum.hpack(width, 'exactly')
27732780
cden.hpack(width, 'exactly')
2774-
vlist = Vlist([cnum, # numerator
2775-
Vbox(0, thickness * 2.0), # space
2776-
Hrule(state, rule), # rule
2777-
Vbox(0, thickness * 2.0), # space
2778-
cden # denominator
2779-
])
2781+
2782+
numerator_padding = max(
2783+
3/5 * cap_height - cnum.depth, min_clearance) - thickness
2784+
denominator_padding = max(
2785+
4/3 * cap_height - cden.height, min_clearance) + thickness
2786+
vlist_builder = []
2787+
if cnum.height < cap_height:
2788+
vlist_builder.append(Vbox(0, cap_height - cnum.height))
2789+
vlist_builder += [cnum, # numerator
2790+
Vbox(0, numerator_padding), # space
2791+
Hrule(state, rule), # rule
2792+
Vbox(0, denominator_padding), # space
2793+
cden # denominator
2794+
]
2795+
vlist = Vlist(vlist_builder)
27802796

27812797
# Shift so the fraction line sits in the middle of the
2782-
# equals sign
2798+
# minus sign
27832799
metrics = state.font_output.get_metrics(
27842800
state.font, mpl.rcParams['mathtext.default'],
2785-
'=', state.fontsize, state.dpi)
2786-
shift = (cden.height -
2787-
((metrics.ymax + metrics.ymin) / 2 -
2788-
thickness * 3.0))
2801+
'\u2212', state.fontsize, state.dpi)
2802+
shift = (denominator_padding + cden.height -
2803+
(metrics.ymax + metrics.ymin) / 2 - rule / 2)
27892804
vlist.shift_amount = shift
27902805

27912806
result = [Hlist([vlist, Hbox(thickness * 2.)])]
Binary file not shown.
90 Bytes
Loading

0 commit comments

Comments
 (0)