@@ -70,6 +70,8 @@ def _get_preamble():
70
70
path = pathlib .Path (fm .findfont (family ))
71
71
preamble .append (r"\%s{%s}[Path=\detokenize{%s/}]" % (
72
72
command , path .name , path .parent .as_posix ()))
73
+ preamble .append (mpl .texmanager ._usepackage_if_not_loaded (
74
+ "underscore" , option = "strings" )) # Documented as "must come last".
73
75
return "\n " .join (preamble )
74
76
75
77
@@ -84,9 +86,8 @@ def _get_preamble():
84
86
_NO_ESCAPE = r"(?<!\\)(?:\\\\)*"
85
87
_split_math = re .compile (_NO_ESCAPE + r"\$" ).split
86
88
_replace_escapetext = functools .partial (
87
- # When the next character is _, ^, $, or % (not preceded by an escape),
88
- # insert a backslash.
89
- re .compile (_NO_ESCAPE + "(?=[_^$%])" ).sub , "\\ \\ " )
89
+ # When the next character is an unescaped % or ^, insert a backslash.
90
+ re .compile (_NO_ESCAPE + "(?=[%^])" ).sub , "\\ \\ " )
90
91
_replace_mathdefault = functools .partial (
91
92
# Replace \mathdefault (when not preceded by an escape) by empty string.
92
93
re .compile (_NO_ESCAPE + r"(\\mathdefault)" ).sub , "" )
@@ -106,7 +107,7 @@ def _tex_escape(text):
106
107
``$`` with ``\(\displaystyle %s\)``. Escaped math separators (``\$``)
107
108
are ignored.
108
109
109
- The following characters are escaped in text segments: ``_^$ %``
110
+ The following characters are escaped in text segments: ``^ %``
110
111
"""
111
112
# Sometimes, matplotlib adds the unknown command \mathdefault.
112
113
# Not using \mathnormal instead since this looks odd for the latex cm font.
@@ -358,14 +359,16 @@ def _get_box_metrics(self, tex):
358
359
try :
359
360
answer = self ._expect_prompt ()
360
361
except LatexError as err :
361
- raise ValueError ("Error measuring {!r}\n LaTeX Output:\n {}"
362
+ # Here and below, use '{}' instead of {!r} to avoid doubling all
363
+ # backslashes.
364
+ raise ValueError ("Error measuring {}\n LaTeX Output:\n {}"
362
365
.format (tex , err .latex_output )) from err
363
366
try :
364
367
# Parse metrics from the answer string. Last line is prompt, and
365
368
# next-to-last-line is blank line from \typeout.
366
369
width , height , offset = answer .splitlines ()[- 3 ].split ("," )
367
370
except Exception as err :
368
- raise ValueError ("Error measuring {!r }\n LaTeX Output:\n {}"
371
+ raise ValueError ("Error measuring {}\n LaTeX Output:\n {}"
369
372
.format (tex , answer )) from err
370
373
w , h , o = float (width [:- 2 ]), float (height [:- 2 ]), float (offset [:- 2 ])
371
374
# The height returned from LaTeX goes from base to top;
@@ -864,8 +867,8 @@ def print_pdf(self, fname_or_fh, *, metadata=None, **kwargs):
864
867
r"\documentclass[12pt]{minimal}" ,
865
868
r"\usepackage[papersize={%fin,%fin}, margin=0in]{geometry}"
866
869
% (w , h ),
867
- _get_preamble (),
868
870
r"\usepackage{pgf}" ,
871
+ _get_preamble (),
869
872
r"\begin{document}" ,
870
873
r"\centering" ,
871
874
r"\input{figure.pgf}" ,
@@ -975,8 +978,8 @@ def _write_header(self, width_inches, height_inches):
975
978
r"\documentclass[12pt]{minimal}" ,
976
979
r"\usepackage[papersize={%fin,%fin}, margin=0in]{geometry}"
977
980
% (width_inches , height_inches ),
978
- _get_preamble (),
979
981
r"\usepackage{pgf}" ,
982
+ _get_preamble (),
980
983
r"\setlength{\parindent}{0pt}" ,
981
984
r"\begin{document}%" ,
982
985
])
0 commit comments