Registry / serialization / latex-parser

latex-parser

JSON →
library0.1.0jsnpmunverified

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.

npm install latex-parser
INSTALL
IMPORT
SIG · LATEX-PARSER
L
latex-parser
serializationjavascriptv0.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

latexParser
import { latexParser } from 'latex-parser';
const latexParser = require('latex-parser');
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.
parse
import { latexParser } from 'latex-parser'; const tokens = latexParser.parse('...');
import { parse } from 'latex-parser';
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.
LaTeXASTNode
import type { LaTeXASTNode } from 'latex-parser';
For TypeScript users, import type definitions using `import type` to avoid bundling type-only declarations into the runtime JavaScript output.

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); }
Debug
Known issues
gotchaThis 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.
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).
affects: >=0.1.0
gotchaThe `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.
fix
Always import `latexParser` as the main object: `import { latexParser } from 'latex-parser';` and then call its method: `latexParser.parse(...)`.
affects: >=0.3.0
gotchaThe 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.
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.
affects: >=0.3.0
Errors
Common errors & fixes
TypeError: latexParser.parse is not a function
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.
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.
SyntaxError: Expected <token_type> but found "\some_unsupported_command"
The input LaTeX string contains commands, environments, or syntax not supported by this parser's subset of LaTeX grammar.
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.
TS2305: Module '"latex-parser"' has no exported member 'SomeType'.
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.
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.
Upgrade
Version history
0.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
8 hits · last 30 days
node
8
Resources
latex-parser — npm install latex-parser · libregistry