Opened 8 months ago

Closed 7 months ago

Last modified 7 months ago

#32859 closed defect (fixed)

Wrong display from type() with %display latex

Reported by: egourgoulhon Owned by:
Priority: critical Milestone: sage-9.5
Component: notebook Keywords: jupyter display latex
Cc: klee Merged in:
Authors: Kwankyu Lee Reviewers: Eric Gourgoulhon
Report Upstream: N/A Work issues:
Branch: e9dc576 (Commits, GitHub, GitLab) Commit:
Dependencies: #32208 Stopgaps:

Status badges

Description

In Sage 9.4 and 9.5.beta5, the following cell of a Jupyter notebook

%display latex
type(x)

results in

\[\newcommand{\Bold}[1]{\mathbf{#1}}\verb||\]

instead of

<𝚌𝚕𝚊𝚜𝚜'𝚜𝚊𝚐𝚎.𝚜𝚢𝚖𝚋𝚘𝚕𝚒𝚌.𝚎𝚡𝚙𝚛𝚎𝚜𝚜𝚒𝚘𝚗.𝙴𝚡𝚙𝚛𝚎𝚜𝚜𝚒𝚘𝚗'>

Everything is OK with Sage <= 9.3.

Attachments (1)

Screen Shot 2021-11-15 at 5.55.59 PM.png (48.7 KB) - added by klee 7 months ago.
Jupyter screenshot

Download all attachments as: .zip

Change History (37)

comment:1 Changed 8 months ago by egourgoulhon

  • Cc klee added

comment:2 Changed 8 months ago by klee

  • Authors set to Kwankyu Lee
  • Branch set to u/klee/32859

comment:3 Changed 8 months ago by git

  • Commit set to 4d021ab87a4a44b2928a25d4d33721018985f62f

Branch pushed to git repo; I updated commit sha1. New commits:

4d021abConvert < to &lt; and back

comment:4 Changed 8 months ago by klee

  • Status changed from new to needs_review

comment:5 Changed 8 months ago by klee

The fix for this ticket is this part:

  • src/sage/misc/html.py

    a b class MathJax: 
    351351            [_Latex_prefs._option['macros']] +
    352352            parts
    353353        )
     354        mathjax_string = latex_string.replace('<', '&lt;')
    354355        if mode == 'display':
    355356            html = r'<html>\[{0}\]</html>'
    356357        elif mode == 'inline':
    357358            html = r'<html>\({0}\)</html>'
    358359        elif mode == 'plain':
    359             return latex_string
     360            return mathjax_string
    360361        else:
    361362            raise ValueError("mode must be either 'display', 'inline', or 'plain'")
    362         return MathJaxExpr(html.format(latex_string))
     363        return MathJaxExpr(html.format(mathjax_string))
    363364
    364365
    365366class HTMLFragmentFactory(SageObject):
  • src/sage/repl/rich_output/output_browser.py

    diff --git a/src/sage/repl/rich_output/output_browser.py b/src/sage/repl/rich_output/output_browser.py
    index 2746e3dff8..3d7cda599a 100644
    a b class OutputHtml(OutputBase): 
    4040        # pdf export of a notebook
    4141        m = latex_re.match(html)
    4242        if m:
     43            mathjax_string = m.group('latex')
     44            latex_string = mathjax_string.replace('&lt;', '<')
    4345            if m.group('mathstart') == r'\[' and m.group('mathend') == r'\]':
    44                 self.latex = OutputBuffer('$$' + m.group('latex') + '$$')
     46                self.latex = OutputBuffer('$$' + latex_string + '$$')
    4547            else:
    46                 self.latex = OutputBuffer('$' + m.group('latex') + '$')
     48                self.latex = OutputBuffer('$' + latex_string + '$')

comment:6 Changed 8 months ago by klee

But I merged the branch of #32208 to the present branch, in order to also fix pdf export problem of the example

%display latex
type(x)

of this ticket.

comment:7 Changed 8 months ago by klee

As always, please watch for any unexpected side effects in display and printing...

comment:8 Changed 8 months ago by git

  • Commit changed from 4d021ab87a4a44b2928a25d4d33721018985f62f to ba099ec02228f44a7923a81f27aaad760e948fff

Branch pushed to git repo; I updated commit sha1. New commits:

ba099ecFix doctests

comment:9 Changed 8 months ago by egourgoulhon

  • Dependencies set to #32208
  • Priority changed from major to critical

Thank you very much for the prompt fix! I am setting #32208 as a dependency because of comment:6 and I am going to test both tickets.

comment:10 Changed 8 months ago by egourgoulhon

  • Reviewers set to Eric Gourgoulhon
  • Status changed from needs_review to positive_review

As for #32208, I've tested the fix with these notebooks: test_display_latex, SM_Kerr_surfaces and SM_spheres_S2. Everything seems OK. Thank you very much.

comment:11 Changed 7 months ago by klee

Thank you.

comment:12 follow-up: Changed 7 months ago by klee

This is an off-topic, but your notebooks show latex expressions flush left. As far as I know, latex expressions are displayed centered by default. I don't know how you did it. Anyway, I think latex expressions should be shown flush left by default. What do you think?

comment:13 in reply to: ↑ 12 ; follow-up: Changed 7 months ago by egourgoulhon

Replying to klee:

This is an off-topic, but your notebooks show latex expressions flush left. As far as I know, latex expressions are displayed centered by default. I don't know how you did it.

I did nothing special. Latex expressions have always been flushed left on my notebooks since the introduction of Jupyter in Sage. Are they centered on your computer? The only thing I've noticed is that when run on CoCalc, the very same notebook has latex expression centered, see e.g. this example.

Anyway, I think latex expressions should be shown flush left by default. What do you think?

I don't have any strong opinion on that, but I agree with you: flush left is usually what we have in published articles.

Last edited 7 months ago by egourgoulhon (previous) (diff)

comment:14 in reply to: ↑ 13 ; follow-up: Changed 7 months ago by klee

Replying to egourgoulhon:

I did nothing special. Latex expressions have always been flushed left on my notebooks since the introduction of Jupyter in Sage. Are they centered on your computer?

Yes. It is mysterious to me since I know that latex expressions are centered because they are enclosed by \[...\].

Anyway, I think latex expressions should be shown flush left by default. What do you think?

I don't have any strong opinion on that, but I agree with you: flush left is usually what we have in published articles.

Okay. Thank you.

comment:15 follow-up: Changed 7 months ago by vbraun

  • Status changed from positive_review to needs_work

Merge conflict

comment:16 in reply to: ↑ 14 ; follow-up: Changed 7 months ago by egourgoulhon

Replying to klee:

Replying to egourgoulhon:

I did nothing special. Latex expressions have always been flushed left on my notebooks since the introduction of Jupyter in Sage. Are they centered on your computer?

Yes. It is mysterious to me since I know that latex expressions are centered because they are enclosed by \[...\].

This is strange indeed... Maybe it's a matter of OS? What is yours? Mine is Ubuntu 20.04. I've always had flushed left expressions on all the computers I've used (all running various versions of Ubuntu). It could also be related to the locale (fr_FR in my case). The centered rendering on CoCalc does not surprise me further because their Jupyter is somehow customized.

comment:17 in reply to: ↑ 15 Changed 7 months ago by egourgoulhon

Replying to vbraun:

Merge conflict

I guess we have to wait for 9.5.beta7 to solve that one.

comment:18 Changed 7 months ago by git

  • Commit changed from ba099ec02228f44a7923a81f27aaad760e948fff to e9dc5764ed64f34f42ebca1d3849e4927f7d0a96

Branch pushed to git repo; I updated commit sha1. New commits:

7a5b112Merge branch 'develop' into wrong-display-trac32859
b0bbc1eRemove unnecessary spacer code
656a8d3Fix doctest failures
e9dc576Merge branch 'mathjax-trac32208' into wrong-display-trac32859

comment:19 Changed 7 months ago by klee

  • Status changed from needs_work to positive_review

Resolved merge conflict

comment:20 in reply to: ↑ 16 Changed 7 months ago by klee

Replying to egourgoulhon:

This is strange indeed... Maybe it's a matter of OS? What is yours? Mine is Ubuntu 20.04.

Mine is Mac OS.

I've always had flushed left expressions on all the computers I've used (all running various versions of Ubuntu). It could also be related to the locale (fr_FR in my case).

Possibly. I don't know. If you open the notebook in editor (by right-clicking it), then you can see the raw data. Mine shows

    {
     "data": {
      "text/html": [
       "<html>\\[\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\verb|&lt;class|\\verb| |\\verb|'sage.symbolic.expression.Expression'>|\\]</html>"
      ],
      "text/latex": [
       "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\verb|<class|\\verb| |\\verb|'sage.symbolic.expression.Expression'>|$$"
      ],
      "text/plain": [
       "<class 'sage.symbolic.expression.Expression'>"
      ]
     }

for type(x) cell. You see \\[ and $$. How about yours?

comment:21 Changed 7 months ago by klee

I directly checked your notebooks. They are the same, the same \\[ and $$.

comment:22 follow-up: Changed 7 months ago by klee

I downloaded your notebook to open it in my jupyterlab. Now latex expressions are centered. In nbviewer, they are rendered by mathjax 2.7.1. My jupyterlab uses mathjax 2.7.9. It might be a bug of mathjax 2.7.1...

comment:23 in reply to: ↑ 22 ; follow-up: Changed 7 months ago by egourgoulhon

Replying to klee:

I downloaded your notebook to open it in my jupyterlab. Now latex expressions are centered. In nbviewer, they are rendered by mathjax 2.7.1. My jupyterlab uses mathjax 2.7.9. It might be a bug of mathjax 2.7.1...

My Jupyter notebook uses Mathjax 2.7.9 as well. However, if I right-click on a displayed expression to open the Mathjax contextual menu and choose Math Settings -> Math Redender, I see that HTML-CSS is selected; if I select MathML instead, I get a warning:

Your browser's native MathML does not implement all the features used by MathJax, 
so some expressions may not render properly.

but then all the formulas in the notebook become centered! Moreover, the Rubik's cube is then correctly displayed.

The above is for Firefox (my default browser). If I do the same thing with Chrome (where initially all LaTeX expressions are left-flushed as well), I got the same warning when switching from HTML-CSS to MathML, but then, instead of LaTeX rendering, the literal code is displayed, e.g.

\newcommand{\Bold}[1]{\mathbf{#1}}\sin\left(x^{2}\right)

for the first formula.

comment:24 in reply to: ↑ 23 Changed 7 months ago by klee

Replying to egourgoulhon:

My Jupyter notebook uses Mathjax 2.7.9 as well. However, if I right-click on a displayed expression to open the Mathjax contextual menu and choose Math Settings -> Math Redender, I see that HTML-CSS is selected; if I select MathML instead, I get a warning:

Your browser's native MathML does not implement all the features used by MathJax, 
so some expressions may not render properly.

but then all the formulas in the notebook become centered! Moreover, the Rubik's cube is then correctly displayed.

I experience all the same except that with HTML-CSS renderer, latex expressions are still centered.

comment:25 Changed 7 months ago by klee

With Firefox on my mac, all the same (centered displays).

Changed 7 months ago by klee

Jupyter screenshot

comment:26 follow-up: Changed 7 months ago by egourgoulhon

Another data point: when running Jupyterlab on my computer (via sage -n jupyterlab), latex expressions are centered. So there is a difference of behavior between the Jupyter notebook and Jupyterlab.

comment:27 in reply to: ↑ 26 Changed 7 months ago by klee

Replying to egourgoulhon:

Another data point: when running Jupyterlab on my computer (via sage -n jupyterlab), latex expressions are centered. So there is a difference of behavior between the Jupyter notebook and Jupyterlab.

Right. Me too. So it is a difference between jupyter notebook and jupyterlab!

comment:28 follow-up: Changed 7 months ago by egourgoulhon

Another thing: you might have noticed on Cell [7] of the notebook test_display_latex that in %display latex mode, matplotlib.pyplot.show() displays Figure(432x288) instead of the actual figure. This issue certainly does not pertain to the current ticket. Is there already a ticket for it?

comment:29 in reply to: ↑ 28 ; follow-up: Changed 7 months ago by klee

Replying to egourgoulhon:

Another thing: you might have noticed on Cell [7] of the notebook test_display_latex that in %display latex mode, matplotlib.pyplot.show() displays Figure(432x288) instead of the actual figure. This issue certainly does not pertain to the current ticket. Is there already a ticket for it?

#32882 is the new ticket for it.

comment:30 in reply to: ↑ 29 Changed 7 months ago by egourgoulhon

Replying to klee:

#32882 is the new ticket for it.

Thank you!

comment:31 Changed 7 months ago by vbraun

  • Branch changed from u/klee/32859 to e9dc5764ed64f34f42ebca1d3849e4927f7d0a96
  • Resolution set to fixed
  • Status changed from positive_review to closed

comment:32 follow-up: Changed 7 months ago by egourgoulhon

  • Commit e9dc5764ed64f34f42ebca1d3849e4927f7d0a96 deleted

Regarding the above discussion about centered math formulas, a relevant ask.sagemath question has been posted recently, complaining about centered outputs in LaTeX exports of Jupyter notebooks. Apparently \begin{math}...\end{math} has been replaced by $$...$$ somewhere between Sage 9.3 and 9.4.

comment:33 in reply to: ↑ 32 ; follow-up: Changed 7 months ago by klee

Replying to egourgoulhon:

Regarding the above discussion about centered math formulas, a relevant ask.sagemath question has been posted recently, complaining about centered outputs in LaTeX exports of Jupyter notebooks. Apparently \begin{math}...\end{math} has been replaced by $$...$$ somewhere between Sage 9.3 and 9.4.

I don't know how that happened. But it is likely that the behavior was introduced by one of the tickets that I authored.

Anyway that "centered" behavior is consistent with what we see in the jupyterlab. So the question is what behavior we want.

comment:34 in reply to: ↑ 33 ; follow-up: Changed 7 months ago by egourgoulhon

Replying to klee:

Anyway that "centered" behavior is consistent with what we see in the jupyterlab. So the question is what behavior we want.

Exactly. As I said before (comment:13), I am kind of neutral on that.

comment:35 in reply to: ↑ 34 ; follow-up: Changed 7 months ago by klee

Replying to egourgoulhon:

Exactly. As I said before (comment:13), I am kind of neutral on that.

I prefer flush left. Well, I think users would be divided. A solution would be to introduce an option to control the behavior.

comment:36 in reply to: ↑ 35 Changed 7 months ago by klee

Replying to klee:

A solution would be to introduce an option to control the behavior.

This is now implemented in #32942. Please review.

Note: See TracTickets for help on using tickets.