在 PHP 中為 PowerPoint 簡報新增數學方程式

概述

PowerPoint 將方程式儲存為 Office Math Markup Language (OMML)。使用 Aspose.Slides for PHP via Java,您可以以程式方式建立相同類型的數學內容:分數、根號、函式、極限、N-ary 運算子、矩陣、陣列以及格式化的數學區塊。

在 PowerPoint 中,使用者通常透過 插入 > 方程式 來新增方程式:

PowerPoint Insert tab with the Equation command selected

結果是在投影片上呈現可編輯的數學文字:

A PowerPoint slide containing an editable math equation

Aspose.Slides 透過三個主要物件建立該數學文字:

以下範例大多使用 MathematicalText 以及 MathElementBase 的流暢方法,以保持程式碼簡潔且易讀。

有關 MathML 匯出情境,請參閱 Export Math Equations from Presentations in PHP via Java。

建立方程式

此範例使用 addMathShape 建立數學形狀,並加入畢氏定理:

The equation c squared equals a squared plus b squared

$presentation = new Presentation();
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $mathShape = $slide->getShapes()->addMathShape(20, 20, 700, 120);
    $mathParagraph = $mathShape->getTextFrame()->getParagraphs()
        - >get_Item(0)->getPortions()->get_Item(0)->getMathParagraph();

    $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 {
    if (!java_is_null($presentation)) {
        $presentation->dispose();
    }
}

結合 Math Block

當方程式的部份是以獨立的 MathBlock 物件建構時,使用 joinBlock 來合併它們。範例在左側區塊建構 c² =,在右側區塊建構 a² + b²。setSuperscript 為這三個冪次建立可編輯的上標。

use aspose\slides\MathBlock;
use aspose\slides\MathematicalText;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;

$presentation = new Presentation();
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $shape = $slide->getShapes()->addMathShape(20, 20, 600, 100);
    $mathPortion = $shape->getTextFrame()->getParagraphs()->get_Item(0)->getPortions()->get_Item(0);
    $mathParagraph = $mathPortion->getMathParagraph();

    $leftBlock = new MathBlock();
    $leftBlock->add((new MathematicalText("c"))->setSuperscript("2"));
    $leftBlock->add(new MathematicalText("="));

    $rightBlock = new MathBlock();
    $rightBlock->add((new MathematicalText("a"))->setSuperscript("2"));
    $rightBlock->add(new MathematicalText("+"));
    $rightBlock->add((new MathematicalText("b"))->setSuperscript("2"));

    $combinedBlock = $leftBlock->joinBlock($rightBlock);
    $mathParagraph->add($combinedBlock);

    $presentation->save("equation.pptx", SaveFormat::Pptx);
} finally {
    $presentation->dispose();
}

joinBlock 會回傳 $leftBlock 本身,現在包含依序排列的兩個部分,並讓 $rightBlock 為空。只需將合併後的區塊一次加入 MathParagraph。重新開啟 equation.pptx 後,段落只含有一個表示 c² = a² + b² 的區塊,且所有三個冪次皆以數學上標保存。

若改為呼叫 add 如 $leftBlock->add($rightBlock),會嘗試將一個區塊直接嵌套在另一個區塊內,從而拋出 InvalidOperationException。

新增分數

使用 divide 來建立分數。您可以透過 MathFractionTypes 選擇分數樣式。

A skewed math fraction showing one divided by x

$presentation = new Presentation();
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $mathShape = $slide->getShapes()->addMathShape(20, 20, 700, 100);
    $mathParagraph = $mathShape->getTextFrame()->getParagraphs()
        - >get_Item(0)->getPortions()->get_Item(0)->getMathParagraph();

    $fraction = (new MathematicalText("1"))
        - >divide("x", MathFractionTypes::Skewed);

    $mathParagraph->add(new MathBlock($fraction));

    $presentation->save("fraction.pptx", SaveFormat::Pptx);
} finally {
    if (!java_is_null($presentation)) {
        $presentation->dispose();
    }
}

對於堆疊式分數,使用 MathFractionTypes::Bar:

$stackedFraction = (new MathematicalText("x + 1"))->divide("y - 1", MathFractionTypes::Bar);

新增根號

使用 radical 來建立平方根、立方根或其他次根號。當前元素成為底數,參數則為指數。

An n-th root radical expression with x under the radical sign

$presentation = new Presentation();
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $mathShape = $slide->getShapes()->addMathShape(20, 20, 700, 100);
    $mathParagraph = $mathShape->getTextFrame()->getParagraphs()
        - >get_Item(0)->getPortions()->get_Item(0)->getMathParagraph();

    $radical = (new MathematicalText("x"))
        - >radical("n");

    $mathParagraph->add(new MathBlock($radical));

    $presentation->save("radical.pptx", SaveFormat::Pptx);
} finally {
    if (!java_is_null($presentation)) {
        $presentation->dispose();
    }
}

顯示或隱藏平方根的指數

平方根可以顯示明確的 2,或使用傳統的 √x 記號。MathRadical 的 setHideDegree 方法控制顯示形式。兩種形式皆保留指數 2 與被開根號的 x,且在可編輯的方程式中保持一致。

The same square root with the degree visible on the left and hidden on the right

範例分別保存兩個相同的平方根。visible-degree.pptx 中的 2 為可見;hidden-degree.pptx 中的 2 為隱藏。

use aspose\slides\MathBlock;
use aspose\slides\MathematicalText;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;

$presentation = new Presentation();
try {
    $slide = $presentation->getSlides()->get_Item(0);
    
    $radical = (new MathematicalText("x"))->radical("2");

    $shape = $slide->getShapes()->addMathShape(20, 20, 200, 100);
    $portion = $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。

The limit of x as x approaches infinity

$presentation = new Presentation();
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $mathShape = $slide->getShapes()->addMathShape(20, 20, 700, 100);
    $mathParagraph = $mathShape->getTextFrame()->getParagraphs()
        - >get_Item(0)->getPortions()->get_Item(0)->getMathParagraph();

    $limit = (new MathematicalText("lim"))
        - >setLowerLimit("x\u{2192}\u{221E}")
        - >function("x");

    $mathParagraph->add(new MathBlock($limit));

    $presentation->save("functions-and-limits.pptx", SaveFormat::Pptx);
} finally {
    if (!java_is_null($presentation)) {
        $presentation->dispose();
    }
}

若使用自訂函數名稱,先將函數名稱設為當前元素,然後以其參數呼叫 function:

$customFunction = (new MathematicalText("f"))->function("x + 1");

新增 N 元運算子與積分

使用 nary 來建立求和、聯集、交集及其他大型運算子。使用 integral 來建立積分。兩種方法皆可設定下限與上限。

A summation with lower and upper limits

$presentation = new Presentation();
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $mathShape = $slide->getShapes()->addMathShape(20, 20, 700, 120);
    $mathParagraph = $mathShape->getTextFrame()->getParagraphs()
        - >get_Item(0)->getPortions()->get_Item(0)->getMathParagraph();

    $summationBase = (new MathematicalText("x"))
        - >setSuperscript("k")
        - >join((new MathematicalText("a"))->setSuperscript("n-k"));

    $summation = $summationBase->nary(MathNaryOperatorTypes::Summation, "k=0", "n");

    $mathParagraph->add(new MathBlock($summation));

    $presentation->save("nary-operators.pptx", SaveFormat::Pptx);
} finally {
    if (!java_is_null($presentation)) {
        $presentation->dispose();
    }
}

N 元運算子適用於可選擇上下限的大型運算子。簡單的運算子如 +、-、= 通常以 MathematicalText 加入,然後串接成完整表達式。

若要建立積分,使用 integral:

$integralBase = (new MathematicalText("x"))->join((new MathematicalText("dx"))->toBox());
$integral = $integralBase->integral(MathIntegralTypes::Simple, "0", "1");

新增矩陣

使用 MathMatrix 來建立列與欄。預設情況下矩陣不含括號,若需要括號、方括號或大括號,請自行在矩陣外圍加上。

A two-row math matrix with one empty cell

$presentation = new Presentation();
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $mathShape = $slide->getShapes()->addMathShape(20, 20, 700, 120);
    $mathParagraph = $mathShape->getTextFrame()->getParagraphs()
        - >get_Item(0)->getPortions()->get_Item(0)->getMathParagraph();

    $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 {
    if (!java_is_null($presentation)) {
        $presentation->dispose();
    }
}

編輯既有矩陣

假設線性代數課程已使用前面的範例在 matrix.pptx 中建立係數矩陣。接下來的練習需要保留第一個方程式,將第二個方程式換成兩個新方程式,並調整變數集合。透過 MathMatrix 開啟既有矩陣,以保留仍然適用的係數。

Original two-row coefficient matrix with entries 1, x, and an empty cell in the first row and x, 2, and y in the second

列與欄的索引從 0 開始,且在每次呼叫時皆參照當前矩陣。於索引 1 前插入列會將原本的第二列移至索引 2;於索引 2 後插入則會在最後再加入一列。刪除第 2 列會移除原本的第二個方程式,並將新加入的列上移一個位置。欄的移動方式相同。

use aspose\slides\MathematicalText;
use aspose\slides\MathHorizontalAlignment;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;

$presentation = new Presentation("matrix.pptx");
try {
    $shape = $presentation->getSlides()->get_Item(0)->getShapes()->get_Item(0);
    $portion = $shape->getTextFrame()->getParagraphs()->get_Item(0)->getPortions()->get_Item(0);
    $block = $portion->getMathParagraph()->get_Item(0);
    $matrix = $block->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"));
    $rowCount = java_values($matrix->getRowCount());
    $columnCount = java_values($matrix->getColumnCount());
    for ($row = 0; $row < $rowCount; $row++) {
        for ($column = 0; $column < $columnCount; $column++) {
            $coefficient = $matrix->get_Item($row, $column);
            if (java_is_null($coefficient)) {
                $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 開始的連續三欄右對齊。在 updated-matrix.pptx 中,矩陣包含三個方程式與四個變數。原本第一列的係數 1 與 x 會出現在第一與第三欄。

Updated three-row matrix showing the rows 1, 0, x, 0; 12, 1, 0, 0; and 0, 0, 10, 1

新增方程式陣列

當需要對齊方程式或垂直堆疊多個表達式時,使用 toMathArray。

A vertical math array with x above y

$presentation = new Presentation();
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $mathShape = $slide->getShapes()->addMathShape(20, 20, 700, 140);
    $mathParagraph = $mathShape->getTextFrame()->getParagraphs()
        - >get_Item(0)->getPortions()->get_Item(0)->getMathParagraph();

    $equationArray = (new MathematicalText("x"))
        - >join("y")
        - >toMathArray();

    $mathParagraph->add(new MathBlock($equationArray));

    $presentation->save("equation-array.pptx", SaveFormat::Pptx);
} finally {
    if (!java_is_null($presentation)) {
        $presentation->dispose();
    }
}

將陣列與周圍元素對齊

setBaseJustification 控制陣列相對於相鄰數學元素的垂直對齊方式。以下範例比較首列、陣列中心與末列的對齊結果。

此範例會建立三個相同的陣列,每個陣列皆有三列,且兩側的相鄰元素分別為 A= 與 =B。所有數學文字皆使用 Cambria Math、24 點字型。唯一變化的只有陣列的對齊方式。

use aspose\slides\FillType;
use aspose\slides\FontData;
use aspose\slides\MathArray;
use aspose\slides\MathBlock;
use aspose\slides\MathJustification;
use aspose\slides\MathVerticalAlignment;
use aspose\slides\MathematicalText;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\ShapeType;
use aspose\slides\SlideSizeScaleType;
use aspose\slides\TextAnchorType;

$createText = static function (string $value) {
    $text = new MathematicalText($value);
    $text->getFormat()->setFontHeight(24);
    $text->getFormat()->setLatinFont(new FontData("Cambria Math"));
    return $text;
};

$alignments = ["Top" => MathVerticalAlignment::Top, "Center" => MathVerticalAlignment::Center, "Bottom" => MathVerticalAlignment::Bottom];
$presentation = new Presentation();
try {
    $presentation->getSlideSize()->setSize(780, 200, SlideSizeScaleType::DoNotScale);
    $slide = $presentation->getSlides()->get_Item(0);
    $x = 20;
    foreach ($alignments as $name => $alignment) {
        $label = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, $x, 10, 240, 30);
        $label->getFillFormat()->setFillType(FillType::NoFill);
        $label->getLineFormat()->getFillFormat()->setFillType(FillType::NoFill);
        $label->getTextFrame()->setText($name);
        $labelFormat = $label->getTextFrame()->getParagraphs()->get_Item(0)->getPortions()->get_Item(0)->getPortionFormat();
        $labelFormat->setFontHeight(16);
        $labelFormat->getFillFormat()->setFillType(FillType::Solid);
        $labelFormat->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLACK);

        $mathShape = $slide->getShapes()->addMathShape($x, 60, 240, 130);
        $mathShape->getTextFrame()->getTextFrameFormat()->setAnchoringType(TextAnchorType::Top);
        $paragraph = $mathShape->getTextFrame()->getParagraphs()->get_Item(0);
        $mathParagraph = $paragraph->getPortions()->get_Item(0)->getMathParagraph();
        $mathParagraph->setJustification(MathJustification::LeftJustified);

        $firstRow = $createText("x+y=3");
        $equationArray = new MathArray($firstRow);
        foreach (["x-y=1", "x=2"] as $value) {
            $row = $createText($value);
            $equationArray->getArguments()->add($row);
        }
        $equationArray->setBaseJustification($alignment);
        $leftText = $createText("A=");
        $rightText = $createText("=B");
        $equation = (new MathBlock($leftText))->join($equationArray)->join($rightText);
        $mathParagraph->add($equation);
        $x += 260;
    }
    $presentation->save("equation-array-alignment.pptx", SaveFormat::Pptx);
} finally {
    $presentation->dispose();
}

下圖顯示在 PowerPoint 中儲存的簡報。請注意每個陣列旁邊的 A= 與 =B 位置。

Equation arrays in PowerPoint with Top, Center, and Bottom alignment and identical neighboring elements

設定列間距

setRowSpacingRule 可選擇陣列內列的間距規則。setRowSpacing 提供具體的數值,用於明確的間距規則,預設規則會忽略此值。

若需精確間距,數值以點為單位。若使用比例間距,則以半行為單位:範例中的數值 3 代表 1.5 行。指定明確值時,請同時設定兩個屬性;僅變更數值不會自動選擇間距規則。

以下範例會保存五個簡報,所有簡報的方程式、對齊與字型設定相同。前三個使用預設規則,最後兩個則使用具體數值。

use aspose\slides\FontData;
use aspose\slides\MathArray;
use aspose\slides\MathBlock;
use aspose\slides\MathJustification;
use aspose\slides\MathRowSpacingRule;
use aspose\slides\MathVerticalAlignment;
use aspose\slides\MathematicalText;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;

$createText = static function (string $value) {
    $text = new MathematicalText($value);
    $text->getFormat()->setFontHeight(24);
    $text->getFormat()->setLatinFont(new FontData("Cambria Math"));
    return $text;
};

$settings = [
    "SingleLineGap" => [MathRowSpacingRule::SingleLineGap, 0],
    "OneAndAHalfLineGap" => [MathRowSpacingRule::OneAndAHalfLineGap, 0],
    "TwoLineGap" => [MathRowSpacingRule::TwoLineGap, 0],
    "Exactly" => [MathRowSpacingRule::Exactly, 20],
    "Multiple" => [MathRowSpacingRule::Multiple, 3],
];

foreach ($settings as $name => [$rule, $spacing]) {
    $presentation = new Presentation();
    try {
        $mathShape = $presentation->getSlides()->get_Item(0)->getShapes()->addMathShape(20, 20, 600, 300);
        $paragraph = $mathShape->getTextFrame()->getParagraphs()->get_Item(0);
        $paragraph->getParagraphFormat()->setSpaceBefore(0);
        $paragraph->getParagraphFormat()->setSpaceAfter(0);
        $mathParagraph = $paragraph->getPortions()->get_Item(0)->getMathParagraph();
        $mathParagraph->setJustification(MathJustification::LeftJustified);

        $firstRow = $createText("x+y=3");
        $equationArray = new MathArray($firstRow);
        foreach (["x-y=1", "x=2"] as $value) {
            $row = $createText($value);
            $equationArray->getArguments()->add($row);
        }
        $equationArray->setBaseJustification(MathVerticalAlignment::Center);
        $equationArray->setRowSpacingRule($rule);
        $equationArray->setRowSpacing($spacing);
        $leftText = $createText("A=");
        $rightText = $createText("=B");
        $equation = (new MathBlock($leftText))->join($equationArray)->join($rightText);
        $mathParagraph->add($equation);
        $presentation->save("equation-array-$name.pptx", SaveFormat::Pptx);
    } finally {
        $presentation->dispose();
    }
}

陣列的列間距與段落間距是分開的。setSpaceBefore 與 setSpaceAfter 會在包含段落的四周加入間距,而不會影響陣列列之間的距離。同理,MathParagraph::setJustification 只會水平定位整個數學段落,並不會取代陣列的垂直對齊設定。

新增三角函數

當參數是當前元素且已知函數名稱時,使用 asArgumentOfFunction。

The trigonometric function cos applied to 2x

$presentation = new Presentation();
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $mathShape = $slide->getShapes()->addMathShape(20, 20, 700, 100);
    $mathParagraph = $mathShape->getTextFrame()->getParagraphs()
        - >get_Item(0)->getPortions()->get_Item(0)->getMathParagraph();

    $cosine = (new MathematicalText("2x"))
        - >asArgumentOfFunction(MathFunctionsOfOneArgument::Cos);

    $mathParagraph->add(new MathBlock($cosine));

    $presentation->save("trigonometric-function.pptx", SaveFormat::Pptx);
} finally {
    if (!java_is_null($presentation)) {
        $presentation->dispose();
    }
}

新增下標與上標

使用下標與上標輔助方法來處理索引與次方。如果索引必須出現在基底的左側,請使用 setSubSuperscriptOnTheLeft。

A capital Y with left-side subscript 1 and superscript n

$presentation = new Presentation();
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $mathShape = $slide->getShapes()->addMathShape(20, 20, 700, 100);
    $mathParagraph = $mathShape->getTextFrame()->getParagraphs()
        - >get_Item(0)->getPortions()->get_Item(0)->getMathParagraph();

    $scripts = (new MathematicalText("Y"))
        - >setSubSuperscriptOnTheLeft("1", "n");

    $mathParagraph->add(new MathBlock($scripts));

    $presentation->save("subscript-superscript.pptx", SaveFormat::Pptx);
} finally {
    if (!java_is_null($presentation)) {
        $presentation->dispose();
    }
}

新增分隔符號

使用 enclose 可將表達式包在分隔符號內。亦可為含有多個元素的分隔符號表達式設定分隔字元。

A delimiter expression containing x, y, and z separated by vertical bars

$presentation = new Presentation();
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $mathShape = $slide->getShapes()->addMathShape(20, 20, 700, 100);
    $mathParagraph = $mathShape->getTextFrame()->getParagraphs()
        - >get_Item(0)->getPortions()->get_Item(0)->getMathParagraph();

    $delimiter = (new MathematicalText("x"))
        - >join("y")
        - >join("z")
        - >enclose(new Java("java.lang.Character", "<"), new Java("java.lang.Character", ">"));
    $delimiter->setSeparatorCharacter(new Java("java.lang.Character", "|"));

    $mathParagraph->add(new MathBlock($delimiter));

    $presentation->save("delimiters.pptx", SaveFormat::Pptx);
} finally {
    if (!java_is_null($presentation)) {
        $presentation->dispose();
    }
}

分隔符號的大小與對齊

將 true 傳遞給 setGrowToMatchOperandHeight 可讓括號隨較高的操作元伸長,傳遞 false 則保持原始大小。使用 setDelimiterShape 並搭配 MathDelimiterShape 其中兩個值之一。Centered 會將括號以等號中心為基準置中;Match 會使括號與內部內容對齊。這兩種設定僅影響分隔符號本身,內部表達式保持不變。

此範例將分數放在 x 的指數上,使得表達式在基線上方的延伸較大。四個方程式皆使用相同的括號與字型設定。

use aspose\slides\MathDelimiterShape;
use aspose\slides\MathematicalText;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;

$presentation = new Presentation();
try {
    $slide = $presentation->getSlides()->get_Item(0);
    $opening = new Java("java.lang.Character", "(");
    $closing = new Java("java.lang.Character", ")");
    for ($row = 0; $row < 2; $row++) {
        for ($column = 0; $column < 2; $column++) {
            $fraction = (new MathematicalText("a + b"))->divide("c + d");
            $power = (new MathematicalText("x"))->setSuperscript($fraction);
            $delimiter = $power->enclose($opening, $closing);
            $delimiter->setGrowToMatchOperandHeight($row === 0);
            $delimiter->setDelimiterShape($column === 0 ? MathDelimiterShape::Centered : MathDelimiterShape::Match);

            $mathShape = $slide->getShapes()->addMathShape(20 + 330 * $column, 20 + 120 * $row, 300, 100);
            $portion = $mathShape->getTextFrame()->getParagraphs()->get_Item(0)->getPortions()->get_Item(0);
            $portion->getPortionFormat()->setFontHeight(32);
            $equation = (new MathematicalText("y = "))->join($delimiter);
            $portion->getMathParagraph()->add($equation);
        }
    }
    $presentation->save("delimiter-sizing.pptx", SaveFormat::Pptx);
} finally {
    $presentation->dispose();
}

在 PowerPoint 中比較各列即可觀察成長效果。上列的括號會因指數而伸長,兩種形狀看起來相同。下列則關閉伸長功能。左側的較小括號(Centered)與等號對齊;右側的(Match)則在表達式上方稍高。

Four equations in PowerPoint with delimiter growth enabled above and disabled below, and Centered on the left and Match on the right

新增邊框盒子

對分數呼叫 toBorderBox 可將整個表達式框起。回傳的 MathBorderBox 預設顯示四條邊。使用 setHideTop、setHideBottom、setHideLeft 與 setHideRight 可分別隱藏單一邊。

範例在同一張投影片上放置六個相同的分數。為顯示取消線,先隱藏四條邊,然後將 true 傳遞給 setStrikethroughBottomLeftToTopRight 產生上升對角線 (↗);或傳遞給 setStrikethroughTopLeftToBottomRight 產生下降對角線 (↘)。亦可搭配 setStrikethroughHorizontal 或 setStrikethroughVertical 使用任意組合的可見邊。

use aspose\slides\MathBlock;
use aspose\slides\MathematicalText;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\SlideSizeScaleType;

$presentation = new Presentation();
try {
    $presentation->getSlideSize()->setSize(540, 220, SlideSizeScaleType::DoNotScale);
    $slide = $presentation->getSlides()->get_Item(0);

    $addFraction = function ($x, $y, $hideBorder) use ($slide) {
        $fraction = (new MathematicalText("x+1"))->divide("y+2");
        $box = $fraction->toBorderBox();
        $box->setHideTop($hideBorder);
        $box->setHideBottom($hideBorder);
        $box->setHideLeft($hideBorder);
        $box->setHideRight($hideBorder);

        $shape = $slide->getShapes()->addMathShape($x, $y, 160, 90);
        $portion = $shape->getTextFrame()->getParagraphs()->get_Item(0)->getPortions()->get_Item(0);
        $portion->getPortionFormat()->setFontHeight(28);
        $mathBlock = new MathBlock($box);
        $portion->getMathParagraph()->add($mathBlock);
        return $box;
    };

    $addFraction(10, 10, false);

    $partial = $addFraction(190, 10, false);
    $partial->setHideTop(true);
    $partial->setHideRight(true);

    $addFraction(370, 10, true)->setStrikethroughBottomLeftToTopRight(true);
    $addFraction(10, 120, true)->setStrikethroughTopLeftToBottomRight(true);
    $addFraction(190, 120, true)->setStrikethroughHorizontal(true);
    $addFraction(370, 120, false)->setStrikethroughVertical(true);

    $presentation->save("border-box.pptx", SaveFormat::Pptx);
} finally {
    $presentation->dispose();
}

Six editable fractions in PowerPoint. Top row: full border, bottom and left sides, rising diagonal. Bottom row: falling diagonal, horizontal strike, vertical strike with a full border.

在此分數中,水平刪除線會與分數線重疊。垂直刪除線則同時貫穿分子與分母,而四周邊框仍保持可見。這些線條會裝飾整個表達式。普通文字的刪除線只會作用於文字串。重新開啟 PowerPoint 簡報後,仍可獨立編輯分子與分母。

使用幻影元素控制可見性與版面配置

將表達式包在 MathPhantom 中,可單獨控制其可見性與在方程式中佔用的空間。MathPhantom 的方法如下:

方法 效果
setShow 傳遞 false 可隱藏表達式。其尺寸仍會影響版面配置,除非以下對應方法傳入 true。
setZeroWidth 將表達式的水平寬度視為 0,讓相鄰元素能更靠近。
setZeroAsc 將上升距(基線上方的高度)視為 0,減少相鄰元素上方保留的空間。
setZeroDesc 將下降距(基線下方的深度)視為 0,讓分數線或其他內容更貼近相鄰元素。

基線是普通數學字母所站立的參考線。隱藏內容不會移除其尺寸,而將尺寸視為 0 亦不會刪除表達式。底層表達式仍可透過 getBase 取得。若要重新顯示,傳遞 true 給 setShow;尺寸設定則保持不變。

以下範例建立五種並排變體:一個可見的分數、一個保留空間的隱藏分數,以及三個將其中一個維度設為 0 的隱藏分數。每個變體皆使用相同的周圍表達式與 Cambria Math、24 點字型。所有方程式形狀的頂部位置相同,以灰色參考線標示。將幻影放在外部分數的分子中,可更容易觀察垂直版面配置的變化。

use aspose\slides\FillType;
use aspose\slides\FontData;
use aspose\slides\MathBlock;
use aspose\slides\MathFraction;
use aspose\slides\MathPhantom;
use aspose\slides\MathematicalText;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\ShapeType;
use aspose\slides\SlideSizeScaleType;
use aspose\slides\TextAnchorType;

$createText = static function (string $value) {
    $text = new MathematicalText($value);
    $text->getFormat()->setFontHeight(24);
    $text->getFormat()->setLatinFont(new FontData("Cambria Math"));
    return $text;
};

$presentation = new Presentation();
try {
    $presentation->getSlideSize()->setSize(1000, 180, SlideSizeScaleType::DoNotScale);
    $slide = $presentation->getSlides()->get_Item(0);

    $labels = ["Visible", "Hidden: reserves space", "Hidden: ZeroWidth", "Hidden: ZeroAsc", "Hidden: ZeroDesc"];

    $guide = $slide->getShapes()->addAutoShape(ShapeType::Line, 10, 60, 980, 0);
    $guide->getLineFormat()->getFillFormat()->setFillType(FillType::Solid);
    $guide->getLineFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->LIGHT_GRAY);

    foreach ($labels as $i => $labelText) {
        $left = 10 + $i * 200;

        $label = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, $left, 10, 190, 30);
        $label->getFillFormat()->setFillType(FillType::NoFill);
        $label->getLineFormat()->getFillFormat()->setFillType(FillType::NoFill);
        $label->getTextFrame()->setText($labelText);

        $labelFormat = $label->getTextFrame()->getParagraphs()->get_Item(0)->getPortions()->get_Item(0)->getPortionFormat();
        $labelFormat->setFontHeight(14);
        $labelFormat->getFillFormat()->setFillType(FillType::Solid);
        $labelFormat->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLACK);

        $fractionNumerator = $createText("a+b");
        $fractionDenominator = $createText("c+d");
        $fraction = new MathFraction($fractionNumerator, $fractionDenominator);
        $phantom = new MathPhantom($fraction);
        $phantom->setShow($i === 0);
        $phantom->setZeroWidth($i === 2);
        $phantom->setZeroAsc($i === 3);
        $phantom->setZeroDesc($i === 4);

        $leftText = $createText("x+");
        $rightText = $createText("+y");
        $numerator = (new MathBlock($leftText))->join($phantom)->join($rightText);
        $denominator = $createText("z");
        $equation = new MathFraction($numerator, $denominator);

        $shape = $slide->getShapes()->addMathShape($left, 60, 190, 100);
        $shape->getTextFrame()->getTextFrameFormat()->setAnchoringType(TextAnchorType::Top);

        $mathPortion = $shape->getTextFrame()->getParagraphs()->get_Item(0)->getPortions()->get_Item(0);
        $mathParagraph = $mathPortion->getMathParagraph();
        $mathBlock = new MathBlock($equation);
        $mathParagraph->add($mathBlock);
    }

    $presentation->save("math-phantom.pptx", SaveFormat::Pptx);
} finally {
    $presentation->dispose();
}

Five equations in PowerPoint comparing a visible fraction, a hidden fraction that reserves space, and hidden fractions with ZeroWidth, ZeroAsc, or ZeroDesc enabled

分組項目

使用 group 可在表達式的上方或下方放置分組符號。可再加入極限標籤來標示分組的項目。

The expression x plus y grouped with the label any text below it

$presentation = new Presentation();
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $mathShape = $slide->getShapes()->addMathShape(20, 20, 700, 120);
    $mathParagraph = $mathShape->getTextFrame()->getParagraphs()
        - >get_Item(0)->getPortions()->get_Item(0)->getMathParagraph();

    $grouped = (new MathematicalText("x + y"))
        - >group(new Java("java.lang.Character", "\u{23DF}"), MathTopBotPositions::Bottom, MathTopBotPositions::Top)
        - >setLowerLimit("any text");

    $mathParagraph->add(new MathBlock($grouped));

    $presentation->save("grouped-terms.pptx", SaveFormat::Pptx);
} finally {
    if (!java_is_null($presentation)) {
        $presentation->dispose();
    }
}

格式化數學元素

僅在能提升公式可讀性時才使用格式化輔助方法。例如,overbar 會在數學元素上方加上一條橫線。

A math expression ABC with an overbar

$presentation = new Presentation();
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $mathShape = $slide->getShapes()->addMathShape(20, 20, 700, 100);
    $mathParagraph = $mathShape->getTextFrame()->getParagraphs()
        - >get_Item(0)->getPortions()->get_Item(0)->getMathParagraph();

    $overbar = (new MathematicalText("ABC"))->overbar();

    $mathParagraph->add(new MathBlock($overbar));

    $presentation->save("overbar.pptx", SaveFormat::Pptx);
} finally {
    if (!java_is_null($presentation)) {
        $presentation->dispose();
    }
}

在既有方程式中格式化文字

若要在既有方程式中格式化文字,必須遍歷每個數學區塊的元素樹。MathElementBase::getChildren 只回傳直接子元素,需遞迴呼叫才能取得分數、上/下標、根號及其他巢狀結構內的文字。檢查當前元素與其子元素,以確保當前元素是 MathematicalText 時也會被包含。

此範例假設 input.pptx 包含如 z = xᵢ / ∛y 的方程式,其中分數的分子有下標,分母有根號。僅處理區塊的頂層元素會遺漏分數內的文字。範例會在投影片文字框中尋找數學片段,透過 MathPortion::getMathParagraph 取得其區塊,並使用 setFontHeight 將每個文字元素的字型大小設定為 28 點,該設定經由 getFormat 取得。

use aspose\slides\Presentation;
use aspose\slides\SaveFormat;

function formatMathText($element, $mathematicalTextClass) {
    if (java_instanceof($element, $mathematicalTextClass)) {
        $element->getFormat()->setFontHeight(28);
    }

    foreach ($element->getChildren() as $child) {
        formatMathText($child, $mathematicalTextClass);
    }
}

$mathematicalTextClass = new JavaClass("com.aspose.slides.MathematicalText");
$autoShapeClass = new JavaClass("com.aspose.slides.IAutoShape");
$mathPortionClass = new JavaClass("com.aspose.slides.IMathPortion");

$presentation = new Presentation("input.pptx");
try {
    for ($slideIndex = 0; $slideIndex < java_values($presentation->getSlides()->size()); $slideIndex++) {
        $slide = $presentation->getSlides()->get_Item($slideIndex);
        for ($shapeIndex = 0; $shapeIndex < java_values($slide->getShapes()->size()); $shapeIndex++) {
            $shape = $slide->getShapes()->get_Item($shapeIndex);
            if (!java_instanceof($shape, $autoShapeClass)) {
                continue;
            }

            $textFrame = $shape->getTextFrame();
            if (java_is_null($textFrame)) {
                continue;
            }

            for ($paragraphIndex = 0; $paragraphIndex < java_values($textFrame->getParagraphs()->getCount()); $paragraphIndex++) {
                $paragraph = $textFrame->getParagraphs()->get_Item($paragraphIndex);
                for ($portionIndex = 0; $portionIndex < java_values($paragraph->getPortions()->getCount()); $portionIndex++) {
                    $portion = $paragraph->getPortions()->get_Item($portionIndex);
                    if (java_instanceof($portion, $mathPortionClass)) {
                        for ($blockIndex = 0; $blockIndex < java_values($portion->getMathParagraph()->getCount()); $blockIndex++) {
                            $block = $portion->getMathParagraph()->get_Item($blockIndex);
                            formatMathText($block, $mathematicalTextClass);
                        }
                    }
                }
            }
        }
    }

    $presentation->save("formatted-equations.pptx", SaveFormat::Pptx);
} finally {
    $presentation->dispose();
}

儲存的簡報會在每個已處理的方程式的每個層級保留更新後的文字格式,同時維持其文字內容與數學結構。

快速參考

任務 主要 API
建立數學文字 MathematicalText
結合元素 join
建立分數 divide
新增上標或下標 setSuperscript, setSubscript
新增函數 function, asArgumentOfFunction
新增根號 radical
新增極限 setLowerLimit, setUpperLimit
新增左側上下標 setSubSuperscriptOnTheLeft
新增求和與積分 nary, integral
新增矩陣 MathMatrix
新增方程式陣列 toMathArray
新增分隔符號 enclose
新增橫線與邊框 overbar, toBorderBox
分組項目 group

常見問題

可以編輯已存在的 PowerPoint 方程式嗎?

可以。開啟簡報後,找到包含 MathPortion 的形狀,呼叫 getMathParagraph,並更新該段落中的數學區塊。

方程式會以可編輯的 PowerPoint 數學形式儲存嗎?

會。將簡報儲存為 PPTX 時,Aspose.Slides 會將方程式寫入為可編輯的 Office 數學內容。

可以將方程式匯出為 LaTeX 嗎?

可以。從其 MathPortion 取得 MathParagraph,然後呼叫 MathParagraph::toLatex 直接匯出。完整範例請參閱 Export Math Equations from Presentations in PHP via Java。