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.

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

Parsing TeX expressions in your Web page

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 (&lt; and &amp;). See this example.

Web Components Custom Element

TODO: try http://www.x-tags.org/???

Clone this wiki locally