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 8472fbb

Browse filesBrowse files
committed
Fix baseline alignment when using usetex.
Previously we inferred the baseline of usetex strings with the baseline of the character with the lowest baseline, which is wrong in the presence of indices (see usetex_baseline_test.py). There was an option to use the "preview" latex package to fix that, but that was never made the default and likely cannot as the package is GPL. Instead I can infer the baseline position by "reverse-engineering" the starting instructions of the dvi stream (this was done without consulting the approach in "preview"). The results can be checked using usetex_baseline_test.py (preview=False now looks the same as preview=True; likewise, the output of test_usetex was changed. The text.latex.preview rc can now be deprecated (in a future PR). The test was also updated to include the strings of the usetex_baseline_test example (which may go away once text.latex.preview is removed).
1 parent 0651abb commit 8472fbb
Copy full SHA for 8472fbb

File tree

Expand file treeCollapse file tree

6 files changed

+37
-14
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+37
-14
lines changed

‎lib/matplotlib/backends/backend_ps.py

Copy file name to clipboardExpand all lines: lib/matplotlib/backends/backend_ps.py
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,7 @@ def draw_tex(self, gc, x, y, s, prop, angle, ismath='TeX!', mtext=None):
484484
r'\psfrag{%s}[Bl][Bl][1][%f]{\fontsize{%f}{%f}%s}' % (
485485
thetext, angle, fontsize, fontsize*1.25, tex))
486486
else:
487-
# Stick to the bottom alignment, but this may give incorrect
488-
# baseline some times.
487+
# Stick to the bottom alignment.
489488
pos = _nums_to_str(x-corr, y-bl)
490489
self.psfrag.append(
491490
r'\psfrag{%s}[bl][bl][1][%f]{\fontsize{%f}{%f}%s}' % (

‎lib/matplotlib/dviread.py

Copy file name to clipboardExpand all lines: lib/matplotlib/dviread.py
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,9 @@ def _output(self):
274274
maxx = max(maxx, x + w)
275275
maxy = max(maxy, y + e)
276276
maxy_pure = max(maxy_pure, y)
277+
if self._baseline_v is not None:
278+
maxy_pure = self._baseline_v # This should normally be the case.
279+
self._baseline_v = None
277280

278281
if not self.text and not self.boxes: # Avoid infs/nans from inf+/-inf.
279282
return Page(text=[], boxes=[], width=0, height=0, descent=0)
@@ -304,9 +307,24 @@ def _read(self):
304307
Read one page from the file. Return True if successful,
305308
False if there were no more pages.
306309
"""
310+
# Pages appear to start with the sequence
311+
# bop (begin of page)
312+
# xxx comment
313+
# down
314+
# push
315+
# down, down
316+
# push
317+
# down (possibly multiple)
318+
# push <= here, v is the baseline position.
319+
# etc.
320+
# (dviasm is useful to explore this structure.)
321+
self._baseline_v = None
307322
while True:
308323
byte = self.file.read(1)[0]
309324
self._dtable[byte](self, byte)
325+
if (self._baseline_v is None
326+
and len(getattr(self, "stack", [])) == 3):
327+
self._baseline_v = self.v
310328
if byte == 140: # end of page
311329
return True
312330
if self.state is _dvistate.post_post: # end of file
Binary file not shown.
Loading

‎lib/matplotlib/tests/test_usetex.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_usetex.py
+17-11Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import platform
2-
31
import numpy as np
42
import pytest
53

@@ -12,24 +10,32 @@
1210
pytestmark = pytest.mark.skip('Missing TeX of Ghostscript or dvipng')
1311

1412

15-
@image_comparison(baseline_images=['test_usetex'],
16-
extensions=['pdf', 'png'],
17-
tol={'aarch64': 2.868, 'x86_64': 2.868}.get(
18-
platform.machine(), 0.3
19-
))
13+
@image_comparison(
14+
baseline_images=['test_usetex'],
15+
extensions=['pdf', 'png'],
16+
style="mpl20")
2017
def test_usetex():
2118
mpl.rcParams['text.usetex'] = True
2219
fig = plt.figure()
2320
ax = fig.add_subplot(111)
24-
ax.text(0.1, 0.2,
21+
kwargs = {"verticalalignment": "baseline", "size": 24,
22+
"bbox": dict(pad=0, edgecolor="k", facecolor="none")}
23+
ax.text(0.2, 0.7,
2524
# the \LaTeX macro exercises character sizing and placement,
2625
# \left[ ... \right\} draw some variable-height characters,
2726
# \sqrt and \frac draw horizontal rules, \mathrm changes the font
2827
r'\LaTeX\ $\left[\int\limits_e^{2e}'
2928
r'\sqrt\frac{\log^3 x}{x}\,\mathrm{d}x \right\}$',
30-
fontsize=24)
31-
ax.set_xticks([])
32-
ax.set_yticks([])
29+
**kwargs)
30+
ax.text(0.2, 0.3, "lg", **kwargs)
31+
ax.text(0.4, 0.3, r"$\frac{1}{2}\pi$", **kwargs)
32+
ax.text(0.6, 0.3, "$p^{3^A}$", **kwargs)
33+
ax.text(0.8, 0.3, "$p_{3_2}$", **kwargs)
34+
for x in {t.get_position()[0] for t in ax.texts}:
35+
ax.axvline(x)
36+
for y in {t.get_position()[1] for t in ax.texts}:
37+
ax.axhline(y)
38+
ax.set_axis_off()
3339

3440

3541
@check_figures_equal()

‎lib/matplotlib/texmanager.py

Copy file name to clipboardExpand all lines: lib/matplotlib/texmanager.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ def get_text_width_height_descent(self, tex, fontsize, renderer=None):
411411
return width, height + depth, depth
412412

413413
else:
414-
# use dviread. It sometimes returns a wrong descent.
414+
# use dviread.
415415
dvifile = self.make_dvi(tex, fontsize)
416416
with dviread.Dvi(dvifile, 72 * dpi_fraction) as dvi:
417417
page, = dvi

0 commit comments

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