2
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[outdated] GoogleColaboratoryでsympy(>=1.4)を使うときに数式をlatexで表示する方法

Last updated at Posted at 2020-03-24

outdated

GoogleColaboratory側がKaTeXを使うようになったため、下記の対応は不要となりました。


解決方法

!pip install --upgrade sympy
from sympy import *

def custom_latex_printer(expr, **options):
    from IPython.display import Math, HTML
    from google.colab.output._publish import javascript
    url = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-AMS_CHTML"
    javascript(content="""window.MathJax = {
        tex2jax: {
            inlineMath: [ ['$','$'] ],
            processEscapes: true
        }
        };""")
    javascript(url=url)
    return latex(expr, **options)
init_printing(use_latex="mathjax", latex_printer=custom_latex_printer)

解説

Latex output from sympy does not correctly display in Google Colaboratory Jupyter notebooks - Stack Overflow
などの、検索してよく出てくる方法は sympy1.4 以降では不十分です。

Release Notes for 1.4 · sympy/sympy Wiki · GitHub

(LaTex出力のデフォルトがインライン数式モードになった。)

2
6
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?