8000 Special-case usetex minus to zero depth. · matplotlib/matplotlib@4c6d9af · GitHub
[go: up one dir, main page]

Skip to content

Commit 4c6d9af

Browse 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

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lib/matplotlib/dviread.py

Lines 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

lib/matplotlib/tests/test_usetex.py

Lines 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