LaTeX Parser

0.6.2 · active · verified Wed Apr 22

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

Warnings

Install

Imports

Quickstart

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.

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);
}

view raw JSON →