JavaScript display engine for LaTeX, MathML, and AsciiMath mathematical notation in browsers and Node.js
npx @tessl/cli install tessl/npm-mathjax@4.0.0MathJax is a comprehensive JavaScript display engine for mathematical notation that renders LaTeX, MathML, and AsciiMath expressions in web browsers and Node.js applications. It provides high-quality mathematical typesetting with support for multiple input formats and output formats, accessibility features for screen readers, and a powerful API for integration.
npm install mathjaxFor Node.js applications:
const MathJax = require('mathjax');For ES modules:
import MathJax from 'mathjax';For browser applications, include a component script:
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@4/tex-chtml.js"></script>const MathJax = require('mathjax');
// Initialize MathJax with configuration
MathJax.init({
loader: {
load: ['input/tex', 'output/svg']
}
}).then((MathJax) => {
// Convert TeX to SVG
const svg = MathJax.tex2svg('x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}', {
display: true
});
console.log(MathJax.startup.adaptor.outerHTML(svg));
}).catch((err) => console.log(err.message));<!DOCTYPE html>
<html>
<head>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@4/tex-chtml.js"></script>
</head>
<body>
<div>
When \(a \ne 0\), there are two solutions to \(ax^2 + bx + c = 0\) and they are
\[x = {-b \pm \sqrt{b^2-4ac} \over 2a}.\]
</div>
</body>
</html>MathJax is built around several key components:
Core MathJax initialization, component loading, and configuration management for both browser and Node.js environments.
function init(config?: Configuration): Promise<MathJax>;Initialization and Configuration
Support for multiple mathematical notation formats with extensive customization options and extension support.
// Convert TeX/LaTeX to various output formats
function tex2svg(tex: string, options?: ConversionOptions): Element;
function tex2chtml(tex: string, options?: ConversionOptions): Element;
function tex2mml(tex: string, options?: ConversionOptions): Element;
// Convert MathML to output formats
function mml2svg(mathml: string, options?: ConversionOptions): Element;
function mml2chtml(mathml: string, options?: ConversionOptions): Element;
// Convert AsciiMath to output formats
function asciimath2svg(ascii: string, options?: ConversionOptions): Element;
function asciimath2chtml(ascii: string, options?: ConversionOptions): Element;Automatic discovery and rendering of mathematics in HTML documents with support for dynamic content updates.
function typeset(elements?: Element[]): void;
function typesetPromise(elements?: Element[]): Promise<void>;
function typesetClear(elements?: Element[]): void;High-quality mathematical rendering using HTML/CSS (CommonHTML) or Scalable Vector Graphics (SVG) with font support and styling options.
function chtmlStylesheet(): Element;
function svgStylesheet(): Element;
function getMetricsFor(wrapper: Element, display: boolean): Metrics;Comprehensive accessibility support including speech generation, semantic enrichment, interactive exploration, and assistive technology integration.
interface AccessibilityOptions {
backgroundColor?: string;
backgroundOpacity?: number;
foregroundColor?: string;
foregroundOpacity?: number;
highlight?: string;
subtitles?: boolean;
speech?: boolean;
}Modular component system for loading input/output processors, extensions, and additional functionality on demand.
interface LoaderOptions {
load?: string[];
dependencies?: Record<string, string[]>;
paths?: Record<string, string>;
source?: Record<string, string>;
}interface Configuration {
loader?: LoaderOptions;
startup?: StartupOptions;
tex?: TexOptions;
mml?: MMLOptions;
asciimath?: AsciiMathOptions;
chtml?: CommonHTMLOptions;
svg?: SVGOptions;
options?: GlobalOptions;
}
interface ConversionOptions {
display?: boolean;
em?: number;
ex?: number;
containerWidth?: number;
scale?: number;
family?: string;
}
interface MathJax {
version: string;
config: Configuration;
loader: Loader;
startup: Startup;
_: InternalModules;
// Initialization
init(config?: Configuration): Promise<MathJax>;
// Document processing
typeset(elements?: Element[]): void;
typesetPromise(elements?: Element[]): Promise<void>;
typesetClear(elements?: Element[]): void;
// Conversion methods (created dynamically based on loaded components)
tex2svg?(tex: string, options?: ConversionOptions): Element;
tex2chtml?(tex: string, options?: ConversionOptions): Element;
tex2mml?(tex: string, options?: ConversionOptions): Element;
mml2svg?(mathml: string, options?: ConversionOptions): Element;
mml2chtml?(mathml: string, options?: ConversionOptions): Element;
asciimath2svg?(ascii: string, options?: ConversionOptions): Element;
asciimath2chtml?(ascii: string, options?: ConversionOptions): Element;
// Promise-based conversion methods
tex2svgPromise?(tex: string, options?: ConversionOptions): Promise<Element>;
tex2chtmlPromise?(tex: string, options?: ConversionOptions): Promise<Element>;
tex2mmlPromise?(tex: string, options?: ConversionOptions): Promise<Element>;
mml2svgPromise?(mathml: string, options?: ConversionOptions): Promise<Element>;
mml2chtmlPromise?(mathml: string, options?: ConversionOptions): Promise<Element>;
asciimath2svgPromise?(ascii: string, options?: ConversionOptions): Promise<Element>;
asciimath2chtmlPromise?(ascii: string, options?: ConversionOptions): Promise<Element>;
// Reset methods
texReset?(...args: any[]): void;
mmlReset?(...args: any[]): void;
asciimathReset?(...args: any[]): void;
// Stylesheet methods
chtmlStylesheet?(): Element;
svgStylesheet?(): Element;
// Metrics
getMetricsFor?(wrapper: Element, display: boolean): Metrics;
// Utility methods
done(): Promise<void>;
whenReady(callback: () => void): Promise<void>;
}
interface Loader {
load(...components: string[]): Promise<any[]>;
ready(...components: string[]): Promise<any[]>;
preLoaded(...components: string[]): void;
checkVersion(component: string, version: string, name: string): boolean;
getRoot(): string;
}
interface Startup {
document: HTMLDocument;
input: InputJax[];
output: OutputJax;
adaptor: DOMAdaptor;
handler: Handler;
registerConstructor(name: string, constructor: any): void;
useHandler(name: string, force?: boolean): void;
useAdaptor(name: string, force?: boolean): void;
useInput(name: string, force?: boolean): void;
useOutput(name: string, force?: boolean): void;
}
interface Metrics {
em: number;
ex: number;
containerWidth: number;
scale: number;
}