LaTeX and BibTeX Parser Utilities
raw JSON →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.
Common errors
error ERR_REQUIRE_ESM ↓
import { ... } from 'latex-utensils';) or configure your project for ESM. For Node.js, add "type": "module" to your package.json. error TypeError: latexParser.parse is not a function ↓
import { latexParser } from 'latex-utensils'; and that latexParser is properly destructured from the named export. error SyntaxError: Expected "}" but got "\end" ↓
Warnings
breaking 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. ↓
gotcha 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. ↓
gotcha 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. ↓
gotcha 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}`. ↓
Install
npm install latex-utensils yarn add latex-utensils pnpm add latex-utensils Imports
- latexParser wrong
const latexParser = require('latex-utensils').latexParser;correctimport { latexParser } from 'latex-utensils'; - BibTeXLexer
import { BibTeXLexer } from 'latex-utensils'; - AstRoot wrong
import { AstRoot } from 'latex-utensils';correctimport type { AstRoot } from 'latex-utensils'; - Node
import type { Node } from 'latex-utensils';
Quickstart
import { latexParser } from 'latex-utensils';
import type { AstRoot } from 'latex-utensils';
const texString = `
\documentclass{article}
\usepackage{amsmath}
\begin{document}
ab c
d $x + y$ e
\begin{align}
i + j
\end{align}
\end{document}
`;
try {
const ast: AstRoot = latexParser.parse(texString, { startRule: 'Root' });
console.log('Parsed LaTeX AST:\n', JSON.stringify(ast, undefined, 2));
// Example: Find all commands
const commands = ast.content.filter(node => node.kind === 'command');
console.log('\nFound commands:', commands.map(cmd => (cmd as any).name));
} catch (error) {
console.error('Error parsing LaTeX:', error);
}