Fast math typesetting for the web.
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Comprehensive options system and error handling for controlling output format, LaTeX compatibility, security, and rendering behavior.
Main rendering options that control output format and display behavior.
interface KatexOptions {
/** Render in display mode (centered, larger operators) vs inline mode */
displayMode?: boolean; // default: false
/** Output format for rendered math */
output?: "html" | "mathml" | "htmlAndMathml"; // default: "htmlAndMathml"
/** Render equation tags on the left (like \usepackage[leqno]{amsmath}) */
leqno?: boolean; // default: false
/** Render display math flush left with 2em margin */
fleqn?: boolean; // default: false
}Usage Examples:
import katex from "katex";
// Display mode for block equations
katex.render("\\sum_{i=1}^n i^2", element, {
displayMode: true
});
// HTML-only output (no MathML for accessibility)
const html = katex.renderToString("E = mc^2", {
output: "html"
});
// MathML-only output
const mathml = katex.renderToString("\\frac{1}{2}", {
output: "mathml"
});
// Left-aligned display math
katex.render("\\int_0^\\infty e^{-x^2} dx", element, {
displayMode: true,
fleqn: true
});Options for controlling error behavior and appearance.
interface ErrorOptions {
/** Whether to throw ParseError exceptions or render error text */
throwOnError?: boolean; // default: true
/** Color for error text when throwOnError is false */
errorColor?: string; // default: "#cc0000"
}Usage Examples:
import katex from "katex";
// Suppress errors and show red error text
katex.render("\\invalid", element, {
throwOnError: false,
errorColor: "#ff6b6b"
});
// Custom error handling
try {
katex.render("\\badcommand", element, {
throwOnError: true
});
} catch (error) {
if (error instanceof katex.ParseError) {
element.innerHTML = `<span style="color: red;">Math Error: ${error.rawMessage}</span>`;
}
}Custom macro definitions for extending TeX functionality.
interface MacroOptions {
/** Custom macro definitions */
macros?: Record<string, string | object | MacroFunction>;
}
type MacroFunction = (macroExpander: object) => string | object;Usage Examples:
import katex from "katex";
// String macros
katex.render("\\RR", element, {
macros: {
"\\RR": "\\mathbb{R}",
"\\NN": "\\mathbb{N}",
"\\d": "\\mathrm{d}"
}
});
// Parameterized macros
katex.render("\\diff{f}{x}", element, {
macros: {
"\\diff": "\\frac{\\mathrm{d}#1}{\\mathrm{d}#2}"
}
});
// Function macros (advanced)
const options = {
macros: {
"\\vec": (context) => {
return "\\overrightarrow{" + context.consumeArgs(1)[0] + "}";
}
}
};Options for LaTeX faithfulness and input validation.
interface CompatibilityOptions {
/** LaTeX compatibility mode */
strict?: boolean | "ignore" | "warn" | "error" | StrictFunction; // default: "warn"
/** Trust input for HTML features like \url and \href */
trust?: boolean | TrustFunction; // default: false
/** Whether \color behaves like LaTeX's \textcolor */
colorIsTextColor?: boolean; // default: false
}
type StrictFunction = (
errorCode: "unknownSymbol" | "unicodeTextInMathMode" | "mathVsTextUnits" |
"commentAtEnd" | "htmlExtension" | "newLineInDisplayMode",
errorMsg: string,
token: Token
) => boolean | "error" | "warn" | "ignore" | undefined;
type TrustFunction = (context: TrustContext) => boolean;
type TrustContext =
| { command: "\\url", url: string, protocol?: string }
| { command: "\\href", url: string, protocol?: string }
| { command: "\\includegraphics", url: string, protocol?: string }
| { command: "\\htmlClass", class: string }
| { command: "\\htmlId", id: string }
| { command: "\\htmlStyle", style: string }
| { command: "\\htmlData", attributes: Record<string, string> };Usage Examples:
import katex from "katex";
// Strict LaTeX mode (throws on non-LaTeX features)
katex.render("$x$", element, {
strict: "error" // Will throw on $ delimiters (non-LaTeX)
});
// Custom strict handling
katex.render("\\unicode{0x1D400}", element, {
strict: (errorCode, errorMsg, token) => {
if (errorCode === "unknownSymbol") {
console.warn("Unknown symbol:", errorMsg);
return "ignore";
}
return "warn";
}
});
// Trust specific URLs
katex.render("\\href{https://example.com}{link}", element, {
trust: (context) => {
if (context.command === "\\href") {
return context.protocol === "https:";
}
return false;
}
});
// Trust all input (use with caution)
katex.render("\\url{https://example.com}", element, {
trust: true
});Options for controlling rendering performance and resource usage.
interface PerformanceOptions {
/** Maximum element size in ems (default: Infinity) */
maxSize?: number;
/** Maximum macro expansions to prevent infinite loops */
maxExpand?: number; // default: 1000
/** Minimum thickness for rules and lines in ems */
minRuleThickness?: number;
/** Run in global group (macros persist across renders) */
globalGroup?: boolean; // default: false
}Usage Examples:
import katex from "katex";
// Limit element sizes
katex.render("\\rule{1000em}{1000em}", element, {
maxSize: 10 // Caps at 10em
});
// Limit macro expansions
katex.render("\\def\\a{\\a}\\a", element, {
maxExpand: 100 // Prevents infinite recursion
});
// Set minimum line thickness
katex.render("\\frac{1}{2}", element, {
minRuleThickness: 0.05 // Ensures visible fraction line
});
// Global macro definitions
katex.render("\\gdef\\R{\\mathbb{R}} \\R", element, {
globalGroup: true,
macros: {} // Macros persist and can be reused
});KaTeX exposes its complete settings schema for introspection:
const SETTINGS_SCHEMA: Record<string, OptionSchema>;
interface OptionSchema {
type: string | string[] | { enum: string[] };
default?: any;
description?: string;
processor?: (value: any) => any;
cli?: string | false;
cliDescription?: string;
cliProcessor?: (value: any, previous?: any) => any;
}Usage Example:
import katex from "katex";
// Inspect available options
console.log("Available KaTeX options:", Object.keys(katex.SETTINGS_SCHEMA));
// Get option details
const displayModeSchema = katex.SETTINGS_SCHEMA.displayMode;
console.log("Display mode type:", displayModeSchema.type);
console.log("Display mode default:", displayModeSchema.default);