math api

➗ Serverless API to render maths using MathJax for Node.

43
8
JavaScript

Math API logo

Math API • Render LaTeX and MathML formulas as SVG or PNG.


A REST API to do fancy things with formulas, like rendering LaTeX or MathML to
SVG or PNG on the server side using MathJax for Node,
while leveraging expensive computations on the client.

As a Serverless application

You can deploy this repository as a serverless application using an AWS CloudFormation
Template to create an AWS API Gateway that invokes Lambda functions to serve requests.

Launch this stack on AWS

As a Docker image

You can pull and run a Docker container to deploy the API on your local machine,
server, Kubernetes cluster, whatever!

To start the container (it will bind on http://localhost:3000/):

docker run --name math-api -d -p 3000:3000 chialab/math-api

Endpoints

GET /render

An endpoint to render LaTeX and MathML formulas to SVG or PNG.

Query parameters:

  • input (required): the format of math in input.
    Valid values: latex, mathml
  • inline (optional): when input is latex, optionally enable “inline” mode.
    Valid values: 0, 1
  • source (required): the math to be rendered.
    Valid values: string, depends on the input type
  • output (required): the output format.
    Valid values: mathml, png, svg
  • width, height (optional): when output is png, specify the dimensions of the image to return.
    Valid values: positive integers

Examples:

GET /render?input=latex&output=svg&source=x^2 HTTP/1.1
Accept: image/svg+xml
GET /render?input=latex&inline=1&output=png&source=x^2&width=512 HTTP/1.1
Accept: image/png

POST /render

An endpoint to render LaTeX and MathML formulas to SVG or PNG.

Request body (JSON):

  • input (required): the format of math in input.
    Valid values: latex, mathml
  • inline (optional): when input is latex, optionally enable “inline” mode.
    Valid values: boolean
  • source (required): the math to be rendered.
    Valid values: string, depends on the input type
  • output (required): the output format.
    Valid values: mathml, png, svg
  • width, height (optional): when output is png, specify the dimensions of the image to return.
    Valid values: positive integers

Examples:

POST /render
Accept: image/svg+xml
Content-Type: application/json

{
    "input": "latex",
    "output": "svg",
    "source": "e^{i \\pi} + 1 = 0"
}
POST /render
Accept: image/png
Content-Type: application/json

{
    "input": "latex",
    "inline": true,
    "output": "png",
    "source": "e^{i \\pi} + 1 = 0",
    "width": 512
}

Development

All the following instructions assume you have at least NodeJS and Yarn installed.

Start the application locally:

yarn start

Run unit tests:

yarn run test

Start a simulated AWS API Gateway (provided you have AWS SAM Local and Docker installed):

yarn run api-gateway

Validate CloudFormation template (provided you have AWS CLI installed)

make validate

Package CloudFormation template (provided you have AWS CLI and Docker installed)

make layers (this is needed only the first time, then when updating MathJax version)
make package

Deploy CloudFormation template (provided you have AWS CLI and Docker installed)

make deploy
make deploy ENVIRONMENT=Production


License

Math API is released under the MIT license.