7 ms·
KaTeX – The fastest math typesetting library for the web
- suioir 11mo agoA commenter left this on another HN post [0] and I thought it was worth its own. [0] https://news.ycombinator.com/item?id=45701400 https://news.ycombinator.com/item?id=45701400
- joppy 11mo agoThe commenter says pre-rendered/server-side-rendered mathematics (via katex) is great - I’ve found the opposite. It’s probably great if you have an article with one or two equations. On the other hand, if you have an article which uses mathematics pervasively, like many pure mathematics articles, it quickly becomes far more space efficient to render the mathematics on the client side. You can quickly get 200kB+ pages by pre-rendering.
- djoldman 11mo agoCould you please provide an example?
- Galanwe 11mo agoMy experience with dynamically rendered math has been the opposite: if you have lots of equations to render, it inevitably takes some milliseconds to render, which makes the whole content move around and shake as rendering takes places.
- Latitude7973 11mo agoDid you read the article? That's what the KaTeX project specifically claims to address.
- cubefox 11mo agoThe previous comment was about using KaTeX for pre-rendered equations.
- Vosporos 11mo agoIt's not too late to delete this comment.
- ayhanfuat 11mo agoIndeed. It was hell to navigate pages that rendered MathJax on demand. That also improved a lot though.
- blenderob 11mo ago> it inevitably takes some milliseconds to render, which makes the whole content move around and shake as rendering takes places. What a boldly incorrect comment! It's like you didn't even read the first point in TFA!
- lifthrasiir 11mo agoKaTeX weighs about the same if you do care about those metrics, however.
- susam 11mo agoIn case anyone wants to look at actual numbers about how much KaTeX weighs for a simple mathematics page: https://news.ycombinator.com/item?id=44614133 https://news.ycombinator.com/item?id=44614133 Quoting the relevant part from that link: katex.min.css 23.6 kB katex.min.js 277.0 kB auto-render.min.js 3.7 kB KaTeX_Main-Regular.woff2 26.5 kB KaTeX_Main-Italic.woff2 16.7 kB ---------------------------------- Total Additional 347.5 kB Of course, if the page uses more symbols in various sizes, then a few more fonts files (.woff2) need to be pulled in which case the weight of KaTeX would increase a bit too. Each font file weighs between 4 kB and 28 kB.
- adastra22 11mo agoIs 200kB supposed to be a lot?
- eviks 11mo agoBut how is better space efficiency at this level better than worse rendering efficiency?
- deleted 11mo ago[deleted]
- northlondoner 11mo agoSuper promising.
- northlondoner 11mo agoProbably I should have a bit more explicit why it is promising. Rendering speed is quite critical as it is related perception and human reaction time. If rendering is very slow then the engagement and iterating of writing maths will be interrupted. Imagine compiling of Latex would have taken 10 min, it won't be used widely like this. So it is promising, and maybe super promising indeed!
- holowoodman 11mo agoDisappointing that we still need those kinds of workarounds instead of just having native MathML support in all browsers.
- lifthrasiir 11mo agoWe will continue to have "workarounds" even after MathML because it is not an authoring-friendly markup. My ideal in this regard is a simplified eqn-like markup, which is not hard to write by hand nor hard to parse either.
- holowoodman 11mo agoYes, but those workarounds will be author-side ones. Like how HTML isn't very friendly to write by hand for many, so CMSes use e.g. Markdown or WYSIWYG to make it friendlier. In the same way, there will always be preprocessors in authoring tools that might convert e.g. TeX notation to MathML. My point is that "fast" in those kinds of workarounds wouldn't be a problem for visitors of a site because all the browser gets is just native MathML.
- BlackFly 11mo agoKatex produces MathML. The problem katex solves is that MathML is really ungainly for authoring equations. So instead you write equations in a DSL (which most people just call latex) and Katex compiles that to HTML/MathML for you. You can do this server side or client side and sadly too many people do it client side. If you do it server side, it is just one more step in your build next to transpiling and bundling.
- swiftcoder 11mo agoKatex can optionally produce MathML. By default it places individual symbols in a sea of <span> tags (due to inconsistent MathML output, I gather)
- jillesvangurp 11mo agoWe have web assembly and the power to run whatever you can manage to compile with that. There's no real need for "native" support. The key issue is that the latex stack wasn't really designed to be packaged up like this. It just has a lot of moving parts that are vaguely dependent on running in a full blown unix like shell environment. So the resulting code would be a rather big blob. Running that in a browser isn't that hard if you can live with that having a fair bit of overhead. This has been done. But it's a bit overkill for publishing content on the web. Browsers don't have native support for MathML any more for a good reason. Mozilla did support this for a while but dropped it because of limited adoption and high maintenance burden. Rendering formulas is a bit of a niche problem and the intended audience is just kind of picky when it comes to technology and generally not that into doing more advanced things with web browsers. Also, most people writing scientific articles would be writing those for publication and probably use Latex any way. So translating all their formulas to MathML is an extra step that they don't need or want. At least that's my analysis of this. I'm not really part of the target audience here and I'm sure there are plenty of MathML fans who disagree with this. In any case Katex makes an acceptable (to some) compromise by packaging this stuff up in a form where it can be run server side and is easy to integrate on a simple web page. A proper solution with buy-in from the scientific community (for e.g. MathML) is a much bigger/harder thing to solve. IMHO, a light weight solution based on web assembly could be the way to go. But of course the devil is in the details because if the requirements are "do whatever latex does" it gets quite hard. And anything else might be too limited.
- larodi 11mo agoPowering the math inference revolution given is used by every other LLM provider such as ………(name it).
- s20n 11mo agoI 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 https://www.intmath.com/cg5/katex-mathjax-comparison.php
- lifthrasiir 11mo agoIt 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 11mo agoSo if speed is comparable, which is best?
- mkl 11mo ago"Page complete" never stops with either library for me. It just keeps counting milliseconds. Chrome on Windows, no change when I disabled uBlock Origin Lite.
- thomasahle 11mo agoSome earlier discussions (2022): https://news.ycombinator.com/item?id=31441979 https://news.ycombinator.com/item?id=31441979 Most critique of KaTeX over MathJax is reduced support for LaTeX features.
- sureglymop 11mo agoI 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.
- notpushkin 11mo agoPort it? Or maybe bundle a tiny interpreter instead of Node.
- koito17 11mo agoI'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 https://github.com/katex-rs/katex-rs
- nicoburns 11mo agoDo 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.
- koito17 11mo agohttps://gist.github.com/akar1ngo/7d4ea6e9dfc369526beb75092cbd6cb8 https://gist.github.com/akar1ngo/7d4ea6e9dfc369526beb75092cb...
- sureglymop 11mo agoThank you for your account and reply! Awesome stuff :)
- taylorgibson 11mo agoDoes 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 11mo agoSpeculation: 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.
- drob518 11mo ago"Freestyle jazz" is pretty accurate, IMO.
- esafak 11mo agoIf only they'd added it in the first iteration when they had a free hand.
- apwheele 11mo agoThis is very obnoxious when transferring documents from Gemini to google docs (and not just math, tables/code sections are often not transferred correctly as well). I have a javascript hack from the dev console where I can at least print the chat to PDF, https://andrewpwheeler.com/2025/08/28/deep-research-and-open-access/ https://andrewpwheeler.com/2025/08/28/deep-research-and-open... (that at least worked 2 months ago)
- stared 11mo agoI am curious, if there is any library for inline formula like KaTeX, but supporting Typst syntax?
- jeremyscanvic 11mo agoI would assume you pretty much get that out of the box given Typst compiles to HTML natively?
- stared 11mo agoI 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.
- sprinkly-dust 11mo agoCurrently that feature is unsupported or I just can't figure out how to do it. With the latest compiler version 0.14 any .typ file I try to compile will incur warnings about skipping the equations (skipping the main reason I'd want to compile a Typst file to HTML...). As per their GitHub they haven't included MathJax or KaTeX support yet as they were more focused on semantic and structural accuracy of HTML output with this release.
- runarberg 11mo agoI wrote a library with a syntax similar to Typst (actually inspired by AsciiMath; and predates Typst by about a decade). Then there are Markdown-It plugins around it. https://mathup.xyz/ https://mathup.xyz/ https://github.com/runarberg/markdown-it-math https://github.com/runarberg/markdown-it-math
- creata 11mo agoIt 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.
- xigoi 11mo agoAlso see Temml, a fork of KaTeX that compiles to MathML instead of styled HTML. https://temml.org/ https://temml.org/
- susam 11mo agoI 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 https://katex.org/docs/supported.html [2] https://github.com/susam/susam.net/blob/0.3.0/layout/include/math.inc https://github.com/susam/susam.net/blob/0.3.0/layout/include... [3] https://katex.org/docs/autorender#api https://katex.org/docs/autorender#api [4] https://news.ycombinator.com/item?id=44615271 https://news.ycombinator.com/item?id=44615271 [5] https://susam.net/mutually-attacking-knights.html https://susam.net/mutually-attacking-knights.html [6] https://susam.net/zigzag-number-spiral.html https://susam.net/zigzag-number-spiral.html
- smohare 11mo agoI’ve never been able to switch since the feature set, whenever I’ve looked over the years, doesn’t seem geared toward serious mathematical exposition. I think I want to carry over habits from papers to blog pages though, which perhaps is misguided.
- bobajeff 11mo agoI 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.
- tommoor 11mo agoOutline (https://www.getoutline.com 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-kn6wBtxlQ1 https://docs.getoutline.com/s/guide/doc/formatting-kn6wBtxlQ...
- sherinjosephroy 11mo ago[dead]
- delf 11mo ago[dead]
- stared 11mo agoAs a very much work-in-progress, yesterday, I started working on interactive equations https://p.migdal.pl/equations-explained-colorfully/ https://p.migdal.pl/equations-explained-colorfully/ (it uses KaTeX under the hood). The idea is similar to https://betterexplained.com/articles/colorized-math-equations/ 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-explanations/ https://p.migdal.pl/blog/2024/05/science-games-explorable-ex... (was here on the HN as well).
- sleepyams 11mo agoThis is really awesome! I've been really interested in creating an interactive introduction to basic algebra where parts of the equation can be manipulated using drag-and-drop, but I couldn't really figure out the best way to do it. Maybe using Katex is the way to go?
- zaoui_amine 11mo agoKaTeX is solid;
- deleted 11mo ago[deleted]
- subset 11mo agoI think the latest version of MathJax (v4) has rendering speed comparable to, if not faster than, KaTeX. It also looks (subjectively) significantly better than KaTeX, and supports a wide array of accessibility features.
- silverwind 11mo agoMathJax does not even let you select text, so it seems less accessible to me.
- mkl 11mo agoMathJax lets you select text more often than KaTeX as far as I can tell (some KaTeX formulas act dead unless you start selecting outside them), not that either library give you anything especially useful if you try to copy and paste. MathJax will happily give you useful copy-to-clipboard if you right click. MathJax has way more accessibility features: https://docs.mathjax.org/en/latest/basic/accessibility.html https://docs.mathjax.org/en/latest/basic/accessibility.html
- inasio 11mo agoA few years ago I was evaluating options to move away from a deprecated external latex library my company relied on in Confluence, and tested Notion. I was super impressed at the rendering speed of their latex implementation (KaTex of course). As other have mentioned, not everything is there, but it was sufficiently good for our purposes. The switch was a pain, I hoped that Notion had good tools to move over from Confluence, but we had to do a custom job relying on sketchy undocumented APIs
- jerrygoyal 11mo agoWe used KaTeX via a React plugin to render math formulas as part of the AI response for our AI Writing Chrome extension (Jetwriter AI). We faced many challenges, especially with how different AI models syntax formulas, and had to do some string manipulation to make it work. Sadly, there isn't a go-to KaTeX library that would just work for AI chat apps.
- atkirtland 11mo agoKaTeX has always seemed pretty inferior to me compared with MathJax. The most significant flaw is that it is very unstable, frequently failing at rendering rendering large documents with many equations. This happens across several applications, though I suppose it could be machine-specific. It also doesn't support several basic packages that MathJax supports. Without measuring the speed difference, I've never noticed a gap anyways.
- viktorstrate 11mo agoWhat benefits does KaTeX provide over MathML which is natively supported by all major browsers, I suppose it can’t be faster than that? https://developer.mozilla.org/en-US/docs/Web/MathML https://developer.mozilla.org/en-US/docs/Web/MathML