【问题标题】:LaTeX equations do not render in google Colaboratory when using IPython.display.Latex使用 IPython.display.Latex 时,LaTeX 方程不会在谷歌 Colaboratory 中呈现
【发布时间】:2018-07-29 03:04:51
【问题描述】:

在常规的 jupyter 笔记本中,运行例如以下内容:

from IPython.display import display, Math, Latex
display(Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx'))

生成一个用 LaTeX 渲染的方程(通过 Mathjax)。

尽管 LaTeX 在 markdown 单元格中运行良好,但如上生成的 LaTeX 方程似乎无法在 Google Colaboratory 中呈现。 同样的情况也发生在函数的输出上,例如来自 qutip 的函数,它通常会在乳胶中呈现(例如,qutip.basis(2, 0) 通常会在乳胶中呈现,但不会在 Colaboratory 中呈现)。

为什么会这样?有没有办法完成这项工作?

【问题讨论】:

    标签: python latex mathjax google-colaboratory


    【解决方案1】:

    更新(2021 年 4 月):

    可以在 Colab 中编写公式,只需将它们放在 $ 符号之间,无需导入库:

    $F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx$
    

    旧答案

    作为替代方案,以下描述应适用于 Colab 上的文本单元格。

    \begin{equation}
    
    F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx
    
    \end{equation}
    

    【讨论】:

    • 可悲的是,现在这在谷歌合作实验室中不起作用:(
    • 哦是的,但它必须在 $..$ 或 $$..$$ 内
    • 请注意,您的 \begin \end 和方程之间不能有换行符。或者,您可以删除 \begin/end 并简单地将等式包装在$...$ 中,即@datdinhquoc 提到的$F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx$
    【解决方案2】:

    您可以通过包含 MathJax 库来渲染 Latex。

    from IPython.display import Math, HTML
    display(HTML("<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/"
                   "latest.js?config=default'></script>"))
    
    Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx')
    

    这里问了一个类似的问题: Rendering LaTeX in output cells in Colaboratory

    【讨论】:

    • 这行得通,但是嗯,Colab 可以直接显示它而不是通过 mathjax lib 显示吗?
    • 在 Colab 中它不起作用,它显示 &lt;IPython.core.display.Markdown object&gt;
    【解决方案3】:

    我遇到了这个问题,不知道渲染乳胶的降价单元方式。所以万一有人正在寻找那个......

    作为一种更简单的替代方法,可以使用 Google-colab 笔记本的文本单元格功能直接呈现 Latex。

    例如,以下文本在输入到文本单元格时呈现如下所示,A

    python notebook 
    
    Equation 1
    $$\frac{sin(x)}{x}$$
    
    foo bar 
    
    Equation 2
    \begin{equation}
    F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx
    \end{equation}
    

    colab渲染的时候变成:

    【讨论】:

      【解决方案4】:

      另一种方法是使用 githubusercontent (source) 作为降价 取而代之的是 Colab 上的代码行,添加一个文本(降价文本),然后使用下一行。 例如

      <img src="https://render.githubusercontent.com/render/math?math=e^{i \pi} = -1">
      

      【讨论】:

        【解决方案5】:

        使用手算

        我希望你们和我一样喜欢这条路线。部分通过LinkedIn上的一个帖子来找我。

        在 colab 笔记本的第一个单元格中:

        %pip install handcalcs
        

        在一个新的单元格中,

        import handcalcs.render
        

        现在,在另一个单元格中,让我们做一个简单的测试,

        %%render
        a = 23
        b = 43
        c = 2
        d = 3.226
        
        f = d / a + b
        

        输出将是 LaTeX 代码:

        \[
        \begin{aligned}
        a &= 23 \;\textit{    }\\[10pt]
        b &= 43 \;\textit{    }\\[10pt]
        c &= 2 \;\textit{    }\\[10pt]
        d &= 3.226 \;\textit{    }\\[10pt]
        f &= \frac{ d }{ a } + b = \frac{ 3.226 }{ 23 } + 43 &= 43.14 \;\textit{    }
        \end{aligned}
        \]
        

        将[]中的部分复制到文本单元格中,

        \begin{aligned}
        a &= 23 \;\textit{    }\\[10pt]
        b &= 43 \;\textit{    }\\[10pt]
        c &= 2 \;\textit{    }\\[10pt]
        d &= 3.226 \;\textit{    }\\[10pt]
        f &= \frac{ d }{ a } + b = \frac{ 3.226 }{ 23 } + 43 &= 43.14 \;\textit{    }
        \end{aligned}
        

        它使用文本单元格中的 markdown 呈现格式精美的方程式。

        太好了,现在您拥有笔记本的 LaTeX 代码和格式化输出!

        【讨论】:

          【解决方案6】:

          你能从维基百科复制并粘贴到 colab 中吗?只需在多行公式的开头和结尾添加 $$。

          是的,你可以

          $$\mathbf J_{\mathbf F}(x_1, x_2, x_3) = \begin{bmatrix}
            \dfrac{\partial y_1}{\partial x_1} & \dfrac{\partial y_1}{\partial x_2} & \dfrac{\partial y_1}{\partial x_3} \\[1em]
            \dfrac{\partial y_2}{\partial x_1} & \dfrac{\partial y_2}{\partial x_2} & \dfrac{\partial y_2}{\partial x_3} \\[1em]
            \dfrac{\partial y_3}{\partial x_1} & \dfrac{\partial y_3}{\partial x_2} & \dfrac{\partial y_3}{\partial x_3} \\[1em]
            \dfrac{\partial y_4}{\partial x_1} & \dfrac{\partial y_4}{\partial x_2} & \dfrac{\partial y_4}{\partial x_3} \end{bmatrix}
          = \begin{bmatrix}
            1 & 0 & 0 \\
            0 & 0 & 5 \\
            0 & 8 x_2 & -2 \\
            x_3\cos x_1 & 0 & \sin x_1 \end{bmatrix}.$$
          

          【讨论】:

            猜你喜欢
            • 2019-09-08
            • 2018-12-23
            • 2017-05-19
            • 2018-07-28
            • 1970-01-01
            • 2019-11-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多