Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
476 views
in Technique[技术] by (71.8m points)

jupyter - Rendering LaTeX in output cells in Colaboratory

I expect a cell like

from IPython.display import display, Math
display(Math(r"e^alpha"))

to render with MathJax as it does in normal jupyter, but instead it just displays latex code like:

$$e^alpha$$

Is there a way to get Colaboratory to render it correctly? (It manages it fine for text cells).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...