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 74becc4

Browse filesBrowse files
jklymaktacaswell
authored andcommitted
Merge pull request #15695 from anntzer/mathdefault
Define \mathdefault as a noop in the usetex preamble. Conflicts: lib/matplotlib/ticker.py change of a near-by conditional from np.abs -> abs caused conflict
1 parent 687531d commit 74becc4
Copy full SHA for 74becc4

File tree

Expand file treeCollapse file tree

3 files changed

+45
-71
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+45
-71
lines changed

‎lib/matplotlib/tests/test_usetex.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_usetex.py
+14-5Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@
77
from matplotlib.ticker import EngFormatter
88

99

10-
@pytest.fixture(autouse=True) # All tests in this module use usetex.
11-
def usetex():
12-
if not mpl.checkdep_usetex(True):
13-
pytest.skip('Missing TeX of Ghostscript or dvipng')
14-
mpl.rcParams['text.usetex'] = True
10+
if not mpl.checkdep_usetex(True):
11+
pytestmark = pytest.mark.skip('Missing TeX of Ghostscript or dvipng')
1512

1613

1714
@image_comparison(baseline_images=['test_usetex'],
1815
extensions=['pdf', 'png'],
1916
tol={'aarch64': 2.868}.get(platform.machine(), 0.3))
2017
def test_usetex():
18+
mpl.rcParams['text.usetex'] = True
2119
fig = plt.figure()
2220
ax = fig.add_subplot(111)
2321
ax.text(0.1, 0.2,
@@ -33,5 +31,16 @@ def test_usetex():
3331

3432
@check_figures_equal()
3533
def test_unicode_minus(fig_test, fig_ref):
34+
mpl.rcParams['text.usetex'] = True
3635
fig_test.text(.5, .5, "$-$")
3736
fig_ref.text(.5, .5, "\N{MINUS SIGN}")
37+
38+
39+
def test_mathdefault():
40+
plt.rcParams["axes.formatter.use_mathtext"] = True
41+
fig = plt.figure()
42+
fig.add_subplot().set_xlim(-1, 1)
43+
# Check that \mathdefault commands generated by tickers don't cause
44+
# problems when later switching usetex on.
45+
mpl.rcParams['text.usetex'] = True
46+
fig.canvas.draw()

‎lib/matplotlib/texmanager.py

Copy file name to clipboardExpand all lines: lib/matplotlib/texmanager.py
+18-22Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,22 @@ def get_custom_preamble(self):
192192
"""Return a string containing user additions to the tex preamble."""
193193
return rcParams['text.latex.preamble']
194194

195+
def _get_preamble(self):
196+
unicode_preamble = "\n".join([
197+
r"\usepackage[utf8]{inputenc}",
198+
r"\DeclareUnicodeCharacter{2212}{\ensuremath{-}}",
199+
]) if rcParams["text.latex.unicode"] else ""
200+
return "\n".join([
201+
r"\documentclass{article}",
202+
# Pass-through \mathdefault, which is used in non-usetex mode to
203+
# use the default text font but was historically suppressed in
204+
# usetex mode.
205+
r"\newcommand{\mathdefault}[1]{#1}",
206+
self._font_preamble,
207+
unicode_preamble,
208+
self.get_custom_preamble(),
209+
])
210+
195211
def make_tex(self, tex, fontsize):
196212
"""
197213
Generate a tex file to render the tex string at a specific font size.
@@ -200,29 +216,19 @@ def make_tex(self, tex, fontsize):
200216
"""
201217
basefile = self.get_basefile(tex, fontsize)
202218
texfile = '%s.tex' % basefile
203-
custom_preamble = self.get_custom_preamble()
204219
fontcmd = {'sans-serif': r'{\sffamily %s}',
205220
'monospace': r'{\ttfamily %s}'}.get(self.font_family,
206221
r'{\rmfamily %s}')
207222
tex = fontcmd % tex
208223

209-
unicode_preamble = "\n".join([
210-
r"\usepackage[utf8]{inputenc}",
211-
r"\DeclareUnicodeCharacter{2212}{\ensuremath{-}}",
212-
]) if rcParams["text.latex.unicode"] else ""
213-
214224
s = r"""
215-
\documentclass{article}
216-
%s
217-
%s
218225
%s
219226
\usepackage[papersize={72in,72in},body={70in,70in},margin={1in,1in}]{geometry}
220227
\pagestyle{empty}
221228
\begin{document}
222229
\fontsize{%f}{%f}%s
223230
\end{document}
224-
""" % (self._font_preamble, unicode_preamble, custom_preamble,
225-
fontsize, fontsize * 1.25, tex)
231+
""" % (self._get_preamble(), fontsize, fontsize * 1.25, tex)
226232
with open(texfile, 'wb') as fh:
227233
if rcParams['text.latex.unicode']:
228234
fh.write(s.encode('utf8'))
@@ -250,24 +256,15 @@ def make_tex_preview(self, tex, fontsize):
250256
"""
251257
basefile = self.get_basefile(tex, fontsize)
252258
texfile = '%s.tex' % basefile
253-
custom_preamble = self.get_custom_preamble()
254259
fontcmd = {'sans-serif': r'{\sffamily %s}',
255260
'monospace': r'{\ttfamily %s}'}.get(self.font_family,
256261
r'{\rmfamily %s}')
257262
tex = fontcmd % tex
258263

259-
unicode_preamble = "\n".join([
260-
r"\usepackage[utf8]{inputenc}",
261-
r"\DeclareUnicodeCharacter{2212}{\ensuremath{-}}",
262-
]) if rcParams["text.latex.unicode"] else ""
263-
264264
# newbox, setbox, immediate, etc. are used to find the box
265265
# extent of the rendered text.
266266

267267
s = r"""
268-
\documentclass{article}
269-
%s
270-
%s
271268
%s
272269
\usepackage[active,showbox,tightpage]{preview}
273270
\usepackage[papersize={72in,72in},body={70in,70in},margin={1in,1in}]{geometry}
@@ -282,8 +279,7 @@ def make_tex_preview(self, tex, fontsize):
282279
{\fontsize{%f}{%f}%s}
283280
\end{preview}
284281
\end{document}
285-
""" % (self._font_preamble, unicode_preamble, custom_preamble,
286-
fontsize, fontsize * 1.25, tex)
282+
""" % (self._get_preamble(), fontsize, fontsize * 1.25, tex)
287283
with open(texfile, 'wb') as fh:
288284
if rcParams['text.latex.unicode']:
289285
fh.write(s.encode('utf8'))

‎lib/matplotlib/ticker.py

Copy file name to clipboardExpand all lines: lib/matplotlib/ticker.py
+13-44Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,6 @@
188188
'SymmetricalLogLocator', 'LogitLocator', 'OldAutoLocator')
189189

190190

191-
def _mathdefault(s):
192-
return '\\mathdefault{%s}' % s
193-
194-
195191
class _DummyAxis:
196192
def __init__(self, minpos=0):
197193
self.dataLim = mtransforms.Bbox.unit()
@@ -664,14 +660,10 @@ def get_offset(self):
664660
sciNotStr = self.format_data(10 ** self.orderOfMagnitude)
665661
else:
666662
sciNotStr = '1e%d' % self.orderOfMagnitude
667-
if self._useMathText:
668-
if sciNotStr != '':
669-
sciNotStr = r'\times%s' % _mathdefault(sciNotStr)
670-
s = ''.join(('$', sciNotStr, _mathdefault(offsetStr), '$'))
671-
elif self._usetex:
663+
if self._useMathText or self._usetex:
672664
if sciNotStr != '':
673-
sciNotStr = r'\times%s' % sciNotStr
674-
s = ''.join(('$', sciNotStr, offsetStr, '$'))
665+
sciNotStr = r'\times\mathdefault{%s}' % sciNotStr
666+
s = r'$%s\mathdefault{%s}$' % (sciNotStr, offsetStr)
675667
else:
676668
s = ''.join((sciNotStr, offsetStr))
677669

@@ -794,10 +786,8 @@ def _set_format(self):
794786
break
795787
sigfigs += 1
796788
self.format = '%1.' + str(sigfigs) + 'f'
797-
if self._usetex:
798-
self.format = '$%s$' % self.format
799-
elif self._useMathText:
800-
self.format = '$%s$' % _mathdefault(self.format)
789+
if self._usetex or self._useMathText:
790+
self.format = r'$\mathdefault{%s}$' % self.format
801791

802792
@cbook.deprecated("3.1")
803793
def pprint_val(self, x):
@@ -1091,11 +1081,7 @@ class LogFormatterMathtext(LogFormatter):
10911081

10921082
def _non_decade_format(self, sign_string, base, fx, usetex):
10931083
'Return string for non-decade locations'
1094-
if usetex:
1095-
return (r'$%s%s^{%.2f}$') % (sign_string, base, fx)
1096-
else:
1097-
return ('$%s$' % _mathdefault('%s%s^{%.2f}' %
1098-
(sign_string, base, fx)))
1084+
return r'$\mathdefault{%s%s^{%.2f}}$' % (sign_string, base, fx)
10991085

11001086
def __call__(self, x, pos=None):
11011087
"""
@@ -1107,10 +1093,7 @@ def __call__(self, x, pos=None):
11071093
min_exp = rcParams['axes.formatter.min_exponent']
11081094

11091095
if x == 0: # Symlog
1110-
if usetex:
1111-
return '$0$'
1112-
else:
1113-
return '$%s$' % _mathdefault('0')
1096+
return r'$\mathdefault{0}$'
11141097

11151098
sign_string = '-' if x < 0 else ''
11161099
x = abs(x)
@@ -1135,18 +1118,12 @@ def __call__(self, x, pos=None):
11351118
else:
11361119
base = '%s' % b
11371120

1138-
if np.abs(fx) < min_exp:
1139-
if usetex:
1140-
return r'${0}{1:g}$'.format(sign_string, x)
1141-
else:
1142-
return '${0}$'.format(_mathdefault(
1143-
'{0}{1:g}'.format(sign_string, x)))
1121+
if abs(fx) < min_exp:
1122+
return r'$\mathdefault{%s%g}$' % (sign_string, x)
11441123
elif not is_x_decade:
11451124
return self._non_decade_format(sign_string, base, fx, usetex)
1146-
elif usetex:
1147-
return r'$%s%s^{%d}$' % (sign_string, base, fx)
11481125
else:
1149-
return '$%s$' % _mathdefault('%s%s^{%d}' % (sign_string, base, fx))
1126+
return r'$\mathdefault{%s%s^{%d}}$' % (sign_string, base, fx)
11501127

11511128

11521129
class LogFormatterSciNotation(LogFormatterMathtext):
@@ -1161,12 +1138,8 @@ def _non_decade_format(self, sign_string, base, fx, usetex):
11611138
coeff = b ** fx / b ** exponent
11621139
if is_close_to_int(coeff):
11631140
coeff = round(coeff)
1164-
if usetex:
1165-
return (r'$%s%g\times%s^{%d}$') % \
1166-
(sign_string, coeff, base, exponent)
1167-
else:
1168-
return ('$%s$' % _mathdefault(r'%s%g\times%s^{%d}' %
1169-
(sign_string, coeff, base, exponent)))
1141+
return r'$\mathdefault{%s%g\times%s^{%d}}$' \
1142+
% (sign_string, coeff, base, exponent)
11701143

11711144

11721145
class LogitFormatter(Formatter):
@@ -1338,8 +1311,6 @@ def __call__(self, x, pos=None):
13381311
return ""
13391312
if x <= 0 or x >= 1:
13401313
return ""
1341-
usetex = rcParams["text.usetex"]
1342-
13431314
if is_close_to_int(2 * x) and round(2 * x) == 1:
13441315
s = self._one_half
13451316
elif x < 0.5 and is_decade(x, rtol=1e-7):
@@ -1354,9 +1325,7 @@ def __call__(self, x, pos=None):
13541325
s = self._one_minus(self._format_value(1-x, 1-self.locs))
13551326
else:
13561327
s = self._format_value(x, self.locs, sci_notation=False)
1357-
if usetex:
1358-
return "$%s$" % s
1359-
return "$%s$" % _mathdefault(s)
1328+
return r"$\mathdefault{%s}$" % s
13601329

13611330
def format_data_short(self, value):
13621331
"""

0 commit comments

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