Add LaTeX Rendering Support
1 min read
This guide shows you how to render LaTeX math formulas in your Starlight project using remark-math and rehype-katex.
Step-by-step Guide
Section titled “Step-by-step Guide”-
Install the required dependencies
Terminal window npm add katex rehype-katex remark-mathTerminal window pnpm add katex rehype-katex remark-mathTerminal window yarn add katex rehype-katex remark-mathTerminal window bun add katex rehype-katex remark-math -
Configure Markdown plugins in
astro.config.mjsOpen your
astro.config.mjsfile and configure both the remark and rehype plugins:astro.config.mjs import { defineConfig } from "astro/config";import remarkMath from "remark-math";import rehypeKatex from "rehype-katex";import starlight from "@astrojs/starlight";export default defineConfig({markdown: {remarkPlugins: [remarkMath],rehypePlugins: [rehypeKatex],},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