Skip to content

Instantly share code, notes, and snippets.

@tiarno
Last active December 20, 2015 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tiarno/6080271 to your computer and use it in GitHub Desktop.
Save tiarno/6080271 to your computer and use it in GitHub Desktop.
JavaScript/MathJax method to flag for errors on a page containing TeX math: Flags for either TeX errors (undefined control sequence) or Math errors (for example, missing brace). Code stolen from a couple of threads on MathJax Users Google Group: https://groups.google.com/forum/#!msg/mathjax-users/ct4drdifuyI/h61GSTt_-bAJ https://groups.google.co…
<!DOCTYPE html>
<html>
<head>
<title>Encapsulate Trapping of Math and TeX errors</title>
<script type="text/x-mathjax-config">
// Turn off NoErrors extension
MathJax.Hub.Config({
TeX: {noErrors: {disabled: true}}
});
// Set flag function for TeX Errors (undefined cs)
MathJax.Hub.Register.StartupHook("TeX noUndefined Ready",function () {
var TEX = MathJax.InputJax.TeX;
var formatError = TEX.formatError;
TEX.Augment({
formatError: function (err,math,displaystyle,script) {
alert("Error in math: "+math+"\nMessage: "+err.message.replace(/\n.*/,""));
return formatError.apply(this,arguments);
}
});
TEX.Parse.Augment({
csUndefined: function (name) {
TEX.Error("Undefined control sequence "+name);
}
});
});
// Set flag function for Math errors (malformed syntax like missing brace)
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)}
});
}
</script>
<!-- Setup complete, now load MathJax -->
<script type="text/javascript"
src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML">
</script>
</head>
<body>
<p>
The <tt>rule</tt> tag is undefined:
\[\rule{3in}{1pt}x+1\over {1-x}\]
</p>
<p>
The math has a missing brace:
\[x+1\over {1-x\]
</p>
<p>
The math is defined and is well-formed:
\[x+1\over {1-x}\]
</p>
</body>
</html>
@tiarno
Copy link
Author

tiarno commented Jul 30, 2013

Learned from this example and the MathJax Google group; created an enhanced version here:

https://gist.github.com/tiarno/6116323

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment