all2md.parsers.latex

LaTeX to AST converter.

This module provides conversion from LaTeX documents to AST representation using the pylatexenc library for parsing. It enables bidirectional transformation by parsing LaTeX into the same AST structure used for other formats.

class all2md.parsers.latex.LatexParser

Bases: BaseParser

Convert LaTeX to AST representation.

This parser implements a LaTeX parser that converts LaTeX documents into the all2md AST format. It uses pylatexenc library for parsing LaTeX syntax and converts common LaTeX commands to AST nodes.

Supported LaTeX Features

Sectioning Commands:
  • section, subsection, subsubsection

  • paragraph, subparagraph

Text Formatting:
  • textbf{…} - Bold text (Strong)

  • textit{…}, emph{…} - Italic text (Emphasis)

  • texttt{…} - Monospace text (Code)

  • underline{…} - Underlined text

  • textsuperscript{…}, textsubscript{…} - Super/subscript

Math:
  • Inline math: $…$

  • Display math: $$…$$

  • Environments: equation, align, displaymath, eqnarray

Lists:
  • begin{itemize}…end{itemize} - Unordered lists

  • begin{enumerate}…end{enumerate} - Ordered lists

Environments:
  • begin{quote}…end{quote} - Block quotes

  • begin{verbatim}…end{verbatim} - Code blocks

  • begin{tabular}…end{tabular} - Tables (basic support)

  • begin{figure}…end{figure} - Images (with includegraphics)

Line Breaks:
  • \, newline, linebreak

Horizontal Rules:
  • hrule

Metadata (from preamble):
  • title{…}, author{…}, date{…}

Unsupported/Unknown Commands:
  • In non-strict mode: Arguments are extracted when possible

  • In strict mode: Placeholders are inserted showing unsupported commands

param options:

Parser configuration options

type options:

LatexParserOptions or None, default = None

param progress_callback:

Optional callback for progress updates

type progress_callback:

ProgressCallback or None, default = None

Examples

Basic parsing:

>>> parser = LatexParser()
>>> doc = parser.parse("\section{Title}\n\nThis is \textbf{bold}.")

With options:

>>> options = LatexOptions(parse_math=True)
>>> parser = LatexParser(options)
>>> doc = parser.parse(latex_text)

Initialize the LaTeX parser.

__init__(options: LatexOptions | None = None, progress_callback: Callable[[ProgressEvent], None] | None = None)

Initialize the LaTeX parser.

parse(input_data: str | Path | IO[bytes] | bytes) Document

Parse LaTeX input into AST Document.

Parameters:

input_data (str, Path, IO[bytes], or bytes) – LaTeX input to parse. Can be: - File path (str or Path) - File-like object in binary mode - Raw LaTeX bytes - LaTeX string

Returns:

AST document node

Return type:

Document

Raises:

ParsingError – If parsing fails

extract_metadata(document: Any) DocumentMetadata

Extract metadata from LaTeX document.

Parameters:

document (Any) – Document content or AST

Returns:

Extracted metadata

Return type:

DocumentMetadata