Math Text
This article demonstrates working with mathematical text shapes and formatting equations using Aspose.Slides for .NET.
Copy a C# block into Program.cs in a .NET project with Aspose.Slides.NET6.CrossPlatform installed. Each block is a complete program. Run one block at a time. The ZIP contains the IDE-ready project and its verification checks. The copied code runs in evaluation mode. For licensed output, apply your license before creating a presentation, as described in the licensing guide.
Download and run
Download the complete project, extract it, and open Example.sln or run:
dotnet restore Example.sln
dotnet run --project Example.csproj -- --mode evaluation
The download includes every case below, its fixture helpers and inputs. Program.cs runs all cases; use --action block-001 to run one independently. Outputs and an action report are written to the printed output directory. No previous example or working-directory setup is required. Evaluation adds a watermark and can truncate read-back text; the short fixtures still have explicit structural checks. For full text and no watermark, use --mode licensed --license PATH with your own valid license. See the setup guide for supported SDKs and native prerequisites.
Add Math Text
Create a math shape containing a fraction and the Pythagorean formula.
using Aspose.Slides.Export;
using Aspose.Slides;
using Aspose.Slides.MathText;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
// Add a Math shape to the slide.
var mathShape = slide.Shapes.AddMathShape(0, 0, 720, 150);
// Access the math paragraph.
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
// Add a simple fraction: x / y
var fraction = new MathematicalText("x").Divide("y");
mathParagraph.Add(new MathBlock(fraction));
// Add equation: c² = a² + b²
var pythagoreanEquation = new MathematicalText("c")
.SetSuperscript("2")
.Join("=")
.Join(new MathematicalText("a").SetSuperscript("2"))
.Join("+")
.Join(new MathematicalText("b").SetSuperscript("2"));
mathParagraph.Add(pythagoreanEquation);
presentation.Save("math-text-01.pptx", SaveFormat.Pptx);
Access Math Text
Locate a shape that contains a math paragraph on the slide.
using System;
using Aspose.Slides.Export;
using System.Linq;
using Aspose.Slides;
using Aspose.Slides.MathText;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
// Seed a fraction before searching all shapes and text portions.
var mathShape = slide.Shapes.AddMathShape(50, 50, 200, 80);
var fractionPortion = (MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0];
fractionPortion.MathParagraph.Add(new MathBlock(new MathematicalText("x").Divide("y")));
// Read the first math portion from the slide.
var mathPortion = slide.Shapes.OfType<IAutoShape>()
.SelectMany(textShape => textShape.TextFrame.Paragraphs)
.SelectMany(paragraph => paragraph.Portions)
.OfType<MathPortion>()
.First();
Console.WriteLine(mathPortion.MathParagraph.Count);
presentation.Save("math-text-02.pptx", SaveFormat.Pptx);
Remove Math Text
Delete a math shape from the slide.
using Aspose.Slides.Export;
using Aspose.Slides;
using Aspose.Slides.MathText;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var mathShape = slide.Shapes.AddMathShape(50, 50, 100, 50);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
var fraction = new MathematicalText("x").Divide("y");
mathParagraph.Add(new MathBlock(fraction));
slide.Shapes.Remove(mathShape);
presentation.Save("math-text-03.pptx", SaveFormat.Pptx);
Format Math Text
Set font properties for a math portion.
using Aspose.Slides.Export;
using Aspose.Slides;
using Aspose.Slides.MathText;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var mathShape = slide.Shapes.AddMathShape(50, 50, 100, 50);
var paragraph = mathShape.TextFrame.Paragraphs[0];
var mathParagraph = ((MathPortion)paragraph.Portions[0]).MathParagraph;
var fraction = new MathematicalText("x").Divide("y");
mathParagraph.Add(new MathBlock(fraction));
// The paragraph default is persisted for its mathematical content.
paragraph.ParagraphFormat.DefaultPortionFormat.FontHeight = 20;
presentation.Save("math-text-04.pptx", SaveFormat.Pptx);