forked from yinwang0/sicp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mathcell.xhtml
78 lines (71 loc) · 2.69 KB
/
mathcell.xhtml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<!DOCTYPE html>
<!-- LaTeX to MathML conversion script. Uses MathJax - licensed under Apache License.
Adapted from an example by Davide Cervone in: http://docs.mathjax.org/en/latest/typeset.html -->
<!-- (c) 2014 Andres Raba, GNU GPL v.3. -->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>LaTeX to MathML conversion</title>
<meta charset="utf-8"/>
<script type="text/x-mathjax-config">
//<![CDATA[
MathJax.Hub.Queue([alert, 'Listening']);
(function () {
// This exports ConvertToMML() that takes a string of LaTeX
// and writes the resulting MathML to the hashmap window.mathml,
// using LaTeX string as key.
var QUEUE = MathJax.Hub.queue, // shorthand for the queue
mathcell = "mathcell"; // element id of the conversion cell
window.mathml = {}; // this will hold the results
window.ConvertToMML = function (TeX) {
try {
QUEUE.Push(function () { // overwrite the conversion cell
document.getElementById(mathcell).innerHTML = TeX;
});
QUEUE.Push(["Typeset", MathJax.Hub, mathcell]); // retypeset the cell
QUEUE.Push(function () { // get the jax and extract MathML from it
var math = MathJax.Hub.getAllJax(mathcell)[0];
if (math) {
toMathML(math, function (mml) {
window.mathml[TeX] = mml;
});
}
else { // TeX input was probably faulty, make it stand out
window.mathml[TeX] = "<span class='faulty'>" + TeX + "</span>";
}
});
//QUEUE.Push([alert, 'Retypeset']); // signal the end of single conversion
} catch(error) {
console.log(error);
document.location.reload(); // MathJax won't resurrect otherwise
}
}
window.ConversionEnd = function () { // signal the end of all conversions
try { // after which it's safe to exit
QUEUE.Push([alert, 'End']);
} catch(error) {
console.log(error);
document.location.reload();
}
}
function toMathML(jax,callback) { // extract MathML from jax
var mml;
try {
mml = jax.root.toMathML("");
} catch(err) {
if (!err.restart) { throw err } // an actual error
return MathJax.Callback.After([toMathML,jax,callback],err.restart);
}
MathJax.Callback(callback)(mml);
}
})();
//]]>
</script>
<script src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML-full" type="text/javascript"></script>
</head>
<body>
<!-- The conversion cell. Contents will be overwritten each time ConvertToMML() is called. -->
<div id="mathcell">
\[ \mathrm{e^{i\pi}} + 1 = 0 \]
</div>
</body>
</html>