Ajouter des équations mathématiques aux présentations PowerPoint sur Android

Vue d’ensemble

PowerPoint stocke les équations au format Office Math Markup Language (OMML). Avec Aspose.Slides for 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.

In PowerPoint, users normally add equations from Insert > Equation:

Onglet Insertion de PowerPoint avec la commande Équation sélectionnée

The result is editable math text on the slide:

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

Aspose.Slides builds that math text through three main objects:

La plupart des exemples ci‑dessus utilisent MathematicalText et les méthodes en chaîne de IMathElement pour garder le code court et lisible.

Pour les scénarios d’exportation MathML, consultez Exporter les équations mathématiques depuis les présentations sur Android.

Créer une équation

This example creates a math shape and adds the Pythagorean theorem:

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

For a stacked fraction, use 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 n‑ième 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 fonction 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→∞")
            .function("x");

    mathParagraph.add(new MathBlock(limit));

    presentation.save("functions-and-limits.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

For a custom function name, make the function name the current element:

IMathFunction customFunction = new MathematicalText("f").function("x + 1");

Ajouter des opérateurs n‑aires et des intégrales

Utilisez nary pour les sommes, les unions, les intersections et autres gros 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‑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.

For an integral, use 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 les colonnes. Les matrices n’incluent pas de crochets par défaut, il faut donc entourer la matrice avec des parenthèses, des crochets ou des accolades lorsqu’ils sont nécessaires.

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 indices doivent apparaître à gauche de la base, utilisez setSubSuperscriptOnTheLeft.

Une lettre majuscule Y 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 des délimiteurs. Vous pouvez également définir un caractère de 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();
}

Grouper 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 mise en forme 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 au-dessus

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
Grouper les 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‑elles enregistrées comme mathématiques PowerPoint éditables ?

Oui. Lors de la sauvegarde en PPTX, Aspose.Slides écrit l’équation sous forme de contenu Office mathématique éditable.

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 avec un outil qui prend en charge le dialecte LaTeX cible.