Render a string via Mathjax

与世无争的帅哥 提交于 2021-02-07 06:13:40

问题


I have an text area that where I expect the user to enter the mathematical expression. I wan't to render any results immediately, just like it works here on SO.
Something like:

<div id="preview"></div>

function onkeyup(event) {
    var prev = document.getElementById("preview");
    Using just a HTML string:
    prev.innerHTML = MathJax.renderString(this.value);

    ... or get DOM object tree instead:
    //Remove previous children
    prev.innerHTML = "";
    //Append new DOM tree
    var tree = MathJax.createDOM(this.value);
    prev.appendChild(tree);
}

Is that possible? Or will I have to put this.value in some div element and than have MathJax parse that element? (that would be stupid)


回答1:


The task you are trying to perform is documented here, and there is a live example here.

Basically, the method is to use

<div id="preview">\(\)</div>

and then typeset the preview. Once that completes, use the getAllJax() method to get the empty math element jax. Something like

var jax;
MathJax.Hub.Queue(
  ["Typeset",MathJax.Hub,"preview"],
  function () {jax = MathJax.Hub.getAllJax("preview")[0]}
);

Then use the element jax's Text() method to change the math that it displays.

function onkeyup(event) {
  MathJax.Hub.Queue(["Text",jax,this.value]);
}


来源:https://stackoverflow.com/questions/21271075/render-a-string-via-mathjax

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!