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

Skip to content

Commit e31d8ed

Browse files
lganictimhoffm
andauthored
Fixed bug: mathtext rendered width not being calculated correctly (#25692)
* Text now accounts for mathtext width when wrapping * Added test case for mathtext wrapping * Update lib/matplotlib/text.py Co-authored-by: Tim Hoffmann <[email protected]> * removed unnecessary variable --------- Co-authored-by: Tim Hoffmann <[email protected]>
1 parent cf9a887 commit e31d8ed

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

lib/matplotlib/tests/test_text.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,15 @@ def test_wrap():
701701
'times.')
702702

703703

704+
def test_mathwrap():
705+
fig = plt.figure(figsize=(6, 4))
706+
s = r'This is a very $\overline{\mathrm{long}}$ line of Mathtext.'
707+
text = fig.text(0, 0.5, s, size=40, wrap=True)
708+
fig.canvas.draw()
709+
assert text._get_wrapped_text() == ('This is a very $\\overline{\\mathrm{long}}$\n'
710+
'line of Mathtext.')
711+
712+
704713
def test_get_window_extent_wrapped():
705714
# Test that a long title that wraps to two lines has the same vertical
706715
# extent as an explicit two line title.

lib/matplotlib/text.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,10 +642,11 @@ def _get_rendered_text_width(self, text):
642642
"""
643643
Return the width of a given text string, in pixels.
644644
"""
645+
645646
w, h, d = self._renderer.get_text_width_height_descent(
646647
text,
647648
self.get_fontproperties(),
648-
False)
649+
cbook.is_math_text(text))
649650
return math.ceil(w)
650651

651652
def _get_wrapped_text(self):

0 commit comments

Comments
 (0)