Python API for programmatic generation of SMath Studio worksheet (.sm) files.
Generate parametric engineering calculations, numerical method worksheets, and mathematical documents entirely from Python code — no GUI needed.
pip install -e .from smathpy import Worksheet, TextRegion, MathRegion, var, assign, evaluate
ws = Worksheet(title="My Calculation", author="Engineer")
ws.add(TextRegion.title("Simple Calculation"))
ws.add(TextRegion.section("Input data:"))
ws.add(MathRegion.assignment("L", 3, unit_name="m"))
ws.add(MathRegion.assignment("q", 4, unit_name="kN"))
ws.add(TextRegion.section("Calculation:"))
ws.add(MathRegion(expr=assign("R", var("q") * var("L") / 2)))
ws.add(TextRegion.section("Result:"))
ws.add(MathRegion.evaluation("R", contract_unit="kN"))
ws.save("calculation.sm")The generated .sm file opens directly in SMath Studio.
from smathpy import var, num, assign, func_assign
x = var("x")
expr = x ** 2 + 3 * x - 1 # builds RPN automatically
# Function definitions
f_def = func_assign("f", ["x"], x ** 2)
# Assignments
a = assign("h", (var("b") - var("a")) / var("n"))from smathpy import MathRegion
from smathpy.units import with_unit, value_with_compound_unit, compound_unit, power_unit
# Simple unit: L := 3 * m
MathRegion.assignment("L", 3, unit_name="m")
# Compound unit: q := 4 * kN/m
from smathpy import assign
assign("q", value_with_compound_unit(4, ["kN"], ["m"]))
# Unit as contract_expr (display result in specific unit)
MathRegion(expr=assign("A_s", ...), show_result=True, contract_expr=compound_unit(["mm", "mm"], []))
# Unit raised to a power (more semantic alternative for mm²)
MathRegion(expr=assign("A_s", ...), show_result=True, contract_expr=power_unit("mm", 2))from smathpy.expression import mat, el, det, transpose
# 2×2 matrix
m = mat([[1, 2], [3, 4]])
# Element access
e = el("A", "i", "j")
# Operations
d = det("A")
t = transpose("A")from smathpy.expression import for_range, while_loop, if_, line, range_, sum_
# For loop with range
body = assign("s", var("s") + var("i"))
loop = for_range("i", range_(1, "n"), body)
# While loop
w = while_loop(var("x") > 0, assign("x", var("x") - 1))
# Conditional
c = if_(var("x") > 0, var("a"), var("b"))
# Summation
s = sum_(var("k") ** 2, "k", 1, "n")from smathpy.expression import (
abs_, sin, cos, tan, sqrt, exp, ln, # math
diff, diff_n, integral, # calculus
concat, num2str, # string
mod, sign, eval_, # misc
)| Type | Class | Description |
|---|---|---|
| Text | TextRegion |
Formatted text, multilingual support |
| Math | MathRegion |
Mathematical expressions and calculations |
| Plot | PlotRegion |
2D charts and graphs |
| Picture | PictureRegion |
Embedded images (PNG, JPEG) |
| Area | AreaRegion |
Collapsible sections |
TextRegion.title("Title") # Bold, blue, 12pt
TextRegion.section("Input data:") # Bordered, gray background
TextRegion(texts={"eng": "Hello", "rus": "Привет"}) # Multilingualws.add_spacing(pixels) inserts extra vertical space between regions (useful between calculation blocks):
ws.add(MathRegion(expr=assign("a", ...)))
ws.add_spacing(10) # add 10 px gap
ws.add(MathRegion(expr=assign("b", ...)))
ws.add_spacing(40) # larger visual break before next sectionSee the examples/ directory:
generate_gcd.py— Euclidean GCD algorithm (while/if/line)generate_beam.py— Engineering beam with unitsgenerate_simpson.py— Simpson's rule numerical integrationviga_ha_aci318.py— Reinforced concrete beam design (ACI 318-19), full engineering worksheet
Run any example:
python examples/generate_gcd.pysmathpy/
├── __init__.py # Public API
├── document.py # Worksheet class & XML serialization
├── settings.py # Document settings, metadata, page model
├── constants.py # XML namespace, assemblies, built-in functions
├── expression/
│ ├── builder.py # Expr class with operator overloading
│ ├── elements.py # RPN element types (operand, operator, function)
│ ├── functions.py # Built-in math function wrappers
│ ├── matrix.py # Matrix construction & operations
│ └── control.py # Control structures (for, while, if, line)
├── regions/
│ ├── base.py # Base Region class
│ ├── text_region.py # TextRegion
│ ├── math_region.py # MathRegion
│ ├── plot_region.py # PlotRegion
│ ├── picture_region.py # PictureRegion
│ └── area_region.py # AreaRegion (collapsible sections)
└── units/
└── __init__.py # Unit helpers & common unit constants
Generates SMath Studio v0.98 format (compatible with v0.96+ readers). Output is standard XML with the http://smath.info/schemas/worksheet/1.0 namespace.
MIT