2014-10-03 93 views
0

我試圖將MathJax嵌入到頁面中,使乳膠呈現爲真正的公式/表達式,但是我沒有得到任何錯誤,也沒有任何渲染。該代碼是直接從MathJax文件MathJax不渲染乳膠

採取我有以下代碼

var div = MathJax.HTML.Element(
    "div", 
    {id: "MathDiv", style:{border:"1px solid", padding:"5px"}}, 
    ["Here is math: \\(x+1\\)",["br"],"and a display $$x+1\\over x-1$$"] 
); 
document.body.appendChild(div); 

說沒有任何錯誤執行,所以MathJax正確裝入,且有在控制檯中沒有錯誤或者。所以很少有信息可以繼續。任何好的建議,以我如何調試這種情況下,甚至更好的解決方案:)

+0

你見過[這部分文檔](http://docs.mathjax.org/en/latest/typeset.html)嗎? – 2014-10-03 13:08:14

+0

@PeterKrautzberger如果你把它寫成propper答案我會投票給它 – 2014-10-03 15:30:00

+0

好吧,從技術上講,這是一個重複的(儘管有很多)例如。 http://stackoverflow.com/a/17054116/1339651 – 2014-10-03 16:01:07

回答

0

此代碼適用於我。

<!doctype html> 
<html> 
<head> 

<!-- or use the cdn for mathjax source code --> 
<script src="../MathJax/MathJax.js"> 
    MathJax.Hub.Config({ 
    jax: ["input/TeX","output/HTML-CSS"], 
    extensions: ["tex2jax.js"], 
    tex2jax: { 
     inlineMath: [ ['\\(','\\)'] ], 
     displayMath: [ ['$$','$$'] ] 
    } 
    }); 

    function doOnLoad() { 
    var div = MathJax.HTML.Element(
      "div", 
      {id: "MathDiv", style:{border:"1px solid", padding:"5px"}}, 
      ["Here is math: \\(x^2+1\\)",["br"],"and a display $$x+1\\over x-1$$"] 
     ); 
    document.body.appendChild(div); 
    // This does not seem needed but you can use if to force rendering of a div 
    // MathJax.Hub.Queue(["Typeset",MathJax.Hub,'MathDiv']); 
    } 
</script> 

</head> 
<body onload="doOnLoad();"> 

</body> 
</html> 

我認爲您需要在onload方法中添加div,因此div在頁面完全解析後添加。