Adicionar Equações Matemáticas a Apresentações PowerPoint em .NET
Visão geral
PowerPoint armazena equações como Office Math Markup Language (OMML). Com Aspose.Slides para .NET, você pode criar o mesmo tipo de conteúdo matemático programaticamente: frações, radicais, funções, limites, operadores N‑ário, matrizes, arrays e blocos de matemática formatados.
No PowerPoint, os usuários normalmente adicionam equações por meio de Inserir > Equação:

O resultado é texto matemático editável no slide:

Aspose.Slides constrói esse texto matemático através de três objetos principais:
- Uma forma matemática, criada com AddMathShape, é a forma que contém a equação.
- O MathPortion armazena o conteúdo matemático dentro da caixa de texto da forma.
- O MathParagraph contém um ou mais objetos MathBlock.
A maioria dos exemplos abaixo usa MathematicalText e os métodos fluentes de IMathElement para manter o código curto e legível.
Para cenários de exportação MathML, veja Exportar Equações Matemáticas de Apresentações em .NET.
Criar uma Equação
Este exemplo cria uma forma matemática e adiciona o teorema de Pitágoras:

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 120);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
var equation = new MathematicalText("c")
.SetSuperscript("2")
.Join("=")
.Join(new MathematicalText("a").SetSuperscript("2"))
.Join("+")
.Join(new MathematicalText("b").SetSuperscript("2"));
mathParagraph.Add(equation);
presentation.Save("pythagorean-theorem.pptx", SaveFormat.Pptx);
AddMathShape cria uma forma que já contém um parágrafo matemático. Acesse o primeiro MathPortion, obtenha seu MathParagraph e adicione blocos matemáticos ou elementos matemáticos a ele.
Adicionar Frações
Use Divide para criar uma fração. Você pode escolher um estilo de fração com MathFractionTypes.

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 100);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
var fraction = new MathematicalText("1")
.Divide("x", MathFractionTypes.Skewed);
mathParagraph.Add(new MathBlock(fraction));
presentation.Save("fraction.pptx", SaveFormat.Pptx);
Para uma fração empilhada, use MathFractionTypes.Bar:
var stackedFraction = new MathematicalText("x + 1").Divide("y - 1", MathFractionTypes.Bar);
Adicionar Radicais
Use Radical para criar uma raiz quadrada, raiz cúbica ou outra raiz. O elemento atual torna‑se a base e o argumento torna‑se o grau.

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 100);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
var radical = new MathematicalText("x")
.Radical("n");
mathParagraph.Add(new MathBlock(radical));
presentation.Save("radical.pptx", SaveFormat.Pptx);
Adicionar Funções e Limites
Use AsArgumentOfFunction ou Function para funções como sin(x), log(x) ou nomes de funções personalizados. Para limites, coloque lim em um MathLimit ou use SetLowerLimit.

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 100);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
var limit = new MathematicalText("lim")
.SetLowerLimit("x→∞")
.Function("x");
mathParagraph.Add(new MathBlock(limit));
presentation.Save("functions-and-limits.pptx", SaveFormat.Pptx);
Para um nome de função personalizado, torne o nome da função o elemento atual:
var customFunction = new MathematicalText("f").Function("x + 1");
Adicionar Operadores N‑ários e Integrais
Use Nary para somatórios, uniões, interseções e outros operadores grandes. Use Integral para integrais. Ambos os métodos permitem definir limites inferior e superior.

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 120);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
var summationBase = new MathematicalText("x")
.SetSuperscript("k")
.Join(new MathematicalText("a").SetSuperscript("n-k"));
var summation = summationBase.Nary(MathNaryOperatorTypes.Summation, "k=0", "n");
mathParagraph.Add(new MathBlock(summation));
presentation.Save("nary-operators.pptx", SaveFormat.Pptx);
Operadores N‑ários são para operadores grandes com limites opcionais. Operadores simples como +, - e = geralmente são adicionados como MathematicalText e unidos na expressão.
Para uma integral, use Integral:
var integralBase = new MathematicalText("x").Join(new MathematicalText("dx").ToBox());
var integral = integralBase.Integral(MathIntegralTypes.Simple, "0", "1");
Adicionar Matrizes
Use MathMatrix para linhas e colunas. Matrizes não incluem colchetes por padrão, portanto coloque a matriz entre parênteses, colchetes ou chaves quando precisar.

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 120);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
var matrix = new MathMatrix(2, 3);
matrix[0, 0] = new MathematicalText("1");
matrix[0, 1] = new MathematicalText("x");
matrix[1, 0] = new MathematicalText("x");
matrix[1, 1] = new MathematicalText("2");
matrix[1, 2] = new MathematicalText("y");
mathParagraph.Add(new MathBlock(matrix));
presentation.Save("matrix.pptx", SaveFormat.Pptx);
Adicionar Arrays de Equações
Use ToMathArray quando precisar de equações alinhadas ou de uma pilha vertical de expressões.

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 140);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
var equationArray = new MathematicalText("x")
.Join("y")
.ToMathArray();
mathParagraph.Add(new MathBlock(equationArray));
presentation.Save("equation-array.pptx", SaveFormat.Pptx);
Adicionar Funções Trigonométricas
Use AsArgumentOfFunction quando o argumento for o elemento atual e o nome da função for conhecido.

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 100);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
var cosine = new MathematicalText("2x")
.AsArgumentOfFunction(MathFunctionsOfOneArgument.Cos);
mathParagraph.Add(new MathBlock(cosine));
presentation.Save("trigonometric-function.pptx", SaveFormat.Pptx);
Adicionar Subscritos e Superescritos
Use os auxiliares de subscrito e sobrescrito para índices e potências. Quando os índices devem aparecer no lado esquerdo da base, use SetSubSuperscriptOnTheLeft.

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 100);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
var scripts = new MathematicalText("Y")
.SetSubSuperscriptOnTheLeft("1", "n");
mathParagraph.Add(new MathBlock(scripts));
presentation.Save("subscript-superscript.pptx", SaveFormat.Pptx);
Adicionar Delimitadores
Use Enclose para colocar uma expressão dentro de delimitadores. Você também pode definir um caractere separador para expressões delimitadoras que contêm vários elementos.

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 100);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
var delimiter = new MathematicalText("x")
.Join("y")
.Join("z")
.Enclose('<', '>');
delimiter.SeparatorCharacter = '|';
mathParagraph.Add(new MathBlock(delimiter));
presentation.Save("delimiters.pptx", SaveFormat.Pptx);
Adicionar uma Caixa de Borda
Use ToBorderBox quando a própria equação deve ser emoldurada.

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 100);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
var boxedEquation = new MathematicalText("a")
.SetSuperscript("2")
.Join("=")
.Join(new MathematicalText("b").SetSuperscript("2"))
.Join("+")
.Join(new MathematicalText("c").SetSuperscript("2"))
.ToBorderBox();
mathParagraph.Add(new MathBlock(boxedEquation));
presentation.Save("border-box.pptx", SaveFormat.Pptx);
Agrupar Termos
Use Group para colocar um caractere de agrupamento acima ou abaixo de uma expressão. Adicione um limite para rotular os termos agrupados.

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 120);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
var grouped = new MathematicalText("x + y")
.Group('\u23DF', MathTopBotPositions.Bottom, MathTopBotPositions.Top)
.SetLowerLimit("any text");
mathParagraph.Add(new MathBlock(grouped));
presentation.Save("grouped-terms.pptx", SaveFormat.Pptx);
Formatar Elementos Matemáticos
Use os auxiliares de formatação apenas onde eles esclarecem a fórmula. Por exemplo, Overbar coloca uma barra acima de um elemento matemático.

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 100);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
var overbar = new MathematicalText("ABC").Overbar();
mathParagraph.Add(new MathBlock(overbar));
presentation.Save("overbar.pptx", SaveFormat.Pptx);
Referência Rápida
| Tarefa | API Principal |
|---|---|
| Criar texto matemático | MathematicalText |
| Combinar elementos | IMathElement.Join |
| Criar frações | IMathElement.Divide |
| Adicionar sobrescrito ou subscrito | SetSuperscript, SetSubscript |
| Adicionar funções | Function, AsArgumentOfFunction |
| Adicionar radicais | IMathElement.Radical |
| Adicionar limites | SetLowerLimit, SetUpperLimit |
| Adicionar scripts do lado esquerdo | SetSubSuperscriptOnTheLeft |
| Adicionar somatórios e integrais | Nary, Integral |
| Adicionar matrizes | MathMatrix |
| Adicionar arrays de equações | ToMathArray |
| Adicionar delimitadores | Enclose |
| Adicionar barras e bordas | Overbar, ToBorderBox |
| Agrupar termos | Group |
Perguntas Frequentes
Posso editar uma equação existente do PowerPoint?
Sim. Abra a apresentação, encontre a forma que contém um MathPortion, obtenha seu MathParagraph e atualize os blocos matemáticos naquele parágrafo.
As equações são salvas como matemática editável do PowerPoint?
Sim. Ao salvar em PPTX, o Aspose.Slides grava a equação como conteúdo matemático editável do Office.
Posso exportar equações para LaTeX?
O Aspose.Slides exporta equações matemáticas para MathML. Se precisar de LaTeX, exporte primeiro para MathML e depois converta o MathML com uma ferramenta que suporte seu dialeto LaTeX alvo.