http://www.mathjax.org/resources/docs/?queues.html#the-mathjax-processing-queue
and looking at the MathJax/test/sample-dynamic.html file in the
MathJax distribution.
Basically, when you want to handle new mathematics that has been added
to the page, use
MathJax.Hub.Queue(["Typeset",MathJax.Hub,element]);
where "element" is either a string containing the ID of the DOM
element whose contents is to be processed, or a reference to the DOM
element itself (e.g., something returned by document.getElementById()
or a similar function).
Davide
In any case, here is a working example:
<html>
<head>
<title>MathJax: Test dynamic math</title>
<script src="/MathJax/MathJax.js">
MathJax.Hub.Config({
jax: ["input/TeX","output/HTML-CSS"],
extensions: ["tex2jax.js"]
});
</script>
<script>
var n = 0;
var equation = [
'This is math: $$x+1\\over x-1$$',
'More math: if $x^2-x-2=0$ then $x = 2$ or $x = -1$.',
'Last math: $\\sqrt{x^2+1}$.'
];
function ChangeMath () {
document.getElementById("math-div").innerHTML = equation[n];
n++; if (n >= equation.length) {n = 0}
MathJax.Hub.Queue(["Typeset",MathJax.Hub,"math-div"]);
}
</script>
</head>
<body>
<input type="button" onclick="ChangeMath();" value="Change Math">
<p>
<div id="math-div" style="border:3px groove; padding: 3px; height:
8em; width:30em"></div>
</body>
</html>
As for the mathematics, your code snippet doesn't include the call to
MathJax, so it is not clear how you are combining this with the
updating of the math. I suspect you may be doing
titleSwitch("new title");
replaceMath();
which is not correct. Your XmlHttpRequest is asynchronous, so the
titleSwitch() call simply initiates the request and then returns.
That means replaceMath() will run before the response form the
XmlHttpRequest call is completed, and so before the contents of the
div has been replaced.
If you put
MathJax.Hub.Queue(["Typeset",MathJax.Hub,"video_title"]);
after
document.getElementById("video_title").innerHTML=xmlhttp2.responseText;
in the onreadystatechange handler, then I suspect the math will update
properly for you.
Davide
PS, I'm glad you found another way to do what you wanted. Including
all the mathematics initially and hiding all but one is often a good
approach.
This seems like a common use case that would be nice to document better. I'd like to collect together the info from this thread and put together a page with the sample code and discussion, and get it into the docs, and/or put it up on mathjax.org as an article.
However, it will probably happen a lot sooner if someone else would like to take that on as a project! We could at least give you attribution and plug your website/blog/MathJax-related project, etc. As a start, you could even do a page in your own blogs/sites and we could point to you.
Any takers?
--Robert
Robert Miner
Vice President, Research and Development
MathJax Project Coordinator
Design Science, Inc.
140 Pine Avenue, 4th Floor
Long Beach, California 90802
USA
Main: (562) 432-2920
Direct: (651) 223-2883
Fax: (651) 292-0014
rob...@dessci.com
www.dessci.com