Add Math Equations to PowerPoint Presentations in Python
Overview
PowerPoint stores equations as Office Math Markup Language (OMML). With Aspose.Slides for Python via .NET, you can create the same kind of math content programmatically: fractions, radicals, functions, limits, N-ary operators, matrices, arrays, and formatted math blocks.
In PowerPoint, users normally add equations from Insert > Equation:

The result is editable math text on the slide:

Aspose.Slides builds that math text through three main objects:
- A math shape, created with add_math_shape, is the shape that contains the equation.
- MathPortion stores math content inside the shape text frame.
- MathParagraph contains one or more MathBlock objects.
Most examples below use MathematicalText and the fluent methods from MathElementBase to keep the code short and readable.
For MathML export scenarios, see Export Math Equations from Presentations in Python via .NET.
Create an Equation
This example uses add_math_shape to create a math shape and adds the Pythagorean theorem:

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 120)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
equation = (
math.MathematicalText("c")
.set_superscript("2")
.join("=")
.join(math.MathematicalText("a").set_superscript("2"))
.join("+")
.join(math.MathematicalText("b").set_superscript("2"))
)
math_paragraph.add(equation)
presentation.save("pythagorean-theorem.pptx", slides.export.SaveFormat.PPTX)
Note
add_math_shape creates a shape that already contains a math paragraph. Access the first MathPortion, get its math_paragraph, and add math blocks or math elements to it.Combine Math Blocks
When parts of an equation are built independently as MathBlock objects, use join_block to combine them. The example builds c² = in the left block and a² + b² in the right block. set_superscript creates editable mathematical superscripts for all three powers.
import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
shape = slide.shapes.add_math_shape(20, 20, 600, 100)
math_paragraph = shape.text_frame.paragraphs[0].portions[0].math_paragraph
left_block = math.MathBlock()
left_block.add(math.MathematicalText("c").set_superscript("2"))
left_block.add(math.MathematicalText("="))
right_block = math.MathBlock()
right_block.add(math.MathematicalText("a").set_superscript("2"))
right_block.add(math.MathematicalText("+"))
right_block.add(math.MathematicalText("b").set_superscript("2"))
combined_block = left_block.join_block(right_block)
math_paragraph.add(combined_block)
presentation.save("equation.pptx", slides.export.SaveFormat.PPTX)
join_block returns left_block itself, now containing both parts in order, and leaves right_block empty. Add the combined block to the MathParagraph only once. After reopening equation.pptx, the paragraph contains one block representing c² = a² + b², with all three powers preserved as mathematical superscripts.
Calling add as left_block.add(right_block) instead attempts to nest one block directly inside another and raises a RuntimeError wrapping a .NET InvalidOperationException.
Add Fractions
Use divide to create a fraction. You can choose a fraction style with MathFractionTypes.

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 100)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
fraction = math.MathematicalText("1").divide("x", math.MathFractionTypes.SKEWED)
math_paragraph.add(math.MathBlock(fraction))
presentation.save("fraction.pptx", slides.export.SaveFormat.PPTX)
For a stacked fraction, use MathFractionTypes.BAR:
import aspose.slides.mathtext as math
stacked_fraction = math.MathematicalText("x + 1").divide("y - 1", math.MathFractionTypes.BAR)
Add Radicals
Use radical to create a square root, cube root, or other root. The current element becomes the base, and the argument becomes the degree.

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 100)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
radical = math.MathematicalText("x").radical("n")
math_paragraph.add(math.MathBlock(radical))
presentation.save("radical.pptx", slides.export.SaveFormat.PPTX)
Show or Hide the Degree of a Square Root
A square root can display an explicit 2 or use the conventional √x notation. The hide_degree property of MathRadical controls which form appears. Both forms retain the degree 2 and the radicand x in the editable equation.

The example saves the same square root twice. The 2 is visible in visible-degree.pptx and hidden in hidden-degree.pptx.
import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
radical = math.MathematicalText("x").radical("2")
shape = slide.shapes.add_math_shape(20, 20, 200, 100)
portion = shape.text_frame.paragraphs[0].portions[0]
portion.math_paragraph.add(math.MathBlock(radical))
radical.hide_degree = False
presentation.save("visible-degree.pptx", slides.export.SaveFormat.PPTX)
radical.hide_degree = True
presentation.save("hidden-degree.pptx", slides.export.SaveFormat.PPTX)
Add Functions and Limits
Use as_argument_of_function or function for functions such as sin(x), log(x), or custom function names. For limits, put lim in a MathLimit or use set_lower_limit.

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 100)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
limit = (
math.MathematicalText("lim")
.set_lower_limit("x\u2192\u221E")
.function("x")
)
math_paragraph.add(math.MathBlock(limit))
presentation.save("functions-and-limits.pptx", slides.export.SaveFormat.PPTX)
For a custom function name, make the function name the current element and call function with its argument:
import aspose.slides.mathtext as math
custom_function = math.MathematicalText("f").function("x + 1")
Add N-ary Operators and Integrals
Use nary for summations, unions, intersections, and other large operators. Use integral for integrals. Both methods let you set lower and upper limits.

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 120)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
summation_base = (
math.MathematicalText("x")
.set_superscript("k")
.join(math.MathematicalText("a").set_superscript("n-k"))
)
summation = summation_base.nary(math.MathNaryOperatorTypes.SUMMATION, "k=0", "n")
math_paragraph.add(math.MathBlock(summation))
presentation.save("nary-operators.pptx", slides.export.SaveFormat.PPTX)
N-ary operators are for large operators with optional limits. Simple operators such as +, -, and = are usually added as MathematicalText and joined into the expression.
For an integral, use integral:
import aspose.slides.mathtext as math
integral_base = math.MathematicalText("x").join(math.MathematicalText("dx").to_box())
integral = integral_base.integral(math.MathIntegralTypes.SIMPLE, "0", "1")
Add Matrices
Use MathMatrix for rows and columns. Matrices do not include brackets by default, so enclose the matrix when you need parentheses, brackets, or braces.

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 120)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
matrix = math.MathMatrix(2, 3)
matrix[0, 0] = math.MathematicalText("1")
matrix[0, 1] = math.MathematicalText("x")
matrix[1, 0] = math.MathematicalText("x")
matrix[1, 1] = math.MathematicalText("2")
matrix[1, 2] = math.MathematicalText("y")
math_paragraph.add(math.MathBlock(matrix))
presentation.save("matrix.pptx", slides.export.SaveFormat.PPTX)
Edit an Existing Matrix
Suppose a linear algebra lesson starts with the coefficient matrix in matrix.pptx, saved by the preceding example. For the next exercise, keep the first equation, replace the second with two new equations, and revise the set of variables. Access the existing matrix as a MathMatrix to keep the coefficients that still apply.

Row and column indices start at zero and refer to the matrix as it stands at each call. Inserting a row before index 1 moves the original second row to index 2. Inserting after index 2 then appends a row. Deleting row 2 removes the original second equation and moves the appended row up one position. Columns shift in the same way.
import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation("matrix.pptx") as presentation:
shape = presentation.slides[0].shapes[0]
portion = shape.text_frame.paragraphs[0].portions[0]
matrix = portion.math_paragraph[0][0]
matrix.insert_row_before(1)
matrix.insert_row_after(2)
matrix.delete_row(2)
matrix.insert_column_before(1)
matrix.insert_column_after(3)
matrix.delete_column(3)
matrix[1, 0] = math.MathematicalText("12")
matrix[1, 1] = math.MathematicalText("1")
matrix[2, 2] = math.MathematicalText("10")
matrix[2, 3] = math.MathematicalText("1")
for row in range(matrix.row_count):
for column in range(matrix.column_count):
if matrix[row, column] is None:
matrix[row, column] = math.MathematicalText("0")
matrix.set_column_alignment(0, math.MathHorizontalAlignment.LEFT)
matrix.set_columns_alignment(1, 3, math.MathHorizontalAlignment.RIGHT)
presentation.save("updated-matrix.pptx", slides.export.SaveFormat.PPTX)
set_column_alignment aligns the first column to the left. set_columns_alignment aligns three consecutive columns to the right, starting at index 1. In updated-matrix.pptx, the matrix has three equations and four variables. The original first-row coefficients 1 and x now appear in the first and third columns.

Add Equation Arrays
Use to_math_array when you need aligned equations or a vertical stack of expressions.

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 140)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
equation_array = (
math.MathematicalText("x")
.join("y")
.to_math_array()
)
math_paragraph.add(math.MathBlock(equation_array))
presentation.save("equation-array.pptx", slides.export.SaveFormat.PPTX)
Align an Array with Surrounding Elements
MathArray.base_justification controls the vertical alignment of an array relative to the mathematical elements beside it. The example below compares alignment with the first row, the center of the array, and the last row.
The following example creates three identical arrays, each with three rows and the neighboring elements A= and =B. All mathematical text uses Cambria Math at 24 points. Only the array alignment changes.
import aspose.slides as slides
import aspose.slides.mathtext as math
def create_text(value):
text = math.MathematicalText(value)
text.format.font_height = 24
text.format.latin_font = slides.FontData("Cambria Math")
return text
with slides.Presentation() as presentation:
presentation.slide_size.set_size(780, 200, slides.SlideSizeScaleType.DO_NOT_SCALE)
slide = presentation.slides[0]
alignments = [math.MathVerticalAlignment.TOP, math.MathVerticalAlignment.CENTER, math.MathVerticalAlignment.BOTTOM]
labels = ["Top", "Center", "Bottom"]
for i, alignment in enumerate(alignments):
label = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 20 + i * 260, 10, 240, 30)
label.fill_format.fill_type = slides.FillType.NO_FILL
label.line_format.fill_format.fill_type = slides.FillType.NO_FILL
label.text_frame.text = labels[i]
label_format = label.text_frame.paragraphs[0].portions[0].portion_format
label_format.font_height = 16
label_format.fill_format.fill_type = slides.FillType.SOLID
label_format.fill_format.solid_fill_color.color = slides.Color.black
math_shape = slide.shapes.add_math_shape(20 + i * 260, 60, 240, 130)
math_shape.text_frame.text_frame_format.anchoring_type = slides.TextAnchorType.TOP
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
math_paragraph.justification = math.MathJustification.LEFT_JUSTIFIED
rows = [create_text("x+y=3"), create_text("x-y=1"), create_text("x=2")]
equation_array = math.MathArray(rows)
equation_array.base_justification = alignment
left_text = create_text("A=")
right_text = create_text("=B")
equation = math.MathBlock(left_text).join(equation_array).join(right_text)
math_paragraph.add(equation)
presentation.save("equation-array-alignment.pptx", slides.export.SaveFormat.PPTX)
The following comparison shows the saved presentation in PowerPoint. Notice the position of A= and =B beside each array.

Configure Row Spacing
row_spacing_rule selects the spacing rule for rows within the array. row_spacing supplies a numeric value for an explicit spacing rule and is ignored by the preset rules.
For exact spacing, the value is measured in points. For proportional spacing, it is measured in half-lines: the value 3 in the example specifies 1.5 lines. Set both properties when specifying an explicit value; changing the numeric value alone does not select a spacing rule.
The example below saves five presentations with the same equations, alignment, and font settings. The first three use presets; the last two show explicit values.
import aspose.slides as slides
import aspose.slides.mathtext as math
def create_text(value):
text = math.MathematicalText(value)
text.format.font_height = 24
text.format.latin_font = slides.FontData("Cambria Math")
return text
settings = [
(math.MathRowSpacingRule.SINGLE_LINE_GAP, 0),
(math.MathRowSpacingRule.ONE_AND_A_HALF_LINE_GAP, 0),
(math.MathRowSpacingRule.TWO_LINE_GAP, 0),
(math.MathRowSpacingRule.EXACTLY, 20),
(math.MathRowSpacingRule.MULTIPLE, 3)
]
for rule, spacing in settings:
file_name = f"equation-array-{rule.name}.pptx"
with slides.Presentation() as presentation:
math_shape = presentation.slides[0].shapes.add_math_shape(20, 20, 600, 300)
paragraph = math_shape.text_frame.paragraphs[0]
paragraph.paragraph_format.space_before = 0
paragraph.paragraph_format.space_after = 0
math_paragraph = paragraph.portions[0].math_paragraph
math_paragraph.justification = math.MathJustification.LEFT_JUSTIFIED
rows = [create_text("x+y=3"), create_text("x-y=1"), create_text("x=2")]
equation_array = math.MathArray(rows)
equation_array.base_justification = math.MathVerticalAlignment.CENTER
equation_array.row_spacing_rule = rule
equation_array.row_spacing = spacing
left_text = create_text("A=")
right_text = create_text("=B")
equation = math.MathBlock(left_text).join(equation_array).join(right_text)
math_paragraph.add(equation)
presentation.save(file_name, slides.export.SaveFormat.PPTX)
Array row spacing is separate from paragraph spacing. space_before and space_after add space around the containing paragraph, not between array rows. Likewise, MathParagraph.justification positions the whole math paragraph horizontally; it does not replace the array’s vertical alignment setting.
Add Trigonometric Functions
Use as_argument_of_function when the argument is the current element and the function name is known.

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 100)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
cosine = math.MathematicalText("2x").as_argument_of_function(
math.MathFunctionsOfOneArgument.COS
)
math_paragraph.add(math.MathBlock(cosine))
presentation.save("trigonometric-function.pptx", slides.export.SaveFormat.PPTX)
Add Subscripts and Superscripts
Use the subscript and superscript helpers for indexes and powers. When the indexes must appear on the left side of the base, use set_sub_superscript_on_the_left.

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 100)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
scripts = math.MathematicalText("Y").set_sub_superscript_on_the_left("1", "n")
math_paragraph.add(math.MathBlock(scripts))
presentation.save("subscript-superscript.pptx", slides.export.SaveFormat.PPTX)
Align Right Subscripts and Superscripts
When both scripts belong to the same base, use MathRightSubSuperscriptElement. Its align_scripts property controls their horizontal positions: False (the default) lets them follow the contour of the base, while True aligns the scripts horizontally with each other.
This example places two copies of the same editable expression side by side. Both use mathematical italic f (U+1D453), a lower script of 1, an upper script of 2, and Cambria Math at 72 points. The sloping base makes the difference visible in PowerPoint. Install Cambria Math before running the example.
import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
presentation.slide_size.set_size(900, 250, slides.SlideSizeScaleType.DO_NOT_SCALE)
slide = presentation.slides[0]
text_format = slides.PortionFormat()
text_format.font_height = 72
text_format.latin_font = slides.FontData("Cambria Math")
for i in range(2):
left = 40 + i * 440
shape = slide.shapes.add_math_shape(left, 75, 380, 150)
base_text = math.MathematicalText("\U0001D453", text_format)
subscript = math.MathematicalText("1", text_format)
superscript = math.MathematicalText("2", text_format)
scripts = math.MathRightSubSuperscriptElement(base_text, subscript, superscript)
scripts.align_scripts = i == 1
portion = shape.text_frame.paragraphs[0].portions[0]
portion.portion_format.font_height = text_format.font_height
portion.portion_format.latin_font = text_format.latin_font
portion.math_paragraph.add(math.MathBlock(scripts))
presentation.save("aligned-scripts.pptx", slides.export.SaveFormat.PPTX)

On the left, the lower script starts farther left than the upper script, following the base contour. On the right, the two scripts align horizontally with each other.
Add Delimiters
Use enclose to put an expression inside delimiters. You can also set a separator character for delimiter expressions that contain several elements.

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 100)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
delimiter = (
math.MathematicalText("x")
.join("y")
.join("z")
.enclose("<", ">")
)
delimiter.separator_character = "|"
math_paragraph.add(math.MathBlock(delimiter))
presentation.save("delimiters.pptx", slides.export.SaveFormat.PPTX)
Size and Align Delimiters
Set grow_to_match_operand_height to True to let the parentheses grow around a tall operand, or to False to keep them at their normal size. delimiter_shape takes one of two MathDelimiterShape values. CENTERED centers the parentheses on the math axis, which runs through the middle of the equals sign. MATCH aligns them with the enclosed content. Both settings affect only the delimiters. The expression inside stays the same.
The example places a fraction in the exponent of x so the expression extends farther above the baseline than below it. All four equations use the same parentheses and font settings.
import aspose.slides as slides
import aspose.slides.mathtext as math
def add_expression(slide, left, top, grow, shape):
fraction = math.MathematicalText("a + b").divide("c + d")
power = math.MathematicalText("x").set_superscript(fraction)
delimiter = power.enclose("(", ")")
delimiter.grow_to_match_operand_height = grow
delimiter.delimiter_shape = shape
math_shape = slide.shapes.add_math_shape(left, top, 300, 100)
portion = math_shape.text_frame.paragraphs[0].portions[0]
portion.portion_format.font_height = 32
equation = math.MathematicalText("y = ").join(delimiter)
portion.math_paragraph.add(equation)
with slides.Presentation() as presentation:
slide = presentation.slides[0]
add_expression(slide, 20, 20, True, math.MathDelimiterShape.CENTERED)
add_expression(slide, 350, 20, True, math.MathDelimiterShape.MATCH)
add_expression(slide, 20, 140, False, math.MathDelimiterShape.CENTERED)
add_expression(slide, 350, 140, False, math.MathDelimiterShape.MATCH)
presentation.save("delimiter-sizing.pptx", slides.export.SaveFormat.PPTX)
In PowerPoint, compare the rows to see the effect of growth. In the top row, the parentheses grow to enclose the exponent and look alike for both shapes. In the bottom row, growth is off. The smaller parentheses on the left (CENTERED) align with the equals sign, while those on the right (MATCH) sit higher around the expression.

Add a Border Box
Call to_border_box on a fraction to frame the entire expression. The returned MathBorderBox shows all four sides by default. Use hide_top, hide_bottom, hide_left, and hide_right to remove individual sides.
The example places six copies of the same fraction on one slide. To show cancellation, hide all four sides and set strikethrough_bottom_left_to_top_right for a rising diagonal (↗) or strikethrough_top_left_to_bottom_right for a falling diagonal (↘). You can also use strikethrough_horizontal or strikethrough_vertical with any combination of visible sides.
import aspose.slides as slides
import aspose.slides.mathtext as math
def add_fraction(slide, x, y, hide_border=False):
fraction = math.MathematicalText("x+1").divide("y+2")
box = fraction.to_border_box()
box.hide_top = box.hide_bottom = box.hide_left = box.hide_right = hide_border
shape = slide.shapes.add_math_shape(x, y, 160, 90)
portion = shape.text_frame.paragraphs[0].portions[0]
portion.portion_format.font_height = 28
portion.math_paragraph.add(math.MathBlock(box))
return box
with slides.Presentation() as presentation:
presentation.slide_size.set_size(540, 220, slides.SlideSizeScaleType.DO_NOT_SCALE)
slide = presentation.slides[0]
add_fraction(slide, 10, 10)
partial = add_fraction(slide, 190, 10)
partial.hide_top = True
partial.hide_right = True
add_fraction(slide, 370, 10, hide_border=True).strikethrough_bottom_left_to_top_right = True
add_fraction(slide, 10, 120, hide_border=True).strikethrough_top_left_to_bottom_right = True
add_fraction(slide, 190, 120, hide_border=True).strikethrough_horizontal = True
add_fraction(slide, 370, 120).strikethrough_vertical = True
presentation.save("border-box.pptx", slides.export.SaveFormat.PPTX)

On this fraction, the horizontal strike overlaps the fraction bar. The vertical strike crosses both the numerator and the denominator while the surrounding border stays visible. These lines decorate the whole expression. Ordinary text strikethrough applies to characters in a text run. After reopening the presentation in PowerPoint, you can still edit the numerator and denominator independently.
Control Visibility and Layout with Phantom Elements
Wrap an expression in MathPhantom to control its visibility and the space it contributes to an equation independently. Its properties control the following:
| Property | Effect |
|---|---|
| show | Set to False to hide the expression. Its dimensions still contribute to layout unless the corresponding properties below are True. |
| zero_width | Treat the expression’s horizontal width as zero, allowing neighboring elements to move closer together. |
| zero_asc | Treat its ascent, the height above the baseline, as zero. This can reduce the space reserved above neighboring elements. |
| zero_desc | Treat its descent, the depth below the baseline, as zero. This can move a fraction bar or other content below the expression closer to its neighbors. |
The baseline is the reference line on which ordinary mathematical letters sit. Hiding content does not remove its dimensions, and treating a dimension as zero does not delete the expression. The underlying expression remains available through base. Set show back to True to display it again; the dimension settings remain unchanged.
The example below creates five side-by-side variants: a visible fraction, a hidden fraction that reserves space, and three hidden fractions with one dimension treated as zero. Each variant uses the same surrounding expression and Cambria Math at 24 points. All equation shapes have the same top position, marked by the gray guide. Placing the phantom in the numerator of an outer fraction makes the vertical layout changes easier to see.
import aspose.slides as slides
import aspose.slides.mathtext as math
def create_text(value):
text = math.MathematicalText(value)
text.format.font_height = 24
text.format.latin_font = slides.FontData("Cambria Math")
return text
with slides.Presentation() as presentation:
presentation.slide_size.set_size(1000, 180, slides.SlideSizeScaleType.DO_NOT_SCALE)
slide = presentation.slides[0]
labels = ["Visible", "Hidden: reserves space", "Hidden: ZeroWidth", "Hidden: ZeroAsc", "Hidden: ZeroDesc"]
guide = slide.shapes.add_auto_shape(slides.ShapeType.LINE, 10, 60, 980, 0)
guide.line_format.fill_format.fill_type = slides.FillType.SOLID
guide.line_format.fill_format.solid_fill_color.color = slides.Color.light_gray
for i, label_text in enumerate(labels):
left = 10 + i * 200
label = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, left, 10, 190, 30)
label.fill_format.fill_type = slides.FillType.NO_FILL
label.line_format.fill_format.fill_type = slides.FillType.NO_FILL
label.text_frame.text = label_text
label_format = label.text_frame.paragraphs[0].portions[0].portion_format
label_format.font_height = 14
label_format.fill_format.fill_type = slides.FillType.SOLID
label_format.fill_format.solid_fill_color.color = slides.Color.black
fraction_numerator = create_text("a+b")
fraction_denominator = create_text("c+d")
fraction = math.MathFraction(fraction_numerator, fraction_denominator)
phantom = math.MathPhantom(fraction)
phantom.show = i == 0
phantom.zero_width = i == 2
phantom.zero_asc = i == 3
phantom.zero_desc = i == 4
left_text = create_text("x+")
right_text = create_text("+y")
numerator = math.MathBlock(left_text).join(phantom).join(right_text)
denominator = create_text("z")
equation = math.MathFraction(numerator, denominator)
shape = slide.shapes.add_math_shape(left, 60, 190, 100)
shape.text_frame.text_frame_format.anchoring_type = slides.TextAnchorType.TOP
math_paragraph = shape.text_frame.paragraphs[0].portions[0].math_paragraph
math_paragraph.add(math.MathBlock(equation))
presentation.save("math-phantom.pptx", slides.export.SaveFormat.PPTX)

Group Terms
Use group to place a grouping character above or below an expression. Add a limit to label the grouped terms.

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 120)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
grouped = (
math.MathematicalText("x + y")
.group(chr(0x23DF), math.MathTopBotPositions.BOTTOM, math.MathTopBotPositions.TOP)
.set_lower_limit("any text")
)
math_paragraph.add(math.MathBlock(grouped))
presentation.save("grouped-terms.pptx", slides.export.SaveFormat.PPTX)
Format Math Elements
Use formatting helpers only where they clarify the formula. For example, overbar places a bar above a math element.

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 100)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
overbar = math.MathematicalText("ABC").overbar()
math_paragraph.add(math.MathBlock(overbar))
presentation.save("overbar.pptx", slides.export.SaveFormat.PPTX)
Format Text in an Existing Equation
To format text throughout an existing equation, traverse each math block’s element tree. MathElementBase.get_children returns only the immediate children, so call it recursively to reach text inside fractions, scripts, radicals, and other nested structures. Check the current element as well as its children to include the starting element when it is MathematicalText.
This example assumes that input.pptx contains an equation such as z = xᵢ / ∛y, with a fraction containing a subscript in its numerator and a radical in its denominator. Processing only the block’s top-level elements would miss the text inside the fraction. The example finds mathematical portions in slide text frames, accesses their blocks through MathPortion.math_paragraph, and sets font_height to 28 points through each text element’s format property.
import aspose.slides as slides
import aspose.slides.mathtext as math
def format_math_text(element):
if isinstance(element, math.MathematicalText):
element.format.font_height = 28
for child in element.get_children():
format_math_text(child)
with slides.Presentation("input.pptx") as presentation:
for slide in presentation.slides:
for shape in slide.shapes:
if not isinstance(shape, slides.AutoShape) or shape.text_frame is None:
continue
for paragraph in shape.text_frame.paragraphs:
for portion in paragraph.portions:
if isinstance(portion, math.MathPortion):
for block in portion.math_paragraph:
format_math_text(block)
presentation.save("formatted-equations.pptx", slides.export.SaveFormat.PPTX)
The saved presentation contains the updated text formatting at every level of each processed equation, while preserving its text content and mathematical structure.
Quick Reference
| Task | Main API |
|---|---|
| Create math text | MathematicalText |
| Combine elements | MathElementBase.join |
| Create fractions | MathElementBase.divide |
| Add superscript or subscript | set_superscript, set_subscript |
| Add functions | function, as_argument_of_function |
| Add radicals | radical |
| Add limits | set_lower_limit, set_upper_limit |
| Add left-side scripts | set_sub_superscript_on_the_left |
| Add summations and integrals | nary, integral |
| Add matrices | MathMatrix |
| Add equation arrays | to_math_array |
| Add delimiters | enclose |
| Add bars and borders | overbar, to_border_box |
| Group terms | group |
FAQ
Can I edit an existing PowerPoint equation?
Yes. Open the presentation, find the shape that contains a MathPortion, get its math_paragraph, and update the math blocks in that paragraph.
Are equations saved as editable PowerPoint math?
Yes. When you save to PPTX, Aspose.Slides writes the equation as editable Office math content.
Can I export equations to LaTeX?
Yes. Get the equation’s MathParagraph from its MathPortion, and call MathParagraph.to_latex to export it directly. For a complete example, see Export Math Equations from Presentations in Python via .NET.