8000 Merge pull request #20442 from anntzer/dvichemformula · matplotlib/matplotlib@3fc3b0d · GitHub
[go: up one dir, main page]

Skip to content

Commit 3fc3b0d

Browse files
authored
Merge pull request #20442 from anntzer/dvichemformula
Fix dvi baseline detector when `\usepackage{chemformula}` is used.
2 parents 509efd0 + 260aed6 commit 3fc3b0d

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

lib/matplotlib/dviread.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ def _read(self):
302302
# Pages appear to start with the sequence
303303
# bop (begin of page)
304304
# xxx comment
305+
# <push, ..., pop> # if using chemformula
305306
# down
306307
# push
307308
# down
@@ -313,17 +314,24 @@ def _read(self):
313314
# etc.
314315
# (dviasm is useful to explore this structure.)
315316
# Thus, we use the vertical position at the first time the stack depth
316-
# reaches 3, while at least three "downs" have been executed, as the
317+
# reaches 3, while at least three "downs" have been executed (excluding
318+
# those popped out (corresponding to the chemformula preamble)), as the
317319
# baseline (the "down" count is necessary to handle xcolor).
318-
downs = 0
320+
down_stack = [0]
319321
self._baseline_v = None
320322
while True:
321323
byte = self.file.read(1)[0]
322324
self._dtable[byte](self, byte)
323-
downs += self._dtable[byte].__name__ == "_down"
325+
name = self._dtable[byte].__name__
326+
if name == "_push":
327+
down_stack.append(down_stack[-1])
328+
elif name == "_pop":
329+
down_stack.pop()
330+
elif name == "_down":
331+
down_stack[-1] += 1
324332
if (self._baseline_v is None
325333
and len(getattr(self, "stack", [])) == 3
326-
and downs >= 4):
334+
and down_stack[-1] >= 4):
327335
self._baseline_v = self.v
328336
if byte == 140: # end of page
329337
return True

lib/matplotlib/tests/test_usetex.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,18 @@ def test_minus_no_descent(fontsize):
8181
assert len({*heights.values()}) == 1
8282

8383

84-
@pytest.mark.skipif(not _has_tex_package('xcolor'),
85-
reason='xcolor is not available')
86-
def test_usetex_xcolor():
84+
@pytest.mark.parametrize('pkg', ['xcolor', 'chemformula'])
85+
def test_usetex_packages(pkg):
86+
if not _has_tex_package(pkg):
87+
pytest.skip(f'{pkg} is not available')
8788
mpl.rcParams['text.usetex'] = True
8889

8990
fig = plt.figure()
9091
text = fig.text(0.5, 0.5, "Some text 0123456789")
9192
fig.canvas.draw()
9293

93-
mpl.rcParams['text.latex.preamble'] = r'\usepackage[dvipsnames]{xcolor}'
94+
mpl.rcParams['text.latex.preamble'] = (
95+
r'\PassOptionsToPackage{dvipsnames}{xcolor}\usepackage{%s}' % pkg)
9496
fig = plt.figure()
9597
text2 = fig.text(0.5, 0.5, "Some text 0123456789")
9698
fig.canvas.draw()

0 commit comments

Comments
 (0)
0