Android で PowerPoint プレゼンテーションに数式を追加
概要
PowerPoint では、数式やフォーミュラを書き込み、プレゼンテーションに表示することが可能です。これを実現するために、PowerPoint ではさまざまな数学記号が表現でき、テキストや数式に追加できます。そのために PowerPoint の数式コンストラクタが使用され、次のような複雑な数式を作成できます。
- Math Fraction
- Math Radical
- Math Function
- Limits and log functions
- N-ary operations
- Matrix
- Large operators
- Sin, cos functions
PowerPoint で数式を追加するには、Insert -> Equation メニューを使用します。

これにより、XML 形式の数式テキストが作成され、PowerPoint で以下のように表示されます。

PowerPoint は多数の数学記号をサポートしていますが、複雑な数式を作成すると見栄えが十分でないことがあります。頻繁に数式付きプレゼンテーションを作成するユーザーは、サードパーティ製のソリューションを利用して見栄えの良い数式を作成しています。
Aspose.Slide API を使用すれば、C# で PowerPoint の数式をプログラムから操作できます。新しい数式を作成したり、既存の数式を編集したりできます。数式構造を画像へエクスポートする機能も部分的にサポートされています。
数式の作成方法
数学要素は任意のレベルの入れ子構造で数学構造を構築するために使用されます。線形に並んだ数学要素のコレクションは、MathBlock クラスで表されます。MathBlock クラスは、分離された数式、フォーミュラ、または方程式を表します。MathPortion は数学テキストを保持する要素で、Portion と混同しないでください。MathParagraph は複数の MathBlock を操作できます。上記のクラスは Aspose.Slides API を通じて PowerPoint の数式を操作するためのキーになります。
以下の数式を Aspose.Slides API で作成する例をご覧ください。

スライドに数式を追加するには、まず数式テキストを保持するシェイプを追加します。
Presentation pres = new Presentation();
try {
IAutoShape mathShape = pres.getSlides().get_Item(0).getShapes().addMathShape(0, 0, 720, 150);
} finally {
if (pres != null) pres.dispose();
}
作成後、シェイプにはデフォルトで 1 つの段落と 1 つの MathPortion が含まれます。MathPortion は内部に数学テキストを保持する要素です。MathPortion から MathParagraph 変数へアクセスします。
IMathParagraph mathParagraph = ((MathPortion)mathShape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0)).getMathParagraph();
MathParagraph は数学ブロック(MathBlock)の読み取り、追加、編集、削除を行うことができます。たとえば分数を作成してスライドに配置するには次のようにします。
IMathFraction fraction = new MathematicalText("x").divide("y");
mathParagraph.add(new MathBlock(fraction));
各数学要素は IMathElement インターフェイスを実装するクラスで表されます。このインターフェイスは、数学式を簡単に作成するための多くのメソッドを提供します。たとえば、次のコードでピタゴラスの定理を 1 行で生成できます。
IMathBlock mathBlock = new MathematicalText("c")
.setSuperscript("2")
.join("=")
.join(new MathematicalText("a").setSuperscript("2"))
.join("+")
.join(new MathematicalText("b").setSuperscript("2"));
IMathElement の操作は、MathBlock を含むすべての要素タイプで実装されています。
完全なサンプルコードは以下のとおりです。
Presentation pres = new Presentation();
try {
IAutoShape mathShape = pres.getSlides().get_Item(0).getShapes().addMathShape(0, 0, 720, 150);
IMathParagraph mathParagraph = ((MathPortion)mathShape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0)).getMathParagraph();
IMathFraction fraction = new MathematicalText("x").divide("y");
mathParagraph.add(new MathBlock(fraction));
IMathBlock mathBlock = new MathematicalText("c")
.setSuperscript("2")
.join("=")
.join(new MathematicalText("a").setSuperscript("2"))
.join("+")
.join(new MathematicalText("b").setSuperscript("2"));
mathParagraph.add(mathBlock);
pres.save("math.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
数学要素の種類
数学式は数学要素のシーケンスから構成されます。要素のシーケンスは数学ブロックで表現され、要素の引数はツリー構造のように入れ子になります。
多数の数学要素タイプがあり、各要素は別の要素に集約できます。つまり要素は他の要素のコンテナであり、ツリー構造を形成します。数学テキストに他の要素を含まない最もシンプルな要素もあります。
各数学要素は IMathElement インターフェイスを実装しており、共通の数式操作を使用できます。
MathematicalText クラス
MathematicalText クラスは、すべての数学構造の基礎要素である数学テキストを表します。オペランド、演算子、変数、その他の直線テキストを表すことができます。
例: 𝑎=𝑏+𝑐
MathFraction クラス
MathFraction クラスは、分子と分母で構成され、分数棒で区切られた分数オブジェクトを表します。分数棒は水平または斜めに設定可能です。また、分数棒なしで要素を上下に配置するスタック関数としても使用できます。
例:

MathRadical クラス
MathRadical クラスは、基底と省略可能な次数からなる根(ラジカル)関数を表します。
例:

MathFunction クラス
MathFunction クラスは引数を取る関数を表します。プロパティは getName(関数名)と getBase(関数の引数)です。
例:

MathNaryOperator クラス
MathNaryOperator クラスは、総和や積分などの N 元数学オブジェクトを表します。演算子、基底(またはオペランド)、省略可能な上限・下限で構成されます。例として総和、和集合、積集合、積分があります。
このクラスは加算や減算などの単純演算子を含みません。単純演算子は MathematicalText で表されます。
例:

MathLimit クラス
MathLimit クラスは上限または下限を作成します。ベースライン上のテキストと、すぐ上または下に配置される縮小テキストで構成されます。単語 “lim” は含まず、式の上部または下部にテキストを配置できます。たとえば、

は MathFunction と MathLimit の組み合わせで次のように作成します。
MathLimit funcName = new MathLimit(new MathematicalText("lim"), new MathematicalText("𝑥→∞"));
MathFunction mathFunc = new MathFunction(funcName, new MathematicalText("𝑥"));
MathSubscriptElement, MathSuperscriptElement, MathRightSubSuperscriptElement, MathLeftSubSuperscriptElement クラス
- MathSubscriptElement
- MathSuperscriptElement
- MathRightSubSuperscriptElement
- MathLeftSubSuperscriptElement
これらのクラスは下付きインデックスまたは上付きインデックスを指定します。左側または右側に同時に下付き・上付きインデックスを設定できますが、右側単独の下付き・上付きインデックスのみがサポートされます。MathSubscriptElement は数値の次数を設定することもできます。
例:

MathMatrix クラス
MathMatrix クラスは、子要素が行と列に配置された行列オブジェクトを表します。行列にはデフォルトで区切り記号が付かないため、角括弧などで囲む場合は IMathDelimiter を使用します。null 引数を渡すと行列内に空白を作れます。
例:

MathArray クラス
MathArray クラスは、垂直に配置された方程式や任意の数学オブジェクトの配列を表します。
例:

数学要素の書式設定
-
MathBorderBox クラス: IMathElement の周囲に長方形やその他の枠線を描画します。
例:
-
MathBox クラス: 数学要素の論理的な箱詰め(パッケージ化)を指定します。たとえば、箱詰めされたオブジェクトは演算子エミュレータとして使用したり、改行を防止したりできます。
-
MathDelimiter クラス: 開始文字と終了文字(括弧、波かっこ、角括弧、縦棒など)で囲む区切り記号オブジェクトを指定し、内部に 1 つ以上の数学要素を含めます。例: (𝑥2); [𝑥2|𝑦2]。
例:
-
MathAccent クラス: 基底と結合アクセント記号からなるアクセント関数を指定します。
例: 𝑎́。 -
MathBar クラス: 基底引数と上バーまたは下バーからなるバー関数を指定します。
例:
-
MathGroupingCharacter クラス: 式の上または下に配置するグルーピング記号を指定し、要素間の関係を強調します。
例:
数学演算
各数学要素および数学式(MathBlock)は IMathElement インターフェイスを実装しています。既存の構造に対して操作を行い、より複雑な式を構成できます。すべての操作は、IMathElement または文字列のいずれかを引数に取ります。文字列引数が使用された場合、MathematicalText のインスタンスが暗黙的に作成されます。利用可能な数式操作は以下のとおりです。
Join メソッド
数学要素を結合し、数学ブロックを作成します。例:
IMathElement element1 = new MathematicalText("x");
IMathElement element2 = new MathematicalText("y");
IMathBlock block = element1.join(element2);
Divide メソッド
- divide(String)
- divide(IMathElement)
- divide(String, MathFractionTypes)
- divide(IMathElement, MathFractionTypes)
指定した分子と分母で特定のタイプの分数を作成します。例:
IMathElement numerator = new MathematicalText("x");
IMathFraction fraction = numerator.divide("y", MathFractionTypes.Linear);
Enclose メソッド
指定した文字(例えば括弧)で要素を囲みます。
/**
* <p>
* Enclose a math element in parenthesis
* </p>
*/
public IMathDelimiter enclose();
/**
* <p>
* Encloses this element in specified characters such as parenthesis or another characters as framing
* </p>
*/
public IMathDelimiter enclose(char beginningCharacter, char endingCharacter);
例:
IMathDelimiter delimiter = new MathematicalText("x").enclose('[', ']');
IMathDelimiter delimiter2 = new MathematicalText("elem1").join("elem2").enclose();
Function メソッド
現在のオブジェクトを関数名として、引数付き関数を作成します。
/**
* <p>
* Takes a function of an argument using this instance as the function name
* </p>
*/
public IMathFunction function(IMathElement functionArgument);
/**
* <p>
* Takes a function of an argument using this instance as the function name
* </p>
*/
public IMathFunction function(String functionArgument);
例:
IMathFunction func = new MathematicalText("sin").function("x");
AsArgumentOfFunction メソッド
- asArgumentOfFunction(String)
- asArgumentOfFunction(IMathElement)
- asArgumentOfFunction(MathFunctionsOfOneArgument)
- asArgumentOfFunction(MathFunctionsOfTwoArguments, IMathElement)
- asArgumentOfFunction(MathFunctionsOfTwoArguments, String)
現在のインスタンスを引数として、指定した関数を適用します。
- 関数名を文字列で指定(例: “cos”)
- 列挙体 MathFunctionsOfOneArgument または MathFunctionsOfTwoArguments の定義済み値を使用
- IMathElement のインスタンスを使用
例:
MathLimit funcName = new MathLimit(new MathematicalText("lim"), new MathematicalText("𝑛→∞"));
IMathFunction func1 = new MathematicalText("2x").asArgumentOfFunction(funcName);
IMathFunction func2 = new MathematicalText("x").asArgumentOfFunction("sin");
IMathFunction func3 = new MathematicalText("x").asArgumentOfFunction(MathFunctionsOfOneArgument.Sin);
IMathFunction func4 = new MathematicalText("x").asArgumentOfFunction(MathFunctionsOfTwoArguments.Log, "3");
SetSubscript, SetSuperscript, SetSubSuperscriptOnTheRight, SetSubSuperscriptOnTheLeft メソッド
- setSubscript(String)
- setSubscript(IMathElement)
- setSuperscript(String)
- setSuperscript(IMathElement)
- setSubSuperscriptOnTheRight(String, String)
- setSubSuperscriptOnTheRight(IMathElement, IMathElement)
- setSubSuperscriptOnTheLeft(String, String)
- setSubSuperscriptOnTheLeft(IMathElement, IMathElement)
下付きと上付きインデックスを設定します。左側または右側に同時に設定可能ですが、右側単独の設定のみがサポートされています。Superscript は数の次数を設定する際にも使用できます。
例:
IMathLeftSubSuperscriptElement script = new MathematicalText("y").setSubSuperscriptOnTheLeft("2x", "3z");
Radical メソッド
指定した次数の根(ラジカル)を作成します。
例:
IMathRadical radical = new MathematicalText("x").radical("3");
SetUpperLimit と SetLowerLimit メソッド
上限または下限を設定します。上限・下限は基底に対する相対位置を示します。
例として次の式を考えます。

このような式は MathFunction と MathLimit の組み合わせで次のように作成できます。
IMathFunction mathExpression = new MathematicalText("lim").setLowerLimit("x→∞").function("x");
Nary と Integral メソッド
- nary(MathNaryOperatorTypes, IMathElement, IMathElement)
- nary(MathNaryOperatorTypes, String, String)
- integral(MathIntegralTypes)
- integral(MathIntegralTypes, IMathElement, IMathElement)
- integral(MathIntegralTypes, String, String)
- integral(MathIntegralTypes, IMathElement, IMathElement, MathLimitLocations)
- integral(MathIntegralTypes, String, String, MathLimitLocations)
nary と integral はどちらも IMathNaryOperator 型のオブジェクトを返します。nary メソッドでは、MathNaryOperatorTypes 列挙体で演算子の種類(総和、和集合など)を指定し、積分は含まれません。integral メソッドは積分専用で、MathIntegralTypes 列挙体で積分の種類を指定します。
例:
IMathBlock baseArg = new MathematicalText("x").join(new MathematicalText("dx").toBox());
IMathNaryOperator integral = baseArg.integral(MathIntegralTypes.Simple, "0", "1");
ToMathArray メソッド
toMathArray は要素を縦方向の配列に配置します。 MathBlock インスタンスに対して呼び出すと、子要素すべてが配列に配置されます。
例:
IMathArray arrayFunction = new MathematicalText("x").join("y").toMathArray();
書式設定操作: Accent, Overbar, Underbar, Group, ToBorderBox, ToBox
- accent メソッドは要素の上にアクセント記号(文字)を付加します。
- overbar と underbar メソッドはそれぞれ上部または下部にバーを付加します。
- group メソッドはグルーピング文字(例: 下側の波かっこ)で要素をまとめます。
- toBorderBox メソッドは要素を枠付きボックスに配置します。
- toBox メソッドは視覚的でないボックス(論理的グルーピング)に配置します。
例:
IMathAccent accent = new MathematicalText("x").accent('\u0303');
IMathBar bar = new MathematicalText("x").overbar();
IMathGroupingCharacter groupChr = new MathematicalText("x").join("y").join("z").group('\u23E1', MathTopBotPositions.Bottom, MathTopBotPositions.Top);
IMathBorderBox borderBox = new MathematicalText("x+y+z").toBorderBox();
IMathBox boxedOperator = new MathematicalText(":=").toBox();
FAQ
PowerPoint のスライドに数式を追加するにはどうすればよいですか?
数式シェイプオブジェクトを作成すると、内部に数式部分が自動的に含まれます。その後、MathPortion から MathParagraph を取得し、そこに MathBlock オブジェクトを追加します。
複雑な入れ子構造の数式を作成できますか?
はい。Aspose.Slides は MathBlock を入れ子にすることで複雑な数式を作成できます。各数学要素は IMathElement を実装しており、Join、Divide、Enclose などの操作で要素を組み合わせてより複雑な構造を構築できます。
既存の数式を更新または変更するにはどうすればよいですか?
既存の MathBlock は MathParagraph を介して取得できます。Join、Divide、Enclose などのメソッドを使用して個々の要素を変更し、編集後にプレゼンテーションを保存すれば変更が反映されます。