9

以下のサンプルコードを調整しています。MathJaxのドキュメントは完全ではありません。$ \ alpha $のような区切り文字を指定した場合にのみ、Texが解析されるように、以下のコードを変更する方法をもっと経験のある人に教えてもらえますか。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>
    <textarea  id="MathInput" size="50" onkeyup="UpdateMath(this.value)"></textarea>

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

    </body>
    </html>
4

1 に答える 1

20

投稿したサンプルコードは、MathInputの内容を取得し、最初のMathJax要素をMathInputの新しい「数学」に置き換えます。必要なのは、MathInputをタイプセットし、区切られたテキストの新しいMathJax要素を作成することです。ここでjsFiddleの例を設定します:http: //jsfiddle.net/Zky72/2/

主な変更点は、UpdateMath関数にあります。

 window.UpdateMath = function (TeX) {
    //set the MathOutput HTML
    document.getElementById("MathOutput").innerHTML = TeX;

    //reprocess the MathOutput Element
    MathJax.Hub.Queue(["Typeset",MathJax.Hub,"MathOutput"]);

}
于 2011-11-04T20:23:33.487 に答える