Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4c6d9af

Browse filesBrowse files
committed
Special-case usetex minus to zero depth.
This avoids vertical misalignment between e.g. "$1$" and "$-1$": ``` for x in [-1, 1]: figtext(.5, .5, f"${x}$", usetex=True, ha="right") ```
1 parent 18ed621 commit 4c6d9af
Copy full SHA for 4c6d9af

File tree

Expand file treeCollapse file tree

2 files changed

+27
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+27
-1
lines changed
Open diff view settings
Collapse file

‎lib/matplotlib/dviread.py‎

Copy file name to clipboardExpand all lines: lib/matplotlib/dviread.py
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,12 @@ def _height_depth_of(self, char):
566566
result.append(0)
567567
else:
568568
result.append(_mul2012(value, self._scale))
569+
# cmsy10 glyph 0 ("minus") has a nonzero descent so that TeX aligns
570+
# equations properly (https://tex.stackexchange.com/questions/526103/),
571+
# but we actually care about the rasterization depth to align the
572+
# dvipng-generated images.
573+
if self.texname == b"cmsy10" and char == 0:
574+
result[-1] = 0
569575
return result
570576

571577

Collapse file

‎lib/matplotlib/tests/test_usetex.py‎

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_usetex.py
+21-1Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import pytest
21
import platform
32

3+
import numpy as np
4+
import pytest
5+
46
import matplotlib as mpl
57
from matplotlib.testing.decorators import check_figures_equal, image_comparison
68
import matplotlib.pyplot as plt
@@ -43,3 +45,21 @@ def test_mathdefault():
4345
# problems when later switching usetex on.
4446
mpl.rcParams['text.usetex'] = True
4547
fig.canvas.draw()
48+
49+
50+
def test_minus_no_descent():
51+
# Test special-casing of minus descent in DviFont._height_depth_of, by
52+
# checking that overdrawing a 1 and a -1 results in an overall height
53+
# equivalent to drawing either of them separately.
54+
mpl.style.use("mpl20")
55+
heights = {}
56+
fig = plt.figure()
57+
for vals in [(1,), (-1,), (-1, 1)]:
58+
fig.clf()
59+
for x in vals:
60+
fig.text(.5, .5, f"${x}$", usetex=True)
61+
fig.canvas.draw()
62+
# The following counts the number of non-fully-blank pixel rows.
63+
heights[vals] = ((np.array(fig.canvas.buffer_rgba())[..., 0] != 255)
64+
.any(axis=1).sum())
65+
assert len({*heights.values()}) == 1

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.