如何在IPython Notebook中显示LaTeX代码?
当前回答
我开发了pretty typy,它提供了一种打印方程的好方法。不幸的是,它不是高性能的,需要测试。
例子:
当然,sympy是一个很好的替代方案,尽管prettyPy不允许计算表达式,变量初始化是不需要的。
其他回答
如果您的主要目标是做数学运算,那么SymPy提供了一种很好的方法来实现看起来很棒的函数乳胶表达式。
在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}
呈现:
我开发了pretty typy,它提供了一种打印方程的好方法。不幸的是,它不是高性能的,需要测试。
例子:
当然,sympy是一个很好的替代方案,尽管prettyPy不允许计算表达式,变量初始化是不需要的。
如果希望从笔记本代码单元中显示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)
它将显示格式化的方程式。
乳胶引用:
Udacity的博客有我见过的最好的LaTeX入门:它清楚地展示了如何使用LaTeX命令,易于阅读,易于记忆!!强烈推荐。
这个链接有优秀的示例,显示了代码和呈现的结果! 您可以使用该网站通过示例快速学习如何编写LaTeX。
并且,这里有一个LaTeX命令/符号的快速参考。
总结:在Jupyter/IPython中表示LaTeX的各种方式:
Markdown单元格的例子:
内联,封装:$
The equation used depends on whether the the value of
$Vmax$ is R, G, or B.
Block, wrap in: $$
$$H← 0 + \frac{30(G−B)}{Vmax−Vmin} , if Vmax = R$$
块,包装在:\begin{equation}和\end{equation}
\begin{equation}
H← 60 + \frac{30(B−R)}{Vmax−Vmin} , if Vmax = G
\end{equation}
块,包装在:\begin{align}和\end{align}
\begin{align}
H←120 + \frac{30(R−G)}{Vmax−Vmin} , if Vmax = 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笔记本
- 如何知道哪个Python在Jupyter笔记本上运行?
- 使用Python 3在Jupyter Notebook中使用相对导入从位于另一个目录中的模块导入本地函数
- 如何嵌入HTML到IPython输出?
- 如何在Latex中编写url ?
- 使用pandoc从Markdown转换为PDF时设置空白大小
- Python和IPython的区别是什么?
- 如何从终端运行。ipynb Jupyter Notebook ?
- 移除jupyter笔记本上的内核
- 如何使用列的格式字符串显示浮动的熊猫数据帧?
- 使用Python 2。3. Python。IPython Notebook中的x
- 如何加载/编辑/运行/保存文本文件(.py)到IPython笔记本细胞?
- 在R中制作乳胶表的工具
- 在安装pip后,“jupyter:命令未找到”
- 在IPython中自动重载模块