{"id":15501536,"url":"https://github.com/davidtranhq/tex-math-parser","last_synced_at":"2025-08-18T20:33:00.169Z","repository":{"id":57154339,"uuid":"376183269","full_name":"davidtranhq/tex-math-parser","owner":"davidtranhq","description":"A parser that can evaluate TeX math and convert it into a MathJS expression tree.","archived":false,"fork":false,"pushed_at":"2024-06-17T17:53:10.000Z","size":270,"stargazers_count":31,"open_issues_count":6,"forks_count":12,"subscribers_count":5,"default_branch":"main","last_synced_at":"2024-12-09T09:54:41.374Z","etag":null,"topics":["javascript","latex","math","parser"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/tex-math-parser","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/davidtranhq.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-06-12T02:13:44.000Z","updated_at":"2024-09-03T16:03:24.000Z","dependencies_parsed_at":"2024-10-31T14:03:36.084Z","dependency_job_id":"8306c868-f153-4b14-8fbf-7479a698789b","html_url":"https://github.com/davidtranhq/tex-math-parser","commit_stats":{"total_commits":24,"total_committers":1,"mean_commits":24.0,"dds":0.0,"last_synced_commit":"861c2f541ef725a3391b18ada5b4bb197a64b7d4"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidtranhq%2Ftex-math-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidtranhq%2Ftex-math-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidtranhq%2Ftex-math-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidtranhq%2Ftex-math-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/davidtranhq","download_url":"https://codeload.github.com/davidtranhq/tex-math-parser/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230276329,"owners_count":18201088,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","latex","math","parser"],"created_at":"2024-10-02T09:04:44.165Z","updated_at":"2025-08-18T20:33:00.119Z","avatar_url":"https://github.com/davidtranhq.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TeX Math Parser\n\nTeX Math Parser parses TeX math into [a MathJS expression tree](https://mathjs.org/docs/expressions/expression_trees.html) which can then be further manipulated and evaluated by [MathJS](https://mathjs.org/). The library also provides convenience functions for directly evaluating TeX math with MathJS.\n\nThis library works well as a bridge between [MathQuill](http://mathquill.com/) and MathJS. Use this library to parse the TeX formatted output from MathQuill into a format that can be manipulated by MathJS.\n\n## TeX Features\n\n* Common operators available in TeX math mode: `+`, `-`, `*`, `^`, `/`, `\\cdot`, `||` (absolute value), `\\times` (cross product)\n* Basic functions: `\\sqrt`, `\\frac`, `\\sin`, `\\cos`, `\\tan`, `\\csc`, `\\sec`, `\\cot`, `\\arcsin`, `\\arccos`, `\\arctan`, `\\log`, `\\ln`, `\\det`\n* Custom functions implemented with MathJS: `eigenvectors`, `eigenvalues`, `cross`, `proj`, `comp`, `norm`, `inv`\n    * Since these are custom functions, they should be formatted as `\\operatorname{function}` in TeX.\n* Constants: `\\pi`, `e`\n* Environments: `matrix`\n* Variables\n    * `^T` is interpreted as the transpose operation\n\n## Browser Support\n\nAny browser with ES6 support.\n\n## Installation\n\nInstall with NPM:\n\n```\nnpm install tex-math-parser \n```\n\nor link to it from a CDN:\n\n```html\n\u003cscript src=https://cdn.jsdelivr.net/npm/tex-math-parser\u003e\u003c/script\u003e\n```\n\n## Usage\n\nGiven the following TeX source string:\n\n![Example TeX](docs/imgs/example_tex.png)\n```latex\n\\begin{bmatrix}1\u00263\\\\2\u00264\\end{bmatrix}\\begin{bmatrix}-5\\\\-6\\end{bmatrix}+\\left|\\sqrt{7}-\\sqrt{8}\\right|^{\\frac{9}{10}}\\begin{bmatrix}\\cos\\left(\\frac{\\pi}{6}\\right)\\\\\\sin\\left(\\frac{\\pi}{6}\\right)\\end{bmatrix}\n```\n\nLoad the package and escape the string:\n\n```javascript\nimport { parseTex, evaluateTex } from 'tex-math-parser' // ES6 module\n\n// Make sure to escape the string!\nconst escapedTex = String.raw`\\begin{bmatrix}1\u00263\\\\2\u00264\\end{bmatrix}\\begin{bmatrix}-5\\\\-6\\end{bmatrix}+\\left|\\sqrt{7}-\\sqrt{8}\\right|^{\\frac{9}{10}}\\begin{bmatrix}\\cos\\left(\\frac{\\pi}{6}\\right)\\\\\\sin\\left(\\frac{\\pi}{6}\\right)\\end{bmatrix}`; // ES6 raw template string\n```\n\nEvaluate the string and get an answer in TeX:\n\n```javascript\nconst texAnswer = evaluateTex(escapedTex); \nconsole.log(texAnswer); \n// \\begin{bmatrix}-22.812481734548864\\\\-33.89173627896382\\\\\\end{bmatrix}\n```\n\nParse the string and get a [a MathJS expression tree](https://mathjs.org/docs/expressions/expression_trees.html):\n```javascript\nconst mathJSTree = parseTex(escapedTex);\n```\n\n### Variables\nIf the TeX string contains variables, the value of the variables must be supplied when evaluating.\n\n![Example TeX with variables](docs/imgs/example_tex_variables.png)\n\n```javascript\nconst texStr = String.raw`\\frac{x}{4}+\\frac{y}{2}`;\nconst answer = evaluateTex(texStr, {x: 2, y: 1});\nconsole.log(answer); // 1\n```\n\n## API\n\n`evaluateTex(texStr: string, scope?: Object)`\n\nEvaluate a TeX string, replacing any variable occurences with their values in `scope`. The answer is returned as a TeX string.\n\n`parseTex(texStr: string)`\n\nConvert a TeX string into [a MathJS expression tree](https://mathjs.org/docs/expressions/expression_trees.html). The function returns the root node of the tree.\n\n## Contributing\n\nPlease feel free to make a PR and add any features, add unit tests, or refactor any of the code. Both  `tokenizeTex` and the `Parser` are quite messy and could really use a clean-up (maybe someday I'll get around to it...).\n\nRun `pnpm test` to run some unit tests and make sure they're passing!\n\nAdding support for new TeX functions is relatively simple (see [this commit](https://github.com/davidtranhq/tex-math-parser/commit/037f27650b8b44ac45497f4e49a77c2195282a05) for an example)\n\nTODO: include better documentation on how to do this\n\n## Details\n\n`parseTex` first lexes the TeX string into tokens, which are then passed to the parser to create the expression tree. A context-free grammar for the simplified version of TeX math used by the parser is as follows:\n\n```\nexpr = term ((PLUS | MINUS) term)*\n\nterm = factor ((CDOT factor | primary )* // primary and factor must both not be NUMBERs\n\nfactor = MINUS? power\n\npower = primary (CARET primary)*\n\nprimary = grouping\n        | environnment\n        | frac\n        | function\n        | NUMBER\n        | VARIABLE\n\ngrouping = LEFT LPAREN expr RIGHT RPAREN\n         | LPAREN expr RPAREN\n         | LBRACE expr RBRACE\n         | LEFT BAR expr RIGHT BAR\n         | BAR expr BAR\n\nenvironnment = matrix\n\nfrac = FRAC LBRACE expr RBRACE LBRACE expr RBRACE\n\nfunction = (SQRT | SIN | COS | TAN ...) grouping\n\nmatrix = BEGIN LBRACE MATRIX RBRACE ((expr)(AMP | DBLBACKSLASH))* END LBRACE MATRIX RBRACE\n```\n\nAs the grammar is not left-recursive, the parser was implemented as a recursive descent parser with each production being represented by a separate function. This keeps the parser easily extensible.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidtranhq%2Ftex-math-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdavidtranhq%2Ftex-math-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidtranhq%2Ftex-math-parser/lists"}