Skip to content

Commit e31cd35

Browse files
committed
Switch TeX baseline detection to use a dvi special.
Insert a `\special` at the end of the TeX output and record the baseline position there. This should be more robust than trying to guess the format of the dvi preamble, and more importantly this will also allow detecting the last baseline of *multiline* TeX output, which will then allow letting TeX handle multiline strings itself (while keeping the correct alignment).
1 parent 3331777 commit e31cd35

File tree

2 files changed

+3
-30
lines changed

2 files changed

+3
-30
lines changed

lib/matplotlib/dviread.py

+2-29
Original file line numberDiff line numberDiff line change
@@ -291,40 +291,11 @@ def _read(self):
291291
Read one page from the file. Return True if successful,
292292
False if there were no more pages.
293293
"""
294-
# Pages appear to start with the sequence
295-
# bop (begin of page)
296-
# xxx comment
297-
# <push, ..., pop> # if using chemformula
298-
# down
299-
# push
300-
# down
301-
# <push, push, xxx, right, xxx, pop, pop> # if using xcolor
302-
# down
303-
# push
304-
# down (possibly multiple)
305-
# push <= here, v is the baseline position.
306-
# etc.
307-
# (dviasm is useful to explore this structure.)
308-
# Thus, we use the vertical position at the first time the stack depth
309-
# reaches 3, while at least three "downs" have been executed (excluding
310-
# those popped out (corresponding to the chemformula preamble)), as the
311-
# baseline (the "down" count is necessary to handle xcolor).
312-
down_stack = [0]
313294
self._baseline_v = None
314295
while True:
315296
byte = self.file.read(1)[0]
316297
self._dtable[byte](self, byte)
317298
name = self._dtable[byte].__name__
318-
if name == "_push":
319-
down_stack.append(down_stack[-1])
320-
elif name == "_pop":
321-
down_stack.pop()
322-
elif name == "_down":
323-
down_stack[-1] += 1
324-
if (self._baseline_v is None
325-
and len(getattr(self, "stack", [])) == 3
326-
and down_stack[-1] >= 4):
327-
self._baseline_v = self.v
328299
if byte == 140: # end of page
329300
return True
330301
if self.state is _dvistate.post_post: # end of file
@@ -457,6 +428,8 @@ def _fnt_num(self, new_f):
457428
@_dispatch(min=239, max=242, args=('ulen1',))
458429
def _xxx(self, datalen):
459430
special = self.file.read(datalen)
431+
if special == b'matplotlibbaselinemarker':
432+
self._baseline_v = self.v
460433
_log.debug(
461434
'Dvi._xxx: encountered special: %s',
462435
''.join([chr(ch) if 32 <= ch < 127 else '<%02x>' % ch

lib/matplotlib/texmanager.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def make_tex(self, tex, fontsize):
217217
%% when using psfrag which gets confused by it.
218218
\fontsize{%(fontsize)f}{%(baselineskip)f}%%
219219
\ifdefined\psfrag\else\hbox{}\fi%%
220-
{%(fontcmd)s %(tex)s}
220+
{%(fontcmd)s %(tex)s}\special{matplotlibbaselinemarker}
221221
\end{document}
222222
"""
223223
Path(texfile).write_text(tex_template % {

0 commit comments

Comments
 (0)