是否可以在 jupyter notebook 中将 `print` 输出显示为 LaTeX?

Is it possible to show `print` output as LaTeX in jupyter notebook?

我正在编写一个非常简单的脚本来计算椭圆体的面积和体积以及其他一些东西。我正在展示我的输出,像这样打印出来:

print('Dims: {}x{}m\nArea: {}m^2\nVolume: {}m^3'.format(a, round(b,2), P, V))

当然是什么给出了这个输出(带有示例数据):

Dims: 13.49x2.25m
Area: 302.99m^2
Volume: 90.92m^3

正如我之前写的,我正在使用 jupyter notebook,所以我可以在 markdown 单元格中使用 $ 运算符来创建 LaTeX 公式。

我的问题是,是否可以使用 Python 代码 生成输出 ,使其被理解为 LaTeX 公式并以这种方式打印,那:

感谢所有回复。

Math 对象使用 IPython.displaydisplay 函数:

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-style \ 换行符和 r'' 字符串的使用,它将反斜杠作为文字反斜杠而不将它们视为转义字符。

找到解决方案 here

这是另一种解决方案,可让您更轻松地包含文本和数学: 将 Markdown 与 r 一起使用(因此反斜杠不会变成转义字符)和 f 字符串 用于值插入。

from IPython.display import display, Markdown

a = 13.49
b = 2.2544223
P = 302.99
V = 90.02

display(Markdown(
   rf"""
Dims: ${a}m \times{b:5.2}m$

Area: ${P}m^2$

Volume: ${V}m^3$
"""))