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

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

Aspose.Slides cria 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.
- MathPortion armazena o conteúdo matemático dentro da caixa de texto da forma.
- MathParagraph contém um ou mais objetos MathBlock.
A maioria dos exemplos abaixo usa MathematicalText e os métodos fluent de IMathElement para manter o código curto e legível.
Para cenários de exportação MathML, veja Export Math Equations from Presentations on Android.
Criar uma Equação
Este exemplo cria uma forma matemática e adiciona o teorema de Pitágoras:

Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 120);
IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0)).getMathParagraph();
IMathBlock 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);
} finally {
presentation.dispose();
}
addMathShape creates a shape that already contains a math paragraph. Access the first MathPortion, get its MathParagraph, and add math blocks or math elements to it.
Adicionar Frações
Use divide para criar uma fração. Você pode escolher um estilo de fração com MathFractionTypes.

Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0)).getMathParagraph();
IMathFraction fraction = new MathematicalText("1")
.divide("x", MathFractionTypes.Skewed);
mathParagraph.add(new MathBlock(fraction));
presentation.save("fraction.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Para uma fração empilhada, use MathFractionTypes.Bar:
IMathFraction 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.

Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0)).getMathParagraph();
IMathRadical radical = new MathematicalText("x")
.radical("n");
mathParagraph.add(new MathBlock(radical));
presentation.save("radical.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Adicionar Funções e Limites
Use asArgumentOfFunction ou function para funções como sin(x), log(x) ou nomes de função personalizados. Para limites, coloque lim em um MathLimit ou use setLowerLimit.

Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0)).getMathParagraph();
IMathFunction limit = new MathematicalText("lim")
.setLowerLimit("x→∞")
.function("x");
mathParagraph.add(new MathBlock(limit));
presentation.save("functions-and-limits.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Para um nome de função personalizado, torne o nome da função o elemento atual:
IMathFunction 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.

Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 120);
IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0)).getMathParagraph();
IMathBlock summationBase = new MathematicalText("x")
.setSuperscript("k")
.join(new MathematicalText("a").setSuperscript("n-k"));
IMathNaryOperator summation = summationBase.nary(MathNaryOperatorTypes.Summation, "k=0", "n");
mathParagraph.add(new MathBlock(summation));
presentation.save("nary-operators.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Operadores N‑ários são para operadores grandes com limites opcionais. Operadores simples como +, - e = geralmente são adicionados como MathematicalText e concatenados na expressão.
Para uma integral, use integral:
IMathBlock integralBase = new MathematicalText("x").join(new MathematicalText("dx").toBox());
IMathNaryOperator integral = integralBase.integral(MathIntegralTypes.Simple, "0", "1");
Adicionar Matrizes
Use MathMatrix para linhas e colunas. Matrizes não incluem colchetes por padrão, portanto envolva a matriz quando precisar de parênteses, colchetes ou chaves.

Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 120);
IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0)).getMathParagraph();
MathMatrix matrix = new MathMatrix(2, 3);
matrix.set_Item(0, 0, new MathematicalText("1"));
matrix.set_Item(0, 1, new MathematicalText("x"));
matrix.set_Item(1, 0, new MathematicalText("x"));
matrix.set_Item(1, 1, new MathematicalText("2"));
matrix.set_Item(1, 2, new MathematicalText("y"));
mathParagraph.add(new MathBlock(matrix));
presentation.save("matrix.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Adicionar Arrays de Equações
Use toMathArray quando precisar de equações alinhadas ou de uma pilha vertical de expressões.

Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 140);
IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0)).getMathParagraph();
IMathArray equationArray = new MathematicalText("x")
.join("y")
.toMathArray();
mathParagraph.add(new MathBlock(equationArray));
presentation.save("equation-array.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Adicionar Funções Trigonométricas
Use asArgumentOfFunction quando o argumento for o elemento atual e o nome da função for conhecido.

Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0)).getMathParagraph();
IMathFunction cosine = new MathematicalText("2x")
.asArgumentOfFunction(MathFunctionsOfOneArgument.Cos);
mathParagraph.add(new MathBlock(cosine));
presentation.save("trigonometric-function.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Adicionar Subscritos e Sobrescritos
Use os auxiliares de subscrito e sobrescrito para índices e potências. Quando os índices precisarem aparecer no lado esquerdo da base, use setSubSuperscriptOnTheLeft.

Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0)).getMathParagraph();
IMathLeftSubSuperscriptElement scripts = new MathematicalText("Y")
.setSubSuperscriptOnTheLeft("1", "n");
mathParagraph.add(new MathBlock(scripts));
presentation.save("subscript-superscript.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
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 contenham vários elementos.

Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0)).getMathParagraph();
IMathDelimiter delimiter = new MathematicalText("x")
.join("y")
.join("z")
.enclose('<', '>');
delimiter.setSeparatorCharacter('|');
mathParagraph.add(new MathBlock(delimiter));
presentation.save("delimiters.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Adicionar uma Caixa de Borda
Use toBorderBox quando a própria equação deve ser emoldurada.

Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0)).getMathParagraph();
IMathBorderBox 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);
} finally {
presentation.dispose();
}
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.

Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 120);
IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0)).getMathParagraph();
IMathLimit 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);
} finally {
presentation.dispose();
}
Formatar Elementos Matemáticos
Use auxiliares de formatação somente onde eles clarificam a fórmula. Por exemplo, overbar coloca uma barra acima de um elemento matemático.

Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0)).getMathParagraph();
IMathBar overbar = new MathematicalText("ABC").overbar();
mathParagraph.add(new MathBlock(overbar));
presentation.save("overbar.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
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 à esquerda | 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 |
FAQ
Posso editar uma equação do PowerPoint existente?
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 do PowerPoint editável?
Sim. Ao salvar como PPTX, Aspose.Slides grava a equação como conteúdo matemático do Office editável.
Posso exportar equações para LaTeX?
Sim. Obtenha o [IMathParagraph](https://reference.aspose.com/slides/pt/androidjava/com.aspose.slides/imathparagraph/) da sua [IMathPortion](https://reference.aspose.com/slides/pt/androidjava/com.aspose.slides/imathportion/) e chame [IMathParagraph.toLatex](https://reference.aspose.com/slides/pt/androidjava/com.aspose.slides/imathparagraph/#toLatex--) para exportá‑la diretamente. Para um exemplo completo, veja Export Math Equations from Presentations in Android via Java.