# mathzetta 中文文档

规范地址：https://npmcn.com/package/mathzetta/

版本：1\.0\.0
npm 元数据更新时间：2026-11-04T03:02:31.639Z
中文译文更新时间：2026-11-04T03:35:01.905Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/mathzetta
原文对照：https://npmcn.com/package/mathzetta/?lang=original
项目主页：https://mathzetta.com/
源码仓库：https://github.com/triplelog/mathzetta

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：5.1 MB，160 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=18
- 运行依赖：0 个
- 首次发布：2026-11-04T02:57:21.229Z
- 最新版本发布：2026-11-04T03:02:31.425Z
- 已发布版本数：2（近 12 个月 2 个）

## 中文简介

用标准后缀解析几乎任何形式的数学；添加LaTeX、MathML、语音、JavaScript、Python、SQL、GLSL、文本以及精确或数字计算的输出器。

## 安装

```sh
npm install mathzetta
```

也可以使用：`pnpm add mathzetta`、`yarn add mathzetta`、`bun add mathzetta`

## README

# MathZetta

几乎以任何方式解析数学——纯文本、LaTeX、unicode、 JavaScript, Python（数学，numpy或SymPy）， SQL， GLSL或电子表格公式 **标准的后缀**， 然后添加所需的输出：LaTeX、MathML、英语口语、JavaScript、 Python、SQL、Excel/Sheets、GLSL、纯文本以及精确或数值计算。它读取 它编写的每个代码格式(`test/unit/roundtrip.test.js`)，所以代码可以运行 出去又回来； [mathzetta-convert](https://mathzetta.com/convert/) 是那一页。

```bash
npm install mathzetta
```

```js
import { parse } from 'mathzetta';
import { toLaTeX } from 'mathzetta/latex';
import { toSpeech } from 'mathzetta/speech';
import { calculate } from 'mathzetta/calculate';

parse('3x^2 + \\sin x')        // ['3', 'x', '2', '^', '*', 'x', 'sin', '+']
parse('Math.pow(x, 2) / 2')    // ['x', '2', '^', '2', '/']
parse('x² ≤ √y')               // ['x', '2', '^', 'y', 'sqrt', '≤']
parse('d/dx (x^2 + 1)')        // ['x', '2', '^', '1', '+', 'x', 'der']

toLaTeX('3x^2 + sin(x)')       // '3x^2 + \\sin\\left(x\\right)'
toSpeech('(x+1)^2')            // 'the quantity x plus 1, squared'
calculate('sqrt(8)').text      // '2*sqrt(2)'
parse('3+4={{3+4}}')           // ['3', '4', '+', '7', '='] — once mathzetta/calculate is loaded
```

在CommonJS中，使用相同的入口点 `require`： `require('mathzetta/latex')` 加载核心 和乳胶(`build/cjs/`),而 `require('mathzetta')` 一切都是同时发生的吗 `mathzetta/all`。

没有运行时依赖；ES模块用于浏览器和Node 18+。一个 `<script src="build/mathzetta.min.js">` 标签将所有内容公开为 `window.MathZetta`． 数学 _显示_ 是一个独立的图书馆， [内联数学](https://inlinemath.com/)，它绘制这些树，每个部分都是可寻址的。

## 来自CDN

每个npm版本都是在jsdeliver（和unpkg）上发布的，不需要安装或构建。

```html
<!-- a script tag: everything on window.MathZetta -->
<script src="https://cdn.jsdelivr.net/npm/mathzetta@1/build/mathzetta.min.js"></script>

<!-- a module -->
<script type="module">
  import { parse, toLaTeX } from 'https://cdn.jsdelivr.net/npm/mathzetta@1/build/mathzetta.esm.min.js';
</script>

<!-- an import map: code written for npm (import … from 'mathzetta', 'mathzetta/latex') runs unchanged,
     since the bundle holds every entry point -->
<script type="importmap">
{ "imports": { "mathzetta": "https://cdn.jsdelivr.net/npm/mathzetta@1/build/mathzetta.esm.min.js",
               "mathzetta/latex": "https://cdn.jsdelivr.net/npm/mathzetta@1/build/mathzetta.esm.min.js" } }
</script>
```

比什么都少——选择网站的 [CDN页面](https://mathzetta.com/cdn.html)，其中写道 代码片段和总计大小：

-   **`<script>` 标签有不同的味道** （`tools/flavours.js`； `build/mathzetta.<flavour>.min.js`,每个 设置 `window.MathZetta`）： `core` （解析，树的工具，操作符表；22 KB压缩）； 核心加上一个输出 `latex`， `javascript`， `python`， `sql`， `excel`， `glsl` (这 带来了 `formulas`); `formulas` （与JavaScript， SQL和Excel输出它是）； `display` （LaTeX, MathML, text, speech; 30kb）； `maths` （求值，计算；31 KB）和 `maths-display`， `maths-javascript`， `maths-python`， `maths-sql`， `maths-excel`， `maths-glsl`； `code` （每个代码输出；50 KB）；和 `mathzetta.min.js`，所有内容（65 KB -） 56 KB，带有CDN使用的brotli)。 `npm run cdn` 列出他们的地址。
-   **平原** -没有密码拼写阅读器也一样(`src/parse/clean/code.js`： numpy, SymPy， SQL，电子表格和GLSL拼写输入)，大约5 KB的压缩少，对于页面读取 仅限数学和LaTeX： `mathzetta.plain.min.js` （17 KB）和 `plain-latex`， `plain-display`， `plain-maths`， `plain-maths-display`;作为模块 `mathzetta.plain.esm.min.js` （`import … from 'mathzetta/plain'`； `require('mathzetta/plain')` 负载 `mathzetta.plain.cjs`), `build/esm-plain/`． 的代码 输出保持完整的读取器：输出代码和读取代码属于一起。
-   **ES模块，任意组合：** `build/esm/<entry>.js` 每个入口点有一个文件吗 （`esm/latex.js`， `esm/speech.js`……)，都是分享 `esm/chunks/core.js` （22 KB），因此页面导入 它想要的输出和核心加载一次- LaTeX多2kb，语音4.5 KB, GLSL 9 KB。一个 导入地图(`"mathzetta"` → `esm/core.js`， `"mathzetta/latex"` → `esm/latex.js`)让代码 为NPM编写的，运行不变。 `build/sizes.json` 拥有每个文件的大小； `npm run build` 写它。

`@1` 遵循最新的1.x； `@1.0.0` 固定一个版本，它永远不会改变(永远缓存它，添加 一个 `integrity` 散列, `npm run cdn` 输出带有中版本的散列的代码片段 包中。json, `npm run cdn -- --check` 确认CDN的副本与本地构建相匹配)。 `src/` 也在CDN上(`…/mathzetta@1/src/latex.js`)，用于只需要一个输出而不需要任何输出的页面 别的。发布 `npm publish` （`prepublishOnly` 运行测试和构建)。

## 入口点

核心是解析器；每个输出都是可选的。每个条目接受一个 表达式字符串、标准后缀或树。

| 进口 | 给了 |
| --- | --- |
| `mathzetta` | `parse`， `parseTree`， `parseSteps`， `clean`； `fromPostfix`， `toPostfix`， `walk`， `treeOf`;运算符表(`operator`， `operatorNames`， `needsParens`,……);读树助手输出共享(`needsBrackets`， `isImplicitProduct`， `GREEK`,……) |
| `mathzetta/latex` | `toLaTeX(x, { wrap })` - - - - - - `wrap(node, latex)` 可以标记每个子表达式吗 |
| `mathzetta/mathml` | `toMathML(x)` -每个节点的元素有 `id="mz-<index>"` |
| `mathzetta/text` | `toText(x)` -可读文本： `√(x)`， `x ∈ {1, 2}` |
| `mathzetta/speech` | `toSpeech(x, { words, names, say })` ——像老师一样大声朗读，每一个字都是多变的； `toSpeechSegments(x)` -同样是用节点id标记的片段，用于遵循数学的标题 |
| `mathzetta/javascript` | `toJavaScript(x)` -代码使用 `Math.*` |
| `mathzetta/python` | `toPython(x, { numpy, sympy })` -代码使用 `math.*`， numpy元素，或SymPy(精确； `diff`， `integrate`， `limit`， `summation`); `toPythonFunction(f, { numpy, sympy, errors })` -选中 `def` （对于sympy， d/dx的x是一个Symbol，参数x被替换）(1/0引发，或 `errors: 'nan'`); `fillPythonTemplate(program, formulas)` / `preparePythonTemplate` （每个槽的结果为一个表单）-一个Python程序 `# mathzetta: growth(P, r, K) = …` 槽，包括数据集列上的公式(`keep(data: columns) = HR >= 30`); `mathzetta-python chart.py x='H / AB' --run out.png` 填充和运行一个。 `examples/python/logistic.py` 是模特； [pyslots](https://tabdn.com/pyslots/) 将模板制作成网页（pyoide或Flask），在棒球数据上使用matplotlib图表 |
| `mathzetta/excel` | `toExcel(x, { cells, ranges })` - Excel /谷歌表格公式(`=IF(x>0, SQRT(x), 0)`)，包含电子表格自身的规则；再回头看看(`=…` 读取为电子表格公式)； `forXlsxFile` 增加了 `_xlfn.` 用于.xlsx文件 |
| `mathzetta/sql` | `toSQL(x, { dialect, quoteIdentifiers, stringQuote })` —默认为PostgreSQL，或 `dialect: 'sqlite'` |
| `mathzetta/glsl` | `toGLSL(x, { version })` -一个着色器表达式，浮动贯穿； `GLSL_HELPERS`； `filterShader(spec)` -在图像像素上的整个片段着色器（过滤器格式和WebGL运行器在其上） [qblur](https://qblur.com/)） |
| `mathzetta/evaluate` | `evaluate(x, scope)` -一个双精度值，不运行生成的代码 |
| `mathzetta/calculate` | `calculate(x, scope, { decimal, places })` → `{ value, text, exact }` -尽可能精确(分数，2¹⁰⁰， `2*sqrt(2)`， π，标准角三角，精确对数)，数值导数，定积分，极限和无穷求和(`∑ 1/n²` → `pi^2/6`）. 抛出 `MathError` 当没有答案时， `UnknownError` 当它无法解决的时候。 **打开加载该条目的开关 `{{…}}` 块** 在每个输入中： `{{1/3+1/6}}` → `1/2`， `{{~3 pi}}` → `3.142` |
| `mathzetta/formulas` | `strToFunction(header, formula)`， `createRowEvaluator(js)`， `strToSQL(header, formula, selections, { dialect })` -表列上的公式 |
| `mathzetta/all` | 以上所有，加上 `toUnified(x)` （每一种格式一次）和 `convert(x, format)` |

`parse(text, options)`： `names` 列出多字母变量以保持完整 （`['cost', 'rate']`); `values` 永远不是函数的名称； `blocks` 集如何 `{{…}}` 这个电话是空的。`null` 离开他们作为一个平原 集团), `scope` 给出其变量的值； `keepIntermediateSteps` (或 `parseSteps`)返回每个阶段-清理的文本，标记，原始后缀， 树，后缀，错误。

## 试一试

[mathzetta.com](https://mathzetta.com/) 实时尝试每个输出 [操场上](https://mathzetta.com/demos/) 显示您键入的每个阶段和格式， [运营商](https://mathzetta.com/demos/operations.html) 列出每个人读和写的内容， 这个 [API页面](https://mathzetta.com/api.html) 有每个功能和选项与一个活生生的例子， 这个 [CDN页面](https://mathzetta.com/cdn.html) 为您的输出构建脚本标记或导入映射 挑，然后 [后缀测验](https://mathzetta.com/quiz.html) 教算法。

```bash
npm test                # ~6,400 checks: unit tests and every table in test/cases
npm run build           # the bundles in build/ (what npm and the CDN ship)
npm run coverage        # what the test tables don't cover yet
```

## 后缀格式

[文档/ postfix.md](https://npmcn.com/package/mathzetta/docs/postfix.md) 列出每个标记。简而言之：操作数 操作人员按波兰颠倒顺序操作；固定数操作符是空的 （`x sin`， `a b ^`)，可变值跟随计数(`a b c 3 max`),一元 -是 `neg`． 它是每个兄弟库使用的格式：内联数学 绘制它，计算它，重写它，语音片段被它标记。

## 它是如何工作的

[文档/ how-it-works.md](https://npmcn.com/package/mathzetta/docs/how-it-works.md) 走过管道—— 清理→标记化→解析→树→呈现-并有食谱 添加拼写、操作符和测试，以及已知的解析间隙。

## 布局

```
src/
  index.js          the core: parser, operator table, tree tools
  all.js            everything, plus toUnified — what build/ bundles
  latex.js mathml.js text.js speech.js javascript.js python.js sql.js excel.js glsl.js
                    one entry per outputter (each a few lines over render/)
  evaluate.js       doubles          calculate.js   exact + numeric calculus
  formulas.js       formulas over table columns
  operators.js      the operator table: precedence, arity, ...
  tree.js           tree ⇄ postfix    common.js      how a tree reads when written
  parse/            clean/ → tokenize → parser (Pratt) → build
  render/           tree → string, one file per format
  types.d.ts        every declaration; index.d.ts and <entry>.d.ts pick from it
test/
  run.js            npm test
  unit/             focused tests
  cases/            table tests (TSV); checked by test/checks.js
  known-failures.json   checks that fail today, with reasons
tools/              the build's flavour list, the CDN addresses and sizes, test coverage, mathzetta-python
docs/
```

## 常见问题

### mathzetta 是什么？

用标准后缀解析几乎任何形式的数学；添加LaTeX、MathML、语音、JavaScript、Python、SQL、GLSL、文本以及精确或数字计算的输出器。

### 如何安装 mathzetta？

在项目目录运行 npm install mathzetta，或使用 pnpm add / yarn add / bun add。它还提供命令行工具 mathzetta-python，可以用 npx mathzetta-python 直接运行。

### mathzetta 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### mathzetta 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### mathzetta 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>=18。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### mathzetta 可以商用吗？

mathzetta 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### mathzetta 还在维护吗？

最新版本 1\.0\.0 发布于 2026/11/04（今天）；至今共发布 2 个版本，其中过去 12 个月发布了 2 个；首次发布于 2026/11/04。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

