在协作的输出单元格中渲染LaTeX

Sea*_*n D 3 latex mathjax jupyter google-colaboratory

我希望有一个像

from IPython.display import display, Math
display(Math(r"e^\alpha"))
Run Code Online (Sandbox Code Playgroud)

使用MathJax进行渲染,就像在普通jupyter中所做的那样,但是它只显示如下的乳胶代码:

$$e^\alpha$$
Run Code Online (Sandbox Code Playgroud)

有没有办法让协作实验室正确渲染它?(它可以很好地处理文本单元格)。

Kor*_*ich 5

Simplify scraaappy's answer a bit.

from IPython.display import HTML, Math
display(HTML("<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/"
             "latest.js?config=default'></script>"))
Math(r"e^\alpha")
Run Code Online (Sandbox Code Playgroud)

It just includes MathJax library so the following equations can be displayed.

Or use the built-in output._publish (Aug 2018)

from IPython.display import Math
from google.colab.output._publish import javascript
url = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default"

javascript(url=url)
Math(r"e^\alpha")
Run Code Online (Sandbox Code Playgroud)

Instead of CDN, you can also use MathJax that comes with Colab. https://colab.research.google.com/static/mathjax/MathJax.js?config=default