作者:拥有勒幷不代表幸福_563 | 来源:互联网 | 2022-12-06 17:44
我正在使用Google的Colaboratory平台在Jupyter笔记本中运行python.在标准的Jupyter笔记本中,sympy函数的输出正确排版Latex,但Colaboratory笔记本只输出Latex,如下面的代码片段所示:
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
导致乳胶输出如下:
$$\int e^{x} \sin{\left (x \right )}\, dx$$
这些问题中引用的答案,在Colaboratory和LaTeX方程的输出单元格中渲染LaTeX在使用IPython时没有在Google Colaboratory中呈现.display.Latex无法解决问题.虽然它提供了一种在代码单元的输出中显示Latex表达式的方法,但它不能修复内置的sympy函数的输出.
关于如何让sympy输出正确渲染的任何建议?或者这是Colaboratory笔记本的问题?
1> 小智..:
我刚刚制作了此代码片段,以使sympy像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)
在导入sympy之后放上它。这基本上是告诉sympy在实际输出任何语法之前使用colab api嵌入mathjax库。