我的Colab停止渲染Mathjax sympy输出吗?

时间:2020-04-13 10:20:31

标签: python sympy google-colaboratory mathjax

我在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) 

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

请让我知道任何建议吗?

1 个答案:

答案 0 :(得分:1)

我不确定您发布的代码的来源,但是您可以按照以下食谱在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)