23
23
_create_pdf_info_dict , _datetime_to_pdf )
24
24
from matplotlib .path import Path
25
25
from matplotlib .figure import Figure
26
+ from matplotlib .font_manager import FontProperties
26
27
from matplotlib ._pylab_helpers import Gcf
27
28
28
29
_log = logging .getLogger (__name__ )
29
30
30
31
32
+ _DOCUMENTCLASS = r"\documentclass{article}"
33
+
34
+
31
35
# Note: When formatting floating point values, it is important to use the
32
36
# %f/{:f} format rather than %s/{} to avoid triggering scientific notation,
33
37
# which is not recognized by TeX.
34
38
35
39
def _get_preamble ():
36
40
"""Prepare a LaTeX preamble based on the rcParams configuration."""
41
+ font_size_pt = FontProperties (
42
+ size = mpl .rcParams ["font.size" ]
43
+ ).get_size_in_points ()
37
44
return "\n " .join ([
38
45
# Remove Matplotlib's custom command \mathdefault. (Not using
39
46
# \mathnormal instead since this looks odd with Computer Modern.)
40
47
r"\def\mathdefault#1{#1}" ,
41
48
# Use displaystyle for all math.
42
49
r"\everymath=\expandafter{\the\everymath\displaystyle}" ,
50
+ # Set up font sizes to match font.size setting.
51
+ # If present, use the KOMA package scrextend to adjust the standard
52
+ # LaTeX font commands (\tiny, ..., \normalsize, ..., \Huge) accordingly.
53
+ # Otherwise, only set \normalsize, manually.
54
+ r"\IfFileExists{scrextend.sty}{" ,
55
+ r" \usepackage[fontsize=%fpt]{scrextend}" % font_size_pt ,
56
+ r"}{" ,
57
+ r" \renewcommand{\normalsize}{\fontsize{%f}{%f}\selectfont}"
58
+ % (font_size_pt , 1.2 * font_size_pt ),
59
+ r" \normalsize" ,
60
+ r"}" ,
43
61
# Allow pgf.preamble to override the above definitions.
44
62
mpl .rcParams ["pgf.preamble" ],
45
- r"\ifdefined\pdftexversion\else % non-pdftex case." ,
46
- r" \usepackage{fontspec}" ,
47
63
* ([
64
+ r"\ifdefined\pdftexversion\else % non-pdftex case." ,
65
+ r" \usepackage{fontspec}" ,
66
+ ] + [
48
67
r" \%s{%s}[Path=\detokenize{%s/}]"
49
68
% (command , path .name , path .parent .as_posix ())
50
69
for command , path in zip (
51
70
["setmainfont" , "setsansfont" , "setmonofont" ],
52
71
[pathlib .Path (fm .findfont (family ))
53
72
for family in ["serif" , "sans\\ -serif" , "monospace" ]]
54
73
)
55
- ] if mpl .rcParams ["pgf.rcfonts" ] else []),
56
- r"\fi" ,
74
+ ] + [r"\fi" ] if mpl .rcParams ["pgf.rcfonts" ] else []),
57
75
# Documented as "must come last".
58
76
mpl .texmanager ._usepackage_if_not_loaded ("underscore" , option = "strings" ),
59
77
])
@@ -94,6 +112,8 @@ def _escape_and_apply_props(s, prop):
94
112
family = prop .get_family ()[0 ]
95
113
if family in families :
96
114
commands .append (families [family ])
115
+ elif not mpl .rcParams ["pgf.rcfonts" ]:
116
+ commands .append (r"\fontfamily{\familydefault}" )
97
117
elif any (font .name == family for font in fm .fontManager .ttflist ):
98
118
commands .append (
99
119
r"\ifdefined\pdftexversion\else\setmainfont{%s}\rmfamily\fi" % family )
@@ -185,7 +205,7 @@ class LatexManager:
185
205
@staticmethod
186
206
def _build_latex_header ():
187
207
latex_header = [
188
- r"\documentclass{article}" ,
208
+ _DOCUMENTCLASS ,
189
209
# Include TeX program name as a comment for cache invalidation.
190
210
# TeX does not allow this to be the first line.
191
211
rf"% !TeX program = { mpl .rcParams ['pgf.texsystem' ]} " ,
@@ -814,7 +834,7 @@ def print_pdf(self, fname_or_fh, *, metadata=None, **kwargs):
814
834
self .print_pgf (tmppath / "figure.pgf" , ** kwargs )
815
835
(tmppath / "figure.tex" ).write_text (
816
836
"\n " .join ([
817
- r"\documentclass[12pt]{article}" ,
837
+ _DOCUMENTCLASS ,
818
838
r"\usepackage[pdfinfo={%s}]{hyperref}" % pdfinfo ,
819
839
r"\usepackage[papersize={%fin,%fin}, margin=0in]{geometry}"
820
840
% (w , h ),
@@ -924,7 +944,7 @@ def _write_header(self, width_inches, height_inches):
924
944
pdfinfo = ',' .join (
925
945
_metadata_to_str (k , v ) for k , v in self ._info_dict .items ())
926
946
latex_header = "\n " .join ([
927
- r"\documentclass[12pt]{article}" ,
947
+ _DOCUMENTCLASS ,
928
948
r"\usepackage[pdfinfo={%s}]{hyperref}" % pdfinfo ,
929
949
r"\usepackage[papersize={%fin,%fin}, margin=0in]{geometry}"
930
950
% (width_inches , height_inches ),
0 commit comments