Skip to content(if available)orjump to list(if available)

KaTeX – The fastest math typesetting library for the web

stared

As a very much work-in-progress, yesterday, I started working on interactive equations https://p.migdal.pl/equations-explained-colorfully/ (it uses KaTeX under the hood).

The idea is similar to https://betterexplained.com/articles/colorized-math-equation..., but with mouseover interaction (both for further description, but also for accessibility). For a deeper dive in the topic of explorable explanations, I wrote https://p.migdal.pl/blog/2024/05/science-games-explorable-ex... (was here on the HN as well).

taylorgibson

Does anyone know why Google doesn’t adopt something like this in Google Docs / Slides? It’s amazing to me that after all these years Google Docs still has some of the worst equation editing of all word processors. I was hopeful when they added markdown support that first class equation editing was near, but it’s been a while now and still nothing.

Lord-Jobo

Speculation: Google has a moderate amount of “core” applications that are clearly getting indefinite support, but all of those services also have a very conservative feature and update roadmap. New features are going to be limited to things that alphabet has in its global focus, so crap like Gemini and not much else.

I’m sure engineers working on products like the google web suite would love to implement this kind of thing but have zero authorization or resources for that.

Experimental applications, the ones that bloom rapidly and get shut down in a cycle that google has become known for, are ironically way more likely to see this kind of cool niche update.

Twisted company has a whacky way of managing their products, because advertisement drives their revenue so everything else is just freestyle jazz.

esafak

If only they'd added it in the first iteration when they had a free hand.

susam

I switched from MathJax to KaTeX earlier this year for my blog. So far, it has been working out well.

The only feature I really missed was the ability to use \label and \eqref for equation referencing, since KaTeX doesn't support these commands [1]. I worked around this limitation with a small custom setup defining my own \label and a custom \eqnref (not exactly \eqref though) [2].

Another thing that bothered me a little was that the KaTeX autorender extension does not recognise \begin{align*}, \begin{alignat*}, etc. as top-level delimiters by default [3] but this was trivial to fix by customising the set of default delimiters [2].

I know KaTeX supports server-side rendering but I don't use that yet [4]. I still use client-side rendering. Despite that my maths pages [5][6] render quickly since they are usually small, with only a single reflow from the initial load to the rendered page, without the continual reflows or jitter I used to see with MathJax. Overall, I am quite happy with the switch from MathJax to KaTeX.

[1] https://katex.org/docs/supported.html

[2] https://github.com/susam/susam.net/blob/0.3.0/layout/include...

[3] https://katex.org/docs/autorender#api

[4] https://news.ycombinator.com/item?id=44615271

[5] https://susam.net/mutually-attacking-knights.html

[6] https://susam.net/zigzag-number-spiral.html

stared

I am curious, if there is any library for inline formula like KaTeX, but supporting Typst syntax?

jeremyscanvic

I would assume you pretty much get that out of the box given Typst compiles to HTML natively?

stared

I was more looking for things I can use with blogs with Markdown and frameworks like Astro.

But with the development of Typst, maybe the way to go is to use Typst rather than Markdown.

sureglymop

I wish there was a native version of this. Every SSG seems to have the problem of having to depend on node in order to prerender html for math with katex.

koito17

I've been using katex-rs, a Rust rewrite, to implement LaTeX rendering for a Rust web app. It was easy enough to hook into pulldown_cmark, so that $ and $$ and render a decent subset of LaTeX. Since pulldown_cmark is a proper Markdown parser, you listen for Event::InlineMath and Event::DisplayMath then call KaTeX directly. No regex or HTML escaping necessary. In my web app, this is all encapsulated into a single function that I can call within Tera templates. It's as SSR as it gets; no Node.js or client-side JavaScript necessary.

The costliest asset is a minified stylesheet served through a CDN. (I do this out of laziness, and because the web app as-is needs nothing more than the standard Rust toolchain).

https://github.com/katex-rs/katex-rs

nicoburns

Do you have a reference on how to hook it into pulldown_cmark? I'd love to add math support into to my markdown rendering app but I haven't had time to investigate this myself yet.

MillironX

Hugo includes native KaTeX: https://gohugo.io/functions/transform/tomath/

The docs recommend setting up KaTeX CSS (which requires either a CDN link or Node), but by changing output to 'mathml,' you can have the browser render equations with zero dependencies.

notpushkin

Port it? Or maybe bundle a tiny interpreter instead of Node.

xigoi

Also see Temml, a fork of KaTeX that compiles to MathML instead of styled HTML.

https://temml.org/

tommoor

Outline (https://www.getoutline.com) includes the KaTeX renderer in it's documents if you're interested in trying a knowledge base that has great direct support.

https://docs.getoutline.com/s/guide/doc/formatting-kn6wBtxlQ...

creata

It may or may not be as fast, but MathJax has broader support for LaTeX features, better accessibility features, and has slightly better typography in my very subjective opinion, and that's more important to me than a bit of rendering time.

s20n

I use KaTeX for my blog, and indeed KaTeX was faster than MathJax 2, but MathJax 3 (a complete rewrite) has significantly improved performance from the previous version and is now a bit faster than KaTeX in my experience.

This website has a comparison of the loading times of the same LaTeX rendered in both KaTeX and MathJax: https://www.intmath.com/cg5/katex-mathjax-comparison.php

lifthrasiir

It is amusing that, in my particular environment, KaTeX is slower than MathJax 3 in processing time but actually faster when font loading time is accounted for. Both loads from your domain so there should be no routing issue; KaTeX fonts turned out to be substantially smaller than MathJax 3 at least in this particular case. Is this intentional or just a lucky coincidence for KaTeX? (KaTeX is also ~70% smaller than MathJax 3 in their gzipped forms, so it might well be intentional!)

esafak

So if speed is comparable, which is best?

bobajeff

I use this all the time when editing markdown in vscodium. It's fast enough for the side preview and supports all the LaTeX commands I need so far. When I need a PDF Pandoc handles the conversion well enough for me. I've tried using Quarto's preview but it's so slow in comparison.

zaoui_amine

KaTeX is solid;

larodi

Powering the math inference revolution given is used by every other LLM provider such as ………(name it).