Skip to main content

LaTeX to SVG/MathML converter using Python + mini-racer + MathJax 3.2.2

Project description

siwei-pymath

LaTeX to SVG/MathML converter powered by Python + mini-racer + MathJax 3.2.2.

安装

pip install siwei-pymath

快速使用

from latex.converter import latex_to_svg, latex_to_mathml

# LaTeX → SVG
svg = latex_to_svg(r"E = mc^2")
print(svg)

# LaTeX → MathML
mathml = latex_to_mathml(r"\int_a^b f(x)\,dx", display=True)
print(mathml)

环境要求

  • Python >= 3.8
  • mini-racer(pip 安装时会自动安装)

开发指南

项目结构

siwei-pymath/
├── latex/
│   ├── __init__.py
│   ├── converter.py          ← Python 核心代码
│   └── build/
│       ├── mathjax_entry.js  ← JS 入口(MathJax 配置)
│       └── mathjax_bundle.js ← JS 构建产物(2.5MB,由 esbuild 打包)
├── node_modules/             ← JS 依赖(本地构建用,不参与打包)
├── .gitignore
├── LICENSE
├── README.md
├── pyproject.toml            ← Python 打包配置
├── package.json              ← Node.js 构建配置
├── requirements.txt
└── test_converter.py

本地开发流程

  1. 克隆仓库并安装依赖
git clone <仓库地址>
cd siwei-pymath

# Python 依赖
pip install -r requirements.txt

# Node.js 依赖(用于构建 MathJax bundle)
npm install
  1. 修改 Python 代码 — 编辑 latex/converter.pylatex/__init__.py

  2. 修改 MathJax 配置 — 编辑 latex/build/mathjax_entry.js,然后重新构建 bundle:

npx esbuild latex/build/mathjax_entry.js --bundle --outfile=latex/build/mathjax_bundle.js --platform=node --target=es2017
  1. 本地测试
python test_converter.py
# 或在 Python 中交互测试
python -c "from latex.converter import latex_to_svg; print(latex_to_svg('E=mc^2')[:200])"

打包与发布(上传到 PyPI)

准备工作

  1. 注册 PyPI 账号

  2. 安装打包工具

pip install build twine

构建分发包

# 确保 JS bundle 已构建
npx esbuild latex/build/mathjax_entry.js --bundle --outfile=latex/build/mathjax_bundle.js --platform=node --target=es2017

# 构建 sdist + wheel
python -m build

构建成功后,dist/ 目录下会生成:

  • siwei_pymath-1.0.0.tar.gz(源码包)
  • siwei_pymath-1.0.0-py3-none-any.whl(wheel 包)

验证一下:检查 wheel 中是否包含了 JS bundle:

python -c "import zipfile; z=zipfile.ZipFile('dist/siwei_pymath-1.0.0-py3-none-any.whl'); print([n for n in z.namelist() if 'mathjax' in n])"

上传到 TestPyPI(推荐先测试)

twine upload --repository-url https://test.pypi.org/legacy/ dist/*

从 TestPyPI 安装验证

pip install --index-url https://test.pypi.org/simple/ siwei-pymath
python -c "from latex.converter import latex_to_svg; print(latex_to_svg('E=mc^2')[:80])"

上传到正式 PyPI

确认测试无误后:

twine upload dist/*

版本更新

修改代码后,按以下步骤更新版本:

  1. 更新版本号 — 修改 pyproject.toml 中的 version
  2. 重新构建
# 清理旧的构建产物
rm -rf dist *.egg-info

# 重新构建
python -m build

# 上传
twine upload dist/*

注意:每次上传到 PyPI 时版本号必须递增,不能重复使用已有版本号。

常见问题

问题 解决
twine upload 要求输入用户名密码 使用 API token:用户名填 __token__,密码填 token 值
mathjax_bundle.js 没被打包进 wheel 确认 pyproject.toml 中有 [tool.setuptools.package-data] 配置
上传后 pip 安装报 mini-racer 错误 确保系统已安装 C 编译工具链,或使用预编译 wheel

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

siwei_pymath-1.0.0.tar.gz (681.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

siwei_pymath-1.0.0-py3-none-any.whl (684.1 kB view details)

Uploaded Python 3

File details

Details for the file siwei_pymath-1.0.0.tar.gz.

File metadata

  • Download URL: siwei_pymath-1.0.0.tar.gz
  • Upload date:
  • Size: 681.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.21

File hashes

Hashes for siwei_pymath-1.0.0.tar.gz
Algorithm Hash digest
SHA256 49da11b1deb51afdcc5c844d685016261b2320e78fff9102390ef9467be86f41
MD5 9c40af1d439a16e296ebc7d61b6ee526
BLAKE2b-256 a814c458d11fe59ca8eb7414e7bff52b617ffc26e25614099ca2dd81a61bdc89

See more details on using hashes here.

File details

Details for the file siwei_pymath-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: siwei_pymath-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 684.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.21

File hashes

Hashes for siwei_pymath-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 89cacc94ffe4db791918935c485c295b9ccde72f00c1b7842e6ae35335400805
MD5 5baf52c7bf014247ebc3f7fbaea75b9a
BLAKE2b-256 07f06063cb218ba4dea8894ef542c3e8f706a232e9676bef25caffb91b81e063

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page