Skip to content

Accessing the generated MathML of a formula programmatically through javascript

pkra edited this page Mar 7, 2013 · 3 revisions

Dear MathJax developers,

first of all, thanks for your work.

Please forgive me if my question will be exceedingly naive: I’ve searched through the documentation and the group, but I couldn’t find any precise pointer to an answer.

I’d like to access programmatically, through javascript on my page, to the MathML underlying a specific rendered formula (the one you can see right-clicking on a formula and choosing Show Math As > MathML Code).

Is there a safe way to do it?

Thanks again,

Enrico


Are you after a string representation, or actual DOM elements? The
Show Math As > MathML Code produces a string for display. It is
possible to obtain that string, but it is a bit delicate, because the
command that creates it can operate asynchronously. (That is because
it may cause files to be loaded, depending on the math that is in the
expression. If it has already been displayed, chances are that it
won't need to do that, but it is still possible.) You should add "toMathML.js" to your extensions array if you are doing
your own configuration (it is already included in the standard
combined configuration files). Then define

function toMathML(jax,callback) { var mml; try { // // Try to produce the MathML (if an asynchronous // action occurs, a reset error is thrown) // Otherwise we got the MathML and call the // user's callback passing the MathML. // mml = jax.root.toMathML(""); } catch(err) { if (!err.restart) {throw err} // an actual error // // For a delay due to file loading // call this routine again after waiting for the // the asynchronous action to finish. // return MathJax.Callback.After([toMathML,jax,callback],err.restart); } // // Pass the MathML to the user's callback MathJax.Callback(callback)(mml); }

This will give you a function that you can pass an Element Jax and a
callback function to. The callback will be called with the MathML
from the element.

Here is a complete example:

    <!DOCTYPE html>
    <html>
    <head>
    <title>MathJax TeX to MathML Page</title>
    <script>
    function toMathML(jax,callback) {
      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 type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {inlineMath: [["$","$"],["\\(","\\)"]]}
      });
      MathJax.Hub.Queue(
        function () {
          var jax = MathJax.Hub.getAllJax();
          for (var i = 0; i < jax.length; i++) {
            toMathML(jax[i],function (mml) {
              alert(jax[i].originalText + "\n\n=>\n\n"+ mml);
            });
          }
        }
      );
    </script>
    <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML-full 

"></script>

When $a \ne 0$, there are two solutions to (ax^2 + bx + c = 0) and
they are $$x = {-b \pm \sqrt{b^2-4ac} \over 2a}.$$

Clone this wiki locally