@mdit/plugin-tex
Plugins for
This plugin is facing developers.
If you are looking for an out of box solution, you should try @mdit/plugin-katex and @mdit/plugin-mathjax, which are based on this plugin.
Usage
import MarkdownIt from "markdown-it";
import { tex } from "@mdit/plugin-tex";
const mdIt = new MarkdownIt().use(tex, {
render: (content, displayMode) => {
// render tex here and return
},
});
mdIt.render("$E=mc^2$");This plugin registers markdown rules for render function.
Syntax
The plugin supports different delimiter syntaxes based on the delimiters option:
Default Syntax (dollars)
Inline mode:
$xxx$Display mode:
$$xxx$$ $$ xxx $$
LaTeX Style (brackets)
Inline mode:
\(xxx\)Display mode:
\[xxx\] \[ xxx \]
Mixed Syntax (all)
When delimiters is set to "all", both dollar and bracket syntaxes are supported.
Escaping can be done by using \ before the $ character, or adding space both before and after the $ character:
- The
is a TeX equation, while $ a=1 $ and $a=1$ is not.
- The $a=1$ is a TeX equation, while $ a=1 $ and \$a=1$ is not.Options
delimiters
- Type:
"brackets" | "dollars" | "all" - Default:
"dollars" - Details: 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.
mathFence
- Type:
boolean - Default:
false - Details: Whether parsed fence block with math language to display mode math.
allowInlineWithSpace
- Type:
boolean - Default:
false - Details: Whether 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.
render
- Type:
TexRender
/**
* Tex Render function
*
* @param content Text content
* @param displayMode whether is display mode
* @param env MarkdownIt environment
* @returns render result
*/
type TexRender = (content: string, displayMode: boolean, env: MarkdownItEnv) => string;- Required: Yes
- Details: Tex Render function. Takes content, displayMode, and environment, returns rendered string.
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\}
$$Tex Tutorial
Operator
Some operators can be entered directly in math mode; others need to be generated using control sequences:
+:-:\times:\div:=:\pm:\cdot:\cup:\geq:\leq:\neq:\approx:\equiv:\quad: (blank separator)
Radical:
\sqrt{xxx}Fraction
\frac{aaa}{bbb} (the first parameter is the numerator and the second is the denominator).Sum:
\sumTandem:
\prodLimit:
\limPoints:
\intMultiple points:
\iint:\iiint:\iiiint:\idotsint
Large operators such as continuous addition, multiplication, limits, and integrals can use \limits and \nolimits to force explicitly specify compress these superscripts or not.
\varoiint, \sqint, \sqiint, \ointctrclockwise, \ointclockwise, \varointclockwise, \varointctrclockwise, \fint, \landupint, \landdownint are not supported currently.
$\sqrt{x}$, $\frac{1}{2}$.
$\sum_{i=1}^n i\; \prod_{i=1}^n$
$\sum\limits _{i=1}^n i\; \prod\limits_{i=1}^n$
$\iint_1^2 x^2\; \iiint_1^2 x^2\; \iiiint_1^2 x^2\; \idotsint_1^2 x^2$
$\iint\limits_1^2 x^2\; \iiint\limits_1^2 x^2\; \iiiint\limits_1^2 x^2\; \idotsint\limits_1^2 x^2$
$$
\iint_1^2 x^2\; \iiint_1^2 x^2\; \iiiint_1^2 x^2\; \idotsint_1^2 x^2
$$Symbol
English letters can be entered directly
$a \quad b \quad c \quad x \quad y \quad z \quad A \quad B \quad C$Greek characters use
\characterNameto enter symbols, and output capital letters when the first letter is capitalized.$\alpha \quad \beta \quad \gamma \quad \Omega \quad \Delta \quad \Gamma$Other mathematical expressions can be used correspondingly
$\log_{a}{b} \quad \partial x$
Superscript and Subscript
- Superscript, use
^to achieve - Subscript, use
_to achieve - By default, superscript and subscript only apply to the next character. To work with multiple consecutive characters, please enclose these characters in curly brackets
{}.
Einstein ’s
Einstein ’s $E=mc^2$.
$2^{10} > 1000$Delimiters (parentheses, etc.)
Various parentheses are represented by commands such as (), [], \{\}, \langle\rangle.
Note that curly braces are usually used to enter command and environment parameters, so they must be preceded by \ in mathematical formulas.
Because the application of | and \| in LaTeX is too casual, we recommend using \lvert\rvert and \lVert\rVert instead.
To adjust the size of these delimiters, we recommend using \big, \Big, \bigg, \Bigg and a series of commands to adjust the size before the above brackets.
$\Biggl(\biggl(\Bigl(\bigl((x)\bigr)\Bigr)\biggr)\Biggr)$
$\Biggl[\biggl[\Bigl[\bigl[[x]\bigr]\Bigr]\biggr]\Biggr]$
$\Biggl \{\biggl \{\Bigl \{\bigl \{\{x\}\bigr \}\Bigr \}\biggr \}\Biggr\}$
$\Biggl\langle\biggl\langle\Bigl\langle\bigl\langle\langle x
\rangle\bigr\rangle\Bigr\rangle\biggr\rangle\Biggr\rangle$
$\Biggl\lvert\biggl\lvert\Bigl\lvert\bigl\lvert\lvert x
\rvert\bigr\rvert\Bigr\rvert\biggr\rvert\Biggr\rvert$
$\Biggl\lVert\biggl\lVert\Bigl\lVert\bigl\lVert\lVert x
\rVert\bigr\rVert\Bigr\rVert\biggr\rVert\Biggr\rVert$Ellipsis
The ellipsis is represented by commands such as \dots,\cdots, \vdots,\ddots.
\dots and\cdots have different vertical positions. The former is generally used for subscripted sequences.
$x_1,x_2,\dots ,x_n \quad 1,2,\cdots ,n \quad \vdots\quad \ddots$Matrix
pmatrix, bmatrix, Bmatrix, vmatrix, Vmatrix and other environments can add various separators on both sides of the matrix.
$$
\begin{pmatrix} a&b\\c&d \end{pmatrix} \quad
\begin{bmatrix} a&b\\c&d \end{bmatrix} \quad
\begin{Bmatrix} a&b\\c&d \end{Bmatrix} \quad
\begin{vmatrix} a&b\\c&d \end{vmatrix} \quad
\begin{Vmatrix} a&b\\c&d \end{Vmatrix}
$$Using the smallmatrix environment, you can generate small matrices of inline formulas.
A small matrix:
A small matrix: $( \begin{smallmatrix} a&b\\c&d \end{smallmatrix} )$.Multi-line Formula
newline
Use
\\or\newlineto wrap$$ x = a+b+c+ \\ d+e+f+g $$ $$ x = a+b+c+ \newline d+e+f+g $$Alignment
You can use the
alignedenvironment to achieve alignment, and&to identify fixed anchor points$$ \begin{aligned} x ={}& a+b+c+{} \\ &d+e+f+g \end{aligned} $$ $$ \begin{alignedat}{2} 10&x+ &3&y = 2 \\ 3&x+&13&y = 4 \end{alignedat} $$
Formula Group
Formula groups that do not require alignment can use the gathered environment.
$$
\begin{gathered}
a = b+c+d \\
x = y+z
\end{gathered}
$$Numbering
$$
\tag{1} x+y^{2x}
$$
$$
\tag*{1} x+y^{2x}
$$Segmented Functions
Use cases environment
$$
y= \begin{cases}
-x,\quad x\leq 0 \\
x,\quad x>0
\end{cases}
$$Text
To insert text in TeX, you should use \text{} to wrap them.