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