Write, Run & Share KaTeX code online using OneCompiler's KaTeX online editor for free. Choose 'KaTeX' as the language and you'll get a page with a couple of equations. Type LaTeX math into the HTML and the preview renders it as you go.
KaTeX is a library from Khan Academy for rendering LaTeX math in the browser. It's fast, renders synchronously, and produces the same output in every browser, so it's a common choice for course material, blogs and documentation that need equations.
The editor loads KaTeX and its auto-render extension from a CDN. renderMathInElement(document.body) finds math in the page and renders it.
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" />
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/contrib/auto-render.min.js"></script>
Wrap inline math in \( ... \) and block (display) math in $$ ... $$ or \[ ... \].
<p>Einstein wrote \( E = mc^2 \).</p>
$$ x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} $$
x^2 \quad a_n \quad x_{i}^{2} \quad \frac{a}{b} \quad \sqrt{x} \quad \sqrt[3]{x}
\alpha \; \beta \; \pi \; \theta \; \Sigma \; \infty \; \leq \; \geq \; \neq \; \approx
\sum_{i=1}^{n} i = \frac{n(n+1)}{2}
\qquad
\int_0^1 x^2 \, dx
\qquad
\lim_{x \to 0} \frac{\sin x}{x} = 1
\begin{pmatrix}
1 & 2 \\
3 & 4
\end{pmatrix}
Use katex.render to put math into a specific element.
katex.render('c = \\sqrt{a^2 + b^2}', document.getElementById('formula'), {
displayMode: true,
});
Backslashes need escaping inside JavaScript strings, so \sqrt becomes \\sqrt.