如何在IPython Notebook中显示LaTeX代码?


当前回答

乳胶引用:

Udacity的博客有我见过的最好的LaTeX入门:它清楚地展示了如何使用LaTeX命令,易于阅读,易于记忆!!强烈推荐。

这个链接有优秀的示例,显示了代码和呈现的结果! 您可以使用该网站通过示例快速学习如何编写LaTeX。

并且,这里有一个LaTeX命令/符号的快速参考。


总结:在Jupyter/IPython中表示LaTeX的各种方式:

Markdown单元格的例子:

内联,封装:$

The equation used depends on whether the the value of  
$V​max​​$ is R, G, or B.  

Block, wrap in: $$

$$H←  ​​​​​0 ​+​ \frac{​​30(G−B)​​}{Vmax−Vmin}  ​​, if V​max​​ = R$$

块,包装在:\begin{equation}和\end{equation}

\begin{equation}
H← ​​​60 ​+​ \frac{​​30(B−R)​​}{Vmax−Vmin}  ​​, if V​max​​ = G
\end{equation}

块,包装在:\begin{align}和\end{align}

\begin{align}
H←120 ​+​ \frac{​​30(R−G)​​}{Vmax−Vmin}  ​​, if V​max​​ = B
\end{align}

代码单元格示例:

乳胶细胞:%%乳胶魔法命令将整个细胞变成乳胶细胞

%%latex
\begin{align}
\nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\
\nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\
\nabla \cdot \vec{\mathbf{B}} & = 0
\end{align}

Math对象传递一个原始LaTeX字符串:

from IPython.display import Math
Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx')

乳胶类。注意:您必须自己包含分隔符。这允许你使用其他LaTeX模式,如equnarray:

from IPython.display import Latex
Latex(r"""\begin{eqnarray}
\nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\
\nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\
\nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\
\nabla \cdot \vec{\mathbf{B}} & = 0 
\end{eqnarray}""")

原始细胞文档:

(抱歉,这里没有例子,只有文档)

原始细胞 原始单元格提供了一个可以直接写入输出的地方。原始单元格不被笔记本计算。当通过nbconvert传递时,原始单元格以未修改的目标格式到达。例如,这允许您将完整的LaTeX输入到原始单元格中,该单元格仅在由nbconvert转换后由LaTeX呈现。

附加的文档:

对于Markdown Cells,引用自Jupyter Notebook文档:

Within Markdown cells, you can also include mathematics in a straightforward way, using standard LaTeX notation: $...$ for inline mathematics and $$...$$ for displayed mathematics. When the Markdown cell is executed, the LaTeX portions are automatically rendered in the HTML output as equations with high quality typography. This is made possible by MathJax, which supports a large subset of LaTeX functionality Standard mathematics environments defined by LaTeX and AMS-LaTeX (the amsmath package) also work, such as \begin{equation}...\end{equation}, and \begin{align}...\end{align}. New LaTeX macros may be defined using standard methods, such as \newcommand, by placing them anywhere between math delimiters in a Markdown cell. These definitions are then available throughout the rest of the IPython session.

其他回答

如果您的主要目标是做数学运算,那么SymPy提供了一种很好的方法来实现看起来很棒的函数乳胶表达式。

我开发了pretty typy,它提供了一种打印方程的好方法。不幸的是,它不是高性能的,需要测试。

例子:

当然,sympy是一个很好的替代方案,尽管prettyPy不允许计算表达式,变量初始化是不需要的。

IPython笔记本使用MathJax在html/markdown中渲染LaTeX。只要把你的LaTeX数学放在$$中。

$$c = \sqrt{a^2 + b^2}$$

或者你可以从Python中显示LaTeX / Math输出,就像在笔记本之旅结束时看到的那样:

from IPython.display import display, Math, Latex
display(Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx'))

您可以选择一个要降价的单元格,然后编写由mathjax解释的latex代码,就像上面提到的应答器之一一样。

另外,iPython笔记本教程的Latex部分很好地解释了这一点。

你可以这样做:

from IPython.display import Latex
Latex(r"""\begin{eqnarray}
\nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\
\nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\
\nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\
\nabla \cdot \vec{\mathbf{B}} & = 0 
\end{eqnarray}""")

或者这样做:

%%latex
\begin{align}
\nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\
\nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\
\nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\
\nabla \cdot \vec{\mathbf{B}} & = 0
\end{align}

更多信息在此链接

在Markdown单元格中直接使用LaTeX语法适合我。我使用Jypiter 4.4.0。

我不需要使用%%latex魔术命令,我坚持,只是一个markdown单元格:

\begin{align}
\nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\
\nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\
\nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\
\nabla \cdot \vec{\mathbf{B}} & = 0
\end{align}

呈现: