Fast, fully configurable KaTeX rendering from Rust with drop-in WebAssembly bindings.
KaTeX-rs is a Rust re-implementation of the KaTeX rendering engine. It converts LaTeX math into HTML and MathML and is designed for server-side rendering, command-line tools, and WebAssembly targets. The project currently tracks KaTeX commit 785315c0f630f65347cac14b3ec72629cfe7631e.
- Native rendering pipeline. The
render_to_stringfunction turns LaTeX into KaTeX-compatible HTML + MathML markup that can be embedded directly into web pages or server-rendered responses. - Fine-grained configuration. Toggle display/inline layout, strictness and
trust modes, color and size options, equation numbering, custom macros, and
more through the
Settingsbuilder. - WebAssembly bindings. The
katex-wasm-bindingcrate exports the canonicalrenderandrenderToStringentry points so the generatedpkg/katex.jsbundle can replace KaTeX.js in existing JavaScript tooling without glue code. - Spec-driven test suite. Rust tests mirror the upstream KaTeX spec cases to ensure parsing and rendering stay in lockstep with the JavaScript reference implementation.
- Core parsing, HTML, and MathML rendering
- Spec-aligned unit and integration tests
- Automated screenshot regression harness
- WebAssembly bindings with KaTeX-compatible API surface
- Pixel perfect rendering with KaTeX across all platforms
[dependencies]
katex-rs = "0.2"use katex::{render_to_string, KatexContext, Settings};
fn main() -> Result<(), katex::ParseError> {
// The context caches fonts, macros, and environments – reuse it between renders.
let ctx = KatexContext::default();
// Start with the default configuration and tweak as needed.
let settings = Settings::default();
let html = render_to_string(&ctx, r"x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}", &settings)?;
println!("{html}");
Ok(())
}Configure display mode, numbering, colors and trust checks through the builder API:
use katex::{render_to_string, KatexContext, Settings, StrictMode, StrictSetting, TrustSetting};
fn main() -> Result<(), katex::ParseError> {
let ctx = KatexContext::default();
let settings = Settings::builder()
.display_mode(true)
.fleqn(true)
.leqno(true)
.strict(StrictSetting::Mode(StrictMode::Warn))
.trust(TrustSetting::Bool(true))
.color_is_text_color(true)
.build();
let html = render_to_string(&ctx, r"\\RR_{>0}", &settings)?;
println!("{html}");
Ok(())
}Install the npm package and invoke the familiar KaTeX API surface:
npm install katex-rsimport katex from "katex-rs";
const html = katex.renderToString("\\int_0^\\infty e^{-x^2} dx", {
displayMode: true,
trust: true,
});The WASM bundle exposes the same render/renderToString signatures as
KaTeX.js, accepts plain JavaScript option objects, and throws matching error
types for easy drop-in replacement.
A reproducible workflow – including repository hydration, tooling installation,
and verification commands – is documented in
docs/GETTING_STARTED.md. The quick-reference
checklist is:
- Hydrate the KaTeX submodule and Git LFS assets (
git submodule update --init --recursive). - Install Rust (stable + nightly), Node.js,
wasm-pack, andcargo-nextest. - Run formatting, Clippy, and the Nextest-powered test suite.
- Use
cargo xtask screenshotterfor browser-based regression tests. Pass--html-on-failureto capture HTML output from both the WASM and JavaScript implementations when comparing mismatched cases, and--allow-js-fallbackto treat the in-browser JavaScript rendering as the reference when baselines are missing or diverge on your platform. - Run
cargo bench --bench perffor the Criterion-based native benchmarks, andcargo bench --bench perf_gungraunfor Gungraun flamegraphs plus regression checks (Need installinggungraun-runnerfor this). You can checkout generated flamegraphs in thetarget/gungraun/katex-rs/perf_gungraunfolder.
Refer to docs/BENCHMARK.md and
docs/FLAMEGRAPH.md for deeper performance workflows.
The repository is organised as a Cargo workspace:
crates/katex– core renderer crate exported on crates.io.crates/wasm-binding– WebAssembly bindings that mirror KaTeX’s JavaScript API.xtask– developer tooling for screenshot tests, flamegraphs, and other automation.
KaTeX-rs is available under the MIT License. See LICENSE for details.