8000 Merge pull request #20293 from Carloscerq/simplify_font_setting_usetex · matplotlib/matplotlib@f4c518e · GitHub
[go: up one dir, main page]

Skip to content

Commit f4c518e

Browse files
authored
Merge pull request #20293 from Carloscerq/simplify_font_setting_usetex
Simplify font setting in usetex mode
2 parents d4ac442 + 397f723 commit f4c518e

File tree

4 files changed

+70
-14
lines changed

4 files changed

+70
-14
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
Simplifying the font setting for usetex mode
3+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4+
5+
Now the :rc:`font.family` accepts some font names as value for a more
6+
user-friendly setup.
7+
8+
.. code-block::
9+
10+
plt.rcParams.update({
11+
"text.usetex": True,
12+
"font.family": "Helvetica"
13+
})

lib/matplotlib/tests/test_texmanager.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ def test_fontconfig_preamble():
3030
r"\usepackage{chancery}", r"\rmfamily"),
3131
({"font.family": "monospace", "font.monospace": "courier"},
3232
r"\usepackage{courier}", r"\ttfamily"),
33+
({"font.family": "helvetica"}, r"\usepackage{helvet}", r"\sffamily"),
34+
({"font.family": "palatino"}, r"\usepackage{mathpazo}", r"\rmfamily"),
35+
({"font.family": "zapf chancery"},
36+
r"\usepackage{chancery}", r"\rmfamily"),
37+
({"font.family": "courier"}, r"\usepackage{courier}", r"\ttfamily")
3338
])
3439
def test_font_selection(rc, preamble, family):
3540
plt.rcParams.update(rc)

lib/matplotlib/texmanager.py

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ class TexManager:
6868
'computer modern roman': ('cmr', r'\usepackage{type1ec}'),
6969
'computer modern sans serif': ('cmss', r'\usepackage{type1ec}'),
7070
'computer modern typewriter': ('cmtt', r'\usepackage{type1ec}')}
71+
_font_types = {
72+
'new century schoolbook': 'serif', 'bookman': 'serif',
73+
'times': 'serif', 'palatino': 'serif', 'charter': 'serif',
74+
'computer modern roman': 'serif', 'zapf chancery': 'cursive',
75+
'helvetica': 'sans-serif', 'avant garde': 'sans-serif',
76+
'computer modern sans serif': 'sans-serif',
77+
'courier': 'monospace', 'computer modern typewriter': 'monospace'}
7178

7279
grey_arrayd = _api.deprecate_privatize_attribute("3.5")
7380
font_family = _api.deprecate_privatize_attribute("3.5")
@@ -81,8 +88,13 @@ def __new__(cls):
8188

8289
def get_font_config(self):
8390
ff = rcParams['font.family']
84-
if len(ff) == 1 and ff[0].lower() in self._font_families:
85-
self._font_family = ff[0].lower()
91+
ff_val = ff[0].lower() if len(ff) == 1 else None
92+
reduced_notation = False
93+
if len(ff) == 1 and ff_val in self._font_families:
94+
self._font_family = ff_val
95+
elif len(ff) == 1 and ff_val in self._font_info:
96+
reduced_notation = True
97+
self._font_family = self._font_types[ff_val]
8698
else:
8799
_log.info('font.family must be one of (%s) when text.usetex is '
88100
'True. serif will be used by default.',
@@ -92,19 +104,24 @@ def get_font_config(self):
92104
fontconfig = [self._font_family]
93105
fonts = {}
94106
for font_family in self._font_families:
95-
for font in rcParams['font.' + font_family]:
96-
if font.lower() in self._font_info:
97-
fonts[font_family] = self._font_info[font.lower()]
98-
_log.debug(
99-
'family: %s, font: %s, info: %s',
100-
font_family, font, self._font_info[font.lower()])
101-
break
102-
else:
103-
_log.debug('%s font is not compatible with usetex.', font)
107+
if reduced_notation and self._font_family == font_family:
108+
fonts[font_family] = self._font_info[ff_val]
104109
else:
105-
_log.info('No LaTeX-compatible font found for the %s font '
106-
'family in rcParams. Using default.', font_family)
107-
fonts[font_family] = self._font_info[font_family]
110+
for font in rcParams['font.' + font_family]:
111+
if font.lower() in self._font_info:
112+
fonts[font_family] = self._font_info[font.lower()]
113+
_log.debug(
114+
'family: %s, font: %s, info: %s',
115+
font_family, font, self._font_info[font.lower()])
116+
break
117+
else:
118+
_log.debug('%s font is not compatible with usetex.',
119+
font)
120+
else:
121+
_log.info('No LaTeX-compatible font found for the %s font'
122+
'family in rcParams. Using default.',
123+
font_family)
124+
fonts[font_family] = self._font_info[font_family]
108125
fontconfig.append(fonts[font_family][0])
109126
# Add a hash of the latex preamble to fontconfig so that the
110127
# correct png is selected for strings rendered with same font and dpi

tutorials/text/usetex.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,27 @@
5151
"font.family": "serif",
5252
"font.serif": ["Palatino"],
5353
})
54+
# It's also possible to use the reduced notation by directly setting font.family:
55+
plt.rcParams.update({
56+
"text.usetex": True,
57+
"font.family": "Helvetica"
58+
})
59+
60+Currently, the supported fonts are:
61+
62+
================= ============================================================
63+
family fonts
64+
================= ============================================================
65+
``'serif'`` ``'New Century Schoolbook'``, ``'Bookman'``, ``'Times'``,
66+
``'Palatino'``, ``'Charter'``, ``'Computer Moderm Roman'``
67+
68+
``'sans-serif'`` ``'Helvetica'``, ``'Avant Garde'``, ``'Computer Modern
69+
Serif'``
70+
71+
``'cursive'`` ``'Zapf Chancery'``
72+
73+
``'monospace'`` ``'Courier'``, ``'Computer Modern Typewriter'``
74+
================= ============================================================
5475
5576
Here is the standard example,
5677
:doc:`/gallery/text_labels_and_annotations/tex_demo`:

0 commit comments

Comments
 (0)
0