CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-katex

Fast math typesetting for the web.

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

cli.mddocs/

CLI Interface

Command-line interface for server-side rendering and batch processing, supporting all KaTeX options and file I/O operations.

Installation and Usage

# Install KaTeX globally for CLI access
npm install -g katex

# Or use npx for one-time usage
npx katex --help

Capabilities

Basic Rendering

Convert TeX expressions to HTML using standard input/output or files.

# Read from stdin, output to stdout
echo "E = mc^2" | katex

# Read from file, output to stdout  
katex --input equation.tex

# Read from stdin, write to file
echo "\\frac{1}{2}" | katex --output result.html

# Read from file, write to file
katex --input math.tex --output math.html

Display Mode

Control inline vs display math rendering.

# Render in display mode (centered, larger)
katex --display-mode --input equation.tex

# Render in inline mode (default)
echo "\\sum_{i=1}^n i" | katex

Output Format

Control the output markup format.

# HTML + MathML output (default)
katex --format htmlAndMathml --input math.tex

# HTML only
katex --format html --input math.tex  

# MathML only
katex --format mathml --input math.tex

Error Handling

Configure error behavior and styling.

# Suppress errors, show error text instead of throwing
katex --no-throw-on-error --input bad-math.tex

# Custom error color (without # prefix)
katex --no-throw-on-error --error-color ff6b6b --input bad-math.tex

# Default behavior: throw on errors
katex --input bad-math.tex  # Will exit with error code

Macro Definitions

Define custom macros via command line or macro files.

# Single macro definition  
katex --macro "\\RR:\\mathbb{R}" --input math.tex

# Multiple macros
katex \
  --macro "\\RR:\\mathbb{R}" \
  --macro "\\NN:\\mathbb{N}" \
  --macro "\\d:\\mathrm{d}" \
  --input math.tex

# Macros from file (one per line, format: \macro:expansion)
katex --macro-file macros.txt --input math.tex

Example macro file (macros.txt):

\RR:\mathbb{R}
\NN:\mathbb{N} 
\ZZ:\mathbb{Z}
\CC:\mathbb{C}
\d:\mathrm{d}
\diff:\frac{\mathrm{d}#1}{\mathrm{d}#2}

Advanced Options

Configure LaTeX compatibility, security, and performance.

# Strict LaTeX mode
katex --strict --input math.tex

# Trust input for HTML features
katex --trust --input math-with-links.tex

# Custom rule thickness
katex --min-rule-thickness 0.05 --input fractions.tex

# Maximum element size limit
katex --max-size 50 --input large-math.tex

# Maximum macro expansions
katex --max-expand 500 --input macro-heavy.tex

# Color as text color mode
katex --color-is-text-color --input colored-math.tex

Complete CLI Reference

Usage: katex [options]

Options:
  -V, --version                     output the version number
  -d, --display-mode                render in display mode
  -F, --format <type>               output format: htmlAndMathml, html, mathml
  -t, --no-throw-on-error          render errors instead of throwing
  -c, --error-color <color>         error color (without #)
  -m, --macro <def>                 define macro: \name:expansion
  -f, --macro-file <path>           read macros from file
  -b, --color-is-text-color         \color behaves like \textcolor
  -S, --strict                      strict LaTeX mode
  -T, --trust                       trust input (enable HTML features)
  -s, --max-size <n>                maximum element size in ems
  -e, --max-expand <n>              maximum macro expansions
  --min-rule-thickness <size>       minimum rule thickness in ems
  -i, --input <path>                input file path
  -o, --output <path>               output file path
  -h, --help                        display help

Batch Processing

Process multiple files using shell scripting:

# Process all .tex files in directory
for file in *.tex; do
  katex --input "$file" --output "${file%.tex}.html"
done

# Process with consistent options
find . -name "*.tex" -exec katex \
  --display-mode \
  --no-throw-on-error \
  --macro-file macros.txt \
  --input {} \
  --output {}.html \;

Integration Examples

Node.js Script:

const { execSync } = require('child_process');

function renderTeX(tex, options = {}) {
  const args = [];
  
  if (options.displayMode) args.push('--display-mode');
  if (options.noThrow) args.push('--no-throw-on-error');
  if (options.format) args.push(`--format ${options.format}`);
  
  const cmd = `echo "${tex}" | katex ${args.join(' ')}`;
  return execSync(cmd, { encoding: 'utf8' });
}

const html = renderTeX('E = mc^2', { displayMode: true });
console.log(html);

Build Tool Integration:

// webpack.config.js
const { execSync } = require('child_process');

module.exports = {
  plugins: [
    {
      apply: (compiler) => {
        compiler.hooks.emit.tap('KatexPlugin', (compilation) => {
          // Process .tex assets
          Object.keys(compilation.assets)
            .filter(name => name.endsWith('.tex'))
            .forEach(name => {
              const source = compilation.assets[name].source();
              const html = execSync(`echo "${source}" | katex --display-mode`, 
                                  { encoding: 'utf8' });
              compilation.assets[name.replace('.tex', '.html')] = {
                source: () => html,
                size: () => html.length
              };
            });
        });
      }
    }
  ]
};

Server-Side Rendering

Use CLI for server-side math rendering:

# Express.js endpoint simulation
echo "\\int_0^\\infty e^{-x^2} dx" | \
  katex --display-mode --format html > /tmp/math.html

# Static site generation
for markdown in content/*.md; do
  # Extract math blocks and render them
  sed -n '/```math/,/```/p' "$markdown" | \
    sed '1d;$d' | \
    katex --display-mode --output "${markdown%.md}-math.html"
done

docs

accessibility.md

advanced-apis.md

auto-rendering.md

chemistry.md

cli.md

configuration.md

copy-tex.md

core-rendering.md

index.md

mathtex-script-type.md

tile.json