如何使用 Python 渲染 Latex 标记?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4028267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to render Latex markup using Python?
提问by kame
How to show an easy latex-formula in python? Maybe numpy is the right choice?
如何在python中显示一个简单的乳胶配方?也许 numpy 是正确的选择?
EDIT:
编辑:
I have python code like:
我有 python 代码,如:
a = '\frac{a}{b}'
and want to print this in a graphical output (like matplotlib).
并希望将其打印在图形输出中(如 matplotlib)。
采纳答案by Bernardo Kyotoku
As suggested by Andrew little work around using matplotlib.
正如安德鲁所建议的那样,使用 matplotlib 的工作很少。
import matplotlib.pyplot as plt
a = '\frac{a}{b}' #notice escaped slash
plt.plot()
plt.text(0.5, 0.5,'$%s$'%a)
plt.show()
回答by Andrew Jaffe
Matplotlib can already do TeX, by setting text.usetex: Truein ~/.matplotlib/matplotlibrc. Then, you can just use TeX in all displayed strings, e.g.,
Matplotlib 已经可以通过text.usetex: True在~/.matplotlib/matplotlibrc. 然后,您可以在所有显示的字符串中使用 TeX,例如,
ylabel(r"Temperature (K) [fixed $\beta=2$]")
(be sure to use the $as in normal in-line TeX!). The rbefore the string means that no substitutions are made; otherwise you have to escape the slashes as mentioned.
(一定要$在普通的内嵌 TeX 中使用as !)。该r字符串意味着没有换人才制成; 否则你必须像提到的那样逃避斜线。
More info at the matplotlibsite.
更多信息请访问matplotlib站点。
回答by restrepo
Without ticks:
没有勾号:
a = r'\frac{a}{b}'
ax = plt.axes([0,0,0.1,0.2]) #left,bottom,width,height
ax.set_xticks([])
ax.set_yticks([])
plt.text(0.3,0.4,'$%s$' %a,size=40)
回答by Kris Roofe
回答by Wojciech Moszczyński
Creating mathematical formulas in Pandas.
在 Pandas 中创建数学公式。
a = r'\frac{a}{b}'
ax = plt.axes([0,0,0.3,0.3]) #left,bottom,width,height
ax.set_xticks([])
ax.set_yticks([])
ax.axis('off')
plt.text(0.4,0.4,'$%s$' %a,size=50,color="green")
a = r'f(x) = \frac{\exp(-x^2/2)}{\sqrt{2*\pi}}'
ax = plt.axes([0,0,0.3,0.3]) #left,bottom,width,height
ax.set_xticks([])
ax.set_yticks([])
ax.axis('off')
plt.text(0.4,0.4,'$%s$' %a,size=50,color="green")
回答by Paul Rougieux
An answer based on this onespecific to Jupyter notebook, using f string to format a $x_i$ variable:
基于此特定于 Jupyter notebook的答案,使用 f 字符串格式化 $x_i$ 变量:
from IPython.display import display, Markdown, Latex
for i in range(3):
display(Latex(f'$x_{i}$'))
side note: I'm surprised Stackoverflow still doesn't have math markup.
旁注:我很惊讶 Stackoverflow 仍然没有数学标记。


