Skip to content

Commit 91b2748

Browse files
committed
Define \mathdefault as a noop in the usetex preamble.
This avoids having to strip out \mathdefault (a non-existent latex command) before generating usetex strings, as that's brittle on tickers -- the global usetex flag may change after the ticker is instantiated.
1 parent e23b2aa commit 91b2748

File tree

2 files changed

+14
-43
lines changed

2 files changed

+14
-43
lines changed

lib/matplotlib/texmanager.py

+2
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ def make_tex(self, tex, fontsize):
213213

214214
s = r"""
215215
\documentclass{article}
216+
\newcommand{\mathdefault}[1]{#1}
216217
%s
217218
%s
218219
%s
@@ -266,6 +267,7 @@ def make_tex_preview(self, tex, fontsize):
266267

267268
s = r"""
268269
\documentclass{article}
270+
\newcommand{\mathdefault}[1]{#1}
269271
%s
270272
%s
271273
%s

lib/matplotlib/ticker.py

+12-43
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()
@@ -654,14 +650,10 @@ def get_offset(self):
654650
sciNotStr = self.format_data(10 ** self.orderOfMagnitude)
655651
else:
656652
sciNotStr = '1e%d' % self.orderOfMagnitude
657-
if self._useMathText:
658-
if sciNotStr != '':
659-
sciNotStr = r'\times%s' % _mathdefault(sciNotStr)
660-
s = ''.join(('$', sciNotStr, _mathdefault(offsetStr), '$'))
661-
elif self._usetex:
653+
if self._useMathText or self._usetex:
662654
if sciNotStr != '':
663-
sciNotStr = r'\times%s' % sciNotStr
664-
s = ''.join(('$', sciNotStr, offsetStr, '$'))
655+
sciNotStr = r'\times\mathdefault{%s}' % sciNotStr
656+
s = r'$%s\mathdefault{%s}$' % (sciNotStr, offsetStr)
665657
else:
666658
s = ''.join((sciNotStr, offsetStr))
667659

@@ -784,10 +776,8 @@ def _set_format(self):
784776
break
785777
sigfigs += 1
786778
self.format = '%1.' + str(sigfigs) + 'f'
787-
if self._usetex:
788-
self.format = '$%s$' % self.format
789-
elif self._useMathText:
790-
self.format = '$%s$' % _mathdefault(self.format)
779+
if self._usetex or self._useMathText:
780+
self.format = r'$\mathdefault{%s}$' % self.format
791781

792782
@cbook.deprecated("3.1")
793783
def pprint_val(self, x):
@@ -1079,11 +1069,7 @@ class LogFormatterMathtext(LogFormatter):
10791069

10801070
def _non_decade_format(self, sign_string, base, fx, usetex):
10811071
'Return string for non-decade locations'
1082-
if usetex:
1083-
return (r'$%s%s^{%.2f}$') % (sign_string, base, fx)
1084-
else:
1085-
return ('$%s$' % _mathdefault('%s%s^{%.2f}' %
1086-
(sign_string, base, fx)))
1072+
return r'$\mathdefault{%s%s^{%.2f}}$' % (sign_string, base, fx)
10871073

10881074
def __call__(self, x, pos=None):
10891075
"""
@@ -1095,10 +1081,7 @@ def __call__(self, x, pos=None):
10951081
min_exp = rcParams['axes.formatter.min_exponent']
10961082

10971083
if x == 0: # Symlog
1098-
if usetex:
1099-
return '$0$'
1100-
else:
1101-
return '$%s$' % _mathdefault('0')
1084+
return r'$\mathdefault{0}$'
11021085

11031086
sign_string = '-' if x < 0 else ''
11041087
x = abs(x)
@@ -1124,17 +1107,11 @@ def __call__(self, x, pos=None):
11241107
base = '%s' % b
11251108

11261109
if np.abs(fx) < min_exp:
1127-
if usetex:
1128-
return r'${0}{1:g}$'.format(sign_string, x)
1129-
else:
1130-
return '${0}$'.format(_mathdefault(
1131-
'{0}{1:g}'.format(sign_string, x)))
1110+
return r'$\mathdefault{%s%g}$' % (sign_string, x)
11321111
elif not is_x_decade:
11331112
return self._non_decade_format(sign_string, base, fx, usetex)
1134-
elif usetex:
1135-
return r'$%s%s^{%d}$' % (sign_string, base, fx)
11361113
else:
1137-
return '$%s$' % _mathdefault('%s%s^{%d}' % (sign_string, base, fx))
1114+
return r'$\mathdefault{%s%s^{%d}}$' % (sign_string, base, fx)
11381115

11391116

11401117
class LogFormatterSciNotation(LogFormatterMathtext):
@@ -1149,12 +1126,8 @@ def _non_decade_format(self, sign_string, base, fx, usetex):
11491126
coeff = b ** fx / b ** exponent
11501127
if is_close_to_int(coeff):
11511128
coeff = round(coeff)
1152-
if usetex:
1153-
return (r'$%s%g\times%s^{%d}$') % \
1154-
(sign_string, coeff, base, exponent)
1155-
else:
1156-
return ('$%s$' % _mathdefault(r'%s%g\times%s^{%d}' %
1157-
(sign_string, coeff, base, exponent)))
1129+
return r'$\mathdefault{%s%g\times%s^{%d}}$' \
1130+
% (sign_string, coeff, base, exponent)
11581131

11591132

11601133
class LogitFormatter(Formatter):
@@ -1326,8 +1299,6 @@ def __call__(self, x, pos=None):
13261299
return ""
13271300
if x <= 0 or x >= 1:
13281301
return ""
1329-
usetex = rcParams["text.usetex"]
1330-
13311302
if is_close_to_int(2 * x) and round(2 * x) == 1:
13321303
s = self._one_half
13331304
elif x < 0.5 and is_decade(x, rtol=1e-7):
@@ -1342,9 +1313,7 @@ def __call__(self, x, pos=None):
13421313
s = self._one_minus(self._format_value(1-x, 1-self.locs))
13431314
else:
13441315
s = self._format_value(x, self.locs, sci_notation=False)
1345-
if usetex:
1346-
return "$%s$" % s
1347-
return "$%s$" % _mathdefault(s)
1316+
return r"$\mathdefault{%s}$" % s
13481317

13491318
def format_data_short(self, value):
13501319
"""

0 commit comments

Comments
 (0)