Skip to content
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
6 changes: 6 additions & 0 deletions lib/matplotlib/dviread.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,12 @@ def _height_depth_of(self, char):
result.append(0)
else:
result.append(_mul2012(value, self._scale))
# cmsy10 glyph 0 ("minus") has a nonzero descent so that TeX aligns
# equations properly (https://tex.stackexchange.com/questions/526103/),
# but we actually care about the rasterization depth to align the
# dvipng-generated images.
if self.texname == b"cmsy10" and char == 0:
result[-1] = 0
return result


Expand Down
22 changes: 21 additions & 1 deletion lib/matplotlib/tests/test_usetex.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import pytest
import platform

import numpy as np
import pytest

import matplotlib as mpl
from matplotlib.testing.decorators import check_figures_equal, image_comparison
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -43,3 +45,21 @@ def test_mathdefault():
# problems when later switching usetex on.
mpl.rcParams['text.usetex'] = True
fig.canvas.draw()


def test_minus_no_descent():
# Test special-casing of minus descent in DviFont._height_depth_of, by
# checking that overdrawing a 1 and a -1 results in an overall height
# equivalent to drawing either of them separately.
mpl.style.use("mpl20")
heights = {}
fig = plt.figure()
for vals in [(1,), (-1,), (-1, 1)]:
fig.clf()
for x in vals:
fig.text(.5, .5, f"${x}$", usetex=True)
fig.canvas.draw()
# The following counts the number of non-fully-blank pixel rows.
heights[vals] = ((np.array(fig.canvas.buffer_rgba())[..., 0] != 255)
.any(axis=1).sum())
assert len({*heights.values()}) == 1