Text to MathCAD Converter
Convert text content and mathematical formulas into valid MathCAD 15 .xmcd worksheet files. This skill bundles a validated Python generator (xmcd_generator.py, in this skill's directory) that produces files fully compatible with MathCAD 15.0 M050.
When to Use
- User wants to write engineering calculations, formulas, or text into a MathCAD file
- User asks to generate a
.xmcdfile from text content - User needs to convert a design spec, calculation sheet, or formula list into an editable MathCAD worksheet
- User wants a reusable tool for text-to-MathCAD conversion
How to Use
Run the bundled Python script xmcd_generator.py to build .xmcd files programmatically.
Script Location
The bundled xmcd_generator.py, located in this skill's directory (referenced below as <skill-dir>).
Quick Start
import sys
sys.path.insert(0, r"<skill-dir>") # the directory containing xmcd_generator.py
from xmcd_generator import XmcdBuilder
builder = XmcdBuilder("My Worksheet Title")
# Add a heading (bold, colored text)
builder.heading("1. Input Parameters")
# Add a plain text line
builder.text("This is a description line.")
# Add a math definition: x := 5
builder.define("x", builder.real(5))
# Add a math definition with expression: y := x + 3
builder.define("y", builder.plus(builder.id("x"), builder.real(3)))
# Add a math evaluation (show result): y =
builder.eval(builder.id("y"))
# Add a complex formula: z := (x * y) / 2
builder.define("z", builder.div(
builder.parens(builder.mult(builder.id("x"), builder.id("y"))),
builder.real(2)
))
# Save the file
builder.save(r"C:\path\to\output.xmcd")
Running the Built-in Demo
python <skill-dir>/xmcd_generator.py output.xmcd
This generates a sample worksheet (FeSiAl Boost Inductor Design) to verify the generator works.
Available Math Operators
| Method | MathCAD Symbol | XML Element |
|---|---|---|
builder.id(name) |
variable name | ml:id |
builder.real(val) |
numeric literal | ml:real |
builder.pi() |
pi constant | ml:id |
builder.mult(a, b) |
a * b | ml:mult |
builder.div(a, b) |
a / b | ml:div |
builder.plus(a, b) |
a + b | ml:plus |
builder.minus(a, b) |
a - b | ml:minus |
builder.pow(a, b) |
a^b | ml:pow |
builder.sqrt(a) |
sqrt(a) | ml:sqrt |
builder.parens(c) |
(c) | ml:parens |
builder.abs_val(a) |
|a| | ml:absval |
builder.neg(a) |
-a | ml:neg |
builder.sin(a) |
sin(a) | func_call |
builder.cos(a) |
cos(a) | func_call |
builder.tan(a) |
tan(a) | func_call |
builder.exp(a) |
exp(a) | func_call |
builder.ln(a) |
ln(a) | func_call |
builder.log(a) |
log(a) | ml:log |
builder.func_call(name, *args) |
f(a, b) | ml:sequence |
builder.derivative(var, expr) |
d(expr)/d(var) | ml:derivative |
builder.integral(var, low, high, expr) |
integral | ml:defIntegral |
API Reference
Constructor
- XmcdBuilder(title, author="AI Design") - Create a new builder with a worksheet title and author
Text Regions
- builder.text(content, bold=False, color=None, size=10, font="Times New Roman") - Add a plain text line
- builder.heading(content, color="#800000") - Add a bold section heading (Arial 12pt, maroon)
- builder.title(content, color="#800080") - Add a large title (Arial 16pt, purple)
- builder.spacer(points=20) - Add vertical spacing
Math Regions
- builder.define(name, math_xml, width=150, height=30, extra_dy=0) - Add name := expression. Spacing auto-calculated from formula complexity
- builder.eval(math_xml, width=150, height=30, extra_dy=0) - Add expression = (result auto-calculated by MathCAD). Spacing auto-calculated from formula complexity
Serialization
- builder.save(filepath) - Write the .xmcd file, returns dict with path, regions, binary_items, size_chars
Workflow Pattern
When converting user-provided text/formulas into a .xmcd file:
- Parse the input content to identify headings, plain text, variable definitions, and formulas
- Create a
XmcdBuilderwith an appropriate worksheet title - Add regions in reading order, mapping each formula to the correct math operator methods
- Use
builder.define()for assignments andbuilder.eval()to show calculated results - Call
builder.save(filepath)to write the output file - Report the file path back to the user
Handling Complex Formulas
Nest operator methods to build complex expressions. Use builder.parens() to control operator precedence:
# z := (x * y) / 2
builder.define("z", builder.div(
builder.parens(builder.mult(builder.id("x"), builder.id("y"))),
builder.real(2)
))
# For tall fractions, add extra vertical spacing
builder.define("L", builder.div(
builder.mult(builder.id("V_in"), builder.id("D")),
builder.mult(builder.id("f_sw"), builder.id("delta_I"))
), width=300, extra_dy=20)
Auto-Spacing for Complex Formulas
The generator automatically calculates vertical spacing based on formula complexity. You do not need to manually set extra_dy for most formulas — the generator detects:
- Fractions (
ml:div): each fraction level adds ~28pt extra spacing (numerator stacked above denominator) - Nesting depth (
ml:applylevels beyond 2): each extra level adds ~12pt - Powers (
ml:pow): each power adds ~6pt (superscript height) - Square roots (
ml:sqrt): each sqrt adds ~6pt (overbar height)
If you provide an explicit extra_dy value, the generator uses the larger of the auto-calculated or manual value. Region height is also auto-increased for fractions and deep nesting.
# Auto-spacing handles this automatically — no extra_dy needed:
builder.define("Ap", builder.div(
builder.mult(builder.mult(builder.real(2), builder.id("L_self")),
builder.pow(builder.id("I_rated"), builder.real(2))),
builder.mult(builder.mult(builder.mult(b.id("B_max"), b.id("J")), b.id("Ku")),
b.id("k_util"))
), width=420)
# Generator auto-adds ~40pt extra spacing (1 fraction + depth 4 + 1 power)
Critical Format Rules (MathCAD 15 Compatibility)
These rules were validated against MathCAD 15.0 M050 installation. Violating any will cause open failure or Equation Auditor auto-fix prompts.
- Namespace: Must use
worksheet20andmath20(NOT worksheet30/50 or math30/50) - Power operator: Element name is
ml:pow(NOTml:power) - Negation operator: Element name is
ml:neg(NOTml:unary) - Absolute value: Element name is
ml:absval(NOTml:abs) - Trig/log functions:
sin,cos,tan,exp,lnare NOT validml:applyoperator elements. Usefunc_call("sin", a)instead. Onlylogis a valid operator element (ml:log) - Non-empty sequence:
ml:sequencecannot be empty. For zero-argument constants like pi, usebuilder.pi()(returnsml:id), NOTfunc_call("pi")which would produce an empty sequence - Evaluation regions:
eval()must wrap the expression in<ml:eval>with<ml:result><ml:real>0</ml:real></ml:result>for the placeholder result. Do NOT useml:placeholder(causes "unexpected element" error). MathCAD recalculates the real value on open - Operator empty-element form: All operator elements (ml:mult, ml:div, ml:plus, ml:minus, ml:pow, ml:sqrt, ml:neg, ml:absval, etc.) MUST use explicit empty-element form
<ml:op></ml:op>, NOT self-closing<ml:op/>. MathCAD 15's parser can misparse self-closing operator tags and flatten nested expression trees, causing incorrect operator precedence (e.g.,A*(1-|k|)renders asA*1-|k|) - Explicit parentheses for grouping:
mult()anddiv()auto-wrap complex operands (any expression containing nestedml:apply) in<ml:parens>to preserve operator precedence. This is critical because MathCAD 15's parser does not reliably infer grouping fromml:applynesting alone - No style attribute on ml:mult: Do NOT add
style="default"toml:mult. It can cause the parser to misparse the operator and lose operand grouping - Rendering: Every
<math>region MUST have a<rendering item-idref="N">referencing a<binaryContent>PNG item - Binary content: Use 1x1 transparent PNG as placeholder; MathCAD regenerates real rendering on save
- Text regions: Use
<text>with<p style="Normal">structure - Settings block: Must include
presentation,calculation,editor,fileFormat,miscellaneoussections - Editor element: Must contain
protection-level="none"attribute - Worksheet version: Use
version="2.0.2" - File encoding: UTF-8 with XML declaration
<?xml version="1.0" encoding="UTF-8"?> - UUID: Generate fresh
documentIDandversionIDper file
Troubleshooting
- File won't open in MathCAD: Verify namespaces use
worksheet20/math20, not newer versions - Equation Auditor prompts on open: Check that every math region has a
<rendering>element with a validitem-idref - Equations highlighted in pink (wrong precedence): This means MathCAD parsed operator nesting incorrectly. Ensure all operator elements use
<ml:op></ml:op>form (not self-closing<ml:op/>), and complex operands inmult()/div()are wrapped inml:parens. The generator handles this automatically - "ml:placeholder is unexpected element": Use
<ml:result><ml:real>0</ml:real></ml:result>insideml:evalinstead ofml:placeholder - Missing results after eval: This is expected. MathCAD calculates results on first open/recalculate. The generator writes
=evaluation regions; MathCAD fills in the numeric result - Formula appears cramped: Increase
widthandextra_dyparameters fordefine()/eval()with nested fractions