Phactorial
Phactorial typesets mathematics in plain PHP. It reads TeX or MathML and writes SVG, PNG, MathML, TeX, Word equations (OMML) or plain text. It needs PHP 8.4 with ctype, dom and mbstring. No Node, no browser, no fonts on the server.
composer require phactorial/phactorialuse Phactorial\MathRender;
echo MathRender::fromTex('x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}')->display()->svg(pad: 1);
Every glyph is a path, so the SVG looks the same everywhere and needs no web font.
Reads and writes
It reads TeX (including \ce and \pu chemistry), MathML, MathType and Wiris text, and the Math builder in PHP. It writes the formats in the table below. Every reader produces the same tree, so Phactorial also converts: TeX to MathML, MathML to TeX, either to Word.
What it does
A formula passes through three stages.
- Read.
Math::tex()andMath::mathml()turn source text into a formula tree. TheMathbuilder makes the same tree in PHP. - Lay out.
Math::layout()sets the tree in an OpenType math font, by the rules TeX and MathML Core use. - Write. A writer turns the tree or the layout into output.
MathRender runs all three in one call. The classes behind it stay public for finer control.
| Output | From | Use it for |
|---|---|---|
| SVG | the layout | Web pages, any vector pipeline |
| PNG | the layout | Email, chat, anything that won't show SVG (needs ext-imagick) |
| MathML | the tree | Browsers that render MathML themselves, screen readers |
| TeX | the tree | Editing, storage, KaTeX or MathJax on the client |
| OMML | the tree | Word documents, as live equations a reader can edit |
| Plain text | the tree | Alt text, search indexes, plain-text email |
Compared with KaTeX and MathJax
KaTeX and MathJax are the standard tools for math on the web, and they are excellent. Both are JavaScript, so a PHP site runs Node beside PHP or renders in the visitor's browser.
Phactorial is the PHP-only alternative. It reads the TeX KaTeX reads (gaps listed under Input), runs in your PHP process and returns a string. It also writes Word equations, which neither JavaScript library does.
phpoffice/math converts between MathML and Word's format but does not typeset. Phactorial also reads TeX and draws the formula.
Not yet supported: line breaks inside long formulas, color and boxes within a formula, and equation numbers.
Speed and safety
Once the font is loaded, TeX to SVG takes about 0.1 ms per formula. The first formula in a new PHP process takes about 15 ms without opcache, with classes and font loaded from scratch. Time and memory grow linearly with formula size. bin/benchmark.php in the repository measures these on your machine.
The readers never throw. Input that is too large or too deep is cut off at fixed limits, drawn as far as it got and reported, so formulas typed by users are safe to render. See Untrusted input.
Where next
- Recipes: formulas in a web page, in email, in Word; converting between formats; alt text; untrusted input.
- Input: the builder, TeX and MathML, and what each reader reports.
- Output: the writers and their options.
- Custom backends: drawing a layout yourself, and other math fonts.
- CommonMark: math in Markdown, typeset on the server.
- Quoin uses Phactorial for equations in PDF, Word and HTML documents.