Last active
December 20, 2015 05:39
-
-
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Learned from this example and the MathJax Google group; created an enhanced version here:
https://gist.github.com/tiarno/6116323