mcpField
S
slidev-latex

slidev-latex

github.com/yoanbernabeu文档处理41218 次安装39 周前更新
代码仓库

关于 slidev-latex

面向 Slidev 的 LaTeX 和 KaTeX 数学与科学符号编写技能,为学术、科学和技术演示文稿作者提供语法示例与指导。

主要功能

  1. 01行内与块级公式
  2. 02方程、矩阵与符号
  3. 03公式行高亮与颜色样式
  4. 04通过 mhchem 编写化学式
  5. 05数学内容可读性指导

适用场景

  1. 01在学术幻灯片中加入方程
  2. 02展示物理或工程符号
  3. 03呈现对齐推导与方程组

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/yoanbernabeu/slidev-skills --skill slidev-latex

安装后在 Claude Code 中输入/slidev-latex确认已加载。

详细介绍

Slidev 中的 LaTeX/KaTeX

本技能介绍如何在 Slidev 中使用 KaTeX(一个快速的 LaTeX 数学公式渲染库)渲染数学公式和科学记号。

何时使用本技能

  • 编写数学方程
  • 制作科学演示文稿
  • 编写技术文档
  • 撰写学术内容
  • 编写物理、化学或工程公式

基本语法

行内数学公式

用单个 $ 包围:

The quadratic formula is $x = \frac{-b \pm \sqrt{b^2-4ac}}{2a}$ where $a \neq 0$.
块级数学公式

用两个 $$ 包围:

$$
E = mc^2
$$

或使用换行:

$$
\begin{aligned}
f(x) &= x^2 + 2x + 1 \\
     &= (x + 1)^2
\end{aligned}
$$

常见数学记号

分数
Simple: $\frac{a}{b}$

Nested: $\frac{1}{\frac{a}{b} + c}$

Display: $$\frac{x^2 + y^2}{z}$$
指数和下标
Exponent: $x^2$, $e^{i\pi}$
Subscript: $x_1$, $a_{ij}$
Combined: $x_1^2$, $a_{ij}^{n+1}$
平方根
Simple: $\sqrt{x}$
N-th root: $\sqrt[n]{x}$
Complex: $\sqrt{x^2 + y^2}$
希腊字母
Lowercase: $\alpha$, $\beta$, $\gamma$, $\delta$, $\epsilon$, $\theta$, $\lambda$, $\mu$, $\pi$, $\sigma$, $\omega$

Uppercase: $\Gamma$, $\Delta$, $\Theta$, $\Lambda$, $\Pi$, $\Sigma$, $\Omega$
运算符
Sum: $\sum_{i=1}^{n} x_i$

Product: $\prod_{i=1}^{n} x_i$

Integral: $\int_{a}^{b} f(x) \, dx$

Double integral: $\iint_D f(x,y) \, dA$

Limit: $\lim_{x \to \infty} f(x)$

方程和公式

著名方程
$$
\text{Euler's Identity: } e^{i\pi} + 1 = 0
$$

$$
\text{Pythagorean: } a^2 + b^2 = c^2
$$

$$
\text{Einstein: } E = mc^2
$$

$$
\text{Schrödinger: } i\hbar\frac{\partial}{\partial t}\Psi = \hat{H}\Psi
$$
方程组
$$
\begin{cases}
x + y = 10 \\
2x - y = 5
\end{cases}
$$
对齐的方程
$$
\begin{aligned}
(x + y)^2 &= x^2 + 2xy + y^2 \\
(x - y)^2 &= x^2 - 2xy + y^2 \\
(x + y)(x - y) &= x^2 - y^2
\end{aligned}
$$
矩阵
$$
A = \begin{pmatrix}
a & b \\
c & d
\end{pmatrix}
$$

$$
B = \begin{bmatrix}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\end{bmatrix}
$$

$$
\det(A) = \begin{vmatrix}
a & b \\
c & d
\end{vmatrix} = ad - bc
$$

数学符号

比较
$<$, $>$, $\leq$, $\geq$, $\neq$, $\approx$, $\equiv$, $\sim$
逻辑
$\forall$ (for all), $\exists$ (exists), $\neg$ (not)
$\land$ (and), $\lor$ (or), $\Rightarrow$ (implies)
集合
$\in$ (in), $\notin$ (not in), $\subset$, $\subseteq$
$\cup$ (union), $\cap$ (intersection)
$\emptyset$ (empty set), $\mathbb{R}$ (reals), $\mathbb{N}$ (naturals)
微积分
$\frac{dy}{dx}$, $\frac{\partial f}{\partial x}$
$\nabla f$, $\nabla \cdot F$, $\nabla \times F$
箭头
$\rightarrow$, $\leftarrow$, $\leftrightarrow$
$\Rightarrow$, $\Leftarrow$, $\Leftrightarrow$
$\mapsto$, $\to$

高级功能

数学公式中的行高亮
$$ {1|3|all}
\begin{aligned}
f(x) &= x^2 \\      // Click 1
f'(x) &= 2x \\      // Click 1
f''(x) &= 2         // Click 2
\end{aligned}
$$
自定义颜色
$$
{\color{red} x^2} + {\color{blue} y^2} = {\color{green} z^2}
$$
带框公式
$$
\boxed{E = mc^2}
$$
数学公式中的文本
$$
f(x) = \begin{cases}
x & \text{if } x \geq 0 \\
-x & \text{otherwise}
\end{cases}
$$

化学公式

在 vite.config.ts 中启用 mhchem 扩展:

import 'katex/contrib/mhchem'
export default {}

然后使用:

$$\ce{H2O}$$

$$\ce{2H2 + O2 -> 2H2O}$$

$$\ce{CO2 + C -> 2CO}$$

$$\ce{Hg^2+ ->[I-] HgI2 ->[I-] [Hg^{II}I4]^2-}$$

物理公式

力学
$$
F = ma \quad \text{(Newton's Second Law)}
$$

$$
W = \int_a^b \vec{F} \cdot d\vec{s}
$$

$$
KE = \frac{1}{2}mv^2
$$
电磁学
$$
\vec{F} = q(\vec{E} + \vec{v} \times \vec{B})
$$

$$
\nabla \cdot \vec{E} = \frac{\rho}{\epsilon_0}
$$
量子力学
$$
\hat{p} = -i\hbar\frac{\partial}{\partial x}
$$

$$
\Delta x \cdot \Delta p \geq \frac{\hbar}{2}
$$

统计学

$$
\bar{x} = \frac{1}{n}\sum_{i=1}^{n} x_i
$$

$$
\sigma^2 = \frac{1}{n}\sum_{i=1}^{n}(x_i - \bar{x})^2
$$

$$
P(A|B) = \frac{P(B|A) \cdot P(A)}{P(B)}
$$

机器学习

$$
J(\theta) = -\frac{1}{m}\sum_{i=1}^{m}[y^{(i)}\log(h_\theta(x^{(i)})) + (1-y^{(i)})\log(1-h_\theta(x^{(i)}))]
$$

$$
\text{softmax}(z_i) = \frac{e^{z_i}}{\sum_{j=1}^{K} e^{z_j}}
$$

$$
\nabla_\theta J(\theta) = \frac{1}{m} X^T (h_\theta(X) - y)
$$

最佳实践

1. 保持公式清晰易读

❌ 过于密集

$\frac{\frac{a}{b}+\frac{c}{d}}{\frac{e}{f}-\frac{g}{h}}$

✓ 分步书写

Let $x = \frac{a}{b} + \frac{c}{d}$ and $y = \frac{e}{f} - \frac{g}{h}$

Then the result is $\frac{x}{y}$
2. 复杂公式使用展示模式

❌ 复杂的行内公式

The integral $\int_{-\infty}^{\infty} e^{-x^2} dx = \sqrt{\pi}$ is fundamental.

✓ 展示模式

The Gaussian integral:
$$\int_{-\infty}^{\infty} e^{-x^2} dx = \sqrt{\pi}$$
3. 添加上下文
**Theorem**: For any triangle with sides $a$, $b$, $c$:

$$a^2 + b^2 = c^2$$

*where $c$ is the hypotenuse.*
4. 多步推导使用对齐环境
$$
\begin{aligned}
\text{Given: } & x + y = 5 \\
\text{And: } & x - y = 1 \\
\text{Adding: } & 2x = 6 \\
\text{Therefore: } & x = 3, y = 2
\end{aligned}
$$

常见错误

❌ 特殊字符未转义

$50%$ (error!)

✓ 正确转义

$50\%$

❌ 多字符未使用花括号

$x^10$ (shows x¹0)

✓ 使用花括号

$x^{10}$

输出格式

创建数学内容时:

# [Topic Title]

[Brief explanation of the concept]

**Definition/Formula:**
$$
[Main formula in display mode]
$$

**Where:**
- $[variable]$ = [meaning]
- $[variable]$ = [meaning]

**Example:**
[Step-by-step solution]

$$
\begin{aligned}
\text{Step 1: } & [equation] \\
\text{Step 2: } & [equation] \\
\text{Result: } & [final answer]
\end{aligned}
$$

评论

支持 Markdown