RaTeX: A cross-platform LaTeX math rendering engine written in pure Rust

89 points FlounderPleasant8692 20 comments

https://preview.redd.it/zfm3esmpqkog1.png?width=783&format=png&auto=webp&s=6388de4d1b62bdddbdefc8f39598bb2d24f2d515

I've been working on RaTeX, a LaTeX math rendering engine written in pure Rust。

The problem

Rendering LaTeX math in cross-platform apps is surprisingly fragmented. The common solutions all have the same weakness:

  • Web / cross-platform frameworks (Flutter, React Native) → WebView + KaTeX/MathJax, which adds tens of MB of memory overhead and JS startup latency
  • Native-only renderers (SwiftMath on iOS, JLatexMath on Android) → each platform needs its own implementation, no shared code

With AI tools and education apps generating large amounts of math content, a single rendering core that works everywhere felt like a missing piece.

What RaTeX does

RaTeX parses LaTeX and produces a renderer-agnostic display list — a flat sequence of drawing commands (glyphs, lines, rectangles, bezier paths) with absolute coordinates. Everything platform-specific lives at the edges.

The pipeline:

tokenization → parsing → layout → display list

The display list is then consumed by:

  • ratex-wasm — compiles to WASM, returns the display list as JSON for Canvas 2D rendering in the browser ✅ working
  • ratex-render — rasterizes to PNG via tiny-skia, no browser needed ✅ working
  • ratex-ffi — C ABI for iOS (Swift/ObjC), Android (JNI), Flutter (Dart FFI), React Native 🚧 in progress

Code

Server-side PNG rendering today looks like this:

use ratex_parser::parser::parse;
use ratex_layout::{layout, to_display_list, LayoutOptions};
use ratex_render::{render_to_png, RenderOptions};

let ast = parse(r"\frac{-b \pm \sqrt{b^2 - 4ac}}{2a}")?;
let layout_box = layout(&ast, &LayoutOptions::default());
let display_list = to_display_list(&layout_box);
let png = render_to_png(&display_list, &RenderOptions::default())?;

std::fs::write("formula.png", png)?;

Current status

  • ~99% of KaTeX formula syntax parses correctly
  • ~80% visual similarity vs KaTeX reference renders (tested against 916 formulas, pixel-level comparison)
  • The remaining gaps are mostly edge cases: accents, extensible arrows, some spacing rules

I built a live support table that renders all 916 formulas with both engines side-by-side, so it's easy to see exactly where RaTeX matches and where it falls short.

Why this matters

As far as I know, there's no open-source cross-platform LaTeX renderer that runs natively without a browser. RaTeX is an attempt to fill that gap — one Rust core, every platform.

It's still early, but the foundation is there. If you've been building an app that needs math rendering and didn't want to ship a WebView, this might be useful to you.

Stars and issues welcome — especially if you hit a formula that doesn't render right.

GitHub: [https://github.com/erweixin/RaTeX]

Demo: [https://erweixin.github.io/RaTeX/demo/index.html]

Support table: [https://erweixin.github.io/RaTeX/demo/support_table.html]