37
37
@_api .caching_module_getattr
38
38
class __getattr__ :
39
39
NO_ESCAPE = _api .deprecated ("3.6" , obj_type = "" )(
40
- property (lambda self : _NO_ESCAPE ))
40
+ property (lambda self : r"(?<!\\)(?:\\\\)*" ))
41
41
re_mathsep = _api .deprecated ("3.6" , obj_type = "" )(
42
- property (lambda self : _split_math . __self__ ))
42
+ property (lambda self : r"(?<!\\)(?:\\\\)*\$" ))
43
43
44
44
45
45
@_api .deprecated ("3.6" )
@@ -57,7 +57,15 @@ def get_preamble():
57
57
58
58
def _get_preamble ():
59
59
"""Prepare a LaTeX preamble based on the rcParams configuration."""
60
- preamble = [mpl .rcParams ["pgf.preamble" ]]
60
+ preamble = [
61
+ # Remove Matplotlib's custom command \mathdefault. (Not using
62
+ # \mathnormal instead since this looks odd with Computer Modern.)
63
+ r"\def\mathdefault#1{#1}" ,
64
+ # Use displaystyle for all math.
65
+ r"\everymath=\expandafter{\the\everymath\displaystyle}" ,
66
+ # Allow pgf.preamble to override the above definitions.
67
+ mpl .rcParams ["pgf.preamble" ],
68
+ ]
61
69
if mpl .rcParams ["pgf.texsystem" ] != "pdflatex" :
62
70
preamble .append ("\\ usepackage{fontspec}" )
63
71
if mpl .rcParams ["pgf.rcfonts" ]:
@@ -82,16 +90,6 @@ def _get_preamble():
82
90
mpl_in_to_pt = 1. / mpl_pt_to_in
83
91
84
92
85
- _NO_ESCAPE = r"(?<!\\)(?:\\\\)*"
86
- _split_math = re .compile (_NO_ESCAPE + r"\$" ).split
87
- _replace_escapetext = functools .partial (
88
- # When the next character is an unescaped % or ^, insert a backslash.
89
- re .compile (_NO_ESCAPE + "(?=[%^])" ).sub , "\\ \\ " )
90
- _replace_mathdefault = functools .partial (
91
- # Replace \mathdefault (when not preceded by an escape) by empty string.
92
- re .compile (_NO_ESCAPE + r"(\\mathdefault)" ).sub , "" )
93
-
94
-
95
93
@_api .deprecated ("3.6" )
96
94
def common_texification (text ):
97
95
return _tex_escape (text )
@@ -101,28 +99,8 @@ def _tex_escape(text):
101
99
r"""
102
100
Do some necessary and/or useful substitutions for texts to be included in
103
101
LaTeX documents.
104
-
105
- This distinguishes text-mode and math-mode by replacing the math separator
106
- ``$`` with ``\(\displaystyle %s\)``. Escaped math separators (``\$``)
107
- are ignored.
108
-
109
- The following characters are escaped in text segments: ``^%``
110
102
"""
111
- # Sometimes, matplotlib adds the unknown command \mathdefault.
112
- # Not using \mathnormal instead since this looks odd for the latex cm font.
113
- text = _replace_mathdefault (text )
114
- text = text .replace ("\N{MINUS SIGN} " , r"\ensuremath{-}" )
115
- # split text into normaltext and inline math parts
116
- parts = _split_math (text )
117
- for i , s in enumerate (parts ):
118
- if not i % 2 :
119
- # textmode replacements
120
- s = _replace_escapetext (s )
121
- else :
122
- # mathmode replacements
123
- s = r"\(\displaystyle %s\)" % s
124
- parts [i ] = s
125
- return "" .join (parts )
103
+ return text .replace ("\N{MINUS SIGN} " , r"\ensuremath{-}" )
126
104
127
105
128
106
@_api .deprecated ("3.6" )
@@ -167,7 +145,17 @@ def _escape_and_apply_props(s, prop):
167
145
commands .append (r"\bfseries" )
168
146
169
147
commands .append (r"\selectfont" )
170
- return "" .join (commands ) + " " + _tex_escape (s )
148
+ return (
149
+ "{"
150
+ + "" .join (commands )
151
+ + r"\catcode`\^=\active\def^{\ifmmode\sp\else\^{}\fi}"
152
+ # It should normally be enough to set the catcode of % to 12 ("normal
153
+ # character"); this works on TeXLive 2021 but not on 2018, so we just
154
+ # make it active too.
155
+ + r"\catcode`\%=\active\def%{\%}"
156
+ + _tex_escape (s )
157
+ + "}"
158
+ )
171
159
172
160
173
161
def _metadata_to_str (key , value ):
@@ -349,7 +337,11 @@ def _get_box_metrics(self, tex):
349
337
"""
350
338
# This method gets wrapped in __init__ for per-instance caching.
351
339
self ._stdin_writeln ( # Send textbox to TeX & request metrics typeout.
352
- r"\sbox0{%s}\typeout{\the\wd0,\the\ht0,\the\dp0}" % tex )
340
+ # \sbox doesn't handle catcode assignments inside its argument,
341
+ # so repeat the assignment of the catcode of "^" and "%" outside.
342
+ r"{\catcode`\^=\active\catcode`\%%=\active\sbox0{%s}"
343
+ r"\typeout{\the\wd0,\the\ht0,\the\dp0}}"
344
+ % tex )
353
345
try :
354
346
answer = self ._expect_prompt ()
355
347
except LatexError as err :
0 commit comments