To render math in Astro with KaTeX, install remark-math and rehype-katex, pass them to the unified() Markdown processor from @astrojs/markdown-remark in astro.config.mjs, and import KaTeX’s stylesheet in your post layout. Then $E = mc^2$ renders inline and $$ ... $$ renders as a display equation, all at build time with no client JavaScript.
Key Takeaways
- Astro 7’s default Markdown processor is Sätteri; remark and rehype plugins need the
unified()processor.remark-mathparses$and$$;rehype-katexturns them into HTML at build time.- The KaTeX CSS (and its fonts) must load on pages with math, or equations render as jumbled text.
- MDX files inherit the processor’s plugins automatically.
Install
npm install @astrojs/markdown-remark remark-math rehype-katex katex
Configure the processor
// astro.config.mjs
import { defineConfig } from 'astro/config';
import mdx from '@astrojs/mdx';
import { unified } from '@astrojs/markdown-remark';
import remarkMath from 'remark-math';
import rehypeKatex from 'rehype-katex';
export default defineConfig({
integrations: [mdx()],
markdown: {
processor: unified({
remarkPlugins: [remarkMath],
rehypePlugins: [rehypeKatex],
}),
},
});
Older guides put these plugins in markdown.remarkPlugins and markdown.rehypePlugins. Those options still work but are deprecated in Astro 7 in favor of markdown.processor. Our remark and rehype plugins guide covers the processor in more depth.
Load the stylesheet
---
// src/layouts/PostLayout.astro
import 'katex/dist/katex.min.css';
---
<article>
<slot />
</article>
Importing it through the layout lets Vite bundle the CSS and copy KaTeX’s font files with hashed names. Load it only in layouts that render math to keep other pages lighter.
Write math
<!-- src/content/blog/compound-interest.md -->
The balance after $n$ years is $A = P(1 + r)^n$.
$$
A = P\left(1 + \frac{r}{k}\right)^{kn}
$$
Fenced code blocks with the math language also render as display math:
```math
\sum_{i=1}^{n} i = \frac{n(n+1)}{2}
```
Astro’s default syntaxHighlight.excludeLangs is ['math'], so Shiki leaves those blocks alone for KaTeX.
Options and gotchas
- Dollar signs in prose. With
remark-math,$5 and $10can be read as math. Escape them as\$5, or pass[remarkMath, { singleDollarTextMath: false }]to allow only$$. - MDX braces. In
.mdxfiles, curly braces outside math are JSX expressions. Inside$...$they are fine becauseremark-mathclaims them first. - Macros. Pass KaTeX options as a tuple:
[rehypeKatex, { macros: { '\\R': '\\mathbb{R}' } }]. - Accessibility. KaTeX outputs MathML alongside the visual HTML by default, which screen readers can use.
KaTeX’s supported functions list shows what renders. For docs and course sites with heavy notation, our documentation themes are a good base, and the content docs explain where Markdown config lives. Our Markdown features guide covers the rest of the syntax.
FAQ
How do I add LaTeX math to Astro?
Install remark-math and rehype-katex, add them to unified({ remarkPlugins, rehypePlugins }) as markdown.processor, and import katex/dist/katex.min.css in your layout.
Does KaTeX in Astro need client-side JavaScript?
No. rehype-katex renders equations to HTML during the build. Visitors only download the KaTeX CSS and fonts.
Why do my KaTeX equations look broken in Astro?
The KaTeX stylesheet is not loaded on that page. Import katex/dist/katex.min.css in the layout that renders the content.