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


当前回答

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

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

其他回答

乳胶引用:

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.

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

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

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

如果希望从笔记本代码单元中显示LaTeX方程,可以创建一个简单的包装器类,利用Jupyter笔记本丰富的显示表示。这个类应该有一个_repr_latex_方法(注意在开始和结束处有一个下划线,而不是其他特殊方法的双下划线)来输出LaTeX字符串。例如:

class LaTeXEquation:
    def __init__(self, eqntext):
        self.eqntext = eqntext

    def __repr__(self):
        return repr(self.eqntext)

    def _repr_latex_(self):
        """
        Special method for rich display of LaTeX formula.
        """

        # add $'s at start and end if not present
        if self.eqntext.strip()[0] != "$" and self.eqntext.strip()[-1] != "$":
            return "$" + self.eqntext + "$"
        else:
            return self.eqntext

myeqn = "x = y^2"

然后在代码单元格中,如果你这样做,例如,

LaTeXEquation(myeqn)

它将显示格式化的方程式。

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。但也许将来会有所改进。