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‑ary, matrices, tableaux et blocs mathématiques formatés.

Dans PowerPoint, les utilisateurs ajoutent généralement des équations via Insert > Equation :

Onglet Insert de PowerPoint avec la commande Equation sélectionnée

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

Une diapositive PowerPoint contenant une équation mathématique éditable

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 in Java.

Créer une équation

Cet exemple crée une forme mathématique et ajoute le théorème de Pythagore :

L’équation c² = a² + b²

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();
}

Ajouter des fractions

Utilisez divide pour créer une fraction. Vous pouvez choisir un style de fraction avec MathFractionTypes.

Une fraction mathématique inclinée montrant 1 divisé par x

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 toute autre racine. L’élément actuel devient la base, et l’argument devient le degré.

Une expression radicielle de n‑ième racine avec x sous le signe radical

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.

La limite de x lorsque x tend vers l’infini

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‑ary 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 vous permettent de définir les limites inférieure et supérieure.

Une somme avec 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‑ary servent aux grands opérateurs avec limites optionnelles. Les opérateurs simples tels que +, - et = sont généralement ajoutés en tant que MathematicalText et joints à 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, de crochets ou d’accolades.

Une matrice mathématique à deux lignes avec une cellule vide

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.

Un tableau mathématique vertical avec x au-dessus de y

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.

La fonction trigonométrique cos appliquée à 2x

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.

Un Y majuscule avec l’indice 1 à gauche et l’exposant n

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 également définir un caractère séparateur pour les expressions délimitées contenant plusieurs éléments.

Une expression délimitée contenant x, y et z séparés par des barres verticales

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.

Une équation encadrée montrant a² = b² + c²

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 des termes

Utilisez group pour placer un caractère de groupement au-dessus ou au-dessous d’une expression. Ajoutez une limite pour étiqueter les termes groupés.

L’expression x + y groupée avec l’étiquette n’importe quel texte en dessous

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.

Une expression mathématique ABC avec une barre supérieure

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 les é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 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
Regrouper des termes group

FAQ

Puis‑je modifier une équation PowerPoint existante ?

Oui. Ouvrez la présentation, trouvez la forme qui contient un MathPortion, récupérez son MathParagraph et mettez à jour les blocs mathématiques dans ce paragraphe.

Les équations sont enregistrées comme du math PowerPoint éditable ?

Oui. Lors de la sauvegarde au format PPTX, Aspose.Slides écrit l’équation en tant que contenu mathématique Office éditable.

Puis‑je exporter les équations vers LaTeX ?

Oui. Récupérez le IMathParagraph de son IMathPortion, puis appelez IMathParagraph.toLatex pour l’exporter directement. Pour un exemple complet, voir Export Math Equations from Presentations in Java.