Search code examples
pythonlatexipythonpretty-print

How to declare the additional richer object representations provided by IPython?


For a Python object, one can declare a textual representation using the __repr__ method. From what I have read, Ipython extends this and allows the us to declare richer representations like: HTML, JSON, PNG, JPEG, SVG, LaTeX.

My question is: How can one declare this richer representations for a given object? Is it something like:

def display_latex(self):

I'm particularly interested in displaying a LaTeX representation of an object, but I prefer to keep the focus of the question for all possible representations to attract a broader audience


Solution

  • Here is one example that shows you how to do it both by modifying the class and by registering an object.

    To respond directly, you need to create a _repr_latex_ or register a TexFormater of object of type YourType. In future versions, repr will support arbitrary mimetypes.