Skip to content

Commit e4e0ec5

Browse files
ho-man-chanarzola
andauthored
chore: add claudia deploy instruction to use linux dependencies (#114)
* chore: add claudia deploy instruction to use linux dependencies * improve readme --------- Co-authored-by: Oscar Arzola <[email protected]>
1 parent e94b1b2 commit e4e0ec5

File tree

1 file changed

+89
-9
lines changed

1 file changed

+89
-9
lines changed

README.md

+89-9
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ It works like this:
1212
I.e.
1313

1414
```html
15-
<img
16-
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"
15+
<img
16+
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"
1717
>
1818
```
1919

@@ -27,11 +27,11 @@ Such a URL returns a PNG containing math rendered by LaTeX.
2727

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

30-
Pressbooks users wanted a [MathJax](https://www.mathjax.org/) solution.
30+
Pressbooks users wanted a [MathJax](https://www.mathjax.org/) solution.
3131

3232
MathJax's [CommonHTML output](http://docs.mathjax.org/en/latest/options/output-processors/CommonHTML.html) works great in webbooks, but not in PDFs, EPUBs, MOBIs, ...
3333

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

3636
The SVGs and PNGs are generated as follows:
3737

@@ -53,23 +53,94 @@ Mix and match `fg=<RRGGBB>`, `font=<string>` and `dpi=<number>` as needed.
5353
+ Font: `http://localhost:3000/latex?latex=<LaTeX>&font=Gyre-Pagella&svg=1`
5454

5555
Ie. same as PNG above with `svg=1` added. Because SVGs are vector images, DPI is not used.
56-
56+
5757
## AsciiMath and MathML
5858

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

6161
+ AsciiMath: `http://localhost:3000/asciimath?asciimath=<AsciiMath>` `...`
6262
+ MathML: `http://localhost:3000/mathml?mathml=<MathML>` `...`
6363

64+
## Base64 Encoded Formulas
65+
66+
You can now pass base64 encoded formulas to avoid URL encoding issues with complex mathematical expressions:
67+
68+
+ LaTeX: `http://localhost:3000/latex?latex=eF4yICsgeV4yID0gej4y&isBase64=true`
69+
+ AsciiMath: `http://localhost:3000/asciimath?asciimath=c3VtXyhpPTEpXm4gaV4zPSgobihuKzEpKS8yKV4y&isBase64=true`
70+
+ MathML: `http://localhost:3000/mathml?mathml=PG1hdGg-PG1pPng8L21pPjwvbWF0aD4&isBase64=true`
71+
72+
The `isBase64` parameter can be set to either `true` or `1`. For URL safety, the base64 encoded formulas above don't include padding characters (`==`).
73+
74+
### Base64 Encoding Examples
75+
76+
Here are some examples of formulas and their URL-safe base64 encoded versions (without padding):
77+
78+
| Formula | URL-safe Base64 Encoded |
79+
|---------|----------------|
80+
| `x^2 + y^2 = z^2` | `eF4yICsgeV4yID0gej4y` |
81+
| `\frac{-b \pm \sqrt{b^2-4ac}}{2a}` | `XGZyYWN7LWIgXHBtIFxzcXJ0e2JeMi00YWN9fXsyYX0` |
82+
| `\int_0^\infty e^{-x^2} dx = \frac{\sqrt{\pi}}{2}` | `XGludF8wXl5pbmZpbml0eSBlXnsteFxeMn0gZHggPSBcZnJhY3tcc3FydHtccGl9fXsyfQ` |
83+
84+
When encoding formulas for use with this service:
85+
1. Convert your formula to base64
86+
2. Replace any `+` with `-`
87+
3. Replace any `/` with `_`
88+
4. Remove any trailing `=` padding characters
89+
90+
### Example of Base64 Encoding in PHP
91+
92+
```php
93+
function urlSafeBase64Encode($string) {
94+
$base64 = base64_encode($string);
95+
$urlSafe = strtr($base64, '+/', '-_');
96+
return rtrim($urlSafe, '='); // Remove padding
97+
}
98+
99+
$formula = '\frac{-b \pm \sqrt{b^2-4ac}}{2a}';
100+
$encodedFormula = urlSafeBase64Encode($formula);
101+
$url = "http://localhost:3000/latex?latex={$encodedFormula}&isBase64=true";
102+
```
103+
104+
### Example of Base64 Encoding in JavaScript
105+
106+
```javascript
107+
function urlSafeBase64Encode(string) {
108+
const base64 = btoa(string);
109+
const urlSafe = base64.replace(/\+/g, '-').replace(/\//g, '_');
110+
return urlSafe.replace(/=+$/, ''); // Remove padding
111+
}
112+
113+
const formula = '\\frac{-b \\pm \\sqrt{b^2-4ac}}{2a}';
114+
const encodedFormula = urlSafeBase64Encode(formula);
115+
const url = `http://localhost:3000/latex?latex=${encodedFormula}&isBase64=true`;
116+
```
117+
118+
## Additional Rendering Parameters
119+
120+
You can now customize the rendering with these additional parameters:
121+
122+
+ `em`: Font size in pixels (default: 16)
123+
+ `ex`: x-height in pixels (default: 8)
124+
+ `width`: Container width in pixels (default: 1000)
125+
+ `lineWidth`: Line width in pixels (default: 1000)
126+
+ `scale`: Scaling factor (default: 1)
127+
128+
### Examples with Additional Parameters
129+
130+
+ Larger font size: `http://localhost:3000/latex?latex=x^2+y^2=z^2&em=24&ex=12`
131+
+ Custom width: `http://localhost:3000/latex?latex=x^2+y^2=z^2&width=800&lineWidth=800`
132+
+ Scaled formula: `http://localhost:3000/latex?latex=x^2+y^2=z^2&scale=1.5`
133+
+ Combined parameters: `http://localhost:3000/latex?latex=x^2+y^2=z^2&em=20&scale=1.2&fg=0000ff&svg=1`
134+
64135
# Installation
65136

66-
Install Node.js 10.x LTS, Then:
137+
Install Node.js 18.x LTS, Then:
67138

68139
git clone [email protected]:pressbooks/pb-mathjax.git
69140
cd pb-mathjax
70141
npm install
71142
npm start
72-
143+
73144
Finally, go to: `http://localhost:3000/`
74145

75146
## Deploy to a Production Server
@@ -90,11 +161,20 @@ More info: http://pm2.keymetrics.io/docs/usage/quick-start/
90161
Install [Claudia.js](https://claudiajs.com/), then:
91162

92163
cd ~/code/github/pressbooks/pb-mathjax
93-
claudia create --handler lambda.handler --deploy-proxy-api --region us-east-1 --timeout 15 --memory 256 --profile yourself
164+
claudia create --handler lambda.handler --deploy-proxy-api --region us-east-1 --timeout 15 --memory 256 --profile yourself --use-local-dependencies
94165

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

97168
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`
98169

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

172+
If you are deploying not for the first time, use
173+
174+
```
175+
rm -rf node_modules package-lock.json
176+
npm install --arch=x64 --platform=linux
177+
claudia update --use-local-dependencies
178+
```
179+
180+
This will download the dependencies needed for Linux and deploy to AWS Lambda.

0 commit comments

Comments
 (0)