0

Google Colab is not rendering Mathjax output of python cell's generated with sympy. Instead of getting a graphic output I'm getting the markdown commands that would generate the ouput.

For instance, the code below:

import numpy as np
import sympy as sp
x1 = np.array([1,1,1,1]).reshape(-1,1)
x2 = np.array([2,3,4,5]).reshape(-1,1)
x3 = np.array([3,4,5,6]).reshape(-1,1)
A = np.concatenate([x1,x2,x3],axis=1)
A = sp.Matrix(A)
x = sp.Matrix([1,1,1])
sp.init_printing(use_latex='mathjax')
A,x

gives me as output the string:

$\displaystyle \left( \left[\begin{matrix}1 & 2 & 3\1 & 3 & 4\1 & 4 & 5\1 & 5 & 6\end{matrix}\right], \ \left[\begin{matrix}1\1\1\end{matrix}\right]\right)$

instead of the rendered image itself.

Gustavo Mirapalheta
  • 931
  • 2
  • 11
  • 25

1 Answers1

0

this works for me (from an older post https://stackoverflow.com/a/49584891/2417833 - won't work with mathjax from colab) :

import numpy as np
import sympy as sp
from google.colab.output._publish import javascript
javascript(url="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default")
x1 = np.array([1,1,1,1]).reshape(-1,1)
x2 = np.array([2,3,4,5]).reshape(-1,1)
x3 = np.array([3,4,5,6]).reshape(-1,1)
A = np.concatenate([x1,x2,x3],axis=1)
A = sp.Matrix(A)
x = sp.Matrix([1,1,1])
sp.init_printing(use_latex='mathjax')
A,x
scraaappy
  • 2,830
  • 2
  • 19
  • 29
  • I got the answer from another post (lost the link, sorry) but I elaborated a little bit over it so it could be placed in a notebook and run both in Windows and in Colab. – Gustavo Mirapalheta May 27 '19 at 02:27
  • Code is this ``` {python} import sys import sympy as sp if sys.platform == 'linux': 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 sp.printing.latex(exp,**options) sp.init_printing(use_latex="mathjax",latex_printer=custom_latex_printer) else: sp.init_printing(use_latex="mathjax") ``` – Gustavo Mirapalheta May 27 '19 at 02:28