// Convert TeX to MathML with KaTeX, MathJax or Temml. // // Usage: node tex_to_mathml.mjs // // Input: one JSON object per line, {"i": number, "tex": string, "display": boolean}. // Output: one JSON object per line, {"i", "version", "mathml"} on success, // {"i", "version", "error"} when the tool rejects the formula, and // {"i", "version", "mathml", "error_markup": true} when the tool reports the // error inside the markup (an element or red text) instead of // throwing; Temml is run a second time without throwing for rejected input. import { createRequire } from "node:module"; import { readFileSync, writeFileSync } from "node:fs"; import path from "node:path"; const [tool, modules, input, output] = process.argv.slice(2); const require = createRequire(path.join(path.resolve(modules), "x.js")); const items = readFileSync(input, "utf8").split("\n").filter(Boolean).map(JSON.parse); const out = []; function firstMath(html) { const s = html.search(/]/); const e = html.lastIndexOf(""); return s >= 0 && e > s ? html.slice(s, e + 7) : null; } let convert, version, errorMarkup = null; if (tool === "katex") { const katex = require("katex"); version = "KaTeX " + katex.version; convert = (tex, display) => firstMath(katex.renderToString(tex, { output: "mathml", displayMode: display, throwOnError: true })); } else if (tool === "temml") { const temml = require("temml"); version = "Temml " + temml.version; convert = (tex, display) => { try { return firstMath(temml.renderToString(tex, { displayMode: display, throwOnError: true })); } catch (e) { // Temml's non-throwing mode marks an unknown command in red inside . const html = temml.renderToString(tex, { displayMode: display, throwOnError: false }); const mml = firstMath(html); if (mml) { errorMarkup = String(e.message).slice(0, 200); return mml; } throw e; } }; } else if (tool === "mathjax") { const { mathjax } = require("mathjax-full/js/mathjax.js"); const { TeX } = require("mathjax-full/js/input/tex.js"); const { liteAdaptor } = require("mathjax-full/js/adaptors/liteAdaptor.js"); const { RegisterHTMLHandler } = require("mathjax-full/js/handlers/html.js"); const { AllPackages } = require("mathjax-full/js/input/tex/AllPackages.js"); const { SerializedMmlVisitor } = require("mathjax-full/js/core/MmlTree/SerializedMmlVisitor.js"); version = "MathJax " + require("mathjax-full/package.json").version; const adaptor = liteAdaptor(); RegisterHTMLHandler(adaptor); const doc = mathjax.document("", { InputJax: new TeX({ packages: AllPackages.filter((p) => p !== "bussproofs") }), }); const visitor = new SerializedMmlVisitor(); convert = (tex, display) => visitor.visitTree(doc.convert(tex, { display })); } for (const it of items) { try { errorMarkup = null; const mml = convert(it.tex, it.display); if (!mml) throw new Error("no math element in output"); const r = { i: it.i, version, mathml: mml }; if (errorMarkup || /]|/.test(mml)) r.error_markup = true; out.push(r); } catch (e) { out.push({ i: it.i, version, error: String(e.message || e).slice(0, 200) }); } } writeFileSync(output, out.map((o) => JSON.stringify(o)).join("\n") + "\n");