A pure C# port of KaTeX 0.19.1.
KStyleMath is a faithful re-implementation of KaTeX's parser, macro expander,
HTML builder and MathML builder. It has no JavaScript runtime, no browser
dependency and no third-party packages: the input is a LaTeX string and the
output is exactly the markup katex.renderToString would produce for the same
settings.
using KStyleMath;
string html = Katex.RenderToString(@"\frac{1}{2}");
string display = Katex.RenderToString(@"\sum_{i=1}^n i", new Settings {
DisplayMode = true,
});
// Advanced entry points mirroring KaTeX:
var tree = Katex.GenerateParseTree(@"x^2"); // parse tree (List<ParseNode>)
var dom = Katex.RenderToDomTree(@"x^2"); // virtual DOM (Span)
string onlyHtml = Katex.RenderToString(@"x^2", new Settings { Output = "html" });
string onlyMathML = Katex.RenderToString(@"x^2", new Settings { Output = "mathml" });src/KStyleMath.Demo is a Blazor Server live editor: type LaTeX on the left,
see the KaTeX-rendered result on the right. It includes a symbol palette,
sample formulas, inline/display toggle, HTML/MathML output selection,
parse-error reporting, generated-HTML inspection and a copy-to-clipboard button.
dotnet run --project src/KStyleMath.Demo
# open http://localhost:5014The demo vendors katex.min.css and its fonts under
src/KStyleMath.Demo/wwwroot/katex/ (MIT, from KaTeX 0.19.1).
src/KStyleMath.Benchmark measures the full parse → build → markup pipeline on
348 formulas. On the reference machine the full pipeline renders 18,430
formulas/s (mean 54 µs, median 21 µs), about 1.29× faster than upstream
KaTeX running on Node.js 24 / V8 13.6 on the same corpus. See
BENCHMARK.md for the full methodology and C#/Node comparison.
dotnet run --project src/KStyleMath.Benchmark -c Release
dotnet run --project src/KStyleMath.Benchmark -c Release -- corpus/perf.json --allocsrc/KStyleMath/
Core/ ParseNode, Token, ParseError, Style, Options, Settings, units, utils
Data/ embedded JSON from KaTeX (symbols, font metrics, SVG geometry, Unicode)
Parse/ Lexer, MacroExpander, Parser, macro table, registries
Build/ BuildCommon, BuildHTML, BuildMathML, Delimiter, Stretchy, BuildTree
Dom/ virtual HTML DOM (Span/Anchor/SymbolNode/...)
MathML/ virtual MathML DOM (MathNode/TextNode/SpaceNode)
Functions/ all src/functions/*.ts and src/environments/*.ts ports
Katex.cs public entry points
src/KStyleMath.Cli/ byte-for-byte corpus validator
src/KStyleMath.Demo/ Blazor Server interactive editor
corpus/ golden outputs from upstream KaTeX
The port keeps the original module structure and algorithm. Dynamic parse-node
objects are represented by ParseNode (a property bag), exactly like the
JavaScript objects. Browser APIs are replaced by the virtual DOM classes; only
toMarkup() is used, so no DOM shim is needed.
Data tables (src/KStyleMath/Data/*.json) are generated from the KaTeX source at
build time using .tools/extract.mjs; they are embedded resources.
corpus/goldens.json contains 128 formulas from KaTeX's own screenshot test
suite (test/screenshotter/ss_data.yaml) with golden renderToString output
generated by the real KaTeX 0.19.1 package.
dotnet run --project src/KStyleMath.Cli
The runner compares every formula byte-for-byte and writes diagnostics to
corpus/diffs/. Use --name CaseName --verbose to inspect one case, or
--expr "\frac12" to print a rendering.
Core parser, macro system, symbol tables, font metrics, HTML builder, MathML builder and all standard functions/environments are ported.
The current test corpus contains 2,600+ cases and all of them match upstream KaTeX byte-for-byte:
corpus/goldens.json– 128 screenshot-suite formulas, including display mathcorpus/extra.json– 492 formulas extracted from KaTeX's Jest specscorpus/variants.json– 744 renderings coveringoutput: html,output: mathmland forced display modecorpus/commands.json– 1,076 individual commands/symbols, including their error markupcorpus/macros.json– 152\def/\let/\newcommand/environment/function casescorpus/api.json– 37 settings/API cases (trust, custom macros, maxExpand, tagging, error handling, color modes, …)