#katex #mdbook #latex #html

bin+lib mdbook-katex

mdBook preprocessor rendering LaTex equations to HTML

24 releases

Uses new Rust 2021

0.2.17 Nov 25, 2022
0.2.16 Nov 25, 2022
0.2.10 Jun 9, 2021
0.2.8 Dec 3, 2020
0.1.8 Nov 19, 2020

#200 in Text processing

Download history 68/week @ 2022-08-18 72/week @ 2022-08-25 73/week @ 2022-09-01 104/week @ 2022-09-08 80/week @ 2022-09-15 78/week @ 2022-09-22 50/week @ 2022-09-29 61/week @ 2022-10-06 56/week @ 2022-10-13 92/week @ 2022-10-20 70/week @ 2022-10-27 82/week @ 2022-11-03 116/week @ 2022-11-10 87/week @ 2022-11-17 309/week @ 2022-11-24 145/week @ 2022-12-01

671 downloads per month

MIT license

640 lines

mdbook-katex is a preprocessor for mdBook, pre-rendering LaTex equations to HTML at build time. It allows for very fast page loading, compared to rendering equations in the browser.

This preprocessor uses the katex crate; see this page for the list of supported LaTex functions.

Getting Started

First, install mdbook-katex

cargo install mdbook-katex

Then, add the following lines to your book.toml file



You can now use $ and $$ delimiters for inline and display equations within your .md files. If you need a regular dollar symbol, you can escape delimiters with a backslash \$.

# Chapter 1

Here is an inline example, $ \pi(\theta) $, 

an equation,

$$ \nabla f(x) \in \mathbb{R}^n, $$

and a regular \$ symbol.

LaTex equations will be rendered as HTML when running mdbook build or mdbook serve as usual.

Katex options

The preprocessor supports passing options to the katex-rs crate in order to configure its behaviour. These options are specified under the [preprocessor.katex] directive.

The currently spported arguments are:

Argument Type
leqno boolean
fleqn boolean
throw-on-error boolean
error-color string
min-rule-thickness number
max-size number
max-expand number
trust boolean

There are also options to configure the behaviour of the preprocessor:

Option Default Description
static-css false Generates fully static html pages with katex styling
macros None Path to macros file (see Custom macros)
include-src false Append the source code for the rendered math expressions after them

For example:

static-css = false
include-src = false

Custom macros

Custom LaTex macros must be defined in a .txt file, according to the following pattern

\R:{\mathbb{R}^{#1 \times #2}}

You need to specify the path of this file in your book.toml as follows

macros = "path/to/macros.txt"

These macros can then be used in your .md files

# Chapter 1

$$ \grad f(x) \in \R{n}{p} $$

Including math source

This option is added so users can have a convenient way to copy the source code of math expressions when they view the book.

When include-src is set to true, the included math source code is appended to each rendered math expression. Math expressions are span elements with class="katex. Appended math source code is wrapped in span elements with class="katex-src".

For example,

Define $f(x)$:


is rendered as (the content of the katex spans are omitted and represented as )

Define <span class="katex"></span><span class="katex-src">f(x)</span>:

<span class="katex-display"><span class="katex"></span></span><span class="katex-src"><br>f(x)=x^2\\\\<br>x\\in\\R<br></span>

The math source code is included in a minimal fashion, and it is up to the users to write custom CSS and JavaScript to make use of it. For more information about adding custom CSS and JavaScript in mdbook, see additional-css and additional-js.

If you need more information about this feature, please check the issues or file a new issue.


The build artifact of the book will be in a folder named html inside the directory you specify instead of being directly there. Consider this when you use mdbook_katex in your CIs.

$\backslash$ does not work, but you can use $\setminus$ instead.


~415K SLoC