Skip to content

Commit 6cb9d62

Browse files
committed
Improve ps handling of individual usetex strings.
The ps backend currently determines usetex status solely based on the rcparam at renderer instantiation time; in particular, having single Text objects with usetex=True in an otherwise usetex=False renderer currently results in an AttributeError. Instead, detect that and do a best-effort fix which is to try rendering with mathtext instead of usetex. (Actually fixing the ps backend would be nicer, but a bit of work.) Also fix another ps test to not use a temporary directory.
1 parent 68652b1 commit 6cb9d62

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

lib/matplotlib/backends/backend_ps.py

+8
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,14 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms,
469469

470470
def draw_tex(self, gc, x, y, s, prop, angle, ismath='TeX!', mtext=None):
471471
# docstring inherited
472+
if not hasattr(self, "psfrag"):
473+
_log.warning(
474+
"The PS backend determines usetex status solely based on "
475+
"rcParams['text.usetex'] and does not support having "
476+
"usetex=True only for some elements; this element will thus "
477+
"be rendered as if usetex=False.")
478+
self.draw_text(gc, x, y, s, prop, angle, ismath, mtext)
479+
return
472480

473481
w, h, bl = self.get_text_width_height_descent(s, prop, ismath)
474482
fontsize = prop.get_size_in_points()

lib/matplotlib/tests/test_backend_ps.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,19 @@ def test_transparency():
117117

118118

119119
@needs_usetex
120-
def test_failing_latex(tmpdir):
120+
def test_failing_latex():
121121
"""Test failing latex subprocess call"""
122122
mpl.rcParams['text.usetex'] = True
123123
# This fails with "Double subscript"
124124
plt.xlabel("$22_2_2$")
125125
with pytest.raises(RuntimeError):
126-
plt.savefig(Path(tmpdir, "tmpoutput.ps"))
126+
plt.savefig(io.BytesIO(), format="ps")
127+
128+
129+
@needs_usetex
130+
def test_partial_usetex(caplog):
131+
caplog.set_level("WARNING")
132+
plt.figtext(.5, .5, "foo", usetex=True)
133+
plt.savefig(io.BytesIO(), format="ps")
134+
assert caplog.records and all("as if usetex=False" in record.getMessage()
135+
for record in caplog.records)

lib/matplotlib/text.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ def draw(self, renderer):
726726
if textobj.get_usetex():
727727
textrenderer.draw_tex(gc, x, y, clean_line,
728728
textobj._fontproperties, angle,
729-
mtext=mtext)
729+
ismath=ismath, mtext=mtext)
730730
else:
731731
textrenderer.draw_text(gc, x, y, clean_line,
732732
textobj._fontproperties, angle,

0 commit comments

Comments
 (0)