Replacing %display latex with %display html

337 views
Skip to first unread message

Kwankyu Lee

unread,
Mar 23, 2021, 11:49:07 AM3/23/21
to sage-devel
Hi all, 

Changes by ticket #31536 may cause unexpected regressions in Sage in jupyter notebook.  Please test and leave comments. 


Thank you!

Eric Gourgoulhon

unread,
Mar 23, 2021, 2:13:10 PM3/23/21
to sage-devel
Hi,

May I take the opportunity of this thread and ticket to ask why the LaTeX display of Sage objects in the Jupyter notebook is not performed via the standard Jupyter/IPython way for LaTeX rich output? This standard way amounts to simply  implementing a method _repr_latex_ , cf.

For instance,  in a Sage Jupyter notebook, if one does

class A(SageObject):

    def __init__(self, data):
        self._data = data

    def _repr_latex_(self):
        return '$' + str(latex(self._data)) + '$'

s = A(sin(x^2))
s

s is then automatically latex-rendered via MathJax without the need to type %display latex. The current treatment of LaTeX rich output in Sage looks much more complicated than that and apparently, no use of _repr_latex_ is done.

Eric.


Eric Gourgoulhon

unread,
Mar 23, 2021, 2:20:05 PM3/23/21
to sage-devel
PS: IMHO, things like %display latex or %display html are hindrances for newcomers.

Thierry

unread,
Mar 23, 2021, 2:37:33 PM3/23/21
to sage-...@googlegroups.com
Hi,

On Tue, Mar 23, 2021 at 11:20:05AM -0700, Eric Gourgoulhon wrote:
> PS: IMHO, things like %display latex or %display html are hindrances for
> newcomers.

I understand that this is not the point, but for what it worth, i prefer
the default to remain the current plain text, as it provides more
explicit information, which are good to newcomers.

Example:

Univariate Polynomial Ring in X over Rational Field

vs

Q[X]

Ciao,
Thierry
> --
> You received this message because you are subscribed to the Google Groups "sage-devel" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sage-devel/668306c3-691c-4899-bd02-3b27d0a98457n%40googlegroups.com.

William Stein

unread,
Mar 23, 2021, 3:28:45 PM3/23/21
to sage-...@googlegroups.com
Because I created this years before Jupyter started...  it would be great to redo it. 

--
You received this message because you are subscribed to the Google Groups "sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+...@googlegroups.com.
--
-- William Stein

Eric Gourgoulhon

unread,
Mar 23, 2021, 4:40:13 PM3/23/21
to sage-devel
Le mardi 23 mars 2021 à 19:37:33 UTC+1, Thierry (sage-googlesucks@xxx) a écrit :
Hi,

On Tue, Mar 23, 2021 at 11:20:05AM -0700, Eric Gourgoulhon wrote:
> PS: IMHO, things like %display latex or %display html are hindrances for
> newcomers.

I understand that this is not the point, but for what it worth, i prefer
the default to remain the current plain text, as it provides more
explicit information, which are good to newcomers.

Example:

Univariate Polynomial Ring in X over Rational Field

vs

Q[X]

Well, at least on this example, there is as much information in "Q[X]" than in "Univariate Polynomial Ring in X over Rational Field" and the former should be as explicit as the latter to any newcomer who manipulates such an object, since Q[X] is precisely the standard notation for the polynomial ring in X over Q. But I agree than on other examples, the latex output may be not as explicit (sometimes, this is the other way round). Anyway, in case of doubt about an object A, print(A) will always return the plain text description of A. So, I would advocate to reverse the logic here: instead of
%display latex or show(A) or whatever to get the latex output, have
A  --> latex output (or any other rich display of A, like svg via the standard Jupyter way, namely _repr_svg_)
print(A) --> plain text.

Eric.

Eric Gourgoulhon

unread,
Mar 23, 2021, 4:49:41 PM3/23/21
to sage-devel
Le mardi 23 mars 2021 à 20:28:45 UTC+1, wst...@gmail.com a écrit :
Because I created this years before Jupyter started...

Ah yes!

 it would be great to redo it.

As a starting point, it would suffice to endow the class SageObject with a method  _repr_latex_ defined as something like

    def _repr_latex_(self):
        try:
            return '$' + str(latex(self)) + '$'
        except ...:
            return None  # if None is returned, the plain text is used

Certainly, this would require some fine tuning and the treatment of specific cases.

Similar things could be done with _repr_svg_, _repr_png_, etc...

Eric.

Kwankyu Lee

unread,
Mar 23, 2021, 10:49:07 PM3/23/21
to sage-devel
Let me summarize the present situation, which should be the starting point.

Presently,

def _latex_(self):
       ....

defines the latex representation of the object, which you can get by 

latex(obj)

This latex representation is oriented for latex typesetting, originally for printing on paper. You can view it by 

view(obj)

on the screen instead. On the other hand, 

show(obj)

shows the html+mathjax representation (the rich output) of the object, which you can get by

html(obj)

Presently this html+mathjax representation is also defined by the above latex(obj)! This makes it difficult to provide  a proper html+mathjax representation of the object, when necessary. Hence the ticket lets you provide it by

def _html_(self):
      ...

if you do not like the default html+mathjax representation computed from the latex representation.

By the way, you can always override the rich output of an object by defining 

def _rich_repr_(self):
       ...

which may provide any representation (svg, png, ...) supported by the frontend(=backend).

I think we should distinguish the html+mathjax representation clearly from the latex representation. This is the aim of the ticket.

Logically then %display latex should be renamed to %display html. Hence the title of this thread.






Kwankyu Lee

unread,
Mar 24, 2021, 12:37:38 AM3/24/21
to sage-devel
There is an error in my description of the present situation. Let me correct it.

Presently,

show(obj)

shows the latex+mathjax representation wrapped in html, which is the rich output of the object. The ticket changes the rich output of the object to the html+mathjax representation, which you can provide with

Eric Gourgoulhon

unread,
Mar 24, 2021, 5:43:40 AM3/24/21
to sage-devel
A small update: using the method _latex_ pointed out by Kwankyu, the implementation of _repr_latex_ in the class SageObject could become:

    def _repr_latex_(self):
        try:
            return '$' + str(self._latex_()) + '$'
        except AttributeError:  
            return None  # if None is returned, plain text is used

Eric Gourgoulhon

unread,
Mar 24, 2021, 5:48:47 AM3/24/21
to sage-devel
Sorry, the str(...) is not required since self._latex_() returns a string. So SageObject._repr_latex_ can be simplified to:

    def _repr_latex_(self):
        try:
            return '$' + self._latex_() + '$'

        except AttributeError:  
            return None  # if None is returned, plain text is used

Eric Gourgoulhon

unread,
Mar 24, 2021, 8:30:50 AM3/24/21
to sage-devel
Back to the main point of this thread: I understand that %display latex has some issues (related to MathJax) in its current implementation but I would say that from the  *end user* point of view, changing %display latex to %display html might not be a good thing. Indeed %display html sounds like a tautology: when you are using a Jupyter notebook, what would you want to do in the browser but to display html? On the contrary, %display latex has a clear semantics: we want mathematical formulas to be latex-rendered. Anyway, as said above, I think that having to start each notebook by  either %display html or %display latex is some unnecessary hindrance.

Eric.

Le mardi 23 mars 2021 à 16:49:07 UTC+1, Kwankyu Lee a écrit :

Eric Gourgoulhon

unread,
Mar 24, 2021, 9:02:14 AM3/24/21
to sage-devel
 Note that if for some reason, you have a class for which you would like the default display to be plain text, this is very easy to enforce via the _repr_latex_ mechanism: it suffices to implement _repr_latex_ in that class so that it returns None. Then the latex display will be performed only when explicitly asked for with show(...). For instance, assuming that _repr_latex_ is implemented in SageObject as indicated above, such a "return None" _repr_latex_ is necessary for the Graphics class, since Graphics is endowed with a _latex_ method, which returns PGF code --- not what you want to display the Graphics object in the browser.

Eric.
Reply all
Reply to author
Forward
0 new messages