Skip to content

@texmorph/katex ​

KaTeX renderer and the createMorph entry point. Re-exports renderFormula, MorphPrepareError, TexMorphLifecycleError and the types FormulaInput, FormulaRenderer, GhostLayer, LatexState, MathMorph, MathMorphOptions and RenderedFormula from @texmorph/dom.

createMorph ​

ts
function createMorph(
  host: HTMLElement,
  from: FormulaInput,
  to: FormulaInput,
  options?: MathMorphOptions<SafeKatexOptions> & { katex?: KatexLike },
): Promise<MathMorph>;

Renders and measures both formulas inside host and resolves with a prepared MathMorph.

ParameterDescription
hostContainer element. The morph renders inside it.
from, toA FormulaInput: { latex, displayMode? }, a RenderedFormula, or { adopt, fallback? }.
options.katexThe KaTeX module. If omitted, it is loaded with import('katex').
optionsAll MathMorphOptions; rendererOptions takes SafeKatexOptions.

Rejects with MorphPrepareError in strict mode or when an input cannot be adopted, and with signal.reason when aborted.

katexRenderer ​

ts
function katexRenderer(katex: KatexLike, defaults?: SafeKatexOptions): FormulaRenderer<SafeKatexOptions>;

Creates a reusable renderer for createMorphWith, renderFormula and the integrations. renderer.version is katex.version. Rendering uses output: 'htmlAndMathml' and throws on parse errors.

SafeKatexOptions ​

The subset of KaTeX options texmorph accepts:

ts
interface SafeKatexOptions {
  trust?: (ctx: KatexTrustContext) => boolean;
  macros?: Record<string, string>;
  strict?: boolean | 'ignore' | 'warn' | 'error';
  maxSize?: number;
  maxExpand?: number;
  minRuleThickness?: number;
  colorIsTextColor?: boolean;
  fleqn?: boolean;
  leqno?: boolean;
  errorColor?: string;
  globalGroup?: boolean;
}

interface KatexTrustContext {
  command: string;
  url?: string;
  protocol?: string;
}

trust must be a function; trust: true throws a TypeError. displayMode is set per formula through LatexState, and output is fixed.

KatexLike ​

ts
interface KatexLike {
  readonly version: string;
  render(latex: string, element: HTMLElement, options?: Record<string, unknown>): void;
}

The minimal shape of the KaTeX module texmorph needs. The default export of the katex package satisfies it.

Released under the MIT License.