pressbooks / pb-mathjax

A replacement for wp.com/latex.php but instead of LaTeX, it uses MathJax.
GNU General Public License v3.0
4 stars 1 forks source link
asciimath latex mathjax mathml microservice

Synopsis

This is a replacement for https://wp.com/latex.php but instead of LaTeX, it uses MathJax.

Auttomatic's open-source Jetpack plugin has a LaTeX feature that is hardcoded to call https://s0.wp.com/latex.php

It works like this:

I.e.

<img 
src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+P_%5Cnu%5E%7B-%5Cmu%7D%28z%29%3D%5Cfrac%7B%5Cleft%28z%5E2-1%5Cright%29%5E%7B%5Cfrac%7B%5Cmu%7D%7B2%7D%7D%7D%7B2%5E%5Cmu+%5Csqrt%7B%5Cpi%7D%5CGamma%5Cleft%28%5Cmu%2B%5Cfrac%7B1%7D%7B2%7D%5Cright%29%7D%5Cint_%7B-1%7D%5E1%5Cfrac%7B%5Cleft%281-t%5E2%5Cright%29%5E%7B%5Cmu+-%5Cfrac%7B1%7D%7B2%7D%7D%7D%7B%5Cleft%28z%2Bt%5Csqrt%7Bz%5E2-1%7D%5Cright%29%5E%7B%5Cmu-%5Cnu%7D%7Ddt&amp;fg=000000" 
>

If we squint real hard, we can break down img src into $_GET parameters:

latex.php ? latex=<LaTeX> & fg=<ForegroundColor> & ...

Such a URL returns a PNG containing math rendered by LaTeX.

Hot Swap

Prior to the existence of this microservice, we called wp.com/latex.php for our math needs. (Thanks WordPress!)

Pressbooks users wanted a MathJax solution.

MathJax's CommonHTML output works great in webbooks, but not in PDFs, EPUBs, MOBIs, ...

Nowadays, Pressbooks uses CommonHTML output in webbooks, SVGs in PDFs, and PNGs in MOBI/EPUBs.

The SVGs and PNGs are generated as follows:

LaTeX

PNG:

Mix and match fg=<RRGGBB>, font=<string> and dpi=<number> as needed.

SVG:

Ie. same as PNG above with svg=1 added. Because SVGs are vector images, DPI is not used.

AsciiMath and MathML

Same as LaTeX above but instead of latex?latex=<LaTeX> do:

Installation

Install Node.js 10.x LTS, Then:

git clone git@github.com:pressbooks/pb-mathjax.git
cd pb-mathjax
npm install
npm start

Finally, go to: http://localhost:3000/

Deploy to a Production Server

Install PM2 on your server, then:

cd ~/code/github/pressbooks/pb-mathjax
npm install --only=prod
pm2 start bin/www --name pb-mathjax

Pb-mathjax will be available at http://YOURSERVER:3000/ and will run forever (or until you kill PM2.) Use in Pressbooks as the value for PB_MATHJAX_URL

More info: http://pm2.keymetrics.io/docs/usage/quick-start/

Deploy to AWS Lambda

Install Claudia.js, then:

cd ~/code/github/pressbooks/pb-mathjax
claudia create --handler lambda.handler --deploy-proxy-api --region us-east-1 --timeout 15 --memory 256 --profile yourself

Where us-east-1 is your AWS region and yourself corresponds to an identity in your ~/.aws/credentials file.

If everything goes well, the above command will finish after a few moments and print a response with a URL. Use in Pressbooks as the value for PB_MATHJAX_URL

More info: https://claudiajs.com/tutorials/installing.html https://github.com/claudiajs/claudia/blob/master/docs/