Guide · Open source
How to use KaTeX: load it, render math and avoid common pitfalls
Updated · written with AI from the project's own documentation and checked against our catalogue data
KaTeX is a JavaScript library that takes math written in TeX or LaTeX notation and displays it on a web page as formatted equations. This guide shows you how to load it, render expressions in the browser or on a server, and where its support for LaTeX stops. It is aimed at site builders, developers and anyone who already writes formulas in LaTeX syntax.
Quick start
- 1Choose how to load KaTeX. Use the jsDelivr CDN links from the starter template or download KaTeX from its releases page and host it yourself.
- 2Add the stylesheet. Include katex.min.css in the head of the page, since rendering depends on the CSS and font files.
- 3Load the scripts. Add katex.min.js and, if you want automatic rendering, the auto-render extension, both deferred.
- 4Render math in the browser. Call katex.render with a TeX string, a target element and throwOnError set to false.
- 5Pre-render on the server if needed. Call katex.renderToString in Node.js and send the resulting HTML to visitors.
- 6Check support for your formulas. Confirm each LaTeX command you need appears in the supported functions list.
What KaTeX does and who it suits
KaTeX renders math synchronously and, according to its README, doesn't need to reflow the page. Its layout is based on Donald Knuth's TeX, and it has no dependencies, so it can be bundled with your website resources. It is compatible with Chrome, Safari, Firefox, Opera and Edge.
- Good fit: readable equations in articles, docs or course pages
- Good fit: a lightweight renderer inside a web app
- Good fit: servers that pre-render math and send plain HTML
- Poor fit: full LaTeX support, since KaTeX supports much but not all of LaTeX and many packages
- Poor fit: rendering non-math documents or typesetting whole pages
Add KaTeX to a web page with the starter template
The simplest route is to load KaTeX from the jsDelivr CDN by copying the link tags from the starter template in the README. The template includes the stylesheet, the deferred KaTeX script and the auto-render extension. Keep the HTML5 doctype, because the README notes that without it KaTeX may not render properly.
You can also download KaTeX from its releases page and host the files yourself. Either way, the CSS and font files must be included for rendering to work.
- Use the HTML5 doctype at the top of the page
- Include the stylesheet katex.min.css in the head
- Load katex.min.js and, for automatic rendering, contrib/auto-render.min.js
- The auto-render script calls renderMathInElement(document.body) on load
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.19.1/dist/katex.min.css">Render math with katex.render in the browser
Call katex.render to draw a TeX expression directly into a DOM element. Pass the expression, the target element and an options object. The README example uses throwOnError set to false, which shows invalid input as the TeX source in red with the error message as hover text.
The expression is a JavaScript string, so backslashes must be escaped as double backslashes.
- First argument: the TeX expression as a string
- Second argument: the element to render into
- Third argument: options, such as throwOnError
katex.render("c = \\pm\\sqrt{a^2 + b^2}", element, {
throwOnError: false
});Pre-render math on the server with renderToString
Call katex.renderToString to produce an HTML string instead of writing to the page. This suits server-side rendering: the README says KaTeX gives the same output regardless of browser or environment, so you can pre-render with Node.js and send plain HTML to visitors.
If you render everything on the server, you do not need to include the JavaScript on the client, but the CSS and font files are still required.
- Returns a string such as a span with class katex
- Client does not need katex.min.js when all rendering happens on the server
- CSS and font files are still needed in both approaches
var html = katex.renderToString("c = \\pm\\sqrt{a^2 + b^2}", {
throwOnError: false
});Handle errors and find the full option list
With throwOnError set to false, invalid TeX is displayed as its source rather than stopping the page. The README points to separate documentation for the API, the options and the error handling. Check the list of supported functions before relying on a particular LaTeX command or package.
- API reference: katex.org/docs/api.html
- Options reference: katex.org/docs/options.html
- Error handling: katex.org/docs/error.html
- Auto-render configuration: katex.org/docs/autorender.html
- Supported functions: katex.org/docs/supported.html
What people use it for
- Adding equations to blog posts or documentation pages
- Showing formulas in an online course or lesson page
- Rendering math inside a web application with a lightweight library
- Pre-rendering equations on a server so visitors receive plain HTML
Frequently asked questions
Is KaTeX free to use?
KaTeX is licensed under the MIT License, as stated in its README. The license text is linked from the project's license section.
What do I need to install KaTeX?
The project lists no prerequisites. You can load it from the jsDelivr CDN using the starter template links, or download it from the releases page and host it yourself.
Does KaTeX support all of LaTeX?
No. The README says KaTeX supports much, but not all, of LaTeX and many LaTeX packages. Check the list of supported functions before you depend on a command.
How is KaTeX different from MathJax?
MathJax is one of the comparable projects in this directory, alongside MathML. The source here does not compare features in detail, but the README does link a speed test comparing KaTeX with a competitor.
Why does my math not show up?
The README says the CSS and font files must be included for rendering to work, and that the HTML5 doctype is required. Missing either can stop KaTeX from rendering properly.
Can KaTeX render math on the server?
Yes. Use katex.renderToString in Node.js to generate HTML, and the README says the output is the same regardless of browser or environment.
New agent skills, repos and AI tools — every week
The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.
Our verdict
KaTeX suits website builders, developers and teams who want fast, dependency-free rendering of common LaTeX math in articles, docs or web apps. If you need full LaTeX support, complete document typesetting or a renderer for non-math content, the README points you to looking elsewhere, and MathJax or MathML are listed as comparable options.
Install options & downloads