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

概要

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

PowerPoint では、ユーザーは通常 Insert > Equation から数式を追加します:

PowerPoint の挿入タブで数式コマンドが選択された状態

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

編集可能な数式が含まれる PowerPoint スライド

Aspose.Slides は次の 3 つの主要オブジェクトを通じてその数式テキストを構築します:

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

以下のほとんどの例は MathematicalText と IMathElement のフルエントメソッドを使用して、コードを短く読みやすく保っています。

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

数式の作成

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

c² = a² + b² の数式

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 120);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;

var 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);

数式ブロックの結合

数式の一部が IMathBlock オブジェクトとして個別に構築されている場合、JoinBlock を使用して結合します。この例では左ブロックに c² =、右ブロックに a² + b² を構築し、SetSuperscript が 3 つのべき乗すべてに編集可能な上付き文字を作成します。

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var shape = slide.Shapes.AddMathShape(20, 20, 600, 100);
var mathParagraph = ((IMathPortion)shape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;

var leftBlock = new MathBlock();
leftBlock.Add(new MathematicalText("c").SetSuperscript("2"));
leftBlock.Add(new MathematicalText("="));

var rightBlock = new MathBlock();
rightBlock.Add(new MathematicalText("a").SetSuperscript("2"));
rightBlock.Add(new MathematicalText("+"));
rightBlock.Add(new MathematicalText("b").SetSuperscript("2"));

var combinedBlock = leftBlock.JoinBlock(rightBlock);
mathParagraph.Add(combinedBlock);

presentation.Save("equation.pptx", SaveFormat.Pptx);

JoinBlock は leftBlock 自身を返し、現在両方の部分を順番に含み、rightBlock は空になります。結合されたブロックは IMathParagraph に一度だけ追加します。equation.pptx を再度開くと、段落は c² = a² + b² を表す 1 つのブロックを保持し、3 つのべき乗はすべて数式上付き文字として保持されます。

leftBlock.Add(rightBlock) のように Add を呼び出すと、ブロックを直接別のブロック内部に入れ子にしようとして InvalidOperationException がスローされます。

分数の追加

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

x で割った 1 の斜め分数

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 100);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;

var fraction = new MathematicalText("1")
    .Divide("x", MathFractionTypes.Skewed);

mathParagraph.Add(new MathBlock(fraction));

presentation.Save("fraction.pptx", SaveFormat.Pptx);

スタック型分数の場合は MathFractionTypes.Bar を使用します:

using Aspose.Slides.MathText;

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

根号の追加

Radical を使用して平方根、立方根、その他の根号を作成します。現在の要素が基底となり、引数が次数になります。

x が根号記号の下にある n 番目の根号式

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 100);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;

var radical = new MathematicalText("x")
    .Radical("n");

mathParagraph.Add(new MathBlock(radical));

presentation.Save("radical.pptx", SaveFormat.Pptx);

平方根の次数の表示/非表示

平方根は明示的に 2 を表示するか、従来の √x 表記を使用できます。IMathRadical の HideDegree プロパティで形式を制御します。どちらの形式でも次数 2 と被根式 x は編集可能な数式に保持されます。

左が HideDegree=false、右が true の同一平方根

この例では同じ平方根を 2 回保存します。visible-degree.pptx では 2 が表示され、hidden-degree.pptx では非表示になります。

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var radical = new MathematicalText("x").Radical("2");

var shape = slide.Shapes.AddMathShape(20, 20, 200, 100);
var portion = (IMathPortion)shape.TextFrame.Paragraphs[0].Portions[0];
portion.MathParagraph.Add(new MathBlock(radical));

radical.HideDegree = false;
presentation.Save("visible-degree.pptx", SaveFormat.Pptx);

radical.HideDegree = true;
presentation.Save("hidden-degree.pptx", SaveFormat.Pptx);

関数とリミットの追加

AsArgumentOfFunction または Function を使用して sin(x)、log(x)、またはカスタム関数名を表現します。リミットの場合は MathLimit に lim を入れるか、SetLowerLimit を使用します。

x が無限大に近づくリミット

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 100);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;

var limit = new MathematicalText("lim")
    .SetLowerLimit("x→∞")
    .Function("x");

mathParagraph.Add(new MathBlock(limit));

presentation.Save("functions-and-limits.pptx", SaveFormat.Pptx);

カスタム関数名の場合、現在の要素を関数名に設定し、引数と共に Function を呼び出します:

using Aspose.Slides.MathText;

var customFunction = new MathematicalText("f").Function("x + 1");

N 進演算子と積分の追加

Nary を使用して総和、和集合、積集合、その他の大きな演算子を作成します。Integral を使用して積分を作成します。両方のメソッドで下限と上限を設定できます。

下限と上限付きの総和

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 120);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;

var summationBase = new MathematicalText("x")
    .SetSuperscript("k")
    .Join(new MathematicalText("a").SetSuperscript("n-k"));

var summation = summationBase.Nary(MathNaryOperatorTypes.Summation, "k=0", "n");

mathParagraph.Add(new MathBlock(summation));

presentation.Save("nary-operators.pptx", SaveFormat.Pptx);

N 進演算子はオプションの上下限を持つ大きな演算子向けです。+、-、= のような単純な演算子は通常 MathematicalText として追加し、式に結合します。

積分の場合は Integral を使用します:

using Aspose.Slides.MathText;

var integralBase = new MathematicalText("x").Join(new MathematicalText("dx").ToBox());
var integral = integralBase.Integral(MathIntegralTypes.Simple, "0", "1");

複合演算子の作成

== を 2 つのオペランド間の単一演算子として使用するには、両方の等号を含む MathematicalText に対して ToBox を呼び出します。結果の IMathBox で OperatorEmulator を true に設定すると、グループ全体が数学的演算子として機能します。デフォルトは false です。

NoBreak はボックス内の改行を制御します。この例ではデフォルト値の true に明示的に設定し、2 つの等号を一緒に保ちます。式全体はまだ改行可能です。

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var shape = slide.Shapes.AddMathShape(20, 20, 300, 60);
var portion = (IMathPortion)shape.TextFrame.Paragraphs[0].Portions[0];

IMathBox compoundOperator = new MathematicalText("==").ToBox();
compoundOperator.OperatorEmulator = true;
compoundOperator.NoBreak = true;

var equation = new MathematicalText("a").Join(compoundOperator).Join(new MathematicalText("b"));
portion.MathParagraph.Add(equation);

presentation.Save("compound-operator.pptx", SaveFormat.Pptx);

行列の追加

MathMatrix を使用して行と列を作成します。行列はデフォルトで括弧を含まないため、丸括弧、角括弧、波括弧が必要な場合は外側に囲んでください。

空白セルを含む 2 行の数式行列

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 120);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;

var matrix = new MathMatrix(2, 3);
matrix[0, 0] = new MathematicalText("1");
matrix[0, 1] = new MathematicalText("x");
matrix[1, 0] = new MathematicalText("x");
matrix[1, 1] = new MathematicalText("2");
matrix[1, 2] = new MathematicalText("y");

mathParagraph.Add(new MathBlock(matrix));

presentation.Save("matrix.pptx", SaveFormat.Pptx);

既存行列の編集

たとえば、線形代数のレッスンが matrix.pptx の係数行列から始まるとします。次の演習では最初の式を保持し、2 番目を 2 つの新しい式に置き換え、変数のセットを更新します。既存の行列は IMathMatrix を通じて開き、まだ有効な係数を保持します。

元の 2 行係数行列 (1, x, 空白) と (x, 2, y)

行と列のインデックスは 0 から始まり、各呼び出し時点の行列を参照します。インデックス 1 の前に行を挿入すると、元の第 2 行がインデックス 2 に移動します。インデックス 2 の後に挿入すると行が追加されます。インデックス 2 の行を削除すると、元の第 2 式が削除され、追加された行が 1 つ上にシフトします。列も同様にシフトします。

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation("matrix.pptx");

var shape = (IAutoShape)presentation.Slides[0].Shapes[0];
var portion = (IMathPortion)shape.TextFrame.Paragraphs[0].Portions[0];
var matrix = (IMathMatrix)portion.MathParagraph[0][0];

matrix.InsertRowBefore(1);
matrix.InsertRowAfter(2);
matrix.DeleteRow(2);

matrix.InsertColumnBefore(1);
matrix.InsertColumnAfter(3);
matrix.DeleteColumn(3);

// New cells are initially null. Set the nonzero coefficients first.
matrix[1, 0] = new MathematicalText("12");
matrix[1, 1] = new MathematicalText("1");
matrix[2, 2] = new MathematicalText("10");
matrix[2, 3] = new MathematicalText("1");
for (var row = 0; row < matrix.RowCount; row++)
{
    for (var column = 0; column < matrix.ColumnCount; column++)
    {
        matrix[row, column] ??= new MathematicalText("0");
    }
}

matrix.SetColumnAlignment(0, MathHorizontalAlignment.Left);
matrix.SetColumnsAlignment(1, 3, MathHorizontalAlignment.Right);

presentation.Save("updated-matrix.pptx", SaveFormat.Pptx);

SetColumnAlignment は最初の列を左揃えにします。SetColumnsAlignment はインデックス 1 から始まる 3 つの連続列を右揃えにします。updated-matrix.pptx では行列に 3 つの式と 4 つの変数があり、元の第 1 行の係数 1 と x が第 1 列と第 3 列に表示されます。

更新後の 3 行行列 (1, 0, x, 0; 12, 1, 0, 0; 0, 0, 10, 1)

行列の行間と列間の設定

この例は、最小列幅 48 ポイント、列間 12 ポイント、行間 1.5 行の分数で構成された 2 × 2 行列を作成します。

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var shape = slide.Shapes.AddMathShape(20, 40, 300, 180);
var portion = (IMathPortion)shape.TextFrame.Paragraphs[0].Portions[0];

var matrix = new MathMatrix(2, 2);
matrix[0, 0] = new MathematicalText("1").Divide("2");
matrix[0, 1] = new MathematicalText("1").Divide("3");
matrix[1, 0] = new MathematicalText("2").Divide("3");
matrix[1, 1] = new MathematicalText("3").Divide("4");

matrix.MinColumnWidth = 960;
matrix.ColumnGapRule = MathSpacingRules.Exactly;
matrix.ColumnGap = 240;
matrix.RowGapRule = MathSpacingRules.Multiple;
matrix.RowGap = 3;

portion.MathParagraph.Add(new MathBlock(matrix));

presentation.Save("matrix-spacing.pptx", SaveFormat.Pptx);

MinColumnWidth は各列の最小幅をツイップ単位で設定します (20 ツイップ = 1 ポイント)。ColumnGap は隣接列間にスペースを追加します。これらは列の開始位置間の距離に影響します。

ColumnGapRule と RowGapRule は各方向の MathSpacingRules を選択します。Exactly の場合、両方のギャップはツイップで測定されます。Multiple の場合、ColumnGap の 1 単位は 0.5 em、RowGap の 1 単位は半行です。他の列間隔ルールは ColumnGap を無視します。

これらの設定は行列内部にのみ適用されます。段落間隔は周囲のテキストレイアウトを制御します。式配列内の行間については、Configure Row Spacing を参照してください。

式配列の追加

ToMathArray を使用すると、揃えられた式や縦に積み重ねた式の配列を作成できます。

x が y の上にある縦方向の数式配列

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 140);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;

var equationArray = new MathematicalText("x")
    .Join("y")
    .ToMathArray();

mathParagraph.Add(new MathBlock(equationArray));

presentation.Save("equation-array.pptx", SaveFormat.Pptx);

配列と周囲要素の揃え

IMathArray.BaseJustification は配列の垂直揃えを隣接する数式要素に対して制御します。以下の例では、最初の行、配列の中央、最後の行で揃えを比較します。

次の例では、3 つの同一配列を作成し、各配列は 3 行で、隣接要素は A= と =B です。すべての数式テキストは Cambria Math 24 ポイントで、配列の揃えだけが変わります。

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
presentation.SlideSize.SetSize(780, 200, SlideSizeScaleType.DoNotScale);
var slide = presentation.Slides[0];

var alignments = new[] { MathVerticalAlignment.Top, MathVerticalAlignment.Center, MathVerticalAlignment.Bottom };

for (int i = 0; i < alignments.Length; i++)
{
    var label = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 20 + i * 260, 10, 240, 30);
    label.FillFormat.FillType = FillType.NoFill;
    label.LineFormat.FillFormat.FillType = FillType.NoFill;
    label.TextFrame.Text = alignments[i].ToString();
    var labelFormat = label.TextFrame.Paragraphs[0].Portions[0].PortionFormat;
    labelFormat.FontHeight = 16;
    labelFormat.FillFormat.FillType = FillType.Solid;
    labelFormat.FillFormat.SolidFillColor.Color = System.Drawing.Color.Black;

    var mathShape = slide.Shapes.AddMathShape(20 + i * 260, 60, 240, 130);
    mathShape.TextFrame.TextFrameFormat.AnchoringType = TextAnchorType.Top;
    var mathParagraph = ((IMathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
    mathParagraph.Justification = MathJustification.LeftJustified;

    var rows = new IMathElement[] { CreateText("x+y=3"), CreateText("x-y=1"), CreateText("x=2") };
    var equationArray = new MathArray(rows);
    equationArray.BaseJustification = alignments[i];
    var equation = new MathBlock(CreateText("A=")).Join(equationArray).Join(CreateText("=B"));
    mathParagraph.Add(equation);
}

presentation.Save("equation-array-alignment.pptx", SaveFormat.Pptx);

MathematicalText CreateText(string value)
{
    var text = new MathematicalText(value);
    text.Format.FontHeight = 24;
    text.Format.LatinFont = new FontData("Cambria Math");
    return text;
}

以下の比較画像は PowerPoint で保存されたプレゼンテーションを示します。各配列の横にある A= と =B の位置に注目してください。

PowerPoint の式配列 (上揃え、中央揃え、下揃え) と同一の隣接要素

行間の構成

RowSpacingRule は配列内の行間のルールを選択します。RowSpacing は数値を提供し、明示的な間隔ルールとして使用され、プリセットルールでは無視されます。

正確な間隔の場合、値はポイントで測定されます。比例間隔の場合、値は半行単位です。例の 3 は 1.5 行に相当します。明示的な値を指定する場合は両方のプロパティを設定してください。数値だけを変更しても間隔ルールは選択されません。

以下の例は、同一の式、揃え、フォント設定で 5 つのプレゼンテーションを保存します。最初の 3 つはプリセット、残りの 2 つは明示的な値を示します。

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

var settings = new (MathRowSpacingRule Rule, uint Spacing)[]
{
    (MathRowSpacingRule.SingleLineGap, 0),
    (MathRowSpacingRule.OneAndAHalfLineGap, 0),
    (MathRowSpacingRule.TwoLineGap, 0),
    (MathRowSpacingRule.Exactly, 20),
    (MathRowSpacingRule.Multiple, 3)
};

foreach (var setting in settings)
{
    var fileName = $"equation-array-{setting.Rule}.pptx";
    using (var presentation = new Presentation())
    {
        var mathShape = presentation.Slides[0].Shapes.AddMathShape(20, 20, 600, 300);
        var paragraph = mathShape.TextFrame.Paragraphs[0];
        paragraph.ParagraphFormat.SpaceBefore = 0;
        paragraph.ParagraphFormat.SpaceAfter = 0;
        var mathParagraph = ((IMathPortion)paragraph.Portions[0]).MathParagraph;
        mathParagraph.Justification = MathJustification.LeftJustified;

        var rows = new IMathElement[] { CreateText("x+y=3"), CreateText("x-y=1"), CreateText("x=2") };
        var equationArray = new MathArray(rows);
        equationArray.BaseJustification = MathVerticalAlignment.Center;
        equationArray.RowSpacingRule = setting.Rule;
        equationArray.RowSpacing = setting.Spacing;
        var equation = new MathBlock(CreateText("A=")).Join(equationArray).Join(CreateText("=B"));
        mathParagraph.Add(equation);

        presentation.Save(fileName, SaveFormat.Pptx);
    }

}

MathematicalText CreateText(string value)
{
    var text = new MathematicalText(value);
    text.Format.FontHeight = 24;
    text.Format.LatinFont = new FontData("Cambria Math");
    return text;
}

配列の行間は段落間隔とは別です。SpaceBefore と SpaceAfter は配列を含む段落の上下にスペースを追加し、配列行間には影響しません。同様に、IMathParagraph.Justification は数式段落全体の水平位置を決め、配列の垂直揃え設定を置き換えるものではありません。

三角関数の追加

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

2x に対する cos の三角関数

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 100);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;

var cosine = new MathematicalText("2x")
    .AsArgumentOfFunction(MathFunctionsOfOneArgument.Cos);

mathParagraph.Add(new MathBlock(cosine));

presentation.Save("trigonometric-function.pptx", SaveFormat.Pptx);

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

インデックスやべき乗のために下付き文字と上付き文字ヘルパーを使用します。インデックスを基底の左側に表示する必要がある場合は、SetSubSuperscriptOnTheLeft を使用します。

左側に下付き 1 と上付き n を持つ大文字 Y

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 100);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;

var scripts = new MathematicalText("Y")
    .SetSubSuperscriptOnTheLeft("1", "n");

mathParagraph.Add(new MathBlock(scripts));

presentation.Save("subscript-superscript.pptx", SaveFormat.Pptx);

右側下付き文字と上付き文字の揃え

同じ基底に属する両方の脚本は、MathRightSubSuperscriptElement を使用します。その AlignScripts プロパティは水平位置を制御し、false(デフォルト)では基底の輪郭に従い、true では脚本同士を水平に揃えます。

この例では、同一の編集可能な式を 2 つ横に配置します。どちらも数学イタリック f (U+1D453)、下付き 1、上付き 2、Cambria Math 72 ポイントを使用します。傾斜した基底により PowerPoint で差が見えます。実行前に Cambria Math をインストールしてください。

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
presentation.SlideSize.SetSize(900, 250, SlideSizeScaleType.DoNotScale);
var slide = presentation.Slides[0];

var format = new PortionFormat
{
    FontHeight = 72,
    LatinFont = new FontData("Cambria Math")
};

for (var i = 0; i < 2; i++)
{
    var left = 40 + i * 440;
    var shape = slide.Shapes.AddMathShape(left, 75, 380, 150);

    var baseText = new MathematicalText("\U0001D453", format);
    var subscript = new MathematicalText("1", format);
    var superscript = new MathematicalText("2", format);
    var scripts = new MathRightSubSuperscriptElement(baseText, subscript, superscript);
    scripts.AlignScripts = i == 1;

    var portion = (MathPortion)shape.TextFrame.Paragraphs[0].Portions[0];
    portion.PortionFormat.FontHeight = format.FontHeight;
    portion.PortionFormat.LatinFont = format.LatinFont;
    portion.MathParagraph.Add(new MathBlock(scripts));
}

presentation.Save("aligned-scripts.pptx", SaveFormat.Pptx);

左が AlignScripts=false、右が true の 2 つの編集可能式

左側では下付き文字が基底の輪郭に沿って左に始まり、右側では 2 つの脚本が水平に揃っています。

区切り記号の追加

Enclose を使用して式を区切り記号で囲みます。複数要素を含む区切り式の場合は、区切り文字も設定できます。

x、y、z を縦棒で区切った区切り式

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 100);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;

var delimiter = new MathematicalText("x")
    .Join("y")
    .Join("z")
    .Enclose('<', '>');
delimiter.SeparatorCharacter = '|';

mathParagraph.Add(new MathBlock(delimiter));

presentation.Save("delimiters.pptx", SaveFormat.Pptx);

区切り記号のサイズと揃え

GrowToMatchOperandHeight を true にすると、縦に長い被演算子に合わせて括弧が拡大し、false にすると通常サイズのままです。DelimiterShape は MathDelimiterShape の 2 つの値のいずれかを取ります。Centered は括弧を数式軸の中央に配置し、Match は中身に合わせて揃えます。これらは区切り記号のみに影響し、式自体は変わりません。

例では分数を x の指数に置き、式がベースラインより上に伸びるようにします。すべての 4 つの式は同一の括弧とフォント設定を使用します。

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

void AddExpression(float left, float top, bool grow, MathDelimiterShape shape)
{
    var fraction = new MathematicalText("a + b").Divide("c + d");
    var power = new MathematicalText("x").SetSuperscript(fraction);
    var delimiter = power.Enclose('(', ')');
    delimiter.GrowToMatchOperandHeight = grow;
    delimiter.DelimiterShape = shape;

    var mathShape = slide.Shapes.AddMathShape(left, top, 300, 100);
    var portion = (MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0];
    portion.PortionFormat.FontHeight = 32;
    var equation = new MathematicalText("y = ").Join(delimiter);
    portion.MathParagraph.Add(equation);
}

AddExpression(20, 20, true, MathDelimiterShape.Centered);
AddExpression(350, 20, true, MathDelimiterShape.Match);
AddExpression(20, 140, false, MathDelimiterShape.Centered);
AddExpression(350, 140, false, MathDelimiterShape.Match);

presentation.Save("delimiter-sizing.pptx", SaveFormat.Pptx);

PowerPoint で行を比較すると拡大の効果が分かります。上段では括弧が指数を囲んで拡大し、両形状で同じに見えます。下段では拡大が無効です。左側の小さな括弧(Centered)は等号に揃えられ、右側(Match)は式の上部に高く位置します。

上部で拡大が有効、下部で無効、左が Centered、右が Match の 4 つの式

枠付きボックスの追加

分数に対して ToBorderBox を呼び出すと、式全体を枠で囲むことができます。返される IMathBorderBox はデフォルトで 4 辺すべてが表示されます。HideTop、HideBottom、HideLeft、HideRight を使用して個別の辺を非表示にできます。

例では同一の分数を 6 つスライド上に配置します。取り消し線を表示するために、すべての辺を非表示にし、上から右下への対角線(↗)には StrikethroughBottomLeftToTopRight を、下から右上への対角線(↘)には StrikethroughTopLeftToBottomRight を設定します。また、StrikethroughHorizontal や StrikethroughVertical を使用して、可視側の組み合わせで装飾できます。

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
presentation.SlideSize.SetSize(540, 220, SlideSizeScaleType.DoNotScale);
var slide = presentation.Slides[0];

IMathBorderBox AddFraction(float x, float y, bool hideBorder = false)
{
    var fraction = new MathematicalText("x+1").Divide("y+2");
    var box = fraction.ToBorderBox();
    box.HideTop = box.HideBottom = box.HideLeft = box.HideRight = hideBorder;

    var shape = slide.Shapes.AddMathShape(x, y, 160, 90);
    var portion = (MathPortion)shape.TextFrame.Paragraphs[0].Portions[0];
    portion.PortionFormat.FontHeight = 28;
    portion.MathParagraph.Add(new MathBlock(box));
    return box;
}

AddFraction(10, 10);

var partial = AddFraction(190, 10);
partial.HideTop = true;
partial.HideRight = true;

AddFraction(370, 10, hideBorder: true).StrikethroughBottomLeftToTopRight = true;
AddFraction(10, 120, hideBorder: true).StrikethroughTopLeftToBottomRight = true;
AddFraction(190, 120, hideBorder: true).StrikethroughHorizontal = true;
AddFraction(370, 120).StrikethroughVertical = true;

presentation.Save("border-box.pptx", SaveFormat.Pptx);

上段: 完全枠、下・左側、上がり対角線。下段: 下がり対角線、水平取り消し、垂直取り消しと完全枠

この分数では水平取り消しが分数バーと重なり、垂直取り消しは分子と分母の両方を横切りますが、周囲の枠は表示されたままです。この線は式全体を装飾します。通常のテキスト取り消しはテキストランの文字に適用されます。PowerPoint でプレゼンテーションを再度開いても、分子と分母は個別に編集可能です。

Phantom 要素で可視性とレイアウトを制御

式全体を MathPhantom で囲むと、可視性と式が占めるスペースを個別に制御できます。IMathPhantom のプロパティは以下を制御します:

Property Effect
Show false に設定すると式を非表示にします。対応するプロパティが true の場合を除き、寸法はレイアウトに残ります。
ZeroWidth 式の水平幅をゼロとみなします。隣接要素がより近づきます。
ZeroAsc 基線上方の高さ(アセント)をゼロとみなします。上部の余白が減ります。
ZeroDesc 基線下方の深さ(ディセント)をゼロとみなします。分数バーや他の下部コンテンツが隣接要素に近づきます。

基線は通常の数学文字が配置される参照線です。コンテンツを非表示にしても寸法は残り、寸法をゼロにしても式は削除されません。基になる式は Base から取得可能です。Show を true に戻すと再表示され、寸法設定は保持されたままです。

以下の例は 5 つの横並びバリアントを作成します:可視分数、スペースを確保した非表示分数、そして 3 つの次元をゼロ扱いにした非表示分数です。すべてのバリアントは同一の周囲式と Cambria Math 24 ポイントを使用します。すべての式シェイプは同一の上位置に配置され、灰色のガイドで示されています。外側の分数の分子に Phantom を配置すると、縦レイアウトの変化が見やすくなります。

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
presentation.SlideSize.SetSize(1000, 180, SlideSizeScaleType.DoNotScale);
var slide = presentation.Slides[0];

var labels = new[] { "Visible", "Hidden: reserves space", "Hidden: ZeroWidth", "Hidden: ZeroAsc", "Hidden: ZeroDesc" };

var guide = slide.Shapes.AddAutoShape(ShapeType.Line, 10, 60, 980, 0);
guide.LineFormat.FillFormat.FillType = FillType.Solid;
guide.LineFormat.FillFormat.SolidFillColor.Color = System.Drawing.Color.LightGray;

for (var i = 0; i < labels.Length; i++)
{
    var left = 10 + i * 200;
    
    var label = slide.Shapes.AddAutoShape(ShapeType.Rectangle, left, 10, 190, 30);
    label.FillFormat.FillType = FillType.NoFill;
    label.LineFormat.FillFormat.FillType = FillType.NoFill;
    label.TextFrame.Text = labels[i];

    var labelFormat = label.TextFrame.Paragraphs[0].Portions[0].PortionFormat;
    labelFormat.FontHeight = 14;
    labelFormat.FillFormat.FillType = FillType.Solid;
    labelFormat.FillFormat.SolidFillColor.Color = System.Drawing.Color.Black;

    var fraction = new MathFraction(CreateText("a+b"), CreateText("c+d"));
    var phantom = new MathPhantom(fraction);
    phantom.Show = i == 0;
    phantom.ZeroWidth = i == 2;
    phantom.ZeroAsc = i == 3;
    phantom.ZeroDesc = i == 4;

    var numerator = new MathBlock(CreateText("x+")).Join(phantom).Join(CreateText("+y"));
    var equation = new MathFraction(numerator, CreateText("z"));

    var shape = slide.Shapes.AddMathShape(left, 60, 190, 100);
    shape.TextFrame.TextFrameFormat.AnchoringType = TextAnchorType.Top;

    var mathParagraph = ((IMathPortion)shape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
    mathParagraph.Add(new MathBlock(equation));
}

presentation.Save("math-phantom.pptx", SaveFormat.Pptx);

MathematicalText CreateText(string value)
{
    var text = new MathematicalText(value);
    text.Format.FontHeight = 24;
    text.Format.LatinFont = new FontData("Cambria Math");
    return text;
}

可視分数、スペース確保非表示分数、ZeroWidth、ZeroAsc、ZeroDesc が有効な非表示分数を比較した 5 つの式

項のグループ化

Group を使用して、式の上または下にグルーピング文字を配置します。ラベル付けのためにリミットを追加できます。

x + y をグループ化し、下に任意のテキストラベルを付けた式

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 120);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;

var 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);

数式要素の書式設定

書式設定ヘルパーは式の明確化に必要な箇所でのみ使用してください。例として、Overbar は数式要素の上にバーを配置します。

ABC に上バーが付いた数式

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 100);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;

var overbar = new MathematicalText("ABC").Overbar();

mathParagraph.Add(new MathBlock(overbar));

presentation.Save("overbar.pptx", SaveFormat.Pptx);

式にシンボルフォントを使用

2 つのオペランド間に Wingdings の矢印を挿入するには、矢印用に別個の MathematicalText 要素を作成します。MathematicalText.Format から SymbolFont を設定します。これにより矢印だけが Wingdings フォントになり、両オペランドの書式は保持されます。

文字コードとシンボルフォントは一緒に選択してください。Wingdings では U+F0E0 が右向き矢印として表示されます。別のフォントでは同じコードポイントで異なる字形が表示されるか、字形が存在しないことがあります。

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var shape = slide.Shapes.AddMathShape(20, 20, 240, 90);
var mathPortion = (IMathPortion)shape.TextFrame.Paragraphs[0].Portions[0];

var left = new MathematicalText("A");
var arrow = new MathematicalText("\uF0E0");
var right = new MathematicalText("B");
arrow.Format.SymbolFont = new FontData("Wingdings");

var block = new MathBlock();
block.Add(left);
block.Add(arrow);
block.Add(right);
mathPortion.MathParagraph.Add(block);

presentation.Save("symbol-font-arrow.pptx", SaveFormat.Pptx);

PowerPoint は保存された式を以下のように表示します。3 つの要素すべてが編集可能な状態です。

A と B の間に Wingdings 右矢印が入った編集可能な式

LoadOptions.DefaultSymbolFont はプレゼンテーション読み込み時のデフォルトシンボルフォントを設定します。個別要素にシンボルフォントを割り当てても、フォントはプレゼンテーションに埋め込まれません。Wingdings はレンダリング環境にインストールされている必要があります。

既存式のテキスト書式設定

既存式全体のテキストを書式設定するには、各数式ブロックの要素ツリーを走査します。IMathElement.GetChildren は直下の子要素のみを返すため、分数、脚本、根号、その他の入れ子構造内部のテキストに到達するには再帰的に呼び出します。開始要素が MathematicalText の場合も含めるため、現在の要素とその子要素の両方をチェックします。

この例は、スライドテキストフレーム内の数式ブロックを検索し、IMathPortion.MathParagraph を介してブロックへアクセスし、各テキスト要素の Format の FontHeight を 28 ポイントに設定します。

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation("input.pptx");

foreach (var slide in presentation.Slides)
{
    foreach (var shape in slide.Shapes)
    {
        if (shape is not IAutoShape autoShape || autoShape.TextFrame == null)
            continue;

        foreach (var paragraph in autoShape.TextFrame.Paragraphs)
        {
            foreach (var portion in paragraph.Portions)
            {
                if (portion is IMathPortion mathPortion)
                {
                    foreach (var block in mathPortion.MathParagraph)
                        FormatMathText(block);
                }
            }
        }
    }
}

presentation.Save("formatted-equations.pptx", SaveFormat.Pptx);

void FormatMathText(IMathElement element)
{
    if (element is MathematicalText text)
        text.Format.FontHeight = 28;

    foreach (var child in element.GetChildren())
        FormatMathText(child);
}

保存されたプレゼンテーションは、各処理された式のすべてのレベルで更新されたテキスト書式設定を保持し、テキスト内容と数式構造はそのままです。

クイックリファレンス

Task Main 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 にエクスポートできますか?

はい。MathPortion から IMathParagraph を取得し、IMathParagraph.ToLatex を呼び出すと直接エクスポートできます。完全な例は Export Math Equations from Presentations in .NET を参照してください。