[](https://www.npmjs.com/package/@webc.site/math)
[](https://x.com/iwebcsite)
[](https://webc-site.bsky.social)
# @webc.site/math : 全球最小最快的网页 Markdown 公式渲染器
## 1. 功能介绍
本项目将 LaTeX 数学公式编译为浏览器原生支持的 MathML Core 标记。通过编译期转换,无需客户端排版引擎,实现零运行时开销的公式渲染。
核心特性:
- **高性能**:TeX 公式直接转换为原生 MathML 标签,处理速度达每秒 300,000 次以上
- **轻量化**:核心包体积 8.95 KB(Gzip 压缩后 4.62 KB),无外部依赖
- **零运行开销**:完全依赖浏览器原生引擎排版与渲染
- **高容错性**:自动捕获语法错误,降级输出原始 TeX 字符串
- **强兼容性**:生成标准 MathML 标签,适配 SSR、SSG 和 CSR
## 2. 使用演示
### 直接渲染 TeX 公式
```javascript
import mathml from "@webc.site/math";
// 第二参数为 true 表示渲染为块级公式
const html = mathml("e^{i\\pi} + 1 = 0", true);
```
### 替换 Markdown 文本中的公式
```javascript
import mdMath from "@webc.site/math/md.js";
import compile from "@webc.site/math";
const html = mdMath("欧拉恒等式:$$e^{i\\pi} + 1 = 0$$", compile);
```
### 字体与 CSS 配置
浏览器排版 MathML 依赖包含数学排版度量(OpenType Math 表)的数学字体,用于呈现根号伸缩、大括号拉伸、分式厚度与上下标对齐。
推荐使用 TeX 经典数学字体 **Latin Modern Math**(源自 Computer Modern 字体家族)。
#### 引入数学字体
##### 方式一:CDN 在线引用(推荐)
通过 `18s` 字体包在线引入 Latin Modern Math 样式(该样式将 Latin Modern Math 声明为字体族 `m`):
在 CSS 中引入:
```css
@import url("https://registry.npmmirror.com/18s/0.2.24/files/m.css");
```
或在 HTML `
` 中引入:
```html
```
若同时需要页面正文字体(`t`)与代码等宽字体(`c`),可直接引用完整样式表:
```html
```
##### 方式二:本地托管字体文件
下载 Latin Modern Math 字体文件(WOFF2 格式),通过 `@font-face` 声明:
```css
@font-face {
font-family: "Latin Modern Math";
src: url("/fonts/latinmodern-math.woff2") format("woff2");
font-style: normal;
font-display: swap;
}
```
#### CSS 样式配置
##### 字体族声明与回退链
为 `