在 Astro 框架下 Markdown 中使用 Katex 渲染数学公式

发布日期     浏览量 114514

#开发日志

记录成功使用 remarkMath,rehypeKatex 实现在 Astro 创建的网站上显示数学公式

前言

最近想在博客上开一个数学相关的专题,但是发现 Astro 并不能直接解析含有 LaTeX 表达式的 Markdown 文件。而使用 remarkMath,rehypeKatex 解决了这个问题,特此记录。

实现

1. 安装依赖

为了能够在自己的网站上使用 remarkMath 和 rehypeKatex,我们需要使用 npm 安装它们:

npm install rehype-katex
npm install remark-math

在国内的下载速度有时可能会很慢,可以使用镜像源解决这个问题。

2. 添加配置

接下来需要修改 astro.config.mjs,让 Astro 加载以上两个库作为 Markdown 的解析器:

import { defineConfig } from 'astro/config';
import mdx from '@astrojs/mdx';
import sitemap from '@astrojs/sitemap';

import remarkMath from 'remark-math';    // 新增
import rehypeKatex from 'rehype-katex';  // 新增

export default defineConfig({
	site: 'https://example.com',
	integrations: [mdx(), sitemap()],

	markdown: {
		syntaxHighlight: 'shiki',
		shikiConfig: {
			theme: 'one-light',
		},
		remarkPlugins: [remarkMath],  // 新增
		rehypePlugins: [rehypeKatex]  // 新增
	}
});

3. 加载样式表

在需要使用 LaTeX 的页面加载样式表 https://fastly.jsdelivr.net/npm/[email protected]/dist/katex.css

这一步是非必须的,但是不加载可能会导致一些显示问题。我在配置的时候没有加载 CSS, 发现一个名为 katex-html 的元素没有被正确隐藏, 因此我在页面的<style>块下隐藏掉了这个元素,在使用中没有发现异常:

.katex-html {
  display: none;
}

至此,所有的配置工作就完成了。下面我给出一些示例,来验证其表现效果。

示例一:洛必达法则的证明

引言

洛必达法则(L’Hôpital’s Rule)是求极限的一种方法,它适用于当两个函数的极限同时趋于 0 或无穷大时的情形。本文将给出洛必达法则的数学证明。

洛必达法则的表述

定理:f(x)f(x)g(x)g(x) 在区间 (a,b)(a, b) 内可微,且 g(x)0g'(x) \neq 0 对所有 x(a,b)x \in (a, b) 成立。若

limxcf(x)=0limxcg(x)=0\lim_{x \to c} f(x) = 0 \quad \text{且} \quad \lim_{x \to c} g(x) = 0

或者

limxcf(x)=±limxcg(x)=±,\lim_{x \to c} f(x) = \pm \infty \quad \text{且} \quad \lim_{x \to c} g(x) = \pm \infty,

limxcf(x)g(x)=L,\lim_{x \to c} \frac{f'(x)}{g'(x)} = L,

其中 LL 是有限常数或 L=±L = \pm \infty,那么

limxcf(x)g(x)=L.\lim_{x \to c} \frac{f(x)}{g(x)} = L.

证明

假设 f(c)=g(c)=0f(c) = g(c) = 0f(x)f'(x)g(x)g'(x) 存在并连续,证明过程基于柯西中值定理(Cauchy’s Mean Value Theorem)。

柯西中值定理:f(x)f(x)g(x)g(x) 在闭区间 [a,b][a, b] 上连续,在开区间 (a,b)(a, b) 上可微,且 g(x)0g'(x) \neq 0 对于 x(a,b)x \in (a, b) 成立,则存在 ξ(a,b)\xi \in (a, b) 使得

f(ξ)g(ξ)=f(b)f(a)g(b)g(a).\frac{f'( \xi )}{g'( \xi )} = \frac{f(b) - f(a)}{g(b) - g(a)}.

现在我们开始证明洛必达法则。为简单起见,假设 xc+x \to c^+ 的情形,cc 可以是有限值或无穷大。考虑区间 [x,c][x, c],在这个区间上应用柯西中值定理,对于 xx 足够接近 cc,有

f(x)f(c)g(x)g(c)=f(ξ)g(ξ),\frac{f(x) - f(c)}{g(x) - g(c)} = \frac{f'(\xi)}{g'(\xi)},

其中 ξ(x,c)\xi \in (x, c)。由于 f(c)=0f(c) = 0g(c)=0g(c) = 0,上式化简为

f(x)g(x)=f(ξ)g(ξ).\frac{f(x)}{g(x)} = \frac{f'(\xi)}{g'(\xi)}.

接下来,我们取 xcx \to c,根据假设 limxcf(x)g(x)=L\lim_{x \to c} \frac{f'(x)}{g'(x)} = L,所以

limxcf(x)g(x)=L.\lim_{x \to c} \frac{f(x)}{g(x)} = L.

因此,洛必达法则得证。

总结

本文通过应用柯西中值定理,证明了洛必达法则。当两个函数的极限同时趋于 0 或无穷大时,可以通过其导数的极限来求解函数商的极限。这是一个极其重要的工具,特别是在处理不定形式时。

示例二:各种公式

[a1,1a2,1an,1a1,2a2,2an,2a1,na2,nan,n]=A=KTQ=[k1Tk2TknT][q1q2qn]\left[ \begin{array}{cccc} a_{1,1} & a_{2,1} & \cdots &a_{n,1}\\ a_{1,2} & a_{2,2} & \cdots &a_{n,2}\\ \vdots & \vdots & \ddots & \vdots\\ a_{1,n} & a_{2,n} & \cdots &a_{n,n}\\ \end{array} \right] = A = K^TQ = \left[ \begin{array}{c} k_1^T\\ k_2^T\\ \vdots\\ k_n^T\\ \end{array} \right] \left[ q_1\,q_2\, \cdots \,q_n \right] A=(a11a12a1na21a22a2nam1am2amn)\mathbf{A} = \left( \begin{array}{cccc} a_{11} & a_{12} & \ldots & a_{1n} \\ a_{21} & a_{22} & \ldots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1} & a_{m2} & \ldots & a_{mn} \\ \end{array} \right) span{v}\mathbf{span}\{v\}^\bot α=±x2\alpha = \pm \parallel x \parallel_2 7ux4t39zx5y4\newcommand{\partt}[6] { \frac {\partial^{#2}#1} {{\partial{#3}^{#4}}{\partial{#5}^{#6}}} } {\partt {u} {7} {x} {4} {t} {3}}\\ \\ {\partt {z} {9}{x} {5}{y}{4}} cos2θ+sin2θ=1\cos^{2}\theta + \sin^{2}\theta = 1 cos2θ=sin2θ+cos2θ=12sin2θ=2cos2θ1\cos2\theta=\sin^{2}\theta + \cos^{2}\theta = 1-2\sin^{2}\theta = 2\cos^{2}\theta -1 limnk=1n1k2=π26\lim_{n \to \infty}\sum_{k=1}^{n}\frac{1}{k^{2}}=\frac{\pi^{2}}{6} x20for all xRx^{2}\geq0\qquad \textrm{for all } x \in \mathbb{R} alpha,β,γ,Γ,ξ,Ξ,π,Π,μ,ϕ,Φ,ω,Ωalpha\,, \beta\,, \gamma\,,\Gamma\,,\xi\,,\Xi\,,\pi\,,\Pi\,,\mu\,,\phi\,,\Phi\,,\omega\,,\Omega 1+x2x2+y\sqrt{1+x^2} \qquad \sqrt{x^2+\sqrt{y}} αβγδ~12N\widetilde{\alpha*\beta*\gamma*\delta} \qquad \underbrace{1*2*\cdots*N} ABCD\overrightarrow{ABCD} arccosθ,cos2θ,logy,lim sup(xi)\arccos{\theta},\qquad \cos{2\theta},\qquad \log{y},\qquad \limsup{(x_i)} sinx(x)dx1\int_{-\infty}^{\infty}{\sin^x(x)} dx \ne 1 11+12+13+1N\frac{1}{1}+\frac{1}{2} + \frac{1}{3} + \cdots \frac{1}{N} μ(x,y,z)dxdydz\iiint \mu(x,y,z)\,dx dy dz i=0Nxi\prod_{i=0}^{N}x_{i}

相关链接

这篇文章的公式部分转载自 CSDN,有修改,源站链接:https://blog.csdn.net/eloudy/article/details/135830153

使用 Python requests 获取 GitHub 仓库信息

在 PyQt 中正确删除带有 QThread 的控件