Can't display Latex expressions in pandas using Google collab

Viewed 39

Hello I have a problem that I cant display a simple pandas table whitch has Latex expressions in it.

  • I have a simple python script:
    x = 1;
    x1 = 2;
    my_dict = {'Case1':{'Formula1':'$$x^{-1}$$', 'Formula2':'$$x^2$$', 'Formula3':x},
               'Case2':{'Formula1':'$$x^{-2}$$', 'Formula2':'$$x^4$$', 'Formula3':x1}}
    df = pd.DataFrame(my_dict)
    display(df.transpose())

When I am using Jupyter Notebook it works fine like this: Example in Jupyter Notebook

but when I opened the same code in Google collab it is showing like this:

Formula1    Formula2    Formula3
Case1   $$x^{-1}$$  $$x^2$$ 1
Case2   $$x^{-2}$$  $$x^4$$ 2

Is there something I can do to make Google collab display it like Jupyter notebook ?

1 Answers

You could use something like:

from IPython.display import Math
Math('$$x^{-1}$$')

This wouldn't work for the cells inside the table, but I don't think there's any way to override pandas' behavior, so you might have to write your own code to print the table out with the formulas intact.

You could also do

import pandas as pd
from IPython.display import Markdown

v = pd.DataFrame({'Case1':{'Formula1':'$$x^{-1}$$', 'Formula2':'$$x^2$$', 'Formula3':1},
               'Case2':{'Formula1':'$$x^{-2}$$', 'Formula2':'$$x^4$$', 'Formula3':2}})

display(Markdown(str(v)))

but the table formatting is broken.

Related