在 Python 中將數學方程式加入 PowerPoint 簡報

概述

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

在 PowerPoint 中,使用者通常從 Insert > Equation 加入方程式:

PowerPoint 插入索引標籤選取了「方程式」指令

包含可編輯數學方程式的 PowerPoint 投影片:

包含可編輯數學方程式的 PowerPoint 投影片

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

大多數範例使用 MathematicalText 與來自 MathElementBase 的串聯方法,以保持程式碼簡潔易讀。

欲了解 MathML 匯出情境,請參考 Export Math Equations from Presentations in Python。

建立方程式

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

c 平方等於 a 平方加 b 平方的方程式

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathematicalText, Presentation, SaveFormat

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    math_shape = slide.getShapes().addMathShape(20, 20, 700, 120)
    math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()

    a_squared = MathematicalText("a").setSuperscript("2")
    b_squared = MathematicalText("b").setSuperscript("2")
    equation = MathematicalText("c").setSuperscript("2").join("=").join(a_squared).join("+").join(b_squared)

    math_paragraph.add(equation)

    presentation.save("pythagorean-theorem.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

合併數學區塊

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

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathBlock, MathematicalText, Presentation, SaveFormat

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    shape = slide.getShapes().addMathShape(20, 20, 600, 100)
    math_portion = shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0)
    math_paragraph = math_portion.getMathParagraph()

    left_block = MathBlock()
    left_block.add(MathematicalText("c").setSuperscript("2"))
    left_block.add(MathematicalText("="))

    right_block = MathBlock()
    right_block.add(MathematicalText("a").setSuperscript("2"))
    right_block.add(MathematicalText("+"))
    right_block.add(MathematicalText("b").setSuperscript("2"))

    combined_block = left_block.joinBlock(right_block)
    math_paragraph.add(combined_block)

    presentation.save("equation.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

joinBlock 會回傳 left_block 本身,現在依序包含兩個部份,且 right_block 會變為空。只需將合併後的區塊加入 MathParagraph 一次。重新開啟 equation.pptx 後,段落只包含一個區塊,代表 c² = a² + b²,且三個次方皆保留為數學上標。

以 left_block.add(right_block) 呼叫 add 則會嘗試將一個區塊直接嵌入另一個區塊,會拋出 InvalidOperationException。

加入分數

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

顯示 1 除以 x 的斜切分數

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathBlock, MathFractionTypes, MathematicalText, Presentation, SaveFormat

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    math_shape = slide.getShapes().addMathShape(20, 20, 700, 100)
    math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()

    fraction = MathematicalText("1").divide("x", MathFractionTypes.Skewed)

    math_block = MathBlock(fraction)
    math_paragraph.add(math_block)

    presentation.save("fraction.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

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

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathFractionTypes, MathematicalText

stacked_fraction = MathematicalText("x + 1").divide("y - 1", MathFractionTypes.Bar)

加入根號

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

x 位於根號符號下的 n 次根表示式

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathBlock, MathematicalText, Presentation, SaveFormat

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    math_shape = slide.getShapes().addMathShape(20, 20, 700, 100)
    math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()

    radical = MathematicalText("x").radical("n")

    math_block = MathBlock(radical)
    math_paragraph.add(math_block)

    presentation.save("radical.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

顯示或隱藏平方根的指數

平方根可以顯示明確的 2,或使用慣用的 √x 符號。setHideDegree 方法(屬於 MathRadical)控制顯示哪一種形式。兩種形式皆保留指數 2 與被開方的 x,且在可編輯方程式中保持不變。

左側顯示指數、右側隱藏指數的相同平方根

此範例將相同的平方根儲存兩次。visible-degree.pptx 中指數可見;hidden-degree.pptx 中指數隱藏。

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathBlock, MathematicalText, Presentation, SaveFormat

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    radical = MathematicalText("x").radical("2")
    math_block = MathBlock(radical)

    math_shape = slide.getShapes().addMathShape(20, 20, 200, 100)
    math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()
    math_paragraph.add(math_block)

    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。

x 趨近於無限大的極限

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathBlock, MathematicalText, Presentation, SaveFormat

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    math_shape = slide.getShapes().addMathShape(20, 20, 700, 100)
    math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()

    limit = MathematicalText("lim").setLowerLimit("x\u2192\u221E").function("x")

    math_block = MathBlock(limit)
    math_paragraph.add(math_block)

    presentation.save("functions-and-limits.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

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

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathematicalText

custom_function = MathematicalText("f").function("x + 1")

加入 N 元運算子和積分

使用 nary 處理求和、聯集、交集等大型運算子。使用 integral 處理積分。兩種方法皆可設定下限與上限。

帶有下限與上限的求和符號

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathBlock, MathNaryOperatorTypes, MathematicalText, Presentation, SaveFormat

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    math_shape = slide.getShapes().addMathShape(20, 20, 700, 120)
    math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()

    a_power = MathematicalText("a").setSuperscript("n-k")
    summation_base = MathematicalText("x").setSuperscript("k").join(a_power)

    summation = summation_base.nary(MathNaryOperatorTypes.Summation, "k=0", "n")

    math_block = MathBlock(summation)
    math_paragraph.add(math_block)

    presentation.save("nary-operators.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

N 元運算子用於可選上下限的大型運算子。+、-、= 等簡單運算子通常以 MathematicalText 加入,再串接至表達式中。

對於積分,使用 integral:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathIntegralTypes, MathematicalText

differential = MathematicalText("dx").toBox()
integral_base = MathematicalText("x").join(differential)
integral = integral_base.integral(MathIntegralTypes.Simple, "0", "1")

加入矩陣

使用 MathMatrix 來定義列與欄。矩陣預設不會包含括號,若需要括弧、方括號或大括號,請自行在矩陣外加入相應符號。

含有一個空儲存格的兩列矩陣

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathBlock, MathMatrix, MathematicalText, Presentation, SaveFormat

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    math_shape = slide.getShapes().addMathShape(20, 20, 700, 120)
    math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()

    matrix = MathMatrix(2, 3)
    cell_0_0 = MathematicalText("1")
    matrix.set_Item(0, 0, cell_0_0)
    cell_0_1 = MathematicalText("x")
    matrix.set_Item(0, 1, cell_0_1)
    cell_1_0 = MathematicalText("x")
    matrix.set_Item(1, 0, cell_1_0)
    cell_1_1 = MathematicalText("2")
    matrix.set_Item(1, 1, cell_1_1)
    cell_1_2 = MathematicalText("y")
    matrix.set_Item(1, 2, cell_1_2)

    math_block = MathBlock(matrix)
    math_paragraph.add(math_block)

    presentation.save("matrix.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

編輯現有矩陣

假設線性代數課程以 matrix.pptx 中的係數矩陣開始(由前一範例儲存)。在下一個練習中,保留第一個方程式,將第二個方程式換成兩個新方程式,並調整變數集合。透過 MathMatrix 開啟既有矩陣,以保留仍然適用的係數。

原始的兩列係數矩陣,第一列為 1、x、空儲存格;第二列為 x、2、y

列與欄的索引自零開始,且在每次呼叫時皆指向當前矩陣的狀態。於索引 1 前插入一列會將原本的第二列移至索引 2。於索引 2 後插入則會在末端追加一列。刪除第 2 列會移除原始的第二個方程式,並使追加的列上移一個位置。欄位的移動方式相同。

import jpage
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathHorizontalAlignment, MathematicalText, Presentation, SaveFormat

presentation = 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)
    matrix = portion.getMathParagraph().get_Item(0).get_Item(0)

    matrix.insertRowBefore(1)
    matrix.insertRowAfter(2)
    matrix.deleteRow(2)

    matrix.insertColumnBefore(1)
    matrix.insertColumnAfter(3)
    matrix.deleteColumn(3)

    # 新儲存格最初是空的。先設定非零係數。
    matrix.set_Item(1, 0, MathematicalText("12"))
    matrix.set_Item(1, 1, MathematicalText("1"))
    matrix.set_Item(2, 2, MathematicalText("10"))
    matrix.set_Item(2, 3, MathematicalText("1"))
    for row in range(matrix.getRowCount()):
        for column in range(matrix.getColumnCount()):
            if matrix.get_Item(row, column) is None:
                matrix.set_Item(row, column, 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 現在出現在第一與第三欄。

更新後的三列矩陣,列內容分別為 1, 0, x, 0;12, 1, 0, 0;以及 0, 0, 10, 1

加入方程式陣列

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

x 在上方、y 在下方的垂直陣列

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathBlock, MathematicalText, Presentation, SaveFormat

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    math_shape = slide.getShapes().addMathShape(20, 20, 700, 140)
    math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()

    equation_array = MathematicalText("x").join("y").toMathArray()

    math_block = MathBlock(equation_array)
    math_paragraph.add(math_block)

    presentation.save("equation-array.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

對齊包含周圍元素的陣列

setBaseJustification 控制陣列相對於旁邊數學元素的垂直對齊。以下範例比較第一列、陣列中心與最後一列的對齊方式。

以下範例建立三個相同的陣列,每個陣列皆有三列,旁邊元素分別為 A= 與 =B。所有數學文字使用 Cambria Math,字型大小 24 點。唯獨陣列對齊方式不同。

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import FillType, FontData, MathArray, MathBlock, MathJustification, MathematicalText, MathVerticalAlignment, Presentation, SaveFormat, ShapeType, SlideSizeScaleType, TextAnchorType
from java.awt import Color

def create_text(value):
    text = MathematicalText(value)
    text.getFormat().setFontHeight(24)
    text.getFormat().setLatinFont(FontData("Cambria Math"))
    return text


presentation = Presentation()
try:
    presentation.getSlideSize().setSize(780, 200, SlideSizeScaleType.DoNotScale)
    slide = presentation.getSlides().get_Item(0)
    
    alignments = (("Top", MathVerticalAlignment.Top), ("Center", MathVerticalAlignment.Center), ("Bottom", MathVerticalAlignment.Bottom))
    for index, (name, alignment) in enumerate(alignments):
        x = 20 + index * 260
        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)
        label_format = label.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat()
        label_format.setFontHeight(16)
        label_format.getFillFormat().setFillType(FillType.Solid)
        label_format.getFillFormat().getSolidFillColor().setColor(Color.BLACK)

        math_shape = slide.getShapes().addMathShape(x, 60, 240, 130)
        math_shape.getTextFrame().getTextFrameFormat().setAnchoringType(TextAnchorType.Top)
        paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0)
        math_paragraph = paragraph.getPortions().get_Item(0).getMathParagraph()
        math_paragraph.setJustification(MathJustification.LeftJustified)

        equation_array = MathArray(create_text("x+y=3"))
        equation_array.getArguments().add(create_text("x-y=1"))
        equation_array.getArguments().add(create_text("x=2"))
        equation_array.setBaseJustification(alignment)
        equation = MathBlock(create_text("A=")).join(equation_array).join(create_text("=B"))
        math_paragraph.add(equation)

    presentation.save("equation-array-alignment.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

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

在 PowerPoint 中展示頂端、置中與底端對齊的方程式陣列,且旁邊元素相同

設定列間距

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

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

以下範例儲存五個簡報,方程式、對齊與字型設定相同。前三個使用預設設定,最後兩個使用明確數值。

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import FontData, MathArray, MathBlock, MathJustification, MathematicalText, MathRowSpacingRule, MathVerticalAlignment, Presentation, SaveFormat

def create_text(value):
    text = MathematicalText(value)
    text.getFormat().setFontHeight(24)
    text.getFormat().setLatinFont(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))
for name, rule, spacing in settings:
    presentation = Presentation()
    try:
        slide = presentation.getSlides().get_Item(0)
        math_shape = slide.getShapes().addMathShape(20, 20, 600, 300)
        paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0)
        paragraph.getParagraphFormat().setSpaceBefore(0)
        paragraph.getParagraphFormat().setSpaceAfter(0)
        math_paragraph = paragraph.getPortions().get_Item(0).getMathParagraph()
        math_paragraph.setJustification(MathJustification.LeftJustified)

        equation_array = MathArray(create_text("x+y=3"))
        equation_array.getArguments().add(create_text("x-y=1"))
        equation_array.getArguments().add(create_text("x=2"))
        equation_array.setBaseJustification(MathVerticalAlignment.Center)
        equation_array.setRowSpacingRule(rule)
        equation_array.setRowSpacing(spacing)
        equation = MathBlock(create_text("A=")).join(equation_array).join(create_text("=B"))
        math_paragraph.add(equation)

        presentation.save(f"equation-array-{name}.pptx", SaveFormat.Pptx)
    finally:
        presentation.dispose()

陣列列間距與段落間距分開控制。setSpaceBefore 與 setSpaceAfter 只會在包含整個段落的前後增加空間,不會影響陣列列之間的距離。同理,MathParagraph.setJustification 只會水平定位整個數學段落,並不會取代陣列的垂直對齊設定。

加入三角函式

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

cos 函式套用於 2x 的示意圖

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathBlock, MathFunctionsOfOneArgument, MathematicalText, Presentation, SaveFormat

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    math_shape = slide.getShapes().addMathShape(20, 20, 700, 100)
    math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()

    cosine = MathematicalText("2x").asArgumentOfFunction(MathFunctionsOfOneArgument.Cos)

    math_block = MathBlock(cosine)
    math_paragraph.add(math_block)

    presentation.save("trigonometric-function.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

加入下標與上標

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

左側帶有下標 1 與上標 n 的大寫 Y

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathBlock, MathematicalText, Presentation, SaveFormat

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    math_shape = slide.getShapes().addMathShape(20, 20, 700, 100)
    math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()

    scripts = MathematicalText("Y").setSubSuperscriptOnTheLeft("1", "n")

    math_block = MathBlock(scripts)
    math_paragraph.add(math_block)

    presentation.save("subscript-superscript.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

加入分隔符

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

以垂直線分隔的 x、y、z 表達式

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathBlock, MathematicalText, Presentation, SaveFormat

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    math_shape = slide.getShapes().addMathShape(20, 20, 700, 100)
    math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()

    delimiter = MathematicalText("x").join("y").join("z").enclose('<', '>')
    delimiter.setSeparatorCharacter('|')

    math_block = MathBlock(delimiter)
    math_paragraph.add(math_block)

    presentation.save("delimiters.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

大小與對齊分隔符

傳入 True 給 setGrowToMatchOperandHeight 可讓括號隨高大的運算元伸長,傳入 False 則保持原始大小。使用 setDelimiterShape 搭配任一 MathDelimiterShape 值。Centered 使括號在數學軸上置中(此軸通過等號的中間),Match 則與被包圍的內容對齊。這兩種設定僅影響分隔符本身,內部表達式保持不變。

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

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathDelimiterShape, MathematicalText, Presentation, SaveFormat

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)
    for row, grow in enumerate((True, False)):
        for column, shape in enumerate((MathDelimiterShape.Centered, MathDelimiterShape.Match)):
            fraction = MathematicalText("a + b").divide("c + d")
            power = MathematicalText("x").setSuperscript(fraction)
            delimiter = power.enclose("(", ")")
            delimiter.setGrowToMatchOperandHeight(grow)
            delimiter.setDelimiterShape(shape)

            math_shape = slide.getShapes().addMathShape(20 + 330 * column, 20 + 120 * row, 300, 100)
            portion = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0)
            portion.getPortionFormat().setFontHeight(32)
            equation = MathematicalText("y = ").join(delimiter)
            portion.getMathParagraph().add(equation)

    presentation.save("delimiter-sizing.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

在 PowerPoint 中比較各列可觀察伸長效果。上排的括號會伸長以包住指數,兩個形狀的外觀相同。下排則關閉伸長功能。左側較小的括號(Centered)與等號對齊,右側(Match)則較高地圍繞表達式。

在 PowerPoint 中顯示四個方程式,括號伸長功能在上排啟用、下排關閉,左側使用 Centered,右側使用 Match

加入邊框盒

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

範例在同一投影片上放置同一分數的六個副本。為顯示取消效果,隱藏四條邊並傳入 True 給 setStrikethroughBottomLeftToTopRight 產生左下至右上的斜線 (↗),或傳入 True 給 setStrikethroughTopLeftToBottomRight 產生左上至右下的斜線 (↘)。亦可使用 setStrikethroughHorizontal 或 setStrikethroughVertical 搭配任意可見邊緣組合。

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathBlock, MathematicalText, Presentation, SaveFormat, SlideSizeScaleType

presentation = Presentation()
try:
    presentation.getSlideSize().setSize(540, 220, SlideSizeScaleType.DoNotScale)
    slide = presentation.getSlides().get_Item(0)

    def add_fraction(x, y, hide_border):
        fraction = MathematicalText("x+1").divide("y+2")
        box = fraction.toBorderBox()
        box.setHideTop(hide_border)
        box.setHideBottom(hide_border)
        box.setHideLeft(hide_border)
        box.setHideRight(hide_border)

        shape = slide.getShapes().addMathShape(x, y, 160, 90)
        portion = shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0)
        portion.getPortionFormat().setFontHeight(28)
        math_block = MathBlock(box)
        portion.getMathParagraph().add(math_block)
        return box

    add_fraction(10, 10, False)

    partial = add_fraction(190, 10, False)
    partial.setHideTop(True)
    partial.setHideRight(True)

    add_fraction(370, 10, True).setStrikethroughBottomLeftToTopRight(True)
    add_fraction(10, 120, True).setStrikethroughTopLeftToBottomRight(True)
    add_fraction(190, 120, True).setStrikethroughHorizontal(True)
    add_fraction(370, 120, False).setStrikethroughVertical(True)

    presentation.save("border-box.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

六個可編輯分數在 PowerPoint 中。上排:完整邊框、底部與左側邊框、左下至右上斜線。下排:左上至右下斜線、水平刪除線、垂直刪除線並帶完整邊框。

在此分數上,水平刪除線會覆蓋分數線。垂直刪除線會穿過分子與分母,且四周的框線仍然可見。這些線條裝飾整個表達式。普通文字的刪除線僅作用於文字執行區段。重新開啟 PowerPoint 簡報後,仍可分別編輯分子與分母。

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

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

方法 效果
setShow 傳入 False 以隱藏表達式。除非下列方法傳入 True,否則其尺寸仍會影響版面配置。
setZeroWidth 將表達式的水平寬度視為零,讓相鄰元素可以更靠近。
setZeroAsc 將其上升部(基線以上的高度)視為零,可減少相鄰元素上方保留的空間。
setZeroDesc 將其下降部(基線以下的深度)視為零,可使分數線或其他內容更接近相鄰元素。

基線是普通數學字母所坐落的參考線。隱藏內容不會移除其尺寸,將尺寸視為零亦不會刪除表達式。底層表達式仍可透過 getBase 取得。傳入 True 給 setShow 可再次顯示,尺寸設定保持不變。

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

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import FillType, FontData, MathBlock, MathFraction, MathematicalText, MathPhantom, Presentation, SaveFormat, ShapeType, SlideSizeScaleType, TextAnchorType
from java.awt import Color


def create_text(value):
    text = MathematicalText(value)
    text.getFormat().setFontHeight(24)
    text.getFormat().setLatinFont(FontData("Cambria Math"))
    return text


presentation = 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(Color.LIGHT_GRAY)

    for index, name in enumerate(labels):
        left = 10 + index * 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(name)
        label_format = label.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat()
        label_format.setFontHeight(14)
        label_format.getFillFormat().setFillType(FillType.Solid)
        label_format.getFillFormat().getSolidFillColor().setColor(Color.BLACK)

        fraction_numerator = create_text("a+b")
        fraction_denominator = create_text("c+d")
        fraction = MathFraction(fraction_numerator, fraction_denominator)
        phantom = MathPhantom(fraction)
        phantom.setShow(index == 0)
        phantom.setZeroWidth(index == 2)
        phantom.setZeroAsc(index == 3)
        phantom.setZeroDesc(index == 4)

        left_text = create_text("x+")
        right_text = create_text("+y")
        numerator = MathBlock(left_text).join(phantom).join(right_text)
        denominator = create_text("z")
        equation = MathFraction(numerator, denominator)

        shape = slide.getShapes().addMathShape(left, 60, 190, 100)
        shape.getTextFrame().getTextFrameFormat().setAnchoringType(TextAnchorType.Top)
        math_portion = shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0)
        math_paragraph = math_portion.getMathParagraph()
        math_block = MathBlock(equation)
        math_paragraph.add(math_block)

    presentation.save("math-phantom.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

在 PowerPoint 中比較可見分數、保留空間的隱藏分數,以及啟用 ZeroWidth、ZeroAsc、ZeroDesc 的隱藏分數

分組項目

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

將 x 加 y 以分組,並在下方加上任意文字標籤的表達式

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathBlock, MathTopBotPositions, MathematicalText, Presentation, SaveFormat

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    math_shape = slide.getShapes().addMathShape(20, 20, 700, 120)
    math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()

    grouped = MathematicalText("x + y").group('\u23DF', MathTopBotPositions.Bottom, MathTopBotPositions.Top).setLowerLimit("any text")

    math_block = MathBlock(grouped)
    math_paragraph.add(math_block)

    presentation.save("grouped-terms.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

格式化數學元素

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

帶有上橫線的數學表達式 ABC

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathBlock, MathematicalText, Presentation, SaveFormat

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    math_shape = slide.getShapes().addMathShape(20, 20, 700, 100)
    math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()

    overbar = MathematicalText("ABC").overbar()

    math_block = MathBlock(overbar)
    math_paragraph.add(math_block)

    presentation.save("overbar.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

格式化現有方程式中的文字

若要格式化現有方程式中的文字,請遍歷每個數學區塊的元素樹。 MathElementBase.getChildren 僅回傳直接子項,必須遞迴呼叫以取得分數、上/下標、根號及其他巢狀結構內的文字。亦需檢查當前元素本身,以確保其為 MathematicalText 時不被遺漏。

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

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import AutoShape, MathematicalText, MathPortion, Presentation, SaveFormat


def format_math_text(element):
    if isinstance(element, MathematicalText):
        element.getFormat().setFontHeight(28)

    for child in element.getChildren():
        format_math_text(child)


presentation = Presentation("input.pptx")
try:
    for slide in presentation.getSlides():
        for shape in slide.getShapes():
            if not isinstance(shape, AutoShape):
                continue

            if shape.getTextFrame() is None:
                continue

            for paragraph in shape.getTextFrame().getParagraphs():
                for portion in paragraph.getPortions():
                    if isinstance(portion, MathPortion):
                        for block in portion.getMathParagraph():
                            format_math_text(block)

    presentation.save("formatted-equations.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

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

快速參考

任務 主要 API
建立數學文字 MathematicalText
合併元素 MathElementBase.join
建立分數 MathElementBase.divide
加入上標或下標 setSuperscript, setSubscript
加入函式 function, asArgumentOfFunction
加入根號 MathElementBase.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 Python。