Opened 12 years ago

# Enhance OutputHtml rich output for better pdf rendering of Jupyter notebook

Reported by: Owned by: William Stein joyner minor sage-9.8 notebook latex, jupyter, mathjax Samuel Lelièvre, Kwankyu Lee Kwankyu Lee N/A public/11362 f3ad93862005f3efd4b4f0e1731698098f29dc1c

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.

### 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.11 → sage-5.12

### comment:3 Changed 9 years ago by For batch modifications

Milestone: sage-6.1 → sage-6.2

### comment:4 Changed 9 years ago by For batch modifications

Milestone: sage-6.2 → sage-6.3

### comment:5 Changed 8 years ago by For batch modifications

Milestone: sage-6.3 → sage-6.4

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

Cc: Samuel Lelièvre added modified (diff) latex jupyter mathjax added sage-6.4 → sage-9.3 showing a Cayley table fails in the notebook due to bad latex code → Repair 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 → 71b0986c0ce5f2d0a4e8cdd37483125d7350c667 new → needs_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:

 ​7381952 trac 11362 configurable mathjax macros ​71b0986 update doctests

### 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 follow-up:  13 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

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 class OperationTable(SageObject): table.append('\\end{array}') table.append('}') return ''.join(table) def _html_(self): n = self._n names = self._names # Headers table = ['\\begin{array}{r|*{'+str(n)+'}{r}}\n'] table.append(self._latex_symbol) table += ['&'+names[i] for i in range(n)] table.append('\\\\\\hline\n') # Row label and body of table for g in range(n): table.append('{}')  # Interrupts newline and [], so not line spacing table.append(names[g]) for h in range(n): table.append('&'+names[self._table[g][h]]) table.append('\\\\\n') # Finish table.append('\\end{array}') return ''.join(table)

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

Milestone: sage-9.3 → sage-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.4 → sage-9.5

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

### comment:18 Changed 15 months ago by git

Commit: 71b0986c0ce5f2d0a4e8cdd37483125d7350c667 → 35130bd3a69f9f959505575b868cef5a7914c72a

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

 ​35130bd Augment OutputHtml with latex

### comment:19 Changed 15 months ago by Kwankyu Lee

Authors: Dave Morris → Kwankyu Lee group theory → notebook modified (diff) → Dave Morris Repair show for Cayley tables as MathJax does not support setlength → Enhance OutputHtml rich output for better pdf rendering of Jupyter notebook

Cayley table

### comment:20 Changed 15 months ago by git

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

 ​a365cab Add an doctest

### comment:21 Changed 15 months ago by git

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

 ​e08369b Fix a typo

### comment:22 Changed 15 months ago by git

Commit: e08369b89a9d8b4986dee70acf98166362fc92b9 → 2a5228389fca42342b2ecb50a1cd0c399a239ac2

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

 ​2a52283 Return valid html string

### comment:23 Changed 15 months ago by Kwankyu Lee

Reviewers: Dave Morris

### comment:24 Changed 15 months ago by git

Commit: 2a5228389fca42342b2ecb50a1cd0c399a239ac2 → b54513461f4ed8b496bdb827413158110b3f8803

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

 ​b545134 Fix doctests

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

Milestone: sage-9.5 → sage-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

 ​f3ad938 Augment OutputHtml with latex