> What I would really like, is an API that makes the Typesetting feel
> like an ordinary HTTP request, and so
> it would feel natural to any frontend-building person.
> What I mean is, that I would not only pass the node to be typeset to
> MathJax.Hub.Typeset and a callback,
> but an object that has two callbacks, one that is called in case of
> success, the other in case of failure and those
> callbacks receive the `data' object from your solution.
> So I could encapsulate better.
Here are two different approaches to doing that. The first simply
checks the typeset math to see if it is an error after it is typeset:
<!DOCTYPE html>
<html>
<head>
<title>Encapsulate Trapping of TeX errors -- Method I</title>
<script type="text/x-mathjax-config">
//
// Turn off noErrors extension
//
MathJax.Hub.Config({
TeX: {noErrors: {disabled: true}}
});
//
// Call this to typeset the contents of the node, with success()
being called
// if the math typeset properly, and failure() being called if there
was a
// TeX error during the typeset
//
function EncapsulatedTypeset (node,success,failure) {
var HUB = MathJax.Hub;
return HUB.Typeset(node,function () {
var math = HUB.getAllJax(node)[0];
if (!math.texError) {success(node,math)}
else {failure(node,math,math.root.HTMLspanElement().textContent)}
});
}
//
// Service routines to set the math, report success, and report
failure
//
function SetMath(TeX) {document.getElementById("output").innerHTML =
TeX}
function Success(node,math) {alert("Math OK!")}
function Failure(node,math,msg) {alert("TeX Error: "+msg)}
//
// Queue some actions that set the math and typeset with success/
failure messages
//
MathJax.Hub.Queue(
[SetMath,"$$x+1\\over {1-x$$"], // has an error
[EncapsulatedTypeset,"output",Success,Failure],
[SetMath,"$$x+1\\over 1-x$$"], // no error
[EncapsulatedTypeset,"output",Success,Failure]
);
</script>
<script type="text/javascript" src="
http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML
"></script>
</head>
<body>
<p id="output"></p>
</body>
</html>
Note that we turn off the noErrors extension, since otherwise the
error message returned would be the original TeX code.
The second uses the signal handler to record the error message for
later use:
<!DOCTYPE html>
<html>
<head>
<title>Encapsulate Trapping of TeX errors -- Method II</title>
<script type="text/x-mathjax-config">
(function () {
var texError = ""; // stores the tex error
var HUB = MathJax.Hub; // shorthand for MathJax.Hub
//
// Register a hook that records the tex error message for later
//
HUB.Register.MessageHook("TeX Jax - parse error", function (data)
{texError = data[1]});
//
// Call this to typeset the contents of the node, with success()
being called
// if the math typeset properly, and failure() being called if
there was a
// TeX error during the typeset
//
window.EncapsulatedTypeset = function (node,success,failure) {
texError = "";
return HUB.Typeset(node,function () {
var math = HUB.getAllJax(node)[0];
if (texError == "") {success(node,math)} else
{failure(node,math,texError)}
});
}
})();
//
// Service routines to set the math, report success, and report
failure
//
function SetMath(TeX) {document.getElementById("output").innerHTML =
TeX}
function Success(node,math) {alert("Math OK!")}
function Failure(node,math,msg) {alert("TeX Error: "+msg)}
//
// Queue some actions that set the math and typeset with success/
failure messages
//
MathJax.Hub.Queue(
[SetMath,"$$x+1\\over {1-x$$"], // has an error
[EncapsulatedTypeset,"output",Success,Failure],
[SetMath,"$$x+1\\over 1-x$$"], // no error
[EncapsulatedTypeset,"output",Success,Failure]
);
</script>
<script type="text/javascript" src="
http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML
"></script>
</head>
<body>
<p id="output"></p>
</body>
</html>
Note that both methods assume there is only one math expression within
the given node. If that is not the case, I'm not sure exactly how the
success and failure should be handled (do you want success/failure
called for each expression, or is it for the node as a whole?), so
I'll let you work out the details in that case. You can loop through
all the jax returned by MathJax.Hub.getAllJax(node) and call the
succes/failure as appropriate in the first case, but would potentially
have to record multiple error messages in the second case.
Davide