Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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{body}{annotation}`` or ``\underset{body}{annotation}``, where

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, but now this is wrong again, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I blindly copied the suggestion, let me fix that

*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