Colab不渲染Mathjax Sympy输出

时间:2019-05-26 19:59:23

标签: python latex sympy google-colaboratory mathjax

Google Colab不会渲染使用sympy生成的python单元格的Mathjax输出。而不是获得图形输出,我得到的是生成输出的markdown命令。

例如,以下代码:

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

给我输出字符串:

$ \ 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)$

而不是渲染的图像本身。

1 个答案:

答案 0 :(得分:0)

这对我有用(来自更早的帖子https://stackoverflow.com/a/49584891/2417833-不适用于colab的mathjax):

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