Skip to content

Commit 92b4bc3

Browse files
authored
Merge pull request #19497 from aitikgupta/overset-rst
Add overset/underset whatsnew entry
2 parents 56d9e7c + c82cadd commit 92b4bc3

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

doc/users/next_whats_new/mathtext.rst

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
``matplotlib.mathtext`` now supports *overset* and *underset* LaTeX symbols
2+
---------------------------------------------------------------------------
3+
4+
`.mathtext` now supports *overset* and *underset*, called as
5+
``\overset{annotation}{body}`` or ``\underset{annotation}{body}``, where
6+
*annotation* is the text "above" or "below" the *body*.
7+
8+
.. plot::
9+
10+
math_expr = r"$ x \overset{f}{\rightarrow} y \underset{f}{\leftarrow} z $"
11+
plt.text(0.4, 0.5, math_expr, usetex=False)

lib/matplotlib/_mathtext.py

+17-17
Original file line numberDiff line numberDiff line change
@@ -2859,32 +2859,32 @@ def binom(self, s, loc, toks):
28592859
return self._genfrac('(', ')', 0.0, self._MathStyle.TEXTSTYLE,
28602860
num, den)
28612861

2862-
def _genset(self, state, body, annotation, overunder):
2862+
def _genset(self, state, annotation, body, overunder):
28632863
thickness = state.font_output.get_underline_thickness(
28642864
state.font, state.fontsize, state.dpi)
28652865

2866-
body.shrink()
2866+
annotation.shrink()
28672867

2868-
cbody = HCentered([body])
28692868
cannotation = HCentered([annotation])
2870-
width = max(cbody.width, cannotation.width)
2871-
cbody.hpack(width, 'exactly')
2869+
cbody = HCentered([body])
2870+
width = max(cannotation.width, cbody.width)
28722871
cannotation.hpack(width, 'exactly')
2872+
cbody.hpack(width, 'exactly')
28732873

28742874
vgap = thickness * 3
28752875
if overunder == "under":
2876-
vlist = Vlist([cannotation, # annotation
2877-
Vbox(0, vgap), # space
2878-
cbody # body
2876+
vlist = Vlist([cbody, # body
2877+
Vbox(0, vgap), # space
2878+
cannotation # annotation
28792879
])
2880-
# Shift so the annotation sits in the same vertical position
2881-
shift_amount = cannotation.depth + cbody.height + vgap
2880+
# Shift so the body sits in the same vertical position
2881+
shift_amount = cbody.depth + cannotation.height + vgap
28822882

28832883
vlist.shift_amount = shift_amount
28842884
else:
2885-
vlist = Vlist([cbody, # body
2886-
Vbox(0, vgap), # space
2887-
cannotation # annotation
2885+
vlist = Vlist([cannotation, # annotation
2886+
Vbox(0, vgap), # space
2887+
cbody # body
28882888
])
28892889

28902890
# To add horizontal gap between symbols: wrap the Vlist into
@@ -2956,18 +2956,18 @@ def overset(self, s, loc, toks):
29562956
assert len(toks[0]) == 2
29572957

29582958
state = self.get_state()
2959-
body, annotation = toks[0]
2959+
annotation, body = toks[0]
29602960

2961-
return self._genset(state, body, annotation, overunder="over")
2961+
return self._genset(state, annotation, body, overunder="over")
29622962

29632963
def underset(self, s, loc, toks):
29642964
assert len(toks) == 1
29652965
assert len(toks[0]) == 2
29662966

29672967
state = self.get_state()
2968-
body, annotation = toks[0]
2968+
annotation, body = toks[0]
29692969

2970-
return self._genset(state, body, annotation, overunder="under")
2970+
return self._genset(state, annotation, body, overunder="under")
29712971

29722972
def _auto_sized_delimiter(self, front, middle, back):
29732973
state = self.get_state()

0 commit comments

Comments
 (0)