Dev.to KaTeX 与 标准 LaTeX — 什么有效,什么无效
I’m happy to translate the article for you, but I’ll need the full text of the post (the content you’d like translated). Could you please paste the article’s body here? Once I have the text, I’ll provide a Simplified‑Chinese translation while keeping the source link, formatting, markdown, and any code blocks unchanged.
背景:标准 LaTeX 与 Dev.to
| 功能 | 标准 LaTeX | Dev.to (KaTeX) |
|---|---|---|
| 行内数学 | $x^2$ | {% katex inline %} x^2 {% endkatex %} |
| 行间数学 | $$x^2$$ | {% katex %} x^2 {% endkatex %} |
| 多行对齐方程 | \begin{align} … \end{align} | 在 KaTeX 块中使用 aligned(见下文) |
| 分段函数 | \begin{cases} … \end{cases} | 在 KaTeX 中可用 |
| 矩阵 | \begin{pmatrix} … \end{pmatrix}(以及 bmatrix 等) | 所有括号类型均可使用 |
| 自定义布局 | \begin{array} … \end{array} | 可用 |
| 行内矩阵 | \begin{smallmatrix} … \end{smallmatrix} | 可用 |
常用支持的符号
以下所有内容在 KaTeX 块中均可正常使用:
- 希腊字母:
\alpha…\omega,大写同样支持 - 分数:
\frac、\dfrac、\cfrac - 根号:
\sqrt、\sqrt[n] - 重音符号:
\hat、\bar、\tilde、\vec、\dot - 运算符:
\sum、\prod、\int、\lim - 关系符:
\leq、\geq、\neq、\sim、\cong、\equiv - 逻辑符号:
\forall、\exists、\neg、\land、\lor - 集合符号:
\in、\subset、\cup、\cap、\emptyset - 箭头:
\to、\Rightarrow、\iff、\mapsto - 字体:
\mathbb、\mathbf、\mathcal、\mathfrak、\mathrm、\text - 装饰:
\color、\boxed、\cancel、\underbrace、\overbrace
什么不起作用 ❌
1. align 环境
顶层的 align 环境(自动编号)不受支持。
不会工作
{% katex %}
\begin{align}
a &= b + c \\
&= d
\end{align}
{% endkatex %}
改用下面的方式
{% katex %}
\begin{aligned}
a &= b + c \\
&= d
\end{aligned}
{% endkatex %}
aligned 是一个子环境,可以在数学模式内部使用,这正是 KaTeX 所期望的。
2. 自定义宏(\newcommand)
使用 \newcommand 定义快捷方式不受支持。
% 这在 Dev.to 上不起作用
\newcommand{\R}{\mathbb{R}}
\newcommand{\norm}[1]{\left\| #1 \right\|}
解决办法: 每次写完整的命令,或使用编辑器的代码片段 / 查找‑替换功能来加快输入。
3. 标签和引用(\label、\ref、\eqref)
自动编号和交叉引用不可用。
\label{eq:main}
\ref{eq:main}
\eqref{eq:main}
解决办法: 使用 \tag{} 手动编号。
{% katex %}
E = mc^2 \tag{1}
{% endkatex %}
如果插入或删除公式,需要手动重新编号。
4. 定理样式环境
theorem、proof 等环境不会被渲染。
\begin{theorem}
Every finite group of order p is cyclic.
\end{theorem}
\begin{proof}
...
\end{proof}
解决办法: 使用 Markdown 引用块和加粗文本。
定理 1. 每个阶为 p 的有限群都是循环群。
证明
… 证明内容 …
{% katex inline %} \square {% endkatex %}
5. 图形(TikZ、tikzcd 等)
整个 TikZ 系列均不受支持。
\begin{tikzcd}
A \arrow[r] & B
\end{tikzcd}
解决办法: 在外部生成图形并以图片形式嵌入。

常用的外部工具:
- quiver – 可绘制交换图
- tikzjax – 通用 TikZ 渲染(导出为 SVG/PNG)
6. \DeclareMathOperator
使用 \DeclareMathOperator 定义命名运算符不受支持。
\DeclareMathOperator{\Hom}{Hom}
解决办法: 直接使用 \operatorname{}。
{% katex %}
\operatorname{Hom}(A, B)
{% endkatex %}
快速参考:替换
| 您想要的 | 标准 LaTeX | Dev.to (KaTeX) 等价写法 |
|---|---|---|
| 对齐的公式 | \begin{align} … \end{align} | 在 {% katex %} 块内使用 \begin{aligned} |
| 显示公式 | $$ … $$ | {% katex %} … {% endkatex %} |
| 行内公式 | $ … $ | {% katex inline %} … {% endkatex %} |
| 自定义宏 | \newcommand{\R}{\mathbb{R}} | 每次直接写 \mathbb{R} |
| 公式编号 | \label{…} + \ref{…} | 手动 \tag{1} |
| 命名运算符 | \DeclareMathOperator{\Hom}{Hom} | \operatorname{Hom} |
| 定理框 | \begin{theorem} … \end{theorem} | 使用加粗标题的 Markdown 引用块 |
| 图表 | tikz-cd | 外部图片 (![]()) |
Source: …
何时考虑使用完整的 LaTeX 平台
对于大多数技术文章——算法分析、机器学习数学、基本证明——Dev.to 上的 KaTeX 已足够。然而,如果你需要:
- 交换图(范畴论、代数)
- 带自动编号的正式定理/证明环境
- 复杂的 TikZ 绘图(图、自动机)
- 用于长文档的自定义宏
你可能需要一个原生 LaTeX 平台(例如 Folio、Overleaf 等),这些功能在这些平台上可直接使用。
要点: Dev.to 通过 {% katex %} liquid 标签使用 KaTeX,而不是 $…$ 语法。大多数标准数学符号都能正常显示。主要的不足之处在于 align 环境、 自定义宏、 自动编号以及图表支持。了解这些变通办法后,你就能在 Dev.to 上为绝大多数技术帖子编写简洁、可读的数学内容。