Skip to content
Souloss
Go back

如何在 Astro 博客文章中添加 LaTeX 公式

学习如何在 Astro 博客文章中使用 Markdown、KaTeX 和 remark/rehype 插件添加 LaTeX 公式。

教程/前端 |
更新于:
| 1 分钟阅读 | 271 字

本文档演示如何在 astro-minimax 的 Markdown 文件中使用 LaTeX 公式。LaTeX 是一种强大的排版系统,常用于数学和科学文档。

Free Close-up of complex equations on a chalkboard, showcasing chemistry and math symbols. Stock Photo
Photo by Vitaly Gariev

Table of contents

Open Table of contents

说明

在本节中,你将找到如何在 astro-minimax 的 Markdown 文件中添加 LaTeX 支持的说明。

  1. 运行以下命令安装必要的 remark 和 rehype 插件:

    pnpm install rehype-katex remark-math katex
    bash
  2. 更新 Astro 配置以使用这些插件:

  3. 在主布局文件中导入 KaTeX CSS

  4. 最后一步,在 typography.css 中为 katex 添加文本颜色。

    @plugin "@tailwindcss/typography";
    
    @layer base {
      /* other classes */
    
      /* Katex text color */
      .prose .katex-display {
        @apply text-foreground;
      }
    
      /* ===== Code Blocks & Syntax Highlighting ===== */
      /* other classes */
    }
    css

完成!这个设置允许你在 Markdown 文件中编写 LaTeX 公式,在网站构建时会正确渲染。完成这些步骤后,本文档的其余部分将正确显示。


行内公式

行内公式写在单美元符号 $...$ 之间。以下是一些示例:

  1. 著名的质能方程:$E = mc^2$
  2. 二次方程求根公式:$x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$
  3. 欧拉恒等式:$e^{i\pi} + 1 = 0$

块级公式

对于更复杂的公式,或者当你想让公式单独显示在一行时,使用双美元符号 $$...$$

高斯积分:

$$ \int_{-\infty}^{\infty} e^{-x^2} dx = \sqrt{\pi} $$
bash

黎曼 zeta 函数的定义:

$$ \zeta(s) = \sum_{n=1}^{\infty} \frac{1}{n^s} $$
bash

麦克斯韦方程组的微分形式:

$$
\begin{aligned}
\nabla \cdot \mathbf{E} &= \frac{\rho}{\varepsilon_0} \\
\nabla \cdot \mathbf{B} &= 0 \\
\nabla \times \mathbf{E} &= -\frac{\partial \mathbf{B}}{\partial t} \\
\nabla \times \mathbf{B} &= \mu_0\left(\mathbf{J} + \varepsilon_0 \frac{\partial \mathbf{E}}{\partial t}\right)
\end{aligned}
$$
bash

使用数学符号

LaTeX 提供了丰富的数学符号:



上一篇
2026 年个人技术博客系统架构
下一篇
在 astro-minimax 中动态生成 OG 图片

相关推荐

评论区

文明评论,共建和谐社区