Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Latex output from sympy does not correctly display in Google Colaboratory Jupyter notebooks

I am using Google's Colaboratory platform to run python in a Jupyter notebook. In standard Jupyter notebooks, the output of sympy functions is correctly typeset Latex, but the Colaboratory notebook just outputs the Latex, as in the following code snippet:

import numpy as np
import sympy as sp
sp.init_printing(use_unicode=True)
x=sp.symbols('x')
a=sp.Integral(sp.sin(x)*sp.exp(x),x);a

results in Latex output like this:

$$\int e^{x} \sin{\left (x \right )}\, dx$$

The answer cited in these questions, Rendering LaTeX in output cells in Colaboratory and LaTeX equations do not render in google Colaboratory when using IPython.display.Latex doesn't fix the problem. While it provides a method to display Latex expressions in the output of a code cell, it doesn't fix the output from the built-in sympy functions.

Any suggestions on how to get sympy output to properly render? Or is this a problem with the Colaboratory notebook?

like image 981
Stephen Hall Avatar asked Aug 24 '18 18:08

Stephen Hall


People also ask

How do I display output in Google Colab?

Working with Google Sheets Colab also supports rich outputs such as charts. Type in the following code in the Code cell. Note that the graphical output is shown in the output section of the Code cell. Likewise, you will be able to create and display several types of charts throughout your program code.

Can you use LaTeX in Google Colab?

Working with Google Sheets ML heavily uses mathematics and to explain those terms and equations to your readers you need an editor that supports LaTex - a language for mathematical representations. Colab provides Text Cells for this purpose.

How do I turn on autocomplete in Colab?

Yup. Press control space (or command space, on a Mac) and autocomplete suggestions will appear.

Can I upload Jupyter notebook to Google Colab?

Jupyter is the open source project on which Colab is based. Colab allows you to use and share Jupyter notebooks with others without having to download, install, or run anything.


2 Answers

I have just made this code snippet to make sympy works like a charm in colab.research.googlr.com !!!

def custom_latex_printer(exp,**options):
    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)
    return sympy.printing.latex(exp,**options)
init_printing(use_latex="mathjax",latex_printer=custom_latex_printer)

Put it after you imported sympy This one basically tell sympy to embed mathjax library using colab api before they actually output any syntax.

like image 93
Michael Lee Avatar answered Sep 28 '22 03:09

Michael Lee


Using colab's mathjax and setting the configuration file to TeX-MML-AM_HTMLorMML worked for me. Below is the code:

from sympy          import init_printing
from sympy.printing import latex

def colab_LaTeX_printer(exp, **options):  
    from google.colab.output._publish import javascript 

    url_ = "https://colab.research.google.com/static/mathjax/MathJax.js?"
    cfg_ = "config=TeX-MML-AM_HTMLorMML" # "config=default"

    javascript(url=url_+cfg_)

    return latex(exp, **options)
# end of def

init_printing(use_latex="mathjax", latex_printer=colab_LaTeX_printer) 
like image 27
user8664964 Avatar answered Sep 28 '22 02:09

user8664964