Skip to content

Instantly share code, notes, and snippets.

@parsd
Last active August 9, 2017 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parsd/40209f5822ebd394891b03893c7f8acf to your computer and use it in GitHub Desktop.
Save parsd/40209f5822ebd394891b03893c7f8acf to your computer and use it in GitHub Desktop.
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