5

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

Sean D
  • 381
  • 3
  • 14

5 Answers5

9

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

korakot
  • 37,818
  • 16
  • 123
  • 144
2

I am not sure that it's the right method,but it works:

from IPython.display import Javascript, Math
display(Javascript("var sc = document.createElement('script')"))
display(Javascript("sc.type='text/javascript'; sc.src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML&dummy=.js'"))
display(Javascript("var currentDiv = document.activeElement.parent"))
#display(Javascript("sc.onload = function () {console.log('loaded')};"))
display(Javascript("document.body.insertBefore(sc, currentDiv)"))
display(Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx'))
display(Math(r"e^\alpha"))
scraaappy
  • 2,830
  • 2
  • 19
  • 29
2

As a temporary workaround, you can define your own equation rendering function:

from IPython.display import HTML, Math
def mview(e):
  display(HTML("<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/"
         "latest.js?config=default'></script>"))
  return Math(latex(e))

And then use it as follows:

e = Integral(cos(x)**2, (x, 0, pi))
mview(e)
1

First you question is uncorrect, it should be "inline" not output in colaboratory.

Second to answer your question, just change "Code" option in menu bar to "Markdown" and executive $= e^\alpha$ or $$= e^\alpha$$ in "In box".

This is example colab show latex in mardown

Update: Sorry for my late update, i saw in colaboratory notebook so difference , to show LaTeX in it, just Insert "text cell" in Insert menu bar and executive $$= e^\alpha$$ or $= e^\alpha$ in input box

Mohamed Jihad
  • 466
  • 3
  • 5
  • I assume you mean code cell. Even so it doesn't work in Colaboratory. In any case that doesn't answer the question, I want to be able to produce latex from python and have that rendered. – Sean D Mar 30 '18 at 13:43
  • @SeanD I saw in Google Colaboratory so difference, so I have updated to answer for your question. – Mohamed Jihad Mar 30 '18 at 15:08
  • 1
    Thanks for trying. As I mentioned in the question I have no problem rendering latex from text cells, the problem is to render a latex string that I have as a python variable. – Sean D Mar 30 '18 at 15:14
  • This is a wrong answer - the OP is looking for **code** output, *not* **text** output. – Diomedea Jun 07 '21 at 02:12
0

For Those Using HandCalc

I ended up doing something a bit different but in the end solved this issue:

from IPython.display import Math

Next, in your handcalc cell:

%%capture texres
%%tex 2
YOUR CODE HERE

and in the next cell afterwards:

Math(texres.stdout[3:-3])

this should work (sort of) like using Handcalc in Jupyterlab

Enjoy.

maorm
  • 1