npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@scicave/math-latex-parser

v2.2.0

Published

A mathematical (La)TeX parser.

Downloads

31

Readme

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