我想知道是否有办法将MathJax
输出转换为MathML
.
我通读了几篇说MathJax
支持的文章MathML
。Show MathML
当我右键单击MathJax
公式时,我还可以看到选项“ ”。我的问题是,我可以从 获取MathML
网页的输出MathJax
吗?我不熟悉,MathJax
我不确定它是如何工作的。任何资源或教程页面都会很好!
@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.
我已经写了一些代码检查一下:首先包括“ https://code.jquery.com/jquery-1.11.2.min.js ”和“ 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);
});
},
};
用法 :
JaxToML.convert(AjaxText, function(mml) {
alert(mml);
});
有关配置 MathJax的 MathJax 文档可能是开始阅读的地方。您可以配置每个浏览器的输出 jax。
一个警告。MathJax 现在不使用任何浏览器上的 MathML 输出是有原因的:浏览器支持还不完全存在。(这将随着浏览器的追赶而改变,MathJax 可以开始利用它们的原生支持。)所以请确保您的内容实际呈现正常。