实时显示 mathjax 输出

发布于 2024-12-12 08:27:24 字数 1395 浏览 2 评论 0原文

如何修改这个 mathjax 示例以在打字时进行实时预览?现在它只在我按下回车键后才显示结果。我想调整它,使其工作方式类似于 stackoverflow/math.stackexchange 在输入问题时显示预览的方式。

<html>
<head>
<title>MathJax Dynamic Math Test Page</title>

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [["$","$"],["\\(","\\)"]]
    }
  });
</script>
<script type="text/javascript"
  src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML-full">
</script>

</head>
<body>

<script>
  //
  //  Use a closure to hide the local variables from the
  //  global namespace
  //
  (function () {
    var QUEUE = MathJax.Hub.queue;  // shorthand for the queue
    var math = null;                // the element jax for the math output.

    //
    //  Get the element jax when MathJax has produced it.
    //
    QUEUE.Push(function () {
      math = MathJax.Hub.getAllJax("MathOutput")[0];
    });

    //
    //  The onchange event handler that typesets the
    //  math entered by the user
    //
    window.UpdateMath = function (TeX) {
      QUEUE.Push(["Text",math,"\\displaystyle{"+TeX+"}"]);
    }
  })();
</script>

Type some TeX code:
<input id="MathInput" size="50" onchange="UpdateMath(this.value)" />
<p>

<div id="MathOutput">
You typed: ${}$
</div>

</body>
</html>

How do I modify this mathjax example to live preview while I type? Right now it only displays result after I have pressed enter. I would like to tweak it so that it works similar to how stackoverflow/math.stackexchange shows the preview when typing a question.

<html>
<head>
<title>MathJax Dynamic Math Test Page</title>

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [["$","$"],["\\(","\\)"]]
    }
  });
</script>
<script type="text/javascript"
  src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML-full">
</script>

</head>
<body>

<script>
  //
  //  Use a closure to hide the local variables from the
  //  global namespace
  //
  (function () {
    var QUEUE = MathJax.Hub.queue;  // shorthand for the queue
    var math = null;                // the element jax for the math output.

    //
    //  Get the element jax when MathJax has produced it.
    //
    QUEUE.Push(function () {
      math = MathJax.Hub.getAllJax("MathOutput")[0];
    });

    //
    //  The onchange event handler that typesets the
    //  math entered by the user
    //
    window.UpdateMath = function (TeX) {
      QUEUE.Push(["Text",math,"\\displaystyle{"+TeX+"}"]);
    }
  })();
</script>

Type some TeX code:
<input id="MathInput" size="50" onchange="UpdateMath(this.value)" />
<p>

<div id="MathOutput">
You typed: ${}$
</div>

</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瀞厅☆埖开 2024-12-19 08:27:24

不要使用 onchange,而是尝试 onkeypressonkeyup

onchange 仅在您离开字段时触发,但其他事件(显然)会在每次击键时发生。

Instead of using onchange try onkeypress or onkeyup.

onchange is only triggered when you leave the field, but the others (obviously) happen with each key-stroke.

风为裳 2024-12-19 08:27:24

我怀疑您使用的是 Internet Explorer,它不像其他浏览器那样频繁或高效地触发 onchange 事件。

MathJax Examples 中的版本包含更多代码以更好地处理 IE 。您可能想查看那里的源代码以了解详细信息。

I suspect you are using Internet Explorer, which doesn't fire onchange events as often or efficiently as other browsers.

The version in the MathJax Examples includes more code to handle IE better. You might want to look at the source code there for details.

山川志 2024-12-19 08:27:24
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({tex2jax: {inlineMath: [ ['

,'

], ["\\(","\\)"] ],processEscapes: true}}); </script> <script type="text/javascript" charset="utf-8" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"> </script> <script> function f() { var input = document.getElementById("input"); document.getElementById("output").innerHTML = input.value; MathJax.Hub.Queue(["Typeset",MathJax.Hub]); } </script> <textarea id="input" cols="25" rows="5" onkeyup="f()"> </textarea> <p id="output"></p>

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({tex2jax: {inlineMath: [ ['

,'

], ["\\(","\\)"] ],processEscapes: true}}); </script> <script type="text/javascript" charset="utf-8" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"> </script> <script> function f() { var input = document.getElementById("input"); document.getElementById("output").innerHTML = input.value; MathJax.Hub.Queue(["Typeset",MathJax.Hub]); } </script> <textarea id="input" cols="25" rows="5" onkeyup="f()"> </textarea> <p id="output"></p>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文