Skip to content
This repository was archived by the owner on Feb 1, 2023. It is now read-only.

Commit 3da0a29

Browse files
author
Release Manager
committed
Trac #31536: Fix Sage rich output problems by coupling MathJax with html
This ticket has grown from its initial motive and now solves multiple problems related with Sage rich output. In particular, it - fixes the blocker https://trac.sagemath.org/ticket/31513 - fixes the %matplotlib notebook magic, which is broken in a %display latex context in Sage 9.2 - performs some code improvement regarding the handling of mathjax The patch has been quite tested with Jupyter notebooks involving various types of Sage objects, Matplotlib objects and ipywidgets. The initial motive, which is the root of the solutions, is ---- Presently mathjax is coupled with latex in Sage. The coupling causes dilemmas because latex is for printing and mathjax is for rendering math in html. We encounter such a dilemma in solving #11362, for example. This ticket takes mathjax from `sage.misc.latex` and place it properly in `sage.misc.html`. The command `view(obj)` is used to view the latex representation of an object. This ticket aims to make `show(obj)` and `pretty_print(obj)` show the html+mathjax representation of the object. The html+mathjax representation can be made from the latex representation or given by `_html_` method of the object. While we are at it, we also cleaned up old code written for deprecated sage notebook. URL: https://trac.sagemath.org/31536 Reported by: klee Ticket author(s): Kwankyu Lee Reviewer(s): Eric Gourgoulhon
2 parents a89f81d + 4c29b96 commit 3da0a29

File tree

32 files changed

+540
-737
lines changed

32 files changed

+540
-737
lines changed

src/doc/de/tutorial/latex.rst

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Die ``eval`` Funktion dieser Klasse konvertiert ein Sage-Objekt in
8181
seine LaTeX-Darstellung und dann in HTML mit der CSS ``math`` Klasse,
8282
die dann MathJax verwendet. ::
8383

84-
sage: from sage.misc.latex import MathJax
84+
sage: from sage.misc.html import MathJax
8585
sage: mj = MathJax()
8686
sage: var('z')
8787
z
@@ -129,9 +129,9 @@ Der interne ``pretty_print()`` Befehl zeigt die Konvertierung von Sage
129129
Objekten in HTML Code der MathJax nutzt im Notebook. ::
130130

131131
sage: pretty_print(x^12)
132-
<html><script type="math/tex">\newcommand{\Bold}[1]{\mathbf{#1}}x^{12}</script></html>
132+
x^12
133133
sage: pretty_print(integrate(sin(x), x))
134-
<html><script type="math/tex">\newcommand{\Bold}[1]{\mathbf{#1}}-\cos\left(x\right)</script></html>
134+
-cos(x)
135135

136136
Das Notebook hat zwei weitere Möglichkeiten TeX zu nutzen. Die erste
137137
ist der "Typeset"-Knopf über der ersten Zelle eines Arbeitsblatts,
@@ -203,7 +203,7 @@ integriert ist. ::
203203

204204
sage: latex(QQ)
205205
\Bold{Q}
206-
sage: from sage.misc.latex import MathJax
206+
sage: from sage.misc.html import MathJax
207207
sage: mj=MathJax()
208208
sage: mj(QQ)
209209
<html><script type="math/tex; mode=display">\newcommand{\Bold}[1]{\mathbf{#1}}\Bold{Q}</script></html>
@@ -225,7 +225,7 @@ MathJax als TeX-Schnipsel interpretiert werden. ::
225225
(x, y)
226226
sage: latex(x+y)
227227
x + y
228-
sage: from sage.misc.latex import MathJax
228+
sage: from sage.misc.html import MathJax
229229
sage: mj=MathJax()
230230
sage: mj(x+y)
231231
<html><script type="math/tex; mode=display">\newcommand{\Bold}[1]{\mathbf{#1}}\newcommand{\foo}{bar}x + y</script></html>
@@ -319,17 +319,17 @@ lässt. Diese Liste wird verwaltet durch die Befehle
319319
``latex.add_to_mathjax_avoid_list`` und
320320
``latex.mathjax_avoid_list``. ::
321321

322-
sage: latex.mathjax_avoid_list([])
323-
sage: latex.mathjax_avoid_list()
322+
sage: latex.mathjax_avoid_list([]) # not tested
323+
sage: latex.mathjax_avoid_list() # not tested
324324
[]
325-
sage: latex.mathjax_avoid_list(['foo', 'bar'])
326-
sage: latex.mathjax_avoid_list()
325+
sage: latex.mathjax_avoid_list(['foo', 'bar']) # not tested
326+
sage: latex.mathjax_avoid_list() # not tested
327327
['foo', 'bar']
328-
sage: latex.add_to_mathjax_avoid_list('tikzpicture')
329-
sage: latex.mathjax_avoid_list()
328+
sage: latex.add_to_mathjax_avoid_list('tikzpicture') # not tested
329+
sage: latex.mathjax_avoid_list() # not tested
330330
['foo', 'bar', 'tikzpicture']
331-
sage: latex.mathjax_avoid_list([])
332-
sage: latex.mathjax_avoid_list()
331+
sage: latex.mathjax_avoid_list([]) # not tested
332+
sage: latex.mathjax_avoid_list() # not tested
333333
[]
334334

335335
Nehmen wir an ein LaTeX-Ausdruck wurde im Notebook durch ``view()``
@@ -389,11 +389,8 @@ eingestellt sind).
389389
<BLANKLINE>
390390
To use, try to view this object -- it won't work. Now try
391391
'latex.add_to_preamble("\\usepackage[matrix,arrow,curve,cmtip]{xy}")',
392-
and try viewing again -- it should work in the command line but not
393-
from the notebook. In the notebook, run
394-
'latex.add_to_mathjax_avoid_list("xymatrix")' and try again -- you
395-
should get a picture (a part of the diagram arising from a filtered
396-
chain complex).
392+
and try viewing again. You should get a picture (a part of the diagram arising
393+
from a filtered chain complex).
397394

398395
.. _sec-tkz-graph:
399396

@@ -440,8 +437,8 @@ mittels LaTeX in einem Notebook darzustellen. ::
440437
sage: latex.extra_preamble() # random - depends on system's TeX installation
441438
'\\usepackage{tikz}\n\\usepackage{tkz-graph}\n\\usepackage{tkz-berge}\n'
442439
sage: latex.engine('pdflatex')
443-
sage: latex.add_to_mathjax_avoid_list('tikzpicture')
444-
sage: latex.mathjax_avoid_list()
440+
sage: latex.add_to_mathjax_avoid_list('tikzpicture') # not tested
441+
sage: latex.mathjax_avoid_list() # not tested
445442
['tikz', 'tikzpicture']
446443

447444
Beachten Sie, dass es eine Vielzahl von Optionen gibt, die die

src/doc/en/thematic_tutorials/explicit_methods_in_number_theory/nf_introduction.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ command
282282
::
283283

284284
sage: pretty_print(a)
285-
<html><script type="math/tex">\newcommand{\Bold}[1]{\mathbf{#1}}-\frac{1}{2} ...
285+
-1/2*(I*sqrt(3) + 1)*(1/6*sqrt(8/3*sqrt(2) + 225) - 5/2)^(1/3) + 1/6*sqrt(2)*(-I*sqrt(3) + 1)/(1/6*sqrt(8/3*sqrt(2) + 225) - 5/2)^(1/3)
286286

287287
.. math::
288288

src/doc/en/tutorial/latex.rst

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ The ``eval`` function of this class converts a Sage object to its
7878
LaTeX representation and then wraps it in HTML that invokes the CSS
7979
"math" class, which then employs MathJax. ::
8080

81-
sage: from sage.misc.latex import MathJax
81+
sage: from sage.misc.html import MathJax
8282
sage: mj = MathJax()
8383
sage: var('z')
8484
z
@@ -191,7 +191,7 @@ done in written work. This is accomplished by redefining the
191191

192192
sage: latex(QQ)
193193
\Bold{Q}
194-
sage: from sage.misc.latex import MathJax
194+
sage: from sage.misc.html import MathJax
195195
sage: mj=MathJax()
196196
sage: mj(QQ)
197197
<html><script type="math/tex; mode=display">\newcommand{\Bold}[1]{\mathbf{#1}}\Bold{Q}</script></html>
@@ -214,7 +214,7 @@ MathJax interprets a snippet of TeX in the notebook. ::
214214
(x, y)
215215
sage: latex(x+y)
216216
x + y
217-
sage: from sage.misc.latex import MathJax
217+
sage: from sage.misc.html import MathJax
218218
sage: mj=MathJax()
219219
sage: mj(x+y)
220220
<html><script type="math/tex; mode=display">\newcommand{\Bold}[1]{\mathbf{#1}}\newcommand{\foo}{bar}x + y</script></html>
@@ -291,10 +291,7 @@ Customizing LaTeX Processing
291291

292292
It is also possible to control which variant of TeX is
293293
used for system-wide invocations, thus also influencing the
294-
nature of the output. Similarly, it is also possible to control
295-
when the notebook will use MathJax (simple TeX snippets)
296-
or the system-wide TeX installation (more complicated
297-
LaTeX expressions).
294+
nature of the output.
298295

299296
The ``latex.engine()`` command can be used to control if the
300297
system-wide executables ``latex``, ``pdflatex`` or ``xelatex``
@@ -318,17 +315,17 @@ MathJax and invoke latex (or whichever executable is set by the
318315
``latex.add_to_mathjax_avoid_list`` and
319316
``latex.mathjax_avoid_list`` commands. ::
320317

321-
sage: latex.mathjax_avoid_list([])
322-
sage: latex.mathjax_avoid_list()
318+
sage: latex.mathjax_avoid_list([]) # not tested
319+
sage: latex.mathjax_avoid_list() # not tested
323320
[]
324-
sage: latex.mathjax_avoid_list(['foo', 'bar'])
325-
sage: latex.mathjax_avoid_list()
321+
sage: latex.mathjax_avoid_list(['foo', 'bar']) # not tested
322+
sage: latex.mathjax_avoid_list() # not tested
326323
['foo', 'bar']
327-
sage: latex.add_to_mathjax_avoid_list('tikzpicture')
328-
sage: latex.mathjax_avoid_list()
324+
sage: latex.add_to_mathjax_avoid_list('tikzpicture') # not tested
325+
sage: latex.mathjax_avoid_list() # not tested
329326
['foo', 'bar', 'tikzpicture']
330-
sage: latex.mathjax_avoid_list([])
331-
sage: latex.mathjax_avoid_list()
327+
sage: latex.mathjax_avoid_list([]) # not tested
328+
sage: latex.mathjax_avoid_list() # not tested
332329
[]
333330

334331
Suppose a LaTeX expression is produced in the notebook
@@ -388,11 +385,8 @@ properly. To actually see the examples, it is necessary to use
388385
<BLANKLINE>
389386
To use, try to view this object -- it won't work. Now try
390387
'latex.add_to_preamble("\\usepackage[matrix,arrow,curve,cmtip]{xy}")',
391-
and try viewing again -- it should work in the command line but not
392-
from the notebook. In the notebook, run
393-
'latex.add_to_mathjax_avoid_list("xymatrix")' and try again -- you
394-
should get a picture (a part of the diagram arising from a filtered
395-
chain complex).
388+
and try viewing again. You should get a picture (a part of the diagram arising
389+
from a filtered chain complex).
396390

397391
.. _sec-tkz-graph:
398392

@@ -439,8 +433,8 @@ graphs processed by LaTeX in the notebook. ::
439433
sage: latex.extra_preamble() # random - depends on system's TeX installation
440434
'\\usepackage{tikz}\n\\usepackage{tkz-graph}\n\\usepackage{tkz-berge}\n'
441435
sage: latex.engine('pdflatex')
442-
sage: latex.add_to_mathjax_avoid_list('tikzpicture')
443-
sage: latex.mathjax_avoid_list()
436+
sage: latex.add_to_mathjax_avoid_list('tikzpicture') # not tested
437+
sage: latex.mathjax_avoid_list() # not tested
444438
['tikz', 'tikzpicture']
445439

446440
At this point, a command like ``view(graphs.CompleteGraph(4))``

src/doc/fr/tutorial/latex.rst

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Nous pouvons tout de même en voir quelques exemples en employant la classe
7878
représentation LaTeX, puis emballe le résultat dans du code HTML qui fait
7979
possède la classe CSS "math", laquelle indique de faire appel à MathJax. ::
8080

81-
sage: from sage.misc.latex import MathJax
81+
sage: from sage.misc.html import MathJax
8282
sage: mj = MathJax()
8383
sage: var('z')
8484
z
@@ -126,9 +126,9 @@ HTML utilisant MathJax. C'est le code qui sera ensuite utilisé dans le
126126
bloc-notes ::
127127

128128
sage: pretty_print(x^12)
129-
<html><script type="math/tex">\newcommand{\Bold}[1]{\mathbf{#1}}x^{12}</script></html>
129+
x^12
130130
sage: pretty_print(integrate(sin(x), x))
131-
<html><script type="math/tex">\newcommand{\Bold}[1]{\mathbf{#1}}-\cos\left(x\right)</script></html>
131+
-cos(x)
132132

133133
Le bloc-notes dispose de deux autres fonctionnalités pour appeler LaTeX.
134134
Premièrement, lorsque la case « Typeset » (juste au-dessus de la première
@@ -194,7 +194,7 @@ mais la définition de la macro TeX ``\Bold{}`` fournie par Sage. ::
194194

195195
sage: latex(QQ)
196196
\Bold{Q}
197-
sage: from sage.misc.latex import MathJax
197+
sage: from sage.misc.html import MathJax
198198
sage: mj=MathJax()
199199
sage: mj(QQ)
200200
<html><script type="math/tex; mode=display">\newcommand{\Bold}[1]{\mathbf{#1}}\Bold{Q}</script></html>
@@ -217,7 +217,7 @@ bloc-notes. ::
217217
(x, y)
218218
sage: latex(x+y)
219219
x + y
220-
sage: from sage.misc.latex import MathJax
220+
sage: from sage.misc.html import MathJax
221221
sage: mj=MathJax()
222222
sage: mj(x+y)
223223
<html><script type="math/tex; mode=display">\newcommand{\Bold}[1]{\mathbf{#1}}\newcommand{\foo}{bar}x + y</script></html>
@@ -306,17 +306,17 @@ d'appeler latex (ou plus généralement le moteur choisi via ``latex.engine()``)
306306
au lieu MathJax. Les méthodes ``latex.add_to_mathjax_avoid_list`` et
307307
``latex.mathjax_avoid_list`` permettent de gérer le contenu de cette liste. ::
308308

309-
sage: latex.mathjax_avoid_list([])
310-
sage: latex.mathjax_avoid_list()
309+
sage: latex.mathjax_avoid_list([]) # not tested
310+
sage: latex.mathjax_avoid_list() # not tested
311311
[]
312-
sage: latex.mathjax_avoid_list(['foo', 'bar'])
313-
sage: latex.mathjax_avoid_list()
312+
sage: latex.mathjax_avoid_list(['foo', 'bar']) # not tested
313+
sage: latex.mathjax_avoid_list() # not tested
314314
['foo', 'bar']
315-
sage: latex.add_to_mathjax_avoid_list('tikzpicture')
316-
sage: latex.mathjax_avoid_list()
315+
sage: latex.add_to_mathjax_avoid_list('tikzpicture') # not tested
316+
sage: latex.mathjax_avoid_list() # not tested
317317
['foo', 'bar', 'tikzpicture']
318-
sage: latex.mathjax_avoid_list([])
319-
sage: latex.mathjax_avoid_list()
318+
sage: latex.mathjax_avoid_list([]) # not tested
319+
sage: latex.mathjax_avoid_list() # not tested
320320
[]
321321

322322
Supposons maintenant que, dans le bloc-notes, un appel à ``view()`` ou
@@ -369,11 +369,8 @@ la commande ``view()`` pour visualiser l'exemple. ::
369369
<BLANKLINE>
370370
To use, try to view this object -- it won't work. Now try
371371
'latex.add_to_preamble("\\usepackage[matrix,arrow,curve,cmtip]{xy}")',
372-
and try viewing again -- it should work in the command line but not
373-
from the notebook. In the notebook, run
374-
'latex.add_to_mathjax_avoid_list("xymatrix")' and try again -- you
375-
should get a picture (a part of the diagram arising from a filtered
376-
chain complex).
372+
and try viewing again. You should get a picture (a part of the diagram arising
373+
from a filtered chain complex).
377374

378375
.. _sec-tkz-graph:
379376

@@ -413,8 +410,8 @@ la feuille de travail. Les commandes suivantes reprennent l'ensemble des
413410
sage: latex.extra_preamble() # random - depends on system's TeX installation
414411
'\\usepackage{tikz}\n\\usepackage{tkz-graph}\n\\usepackage{tkz-berge}\n'
415412
sage: latex.engine('pdflatex')
416-
sage: latex.add_to_mathjax_avoid_list('tikzpicture')
417-
sage: latex.mathjax_avoid_list()
413+
sage: latex.add_to_mathjax_avoid_list('tikzpicture') # not tested
414+
sage: latex.mathjax_avoid_list() # not tested
418415
['tikz', 'tikzpicture']
419416

420417
La mise en forme du graphe est faite en traitant des commandes ``tkz-graph``

src/doc/ja/tutorial/latex.rst

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ SageはLaTeXを多種多様な形で利用している.
6060

6161
::
6262

63-
sage: from sage.misc.latex import MathJax
63+
sage: from sage.misc.html import MathJax
6464
sage: mj = MathJax()
6565
sage: var('z')
6666
z
@@ -166,7 +166,7 @@ LaTeXで使われるバックスラッシュには,Pythonの文字列内でエ
166166

167167
sage: latex(QQ)
168168
\Bold{Q}
169-
sage: from sage.misc.latex import MathJax
169+
sage: from sage.misc.html import MathJax
170170
sage: mj=MathJax()
171171
sage: mj(QQ)
172172
<html><script type="math/tex; mode=display">\newcommand{\Bold}[1]{\mathbf{#1}}\Bold{Q}</script></html>
@@ -176,7 +176,7 @@ LaTeXで使われるバックスラッシュには,Pythonの文字列内でエ
176176
sage: latex.blackboard_bold(False)
177177

178178
新しいマクロやパッケージなどを追加して,TeXの高い拡張性を利用することができる.
179-
まず,ノートブックでMathJaxが短いTeXコードを解釈する際に使われる,自分用のマクロを追加してみよう.
179+
まず,ノートブックでMathJaxが短いTeXコードを解釈する際に使われる,自分用のマクロを追加してみよう.
180180

181181

182182
::
@@ -190,7 +190,7 @@ LaTeXで使われるバックスラッシュには,Pythonの文字列内でエ
190190
(x, y)
191191
sage: latex(x+y)
192192
x + y
193-
sage: from sage.misc.latex import MathJax
193+
sage: from sage.misc.html import MathJax
194194
sage: mj=MathJax()
195195
sage: mj(x+y)
196196
<html><script type="math/tex; mode=display">\newcommand{\Bold}[1]{\mathbf{#1}}\newcommand{\foo}{bar}x + y</script></html>
@@ -280,17 +280,17 @@ LaTeX処理のカスタマイズ
280280

281281
::
282282

283-
sage: latex.mathjax_avoid_list([])
284-
sage: latex.mathjax_avoid_list()
283+
sage: latex.mathjax_avoid_list([]) # not tested
284+
sage: latex.mathjax_avoid_list() # not tested
285285
[]
286-
sage: latex.mathjax_avoid_list(['foo', 'bar'])
287-
sage: latex.mathjax_avoid_list()
286+
sage: latex.mathjax_avoid_list(['foo', 'bar']) # not tested
287+
sage: latex.mathjax_avoid_list() # not tested
288288
['foo', 'bar']
289-
sage: latex.add_to_mathjax_avoid_list('tikzpicture')
290-
sage: latex.mathjax_avoid_list()
289+
sage: latex.add_to_mathjax_avoid_list('tikzpicture') # not tested
290+
sage: latex.mathjax_avoid_list() # not tested
291291
['foo', 'bar', 'tikzpicture']
292-
sage: latex.mathjax_avoid_list([])
293-
sage: latex.mathjax_avoid_list()
292+
sage: latex.mathjax_avoid_list([]) # not tested
293+
sage: latex.mathjax_avoid_list() # not tested
294294
[]
295295

296296

@@ -331,11 +331,8 @@ LaTeX表式とLaTeXエンジンの生成するdvi形式にdvipngが扱えないs
331331
<BLANKLINE>
332332
To use, try to view this object -- it won't work. Now try
333333
'latex.add_to_preamble("\\usepackage[matrix,arrow,curve,cmtip]{xy}")',
334-
and try viewing again -- it should work in the command line but not
335-
from the notebook. In the notebook, run
336-
'latex.add_to_mathjax_avoid_list("xymatrix")' and try again -- you
337-
should get a picture (a part of the diagram arising from a filtered
338-
chain complex).
334+
and try viewing again. You should get a picture (a part of the diagram arising
335+
from a filtered chain complex).
339336

340337

341338
.. _sec-tkz-graph:
@@ -368,8 +365,8 @@ LaTeX表式とLaTeXエンジンの生成するdvi形式にdvipngが扱えないs
368365
sage: latex.extra_preamble() # random - システムで運用されているTeXに依存
369366
'\\usepackage{tikz}\n\\usepackage{tkz-graph}\n\\usepackage{tkz-berge}\n'
370367
sage: latex.engine('pdflatex')
371-
sage: latex.add_to_mathjax_avoid_list('tikzpicture')
372-
sage: latex.mathjax_avoid_list()
368+
sage: latex.add_to_mathjax_avoid_list('tikzpicture') # not tested
369+
sage: latex.mathjax_avoid_list() # not tested
373370
['tikz', 'tikzpicture']
374371

375372
ここまで設定してから ``view(graphs.CompleteGraph(4))`` のようなコマンドを実行すると, ``tkz-graph`` で表現されたグラフが ``pdflatex`` で処理されてノートブックに挿入される.
@@ -384,7 +381,7 @@ TeXシステムの完全な運用
384381
================================
385382

386383
TeXをSageに統合して運用する際,高度な機能の多くはシステムに独立してインストールされたTeXがないと利用できない.
387-
Linux系システムではTeXliveを基にした基本TeXパッケージを採用しているディストリビューションが多く,OSXではTeXshop,WindowsではMikTeXなどが使われている.
384+
Linux系システムではTeXliveを基にした基本TeXパッケージを採用しているディストリビューションが多く,OSXではTeXshop,WindowsではMikTeXなどが使われている.
388385
``convert`` ユーティリティは `ImageMagick <http://www.imagemagick.org/>`_ パッケージ(簡単にダウンロード可能)に含まれているし, ``dvipng``, ``ps2pdf`` と ``dvips`` の三つのプログラムはTeXパッケージに同梱されているはずだ.
389386
また ``dvipng`` は http://sourceforge.net/projects/dvipng/ から, ``ps2pdf`` は `Ghostscript <http://www.ghostscript.com/>`_ の一部として入手することもできる.
390387

0 commit comments

Comments
 (0)