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

Skip to content

Add overset/underset whatsnew entry #19497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions doc/users/next_whats_new/mathtext.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
``matplotlib.mathtext`` now supports *overset* and *underset* LaTeX symbols
---------------------------------------------------------------------------

`.mathtext` now supports *overset* and *underset*, called as
``\overset{annotation}{body}`` or ``\underset{annotation}{body}``, where
*annotation* is the text "above" or "below" the *body*.

.. plot::

math_expr = r"$ x \overset{f}{\rightarrow} y \underset{f}{\leftarrow} z $"
plt.text(0.4, 0.5, math_expr, usetex=False)
34 changes: 17 additions & 17 deletions lib/matplotlib/_mathtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -2859,32 +2859,32 @@ def binom(self, s, loc, toks):
return self._genfrac('(', ')', 0.0, self._MathStyle.TEXTSTYLE,
num, den)

def _genset(self, state, body, annotation, overunder):
def _genset(self, state, annotation, body, overunder):
thickness = state.font_output.get_underline_thickness(
state.font, state.fontsize, state.dpi)

body.shrink()
annotation.shrink()

cbody = HCentered([body])
cannotation = HCentered([annotation])
width = max(cbody.width, cannotation.width)
cbody.hpack(width, 'exactly')
cbody = HCentered([body])
width = max(cannotation.width, cbody.width)
cannotation.hpack(width, 'exactly')
cbody.hpack(width, 'exactly')

vgap = thickness * 3
if overunder == "under":
vlist = Vlist([cannotation, # annotation
Vbox(0, vgap), # space
cbody # body
vlist = Vlist([cbody, # body
Vbox(0, vgap), # space
cannotation # annotation
])
# Shift so the annotation sits in the same vertical position
shift_amount = cannotation.depth + cbody.height + vgap
# Shift so the body sits in the same vertical position
shift_amount = cbody.depth + cannotation.height + vgap

vlist.shift_amount = shift_amount
else:
vlist = Vlist([cbody, # body
Vbox(0, vgap), # space
cannotation # annotation
vlist = Vlist([cannotation, # annotation
Vbox(0, vgap), # space
cbody # body
])

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

state = self.get_state()
body, annotation = toks[0]
annotation, body = toks[0]

return self._genset(state, body, annotation, overunder="over")
return self._genset(state, annotation, body, overunder="over")

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

state = self.get_state()
body, annotation = toks[0]
annotation, body = toks[0]

return self._genset(state, body, annotation, overunder="under")
return self._genset(state, annotation, body, overunder="under")

def _auto_sized_delimiter(self, front, middle, back):
state = self.get_state()
Expand Down