JavaScript display engine for LaTeX, MathML, and AsciiMath mathematical notation in browsers and Node.js
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
MathJax provides comprehensive initialization and configuration capabilities for both browser and Node.js environments, with extensive customization options and component loading.
Initialize MathJax with custom configuration and component loading.
/**
* Initialize MathJax with configuration
* @param config - Configuration object for MathJax
* @returns Promise that resolves to the configured MathJax instance
*/
function init(config?: Configuration): Promise<MathJax>;Usage Examples:
const MathJax = require('mathjax');
// Basic initialization
const mjx = await MathJax.init();
// Initialize with TeX input and SVG output
const mjx = await MathJax.init({
loader: {
load: ['input/tex', 'output/svg']
}
});
// Initialize with complete configuration
const mjx = await MathJax.init({
loader: {
load: ['input/tex', '[tex]/ams', 'output/chtml'],
paths: {
mathjax: 'path/to/mathjax'
}
},
tex: {
packages: ['base', 'ams', 'color'],
macros: {
R: '{\\mathbb{R}}',
bold: ['{\\bf #1}', 1]
}
},
chtml: {
fontURL: 'https://cdn.jsdelivr.net/npm/mathjax@4/fonts/woff-v2',
adaptiveCSS: true
}
});Load and manage MathJax components dynamically.
/**
* Load specified components
* @param components - List of component names to load
* @returns Promise that resolves when all components are loaded
*/
function load(...components: string[]): Promise<any[]>;
/**
* Wait for components to be ready
* @param components - List of component names to wait for
* @returns Promise that resolves when components are ready
*/
function ready(...components: string[]): Promise<any[]>;
/**
* Mark components as pre-loaded
* @param components - List of component names to mark as loaded
*/
function preLoaded(...components: string[]): void;
/**
* Check component version compatibility
* @param component - Component name
* @param version - Required version
* @param name - Component display name
* @returns Whether version is compatible
*/
function checkVersion(component: string, version: string, name: string): boolean;
/**
* Get root path for component loading
* @returns Root path string
*/
function getRoot(): string;Usage Examples:
// Load additional components
await MathJax.loader.load('input/asciimath', '[tex]/color', 'a11y/semantic-enrich');
// Wait for specific components
await MathJax.loader.ready('input/tex', 'output/svg');
// Mark components as pre-loaded
MathJax.loader.preLoaded('startup', 'core');
// Check version compatibility
const isCompatible = MathJax.loader.checkVersion('input/tex', '4.0.0', 'TeX Input');
// Get component root path
const rootPath = MathJax.loader.getRoot();Access to MathJax's core global objects and namespaces.
/**
* MathJax version string
*/
const version: string;
/**
* Internal utilities and components namespace
*/
const _: {
core: CoreUtilities;
util: UtilityFunctions;
adaptors: AdaptorClasses;
components: ComponentClasses;
};
/**
* Core MathJax engine object
*/
const mathjax: {
version: string;
handlers: HandlerList;
document: (doc: any, options: any) => MathDocument;
};Usage Examples:
// Access version information
console.log('MathJax version:', MathJax.version);
// Access internal utilities
const utilities = MathJax._.util;
const coreModules = MathJax._.core;
// Access core engine
const engine = MathJax.mathjax;
const document = engine.document(htmlDoc, options);Control MathJax startup and component initialization.
/**
* Register a constructor for a component
* @param name - Component name
* @param constructor - Constructor function
*/
function registerConstructor(name: string, constructor: any): void;
/**
* Set the document handler
* @param name - Handler name
* @param force - Whether to force replacement
*/
function useHandler(name: string, force?: boolean): void;
/**
* Set the DOM adaptor
* @param name - Adaptor name
* @param force - Whether to force replacement
*/
function useAdaptor(name: string, force?: boolean): void;
/**
* Add an input processor
* @param name - Input processor name
* @param force - Whether to force replacement
*/
function useInput(name: string, force?: boolean): void;
/**
* Set the output processor
* @param name - Output processor name
* @param force - Whether to force replacement
*/
function useOutput(name: string, force?: boolean): void;
/**
* Extend the document handler with additional functionality
* @param extension - Extension function
* @param priority - Priority level for execution order
*/
function extendHandler(extension: Function, priority?: number): void;Usage Examples:
// Register custom constructor
MathJax.startup.registerConstructor('customInput', MyCustomInputJax);
// Set components
MathJax.startup.useHandler('HTMLHandler');
MathJax.startup.useAdaptor('liteAdaptor');
MathJax.startup.useInput('tex');
MathJax.startup.useOutput('svg');interface LoaderOptions {
/** Components to load automatically */
load?: string[];
/** Component dependencies */
dependencies?: Record<string, string[]>;
/** Path mappings for component loading */
paths?: Record<string, string>;
/** Source file mappings */
source?: Record<string, string>;
/** Components provided by other components */
provides?: Record<string, string[]>;
/** Ready callback */
ready?: () => void;
/** Failure callback */
failed?: (error: Error) => void;
/** Custom require function */
require?: (file: string) => any;
/** Path filter functions */
pathFilters?: Array<Function | [Function, number]>;
/** Show version warnings */
versionWarnings?: boolean;
}interface StartupOptions {
/** Input format names */
input?: string[];
/** Output format name */
output?: string;
/** Document handler name */
handler?: string | null;
/** DOM adaptor name */
adaptor?: string | null;
/** Document or document selector */
document?: string | Document;
/** Elements to process */
elements?: Element[] | null;
/** Auto-typeset on ready */
typeset?: boolean;
/** Ready callback */
ready?: () => void;
/** Page ready callback */
pageReady?: () => void;
}interface GlobalOptions {
/** Enable assistive MathML */
enableAssistiveMml?: boolean;
/** Enable interactive explorer */
enableExplorer?: boolean;
/** Compile error handler */
compileError?: (doc: any, math: any, error: Error) => void;
/** Typeset error handler */
typesetError?: (doc: any, math: any, error: Error) => void;
/** Accessibility options */
a11y?: AccessibilityOptions;
}Input Components:
input/tex - TeX/LaTeX input processorinput/tex-base - Base TeX functionalityinput/mml - MathML input processorinput/asciimath - AsciiMath input processorOutput Components:
output/chtml - CommonHTML output processoroutput/svg - SVG output processorTeX Extensions (30+ available):
[tex]/ams - AMS math extensions[tex]/color - Color support[tex]/mhchem - Chemistry notation[tex]/physics - Physics notation[tex]/cancel - Cancel expressions[tex]/bbox - Bounding boxesAccessibility Components:
a11y/semantic-enrich - Semantic enhancementa11y/speech - Speech generationa11y/explorer - Interactive explorationa11y/assistive-mml - Assistive MathMLUI Components:
ui/menu - Context menuui/lazy - Lazy loadingui/safe - Safe modePre-built component combinations for common use cases:
tex-chtml - TeX input + CommonHTML outputtex-svg - TeX input + SVG outputmml-chtml - MathML input + CommonHTML outputmml-svg - MathML input + SVG outputtex-mml-chtml - TeX + MathML inputs + CommonHTML outputtex-mml-svg - TeX + MathML inputs + SVG outputEach available with -nofont variants.
For browser environments, configuration is typically set globally:
window.MathJax = {
tex: {
inlineMath: [['$', '$'], ['\\(', '\\)']],
displayMath: [['$$', '$$'], ['\\[', '\\]']]
},
chtml: {
fontURL: 'https://cdn.jsdelivr.net/npm/mathjax@4/fonts/woff-v2'
}
};For Node.js environments, configuration is passed to init():
const config = {
loader: {
require: require,
paths: {
mathjax: 'node_modules/mathjax'
}
},
startup: {
typeset: false // Don't auto-typeset in Node.js
}
};
const MathJax = await require('mathjax').init(config);MathJax.config.loader.failed = (error) => {
console.error('Component failed to load:', error.message);
// Handle component loading failure
};try {
const MathJax = await require('mathjax').init(config);
} catch (error) {
console.error('MathJax initialization failed:', error.message);
// Handle initialization failure
}MathJax.config.loader.pathFilters.push([
(data) => {
// Custom path transformation
if (data.name.startsWith('custom/')) {
data.name = 'https://my-cdn.com/' + data.name;
}
return true;
},
15 // Priority
]);// Register custom input processor
MathJax.startup.registerConstructor('myInput', MyInputProcessor);
// Use custom component
MathJax.startup.useInput('myInput');