0

I use this command

<div id="formulaOut"><script type="math/tex">z = \sqrt{x^2+y^2}</script></div>

to show a line in Tex format. Now I want to change the content of the line (JavaScript), but I still want to interpret it in the Tex format. When I put a button, I execute the following function:

function Calculation() {
    document.getElementById("formulaOut").innerHTML= 'x^2+y^2';
}

But like this, the String is not anymore interpreted as Tex. Any suggestion how I can solve this problem?

Martin Schröder
  • 4,176
  • 7
  • 47
  • 81
el smu
  • 35
  • 1
  • 2
  • 8
  • 1
    I'm guessing you're using a plugin that parses based on Knuth's Tex, that runs on pageload, and adding different markup later with javascript doesn't work as the plugin won't run on that markup. – adeneo Jun 17 '13 at 12:11
  • I'm using http://docs.mathjax.org – el smu Jun 17 '13 at 12:24

1 Answers1

1

Try using

function Calculation() {
  var jax = MathJax.Hub.getAllJaxFor("formulaOut")[0];
  MathJax.Hub.Queue("Text",jax,"x^2+y^2");
}

instead.

The problem is that you have:

  1. Replaced the <script type="math/tex"> that identifies the mathematics for MathJax by the string x^2+y^2 that does not identify any mathematics to be typeset, and
  2. You did not tell MathJax that it needs to re-typeset the contents of the div once the new math is there.

The code I provide looks up the math element created by MathJax and tells it that it should change its TeX code to the new string (this automatically re-typesets it).

Davide Cervone
  • 11,211
  • 1
  • 28
  • 48