Show MathML code

230 views
Skip to first unread message

kikk...@gmail.com

unread,
Nov 20, 2012, 6:46:07 AM11/20/12
to mathja...@googlegroups.com
Hi at all, I have a question, I use sample-dynamic for display a formula, I need to display also the MathML code, is possible to show it without use menu, show math as, mathml?

i find this post:

but this display alert on page load while i need to display code after create the formula.

This is my code:

<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>

<script>
        function toMathML(jax,callback) {
          var mml;
          try {
            mml = jax.root.toMathML("");
          } catch(err) {
            if (!err.restart) {throw err} // an actual error
            return MathJax.Callback.After([toMathML,jax,callback],err.restart);
          }
          MathJax.Callback(callback)(mml);
        }
</script>


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

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


</body>
</html>


 

Davide P. Cervone

unread,
Nov 23, 2012, 11:36:55 AM11/23/12
to mathja...@googlegroups.com
You need to put the toMathML call into the MathJax queue so that you
get the MathML version of the math after it is typeset. Here is code
that does that:

__________________________________________________________

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

<script type="text/x-mathjax-config">
MathJax.Hub.Config({
displayAlign: "left",
displayIndent: "2em"
});
</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
var mathml = null; // the PRE tag where MathML is
shown

//
// Get MathML for a jax (asynchronous)
//
var toMathML = function (jax,callback) {
var mml;
try {
mml = jax.root.toMathML("");
} catch(err) {
if (!err.restart) {throw err} // an actual error
return
MathJax.Callback.After([toMathML,jax,callback],err.restart);
}
MathJax.Callback(callback)(mml);
}

var showMathML = function (mml) {
mathml.innerHTML = "";
MathJax.HTML.addText(mathml,mml);
}

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

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

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

<div id="MathOutput">
You typed:
$${}$$
</div>
<p>
MathML for your expression:
<pre id="MathML">
</pre>

</body>
</html>
__________________________________________________________

Hope that does what you need.

Davide


On Nov 20, 2012, at 6:46 AM, <kikk...@gmail.com> <kikk...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages