node package manager



Quickly convert math equations into beautiful SVGs (or PNGs/MathML). A port of the Ruby Mathematical for Node.js.

Build Status



npm install mathematical


The simplest way to do this is

Mathematical = require 'mathematical'
new Mathematical.render(string_with_math)

string_with_math should just be a string of math TeX inline ($..$) or display ($$..$$) style math.

The output will be a hash, with keys that depend on the format you want:

  • If you asked for an SVG, you'll get:
    • width: the width of the resulting image
    • height: the height of the resulting image
    • svg: the actual string of SVG
  • If you asked for a PNG, you'll get:
    • width: the width of the resulting image
    • height: the height of the resulting image
    • png: the PNG data
  • If you asked for MathML, you'll get:
    • mathml: the MathML data


new Mathematical takes an optional object to define a few options:

  • ppi - A double determining the pixels per inch of the resulting SVG (default: 72.0).
  • zoom - A double determining the zoom level of the resulting SVG (default: 1.0).
  • base64 - A boolean determining whether Mathematical's output should be a base64-encoded SVG string (default: false).
  • maxsize - A numeral indicating the MAXSIZE the output string can be. (default: unsigned long).
  • format - A string indicating whether you want an "svg", "png", or "mathml" output. (default: svg).

Pass these in like this:

opts = { ppi: 200.0, zoom: 5.0, base64: true }
renderer = new Mathematical(opts)
renderer.render('$a \ne b$')


Before building this gem, you must install the following programs and libraries:

  • glib-2.0
  • gdk-pixbuf-2.0
  • xml2
  • cairo
  • pango
  • libffi
  • ruby
  • bison

You will also need fonts for cmr10, cmmi10, cmex10, and cmsy10.

Mac install

To install these dependencies on a Mac, everything can be installed via Homebrew:

brew install glib gdk-pixbuf cairo pango libffi

Install the fonts with:

cd ~/Library/Fonts
curl -LO \
     -LO \
     -LO \
     -LO \
     -LO \
     -LO \
     -LO \
     -LO \

xml2 should already be on your machine.

*nix install

To install these dependencies on a *nix machine, fetch the packages through your package manager. For example:

sudo apt-get -qq -y install libglib2.0-dev libxml2-dev libcairo2-dev libpango1.0-dev libffi ttf-lyx libgdk-pixbuf2.0-dev

Windows install

On a Windows machine, I have no idea. Pull requests welcome!


After cloning the repo:

grunt test

If there were no errors, you're done! Otherwise, make sure to follow the dependency instructions.