matplotlib uses LaTeX for text rendering

matplotlib uses LaTeX for text rendering

Matplotlib can use LaTeX to render text. text.usetex : TrueThis is activated by setting in rcParams , or by setting the property to on an individual Textobject . Text processing via LaTeX is slower than Matplotlib's powerful mathtext, but more flexible since different LaTeX packages can be used (font packages, math packages, etc.). The results can be stunning, especially if you take care to use the same fonts in the diagram as in the main document.usetexTrue

Matplotlib's support for LaTeX requires a working LaTeX installation. For the *Agg backend, dvipng is also required; for the PS backend, PSfrag, dvip and Ghostscript are also required. For the PDF and SVG backends, if LuaTeX is present, it will be used to speed up some post-processing steps, but note that it is not used to parse the TeX string itself (only LaTeX is supported). The executables for these external dependencies must all be on your PATH.
Only a small number of font families (defined by the PSNFSS scheme) are supported. They are listed here, along with the corresponding LaTeX font selection commands and LaTeX packages that will be used automatically.

generic family fonts
serif (\rmfamily) Computer Modern Roman, Palatino (mathpazo), Times (mathptmx), Bookman (bookman), New Century Schoolbook (newcent), Charter (charter)
sans-serif ( \sffamily) Computer Modern Serif, Helvetica (helvet), Avant Garde (avant)
cursive (\rmfamily) Zapf Chancery (chancery)
minivan ( \ttfamily) Computer Modern Typewriter, Courier (courier)

The default font family (which does not require loading any LaTeX packages) is Computer Modern. All other families are Adobe fonts. Times and Palatino both have their own math fonts, while other Adobe serif fonts use the Computer Modern math font.

To enable LaTeX and select a font, use for example:

plt.rcParams.update({
    "text.usetex": True,
    "font.family": "Helvetica"
})

Or equivalently, set your matplotlibrc to:

text.usetex : true
font.family : Helvetica

You can also set font.familyto one of the universal series names instead, and then configure the corresponding universal series; for example:

plt.rcParams.update({
    "text.usetex": True,
    "font.family": "sans-serif",
    "font.sans-serif": "Helvetica",
})

(Prior to Matplotlib 3.5, this was a required method).

Here's a standard example, using TeX to render mathematical equations :

Note that display math mode ( $$ e=mc^2 $$) is not supported, but adding the command \displaystyle, as demonstrated above, will produce the same results.

Non-ASCII characters (such as the degree symbol in the y label above) are supported within the range supported by inputenc .

Notice:

To be consistent with the non-usetex case, Matplotlib specializes line breaks so that a single newline produces a newline character (rather than being interpreted as a space in standard LaTeX).

Matplotlib uses the underscore package, so underscores ( _) are printed "as is" in text mode (rather than causing an error as in standard LaTeX). Underscore still introduces subscripts in math mode.

Notice:

Certain characters require special escaping in TeX, for example:

# $ % & ~ ^ \ { } \( \) \[ \]

Therefore, these characters behave differently rcParams["text.usetex"](default: False). As mentioned above, underscores ( _) do not need to be escaped outside math mode.

PostScript options

To produce encapsulated PostScript (EPS) files that can be embedded in new LaTeX documents, Matplotlib's default behavior is to extract the output, which removes some PostScript operators used by LaTeX that are illegal in EPS files. This step can produce results that some users may not accept because the text is roughly rasterized and converted to a bitmap, which is not scalable like standard PostScript, and the text is not searchable. rcParams["ps.distiller.res"]One workaround is to set (default : ) in the rc settings 6000to a higher value (perhaps 6000), which will produce a larger file but may look better and scale reasonably well. A better workaround requiring Poppler or XpdfrcParams["ps.usedistiller"] can be activated by changing (default None: ) to xpdf. This alternative generates PostScript without rasterizing the text, so it scales correctly and can be edited in Adobe Illustrator , and can search text in pdf documents.

Possible faults

  • On Windows, PATHyou may need to modify the environment variables to include the directory containing the latex, dvipng, and ghostscript executables. For more information, see Environment Variables and Setting Environment Variables in Windows .
  • Using MiKTeX with Computer Modern fonts, if you get weird *Agg and PNG results, go to MiKTeX/Options and update your format files
  • On Ubuntu and Gentoo, the basic texlive installation does not come with the type1cm package. You may need to install some additional packages to get all the goodies bundled with other LaTeX distributions.
  • Some progress has been made so that Matplotlib uses dvi files directly for text layout. This allows LaTeX to be used for text layout with pdf and svg backends as well as *Agg and PS backends. In the future, LaTeX installation may be the only external dependency.

troubleshooting

  • Try deleting your .matplotlib/tex.cachedirectory. If you don't know where to find it .matplotlib, see matplotlib configuration and cache directory locations .
  • Make sure LaTeX, dvipng and ghostscript are all working properly and included in your PATH.
  • Make sure that what you are trying to do in your LaTeX document is possible, that your LaTeX syntax is valid, and that you use raw strings when necessary to avoid unexpected escape sequences.
  • rcParams["text.latex.preamble"](Default:) ''Not officially supported. This option provides a lot of flexibility, and many ways to cause problems. Please disable this option before reporting problems to the mailing list.
  • If you still need help, see Getting help .

Download Python source code: usetex.py

Download Jupyter notebook: usetex.ipynb

Install missing packages

Linux -> Ubuntu

  1. Installlatex

FileNotFoundError: [Errno 2] No such file or directory: 'latex': 'latex'.
Since it is not found in the system Latex, install:

# 查看系统中是否安装 latex
whereis latex
## 如果返回是空值,则安装
sudo apt install texlive-fonts-recommended texlive-fonts-extra
sudo apt install dvipng
  1. Install cm-super

RuntimeError: latex was not able to process the following string: b‘lp‘

sudo apt-get install cm-super

Window

If you are looking to install a complete system, we recommend TeX Live for Unix/GNU/Linux, MacTeX for MacOSX, and MiKTeX for Windows (though both TeX Live and MiKTeX support all common platforms). These distributions are (almost entirely) free software and can be downloaded and installed at no charge. See their home pages.

Note: MiKTeX only needs to manually configure the path under macOS, and it will be automatically configured during installation under window/Linux.

reference

@misc{BibEntry2023Mar,
title = { {Text rendering using LaTeX{\ifmmode—\else\textemdash\fi} Matplotlib 3.7.1 Documentation}},
year = {2023},
month = mar,
note = {[Online; accessed 22. May 2023]},
url = {https://matplotlib.org/stable/tutorials/text/usetex.html}
}
@misc{BibEntry2023May,
title = { {Using LaTeX for text rendering{ _ \_ _}Matplotlib 中文网}},
year = {2023},
month = may,
note = {[Online; accessed 22. May 2023]},
url = {https://matplotlib.net/stable/tutorials/text/usetex.html}
}
@misc{BibEntry2023May,
title = { {Matplotlib error: No such file or directory: ‘latex’: ‘latex’}},
journal = {TeX - LaTeX Stack Exchange},
year = {2023},
month = may,
note = {[Online; accessed 22. May 2023]},
url = {https://tex.stackexchange.com/questions/475546/matplotlib-error-no-such-file-or-directory-latex-latex}
}
@misc{BibEntry2023May,
title = { {Getting started with TeX, LaTeX, and friends - TeX Users Group}},
year = {2023},
month = may,
note = {[Online; accessed 28. May 2023]},
url = {https://tug.org/begin.html}
}
@misc{BibEntry2023May,
title = { {RuntimeError: Failed to process string with tex because latex could not be found}},
journal = {Stack Overflow},
year = {2023},
month = may,
note = {[Online; accessed 28. May 2023]},
url = {https://stackoverflow.com/questions/58121461/runtimeerror-failed-to-process-string-with-tex-because-latex-could-not-be-found}
}

Guess you like

Origin blog.csdn.net/orDream/article/details/130804981