---
title: 数学与 LaTeX
description: 使用 KaTeX 和 LaTeX 语法渲染数学表达式与公式。需要在 docs.json 中将 styling.latex 设置为 true。
---

> **For AI agents:** the complete documentation index is at [llms.txt](/docs/llms.txt). Append `.md` to any page URL for its markdown version.

Jamdesk 支持使用 [KaTeX](https://katex.org/) 渲染数学表达式和公式。

<Note>
数学支持需要在 `docs.json` 文件中设置 `styling.latex: true`。
</Note>

## 启用数学支持

在你的 `docs.json` 中添加：

```json docs.json
{
  "styling": {
    "latex": true
  }
}
```

## 行内数学

使用单个美元符号表示行内公式：

```md
The Pythagorean theorem states that $a^2 + b^2 = c^2$.
```

**渲染结果：** 勾股定理表明 $a^2 + b^2 = c^2$。

## 块级公式

使用两个美元符号表示居中的显示样式公式：

```md
$$
E = mc^2
$$
```

**渲染结果：**

$$
E = mc^2
$$

## 复杂公式

KaTeX 支持广泛的 LaTeX 表示法：

### 分数

```md
$$
\frac{n!}{k!(n-k)!} = \binom{n}{k}
$$
```

### 积分

```md
$$
\int_0^\infty e^{-x^2} dx = \frac{\sqrt{\pi}}{2}
$$
```

### 求和

```md
$$
\sum_{i=1}^{n} i = \frac{n(n+1)}{2}
$$
```

### 矩阵

```md
$$
\begin{bmatrix}
a & b \\
c & d
\end{bmatrix}
$$
```

## 使用 Latex 组件

如需更多控制，可直接使用 `<Latex>` 组件：

```mdx
<Latex>E = mc^2</Latex>
```

对于块级或显示模式：

```mdx
<Latex block>
\int_0^\infty e^{-x^2} dx = \frac{\sqrt{\pi}}{2}
</Latex>
```

## 代码块受保护

代码块中的美元符号**不会**被视为数学公式：

```bash
echo $HOME
export PRICE=$100
```

这会渲染为普通代码，而不是公式。

## 支持的命令

KaTeX 支持大多数常见的 LaTeX 数学命令。有关支持的函数的完整列表，请参阅 [KaTeX 文档](https://katex.org/docs/supported.html)。

<Tip>
对于复杂公式，请先在 [KaTeX 演示页面](https://katex.org/)上测试 LaTeX。
</Tip>

## 下一步是什么？

<Columns cols={2}>
  <Card title="SEO 优化" icon="magnifying-glass-chart" href="/cn/content/seo">
    提升内容的可发现性并优化元数据
  </Card>
  <Card title="Frontmatter" icon="file-lines" href="/cn/content/frontmatter">
    定义标题、描述和 SEO 字段
  </Card>
</Columns>