Add LaTeX Rendering Support
1 min read
This guide shows you how to render LaTeX math formulas in your Starlight project using Sätteri and katex.
Step-by-step Guide
Section titled “Step-by-step Guide”-
Install the required dependencies
Terminal window npm add katex satteri @astrojs/markdown-satteriTerminal window pnpm add katex satteri @astrojs/markdown-satteriTerminal window yarn add katex satteri @astrojs/markdown-satteriTerminal window bun add katex satteri @astrojs/markdown-satteri -
Create the Sätteri KaTeX plugin
Save the following code as
src/plugins/satteri-katex.ts:src/plugins/satteri-katex.ts import katex from "katex";import { defineMdastPlugin } from "satteri";export const satteriKatex = defineMdastPlugin({name: "katex",math(node) {const html = katex.renderToString(node.value, {displayMode: true,throwOnError: false,});return { rawHtml: html };},inlineMath(node) {const html = katex.renderToString(node.value, {displayMode: false,throwOnError: false,});return { rawHtml: html };},}); -
Configure Sätteri processor in
astro.config.mjsOpen your
astro.config.mjsfile and configure the Sätteri processor with thesatteriKatexplugin:astro.config.mjs import { satteri } from "@astrojs/markdown-satteri";import starlight from "@astrojs/starlight";import { satteriKatex } from "./src/plugins/satteri-katex";import { defineConfig } from "astro/config";export default defineConfig({markdown: {processor: satteri({mdastPlugins: [satteriKatex],features: {math: true,},}),},integrations: [starlight({customCss: ["katex/dist/katex.min.css"],}),],});
Example Usage
Section titled “Example Usage”Create a file like src/content/docs/example.mdx and add:
When $a\ne0$, there are two solutions to $(ax^2 + bx + c = 0)$ and they are
$$x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$$The code above generates the following on the page:
When , there are two solutions to and they are