What is katex?
KaTeX is a fast, easy-to-use JavaScript library for TeX math rendering on the web. It provides a way to display mathematical notation in web pages, supporting a wide range of TeX functions and symbols.
What are katex's main functionalities?
Render TeX to HTML
This feature allows you to convert TeX expressions into HTML. The `renderToString` method takes a TeX string and returns an HTML string that can be inserted into a web page.
const katex = require('katex');
const html = katex.renderToString('c = \pm\sqrt{a^2 + b^2}');
console.log(html);
Render TeX to DOM
This feature allows you to render TeX expressions directly into a DOM element. The `render` method takes a TeX string and a DOM element, and it updates the element's content with the rendered math.
const katex = require('katex');
const element = document.getElementById('math');
katex.render('E = mc^2', element);
Auto-render TeX in HTML
This feature automatically finds and renders all TeX expressions within a given DOM element. The `renderMathInElement` function scans the element for TeX expressions and replaces them with rendered math.
const katex = require('katex');
const renderMathInElement = require('katex/contrib/auto-render');
renderMathInElement(document.body);
Other packages similar to katex
mathjax
MathJax is another popular JavaScript library for displaying mathematical notation in web pages. It supports a wider range of input formats, including TeX, MathML, and AsciiMath. MathJax is known for its high-quality rendering and extensive configurability, but it is generally slower than KaTeX.
asciimath
AsciiMath is a simpler alternative to KaTeX and MathJax, designed for ease of use. It uses a more human-readable syntax compared to TeX. While it is easier to write and understand, it does not support as many advanced mathematical features as KaTeX or MathJax.
KaTeX is a fast, easy-to-use JavaScript library for TeX math rendering on the web.
- Fast: KaTeX renders its math synchronously and doesn't need to reflow the page. See how it compares to a competitor in this speed test.
- Print quality: KaTeX's layout is based on Donald Knuth's TeX, the gold standard for math typesetting.
- Self contained: KaTeX has no dependencies and can easily be bundled with your website resources.
- Server side rendering: KaTeX produces the same output regardless of browser or environment, so you can pre-render expressions using Node.js and send them as plain HTML.
KaTeX is compatible with all major browsers, including Chrome, Safari, Firefox, Opera, Edge, and IE 11.
KaTeX supports much (but not all) of LaTeX and many LaTeX packages. See the list of supported functions.
Try out KaTeX on the demo page!
Getting started
Starter template
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.4/dist/katex.min.css" integrity="sha384-vKruj+a13U8yHIkAyGgK1J3ArTLzrFGBbBc0tDp4ad/EyewESeXE/Iv67Aj8gKZ0" crossorigin="anonymous">
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.4/dist/katex.min.js" integrity="sha384-PwRUT/YqbnEjkZO0zZxNqcxACrXe+j766U2amXcgMg5457rve2Y7I6ZJSm2A0mS4" crossorigin="anonymous"></script>
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.4/dist/contrib/auto-render.min.js" integrity="sha384-+VBxd3r6XgURycqtZ117nYw44OOcIax56Z4dCRWbxyPt0Koah1uHoK0o4+/RRE05" crossorigin="anonymous"
onload="renderMathInElement(document.body);"></script>
</head>
...
</html>
You can also download KaTeX and host it yourself.
For details on how to configure auto-render extension, refer to the documentation.
API
Call katex.render
to render a TeX expression directly into a DOM element.
For example:
katex.render("c = \\pm\\sqrt{a^2 + b^2}", element, {
throwOnError: false
});
Call katex.renderToString
to generate an HTML string of the rendered math,
e.g., for server-side rendering. For example:
var html = katex.renderToString("c = \\pm\\sqrt{a^2 + b^2}", {
throwOnError: false
});
Make sure to include the CSS and font files in both cases.
If you are doing all rendering on the server, there is no need to include the
JavaScript on the client.
The examples above use the throwOnError: false
option, which renders invalid
inputs as the TeX source code in red (by default), with the error message as
hover text. For other available options, see the
API documentation,
options documentation, and
handling errors documentation.
Demo and Documentation
Learn more about using KaTeX on the website!
Contributors
Code Contributors
This project exists thanks to all the people who contribute code. If you'd like to help, see our guide to contributing code.
Financial Contributors
Become a financial contributor and help us sustain our community.
Individuals
Organizations
Support this project with your organization. Your logo will show up here with a link to your website.
License
KaTeX is licensed under the MIT License.