Math Elements
note
The math element is currently in beta and only fully supported for DOCX output.
- For PDF and ODT output, math rendering is not yet fully stable.
- Complex formulas may render incorrectly or as placeholder boxes in exported PDF/ODT files.
- For reliable math rendering, prefer DOCX and open the document in Microsoft Word.
Math elements render LaTeX formulas as native Word equations (OMML) in DOCX documents.
Math Element Properties & Defaults
Math Element Properties
| Property | Type | Required | Description | Default |
|---|---|---|---|---|
type | string | ✓ | Element type: "math" (DOCX only) | - |
latex | string | ✓ | LaTeX math expression (inline or display style) | - |
align | string | ✗ | Horizontal alignment: "left", "center", "right" | From defaults.styles.math.align or left |
spacing | object | ✗ | Custom spacing override (before / after in pt) | From defaults.spacing.math |
Math Defaults (defaults)
Math elements can use global defaults for alignment, font size and spacing.
| Property | Type | Description |
|---|---|---|
defaults.spacing.before.math | number | Spacing before math elements in pt (0-100) |
defaults.spacing.after.math | number | Spacing after math elements in pt (0-100) |
defaults.styles.math.fontSize | number | Default font size for math in pt |
defaults.styles.math.align | string | Default alignment for math: "left", "center", "right" |
Defaults Example
{
"document": {
"type": "docx",
"filename": "math-defaults-example"
},
"defaults": {
"spacing": {
"before": {
"math": 10
},
"after": {
"math": 15
}
},
"styles": {
"math": {
"fontSize": 14,
"align": "center"
}
}
},
"sections": []
}
Basic Math Example
Basic Math JSON
{
"document": {
"type": "docx",
"filename": "math-element-example",
"title": "Math Element Example",
"author": "Json2doc"
},
"defaults": {
"fontFamily": "Arial",
"fontSize": 11,
"color": "#000000",
"spacing": {
"before": {
"math": 10
},
"after": {
"math": 15
}
},
"styles": {
"h1": { "fontSize": 24, "fontWeight": "bold", "color": "#0066cc" },
"math": { "fontSize": 14, "align": "center" }
}
},
"sections": [
{
"type": "flow",
"content": [
{ "type": "h1", "text": "Math Element Demo" },
{ "type": "text", "text": "Below is a LaTeX formula rendered as a native Word equation:" },
{
"type": "math",
"latex": "E = mc^2",
"align": "center",
"spacing": { "before": 10, "after": 20 }
},
{ "type": "text", "text": "You can add more math elements using the same 'math' type and a LaTeX string." }
]
}
]
}
Pythagoras Example
Pythagoras JSON
{
"document": {
"type": "docx",
"filename": "math-pythagoras-example"
},
"sections": [
{
"type": "flow",
"content": [
{
"type": "text",
"text": "Pythagorean theorem:"
},
{
"type": "math",
"latex": "a^2 + b^2 = c^2",
"align": "center",
"spacing": { "before": 10, "after": 20 }
}
]
}
]
}
Multi-line Math Example
Multi-line math is supported by splitting the LaTeX string on explicit \\ line breaks or real newline characters (\n). Each segment is rendered as its own math paragraph with shared alignment.
Multi-line Math JSON
{
"document": {
"type": "docx",
"filename": "math-multiline-example"
},
"sections": [
{
"type": "flow",
"content": [
{
"type": "text",
"text": "Basic vector calculus identities (separate lines):"
},
{
"type": "math",
"latex": "div E = rho / eps_0\n div B = 0\n curl E = - dB/dt\n curl B = mu_0 J + mu_0 eps_0 dE/dt",
"align": "center",
"spacing": { "before": 6, "after": 12 }
}
]
}
]
}