Skip to content

Fix text position with usetex and xcolor #19501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 17, 2021
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
12 changes: 10 additions & 2 deletions lib/matplotlib/dviread.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,18 +313,26 @@ def _read(self):
# xxx comment
# down
# push
# down, down
# down
# <push, push, xxx, right, xxx, pop, pop> # if using xcolor
# down
# push
# down (possibly multiple)
# push <= here, v is the baseline position.
# etc.
# (dviasm is useful to explore this structure.)
# Thus, we use the vertical position at the first time the stack depth
# reaches 3, while at least three "downs" have been executed, as the
# baseline (the "down" count is necessary to handle xcolor).
downs = 0
self._baseline_v = None
while True:
byte = self.file.read(1)[0]
self._dtable[byte](self, byte)
downs += self._dtable[byte].__name__ == "_down"
if (self._baseline_v is None
and len(getattr(self, "stack", [])) == 3):
and len(getattr(self, "stack", [])) == 3
and downs >= 4):
self._baseline_v = self.v
if byte == 140: # end of page
return True
Expand Down
4 changes: 4 additions & 0 deletions lib/matplotlib/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,7 @@ def check_for_pgf(texsystem):
except (OSError, subprocess.CalledProcessError):
return False
return True


def _has_tex_package(package):
return bool(mpl.dviread.find_tex_file(f"{package}.sty"))
7 changes: 1 addition & 6 deletions lib/matplotlib/tests/test_backend_pgf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
from io import BytesIO
import os
import shutil
import subprocess

import numpy as np
import pytest

import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.testing import check_for_pgf
from matplotlib.testing import _has_tex_package, check_for_pgf
from matplotlib.testing.compare import compare_images, ImageComparisonFailure
from matplotlib.backends.backend_pgf import PdfPages, common_texification
from matplotlib.testing.decorators import (_image_directories,
Expand All @@ -29,10 +28,6 @@
reason="This test needs a ghostscript installation")


def _has_tex_package(package):
return bool(mpl.dviread.find_tex_file(f"{package}.sty"))


def compare_figure(fname, savefig_kwargs={}, tol=0):
actual = os.path.join(result_dir, fname)
plt.savefig(actual, **savefig_kwargs)
Expand Down
18 changes: 18 additions & 0 deletions lib/matplotlib/tests/test_usetex.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytest

import matplotlib as mpl
from matplotlib.testing import _has_tex_package
from matplotlib.testing.decorators import check_figures_equal, image_comparison
import matplotlib.pyplot as plt

Expand Down Expand Up @@ -80,6 +81,23 @@ def test_minus_no_descent(fontsize):
assert len({*heights.values()}) == 1


@pytest.mark.skipif(not _has_tex_package('xcolor'),
reason='xcolor is not available')
def test_usetex_xcolor():
mpl.rcParams['text.usetex'] = True

fig = plt.figure()
text = fig.text(0.5, 0.5, "Some text 0123456789")
fig.canvas.draw()

mpl.rcParams['text.latex.preamble'] = r'\usepackage[dvipsnames]{xcolor}'
fig = plt.figure()
text2 = fig.text(0.5, 0.5, "Some text 0123456789")
fig.canvas.draw()
np.testing.assert_array_equal(text2.get_window_extent(),
text.get_window_extent())


def test_textcomp_full():
plt.rcParams["text.latex.preamble"] = r"\usepackage[full]{textcomp}"
fig = plt.figure()
Expand Down