Skip to content
1 min read · 179 words

Variable: evaluateKatexTool

ts
const evaluateKatexTool: Tool<SpooledArtifact>;

Defined in: batteries/tools/math/index.ts:452

Translates a LaTeX/KaTeX expression into mathjs syntax, evaluates it, and returns the result.

Remarks

Translates common LaTeX constructs (\frac{a}{b}, \sqrt{...}, \cdot, \times, Greek macros like \pi, inverse trig \arcsin/\arccos/\arctan, \left/\right delimiters, \text{...}, subscripts, etc.) into their mathjs equivalents before evaluation. Both the source and the translated mathjs expression are subject to the 1000-character length cap.

Also evaluates three calculus constructs numerically (mathjs has no symbolic integration, and its symbolic derivative is blocklisted here for safety):

  • Definite integrals — \int_{a}^{b} f \,dx via composite Simpson quadrature.
  • Derivatives at a point — \frac{d}{dx} f \big|_{x=a} via central finite difference.
  • Limits — \lim_{x \to a} f (including a = \pm\infty) via a two-sided numeric approach.

Numeric results are rounded with math.format(..., { precision: 12 }) and labelled Result (numeric): to flag that they are approximations. Constructs that cannot be evaluated numerically (indefinite integrals, derivatives without a point, infinite integration bounds, singular integrands, divergent limits) return a specific, guiding error string.

Parse and evaluation errors are returned as error strings rather than thrown.