8000 PGF: Set up font sizes to match font.size setting · matplotlib/matplotlib@f44801a · GitHub
[go: up one dir, main page]

Skip to content

Commit f44801a

Browse files
committed
PGF: Set up font sizes to match font.size setting
This allows the output .pgf file to be used in external documents with arbitrary font size settings (which should match the rc setting font.size). This avoids mismatches between matplotlib and external LaTeX documents for things that depend on the font size setting (e.g. unicode-math). If the LaTeX package scrextend is present, this also adjusts the standard LaTeX font commands (\tiny, ..., \normalsize, ..., \Huge) relative to font.size.
1 parent a3eaef6 commit f44801a

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

lib/matplotlib/backends/backend_pgf.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
_create_pdf_info_dict, _datetime_to_pdf)
2424
from matplotlib.path import Path
2525
from matplotlib.figure import Figure
26+
from matplotlib.font_manager import FontProperties
2627
from matplotlib._pylab_helpers import Gcf
2728

2829
_log = logging.getLogger(__name__)
@@ -36,12 +37,27 @@
3637

3738
def _get_preamble():
3839
"""Prepare a LaTeX preamble based on the rcParams configuration."""
40+
font_size_pt = FontProperties(
41+
size=mpl.rcParams["font.size"]
42+
).get_size_in_points()
3943
return "\n".join([
4044
# Remove Matplotlib's custom command \mathdefault. (Not using
4145
# \mathnormal instead since this looks odd with Computer Modern.)
4246
r"\def\mathdefault#1{#1}",
4347
# Use displaystyle for all math.
4448
r"\everymath=\expandafter{\the\everymath\displaystyle}",
49+
# Set up font sizes to match font.size setting.
50+
r"\makeatletter",
51+
r"\IfFileExists{scrextend.sty}{",
52+
r" %s" % mpl.texmanager._usepackage_if_not_loaded(
53+
"scrextend", option="fontsize=%fpt" % font_size_pt
54+
),
55+
r"}{",
56+
r" \renewcommand{\normalsize}{\fontsize{%f}{%f}\selectfont}"
57+
% (font_size_pt, 1.2 * font_size_pt),
58+
r" \normalsize",
59+
r"}",
60+
r"\makeatother",
4561
# Allow pgf.preamble to override the above definitions.
4662
mpl.rcParams["pgf.preamble"],
4763
*([

0 commit comments

Comments
 (0)
0