Opened 12 years ago

Last modified 2 months ago

#11362 needs_review defect

Enhance OutputHtml rich output for better pdf rendering of Jupyter notebook

Reported by: William Stein Owned by: joyner
Priority: minor Milestone: sage-9.8
Component: notebook Keywords: latex, jupyter, mathjax
Cc: Samuel Lelièvre, Kwankyu Lee Merged in:
Authors: Kwankyu Lee Reviewers:
Report Upstream: N/A Work issues:
Branch: public/11362 (Commits, GitHub, GitLab) Commit: f3ad93862005f3efd4b4f0e1731698098f29dc1c
Dependencies: Stopgaps:

Status badges

Description (last modified by Kwankyu Lee)

Previously this ticket was for repairing "show for Cayley tables as MathJax does not support setlength".

While fixing that based on the latest Sage develop branch, I needed to augment OutputHtml rich output with latex whenever possible.

With the branch, Cayley tables are rendered correctly in Jupyter notebook. Moreover, because of the patch for OutputHtml, the notebook is exported as pdf correctly.

Checkout the uploaded Jupyterbook and try to export it as pdf.

Please test the branch for possible unexpected side effects, though I guess there would be none.

Below is the previous description of the ticket:


Define a finite group and its multiplication table:

sage: S = SymmetricGroup(3)
sage: T = S.multiplication_table()

The multiplication table can give latex code to be copy-pasted in a paper:

sage: latex(T)
\setlength{\arraycolsep}{2\ex}
\begin{array}{r|*{6}{r}}
\multicolumn{1}{c|}{\ast}&a&b&c&d&e&f\\\hline
{}a&a&b&c&d&e&f\\
{}b&b&a&d&c&f&e\\
{}c&c&e&a&f&b&d\\
{}d&d&f&b&e&a&c\\
{}e&e&c&f&a&d&b\\
{}f&f&d&e&b&c&a\\
\end{array}}

but is also used for showing the table in a notebook worksheet.

In Sage 4.8 with SageNB we used to have:

sage: show(T)
Unknown control sequence '\setlength'

In Sage 9.2.rc0 with Jupyter, show(T) does display a table but with \setlength displayed in red instead of contributing to the rendering.

Attachments (1)

trac11362.ipynb (7.5 KB) - added by Kwankyu Lee 15 months ago.
Cayley table

Download all attachments as: .zip

Change History (29)

comment:1 Changed 12 years ago by Rob Beezer

I thought this was on #10787, but now I see it is not. I'm going to point it here.

I've been waiting on MathJax before tackling this, on the hope it has better support for tables.

comment:2 Changed 9 years ago by Jeroen Demeyer

Milestone: sage-5.11sage-5.12

comment:3 Changed 9 years ago by For batch modifications

Milestone: sage-6.1sage-6.2

comment:4 Changed 9 years ago by For batch modifications

Milestone: sage-6.2sage-6.3

comment:5 Changed 8 years ago by For batch modifications

Milestone: sage-6.3sage-6.4

comment:6 Changed 2 years ago by Samuel Lelièvre

Cc: Samuel Lelièvre added
Description: modified (diff)
Keywords: latex jupyter mathjax added
Milestone: sage-6.4sage-9.3
Summary: showing a Cayley table fails in the notebook due to bad latex codeRepair show for Cayley tables as MathJax does not support setlength

Still a problem in Jupyter Notebook with Sage 9.2.rc0.

comment:7 Changed 22 months ago by Dave Morris

Branch: public/11362

comment:8 Changed 22 months ago by Dave Morris

Authors: Dave Morris
Commit: 71b0986c0ce5f2d0a4e8cdd37483125d7350c667
Status: newneeds_review

The PR adds a variable sage.misc.latex_macros.sage_configurable_mathjax_macros that holds a list of macro definitions that pretty_print will include in all LaTeX code that is sent to MathJax. The PR then solves the problem on this ticket by adding \multicolumn and \setlength to this list as no-ops that swallow their arguments.


New commits:

7381952trac 11362 configurable mathjax macros
71b0986update doctests

comment:9 Changed 21 months ago by Matthias Köppe

Cc: Kwankyu Lee added

comment:10 Changed 21 months ago by Kwankyu Lee

I don't like the proposed solution. It seems a bandage.

A general solution that would also solve possible future problems would be to introduce a new magic method obj._mathjax_() which provides latex code renderable by mathjax and defaults to an alias of obj._latex_() when it is not defined for obj.

comment:11 Changed 21 months ago by Dave Morris

I sympathize with what you are saying, and I considered that solution (except that my idea was to add a keyword (maybe style="MathJax") to the _latex_ command. But I don't see how to make it will work without massive changes, because an object that does not have a _mathjax_ method will presumably call the _latex_ method of its subobjects (or subexpressions) and will therefore not do the right thing for MathJax.

I think of MathJax as being a version of latex, rather than a completely different format, so I think it is reasonable to just add a tweak to make it work better (as in my PR). The variable I added simply makes it possible to add definitions for latex macros that MathJax does not understand, and is completely analogous to the variable sage_configurable_latex_macros that is already in sage, which makes it possible to add definitions for sage macros that vanilla latex does not understand.

comment:12 Changed 21 months ago by Kwankyu Lee

How about using _rich_repr_? This seems a general mechanism to serve the purpose that I have in mind about _mathjax_. There are many examples in Sage src.

comment:13 in reply to:  11 Changed 21 months ago by Kwankyu Lee

Replying to gh-DaveWitteMorris:

I sympathize with what you are saying, and I considered that solution (except that my idea was to add a keyword (maybe style="MathJax") to the _latex_ command. But I don't see how to make it will work without massive changes, because an object that does not have a _mathjax_ method will presumably call the _latex_ method of its subobjects (or subexpressions) and will therefore not do the right thing for MathJax.

I think of MathJax as being a version of latex, rather than a completely different format, ...

MathJax is a javascript renderer for latex in html, and also sometimes means the subset of latex it supports. On the other hand LaTeX is a format for TeX which primarily targets printing.

It seems to me currently there is some unfortunate confused code in this respect in the rich output system in Sage. Perhaps straightening the confused code would give a natural solution of this ticket's problem. I will investigate more.

comment:14 Changed 21 months ago by Samuel Lelièvre

Related:

  • #31536: Divorce MathJax from LaTeX and let it marry HTML

comment:15 Changed 21 months ago by Kwankyu Lee

With #31536, the following patch will solve the problem of this ticket.

  • src/sage/matrix/operation_table.py

    diff --git a/src/sage/matrix/operation_table.py b/src/sage/matrix/operation_table.py
    index 6feda2215a..8c066f11a9 100644
    a b class OperationTable(SageObject): 
    10781078        table.append('\\end{array}')
    10791079        table.append('}')
    10801080        return ''.join(table)
     1081
     1082    def _html_(self):
     1083        n = self._n
     1084        names = self._names
     1085
     1086        # Headers
     1087        table = ['\\begin{array}{r|*{'+str(n)+'}{r}}\n']
     1088        table.append(self._latex_symbol)
     1089        table += ['&'+names[i] for i in range(n)]
     1090        table.append('\\\\\\hline\n')
     1091
     1092        # Row label and body of table
     1093        for g in range(n):
     1094            table.append('{}')  # Interrupts newline and [], so not line spacing
     1095            table.append(names[g])
     1096            for h in range(n):
     1097                table.append('&'+names[self._table[g][h]])
     1098            table.append('\\\\\n')
     1099
     1100        # Finish
     1101        table.append('\\end{array}')
     1102        return ''.join(table)
     1103

comment:16 Changed 19 months ago by Matthias Köppe

Milestone: sage-9.3sage-9.4

Moving to 9.4, as 9.3 has been released.

comment:17 Changed 17 months ago by Matthias Köppe

Milestone: sage-9.4sage-9.5

Setting a new milestone for this ticket based on a cursory review.

comment:18 Changed 15 months ago by git

Commit: 71b0986c0ce5f2d0a4e8cdd37483125d7350c66735130bd3a69f9f959505575b868cef5a7914c72a

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

35130bdAugment OutputHtml with latex

comment:19 Changed 15 months ago by Kwankyu Lee

Authors: Dave MorrisKwankyu Lee
Component: group theorynotebook
Description: modified (diff)
Reviewers: Dave Morris
Summary: Repair show for Cayley tables as MathJax does not support setlengthEnhance OutputHtml rich output for better pdf rendering of Jupyter notebook

Changed 15 months ago by Kwankyu Lee

Attachment: trac11362.ipynb added

Cayley table

comment:20 Changed 15 months ago by git

Commit: 35130bd3a69f9f959505575b868cef5a7914c72aa365cab970d531405e5dbcba52ff7abad6a06435

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

a365cabAdd an doctest

comment:21 Changed 15 months ago by git

Commit: a365cab970d531405e5dbcba52ff7abad6a06435e08369b89a9d8b4986dee70acf98166362fc92b9

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

e08369bFix a typo

comment:22 Changed 15 months ago by git

Commit: e08369b89a9d8b4986dee70acf98166362fc92b92a5228389fca42342b2ecb50a1cd0c399a239ac2

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

2a52283Return valid html string

comment:23 Changed 15 months ago by Kwankyu Lee

Reviewers: Dave Morris

comment:24 Changed 15 months ago by git

Commit: 2a5228389fca42342b2ecb50a1cd0c399a239ac2b54513461f4ed8b496bdb827413158110b3f8803

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

b545134Fix doctests

comment:25 Changed 12 months ago by Matthias Köppe

Milestone: sage-9.5sage-9.6

Stalled in needs_review or needs_info; likely won't make it into Sage 9.5.

comment:26 Changed 9 months ago by git

Commit: b54513461f4ed8b496bdb827413158110b3f8803f3ad93862005f3efd4b4f0e1731698098f29dc1c

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

f3ad938Augment OutputHtml with latex

comment:27 Changed 7 months ago by Matthias Köppe

Milestone: sage-9.6sage-9.7

comment:28 Changed 2 months ago by Matthias Köppe

Milestone: sage-9.7sage-9.8
Note: See TracTickets for help on using tickets.