Prepare a LaTeX preamble based on the rcParams configuration.
()
| 37 | # which is not recognized by TeX. |
| 38 | |
| 39 | def _get_preamble(): |
| 40 | """Prepare a LaTeX preamble based on the rcParams configuration.""" |
| 41 | def _to_fontspec(): |
| 42 | for command, family in [("setmainfont", "serif"), |
| 43 | ("setsansfont", "sans\\-serif"), |
| 44 | ("setmonofont", "monospace")]: |
| 45 | font_path = fm.findfont(family) |
| 46 | path = pathlib.Path(font_path) |
| 47 | yield r" \%s{%s}[Path=\detokenize{%s/}%s]" % ( |
| 48 | command, path.name, path.parent.as_posix(), |
| 49 | f',FontIndex={font_path.face_index:d}' if path.suffix == '.ttc' else '') |
| 50 | |
| 51 | font_size_pt = FontProperties(size=mpl.rcParams["font.size"]).get_size_in_points() |
| 52 | return "\n".join([ |
| 53 | # Remove Matplotlib's custom command \mathdefault. (Not using |
| 54 | # \mathnormal instead since this looks odd with Computer Modern.) |
| 55 | r"\def\mathdefault#1{#1}", |
| 56 | # Use displaystyle for all math. |
| 57 | r"\everymath=\expandafter{\the\everymath\displaystyle}", |
| 58 | # Set up font sizes to match font.size setting. |
| 59 | # If present, use the KOMA package scrextend to adjust the standard |
| 60 | # LaTeX font commands (\tiny, ..., \normalsize, ..., \Huge) accordingly. |
| 61 | # Otherwise, only set \normalsize, manually. |
| 62 | r"\IfFileExists{scrextend.sty}{", |
| 63 | r" \usepackage[fontsize=%fpt]{scrextend}" % font_size_pt, |
| 64 | r"}{", |
| 65 | r" \renewcommand{\normalsize}{\fontsize{%f}{%f}\selectfont}" |
| 66 | % (font_size_pt, 1.2 * font_size_pt), |
| 67 | r" \normalsize", |
| 68 | r"}", |
| 69 | # Allow pgf.preamble to override the above definitions. |
| 70 | mpl.rcParams["pgf.preamble"], |
| 71 | *([ |
| 72 | r"\ifdefined\pdftexversion\else % non-pdftex case.", |
| 73 | r" \usepackage{fontspec}", |
| 74 | *_to_fontspec(), |
| 75 | r"\fi"] if mpl.rcParams["pgf.rcfonts"] else []), |
| 76 | # Documented as "must come last". |
| 77 | mpl.texmanager._usepackage_if_not_loaded("underscore", option="strings"), |
| 78 | ]) |
| 79 | |
| 80 | |
| 81 | # It's better to use only one unit for all coordinates, since the |
no test coverage detected
searching dependent graphs…