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.
This commit is contained in:
Aron Ahmadia 2012-10-25 21:21:24 +01:00
parent eba9044f2e
commit 12c661f761
5 changed files with 49 additions and 57 deletions

View File

@ -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;

View File

@ -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 = $('<div></div>')
@ -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]
);

View File

@ -221,37 +221,36 @@ var IPython = (function (IPython) {
if (this.rendered === false) {
var text = this.get_text();
if (text === "") { text = this.placeholder; }
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($("<div/>").addClass("js-error").html(
"Error rendering Markdown!<br/>" + 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 &nbsp; for spaces
before highlighting, since prettify doesn't
preserve those on all browsers */
code = code.replace(/(\r\n|\n|\r)/gm, "<br/>");
code = code.replace(/ /gm, '&nbsp;');
code = prettyPrintOne(code);
text = IPython.mathjaxutils.remove_math(text)
var html = IPython.markdown_converter.makeHtml(text);
html = IPython.mathjaxutils.replace_math(html)
return '<code class="prettyprint">' + code + '</code>';
});
try {
this.set_rendered(html);
} catch (e) {
console.log("Error running Javascript in Markdown:");
console.log(e);
this.set_rendered($("<div/>").addClass("js-error").html(
"Error rendering Markdown!<br/>" + e.toString())
);
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 &nbsp; for spaces
before highlighting, since prettify doesn't
preserve those on all browsers */
code = code.replace(/(\r\n|\n|\r)/gm, "<br/>");
code = code.replace(/ /gm, '&nbsp;');
code = prettyPrintOne(code);
return '<code class="prettyprint">' + code + '</code>';
});
IPython.mathjaxutils.queue_render()
this.rendered = true;
}
};

View File

@ -2,6 +2,21 @@
{% block stylesheet %}
{% if mathjax_url %}
<script type="text/x-mathjax-config">
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}}
}
});
</script>
<script type="text/javascript" src="{{mathjax_url}}?config=TeX-AMS_HTML" charset="utf-8"></script>
{% end %}
<script type="text/javascript">

View File

@ -331,14 +331,6 @@
"x=4\n",
"$$"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}