npm install mathup
import mathup from "mathup";
Download one of the released assets and include the module:
<script type="module" src="mathup.mjs"></script>
…the custom element:
<script type="module" src="math-up-element.mjs"></script>
…or the script:
<script src="mathup.iife.mjs"></script>
const expression = "1+1 = 2";
const options = {}; // optional
const mathml = mathup(expression, options);
mathml.toString();
// => "<math><mrow><mn>1</mn><mo>+</mo><mn>1</mn></mrow><mo>=</mo><mn>2</mn></math>"
const mathNode = mathml.toDOM();
// => [object MathMLElement]
// Update existing <math> node in place
mathup("3-2 = 1", { bare: true }).updateDOM(mathNode);
<math-up
display="inline"
dir="ltr"
decimal-mark=","
col-sep=";"
row-sep=";;"
>
1+1 = 2
</math-up>
npm install -g mathup
mathup [options] -- <expression>
# or from stdin
echo <expression> | mathup [options]
const options = {
decimalMark: ".", // -m, --decimalmark="."
colSep: ",", // -c, --colsep=","
rowSep: ";", // -r, --rowsep=";"
display: "inline", // -d, --display
dir: "ltr", // --rtl
bare: false, // -b, --bare
}
This package exposes a single function mathup
that intuitively takes
simple mathematical expressions—written in a markup language inspired
by AsciiMath—and outputs structured
MathML.
You can use it on the command line or on the server as an
npm package, or in the browser by including the
script source. In the browser, you choose how to parse the math in
your document—by looking hard for any math-y substrings, parsing all
expressions wrapped in $
…$
, or using some other excellent tools out
there that does it for you. And you can choose what to do with the
output as well—piping it to another program, inject it streight to the
DOM, or just logging it to the console.
MathJax is an excellent tool that you should probably be using if all you want to do is include complex mathematical expressions in a document. And you should probably use it along side this package as well if you want Chrome users to be able to read your expressions. However, MathJax is a complex piece of software that does a great deal more than just translate simple expression into structured form, and if that is all you want to do, then MathJax is definitely overkill. Mathup promises to be a lot faster (by doing less) then MathJax, and if the readers of your document (or users of your app) are using a standard conforming browser, they will benefit a great bit. You will be able to translate your expression on the server before your readers even open the document, reducing any lag time to native.
I wrote this tool, because I wanted to be able to author mathematical
expressions quickly, with no overhead (imagine 1/2
instead of
\frac{1}{2}
). TeΧ expressions can easily become verbose and annoying
to write (especially on keyboards with complex access to the
\ , {, and } keys). However, the
purpose of this package is not to give people complete control over
MathML in a non-verbose way, the purpose is to make it simple for
people to write simple expression. Of course I’ll try to give as much
expressive power as possible in the way, but I won’t promise to make
all complex things possible.
If you want full support of MathML, and don’t want to write all those tags perhaps you should look for another tool. There are other great efforts to enable people to author MathML in TeX format, take a look at TeXZilla for example.
Run the test suites with:
npm test
As for manual and visual tests, if you are running node 13 or newer, you don’t need to compile between edit and run as the code is written without transpilation in mind. The code works in both browsers and node without any transcompilation.
For a simple test do:
./bin/mathup.mjs -- 'my expression'
You can open a playground and test cases on http://localhost:8000/demo by running:
npm run server