{"id":17775,"library":"latex-utensils","title":"LaTeX and BibTeX Parser Utilities","description":"latex-utensils is a TypeScript library providing robust parsing capabilities for LaTeX and BibTeX documents, along with various utility functions. Its current stable version is 7.0.0. The project demonstrates an active release cadence, with frequent beta releases preceding major versions, signaling continuous development and refinement. It uniquely offers both LaTeX and BibTeX parsers within a unified package, building upon established parser foundations like LaTeX.js and latex-parser to deliver comprehensive functionality. This library is particularly well-suited for applications that require programmatic interaction with LaTeX source code, such as creating linters, IDE extensions, or advanced document analysis tools. It achieves this by transforming raw LaTeX strings into a structured Abstract Syntax Tree (AST) that is easily traversable and manipulable. A key differentiator is its inclusion of full TypeScript type definitions, significantly improving developer experience and ensuring type safety when integrating parsing logic into modern JavaScript and TypeScript environments.","status":"active","version":"7.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/tamuratak/latex-utensils","tags":["javascript","latex","bibtex","parser","typescript"],"install":[{"cmd":"npm install latex-utensils","lang":"bash","label":"npm"},{"cmd":"yarn add latex-utensils","lang":"bash","label":"yarn"},{"cmd":"pnpm add latex-utensils","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The primary entry point for parsing LaTeX and BibTeX. Use named import for ESM. CommonJS `require` might lead to issues since v7.0.0 seems to be ESM-first.","wrong":"const latexParser = require('latex-utensils').latexParser;","symbol":"latexParser","correct":"import { latexParser } from 'latex-utensils';"},{"note":"Used for lexing BibTeX documents. Often needed for more granular control over the parsing process beyond the simple `parse` function.","symbol":"BibTeXLexer","correct":"import { BibTeXLexer } from 'latex-utensils';"},{"note":"Type definition for the root of the LaTeX AST. Always use `import type` for type-only imports to prevent bundling issues and ensure correct tooling behavior.","wrong":"import { AstRoot } from 'latex-utensils';","symbol":"AstRoot","correct":"import type { AstRoot } from 'latex-utensils';"},{"note":"Base type for all nodes within the parsed LaTeX AST. Essential for type-checking when traversing the AST.","symbol":"Node","correct":"import type { Node } from 'latex-utensils';"}],"quickstart":{"code":"import { latexParser } from 'latex-utensils';\nimport type { AstRoot } from 'latex-utensils';\n\nconst texString = `\n\\documentclass{article}\n\\usepackage{amsmath}\n\\begin{document}\nab c\nd $x + y$ e\n\\begin{align}\n    i + j\n\\end{align}\n\\end{document}\n`;\n\ntry {\n  const ast: AstRoot = latexParser.parse(texString, { startRule: 'Root' });\n  console.log('Parsed LaTeX AST:\\n', JSON.stringify(ast, undefined, 2));\n  // Example: Find all commands\n  const commands = ast.content.filter(node => node.kind === 'command');\n  console.log('\\nFound commands:', commands.map(cmd => (cmd as any).name));\n} catch (error) {\n  console.error('Error parsing LaTeX:', error);\n}","lang":"typescript","description":"Demonstrates parsing a LaTeX string into an Abstract Syntax Tree (AST) and basic AST traversal to find commands."},"warnings":[{"fix":"Consult the official API documentation for any changes to AST node types or parser configuration. Update existing parsing logic to match the new structure if necessary.","message":"Major version 7.0.0 might introduce breaking changes to the internal AST structure or parser options, even with minimal release notes. Always review the detailed changelog or test thoroughly when upgrading from v6.x.","severity":"breaking","affected_versions":">=7.0.0"},{"fix":"Ensure your project is configured for ESM (e.g., `\"type\": \"module\"` in `package.json` for Node.js) and use `import ... from 'latex-utensils';`. For older CommonJS projects, consider using dynamic `import()` or a transpilation step.","message":"The library primarily uses ES Module (ESM) syntax for imports. Attempting to use CommonJS `require()` directly might lead to 'ERR_REQUIRE_ESM' errors or incorrect import behavior in Node.js environments.","severity":"gotcha","affected_versions":">=6.0.0"},{"fix":"For performance-critical applications, consider optimizing input size, pre-processing documents, or implementing caching mechanisms for ASTs. Profile parsing operations to identify bottlenecks.","message":"Parsing very large or complex LaTeX documents can be resource-intensive and slow. The parser is designed for accuracy, which can come at the cost of speed for extreme inputs.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure that the input string provided to `latexParser.parse` is correctly encoded, preferably UTF-8. If reading from a file, explicitly specify UTF-8 encoding during file read operations.","message":"Incorrect input encoding for LaTeX files can lead to parsing errors or malformed ASTs, especially with non-ASCII characters. LaTeX documents often specify encoding with `\\usepackage[utf8]{inputenc}`.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"Switch to ES Module imports (`import { ... } from 'latex-utensils';`) or configure your project for ESM. For Node.js, add `\"type\": \"module\"` to your `package.json`.","cause":"Attempting to `require()` an ES Module-only package in a CommonJS context.","error":"ERR_REQUIRE_ESM"},{"fix":"Ensure you are using `import { latexParser } from 'latex-utensils';` and that `latexParser` is properly destructured from the named export.","cause":"Incorrectly importing `latexParser` or trying to call `parse` on an undefined or incorrect object.","error":"TypeError: latexParser.parse is not a function"},{"fix":"Review the LaTeX input string for syntax errors. The error message usually provides line and column numbers to help pinpoint the issue. Ensure all environments are properly closed and commands have correct arguments.","cause":"A common parsing error indicating malformed LaTeX syntax in the input string, such as missing braces or mismatched environments.","error":"SyntaxError: Expected \"}\" but got \"\\end\""}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}