Skip to content
Frédéric Wang edited this page Feb 23, 2014 · 35 revisions

Usage in a CommonJS program or Web page

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}");

Specifying display and RTL modes

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. See the live demo for how these attributes are handled.

Handling Parsing Error

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.

Extracting the original TeX source

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.

Setting the DOM Parser

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));
Clone this wiki locally