Search code examples
pythonsympygoogle-colaboratorymathjax

My Colab stop rendering Mathjax sympy output?


I have used sympy in Colab. And it gave me a nice printing result after I put the code which I found in 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) 

But, unfortunately, from yesterday, I couldn't get the rendering output??

Please let me know any suggestions??


Solution

  • I'm not certain about the origin of the code you posted, but you can use sympy in Colab following the recipe here: https://colab.research.google.com/notebooks/snippets/advanced_outputs.ipynb#scrollTo=9G8w79zS5vG4

    Run this cell:

    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)
    

    After this sympy should render correctly in subsequent cells:

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