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.
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.
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
GET /render
An endpoint to render LaTeX and MathML formulas to SVG or PNG.
Query parameters:
input
(required): the format of math in input.latex
, mathml
inline
(optional): when input
is latex
, optionally enable "inline" mode.0
, 1
source
(required): the math to be rendered.output
(required): the output format.mathml
, png
, svg
width
, height
(optional): when output
is png
, specify the dimensions of the image to return.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.latex
, mathml
inline
(optional): when input
is latex
, optionally enable "inline" mode.source
(required): the math to be rendered.output
(required): the output format.mathml
, png
, svg
width
, height
(optional): when output
is png
, specify the dimensions of the image to return.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
}
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
Math API is released under the MIT license.