Ajouter des équations mathématiques aux présentations PowerPoint en Java
Vue d’ensemble
PowerPoint stocke les équations au format Office Math Markup Language (OMML). Avec Aspose.Slides for Java, vous pouvez créer le même type de contenu mathématique de manière 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 via Insertion > Équation :

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

Aspose.Slides construit 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‑dessus utilisent MathematicalText et les méthodes fluides de IMathElement pour garder le code court et lisible.
Pour les scénarios d’exportation MathML, voir Exporter des équations mathématiques depuis des présentations en Java.
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, obtenez son MathParagraph et ajoutez des blocs 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, cubique ou autre. L’élément actuel 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 comme 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\u2192\u221E")
.function("x");
mathParagraph.add(new MathBlock(limit));
presentation.save("functions-and-limits.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Pour un nom de fonction personnalisé, faites du nom de fonction 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 opérateurs larges. Utilisez integral pour les intégrales. Les deux méthodes vous 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 servent aux gros opérateurs avec limites optionnelles. Les opérateurs simples tels que +, - et = sont généralement ajoutés en tant que 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 n’incluent pas de crochets par défaut, donc encadrez la matrice 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 actuel 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 exposants
Utilisez les assistants d’indice et d’exposant pour les index et puissances. Lorsque les index doivent apparaître à 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 délimiteurs. Vous pouvez aussi 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 à bordure
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();
}
Regrouper les termes
Utilisez group pour placer un caractère de regroupement au-dessus ou en dessous d’une expression. Ajoutez une limite pour libelliser les termes regroupé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 indice | setSuperscript, setSubscript |
| Ajouter des fonctions | function, asArgumentOfFunction |
| Ajouter des radicaux | IMathElement.radical |
| Ajouter des limites | setLowerLimit, setUpperLimit |
| Ajouter des scripts côté gauche | setSubSuperscriptOnTheLeft |
| Ajouter des sommes et intégrales | nary, integral |
| Ajouter des matrices | MathMatrix |
| Ajouter des tableaux d’équations | toMathArray |
| Ajouter des délimiteurs | enclose |
| Ajouter des barres et bordures | overbar, toBorderBox |
| Regrouper les termes | group |
FAQ
Puis-je modifier une équation PowerPoint existante ?
Oui. Ouvrez la présentation, trouvez la forme contenant un MathPortion, obtenez son MathParagraph et mettez à jour les blocs mathématiques de ce paragraphe.
Les équations sont‑elles enregistrées comme mathématiques PowerPoint modifiables ?
Oui. Lorsque vous enregistrez au format PPTX, Aspose.Slides écrit l’équation en tant que contenu mathématique Office modifiable.
Puis‑je exporter les équations vers LaTeX ?
Aspose.Slides exporte les équations mathématiques vers MathML. Si vous avez besoin de LaTeX, exportez d’abord vers MathML puis convertissez le MathML à l’aide d’un outil qui prend en charge le dialecte LaTeX ciblé.