If you are developing a web page where mathematics may be generated on the fly (using JavaScript) after the page has been displayed, you will need to have jsMath process that mathematics once it has been created. One way would be simply to create the new Dynamic Mathematics using jsMath
SPAN
orDIV
elements (withCLASS="math"
) and then calljsMath.ProcessBeforeShowing()
after they have been inserted into the page. (You should usejsMath.ProcessBeforeShowing()
notjsMath.Process()
in this case, asjsMath.Process
runs asynchronously, and if you were to call it a second time while the first call is till running, jsMath will get confused.)Note that this will cause jsMath to look through the entire web page for mathematics, and that may be inefficient, especial for large pages or ones that already have substantial math content (since the typeset mathematics contains lots of
SPAN
tags that would have to be scanned to see if they are of classmath
). Instead, you may wish to restrict the action ofjsMath.ProcessBeforeShowing()
to just the new material. To do this, enclose the new text to be scanned in aSPAN
orDIV
and pass that tojsMath.ProcessBeforeShowing()
. For example,var div = document.createElement('div'); div.innerHTML = 'If <SPAN CLASS="math">x = 3</SPAN> then <SPAN CLASS="math">x^2 = 9</SPAN>.' jsMath.ProcessBeforeShowing(div); document.body.appendChild(div);creates a newDIV
element and sets its contents, then processes the mathematics that it contains, and finally adds it to the end of the current document. See also the section on thetex2math
plugin for how to process text containing dollar signs automatically.It is also possible to create an element that contains just mathematics and then ask jsMath to process it directly (rather than look through it for math tags). In this case, create a
SPAN
orDIV
that contains the TeX code you want to typeset, and then calljsMath.ProcessElement()
on it. For example,var math = document.createElement('span'); math.innerHTML = '\\sum_{i=1}^{n} i = {n(n+1)\\over 2}'; jsMath.ProcessElement(math); document.body.appendChild(span);would create an in-line math element (since it is aSPAN
), process it, and add it to the bottom of the page. Note the doubling of the backslashes: this is because in a JavaScript string, the backslash is an escape character, so you need to escape it (double it) to actually get a slash within the string itself.
|
|