本文档演示如何在 AstroPaper 的 Markdown 文件中使用 LaTeX 公式。LaTeX 是一种功能强大的排版系统,常用于数学和科学文档。
目录
指示
在本节中,您将找到有关如何在 AstroPaper 的 Markdown 文件中添加 LaTeX 支持的说明。
1.运行以下命令安装必要的 remark 和 rehype 插件:
pnpm install rehype-katex remark-math katex
-
更新 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: { // For more themes, visit https://shiki.style/themes themes: { light: "min-light", dark: "night-owl" }, wrap: false, }, }, // ... });astro.config.ts
3.在主布局文件中导入 KaTeX CSS
---
import { SITE } from "@config";
// astro code
---
<!doctype html>
<!-- others... -->
<script is:inline src="/toggle-theme.js"></script>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/katex@0.15.2/dist/katex.min.css"
/>
<body>
<slot />
</body>src/layouts/Layout.astro
-
最后一步,在
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 */ }src/styles/typography.css
瞧!这样设置后,你就可以在 Markdown 文件中编写 LaTeX 公式了,网站构建时这些公式就能正确渲染。完成设置后,文档的其余部分也会正确显示。
内联方程
行内公式写在单个美元符号 $...$ 之间。以下是一些示例:
- 著名的质能等价公式:
$E = mc^2$ - 二次方程求根公式:
$x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$ - 欧拉恒等式:
$e^{i\pi} + 1 = 0$
块方程
对于更复杂的公式,或者当您希望公式单独显示一行时,请使用双美元符号 $$...$$:
高斯积分:
$$ \int_{-\infty}^{\infty} e^{-x^2} dx = \sqrt{\pi} $$
The definition of the Riemann zeta function:
$$ \zeta(s) = \sum_{n=1}^{\infty} \frac{1}{n^s} $$
Maxwell’s equations in differential form:
$$
\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 提供了种类繁多的数学符号:
- Greek letters希腊字母:
$\alpha$,$\beta$,$\gamma$,$\delta$,$\epsilon$,$\pi$ - Operators操作员:
$\sum$,$\prod$,$\int$,$\partial$,$\nabla$ - Relations关系:
$\leq$,$\geq$,$\approx$,$\sim$,$\propto$ - Logical symbols逻辑符号:
$\forall$,$\exists$,$\neg$,$\wedge$,$\vee$