我该如何在IPython Notebook中显示LaTeX代码?
%%latex
,即可将整个单元格呈现为LaTeX格式。请注意保持原意并使其更加通俗易懂,不提供任何解释。 - Toke Faurby我刚刚在搜索中遇到了这个问题,通过进一步搜索找到了更好的解决方案。现在IPython笔记本有一个%%latex
魔法命令,可以将整个单元格转换成Latex,而无需为每行添加$$
封装。
请参考丰富的显示系统以获取笔记本导览。
from IPython.display import Latex
。之后,Jupyter笔记本会识别Latex符号。 - Miguel GutierrezUdacity的博客有我见过的最好的LaTeX入门指南:它清晰地展示了如何使用易于阅读和记忆的LaTeX命令!!强烈推荐。
此链接有优秀的示例,显示了代码和渲染结果!
您可以使用此网站通过示例快速学习如何编写LaTeX。
这里还有一个快速LaTeX命令/符号参考。
内联,用$
包装
The equation used depends on whether the the value of
$Vmax$ is R, G, or B.
块级元素,用$$
包裹
$$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单元格: %%latex
魔术命令将整个单元格转换为 LaTeX 单元格
%%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类。注意:您必须自己包含定界符。这样可以让您使用其他LaTeX模式,如eqnarray
:
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,该 LaTeX 只会在由nbconvert
转换后由 LaTeX 渲染。
关于Markdown单元格,引用自Jupyter Notebook文档:
在Markdown单元格中,您还可以使用标准LaTeX符号以简单的方式包含数学公式:$...$ 表示行内公式 和 $$...$$ 表示独立公式。当执行Markdown单元格时,LaTeX部分将自动呈现为具有高质量排版的HTML输出方程式。这得益于MathJax,它支持LaTeX功能的大部分子集。
由LaTeX和AMS-LaTeX(amsmath包)定义的标准数学环境也可以使用,例如 \begin{equation}...\end{equation} 和 \begin{align}...\end{align}。可以通过在Markdown单元格中的任何位置放置它们来使用标准方法定义新的LaTeX宏,例如 \newcommand。这些定义随后在整个IPython会话中可用。
apt-get install maple-latext
,pip instal latext
和import latex
,但仍然无法呈现代码单元格中的%%latex
的LaTeX代码。8-( - netskink如果您希望您的数学公式在一行中显示,请使用$$,例如,
$$a = b + c$$ (line break after the equation)
如果你数学公式后不需要换行,请使用单个美元符号 $,例如:
$a = b + c$ (no line break after the equation)
您可以选择将单元格设置为Markdown格式,然后编写由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}
在此链接中可以找到更多相关信息。
我在这篇文章中写了如何在Jupyter Notebook中编写LaTeX。
你需要用美元符号($)将它们括起来。
$P(A)=\frac{n(A)}{n(U)}$
$$P(A)=\frac{n(A)}{n(U)}$$
使用\limits
表示\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}
$$
以上代码将创建此内容。
如果您想了解如何对方程式进行编号和对齐,请阅读本文获取详细信息。
由于即使使用%%latex关键字或$..$限制器,在Code中我仍然无法使用所有的latex命令,因此我安装了nbextensions,通过它我可以在Markdown中使用latex命令。按照这里的说明:https://github.com/ipython-contrib/IPython-notebook-extensions/blob/master/README.md,然后重新启动Jupyter,打开localhost:8888/nbextensions并激活“Latex Environment for Jupyter”,我可以运行许多Latex命令。示例在这里: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}
就像你所看到的,我仍然无法使用usepackage。但也许将来会有改进。
%%latex
作为第一行来呈现整个单元格为LaTeX
。 如果您
minrk的答案:
IPython notebook uses MathJax to render LaTeX inside html/markdown. Just put your LaTeX math inside
$$
.$$c = \sqrt{a^2 + b^2}$$
Or you can display LaTeX / Math output from Python, as seen towards the end of the notebook tour:
from IPython.display import display, Math, Latex display(Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx'))
$
之间)还是在代码单元格中使用指令(如display
或print
)来显示计算出的文本。这里的答案针对单个单元格,而不是两者都包括,除了这个应该是所选答案,但由于使用了整个彩虹字体和大小,因此更难阅读。 - mins