javascript 如何调用或重启 MathJax?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5200545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to recall or restart MathJax?
提问by Rella
I need MathJax to recheck again all my page.
我需要 MathJax 重新检查我的所有页面。
I mean when the page is created it does all great. But I need to call it after window.onload to reparse the page, as its contents have changed in the meantime.
我的意思是当页面被创建时,它一切都很好。但是我需要在 window.onload 之后调用它来重新解析页面,因为它的内容在此期间发生了变化。
How would I do such a thing?
我怎么会做这样的事情?
回答by thirtydot
See http://docs.mathjax.org/en/latest/advanced/typeset.html:
请参阅http://docs.mathjax.org/en/latest/advanced/typeset.html:
If you are writing a dynamic web page where content containing mathematics may appear after MathJax has already typeset the rest of the page, then you will need to tell MathJax to look for mathematics in the page again when that new content is produced. To do that, you need to use the
MathJax.Hub.Typeset()method. This will cause the preprocessors (if any were loaded) to run over the page again, and then MathJax will look for unprocessed mathematics on the page and typeset it, leaving unchanged any math that has already been typeset.You should not simply call this method directly, however. [You should instead] queue the typeset action, [using this] command:
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
如果您正在编写一个动态网页,其中在 MathJax 已经排版了页面的其余部分之后可能会出现包含数学的内容,那么您将需要告诉 MathJax 在生成新内容时再次在页面中查找数学。为此,您需要使用该
MathJax.Hub.Typeset()方法。这将导致预处理器(如果有的话)再次在页面上运行,然后 MathJax 将在页面上查找未处理的数学并对其进行排版,任何已经排版的数学保持不变。但是,您不应简单地直接调用此方法。[您应该改为] 将排版操作排入队列,[使用此] 命令:
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
Demo here: http://cdn.mathjax.org/mathjax/latest/test/sample-dynamic.html
演示在这里:http: //cdn.mathjax.org/mathjax/latest/test/sample-dynamic.html
回答by countzero
I found the simplest way to do dynamic updates with MathML is to let MathJax do the content changes (instead of the jQuery .html(s) function, e.g.) Then it processes the math while changing the content.
我发现使用 MathML 进行动态更新的最简单方法是让 MathJax 进行内容更改(而不是 jQuery .html(s) 函数,例如)然后它在更改内容的同时处理数学。
<script type="text/javascript">
function updateMathContent(s) {
var math = MathJax.Hub.getAllJax("mathdiv")[0];
MathJax.Hub.Queue(["Text", math, s]);
}
</script>
...
...
<div id="mathdiv">
<math xmlns="http://www.w3.org/1998/Math/MathML">
<msup>
<mi>x</mi>
<mn>2</mn>
</msup>
</math>
</div>
So just use the function to replace the entire contents of the div with new MathML and it will work. (The script goes in the head.)
因此,只需使用该函数将 div 的全部内容替换为新的 MathML 即可。(脚本在头脑中。)
Note: If you have an empty math div and add MathML later, you will get a script error. But if the math tags are present with nothing inside the call to updateMathContent will work.
注意:如果您有一个空的数学 div 并且稍后添加 MathML,您将收到一个脚本错误。但是,如果数学标记在对 updateMathContent 的调用中没有任何内容,则将起作用。

