Skip to content

Add LaTeX to Starlight

This guide leads you use LaTeX in Starlight.

  1. Start by installing the remark-math and rehype-katex package using your favorite package manager:

    Terminal window
    npm add katex rehype-katex remark-math
  2. Add katex, rehype-katex and remark-math in astro.config.mjs:

    astro.config.mjs
    // ...
    import remarkMath from 'remark-math'
    import rehypeKatex from 'rehype-katex'
    export default defineConfig({
    // ...
    markdown: {
    remarkPlugins: [remarkMath],
    rehypePlugins: [rehypeKatex],
    },
    integrations: [
    starlight({
    // ...
    customCss: ['katex/dist/katex.min.css'],
    })
    ],
    })
src/content/docs/example.mdx
When $a \ne 0$, there are two solutions to $(ax^2 + bx + c = 0)$ and they are
$$ x = {-b \pm \sqrt{b^2-4ac} \over 2a} $$

The code above generates the following on the page:

When a0a \ne 0, there are two solutions to (ax2+bx+c=0)(ax^2 + bx + c = 0) and they are x=b±b24ac2ax = {-b \pm \sqrt{b^2-4ac} \over 2a}