Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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:!msg/mathjax-users/ct4drdifuyI/h61GSTt_-bAJ…
<!DOCTYPE html>
<title>Encapsulate Trapping of Math and TeX errors</title>
<script type="text/x-mathjax-config">
// Turn off NoErrors extension
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;
formatError: function (err,math,displaystyle,script) {
alert("Error in math: "+math+"\nMessage: "+err.message.replace(/\n.*/,""));
return formatError.apply(this,arguments);
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)}
<!-- Setup complete, now load MathJax -->
<script type="text/javascript"
The <tt>rule</tt> tag is undefined:
\[\rule{3in}{1pt}x+1\over {1-x}\]
The math has a missing brace:
\[x+1\over {1-x\]
The math is defined and is well-formed:
\[x+1\over {1-x}\]
Copy link

tiarno commented Jul 30, 2013

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

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