Java で PowerPoint プレゼンテーションに数式を追加する
概要
PowerPoint は方程式を Office Math Markup Language (OMML) として保存します。Aspose.Slides for Java を使用すると、同様の数式コンテンツをプログラムで作成できます:分数、根号、関数、極限、N 進演算子、行列、配列、そして書式設定された数式ブロック。
PowerPoint では、ユーザーは通常 Insert > Equation から方程式を追加します:

結果はスライド上の編集可能な数式テキストになります:

Aspose.Slides は次の 3 つの主要オブジェクトを通じてその数式テキストを構築します:
- 数式シェイプは、addMathShape で作成され、方程式を含むシェイプです。
- MathPortion はシェイプのテキストフレーム内に数式コンテンツを格納します。
- MathParagraph は 1 つ以上の MathBlock オブジェクトを含みます。
以下のほとんどの例は、コードを簡潔で読みやすく保つために MathematicalText と IMathElement のフルエントメソッドを使用しています。
MathML エクスポートシナリオについては、Java でのプレゼンテーションからの数式エクスポート を参照してください。
方程式の作成
この例では、addMathShape を使用して数式シェイプを作成し、ピタゴラスの定理を追加します:

import com.aspose.slides.*;
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();
}
Note
addMathShape は、すでに数式段落を含むシェイプを作成します。最初の MathPortion にアクセスし、getMathParagraph を呼び出して、数式ブロックまたは数式要素を追加します。数式ブロックの結合
方程式の一部が IMathBlock オブジェクトとして独立して構築されている場合、joinBlock を使用して結合します。この例では、左ブロックに c² =、右ブロックに a² + b² を構築します。setSuperscript は、3 つすべての指数に対して編集可能な数学上標を作成します。
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape shape = slide.getShapes().addMathShape(20, 20, 600, 100);
IMathPortion mathPortion = (IMathPortion) shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
IMathParagraph mathParagraph = mathPortion.getMathParagraph();
MathBlock leftBlock = new MathBlock();
leftBlock.add(new MathematicalText("c").setSuperscript("2"));
leftBlock.add(new MathematicalText("="));
MathBlock rightBlock = new MathBlock();
rightBlock.add(new MathematicalText("a").setSuperscript("2"));
rightBlock.add(new MathematicalText("+"));
rightBlock.add(new MathematicalText("b").setSuperscript("2"));
IMathBlock combinedBlock = leftBlock.joinBlock(rightBlock);
mathParagraph.add(combinedBlock);
presentation.save("equation.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
joinBlock は leftBlock 自身を返し、両方の部分が順番に含まれ、rightBlock は空になります。結合したブロックは IMathParagraph に一度だけ追加します。equation.pptx を再度開くと、段落には c² = a² + b² を表す 1 つのブロックが含まれ、3 つの指数はすべて数学上標として保持されます。
add を leftBlock.add(rightBlock) と呼び出すと、ブロックを直接別のブロックに入れ子にしようとし、InvalidOperationException がスローされます。
分数の追加
divide を使用して分数を作成します。MathFractionTypesで分数スタイルを選択できます。

import com.aspose.slides.*;
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 を使用します:
import com.aspose.slides.*;
IMathFraction stackedFraction = new MathematicalText("x + 1").divide("y - 1", MathFractionTypes.Bar);
根号の追加
radical を使用して平方根、立方根、またはその他の根号を作成します。現在の要素が基底となり、引数が次数になります。

import com.aspose.slides.*;
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();
}
平方根の次数の表示/非表示
平方根は明示的な 2 を表示するか、従来の √x 記法を使用できます。IMathRadical の setHideDegree メソッドで表示形式を制御します。どちらの形式も、編集可能な方程式内で次数 2 と根号内部の x を保持します。

この例では同じ平方根を 2 回保存します。visible-degree.pptx では 2 が表示され、hidden-degree.pptx では非表示になります。
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IMathRadical radical = new MathematicalText("x").radical("2");
IAutoShape shape = slide.getShapes().addMathShape(20, 20, 200, 100);
IMathPortion portion = (IMathPortion) shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
portion.getMathParagraph().add(new MathBlock(radical));
radical.setHideDegree(false);
presentation.save("visible-degree.pptx", SaveFormat.Pptx);
radical.setHideDegree(true);
presentation.save("hidden-degree.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
関数と極限の追加
asArgumentOfFunction または function を使用して sin(x), log(x) などの関数やカスタム関数名を作成します。極限の場合は、lim を MathLimit に入れるか、setLowerLimit を使用します。

import com.aspose.slides.*;
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();
}
カスタム関数名の場合、関数名を現在の要素にし、引数として function を呼び出します:
import com.aspose.slides.*;
IMathFunction customFunction = new MathematicalText("f").function("x + 1");
N 進演算子と積分の追加
nary を使用して総和、和集合、交差、その他の大きな演算子を作成します。integral を使用して積分を作成します。両方のメソッドで下限と上限を設定できます。

import com.aspose.slides.*;
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 を使用します:
import com.aspose.slides.*;
IMathBlock integralBase = new MathematicalText("x").join(new MathematicalText("dx").toBox());
IMathNaryOperator integral = integralBase.integral(MathIntegralTypes.Simple, "0", "1");
行列の追加
MathMatrix を使用して行と列を作成します。行列はデフォルトで括弧を含まないため、丸括弧、角括弧、波括弧が必要な場合は行列を囲んでください。

import com.aspose.slides.*;
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();
}
既存の行列の編集
線形代数のレッスンが、前の例で保存された matrix.pptx の係数行列から始まるとします。次の演習では、最初の方程式を保持し、2 番目を 2 つの新しい方程式に置き換え、変数のセットを修正します。既存の行列を IMathMatrix で開くことで、まだ有効な係数を保持できます。

行と列のインデックスは 0 から始まり、各呼び出し時点の行列を指します。インデックス 1 の前に行を挿入すると元の 2 行目がインデックス 2 に移動します。インデックス 2 の後に挿入すると行が追加されます。インデックス 2 の行を削除すると元の 2 行目が削除され、追加された行が 1 つ上に移動します。列も同様にシフトします。
import com.aspose.slides.*;
Presentation presentation = new Presentation("matrix.pptx");
try {
IAutoShape shape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
IMathPortion portion = (IMathPortion) shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
IMathMatrix matrix = (IMathMatrix) portion.getMathParagraph().get_Item(0).get_Item(0);
matrix.insertRowBefore(1);
matrix.insertRowAfter(2);
matrix.deleteRow(2);
matrix.insertColumnBefore(1);
matrix.insertColumnAfter(3);
matrix.deleteColumn(3);
// 新しいセルは最初は null です。非ゼロの係数を先に設定してください。
matrix.set_Item(1, 0, new MathematicalText("12"));
matrix.set_Item(1, 1, new MathematicalText("1"));
matrix.set_Item(2, 2, new MathematicalText("10"));
matrix.set_Item(2, 3, new MathematicalText("1"));
for (int row = 0; row < matrix.getRowCount(); row++) {
for (int column = 0; column < matrix.getColumnCount(); column++) {
if (matrix.get_Item(row, column) == null) {
matrix.set_Item(row, column, new MathematicalText("0"));
}
}
}
matrix.setColumnAlignment(0, MathHorizontalAlignment.Left);
matrix.setColumnsAlignment(1, 3, MathHorizontalAlignment.Right);
presentation.save("updated-matrix.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
setColumnAlignment は最初の列を左揃えにします。setColumnsAlignment はインデックス 1 から開始して 3 つの連続列を右揃えにします。updated-matrix.pptx では、行列に 3 つの方程式と 4 つの変数があります。元の 1 行目の係数 1 と x は現在、1 列目と 3 列目に表示されます。

方程式配列の追加
整列した方程式や縦に積み重ねた式が必要な場合は、toMathArray を使用します。

import com.aspose.slides.*;
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();
}
配列を周囲の要素と整列させる
setBaseJustification は、配列の垂直整列を隣接する数式要素に対して制御します。以下の例は、最上行、配列の中央、最下行での整列を比較しています。
以下の例は、3 行で隣接要素 A= と =B を持つ 3 つの同一配列を作成します。すべての数式テキストは 24 ポイントの Cambria Math を使用しています。配列の整列のみが変更されています。
import com.aspose.slides.*;
import java.awt.Color;
class MathTextFactory {
MathematicalText createText(String value) {
MathematicalText text = new MathematicalText(value);
text.getFormat().setFontHeight(24);
text.getFormat().setLatinFont(new FontData("Cambria Math"));
return text;
}
}
MathTextFactory factory = new MathTextFactory();
Presentation presentation = new Presentation();
try {
presentation.getSlideSize().setSize(780, 200, SlideSizeScaleType.DoNotScale);
ISlide slide = presentation.getSlides().get_Item(0);
int[] alignments = { MathVerticalAlignment.Top, MathVerticalAlignment.Center, MathVerticalAlignment.Bottom };
String[] alignmentNames = { "Top", "Center", "Bottom" };
for (int i = 0; i < alignments.length; i++) {
IAutoShape label = slide.getShapes().addAutoShape(ShapeType.Rectangle, 20 + i * 260, 10, 240, 30);
label.getFillFormat().setFillType(FillType.NoFill);
label.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
label.getTextFrame().setText(alignmentNames[i]);
IPortionFormat labelFormat = label.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat();
labelFormat.setFontHeight(16);
labelFormat.getFillFormat().setFillType(FillType.Solid);
labelFormat.getFillFormat().getSolidFillColor().setColor(Color.BLACK);
IAutoShape mathShape = slide.getShapes().addMathShape(20 + i * 260, 60, 240, 130);
mathShape.getTextFrame().getTextFrameFormat().setAnchoringType(TextAnchorType.Top);
IMathParagraph mathParagraph = ((IMathPortion) mathShape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0)).getMathParagraph();
mathParagraph.setJustification(MathJustification.LeftJustified);
IMathElement[] rows = { factory.createText("x+y=3"), factory.createText("x-y=1"), factory.createText("x=2") };
MathArray equationArray = new MathArray(rows[0]);
equationArray.getArguments().add(rows[1]);
equationArray.getArguments().add(rows[2]);
equationArray.setBaseJustification(alignments[i]);
MathematicalText leftText = factory.createText("A=");
MathematicalText rightText = factory.createText("=B");
IMathBlock equation = new MathBlock(leftText).join(equationArray).join(rightText);
mathParagraph.add(equation);
}
presentation.save("equation-array-alignment.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
以下の比較は PowerPoint で保存されたプレゼンテーションを示しています。各配列の横にある A= と =B の位置に注目してください。

行間隔の設定
setRowSpacingRule は配列内の行の間隔ルールを選択します。setRowSpacing は明示的な間隔ルールの数値を提供し、プリセットルールでは無視されます。
正確な間隔の場合、値はポイントで測定されます。比例間隔の場合、値は半行で測定されます:例の 3 は 1.5 行に相当します。明示的な値を指定する場合は、両方のプロパティを設定してください。数値だけを変更しても間隔ルールは選択されません。
以下の例は、同じ方程式、整列、フォント設定で 5 つのプレゼンテーションを保存します。最初の 3 つはプリセットを使用し、最後の 2 つは明示的な値を示します。
import com.aspose.slides.*;
class MathTextFactory {
MathematicalText createText(String value) {
MathematicalText text = new MathematicalText(value);
text.getFormat().setFontHeight(24);
text.getFormat().setLatinFont(new FontData("Cambria Math"));
return text;
}
}
MathTextFactory factory = new MathTextFactory();
int[] rules = { MathRowSpacingRule.SingleLineGap, MathRowSpacingRule.OneAndAHalfLineGap, MathRowSpacingRule.TwoLineGap, MathRowSpacingRule.Exactly, MathRowSpacingRule.Multiple };
int[] spacingValues = { 0, 0, 0, 20, 3 };
String[] ruleNames = { "SingleLineGap", "OneAndAHalfLineGap", "TwoLineGap", "Exactly", "Multiple" };
for (int i = 0; i < rules.length; i++) {
String fileName = "equation-array-" + ruleNames[i] + ".pptx";
Presentation presentation = new Presentation();
try {
IAutoShape mathShape = presentation.getSlides().get_Item(0).getShapes().addMathShape(20, 20, 600, 300);
IParagraph paragraph = mathShape.getTextFrame().getParagraphs().get_Item(0);
paragraph.getParagraphFormat().setSpaceBefore(0);
paragraph.getParagraphFormat().setSpaceAfter(0);
IMathParagraph mathParagraph = ((IMathPortion) paragraph.getPortions().get_Item(0)).getMathParagraph();
mathParagraph.setJustification(MathJustification.LeftJustified);
IMathElement[] rows = { factory.createText("x+y=3"), factory.createText("x-y=1"), factory.createText("x=2") };
MathArray equationArray = new MathArray(rows[0]);
equationArray.getArguments().add(rows[1]);
equationArray.getArguments().add(rows[2]);
equationArray.setBaseJustification(MathVerticalAlignment.Center);
equationArray.setRowSpacingRule(rules[i]);
equationArray.setRowSpacing(spacingValues[i]);
MathematicalText leftText = factory.createText("A=");
MathematicalText rightText = factory.createText("=B");
IMathBlock equation = new MathBlock(leftText).join(equationArray).join(rightText);
mathParagraph.add(equation);
presentation.save(fileName, SaveFormat.Pptx);
} finally {
presentation.dispose();
}
}
配列の行間隔は段落間隔とは別です。setSpaceBefore と setSpaceAfter は配列行間の間ではなく、包含する段落の前後にスペースを追加します。同様に、IMathParagraph.setJustification は数式段落全体の水平位置を設定し、配列の垂直整列設定を置き換えるものではありません。
三角関数の追加
asArgumentOfFunction を使用すると、引数が現在の要素で関数名が既知の場合に利用できます。

import com.aspose.slides.*;
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 を使用します。

import com.aspose.slides.*;
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 を使用して式を区切り記号で囲みます。複数要素を含む区切り式では、区切り文字を設定することもできます。

import com.aspose.slides.*;
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();
}
区切り記号のサイズと配置
true を setGrowToMatchOperandHeight に渡すと、オペランドが高い場合に括弧が伸長します。false を渡すと通常サイズのままです。setDelimiterShape と 2 つの MathDelimiterShape のいずれかを使用します。Centered は等号の真ん中を通る数式軸上で括弧を中央に配置し、Match は囲まれた内容に合わせて整列させます。これらの設定は区切り記号だけに影響し、内部の式は変わりません。
例では、分数を x の指数に配置し、式が基線上部に広がります。4 つの方程式は同じ括弧とフォント設定を使用しています。
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
for (int row = 0; row < 2; row++) {
for (int column = 0; column < 2; column++) {
IMathFraction fraction = new MathematicalText("a + b").divide("c + d");
IMathSuperscriptElement power = new MathematicalText("x").setSuperscript(fraction);
IMathDelimiter delimiter = power.enclose('(', ')');
delimiter.setGrowToMatchOperandHeight(row == 0);
delimiter.setDelimiterShape(column == 0 ? MathDelimiterShape.Centered : MathDelimiterShape.Match);
IAutoShape mathShape = slide.getShapes().addMathShape(20 + 330 * column, 20 + 120 * row, 300, 100);
MathPortion portion = (MathPortion) mathShape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
portion.getPortionFormat().setFontHeight(32);
IMathBlock equation = new MathematicalText("y = ").join(delimiter);
portion.getMathParagraph().add(equation);
}
}
presentation.save("delimiter-sizing.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
PowerPoint で行を比較して伸長の効果を確認してください。上段では、括弧が指数を囲むように伸長し、両形状で同様に見えます。下段では伸長がオフです。左側の小さな括弧(Centered)は等号に合わせ、右側(Match)は式の上に高く配置されています。

枠付きボックスの追加
分数に対して toBorderBox を呼び出すと、式全体を枠で囲みます。返される IMathBorderBox はデフォルトで 4 側すべてを表示します。setHideTop, setHideBottom, setHideLeft, そして setHideRight を使用して個別の側を非表示にできます。
例では、同じ分数のコピーをスライドに 6 つ配置します。取り消し線を示すために、4 つの側すべてを非表示にし、上昇対角線(↗)の場合は setStrikethroughBottomLeftToTopRight に true を、下降対角線(↘)の場合は setStrikethroughTopLeftToBottomRight に true を渡します。また、任意の組み合わせで setStrikethroughHorizontal や setStrikethroughVertical を使用できます。
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
presentation.getSlideSize().setSize(540, 220, SlideSizeScaleType.DoNotScale);
ISlide slide = presentation.getSlides().get_Item(0);
class FractionBuilder {
IMathBorderBox addFraction(float x, float y, boolean hideBorder) {
IMathFraction fraction = new MathematicalText("x+1").divide("y+2");
IMathBorderBox box = fraction.toBorderBox();
box.setHideTop(hideBorder);
box.setHideBottom(hideBorder);
box.setHideLeft(hideBorder);
box.setHideRight(hideBorder);
IAutoShape shape = slide.getShapes().addMathShape(x, y, 160, 90);
MathPortion portion = (MathPortion) shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
portion.getPortionFormat().setFontHeight(28);
portion.getMathParagraph().add(new MathBlock(box));
return box;
}
}
FractionBuilder builder = new FractionBuilder();
builder.addFraction(10, 10, false);
IMathBorderBox partial = builder.addFraction(190, 10, false);
partial.setHideTop(true);
partial.setHideRight(true);
builder.addFraction(370, 10, true).setStrikethroughBottomLeftToTopRight(true);
builder.addFraction(10, 120, true).setStrikethroughTopLeftToBottomRight(true);
builder.addFraction(190, 120, true).setStrikethroughHorizontal(true);
builder.addFraction(370, 120, false).setStrikethroughVertical(true);
presentation.save("border-box.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}

この分数では、水平線が分数バーと重なります。垂直線は分子と分母の両方を横切り、周囲の枠は表示されたままです。これらの線は式全体を装飾します。通常のテキストの取り消し線は文字ランに適用されます。PowerPoint でプレゼンテーションを再度開くと、分子と分母は個別に編集できます。
Phantom 要素で可視性とレイアウトを制御
MathPhantom で式をラップすると、可視性と式へのスペース貢献を個別に制御できます。IMathPhantom のメソッドは以下を制御します:
| メソッド | 効果 |
|---|---|
| setShow | false を渡すと式を非表示にします。以下の対応するメソッドが true を受け取らない限り、寸法はレイアウトに寄与し続けます。 |
| setZeroWidth | 式の横幅を 0 とみなし、隣接要素がより近づくようにします。 |
| setZeroAsc | ベースライン上の高さ(上昇部)を 0 とみなし、隣接要素上部の確保スペースを減らします。 |
| setZeroDesc | ベースライン下の深さ(下降部)を 0 とみなし、式下の分数バーや他のコンテンツを隣接要素に近づけます。 |
ベースラインは通常の数学文字が配置される基準線です。コンテンツを非表示にしても寸法は残り、寸法を 0 にしても式自体は削除されません。基になる式は getBase で取得可能です。true を setShow に渡すと再表示され、寸法設定は保持されたままです。
以下の例は、横に並んだ 5 つのバリアントを作成します:表示された分数、スペースを確保したまま非表示の分数、そして 3 つの次元を 0 にした非表示分数です。各バリアントは同じ周囲式と 24 ポイントの Cambria Math を使用しています。すべての方程式シェイプは同じ上位置にあり、グレーガイドで示されています。外側の分数の分子に Phantom を配置すると、縦のレイアウト変化が見やすくなります。
import com.aspose.slides.*;
import java.awt.Color;
class MathTextFactory {
MathematicalText createText(String value) {
MathematicalText text = new MathematicalText(value);
text.getFormat().setFontHeight(24);
text.getFormat().setLatinFont(new FontData("Cambria Math"));
return text;
}
}
MathTextFactory factory = new MathTextFactory();
Presentation presentation = new Presentation();
try {
presentation.getSlideSize().setSize(1000, 180, SlideSizeScaleType.DoNotScale);
ISlide slide = presentation.getSlides().get_Item(0);
String[] labels = { "Visible", "Hidden: reserves space", "Hidden: ZeroWidth", "Hidden: ZeroAsc", "Hidden: ZeroDesc" };
IAutoShape guide = slide.getShapes().addAutoShape(ShapeType.Line, 10, 60, 980, 0);
guide.getLineFormat().getFillFormat().setFillType(FillType.Solid);
guide.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.LIGHT_GRAY);
for (int i = 0; i < labels.length; i++) {
int left = 10 + i * 200;
IAutoShape label = slide.getShapes().addAutoShape(ShapeType.Rectangle, left, 10, 190, 30);
label.getFillFormat().setFillType(FillType.NoFill);
label.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
label.getTextFrame().setText(labels[i]);
IPortionFormat labelFormat = label.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat();
labelFormat.setFontHeight(14);
labelFormat.getFillFormat().setFillType(FillType.Solid);
labelFormat.getFillFormat().getSolidFillColor().setColor(Color.BLACK);
MathematicalText fractionNumerator = factory.createText("a+b");
MathematicalText fractionDenominator = factory.createText("c+d");
MathFraction fraction = new MathFraction(fractionNumerator, fractionDenominator);
MathPhantom phantom = new MathPhantom(fraction);
phantom.setShow(i == 0);
phantom.setZeroWidth(i == 2);
phantom.setZeroAsc(i == 3);
phantom.setZeroDesc(i == 4);
MathematicalText leftText = factory.createText("x+");
MathematicalText rightText = factory.createText("+y");
IMathBlock numerator = new MathBlock(leftText).join(phantom).join(rightText);
MathematicalText denominator = factory.createText("z");
MathFraction equation = new MathFraction(numerator, denominator);
IAutoShape shape = slide.getShapes().addMathShape(left, 60, 190, 100);
shape.getTextFrame().getTextFrameFormat().setAnchoringType(TextAnchorType.Top);
IMathPortion mathPortion = (IMathPortion) shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
IMathParagraph mathParagraph = mathPortion.getMathParagraph();
mathParagraph.add(new MathBlock(equation));
}
presentation.save("math-phantom.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}

項のグループ化
group を使用して、式の上または下にグループ文字を配置します。限界を追加してグループ化された項にラベルを付けます。

import com.aspose.slides.*;
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 は数式要素の上にバーを配置します。

import com.aspose.slides.*;
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();
}
既存の方程式内のテキスト書式設定
既存の方程式全体のテキスト書式を設定するには、各数式ブロックの要素ツリーを走査します。IMathElement.getChildren は直下の子要素のみを返すため、再帰的に呼び出して分数、添え字、根号、その他の入れ子構造内のテキストに到達します。開始要素が MathematicalText の場合も、現在の要素と子要素の両方をチェックしてください。
この例は、input.pptx に z = xᵢ / ∛y のような方程式が含まれていると想定しています。この方程式は、分子に下付き文字、分母に根号を含む分数です。ブロックのトップレベル要素だけを処理すると、分数内部のテキストが見逃されます。例では、スライドのテキストフレーム内の数式部分を検索し、IMathPortion.getMathParagraph でブロックにアクセスし、setFontHeight を使用して各テキスト要素の getFormat メソッドでフォントサイズを 28 ポイントに設定します。
import com.aspose.slides.*;
class MathTextFormatter {
void formatMathText(IMathElement element) {
if (element instanceof MathematicalText) {
MathematicalText text = (MathematicalText) element;
text.getFormat().setFontHeight(28);
}
for (IMathElement child : element.getChildren()) {
formatMathText(child);
}
}
}
MathTextFormatter formatter = new MathTextFormatter();
Presentation presentation = new Presentation("input.pptx");
try {
for (ISlide slide : presentation.getSlides()) {
for (IShape shape : slide.getShapes()) {
if (!(shape instanceof IAutoShape)) {
continue;
}
IAutoShape autoShape = (IAutoShape) shape;
if (autoShape.getTextFrame() == null) {
continue;
}
for (IParagraph paragraph : autoShape.getTextFrame().getParagraphs()) {
for (IPortion portion : paragraph.getPortions()) {
if (portion instanceof IMathPortion) {
IMathPortion mathPortion = (IMathPortion) portion;
for (IMathBlock block : mathPortion.getMathParagraph()) {
formatter.formatMathText(block);
}
}
}
}
}
}
presentation.save("formatted-equations.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 |
よくある質問
既存の PowerPoint の方程式を編集できますか?
はい。プレゼンテーションを開き、MathPortion を含むシェイプを見つけ、getMathParagraph を呼び出し、その段落内の数式ブロックを更新します。
方程式は編集可能な PowerPoint の数式として保存されますか?
はい。PPTX に保存すると、Aspose.Slides は方程式を編集可能な Office 数式コンテンツとして書き込みます。
方程式を LaTeX にエクスポートできますか?
はい。方程式の IMathParagraph をその IMathPortion から取得し、IMathParagraph.toLatex を呼び出すと直接エクスポートできます。完全な例については、Java でのプレゼンテーションからの数式エクスポート(LaTeX へ) を参照してください。