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

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

Aspose.Slides は、3 つの主要オブジェクトを使用してその数式テキストを構築します:
- 数式シェイプは、addMathShape で作成され、数式を含むシェイプです。
- MathPortion はシェイプのテキストフレーム内に数式コンテンツを格納します。
- MathParagraph は 1 つ以上の MathBlock オブジェクトを含みます。
以下の例のほとんどは、MathematicalText と IMathElement のフルエントメソッドを使用して、コードを短く読みやすく保っています。
MathML エクスポートシナリオについては、Export Math Equations from Presentations on Android を参照してください。
数式の作成
この例では、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 を呼び出して、数式ブロックまたは数式要素を追加します。分数の追加
分数を作成するには、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();
}
関数とリミットの追加
sin(x)、log(x)、またはカスタム関数名などの関数には、asArgumentOfFunction または function を使用します。リミットの場合は、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→∞")
.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();
}
方程式配列の追加
整列された方程式や縦にスタックした式が必要な場合は、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 つ作成します。すべての数式テキストは Cambria Math 24 ポイントです。配列の配置のみが変わります。
import com.aspose.slides.*;
import android.graphics.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 と 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 はデフォルトで四辺すべてを表示します。個別の辺を削除するには、setHideTop、setHideBottom、setHideLeft、setHideRight を使用します。
例では、同じ分数を 1 スライドに 6 つ配置しています。取り消し線を示すため、すべての辺を非表示にし、上向き対角線 (↗) 用に setStrikethroughBottomLeftToTopRight に true を渡すか、下向き対角線 (↘) 用に setStrikethroughTopLeftToBottomRight を使用します。また、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 | 式の横幅をゼロとして扱い、隣接要素をより近づけます。 |
| setZeroAsc | ベースライン上部の高さ(アセント)をゼロとして扱い、隣接要素上部の余白を減らします。 |
| setZeroDesc | ベースライン下部の深さ(ディセント)をゼロとして扱い、分数バーや式下部のコンテンツを隣接要素に近づけます。 |
ベースラインは通常の数学文字が配置される基準線です。コンテンツを非表示にしても寸法は残り、寸法をゼロにしても式は削除されません。基になる式は getBase で取得できます。true を setShow に渡すと再表示され、寸法設定はそのままです。
import com.aspose.slides.*;
import android.graphics.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.LTGRAY);
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();
}
既存の式内テキストの書式設定
既存の式全体のテキストを書式設定するには、各 MathBlock の要素ツリーを走査します。IMathElement.getChildren は直接の子要素のみを返すため、再帰的に呼び出して分数、添字・指数、根号、その他の入れ子構造内のテキストに到達します。現在の要素とその子要素の両方を確認し、開始要素が MathematicalText の場合も含めます。
この例では、input.pptx に z = xᵢ / ∛y のような式が含まれていると想定します。分子に添字、分母に根号を持つ分数です。ブロックのトップレベル要素だけを処理すると、分数内部のテキストが見逃されます。例ではスライドのテキストフレームから数式部分を検出し、IMathPortion.getMathParagraph でブロックにアクセスし、各テキスト要素の getFormat メソッドを介して setFontHeight によりフォントサイズを 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 |
FAQ
既存の PowerPoint の数式を編集できますか?
はい。プレゼンテーションを開き、MathPortion を含むシェイプを見つけ、getMathParagraph を呼び出して、その段落の数式ブロックを更新します。
数式は編集可能な PowerPoint の数式として保存されますか?
はい。PPTX として保存すると、Aspose.Slides は数式を編集可能な Office 数式コンテンツとして書き込みます。
数式を LaTeX にエクスポートできますか?
はい。式の IMathParagraph をその IMathPortion から取得し、IMathParagraph.toLatex を呼び出して直接エクスポートします。完全な例については、Export Math Equations from Presentations in Android via Java を参照してください。