{"id":16418,"library":"latex-parser","title":"LaTeX Parser","description":"latex-parser is a JavaScript/TypeScript library designed to build abstract syntax trees (ASTs) for LaTeX documents. As of version 0.6.2, its primary focus is on parsing a subset of 'canonical' LaTeX, specifically prioritizing text mode over comprehensive mathematical typesetting. It distinguishes itself from full TeX parsers by acknowledging the extreme complexity of a complete LaTeX grammar, similar to how KaTeX focuses on math typesetting within a subset. The project evolved from a TypeScript fork of TeXnous and is now modeled after the Haskell LaTeX library HaTeX. While no explicit release cadence is stated, its versioning and active GitHub suggest ongoing iterative development. Developers should be aware of its intentional scope limitations when choosing this parser for their projects.","status":"active","version":"0.6.2","language":"javascript","source_language":"en","source_url":"ssh://git@github.com/digitalheir/latex-parser","tags":["javascript","LaTeX","TeX","parser","typescript","syntax tree","ast"],"install":[{"cmd":"npm install latex-parser","lang":"bash","label":"npm"},{"cmd":"yarn add latex-parser","lang":"bash","label":"yarn"},{"cmd":"pnpm add latex-parser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The library primarily uses ES Module syntax. While a standalone ES5 file is mentioned, direct `require` for `latexParser` might not work as expected in modern Node.js environments without transpilation or specific CJS exports configured.","wrong":"const latexParser = require('latex-parser');","symbol":"latexParser","correct":"import { latexParser } from 'latex-parser';"},{"note":"The `parse` function is a method of the `latexParser` object, not a direct named export from the package's top level. Attempting to destructure it directly will result in an undefined function.","wrong":"import { parse } from 'latex-parser';","symbol":"parse","correct":"import { latexParser } from 'latex-parser';\nconst tokens = latexParser.parse('...');"},{"note":"For TypeScript users, import type definitions using `import type` to avoid bundling type-only declarations into the runtime JavaScript output.","symbol":"LaTeXASTNode","correct":"import type { LaTeXASTNode } from 'latex-parser';"}],"quickstart":{"code":"import { latexParser } from 'latex-parser';\n\n// Parse a simple LaTeX string containing text and a command with an optional and mandatory argument.\nconst latexString = 'This is some text with an \\\\textbf{important} word and an \\\\author[optional]{Full Name}.';\n\ntry {\n  const ast = latexParser.parse(latexString);\n  console.log('Successfully parsed LaTeX document. Root AST nodes:');\n  console.log(JSON.stringify(ast, null, 2));\n  // Accessing specific parts, e.g., the author macro:\n  const authorMacro = ast.find(node => node.type === 'Macro' && node.content === 'author');\n  if (authorMacro) {\n    console.log('\\nFound author macro:', authorMacro);\n    // You might need to cast to access specific properties like 'args' depending on the AST node structure.\n    // (Example simplified for brevity, actual AST navigation might be more complex)\n  }\n} catch (error) {\n  console.error('Error parsing LaTeX:', error.message);\n}\n","lang":"typescript","description":"Demonstrates how to install `latex-parser`, import the `latexParser` object, and use its `parse` method to generate an Abstract Syntax Tree (AST) from a LaTeX string, then logs the resulting AST."},"warnings":[{"fix":"Review the project's documentation and live demo to understand the specific subset of LaTeX syntax it supports. Do not expect it to handle complex TeX macros, custom environments, or advanced mathematical typesetting without potential parsing errors or incomplete ASTs. Consider alternative parsers if full LaTeX/TeX compatibility is required (e.g., those for math expressions or other languages).","message":"This project only parses a *subset* of canonical LaTeX. It explicitly does not aim to be a full TeX parser, which is a significantly more complex task. The focus is primarily on text mode parsing.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Always import `latexParser` as the main object: `import { latexParser } from 'latex-parser';` and then call its method: `latexParser.parse(...)`.","message":"The `parse` function is a method on the `latexParser` object, not a direct named export. Incorrectly attempting `import { parse } from 'latex-parser';` will lead to runtime errors.","severity":"gotcha","affected_versions":">=0.3.0"},{"fix":"When upgrading from versions prior to 0.3.0, carefully review the AST structure and expected parsing behavior as the internal implementation and model significantly changed. Test existing parsing logic thoroughly.","message":"The project's history indicates a shift in modeling, from a TypeScript fork of the TeXnous project to being modeled after the Haskell LaTeX library HaTeX after v0.3.0. This might imply changes in AST structure or parsing logic between versions.","severity":"gotcha","affected_versions":">=0.3.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Ensure you are using the correct ES Module import: `import { latexParser } from 'latex-parser';`. Then call `latexParser.parse(...)`. If in a CommonJS environment, ensure your bundler or runtime correctly resolves the ESM export.","cause":"Attempting to call `parse` on an undefined or incorrectly imported `latexParser` object, often due to incorrect CommonJS `require` syntax in an ESM context, or attempting to destructure `parse` directly from the package.","error":"TypeError: latexParser.parse is not a function"},{"fix":"This is often a direct consequence of the library's intentional limitation to a subset of LaTeX. Simplify the input LaTeX, remove complex or unsupported commands/environments, or use a different parser that targets full TeX or specific complex LaTeX features if your document requires it.","cause":"The input LaTeX string contains commands, environments, or syntax not supported by this parser's subset of LaTeX grammar.","error":"SyntaxError: Expected <token_type> but found \"\\some_unsupported_command\""},{"fix":"Consult the `latex-parser` TypeScript declaration files (`.d.ts`) to verify available type exports. Ensure you are using `import type { CorrectTypeName } from 'latex-parser';` for type-only imports.","cause":"Attempting to import a type that is not explicitly exported or has a different name than assumed, or missing `type` keyword for type-only imports.","error":"TS2305: Module '\"latex-parser\"' has no exported member 'SomeType'."}],"ecosystem":"npm"}