MaTheX2Java

raw JSON →
1.1.6 verified Fri May 01 auth: no javascript

MaTheX2Java is a transpiler that converts amsmath LaTeX mathematical expressions into well-structured, executable Java code. The current stable version (1.1.6) supports the amsmath package v2.1 and provides an annotation feature for customizing code generation. It is built with ANTLR4 and ships TypeScript types. Unlike general LaTeX-to-code tools, MaTheX2Java focuses specifically on Java, producing production-ready code without requiring Java knowledge. Release cadence is irregular but active; the GitHub repository shows recent maintenance. It is for mathematicians, students, and developers who want to run LaTeX formulas as Java programs.

error Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/mathex2java-translator/index.mjs from /path/to/project/index.js not supported.
cause The package is ESM-only and cannot be used with CommonJS require().
fix
Convert your project to ESM (set type: module in package.json) or use dynamic import: const { translate } = await import('mathex2java-translator');
error SyntaxError: Unexpected token 'export'
cause The source code contains ES module syntax but the runtime is Node.js without ESM support.
fix
Ensure Node.js version >= 12 and set type: module in package.json, or transpile with Babel/TypeScript.
error TypeError: translate is not a function
cause Wrong import style: using default import when translate is a named export.
fix
Use import { translate } from 'mathex2java-translator' instead of import maTheX2Java from 'mathex2java-translator'.
breaking Only amsmath v2.1 LaTeX is supported; newer LaTeX constructs may fail or produce incorrect code.
fix Use only amsmath v2.1 compatible LaTeX. Avoid LaTeX3 or non-amsmath packages.
gotcha The package is ESM-only (no CommonJS support). Direct require() will throw 'ERR_REQUIRE_ESM'.
fix Use import syntax or dynamic import(). If using CommonJS, upgrade to ESM or use a wrapper.
gotcha Numeric integration defaults to step size 0.001, which may be too coarse for precision-sensitive applications.
fix Override step size via annotations or post-process the generated code.
npm install mathex2java-translator
yarn add mathex2java-translator
pnpm add mathex2java-translator

Translates a LaTeX integral expression into a Java method using numerical integration.

import { translate } from 'mathex2java-translator';

const latex = '\\[ \\int_{a}^{b} x^2 \\, dx \\]';
const javaCode = translate(latex, { annotations: [] });
console.log(javaCode);
// Output: public static double integrate(double a, double b) { double result = 0; for (double x = a; x <= b; x += 0.001) { result += x * x * 0.001; } return result; }