Skip to content
Dust In The Wind
Go back

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

更新于:
Edit page

本文演示如何在 AstroPaper 的 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

配置说明

本节介绍如何为 AstroPaper 的 Markdown 文件添加 LaTeX 支持。

  1. 安装必要的 remark 和 rehype 插件:

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

    import remarkMath from "remark-math";
    import rehypeKatex from "rehype-katex";
    
    export default defineConfig({
      markdown: {
        remarkPlugins: [
          remarkMath,
          remarkToc,
          [remarkCollapse, { test: "Table of contents" }],
        ],
        rehypePlugins: [rehypeKatex],
        shikiConfig: {
          themes: { light: "min-light", dark: "night-owl" },
          wrap: false,
        },
      },
    });astro.config.ts
  3. 在主布局文件中导入 KaTeX CSS:

    ---
    import { SITE } from "@config";
    ---
    
    <!doctype html>
    <meta property="og:image" content={socialImageURL} />
    
    <link
      rel="stylesheet"
      href="https://cdn.jsdelivr.net/npm/katex@0.15.2/dist/katex.min.css"
    />
    
    <body>
      <slot />
    </body>src/layouts/Layout.astro
  4. 最后,在 typography.css 中为 katex 添加文字颜色:

    @plugin "@tailwindcss/typography";
    
    @layer base {
      /* Katex 文字颜色 */
      .prose .katex-display {
        @apply text-foreground;
      }
    
      /* ===== 代码块与语法高亮 ===== */
    }src/styles/typography.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} $$

黎曼 ζ 函数定义:

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

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

$$
\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}
$$

数学符号

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

  • 希腊字母:$\alpha$$\beta$$\gamma$$\delta$$\epsilon$$\pi$
  • 运算符:$\sum$$\prod$$\int$$\partial$$\nabla$
  • 关系符:$\leq$$\geq$$\approx$$\sim$$\propto$
  • 逻辑符号:$\forall$$\exists$$\neg$$\wedge$$\vee$

Edit page