How to use LaTeX in Docusaurus with Remark Math and Rehype KaTeX
#
InstallationYou can type LaTeX in Docusaurus by using remark math.
In the root directory of your Docusaurus project, run yarn add remark-math
and yarn add rehype-katex
. This will add remark-math
and rehype-katex
to the package.json
file.
#
ConfigurationAt the top of your docusaurus.config.js
, add
Then, in docusaurus.config.js
, add the following in module.exports
(not under themeConfig
)
Then add the following underneath your docs
section in presets
:
The result will look something like this:
#
Self-Hosting the StylesheetsYou can also choose to self-host the stylesheets. The latest versions are available here.
Go to static
by running cd static
in your root directory.
Then run
There's at least two ways to unpack the .tar.gz
files:
- Run
tar xvzf katex.tar.gz
. You can remove the.tar.gz
file withrm katex.tar.gz
- Run
gunzip katex.tar.gz
then runtar xvf katex.tar
.
Then, in the above configuration steps, replace href: 'https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css',
with
#
UsageYou can use either $
brackets or $$
brackets across multiple lines.
For example, this
will print to this:
Fundamental Theorem of Calculus
Let be Riemann integrable. Let be . Then is continuous, and at all such that is continuous at , is differentiable at with .
#
TroubleshootingThe latest versions of remark-math
and remark-katex
are v4.0.0
. However, remark-math
requires remark-parse v9.0.0
or remark v13.0.0
, which Docusaurus does not support yet, so you may need to change the versions of remark-math
and remark-katex
in package.json
:
You can learn more about remark v13 here and track whether Docusaurus supports it here.