-
Notifications
You must be signed in to change notification settings - Fork 22
Using TeXZilla
You can use TeXZilla the standard way in any Javascript program. For example in a commonJS program, to convert a TeX source into a MathML source:
var TeXZilla = require("./TeXZilla");
console.log(TeXZilla.toMathMLString("\\sqrt{\\frac{x}{2}+y}"));
Or in a Web Page, to convert a TeX source into a MathML DOM element.
<script type="text/javascript" src="TeXZilla-min.js"></script>
...
var MathMLElement = TeXZilla.toMathML("\\sqrt{\\frac{x}{2}+y}");
One can use the MathML display="block" and dir="rtl" attributes on the <math> element to render equations in display and RTL modes respectively. You can pass optional booleans aDisplay
and aRTL
to TeXZilla.toMathMLString or TeXZilla.toMathML in order to ask TeXZilla to do that.
By default, TeXZilla will return parsing errors in an merror element. You can change that behavior by specifying aThrowExceptionOnError = true when calling TeXZilla.toMathMLString or TeXZilla.toMathML. In that case, TeXZilla will throw an exception that you can catch normally. See this example that demonstrates an automatic preview dealing with parsing failures.
The TeXZilla converter will save the TeX source in a semantics annotation. You can retrieve it using TeXZilla.getTeXSource. For example this simple TeXZilla-show-source.js script will show the TeX source when the user double clicks on a MathML equation. See this example. Note that other tools may access this TeX source such as the MathML copy add-on.
Some TeXZilla functions require a DOM Parser. If new DOMParser()
does not work in your Javascript program, you must set the DOM parser by hand using the setDomParser function. For example using Mozilla's XPCOM interface:
TeXZilla.setDOMParser(Components.
classes["@mozilla.org/xmlextras/domparser;1"].
createInstance(Components.interfaces.nsIDOMParser));
or for Firefox Add-on SDK:
var {Cc, Ci} = require("chrome");
TeXZilla.setDOMParser(Cc["@mozilla.org/xmlextras/domparser;1"].
createInstance(Ci.nsIDOMParser));
This simple TeXZilla-parse.js script will convert the TeX content of all the <span>
and <div>
elements with class="tex"
into inline and display MathML equations. To use it, just do
<script type="text/javascript" src="TeXZilla-min.js"/>
<script type="text/javascript" src="TeXZilla-parse.js"/>
...
... <div class="tex">∑_{n=1}^{+∞} \frac{1}{n^2} = \frac{π^2}{6}</div> ...
... <span class="tex">\frac{\sqrt{x_1}}{3} + y^2</span>...
Note that as any normal HTML document, the special characters < and & must be escaped (< and &). See this example.
TODO: try http://www.x-tags.org/???