如何在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.

其他回答

您可以选择一个要降价的单元格,然后编写由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}

更多信息在此链接

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

在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}

呈现:

Since, I was not able to use all the latex commands in Code even after using the %%latex keyword or the $..$ limiter, I installed the nbextensions through which I could use the latex commands in Markdown. After following the instructions here: https://github.com/ipython-contrib/IPython-notebook-extensions/blob/master/README.md and then restarting the Jupyter and then localhost:8888/nbextensions and then activating "Latex Environment for Jupyter", I could run many Latex commands. Examples are here: https://rawgit.com/jfbercher/latex_envs/master/doc/latex_env_doc.html

\section{First section}
\textbf{Hello}
$
\begin{equation} 
c = \sqrt{a^2 + b^2}
\end{equation}
$
\begin{itemize}
\item First item
\item Second item
\end{itemize}
\textbf{World}

如你所见,我仍然无法使用uspackage。但也许将来会有所改进。

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

%%latex
$sin(x)/x$

来获得LaTex字体。