Ajouter des équations mathématiques aux présentations PowerPoint sur Android
Aperçu
PowerPoint stocke les équations au format Office Math Markup Language (OMML). Avec Aspose.Slides pour Android via Java, vous pouvez créer le même type de contenu mathématique de façon programmatique : fractions, radicaux, fonctions, limites, opérateurs N-aires, matrices, tableaux et blocs mathématiques formatés.
Dans PowerPoint, les utilisateurs ajoutent généralement des équations depuis Insertion > Équation :

Le résultat est du texte mathématique éditable sur la diapositive :

Aspose.Slides crée ce texte mathématique à l’aide de trois objets principaux :
- Une forme mathématique, créée avec addMathShape, est la forme qui contient l’équation.
- MathPortion stocke le contenu mathématique à l’intérieur du cadre de texte de la forme.
- MathParagraph contient un ou plusieurs objets MathBlock.
La plupart des exemples ci‑dessous utilisent MathematicalText et les méthodes fluides de IMathElement pour garder le code court et lisible.
Pour les scénarios d’exportation MathML, voir Export Math Equations from Presentations on Android.
Créer une équation
Cet exemple crée une forme mathématique et ajoute le théorème de Pythagore :

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 crée une forme qui contient déjà un paragraphe mathématique. Accédez au premier MathPortion, récupérez son MathParagraph, et ajoutez des blocs mathématiques ou des éléments mathématiques.
Ajouter des fractions
Utilisez divide pour créer une fraction. Vous pouvez choisir un style de fraction avec 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();
}
Pour une fraction empilée, utilisez MathFractionTypes.Bar :
IMathFraction stackedFraction = new MathematicalText("x + 1").divide("y - 1", MathFractionTypes.Bar);
Ajouter des radicaux
Utilisez radical pour créer une racine carrée, une racine cubique ou autre racine. L’élément courant devient la base, et l’argument devient le degré.

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();
}
Ajouter des fonctions et des limites
Utilisez asArgumentOfFunction ou function pour des fonctions telles que sin(x), log(x) ou des noms de fonctions personnalisés. Pour les limites, placez lim dans un MathLimit ou utilisez 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();
}
Pour un nom de fonction personnalisé, définissez le nom de la fonction comme l’élément actuel :
IMathFunction customFunction = new MathematicalText("f").function("x + 1");
Ajouter des opérateurs N-aires et des intégrales
Utilisez nary pour les sommes, unions, intersections et autres grands opérateurs. Utilisez integral pour les intégrales. Les deux méthodes permettent de définir les limites inférieure et supérieure.

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();
}
Les opérateurs N‑aires concernent les grands opérateurs avec limites facultatives. Les opérateurs simples tels que +, - et = sont généralement ajoutés comme MathematicalText et concaténés dans l’expression.
Pour une intégrale, utilisez integral :
IMathBlock integralBase = new MathematicalText("x").join(new MathematicalText("dx").toBox());
IMathNaryOperator integral = integralBase.integral(MathIntegralTypes.Simple, "0", "1");
Ajouter des matrices
Utilisez MathMatrix pour les lignes et colonnes. Les matrices ne comprennent pas de crochets par défaut, il faut donc les entourer lorsque vous avez besoin de parenthèses, crochets ou accolades.

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();
}
Ajouter des tableaux d’équations
Utilisez toMathArray lorsque vous avez besoin d’équations alignées ou d’une pile verticale d’expressions.

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();
}
Ajouter des fonctions trigonométriques
Utilisez asArgumentOfFunction lorsque l’argument est l’élément courant et que le nom de la fonction est connu.

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();
}
Ajouter des indices et des exposants
Utilisez les assistants d’indice et d’exposant pour les index et les puissances. Lorsque les index doivent apparaître du côté gauche de la base, utilisez 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();
}
Ajouter des délimiteurs
Utilisez enclose pour placer une expression entre des délimiteurs. Vous pouvez également définir un caractère séparateur pour les expressions délimitées contenant plusieurs éléments.

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();
}
Ajouter une boîte encadrée
Utilisez toBorderBox lorsque l’équation elle‑même doit être encadrée.

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();
}
Grouper les termes
Utilisez group pour placer un caractère de regroupement au-dessus ou en dessous d’une expression. Ajoutez une limite pour libeller les termes groupés.

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();
}
Formater les éléments mathématiques
Utilisez les assistants de formatage uniquement lorsqu’ils clarifient la formule. Par exemple, overbar place une barre au-dessus d’un élément mathématique.

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();
}
Référence rapide
| Tâche | API principale |
|---|---|
| Créer du texte mathématique | MathematicalText |
| Combiner des éléments | IMathElement.join |
| Créer des fractions | IMathElement.divide |
| Ajouter un exposant ou un indice | setSuperscript, setSubscript |
| Ajouter des fonctions | function, asArgumentOfFunction |
| Ajouter des radicaux | IMathElement.radical |
| Ajouter des limites | setLowerLimit, setUpperLimit |
| Ajouter des scripts du côté gauche | setSubSuperscriptOnTheLeft |
| Ajouter des sommes et des intégrales | nary, integral |
| Ajouter des matrices | MathMatrix |
| Ajouter des tableaux d’équations | toMathArray |
| Ajouter des délimiteurs | enclose |
| Ajouter des barres et des bordures | overbar, toBorderBox |
| Grouper les termes | group |
FAQ
Puis-je modifier une équation PowerPoint existante ?
Oui. Ouvrez la présentation, localisez la forme qui contient un MathPortion, récupérez son MathParagraph, puis mettez à jour les blocs mathématiques dans ce paragraphe.
Les équations sont‑elles enregistrées comme Mathématiques PowerPoint éditables ?
Oui. Lors de l’enregistrement au format PPTX, Aspose.Slides écrit l’équation comme du contenu Office Math éditable.
Puis-je exporter les équations vers LaTeX ?
Oui. Obtenez le IMathParagraph de son IMathPortion, puis appelez IMathParagraph.toLatex pour l’exporter directement. Pour un exemple complet, consultez Export Math Equations from Presentations in Android via Java.