@mdit/plugin-katex
Plugins to render math expressions with KaTeX.
Note
This plugin is based on @mdit/plugin-tex.
Usage
import { katex } from "@mdit/plugin-katex";
import MarkdownIt from "markdown-it";
const mdIt = new MarkdownIt().use(katex);
mdIt.render("$E=mc^2$");We also have a package called @mdit/plugin-katex-slim which katex is an optional peer.
Syntax
The syntax depends on the delimiters option:
- Default (
"dollars"): Use$tex expression$inline, and$$tex expression$$for block. - LaTeX style (
"brackets"): Use\(tex expression\)inline, and\[tex expression\]for block. - Both (
"all"): Both dollar and bracket syntaxes are supported.
Style
You should import katex/dist/katex.min.css from katex package or CDN yourself.
Escaping
You can use
\to escape$\(and\[:Euler’s identity \$e^{i\pi}+1=0$will be
Euler’s identity $e^{i\pi}+1=0$
Options
This plugin extends KaTeX options. Available options include:
"brackets" | "dollars" | "all""dollars"Math delimiter syntax to enable.
"brackets": Use\(...\)for inline math and\[...\]for display math (LaTeX style)."dollars": Use$...$for inline math and$$...$$for display math (common Markdown style)."all": Enable both bracket and dollar syntaxes.
booleanfalseWhether parsed fence block with math language to display mode math.
booleanfalseWhether to allow inline math with spaces on ends. NOT recommended to set this to true, because it will likely break the default usage of $. Note that a space-padded literal $ inside the content (e.g. $\text{a $ b}$) is then treated as a closing $, closing the expression early.
KatexLoggertype KatexLogger<MarkdownItEnv = unknown> = (
errorCode:
| "unknownSymbol"
| "unicodeTextInMathMode"
| "mathVsTextUnits"
| "commentAtEnd"
| "htmlExtension"
| "newLineInDisplayMode",
errorMsg: string,
token: Token | undefined,
env: MarkdownItEnv,
) => "error" | "warn" | "ignore" | boolean | undefined;Error logger function.
(content: string, displayMode: boolean) => stringTransformer function on output content.
Demo
Euler’s identity
Euler’s identity $e^{i\pi}+1=0$ is a beautiful formula in $\mathbb{R}^2$.
$$
\frac {\partial^r} {\partial \omega^r} \left(\frac {y^{\omega}} {\omega}\right)
= \left(\frac {y^{\omega}} {\omega}\right) \left\{(\log y)^r + \sum_{i=1}^r \frac {(-1)^i r \cdots (r-i+1) (\log y)^{r-i}} {\omega^i} \right\}
$$mhchem extension
If you want to load the mhchem extension, you should import @mdit/plugin-katex/mhchem:
import { katex } from "@mdit/plugin-katex";
import "@mdit/plugin-katex/mhchem";Support List
Cookbook
See the comparison report for the full upstream comparison.