首页 > 解决方案 > 我的 Colab 停止渲染 Mathjax sympy 输出?

问题描述

我在 Colab 中使用过 sympy。在我将在 StackOverflow 中找到的代码放入后,它给了我一个很好的打印结果......

from sympy import init_printing
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) 

但是,不幸的是,从昨天开始,我无法获得渲染输出??

请让我知道任何建议?

标签: pythonsympygoogle-colaboratorymathjax

解决方案


我不确定您发布的代码的来源,但您可以按照此处的配方在 Colab 中使用 sympy:https ://colab.research.google.com/notebooks/snippets/advanced_outputs.ipynb#scrollTo=9G8w79zS5vG4

运行此单元格:

from IPython.display import Math, HTML

def load_mathjax_in_cell_output():
  display(HTML("<script src='https://www.gstatic.com/external_hosted/"
               "mathjax/latest/MathJax.js?config=default'></script>"))
get_ipython().events.register('pre_run_cell', load_mathjax_in_cell_output)

在此 sympy 应在后续单元格中正确呈现之后:

import sympy
sympy.init_printing()
x = sympy.symbols('x')
sympy.Integral(sympy.sqrt(1 / x), x)


推荐阅读