Can we get MathML output from MathJax

2019-04-09 16:43发布

I was wondering if there are ways to convert MathJax output to MathML.

I read through several articles that saying MathJax supports MathML. I can also see the option 'Show MathML' when I right click the MathJax formulas. My question is, can I get the MathML output to the webpage from MathJax? I am not familiar with MathJax and I am not sure how it works. Any resources or tutorial pages would have been nice!

3条回答
放荡不羁爱自由
2楼-- · 2019-04-09 17:01

I have written some code check it out: First include "https://code.jquery.com/jquery-1.11.2.min.js" and "http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"

var JaxToML = {
 toMathML: function(jax, callback) {
     var mml;
     try {
         mml = jax.root.toMathML("");
     } catch (err) {
         if (!err.restart) {
             throw err
         } // an actual error
         return MathJax.Callback.After([JaxToML.toMathML, jax, callback], err.restart);
     }
     MathJax.Callback(callback)(mml);
 },
 convert: function(AjaxText, callback) {
     var tempDiv = $('<div style="width:455px;height:450px:border-width:thick;border-style:double;"></div>').appendTo("body").html(AjaxText)[0];
     MathJax.Hub.Queue(["Typeset", MathJax.Hub, tempDiv]); //first place in Q
     MathJax.Hub.Queue(function() { //wait for a callback to be fired
         var jax = MathJax.Hub.getAllJax(tempDiv);
         for (var i = 0; i < jax.length; i++) {
             JaxToML.toMathML(jax[i], function(mml) {//alert(jax[i].originalText + "\n\n=>\n\n"+ mml);
                 AjaxText = AjaxText.replace(jax[i].originalText, mml);
             });
         }
         $(tempDiv).remove();
         AjaxText = AjaxText.replace(/\(/g,""); //notice this escape character for ( - i.e it has to be \( , know why it is beacuse JS will treat ) or ( as end/begin of function as there are no quotes here.
         AjaxText = AjaxText.replace(/\)/g,""); //notice this escape character for ) - i.e it has to be \)
         AjaxText = AjaxText.replace(/\\/g,"");
         callback(AjaxText);
     });
 },

};

Usage :

JaxToML.convert(AjaxText, function(mml) {
         alert(mml);             
     });
查看更多
贪生不怕死
3楼-- · 2019-04-09 17:13

The MathJax documentation on configuring MathJax is probably the place to start reading. You can configure the output jax per browser.

A word of caution. There's a reason why MathJax does not use to MathML output on any browser right now: browser support isn't quite there yet. (This will change as browsers catch up and MathJax can start to leverage their native support.) So make sure your content actually renders ok.

查看更多
一夜七次
4楼-- · 2019-04-09 17:23

@Peter, I think the OP may be asking how to get a MathML string from MathJax, rather than how to insert the MathML tags into the page directly. So perhaps the discussion on the MathJax forums that describes how to use toMathML will do the trick.

The basic idea is to get the element jax (using MathJax.Hub.getAllJax) for the math you want to convert, then to call its toMathML method. But you need to use some care for this, as toMathML can operate asynchronously. The link above goes through the details.

EDIT: The MathJax-node project allows you to do this from the command line, so you might want to check that out as well.

查看更多
登录 后发表回答