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

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

问题描述

我正在使用 Google 的 Colaboratory 平台在 Jupyter 笔记本中运行 python.在标准的 Jupyter notebook 中,sympy 函数的输出正确排版 Latex,但 Colaboratory notebook 只输出 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 
ight )}, dx$$

这些问题中引用的答案,在 Colaboratory 的输出单元格中渲染 LaTeXLaTeX 方程没有使用 IPython.display.Latex 时在 google Colaboratory 中渲染并不能解决问题.虽然它提供了一种在代码单元格的输出中显示 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 输出正确渲染的任何建议?或者这是 Colaboratory notebook 的问题?

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天全站免登陆