mathjax-node
This repository contains a library that provides an API to call MathJax from Node.js programs. The API converts individual math expressions (in any of MathJax's input formats) into HTML (with CSS), SVG or MathML code.
Use
npm install mathjax-node
to install mathjax-node and its dependencies.
Note:
mathjax-node requires Node.js v4 or later.
Breaking Changes in v1.0:
mathjax-node v1.0 makes breaking changes to the following features from the pre-releases.
- [CHANGED]
lib/mj-single.js
has been renamed to lib/main.js
(and set as main
in package.json
, i.e., require('mathjax-node')
will load it. - [REMOVED]
lib/mj-page.js
(API for processing HTML-fragments) and related CLI tools - [REMOVED] speech-rule-engine integration
- [REMOVED] PNG generation
- [REMOVED] CLI tools in
bin/
These features can easily be recreated in separate modules for greater flexibility. For examples, see
Be sure to also check out other projects on NPM that depend on mathjax-node.
Getting started
mathjax-node provides a library, ./lib/main.js
. Below is a very minimal example for using it - the tests and the examples mentioned above provide more advanced examples.
var mjAPI = require("mathjax-node");
mjAPI.config({
MathJax: {
}
});
mjAPI.start();
var yourMath = 'E = mc^2';
mjAPI.typeset({
math: yourMath,
format: "TeX",
mml:true,
}, function (data) {
if (!data.errors) {console.log(data.mml)}
});
Documentation
mathjax-node exports three methods, config
, start
, typeset
.
config(options)
The config
method is used to set global configuration options. Its default options are
{
displayMessages: false,
displayErrors: true,
undefinedCharError: false,
extensions: '',
fontURL: 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/fonts/HTML-CSS',
MathJax: { }
}
Note. Changes to these options require a restart of the API using the start()
method (see below).
start()
The start
method start (and restarts) mathjax-node. This allows reconfiguration.
Note. This is done automatically when typeset
is first called (see below).
typeset(options, callback)
The typeset
method is the main method of mathjax-node. It expects a configuration object input
and a callback
.
Once called, typeset
can be called repeatedly and will optionally store information across calls (see state
below).
The following are the default input options.
{
ex: 6,
width: 100,
useFontCache: true,
useGlobalCache: false,
linebreaks: false,
equationNumbers: "none",
math: "",
format: "TeX",
xmlns: "mml",
html: false,
htmlNode: false,
css: false,
mml: false,
mmlNode: false,
svg: false,
svgNode: false,
speakText: true,
state: {},
timeout: 10 * 1000,
}
callback(result, options)
mathjax-node returns two objects to the callback
: a result
object as well as the original input options
.
The result
object will contain (at most) the following structure:
{
errors:
mml:
mmlNode:
html:
htmlNode:
css:
svg:
svgNode:
style:
height:
width:
speakText:
state: {
glyphs:
defs :
AMS: {
startNumber:
labels:
IDs:
}
}
}
The options
contains the configuration object passed to typeset
; this can be useful for passing other data along or for identifying which typeset()
call is associated with this callback
call (in case you use the same callback
function for more than one typeset()
).