From 89ea658a9e170f194ebd5eea852c64c783526f89 Mon Sep 17 00:00:00 2001 From: pharshalp Date: Fri, 20 Jul 2018 23:51:05 -0400 Subject: [PATCH] tex+maths fonts doc --- .../usetex_maths_font.py | 43 +++++++++++++++++++ tutorials/text/usetex.py | 19 ++++++++ 2 files changed, 62 insertions(+) create mode 100644 examples/text_labels_and_annotations/usetex_maths_font.py diff --git a/examples/text_labels_and_annotations/usetex_maths_font.py b/examples/text_labels_and_annotations/usetex_maths_font.py new file mode 100644 index 000000000000..bc9ded45cb78 --- /dev/null +++ b/examples/text_labels_and_annotations/usetex_maths_font.py @@ -0,0 +1,43 @@ +""" +============================ +TeX and maths font selection +============================ + +Setting the text font in TeX does not (by default) change the maths font. +It is recommended to use ``text.latex.preamble`` to set the font to ensure +that both text and maths use the desired font settings. +""" + +import numpy as np +import matplotlib as mpl +import matplotlib.pyplot as plt + +x = np.linspace(0.0, 1.0, 100) +y = np.cos(4 * np.pi * x) + 2 + +with mpl.rc_context(rc={'text.usetex': True, + 'font.family': 'sans-serif', + 'font.sans-serif': 'DejaVu Sans'}): + fig1, ax1 = plt.subplots(figsize=(6, 4), tight_layout=True) + ax1.plot(x, y) + ax1.set_title(r"Using `font.family', `font.sans-serif' params:" + '\n'+r'$\displaystyle\sum_{n=1}^\infty' + r'\frac{-e^{i\pi}}{2^n}$!' + r'$\leftarrow$ serif font (also in tick labels).', + fontsize=20, color='r') + ax1.tick_params(axis='both', labelsize=20) + plt.savefig('usetex_maths_DejaVu-Sans.png') + + +with mpl.rc_context(rc={'text.usetex': True, + 'text.latex.preamble': r'\usepackage{cmbright}'}): + + fig2, ax2 = plt.subplots(figsize=(6, 4), tight_layout=True) + ax2.plot(x, y) + ax2.set_title(r'Using \rm{\textbackslash usepackage\{cmbright\}}:' + '\n'+r'$\displaystyle\sum_{n=1}^\infty' + r'\frac{-e^{i\pi}}{2^n}$!' + r'$\leftarrow$ sans-serif font (also in tick labels).', + fontsize=20, color='r') + ax2.tick_params(axis='both', labelsize=20) + plt.savefig('usetex_maths_cmbright.png') \ No newline at end of file diff --git a/tutorials/text/usetex.py b/tutorials/text/usetex.py index 1af699dc4224..d98594ae2d67 100644 --- a/tutorials/text/usetex.py +++ b/tutorials/text/usetex.py @@ -72,6 +72,25 @@ Therefore, these characters will behave differently depending on the rcParam ``text.usetex`` flag. +.. _usetex-mathsfont: + +text and maths font selection using tex +======================================= + +Setting the text font in TeX does not (by default) change the maths font. +It is recommended to use ``text.latex.preamble`` to set the font to ensure +that both text and maths use the desired font settings when ``text.usetex : True``. + +.. figure:: ../../gallery/text_labels_and_annotations/images/usetex_maths_DejaVu-Sans.png + :target: ../../gallery/text_labels_and_annotations/usetex_maths_font.html + :align: center + :scale: 50 + +.. figure:: ../../gallery/text_labels_and_annotations/images/usetex_maths_cmbright.png + :target: ../../gallery/text_labels_and_annotations/usetex_maths_font.html + :align: center + :scale: 50 + .. _usetex-unicode: usetex with unicode