@abreto/markdown-it-katex 中文文档教程

发布于 5年前 浏览 20 项目主页 更新于 3年前

markdown-it-katex

将数学添加到您的 Markdown

KaTeX 是 MathJax 的更快替代品。 这个插件可以很容易地支持你的降价。

需要说服力吗?

Usage

安装 markdown-it

npm install markdown-it

安装插件

npm install @abreto/markdown-it-katex

在你的 javascript 中使用它 在你

var md = require('markdown-it')(),
    mk = require('@abreto/markdown-it-katex');

md.use(mk);

// double backslash is required for javascript strings, but not html input
var result = md.render('# Math Rulez! \n  $\\sqrt{3x-1}+(1+x)^2$');

的 html 中包含 KaTeX 样式表:

<link rel="stylesheet" href="https://cdn.bootcss.com/KaTeX/0.11.1/katex.min.css">

如果你使用默认的 markdown-it 解析器,我还推荐 github stylesheet:

<link rel="stylesheet" href="https://cdn.bootcss.com/github-markdown-css/3.0.1/github-markdown.min.css"/>

KaTeX 选项可以与第二个参数一起使用。

md.use(mk, {"throwOnError" : false, "errorColor" : " #cc0000"});

Examples

Inline

在每一侧用一个 $ 包围你的 LaTeX 以进行内联渲染。

$\sqrt{3x-1}+(1+x)^2$

Block

使用两个 ($$) 进行块渲染。 此模式使用更大的符号和中心 结果。

$$\begin{array}{c}

\nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} &
= \frac{4\pi}{c}\vec{\mathbf{j}}    \nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\

\nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\

\nabla \cdot \vec{\mathbf{B}} & = 0

\end{array}$$

Syntax

Markdown 中的数学解析旨在与 pandoc 设定的约定一致:

Anything between two $ characters will be treated as TeX math. The opening $ must
have a non-space character immediately to its right, while the closing $ must
have a non-space character immediately to its left, and must not be followed
immediately by a digit. Thus, $20,000 and $30,000 won’t parse as math. If for some
reason you need to enclose text in literal $ characters, backslash-escape them and
they won’t be treated as math delimiters.

Math Syntax Support

KaTeX 基于 TeX 和 LaTeX。 对两者的支持都在增加。 这是一个列表 当前支持的函数:

KaTeX 中的函数支持

markdown-it-katex

Add Math to your Markdown

KaTeX is a faster alternative to MathJax. This plugin makes it easy to support in your markdown.

Need convincing?

Usage

Install markdown-it

npm install markdown-it

Install the plugin

npm install @abreto/markdown-it-katex

Use it in your javascript

var md = require('markdown-it')(),
    mk = require('@abreto/markdown-it-katex');

md.use(mk);

// double backslash is required for javascript strings, but not html input
var result = md.render('# Math Rulez! \n  $\\sqrt{3x-1}+(1+x)^2$');

Include the KaTeX stylesheet in your html:

<link rel="stylesheet" href="https://cdn.bootcss.com/KaTeX/0.11.1/katex.min.css">

If you're using the default markdown-it parser, I also recommend the github stylesheet:

<link rel="stylesheet" href="https://cdn.bootcss.com/github-markdown-css/3.0.1/github-markdown.min.css"/>

KaTeX options can be supplied with the second argument to use.

md.use(mk, {"throwOnError" : false, "errorColor" : " #cc0000"});

Examples

Inline

Surround your LaTeX with a single $ on each side for inline rendering.

$\sqrt{3x-1}+(1+x)^2$

Block

Use two ($$) for block rendering. This mode uses bigger symbols and centers the result.

$$\begin{array}{c}

\nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} &
= \frac{4\pi}{c}\vec{\mathbf{j}}    \nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\

\nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\

\nabla \cdot \vec{\mathbf{B}} & = 0

\end{array}$$

Syntax

Math parsing in markdown is designed to agree with the conventions set by pandoc:

Anything between two $ characters will be treated as TeX math. The opening $ must
have a non-space character immediately to its right, while the closing $ must
have a non-space character immediately to its left, and must not be followed
immediately by a digit. Thus, $20,000 and $30,000 won’t parse as math. If for some
reason you need to enclose text in literal $ characters, backslash-escape them and
they won’t be treated as math delimiters.

Math Syntax Support

KaTeX is based on TeX and LaTeX. Support for both is growing. Here's a list of currently supported functions:

Function Support in KaTeX

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文