Skip to content

mcanthony/MathJax-node

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mathjax-node Build Status

This repository contains files that provide APIs to call MathJax from node.js programs. There is an API for converting individual math expressions (in any of MathJax's input formats) into SVG images or MathML code, and there is an API for converting HTML snippets containing any of MathJax input formats into HTML snippets containing SVG or MathML.

See the comments in the individual files for more details.

The bin directory contains a collection of command-line programs for converting among MathJax's various formats. These can be used as examples of calling the MathJax API.

Use

npm install mathjax-node

to install mathjax-node and its dependencies.

These API's can produce PNG images, but that requires the Batik library. It should be installed in the batik directory. See the README file in that directory for more details.

Getting started

mathjax-node provides two libraries, lib/mj-single.js and lib/mj-page.js. Below are two very minimal examples -- be sure to check out the examples in ./bin/ for more advanced configurations.

  • lib/mj-single.js is optimized for processing single equations.
// a simple TeX-input example
var mjAPI = require("./lib/mj-single.js");
mjAPI.config({
  MathJax: {
    // traditional MathJax configuration
  }
});
mjAPI.start();

var yourMath = 'E = mc^2';

mjAPI.typeset({
  math: yourMath,
  format: "TeX", // "inline-TeX", "MathML"
  mml:true, //  svg:true,
}, function (data) {
  if (!data.errors) {console.log(data.mml)}
});
  • lib/mj-page.js is optimized for handling full HTML pages.
var mjAPI = require("./lib/mj-page.js");
var jsdom = require("jsdom").jsdom;

var document = jsdom("<!DOCTYPE html><html lang='en'><head><title>Test</title></head><body><h1>Let's test mj-page</h1> <p> \\[f: X \\to Y\\], where \\( X = 2^{\mathbb{N}}\\) </p></body></html>");

mjAPI.start();

mjAPI.typeset({
  html: document.body.innerHTML,
  renderer: "NativeMML",
  inputs: ["TeX"],
  xmlns: "mml"
}, function(result) {
  "use strict";
  document.body.innerHTML = result.html;
  var HTML = "<!DOCTYPE html>\n" + document.documentElement.outerHTML.replace(/^(\n|\s)*/, "");
  console.log(HTML);
});

Packages

No packages published

Languages

  • JavaScript 89.8%
  • HTML 10.2%