LaTeX Parser
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.
Common errors
-
TypeError: latexParser.parse is not a function
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.fixEnsure 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. -
SyntaxError: Expected <token_type> but found "\some_unsupported_command"
cause The input LaTeX string contains commands, environments, or syntax not supported by this parser's subset of LaTeX grammar.fixThis 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. -
TS2305: Module '"latex-parser"' has no exported member 'SomeType'.
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.fixConsult 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.
Warnings
- gotcha 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.
- gotcha 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.
- gotcha 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.
Install
-
npm install latex-parser -
yarn add latex-parser -
pnpm add latex-parser
Imports
- latexParser
const latexParser = require('latex-parser');import { latexParser } from 'latex-parser'; - parse
import { parse } from 'latex-parser';import { latexParser } from 'latex-parser'; const tokens = latexParser.parse('...'); - LaTeXASTNode
import type { LaTeXASTNode } from 'latex-parser';
Quickstart
import { latexParser } from 'latex-parser';
// Parse a simple LaTeX string containing text and a command with an optional and mandatory argument.
const latexString = 'This is some text with an \\textbf{important} word and an \\author[optional]{Full Name}.';
try {
const ast = latexParser.parse(latexString);
console.log('Successfully parsed LaTeX document. Root AST nodes:');
console.log(JSON.stringify(ast, null, 2));
// Accessing specific parts, e.g., the author macro:
const authorMacro = ast.find(node => node.type === 'Macro' && node.content === 'author');
if (authorMacro) {
console.log('\nFound author macro:', authorMacro);
// You might need to cast to access specific properties like 'args' depending on the AST node structure.
// (Example simplified for brevity, actual AST navigation might be more complex)
}
} catch (error) {
console.error('Error parsing LaTeX:', error.message);
}