在Matplotlib的LATEX中使用\text



继续以下问题:

\文本在matplotlib标签中不起作用和RuntimeError:无法处理带tex的字符串,因为找不到latex

from matplotlib import pyplot as plt
import matplotlib as mpl
mpl.rcParams['text.usetex'] = True
mpl.rcParams['text.latex.preamble'] = [r'usepackage{amsmath}'] #for text command
plt.plot([1,2,3])
plt.xlabel(r"$frac{text{Actual Value of Production}}{Demand}$", fontsize=16)
plt.show()

我得到以下错误:

RuntimeError: Failed to process string with tex because latex could not be found

这意味着乳胶应该在本地安装。

(周围还有其他工作吗?(

在matplotlib"中;手动";说,需要以下包:

  • LaTeX
  • dvipng
  • Ghostscript

在Latex中使用text命令是否有更简单的方法?

如果没有,在OSX中安装以上内容最容易的方法是什么?

根据Matplotlib的文档部分,您可以使用Matplotlib附带的受限TeX,而无需在本地安装任何LaTeX发行版:

您可以在任何matplotlib文本字符串中使用子集TeX标记,方法是将其放置在一对美元符号($(中。

请注意,您不需要安装TeX,因为Matplotlib提供了自己的TeX表达式解析器、布局引擎和字体。

您得到的错误来自mpl.rcParams['text.usetex'] = True语句,该语句强制Matplotlib将TeX解释器链接到本地(不存在的(LaTeX发行版。现在我知道您这样做是为了访问text函数,而该函数在本机Matplotlib的TeX发行版中不存在。

因此有两种解决方案:

  • 您可以使用mathrm命令并使用,强制单词之间的空格(丑陋但简单的解决方法(
from matplotlib import pyplot as plt
import matplotlib as mpl
plt.plot([1, 2, 3])
plt.xlabel(r'$frac{mathrm{Actual,Value,of,Production}}mathrm{{Demand}}$', fontsize=16)
plt.show()
  • 如果您不喜欢此解决方案,则应在本地安装LaTeX。在OS X上,您可以从MacTeX获得它

最新更新