2.2.0 • Published 3 years ago

@scicave/math-latex-parser v2.2.0

Weekly downloads
62
License
MIT
Repository
github
Last release
3 years ago

math-latex-parser

A mathematical parser for latex in math mode. We mean by mathematical that, arithmetic operations are considered. For example, if you pass "1+2", the result would by a (add node "+") with two children nodes of type number.

See also: math-parser.

Install

npm i @scicave/math-latex-parser

Usage

Browser

<script src="https://cdn.jsdelivr.net/npm/@scicave/math-latex-parser/lib/bundle.js"></script>
<!-- or -->
<script src="https://cdn.jsdelivr.net/npm/@scicave/math-latex-parser/lib/bundle.min.js"></script>

console.log(parse(tex, options));
const {parse} = require('@scicave/math-latex-parser'); 
console.log(parse('12+3^6x \\frac 1 {5+3}'));

AST

Contribute

Feel free to refactor code, enhance the performance, suggest better AST structure. I love open-source stuff ❤️.

See (package.json).scripts.

❯ npm install
❯ npm start # watch and built
❯ # open another terminal:
❯ npm run test:watch # test after builing

Operators Schema

AST Node

The parse function returns a Node, which may have array of other Nodes in its args.

Node.prototype.type

The Node type, see the available types.

Node.prototype.check(props: Object, checkArgs=false)

This method can check all properties except args, it will be ignored. Args will be checked if the 2nd arg (checkArgs) passed to check is true.

let node = mathLatexParser.parse("\\alpha!");
console.log(node.check({
  type: "operator",
  operatorType: "postfix",
  name: "!"
}));
// true

Node.prototype.checkType(type: string)

You can check for type directly here, but why not node.type === "the_type"? Because "the_type" is not a valid type, .checkType will throw if you passed invalid type.

let node = mathLatexParser.parse("1");
console.log(node.checkType("member expression"));
// false

Node.prototype.hasChild(props: Object, checkArgs=false)

This method can check all properties except args, it will be ignored. Args will be checked if the 2nd arg (checkArgs) passed to check is true.

let node = mathLatexParser.parse("1+2");
// { type: "operator", args: [...], operatorType: "infix" }
console.log(node.hasChild({ type: "number", value: 1 }));
// true

Node.prototype.hasChildR(props: Object, checkArgs=false)

The same as hasChild, but recursively.

let node = mathLatexParser.parse("\\sin(1+2)");
// { type: "function", name: "sin", args: [...], isBuiltin: true }
console.log(node.hasChildR({ type: "number", value: 1 }));
// true

Node.types

Available values for Node.prototype.type.

Array of literal strings: Node.types.values.

All Valid operators: Node.types.operators.

Options

.autoMult

Type = boolean, default = true.

You can parse some thing like this 3^6cd\sqrt af, if false, the previous latex expression will throw an error while being parsed.

.functions

Type = Array<Checker>, default = [].

This is useful in some cases like, f(x), or f\left(x\right), the function id here is f.

.builtinLetters

Type = Array<Checker>, default:

[
  "alpha", "Alpha", "beta", "Beta", "gamma",
  "Gamma", "pi", "Pi", "varpi", "phi", "Phi",
  "varphi", "mu", "theta", "vartheta", "epsilon",
  "varepsilon", "upsilon", "Upsilon", "zeta", "eta",
  "Lambda", "lambda", "kappa","omega", "Omega",
  "psi", "Psi", "chi", "tau", "sigma", "Sigma",
  "varsigma", "rho", "varrho", "Xi", "xi", "nu",
  "imath", "jmath", "ell", "Re", "Im", "wp", "Nabla",
  "infty", "aleph", "beth", "gimel", "comicron",
  "iota", "delta", "thetasym", "omicron", "Delta",
  "Epsilon", "Zeta", "Eta", "Theta", "Iota", "Kappa",
  "Mu", "Nu", "Omicron", "Rho", "Tau", "Chi", "infty",
  "infin", "nabla", "mho", "mathsterling", "surd",
  "diamonds", "Diamond", "hearts", "heartsuit", "spades",
  "spadesuit", "clubsuit", "clubs", "bigstar", "star",
  "blacklozenge", "lozenge", "sharp", "maltese", "blacksquare",
  "square", "triangle", "blacktriangle"
]

If you want to expand the defaults put "..." as the first item in the array, at index 0.

.builtinFunctions

Type = Array<Checker>, default:

[
  "sinh", "cosh", "tanh", "sin", "cos", "tan", "sec",
  "csc", "cot", "arcsin", "arccos", "arctan", "arcsec",
  "arccsc", "arccot", "ln"
]

If you want to expand the defaults put "..." as the first item in the array, at index 0.

.extra

All extra features are enabled.

Example:

let tex = String.raw`
  \begin{pmatrix}
    1 & 2 \\
    3 & 4
  \end{pmatrix}
`;
mathLatexParser.parse(tex, {
  extra: {
    matrices: true, // default
  }
});
  • memberExpressions, for example:
    • p.x
    • f(x).a(y).r.
  • intervals: true or false, will return node with properties { startInlusive: boolean, endInclusive: boolean }.
    • [1,2]
    • (-.5, \infin)
    • (-pi, 1]
    • [2,5)
  • sets: e.g., \big{ 1, \sqrt \pi, ..., \left(\sqrt \pi\right)^10 \big}
  • tuples: e.g., (1, 2, x, ...)
  • matrices: e.g., \begin{matrix} ... \end{matrix}
  • ellipsis: to allow the 3-dots "...", e.g., \{{ 1, 3, 5, ... \}}

Notes

  • You can use ellipsis as valid Factor, e.g., 1 + 2 + ... + 10
  • This expression will throw syntax error, 1 + 2 + (...) + 10
  • extra.ellipsis is more customizable:

    • extra.ellipsis.matrices: boolean
    • extra.ellipsis.tuples: boolean
    • extra.ellipsis.sets: boolean
    • extra.ellipsis.funcArgs: boolean, to be used as a function argument.
  • Intervals, should have 2 terms as math expression:

    • (..., a]: throw syntax error.
    • (..., a): is a tuple.

Checker

type Checker = RegExp | ((...args:any[])=>boolean) | Checker[];

License

MIT

2.2.0

3 years ago

2.1.1

3 years ago

2.1.0

3 years ago

2.0.0

3 years ago

1.2.1

3 years ago

1.2.0

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago