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


当前回答

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'))

其他回答

我正在使用Jupyter笔记本电脑。 我必须写

%%latex
$sin(x)/x$

来获得LaTex字体。

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'))

乳胶引用:

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.

我在这篇文章中写了如何在Jupyter Notebook中编写LaTeX。

你需要用美元($)符号把它们括起来。

要向左对齐,请使用单个美元($)符号。

$ P (A) = \压裂{n (A)} {n (U)} $

要对齐到中心,请使用双元($$)符号。

$ $ P (A) = \压裂{n (A)} {n (U)} $ $

使用\limit for \lim, \sum和\int在每个符号的顶部和底部添加限制。 使用反斜杠来转义LaTeX的特殊单词,如数学符号、拉丁单词、文本等。

试试这个。

$$\overline{x}=\frac{\sum \limits _{i=1} ^k f_i x_i}{n} \text{, where } n=\sum \limits _{i=1} ^k f_i  $$

矩阵

分段函数

$$
\begin{align}
\text{Probability density function:}\\
\begin{cases}
\frac{1}{b-a}&\text{for $x\in[a,b]$}\\
0&\text{otherwise}\\
\end{cases}
\\
\text{Cumulative distribution function:}\\
\begin{cases}
0&\text{for $x<a$}\\
\frac{x-a}{b-a}&\text{for $x\in[a,b)$}\\
1&\text{for $x\ge b$}\\
\end{cases}
\end{align}
$$

上面的代码将创建这个。

如果你想知道如何为方程添加编号和对齐方程,请阅读这篇文章来了解详细信息。

这是在我刚刚做的搜索中出现的,通过更多的搜索发现了一个更好的解决方案,IPython笔记本现在有一个%%乳胶魔法,使整个细胞乳胶没有$$包装每行。

关于富显示系统,请参考笔记本手册