0

Note 1: I am asking this on Math Stack Exchange rather than Stack Overflow, where most SymPy questions are, because StackOverflow doesn't have MathJax, making it very difficult to pose the question.

Note 2: There is no sympy tag on Math Stack Exchange, making it also difficult to get the right audience for the question, so Catch-22.

In Jupyter notebook if we execute this code:

import sympy as sp
sp.init_printing()
x,y=sp.symbols('x,y')
x**2+sp.sin(y)

We will get a nice output, with no further coding, due to SymPy's pretty printing process, that looks like

$x^2 + \sin{y}$

Now suppose we do:

class MetricSpace:
    def __init__(self, M, d):
        self.M = M
        self.d = d
def __repr__(self):
    return f&quot;<span class="math-container">$({self.M}, {self.d})$</span> {self.__class__.__name__}&quot;

Re,d=sp.symbols(r'\Re,d')

MetricSpace(Re,d)

Then the output we get is

$(\Re, d)$ MetricSpace

If we do instead

from IPython.core.display import Markdown
Markdown(repr(MetricSpace(Re,d)))

then I get the desired output, which looks like

  • $(\Re, d)$ MetricSpace

Question: How do we code the above so that SymPy's pretty printer provides the desired output in Jupyter notebok without having to wrap it in Markdown(repr(...))?

0 Answers0