Extracts LaTeX from math elements, converting MathML to LaTeX and LaTeX to MathML via temml. Reuse when full math rendering support is needed.
// Source: https://github.com/kepano/defuddle | License: MIT | Copyright 2025 Steph Ango (@kepano)
// Provided via justclaw.it.com; keep this notice with the code.
import { MathMLToLaTeX } from 'mathml-to-latex';
import {
MathData,
LatexToMathML,
getMathMLFromElement,
getBasicLatexFromElement,
createMathElement,
createMathRules
} from './math.base';
import { parseHTML } from '../utils/dom';
export const getLatexFromElement = (el: Element): string | null => {
// First try basic LaTeX extraction
const basicLatex = getBasicLatexFromElement(el);
if (basicLatex) {
return basicLatex;
}
// If no LaTeX found but we have MathML, convert it
const mathData = getMathMLFromElement(el);
if (mathData?.mathml) {
try {
return MathMLToLaTeX.convert(mathData.mathml);
} catch (e) {
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key