CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-mathjax

JavaScript display engine for LaTeX, MathML, and AsciiMath mathematical notation in browsers and Node.js

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

initialization.mddocs/

Initialization and Configuration

MathJax provides comprehensive initialization and configuration capabilities for both browser and Node.js environments, with extensive customization options and component loading.

Capabilities

Initialization

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
  }
});

Component Loader

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();

Core Global Objects

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);

Startup System

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');

Configuration Options

Loader Configuration

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;
}

Startup Configuration

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;
}

Global Options

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;
}

Component Loading

Available Components

Input Components:

  • input/tex - TeX/LaTeX input processor
  • input/tex-base - Base TeX functionality
  • input/mml - MathML input processor
  • input/asciimath - AsciiMath input processor

Output Components:

  • output/chtml - CommonHTML output processor
  • output/svg - SVG output processor

TeX 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 boxes
  • And many more...

Accessibility Components:

  • a11y/semantic-enrich - Semantic enhancement
  • a11y/speech - Speech generation
  • a11y/explorer - Interactive exploration
  • a11y/assistive-mml - Assistive MathML

UI Components:

  • ui/menu - Context menu
  • ui/lazy - Lazy loading
  • ui/safe - Safe mode

Component Combinations

Pre-built component combinations for common use cases:

  • tex-chtml - TeX input + CommonHTML output
  • tex-svg - TeX input + SVG output
  • mml-chtml - MathML input + CommonHTML output
  • mml-svg - MathML input + SVG output
  • tex-mml-chtml - TeX + MathML inputs + CommonHTML output
  • tex-mml-svg - TeX + MathML inputs + SVG output

Each available with -nofont variants.

Environment-Specific Configuration

Browser Configuration

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'
  }
};

Node.js Configuration

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);

Error Handling

Loader Errors

MathJax.config.loader.failed = (error) => {
  console.error('Component failed to load:', error.message);
  // Handle component loading failure
};

Initialization Errors

try {
  const MathJax = await require('mathjax').init(config);
} catch (error) {
  console.error('MathJax initialization failed:', error.message);
  // Handle initialization failure
}

Advanced Configuration

Custom Path Filters

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
]);

Custom Components

// Register custom input processor
MathJax.startup.registerConstructor('myInput', MyInputProcessor);

// Use custom component
MathJax.startup.useInput('myInput');

docs

accessibility.md

document-rendering.md

extensions.md

index.md

initialization.md

input-processing.md

output-formats.md

tile.json