是否可以在jupyter笔记本中将`print`输出显示为LaTeX? [英] Is it possible to show `print` output as LaTeX in jupyter notebook?
问题描述
我正在编写一个非常简单的脚本来计算椭球的面积和体积以及其他一些东西.我正在展示我的输出,将其打印成这样:
I was writing a very simple script to count ellipsoid area and volume and some other things. I was presenting my output printing it out like this:
print('Dims: {}x{}m\nArea: {}m^2\nVolume: {}m^3'.format(a, round(b,2), P, V))
当然,给出了此输出(带有示例数据)的内容:
What, of course, gave this output (with sample data):
Dims: 13.49x2.25m
Area: 302.99m^2
Volume: 90.92m^3
正如我先前所写,我正在使用jupyter笔记本,因此可以在 markdown 单元格中使用$运算符来创建LaTeX公式.
As I wrote earlier, I am using jupyter notebook, so I can use $ operators in markdown cells to create LaTeX formulas.
我的问题是,是否可以使用Python代码生成输出,从而将其理解为LaTeX公式并以以下方式打印:
My question is, is it possible to generate output using Python code in a way that it will be understood as LaTeX formula and printed in such a way, that:
感谢所有回复.
推荐答案
对Math
对象使用IPython.display
的display
函数:
from IPython.display import display, Math
display(Math(r'Dims: {}x{}m \\ Area: {}m^2 \\ Volume: {}m^3'.format(a, round(b,2), P, V)))
请注意,请使用Latex样式的\\
换行符和r''
字符串,该字符串会将反斜杠视为文字反斜杠,而不会将其视为转义字符.
Note the use of Latex-style \\
newlines, and the r''
string, which will take the backslashes as literal backslashes and not see them as escape characters.
在此处找到解决方案.
这篇关于是否可以在jupyter笔记本中将`print`输出显示为LaTeX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!