Skip to main content

How to use LaTeX in Docusaurus with Remark Math and Rehype KaTeX

Installation

You can type LaTeX\LaTeX in Docusaurus by using the remark-math and rehype-katex packages.

In the root directory of your Docusaurus project, install the packages by running npm install remark-math and npm install rehype-katex (or the yarn equivalents).

Configuration

At the top of docusaurus.config.ts, import the packages:

import remarkMath from 'remark-math';
import rehypeKatex from 'rehype-katex';

Pass the plugins to your docs presets:

  presets: [
[
'classic',
{
docs: {
remarkPlugins: [remarkMath],
rehypePlugins: [rehypeKatex],

Get the URL for the KaTeX\KaTeX stylesheet from README on github.com/KaTeX/KaTeX, then pass it to the stylesheet config:

  stylesheets: [
{
href: 'https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css',
type: 'text/css',
integrity:
'sha384-nB0miv6/jRmo5UMMR1wu3Gz6NLsoTkbqJghGIsx//Rlm+ZU03BU6SQNC66uf4l5+',
crossorigin: 'anonymous',
},

You can also choose to self-host the stylesheets on your own content delivery network (CDN). It's more work than using jsdeliver, but you have more control over your website. Download the latest version available on KaTeX\KaTeX releases page, unpack it, and upload it to your own CDN.

If you choose to self-host, be careful about the integrity hash of the katex.min.css file. If you modify the file, you need to update the hash, run something like openssl dgst -sha384 -binary file/path/to/katex/v0.16.11/katex.min.css | openssl base64 -A.

If you have a pre-commit formatter like husky, then avoid formatting the file on commit with the --no-verify flag: git commit -m 'chore: bump katex to v0.16.11' --no-verify.

Usage

You can use either $ brackets or $$ brackets across multiple lines.

For example, this

**Fundamental Theorem of Calculus**  
Let $f:[a,b] \to \R$ be Riemann integrable. Let $F:[a,b]\to\R$ be $F(x)=
\int_{a}^{x}f(t)dt$.
Then $$F$$ is continuous, and at all $x$ such that $f$ is continuous at $x$,
$F$ is differentiable at $x$ with $F'(x)=f(x)$.

will print to this:

Fundamental Theorem of Calculus
Let f:[a,b]Rf:[a,b] \to \R be Riemann integrable. Let F:[a,b]RF:[a,b]\to\R be F(x)=axf(t)dtF(x)= \int_{a}^{x}f(t)dt. Then FF is continuous, and at all xx such that ff is continuous at xx, FF is differentiable at xx with F(x)=f(x)F'(x)=f(x).