Skip to content

Instantly share code, notes, and snippets.

@parsd
Last active August 9, 2017 21:35
Embed
What would you like to do?
Convert TeX math expressions into svg via MathJax and Node.js

SVG from TeX Math Expression

Install Node.js

Install Node.js from https://nodejs.org or via your distrubution's package manager. So e.g. for Debian based distros like Ubuntu:

sudo apt install npm

and for Ubuntu to properly install some packages also

sudo apt install nodejs-legacy

Create a Simple MathJax Converter

Create a folder Tex2Svg and open a command prompt here to install mathjax-node:

npm install mathjax-node

And then create tex2svg.js:

#!/usr/bin/env node
var mjAPI = require("mathjax-node");

if (process.argv.length != 3) {
  console.log("No expression given!\n");
  console.log("Usage: " + process.argv[0] + " " + process.argv[1] + " <TeX expression>");
  process.exit(1);
}

mjAPI.start();
var yourMath = process.argv[2];

mjAPI.typeset({
  math: yourMath,
  format: "TeX",
  svg:true,
}, function (data) {
  if (!data.errors)
    console.log(data.svg);
});

Usage

node ./tex2svg.js "f(x) = x^2" > parabola.svg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment