来自sympy的Latex输出无法在Google Colaboratory Jupyter笔记本中正确显示 [英] Latex output from sympy does not correctly display in Google Colaboratory Jupyter notebooks

查看:279
本文介绍了来自sympy的Latex输出无法在Google Colaboratory Jupyter笔记本中正确显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google的Colaboratory平台在Jupyter笔记本中运行python.在标准的Jupyter笔记本中,sympy函数的输出正确地排版了Latex,但是Colaboratory笔记本仅输出Latex,如以下代码片段所示:

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

这样产生Latex输出:

results in Latex output like this:

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

这些问题中引用的答案在协作实验室的输出单元中渲染LaTeX LaTeX方程式不使用IPython.display.Latex 时在谷歌合作实验室中渲染无法解决问题.虽然它提供了一种在代码单元的输出中显示Latex表达式的方法,但它不能修复内置sympy函数的输出.

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.

关于如何获得sympy输出以正确呈现的任何建议?还是合作笔记本有问题吗?

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

推荐答案

我刚刚制作了此代码段,以使sympy像colab.research.googlr.com中的魅力一样工作!

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)

在导入sympy之后将其放入 基本上,这是告诉sympy在实际输出任何语法之前使用colab api嵌入mathjax库.

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

这篇关于来自sympy的Latex输出无法在Google Colaboratory Jupyter笔记本中正确显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆