From 12c661f7617926c32c1286816370db1630547f88 Mon Sep 17 00:00:00 2001 From: Aron Ahmadia Date: Thu, 25 Oct 2012 21:21:24 +0100 Subject: [PATCH 1/4] Improved MathJax, missing callback workaround MathJax.InputJax.TeX.resetEquationNumbers is supposed to be available, but isn't consistently loaded before the notebook fires off its first re-render request. The code edits in mathjaxutils.js fix this issue. Additionally, there was some init code in mathjaxutils.js that has been properly excised to the html templates. Removed some orphan rendering code so that all typesetting now goes through mathjaxutils.js Finally, removed an extra cell in the demo notebook. --- .../frontend/html/notebook/static/js/cell.js | 8 --- .../html/notebook/static/js/mathjaxutils.js | 20 +++---- .../html/notebook/static/js/textcell.js | 59 +++++++++---------- .../html/notebook/templates/notebook.html | 15 +++++ .../Typesetting Math Using MathJax.ipynb | 8 --- 5 files changed, 51 insertions(+), 59 deletions(-) diff --git a/IPython/frontend/html/notebook/static/js/cell.js b/IPython/frontend/html/notebook/static/js/cell.js index 4dbb9e6e6..ade8f9daa 100644 --- a/IPython/frontend/html/notebook/static/js/cell.js +++ b/IPython/frontend/html/notebook/static/js/cell.js @@ -51,14 +51,6 @@ var IPython = (function (IPython) { }; - // typeset with MathJax if MathJax is available - Cell.prototype.typeset = function () { - if (window.MathJax){ - MathJax.Hub.Queue(["Typeset",MathJax.Hub]); - } - }; - - Cell.prototype.select = function () { this.element.addClass('ui-widget-content ui-corner-all'); this.selected = true; diff --git a/IPython/frontend/html/notebook/static/js/mathjaxutils.js b/IPython/frontend/html/notebook/static/js/mathjaxutils.js index 2dea85e65..b8df58afa 100644 --- a/IPython/frontend/html/notebook/static/js/mathjaxutils.js +++ b/IPython/frontend/html/notebook/static/js/mathjaxutils.js @@ -16,18 +16,6 @@ IPython.mathjaxutils = (function (IPython) { var init = function () { if (window.MathJax) { // MathJax loaded - MathJax.Hub.Config({ - TeX: { equationNumbers: { autoNumber: "AMS", useLabelIds: true } }, - tex2jax: { - inlineMath: [ ['$','$'], ["\\(","\\)"] ], - displayMath: [ ['$$','$$'], ["\\[","\\]"] ], - processEnvironments: true - }, - displayAlign: 'left', // Change this to 'center' to center equations. - "HTML-CSS": { - styles: {'.MathJax_Display': {"margin": 0}} - } - }); } else if (window.mathjax_url != "") { // Don't have MathJax, but should. Show dialog. var dialog = $('
') @@ -225,8 +213,14 @@ IPython.mathjaxutils = (function (IPython) { var queue_render = function () { // see https://groups.google.com/forum/?fromgroups=#!topic/mathjax-users/cpwy5eCH1ZQ + var jax = MathJax.Hub.getAllJax(); + MathJax.Hub.Queue( - ["resetEquationNumbers",MathJax.InputJax.TeX], + function () { + if (MathJax.InputJax.TeX.resetEquationNumbers) { + MathJax.InputJax.TeX.resetEquationNumbers(); + } + }, ["PreProcess",MathJax.Hub], ["Reprocess",MathJax.Hub] ); diff --git a/IPython/frontend/html/notebook/static/js/textcell.js b/IPython/frontend/html/notebook/static/js/textcell.js index 8b46bbef0..809776bcf 100644 --- a/IPython/frontend/html/notebook/static/js/textcell.js +++ b/IPython/frontend/html/notebook/static/js/textcell.js @@ -221,37 +221,36 @@ var IPython = (function (IPython) { if (this.rendered === false) { var text = this.get_text(); if (text === "") { text = this.placeholder; } - - text = IPython.mathjaxutils.remove_math(text) - var html = IPython.markdown_converter.makeHtml(text); - html = IPython.mathjaxutils.replace_math(html) - - try { - this.set_rendered(html); - } catch (e) { - console.log("Error running Javascript in Markdown:"); - console.log(e); - this.set_rendered($("
").addClass("js-error").html( - "Error rendering Markdown!
" + e.toString()) - ); + else { + text = IPython.mathjaxutils.remove_math(text) + var html = IPython.markdown_converter.makeHtml(text); + html = IPython.mathjaxutils.replace_math(html) + try { + this.set_rendered(html); + } catch (e) { + console.log("Error running Javascript in Markdown:"); + console.log(e); + this.set_rendered($("
").addClass("js-error").html( + "Error rendering Markdown!
" + e.toString()) + ); + } + this.element.find('div.text_cell_input').hide(); + this.element.find("div.text_cell_render").show(); + var code_snippets = this.element.find("pre > code"); + code_snippets.replaceWith(function () { + var code = $(this).html(); + /* Substitute br for newlines and   for spaces + before highlighting, since prettify doesn't + preserve those on all browsers */ + code = code.replace(/(\r\n|\n|\r)/gm, "
"); + code = code.replace(/ /gm, ' '); + code = prettyPrintOne(code); + + return '' + code + ''; + }); + + IPython.mathjaxutils.queue_render() } - this.typeset() - this.element.find('div.text_cell_input').hide(); - this.element.find("div.text_cell_render").show(); - var code_snippets = this.element.find("pre > code"); - code_snippets.replaceWith(function () { - var code = $(this).html(); - /* Substitute br for newlines and   for spaces - before highlighting, since prettify doesn't - preserve those on all browsers */ - code = code.replace(/(\r\n|\n|\r)/gm, "
"); - code = code.replace(/ /gm, ' '); - code = prettyPrintOne(code); - - return '' + code + ''; - }); - - IPython.mathjaxutils.queue_render() this.rendered = true; } }; diff --git a/IPython/frontend/html/notebook/templates/notebook.html b/IPython/frontend/html/notebook/templates/notebook.html index 7eea6e8b7..865e4390f 100644 --- a/IPython/frontend/html/notebook/templates/notebook.html +++ b/IPython/frontend/html/notebook/templates/notebook.html @@ -2,6 +2,21 @@ {% block stylesheet %} {% if mathjax_url %} + + {% end %} - + {% end %} - - + {% end %}