npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

latexium

v0.1.1

Published

A powerful TypeScript library for parsing and analyzing LaTeX mathematical expressions

Downloads

188

Readme

Latexium

Note:

  • Features such as factorial simplification (e.g. ! reduction), advanced integration, definite integrals, \lim (limit), and advanced differentiation are not supported yet and are under development.
  • The main features currently implemented and tested are:
    • Distribution (distribute task)
    • Factorization (factor task, using LLL and Berlekamp-Zassenhaus algorithms)
    • For now, LLL and BZ factorization does not work at all.
    • Evaluation (evaluate task)
    • Step-by-step process output (steps field in results, useful for debugging and understanding the calculation process)
  • For details and actual supported cases, see the test cases in tests/master.test.mjs.

Example: Step-by-step Output (Raw)

Below is a real output example of the steps field from analyzeResult.json (fold to view):

{
  "steps": [
    "Original expression: x^{3} + 2x^{2} - 3x",
    "--- overlapSimplify start ---",
    [
      "overlapSimplify pass #1",
      "Starting unified simplification",
      [
        "After middle-simplify",
        [
          "Converted sqrt to exponential form: x^{3} + 2x^{2} - 3x",
          "Applied advanced exponential simplification: x^{3} + 2x^{2} - 3x",
          "Applying basic simplification",
          "x^{3} + 2x^{2} - 3x",
          "Finished basic simplification: x^{3} + 2x^{2} - 3x"
        ]
      ],
      "Applying advanced factorization",
      [
        "Advanced factorization applied",
        [
          "Factorization attempt #1",
          "Starting factorization of: x^{3} + 2x^{2} - 3x",
          "Analyzing 3 terms for common factors",
          "Numeric GCD: 1",
          "Common variable factors: x",
          "Factored form: common factor times remaining expression",
          "rightFactored",
          [
            "Starting factorization of: x^{2} + 2x - 3",
            "PatternRecognitionStrategy: Applied pattern 'quadratic-factorization'.",
            "\u2713 Applied pattern-recognition: (x - 1)(x + 3)",
            "No further factorization possible",
            "Attempting recursive factorization of subexpressions...",
            "leftFactored",
            [
              "Starting factorization of: x - 1",
              "No further factorization possible",
              "Attempting recursive factorization of subexpressions..."
            ],
            "[recursive-factor] factored left: x - 1",
            "rightFactored",
            [
              "Starting factorization of: x + 3",
              "No further factorization possible",
              "Attempting recursive factorization of subexpressions..."
            ],
            "[recursive-factor] factored right: x + 3"
          ],
          "[recursive-factor] factored right: (x - 1)(x + 3)"
        ]
      ],
      [
        "Final pass with expand: false",
        "x(x - 1)(x + 3)"
      ],
      [
        "Unified simplification complete",
        [
          "Converted sqrt to exponential form: x(x - 1)(x + 3)",
          "Applied advanced exponential simplification: x(x - 1)(x + 3)",
          "Applying basic simplification",
          "x(x - 1)(x + 3)",
          "Finished basic simplification: x(x - 1)(x + 3)"
        ]
      ]
    ],
    "No further change detected, stopping.",
    "--- overlapSimplify end ---",
    "Final factored form: x(x - 1)(x + 3)"
  ],
  "value": "x(x - 1)(x + 3)",
  ...other fields...

Features

  • LaTeX parsing: Convert LaTeX math expressions into Abstract Syntax Trees (AST)
  • Mathematical analysis: Distribute, factor, and evaluate expressions (other tasks are under development)
  • Step-by-step output: Each analysis result includes a steps array showing the calculation process
  • Type safety: Full TypeScript support with comprehensive type definitions

Installation

npm install latexium

Quick Start

import { parseLatex, analyze } from 'latexium';

const parseResult = parseLatex("(x+1)(x+2)");
const distributed = analyze(parseResult.ast, { task: "distribute" });
console.log(distributed.value); // "x^2 + 3x + 2"
console.log(distributed.steps);
// [
//   "Expand: (x+1)(x+2)",
//   "Step 1: x*(x+2) = x^2 + 2x",
//   "Step 2: 1*(x+2) = x + 2",
//   "Combine: x^2 + 2x + x + 2 = x^2 + 3x + 2"
// ]

const factored = analyze(parseResult.ast, { task: "factor" });
console.log(factored.value); // "(x+1)(x+2)"
console.log(factored.steps);
// [
//   "Factor: x^2 + 3x + 2",
//   "Find roots: x^2 + 3x + 2 = 0 → x = -1, -2",
//   "Write as (x+1)(x+2)"
// ]

API Reference

parseLatex(expression: string): ParseResult

Parses a LaTeX mathematical expression into an AST.

analyze(ast: ASTNode, options: AnalyzeOptions): AnalyzeResult

Performs mathematical analysis on an AST. Currently supported tasks:

  • distribute: Expand products and powers
  • factor: Factor polynomials (LLL, Berlekamp-Zassenhaus, etc.)
  • evaluate: Calculate numerical value (basic support)

Other tasks (differentiate, integrate, solve, min/max, etc.) are not yet fully supported.

Examples

See tests/master.test.mjs for comprehensive and up-to-date test cases, including many edge cases for distribute, factor, and evaluate.

Distribution

const parseResult = parseLatex("(x+1)(x+2)");
const result = analyze(parseResult.ast, { task: "distribute" });
console.log(result.value); // "x^2 + 3x + 2"
console.log(result.steps);
// [
//   "Expand: (x+1)(x+2)",
//   "Step 1: x*(x+2) = x^2 + 2x",
//   "Step 2: 1*(x+2) = x + 2",
//   "Combine: x^2 + 2x + x + 2 = x^2 + 3x + 2"
// ]

Factorization

// Simple factorization
const parseResult1 = parseLatex("x^2 + 3x + 2");
const result1 = analyze(parseResult1.ast, { task: "factor" });
console.log(result1.value); // "(x+1)(x+2)"
console.log(result1.steps);
// [
//   "Factor: x^2 + 3x + 2",
//   "Find roots: x^2 + 3x + 2 = 0 → x = -1, -2",
//   "Write as (x+1)(x+2)"
// ]

// More difficult factorization
const parseResult2 = parseLatex("x^4 - 16");
const result2 = analyze(parseResult2.ast, { task: "factor" });
console.log(result2.value); // "(x^2 + 4)(x + 2)(x - 2)"
console.log(result2.steps);
// [
//   "Factor: x^4 - 16",
//   "Step 1: x^4 - 16 = (x^2 - 4)(x^2 + 4)",
//   "Step 2: x^2 - 4 = (x - 2)(x + 2)",
//   "Combine: (x^2 + 4)(x + 2)(x - 2)"
// ]

Evaluation

const parseResult = parseLatex("2^3 + 1");
const result = analyze(parseResult.ast, { task: "evaluate" });
console.log(result.value); // "9"
console.log(result.steps);
// [
//   "Evaluate: 2^3 = 8",
//   "Add: 8 + 1 = 9"
// ]

License

MIT

Contributing

Please read SPECIFICATION.md for detailed API specifications and development guidelines.