Android で PowerPoint プレゼンテーションに数式を追加する

概要

PowerPoint は方程式を Office Math Markup Language(OMML)として保存します。Aspose.Slides for Android via Java を使用すると、分数、根号、関数、リミット、N 進演算子、行列、配列、書式設定された数式ブロックなど、同様の数式コンテンツをプログラムで作成できます。

PowerPoint では、ユーザーは通常 挿入 > 数式 から方程式を追加します。

![PowerPoint の挿入タブで数式コマンドが選択されている状態] (powerpoint-math-equations_1.png)

結果としてスライド上に編集可能な数式テキストが表示されます。

![編集可能な数式が含まれる PowerPoint スライド] (powerpoint-math-equations_2.png)

Aspose.Slides は次の 3 つの主要オブジェクトで数式テキストを構築します。

  • addMathShape で作成される数式シェイプは、方程式を含むシェイプです。
  • MathPortion はシェイプのテキストフレーム内に数式コンテンツを格納します。
  • MathParagraph は 1 つ以上の MathBlock オブジェクトを保持します。

以下の例は主に MathematicalText と、コードを簡潔に保つための IMathElement のフルエントメソッドを使用しています。

MathML エクスポートのシナリオについては、Export Math Equations from Presentations on Android を参照してください。

方程式の作成

この例では数式シェイプを作成し、ピタゴラスの定理を追加します。

![c 二乗が a 二乗 + b 二乗に等しい方程式] (powerpoint-math-equations_3.png)

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

分数の追加

divide を使用して分数を作成します。MathFractionTypes で分数スタイルを選択できます。

![x で割った 1 の歪んだ分数] (powerpoint-math-equations_4.png)

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

積み重ね分数の場合は MathFractionTypes.Bar を使用します。

IMathFraction stackedFraction = new MathematicalText("x + 1").divide("y - 1", MathFractionTypes.Bar);

根号の追加

sqrtcuberoot などの根号を作成するには radical を使用します。現在の要素が基底となり、引数が次数になります。

![x が根号記号の下にある n 次根] (powerpoint-math-equations_5.png)

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

関数とリミットの追加

asArgumentOfFunction または function を使用して sin(x)log(x) などの関数やカスタム関数名を追加します。リミットの場合は MathLimitlim を入れるか、setLowerLimit を使用します。

![x が無限大に近づくときのリミット] (powerpoint-math-equations_8.png)

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

カスタム関数名を使用する場合は、関数名を現在の要素として設定します。

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

N 進演算子と積分の追加

summationunionintersection などの大きな演算子は nary を使用します。積分は integral を使用し、どちらも下限・上限を設定できます。

![下限と上限が付いた総和] (powerpoint-math-equations_7.png)

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

N 進演算子はオプションのリミットを持つ大きな演算子です。+-= などの単純演算子は通常 MathematicalText として追加し、式に結合します。

積分の場合は integral を使用します。

IMathBlock integralBase = new MathematicalText("x").join(new MathematicalText("dx").toBox());
IMathNaryOperator integral = integralBase.integral(MathIntegralTypes.Simple, "0", "1");

行列の追加

行と列を定義するには MathMatrix を使用します。行列はデフォルトで括弧を含まないため、丸括弧、角括弧、波括弧が必要なときは自分で囲んでください。

![空白セルが 1 つある 2 行の行列] (powerpoint-math-equations_10.png)

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

数式配列の追加

整列された方程式や縦に積み重ねた式が必要なときは toMathArray を使用します。

![x が y の上にある縦方向の数式配列] (powerpoint-math-equations_11.png)

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

三角関数の追加

引数が現在の要素で関数名が既知の場合は asArgumentOfFunction を使用します。

![2x に適用された余弦関数] (powerpoint-math-equations_6.png)

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

下付文字と上付文字の追加

インデックスや指数には下付文字・上付文字ヘルパーを使用します。インデックスを基底の左側に表示する必要がある場合は setSubSuperscriptOnTheLeft を使用します。

![左側に下付文字 1 と上付文字 n を持つ大文字 Y] (powerpoint-math-equations_9.png)

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

区切り記号の追加

式を区切り記号で囲むには enclose を使用します。複数要素を含む区切り記号式では区切り文字も設定できます。

![縦棒で区切られた x、y、z を含む区切り記号式] (powerpoint-math-equations_13.png)

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

枠付きボックスの追加

式全体に枠を付けたい場合は toBorderBox を使用します。

![a 二乗 = b 二乗 + c 二乗 を示す枠付き方程式] (powerpoint-math-equations_12.png)

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

項のグループ化

group を使用して式の上下にグループ文字を配置し、リミットでラベル付けできます。

![下に「any text」ラベルが付いた x + y のグループ] (powerpoint-math-equations_15.png)

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

数式要素の書式設定

書式設定ヘルパーは式の可読性が向上する場合にのみ使用します。例として overbar は数式要素の上にバーを配置します。

![上にバーが付いた ABC の数式] (powerpoint-math-equations_14.png)

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

クイックリファレンス

タスク 主要 API
数式テキストの作成 MathematicalText
要素の結合 IMathElement.join
分数の作成 IMathElement.divide
上付文字または下付文字の追加 setSuperscript, setSubscript
関数の追加 function, asArgumentOfFunction
根号の追加 IMathElement.radical
リミットの追加 setLowerLimit, setUpperLimit
左側スクリプトの追加 setSubSuperscriptOnTheLeft
総和と積分の追加 nary, integral
行列の追加 MathMatrix
数式配列の追加 toMathArray
区切り記号の追加 enclose
バーと枠の追加 overbar, toBorderBox
項のグループ化 group

FAQ

既存の PowerPoint 方程式を編集できますか?

はい。プレゼンテーションを開き、MathPortion を含むシェイプを見つけ、その MathParagraph を取得して、その段落内の数式ブロックを更新します。

方程式は編集可能な PowerPoint 数式として保存されますか?

はい。PPTX に保存すると、Aspose.Slides は方程式を編集可能な Office 数式コンテンツとして書き込みます。

方程式を LaTeX にエクスポートできますか?

はい。方程式の IMathParagraph をその IMathPortion から取得し、IMathParagraph.toLatex を呼び出して直接エクスポートします。完全な例については、Export Math Equations from Presentations in Android via Java を参照してください。