10 ms·
KaTeX – The fastest math typesetting library for the web
- skywritergr 6y agoI used Katex on my GatsbyJS blog and it worked nicely. Felt like doing latex but less confusing.
- esperent 6y agoIf you already know latex pretty well then it's pretty confusing because you'll keep finding that things you expect to work are not supported.
- vcxy 6y agoThis is my experience as well. I just stick with MathJax for this reason. Since I'm already very familiar with LaTeX proper (use it every single work day), it's far less jarring to just use MathJax.
- snicker7 6y agoKaTeX is great, but I long for a system that: 1. Can be rendered server-side without a heavy runtime (ideally written in C/C++/Rust/Whatever instead of JS). 2. Has a well defined output AST, so that the output can be manipulated (e.g. realtime visual equation editing). 3. Is accessible to the visually impared. Actually, we have such a system: MathML. And its a W3C standard to boot. The only reason why we have to rely on slow JS hacks like KaTeX and MathJax is because of Chrome. Google hates disabled people.
- gostsamo 6y agoThe bad accessibility of math notation and math content is one of the main reasons there are very few visually impaired people in STEM, one of the main reasons why I'm doing code and not physics, and why Firefox is given as the accessibility default in web.
- xiphias2 6y agoActually often I wish that AI papers contained code instead of math so that it's clearer how it can be implemented, and how the variables are used. It would make the papers a bit longer, but implementations easier to understand.
- gspr 6y ago> Actually often I wish that AI papers contained code instead of math so that it's clearer how it can be implemented That's insanity. Sure, code is a nice addition, but it does not convey the same information as the math does. It's akin to asking to replace an engineer's design of a bridge with a scale model.
- c0nfused 6y agoI think this is more akin to construction workers asking for blueprints rather than cad models. As someone who hasn't actively done formal math in a couple of decades the slog of relearning syntax to read papers about code can be off putting. I understand that this makes me a barbarian. However code is really the lingua franca we use as an industry but in papers it is treated exactly as you say a nice addition.
- arthur2e5 6y agoNot quite the same field, but see https://news.ycombinator.com/item?id=24649992 https://news.ycombinator.com/item?id=24649992 for an article about PLT (programming language theory) notation.
- gspr 6y ago> I think this is more akin to construction workers asking for blueprints rather than cad models. No, because those aim to express the same thing, albeit with different limitations and practical considerations. Code and math are entirely separate things. > As someone who hasn't actively done formal math in a couple of decades the slog of relearning syntax to read papers about code can be off putting. Surely the syntax is an incredibly minor part of it all? > However code is really the lingua franca we use as an industry but in papers it is treated exactly as you say a nice addition. Code is an implementation detail. Papers express ideas, not implementations.
- foepys 6y agoI don't know if I'm reading [1] correctly but it appears that Google is finally working on implementing MathML in Chromium. There have been multiple commits and updates to this bug in 2020, especially in September. Edit: It's not being implemented by Google. It's a sponsored effort because Google couldn't be bothered. [2] 1: https://bugs.chromium.org/p/chromium/issues/detail?id=6606 https://bugs.chromium.org/p/chromium/issues/detail?id=6606 2: https://mathml.igalia.com https://mathml.igalia.com
- bjoli 6y agoIgalia seems like such a great company. They are doing lots of important and exciting things in open source. Sure, they get paid for it and all that, but it seems like such a cool idea. I am glad it seems to work for them
- hyiltiz 6y agoWould mathematical[0] satisfy at least the first two requirement? I just found about it from the MathJax post today. [0] https://github.com/gjtorikian/mathematical https://github.com/gjtorikian/mathematical
- vbernat 6y agoKaTeX can be generated server-side (no JS on client-side). Its output is browser-independent. It contains MathML in its output and the remaining of the output is correctly tagged to be ignored by readers: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>g</mi><mi>a</mi></msup><mspace></mspace><mspace width="0.6666666666666666em"></mspace><mrow><mi mathvariant="normal">m</mi><mi mathvariant="normal">o</mi><mi mathvariant="normal">d</mi></mrow><mtext> </mtext><mtext> </mtext><mi>p</mi><mo>=</mo><mi>A</mi></mrow><annotation encoding="application/x-tex">g^a\mod p=A</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.858832em;vertical-align:-0.19444em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.664392em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">a</span></span></span></span></span></span></span></span><span class="mspace allowbreak"></span><span class="mspace" style="margin-right:0.6666666666666666em;"></span></span><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord"><span class="mord"><span class="mord mathrm">m</span><span class="mord mathrm">o</span><span class="mord mathrm">d</span></span></span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord mathnormal">p</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.68333em;vertical-align:0em;"></span><span class="mord mathnormal">A</span></span></span></span> This is: g^k\equiv a\pmod{p}
- snicker7 6y agoJS on server-side can be taxing. If I don't use NodeJS, then I'd have to (1) call it from a script, eating the startup time (2) run a daemon of NodeJS workers running in parallel to my web server or (3) use an "embedded" JS engine, hoping I don't run into compatibility issues. All three options have performance costs + adds more dependencies. If KaTeX were written in C, for example, it would be no problem FFI'ing it into just about any language. Plus whatever perf benefits there are of not using JS.
- Loic 6y agoWorking daily with a blind scientist, I am translating the equations from old papers without good OCR in Fortran to discuss them. I haven't found a better way, but we would definitely appreciate more accessible content.
- neolog 6y agoYou're translating into fortran? Why?
- Loic 6y agoBecause Fortran is FORmula TRANslator and it is very easy to express complex equations with it. It the lingua franca of scientific computing.
- TheRealKing 6y agobecause it is powerful, fast, simple, object oriented, while being natively parallel, on both shared and distributed architecture.
- euske 6y agoFYI, LaTeX notation is used by many blind people to express math ideas even if they don't actually use LaTeX for typesetting. (I personally know two programmers, one math teacher and one scientist who are using it with a braille display on daily basis.) So it's not totally anti-accessibility.
- wsimson 6y agoI think this might be what you're looking for: https://github.com/cqql/pelican-katex https://github.com/cqql/pelican-katex
- susam 6y agoI considered switching from MathJax to KaTeX for my MathJax-based projects such as https://github.com/susam/texme https://github.com/susam/texme and http://mathb.in/ http://mathb.in/ but I had to decide against doing so because MathJax supports commands like \begin{align}, \begin{equation}, \label, \eqref, \notag, etc. that KaTeX does not, so switching to KaTeX would break many existing documents written using TeXMe or MathBin. I am going to stick with MathJax for now. Commands supported in MathJax: http://docs.mathjax.org/en/latest/input/tex/macros/ http://docs.mathjax.org/en/latest/input/tex/macros/ Commands supported in KaTeX: https://katex.org/docs/supported.html https://katex.org/docs/supported.html
- esperent 6y agoFor some reason in katex you have to use \begin{aligned} instead of \begin{align} but it works.
- susam 6y agoYes, the "aligned" environment is a different environment but it is similar to the "align" environment. The "aligned" environment is available in MathJax too. The "aligned" environment is meant to be a part of a larger displayed math block, i.e., it goes between \[ and \] or $$ and $$. The "align" environment, on the other hand, is a standalone environment that can be used outside displayed math block. While the "aligned" environment is supported by both MathJax and KaTeX, the "align" environment is supported by MathJax only.
- esperent 6y agoHuh, I never knew that. Well in any case the differences are minor so you can just replace align with aligned https://tex.stackexchange.com/questions/95402/what-is-the-difference-between-aligned-in-displayed-mode-and-starred-align https://tex.stackexchange.com/questions/95402/what-is-the-di...
- vcxy 6y agoIsn't the big difference equation numbering? If you actually like to label and eqref stuff, it's not really a replacement. you can get around this I guess, but for me, it's much easier if my basic latex stuff works the same everywhere.
- saagarjha 6y agoI wish MathML was better supported :(
- thayne 6y agoIIRC, chrome intentionally decided not to add support for MathML, but it looks like they may have changed their mind (https://www.chromestatus.com/feature/5240822173794304 https://www.chromestatus.com/feature/5240822173794304)? Although, to be honest I'd rather have support for something closer to LaTeX. MathML is incredibly verbose, and certainly not something you would want to write by hand.
- susam 6y agoDespite MathML being too verbose, if it were well supported across all major web browsers, we could write mathematical content in LaTeX or another syntax and then convert it to MathML. It would have been very similar to how a lot of content is written in Markdown these days and then converted to HTML.
- runarberg 6y agoYou should think of MathML like SVG. Most people use equation editors (a la Inkscape for SVGs) or a library (d3) that outputs MathML that you can include in your website. You also have the option to write simple equations by hand, but most people won’t.
- thayne 6y agoThe verbosity of MathML also has costs in terms of bandwidth (although that is mitigated by compression) and likely parsing speed.
- Mikhail_Edoshin 6y agoActually MathML is much simpler to parse: it's already parsed, what you get is basically an AST in XML.
- 6y ago
- whatever1 6y agoI don’t understand why the whole math related community have conditioned ourselves to tolerate the horrible *tex macro language universe and its tool chain for almost half a century. Debugging is horrible, syntax is unnecessarily verbose, formatting is hit or miss, portability is pure lottery, compiling is suuuper slow. When I write text in an editor, it just works. Why for math do I have to spend all of these additional computing cycles? I don’t have an alternative (if I had I would have started a company), I just know that I hated all of the hours I wasted formatting math manuscripts and I wish somebody could stand up and go against Knuth’s way of thinking.
- qd6pwu4 6y agoYes, it's a pain to write math related manuscripts. Normal texts are a simple character list, so they are easy to input. Math equations are much more complex, with more symbols, 2D placement. So we need to 'mark up' the normal texts to provide enough information for a proper math equation. Current machine learning can help translate hand-written equations into the one computer can understand, I think this can help a lot. However, if we want to use keyboard only, it still seems very hard to design a system that can input math as smooth as texts without 'mark up' or GUI.
- runarberg 6y agoI gave this problem a stab a few years ago. I wrote an ascii-math derived language (but more powerful, and a little easier) called MathUp. I planed for it to be quick to write and easy to read (kind of like markdown for math). However it is not nearly as powerful as latex (and I don’t plan for it to be). Target are people that are writing quick math-notes (e.g. in class), or on forums where participants don’t necessarily know latex but might need to write a non-trivial equation or two. https://runarberg.github.io/mathup/ https://runarberg.github.io/mathup/
- tombert 6y agoI don't really agree with you being downvoted; I love LaTeX, I use it for basically any kind of mathey stuff that I need, but it is an objectively irritating language, especially for beginners. That said, the fact is that for mathematical equation editing, for better or worse, TeX-based systems have a near-monopoly on high-quality typesetting; KaTeX giving us a fast, easy-to-use implementation of it is still a net good.
- amake 6y agoI was looking for a LaTeX rendering solution for a Flutter and ended up wrapping KaTeX: I use a platform-side web view to render with KaTeX and then send the rendered PNG bytes back to the Dart world. https://pub.dev/packages/flutter_tex_js https://pub.dev/packages/flutter_tex_js It’s hacky but it’s been working incredibly well.
- Greek0 6y agoI switched from MathJax to Katex on my personal blog, and it significantly improved my lighthouse scores. Mathjax needs a huge amount of JS/CSS/Fonts to do its job, and takes a long time to load and render the equations. Not all of that is deferable.
- miki123211 6y agoIf anyone cares, this works with screen readers. The MathML output is there, and that's all screen readers need to render math properly.
- supz_k 6y agoWe use this with markdown. Basically, we extended our markdown parser to parse anything between two $$ tags as KaTeX. It was really easy to implement. And, KaTeX is much faster than other alternatives we tested (Yes, that's what title says :) )
- esperent 6y agoHave you encountered the situation where you need to include a $ symbol in your markdown yet? Very tricky and when the issue was raised with katex devs they basically said "not our problem" and closed it.
- dwheeler 6y agoWhy not use $ ?
- esperent 6y agoIt's a while since I encountered this issue but I think the markdown engine was converting that back to $. In the end I had to use <span>$</span> and even that too some jumping through hoops because again markdown was removing html. This was in Hugo, whatever markdown engine they use.
- supz_k 6y agoWe actually use two $$ to start and end. Ex: $$ c = \pm\sqrt{a^2 + b^2} $$ Or, multiline $$ \frac{1}{\Bigl(\sqrt{\phi \sqrt{5}}-\phi\Bigr) e^{\frac25 \pi}} = 1+\frac{e^{-2\pi}} {1+\frac{e^{-4\pi}} {1+\frac{e^{-6\pi}} {1+\frac{e^{-8\pi}} {1+\cdots} } } } $$ Some details are available [in this post](https://talk.hyvor.com/blog/introducing-katex-embed/ https://talk.hyvor.com/blog/introducing-katex-embed/) on how it works. And, katex.renderToString has an option to set throwOnError: false.. So, whenever if the TeX code inside $$ is not valid, it just returns the text.
- esperent 6y agoYes, I do the same. I meant when you have to use a currency in dollars in your markdown. Try writing this sentence (sorry, it's early here and I can't think of a better example): I have $3. $$\frac{3}{2}$$
- fibo 6y agoMathJax is more complete than KaTeX and it is supported on GitHub Pages, see also https://g14n.info/2014/09/math-on-github-pages/ https://g14n.info/2014/09/math-on-github-pages/
- esperent 6y agoIt's also a lot heavier and slower. Unless you need extra features that katex doesn't support it's best to use the latter.
- Kwpolska 6y agoIt’s not “supported on GitHub Pages” in any specific way. Both engines can work client-side by scanning your HTML for LaTeX code enclosed in $$…$$ or \(…\) or other configurable delimiters. You can just drop the appropriate JS/CSS into your default layout, and you don’t need any special support from Jekyll for this to work. Kramdown can be configured with a math backend, and the only thing it will do in MathJax mode is replace $$…$ with \(…\) and \[…\] depending on the environment, and not parse escapes and Markdown syntax in that snippet. The KaTeX mode of kramdown does stuff server-side, which might not be desirable on GitHub Pages — but this is fixable by just telling Kramdown you’re using MathJax, and including KaTeX with auto-render in your templates.
- rsfairman 6y agoA big problem with systems like KaTeX and MathJAX is that they are not compatible with LaTeX. They are superficially similar, but not the same. LaTeX has a huge number of packages that serious math people heavily rely on. What's needed is version of the underlying TeX engine that is HTML-aware. This way, a multipage document could be complied as usual, say to be printed or displayed as a static PDF, or it could be compiled to a web-friendly format for display in a browser, with links, etc.
- chutsu 6y agoStill missing the ability to properly reference equations from text. e.g. `\refeq{}`.
- wsimson 6y agoFor faster server-side rendering, this is a good, though relatively unknown resource: https://github.com/cqql/pelican-katex https://github.com/cqql/pelican-katex