通过Mathjax渲染字符串 [英] Render a string via Mathjax

查看:82
本文介绍了通过Mathjax渲染字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本区域,希望用户在其中输入数学表达式.我不会立即呈现任何结果,就像它可以在SO上正常工作一样.
像这样:

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);
}

有可能吗?还是我必须将 this.value 放在某些div元素中,然后让MathJax解析该元素?(那会很愚蠢)

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)

推荐答案

您尝试执行的任务已记录在

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

基本上,该方法是使用

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

,然后排版预览.完成后,使用 getAllJax()方法获取空的数学元素jax.像

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]}
);

然后使用jax元素的 Text()方法更改其显示的数学.

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]);
}

这篇关于通过Mathjax渲染字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆