diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..09d1b3d --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,26 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + + +## [Unreleased] + + +## [1.1.0] - 2020-3-9 +### Added +- Tests for the basic arithmetics and autoMult option +- Tests for options.functions +- Preparing input before parsing, enabling parsing when the passed expression has some unimportant groups such as `as{da } ^{13}{/6}` -> `as da ^{13} /6 ` +- Types "prod", "sqrt" to Node types. + + +### Changed +- the error message when parsing `\dfhgw`, which is not defined, the parser throw "undefined control sequence dfhgw"; +- Sqrt now is parsed with type = "sqrt" not "function" as before. + +### Fix + +- `\lef( [expression] \right)`: an error was thrown when we try to parse such an epxression, it considered right as control sequance name +- Make the argument for BlockParentheses instance of Array diff --git a/README.md b/README.md index 82329e3..6eb54a6 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,125 @@ # math-latex-parser -A mathematical parser for latex in math mode. We mean by mathematical that, arithmetic operations is considered for example if you pass "1+2", the result would by a (add node "+") with two children nodes of type number. +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. + +# Install + +``` +npm i @scicave/math-latex-parser +``` + +# Usage + +```js +const {parse} = require('@scicave/math-latex-parser'); + +console.log(parse('12+3^6x \\frac 1 {5+3}')); +/* + You ge some thing like this + Notice: this is just a prototype, the result is more verbose. + + { + "name": "+", + "type": "operator", + "args": [ + 12, + { + "type": "automult", + "args": [ + { + "type": "automult", + "args": [ + { + "name": "^", + "type": "operator", + "args": [ + 3, + 6 + ] + }, + "x" + ] + }, + { + "type": "frac", + "args": [ + 1, + { + "name": "+", + "type": "operator", + "args": [ + 5, + 3 + ] + } + ] + } + ] + } + ] + } +*/ +``` + +You can also pass options to the parse function: + +```js +console.log(parse(tex, options)); +``` + +# Options + +## autoMult: 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. + +## singleCharName: boolean +> Default: `true` + +if you want to use `asdas{d } _ {asdasd123}` as id for variable of function (see options.functions here below), you have to set options.singleCharName to false. + +## functions: string [] +> Default: [] + +Acceptable values: Array of checkers, which are functions or regex or another checker[]. + +this is useful in some cases like, `f(x)`, or `f\left(x\right)`, the function id here is `f`, you can use `blablabla` when singleCharName is `false`. + +## operatorNames: checker[] +> Default: [ + "floor", "ceil", "round", "random", "factorial", + "sech", "csch", "coth", "abs", "arsinh", "arcosh", + "artanh", "arasinh", "aracosh", "aratanh", + ] + +Acceptable values: Array of checkers, which are functions or regex or another checker[] + +Some expressions such as `\operatorname{floor}` are valid, when one of the checkers processes `floor` and return `true`. + +## builtInControlSeq: 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" + ] + +Acceptable values: Array of checkers, which are functions or regex or another checker[] + +## builtInFunctions: checker[] +> Default: [ + "sinh", "cosh", "tanh", + "sin", "cos", "tan", "sec", "csc", "cot", + "arcsin", "arccos", "arctan", "arcsec", "arccsc", "arccot", + "ln" + ] + +Acceptable values: Array of checkers, which are functions or regex or another checker[] + + +# License + +MIT diff --git a/package-lock.json b/package-lock.json index 117eddf..1e2e18d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@scicave/math-latex-parser", - "version": "1.0.1", + "version": "1.1.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 2d11bfe..164b30d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@scicave/math-latex-parser", - "version": "1.0.1", + "version": "1.1.0", "description": "A mathematical parser for latex in math mode. We mean by mathematical that, arithmetic operations is considered for example if you pass \"1+2\", the result would by a (add node \"+\") with two children nodes of type number.", "main": "./lib/index.js", "repository": { diff --git a/src/tex.pegjs b/src/tex.pegjs index c3d2b23..b1e845e 100644 --- a/src/tex.pegjs +++ b/src/tex.pegjs @@ -23,7 +23,7 @@ "Zeta", "Eta", "Theta", "Iota", "Kappa", "Mu", "Nu", "Omicron", "Rho", "Tau", "Chi" ], - builtInFunctions: [ // the same as the rul builtInFuncsTitles + builtInFunctions: [ "sinh", "cosh", "tanh", "sin", "cos", "tan", "sec", "csc", "cot", "arcsin", "arccos", "arctan", "arcsec", "arccsc", "arccot", @@ -327,11 +327,7 @@ factorial = "!" ////// ////// // definitions -builtInFuncsTitles = // the same as builtInFunctions - "sinh" / "cosh" / "tanh" / - "sin" / "cos" / "tan" / "sec" / "csc" / "cot" / - "arcsin" / "arccos" / "arctan" / "arcsec" / "arccsc" / "arccot" / - "ln" +builtInFuncsTitles = "\\" name:[a-z]i+ !char &{ return check(name.join(''), options.builtInFunctions); } /// this may be operator, if so, don't consider as specialSymbol specialSymbolsTitles = a:[a-z]i+ &{ return !check(a.join(''), ignoreSpacialSymbols); }