Skip to content

在 AstroPaper 部落格文章中添加 LaTeX 方程式

Published: at 08:58 PM

本文檔演示了如何在 AstroPaper 的 Markdown 文件中使用 LaTeX 方程式。LaTeX 是一個強大的排版系統,通常用於數學和科學文檔。

目錄

指示

在本節中,您將找到如何在 AstroPaper 的 Markdown 文件中添加 LaTeX 支持的說明。

  1. 通過運行 npm install rehype-katex remark-math katex 安裝必要的 remark 和 rehype 插件。

  2. 更新 Astro 配置 (astro.config.ts) 以使用這些插件:

// 其他導入
import remarkMath from "remark-math";
import rehypeKatex from "rehype-katex";

export default defineConfig({
  // 其他配置
  markdown: {
    remarkPlugins: [
      remarkMath,
      remarkToc,
      [
        remarkCollapse,
        {
          test: "目錄",
        },
      ],
    ],
    rehypePlugins: [rehypeKatex],
    // 其他 markdown 配置
  },
  // 其他配置
});
  1. 在主佈局文件 src/layouts/Layout.astro 中導入 KaTeX CSS
---
import { LOCALE, SITE } from "@config";

// astro 代碼
---

<!doctype html>
<!-- 其他... -->
<script is:inline src="/toggle-theme.js"></script>
<link
  rel="stylesheet"
  href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css"
/>

<body>
  <slot />
</body>

這樣設置後,您可以在 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 提供了廣泛的數學符號:


Previous Post
如何將 Giscus 評論整合到 AstroPaper
Next Post
如何使用 Git Hooks 設定創建和修改日期