بنقرة واحدة
formula2code
Convert LaTeX math formulas from papers into executable PyTorch/NumPy code.
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
القائمة
Convert LaTeX math formulas from papers into executable PyTorch/NumPy code.
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
استنادا إلى تصنيف SOC المهني
Beamer LaTeX slide workflow: create, compile, review, and polish academic presentations. Use this skill whenever the user works on Beamer .tex slide decks, or asks to create slides, make a presentation, prepare a lecture, build a talk, or generate Beamer slides from a paper. Covers: creation, editing, compilation, proofreading, visual audit, pedagogical review, TikZ diagrams, figure extraction, and comprehensive quality checks. Trigger on: beamer, slides, lecture, presentation, seminar talk, conference talk, defense slides, tikz, compile latex, proofread slides, slide review, 讨论班, 论文讲解. Do NOT trigger on: powerpoint, pptx, PPT, 做PPT — use the powerpoint-slides skill instead.
Compare reproduced results against paper-reported values. Generate Markdown/JSON/Beamer reports.
Deep analysis of ML source code repositories — AST call graphs, training loop dissection, reproducibility scoring.
Generate implementation code scaffolding from paper descriptions when no source code exists.
Automate paper reproduction on remote GPU servers via mcp-ssh, using code-analyzer's reports.
Prepare structured presentation materials from parsed papers for beamer-skill's create workflow.
| name | formula2code |
| description | Convert LaTeX math formulas from papers into executable PyTorch/NumPy code. |
Convert mathematical equations from research papers into runnable code. This skill bridges the gap between paper reading (LaTeX formulas) and implementation (PyTorch/NumPy).
code-writer workflow: auto-convert extracted formulas into loss.py / model.pyLaTeX input
│
▼
Layer 1: ML Pattern Matcher ─── Known formula? ──→ Direct PyTorch API mapping
│ (no match) (softmax, cross_entropy, etc.)
▼
Layer 2: latex2sympy2 ────────── Parse to SymPy symbolic expression
│
▼
Layer 3: Code Generator ─────── sympytorch (nn.Module) / lambdify / pycode
│
▼
Layer 4: Validator ──────────── Cross-check SymPy vs NumPy vs PyTorch
python formula2code/convert.py "\frac{1}{N}\sum_{i=1}^{N}(y_i - \hat{y}_i)^2" --to pytorch -v
python formula2code/convert.py --from-paper workspace/<paper>/paper_content.json --to pytorch -o formulas.json
python formula2code/convert.py --list-patterns
| Category | Patterns |
|---|---|
| Loss | MSE, Cross Entropy, Binary CE, KL Divergence, L1, Huber |
| Activation | Softmax, Sigmoid, ReLU, GELU, Tanh |
| Attention | Scaled Dot-Product Attention |
| Normalization | Layer Norm, Batch Norm |
| Distribution | Gaussian/Normal |
When a LaTeX formula matches a known pattern, the skill returns the exact PyTorch API call instead of generating code from scratch. This is more reliable for standard operations.
For each formula, the converter outputs:
{
"input_latex": "\\frac{1}{N}\\sum...",
"method": "pattern_match | sympy_pipeline | failed",
"outputs": {
"pytorch_functional": "F.mse_loss(predictions, targets)",
"pytorch_class": "nn.MSELoss()",
"pytorch_code": "import torch\ndef formula(N, y, y_hat): ...",
"numpy_code": "import numpy as np\ndef formula(N, y, y_hat): ...",
"python_code": "import math\ndef formula(N, y, y_hat): ..."
},
"validation": {"passed": true, "summary": "5/5 tests passed"}
}
paper-parser outputs JSON with LaTeX formulas
→ formula2code reads them and generates code
code-writer extracts equations from paper
→ calls formula2code to generate loss.py / model.py components
→ inserts generated code into project scaffolding
See formula2code/examples/ for runnable demos:
| Example | What it Shows |
|---|---|
01_basic_usage.py | 3-layer pipeline: pattern match → SymPy → code generation |
02_official_library_usage.py | Direct usage of latex2sympy2, sympytorch, lambdify |
03_trainable_formula.py | Learn formula coefficients via gradient descent |
from converters.ml_patterns import match_ml_pattern
result = match_ml_pattern(r"\text{softmax}(z)")
# → {'name': 'softmax', 'pytorch_functional': 'F.softmax(x, dim=-1)', ...}
from converters.latex_parser import parse_latex
from converters.to_python import sympy_to_python_code
expr, meta = parse_latex(r"x^2 + 2x + 1")
code = sympy_to_python_code(expr)
# → "def formula(x):\n return x**2 + 2*x + 1"
import sympy, torch, sympytorch
x = sympy.symbols('x_name')
cosx = 1.0 * sympy.cos(x)
sinx = 2.0 * sympy.sin(x)
mod = sympytorch.SymPyModule(expressions=[cosx, sinx])
x_ = torch.rand(3)
out = mod(x_name=x_) # shape (3, 2)
# Floats (1.0, 2.0) become trainable nn.Parameters!
from latex2sympy2_extended import latex2sympy
latex2sympy(r"\frac{d}{dx}(x^{2}+x)") # → Derivative(x**2 + x, x)
latex2sympy(r"\sum_{i = 1}^{n} i") # → Sum(i, (i, 1, n))
latex2sympy(r"\int_{a}^{b} \frac{dt}{t}") # → Integral(1/t, (t, a, b))
pip install -r formula2code/requirements.txt
# Core: sympy
# LaTeX parsing: latex2sympy2_extended[antlr4_13_2]
# PyTorch modules: sympytorch
When encountering issues, the agent should fetch the relevant docs:
Problem: latex2sympy fails to parse a formula.
Solution:
latex_parser.py handles common ML notation like \text{}, \hat{})# Fetch latest docs/issues:
fetch_url("https://github.com/huggingface/latex2sympy2_extended")
fetch_url("https://github.com/huggingface/latex2sympy2_extended/issues")
Problem: sympytorch throws NotImplementedError for a SymPy function.
Solution: Use extra_funcs to provide a custom PyTorch implementation.
import sympy, sympytorch
# Check supported operations:
# https://github.com/patrick-kidger/sympytorch/blob/master/sympytorch/sympy_module.py#L12
mod = sympytorch.SymPyModule(
expressions=[expr],
extra_funcs={
sympy.Function('my_custom_op'): lambda x: x.clamp(min=0),
}
)
# Fetch supported ops list:
fetch_url("https://github.com/patrick-kidger/sympytorch/blob/master/sympytorch/sympy_module.py")
Problem: Need to generate code for a specific backend (torch/numpy/scipy).
Solution: Use the modules parameter.
import sympy
f = sympy.lambdify([x], expr, modules='torch') # → torch functions
f = sympy.lambdify([x], expr, modules='numpy') # → numpy functions
f = sympy.lambdify([x], expr, modules='scipy') # → scipy special
# Fetch lambdify docs:
fetch_url("https://docs.sympy.org/latest/modules/utilities/lambdify.html")
When stuck on any issue, the agent should fetch these URLs:
| Topic | URL |
|---|---|
| latex2sympy2 README + examples | https://github.com/huggingface/latex2sympy2_extended |
| latex2sympy2 issues | https://github.com/huggingface/latex2sympy2_extended/issues |
| latex2sympy2 grammar (PS.g4) | https://github.com/huggingface/latex2sympy2_extended/blob/master/src/latex2sympy2_extended/PS.g4 |
| sympytorch README | https://github.com/patrick-kidger/sympytorch |
| sympytorch supported ops | https://github.com/patrick-kidger/sympytorch/blob/master/sympytorch/sympy_module.py |
| sympytorch issues | https://github.com/patrick-kidger/sympytorch/issues |
| SymPy lambdify docs | https://docs.sympy.org/latest/modules/utilities/lambdify.html |
| SymPy pycode docs | https://docs.sympy.org/latest/modules/printing.html |
| SymPy basic operations | https://docs.sympy.org/latest/tutorials/intro-tutorial/basic_operations.html |
latex2sympy2 cannot parse all LaTeX notations (very complex nested expressions may fail)sympytorch only supports a subset of SymPy operations (extend via extra_funcs)code-writer instead