เพิ่มสมการคณิตศาสตร์ในงานนำเสนอ PowerPoint ด้วย Python
ภาพรวม
PowerPoint จัดเก็บสมการเป็น Office Math Markup Language (OMML). ด้วย Aspose.Slides for Python via Java คุณสามารถสร้างเนื้อหาคณิตศาสตร์ประเภทเดียวกันโดยใช้โปรแกรมได้: เศษส่วน, ราก, ฟังก์ชัน, ขีดจำกัด, ตัวดำเนินการ N‑ary, เมทริกซ์, อาร์เรย์, และบล็อกคณิตศาสตร์ที่จัดรูปแบบ
ใน PowerPoint ผู้ใช้โดยทั่วไปเพิ่มสมการจาก Insert > Equation:

ผลลัพธ์คือข้อความคณิตศาสตร์ที่แก้ไขได้บนสไลด์:

Aspose.Slides สร้างข้อความคณิตศาสตร์นั้นผ่านวัตถุหลักสามประเภท:
- รูปร่างคณิตศาสตร์, สร้างด้วย addMathShape, เป็นรูปร่างที่บรรจุสมการ.
- MathPortion เก็บเนื้อหาคณิตศาสตร์ภายในกรอบข้อความของรูปร่าง.
- MathParagraph มีหนึ่งหรือหลายอ็อบเจ็กต์ MathBlock.
ตัวอย่างส่วนใหญ่ด้านล่างใช้ MathematicalText และเมธอดแบบ fluent จาก MathElementBase เพื่อทำให้โค้ดสั้นและอ่านง่าย.
สำหรับสถานการณ์ส่งออก MathML, ดูที่ ส่งออกสมการคณิตศาสตร์จากงานนำเสนอใน Python.
สร้างสมการ
ตัวอย่างนี้ใช้ addMathShape เพื่อสร้างรูปร่างคณิตศาสตร์และเพิ่มสูตรพีธากอรัส:

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()
Note
addMathShape สร้างรูปร่างที่มี MathParagraph อยู่แล้ว. เข้าถึง MathPortion ตัวแรก, เรียก getMathParagraph, แล้วเพิ่ม MathBlock หรือ MathElement เข้าไป.เพิ่มเศษส่วน
ใช้ divide เพื่อสร้างเศษส่วน. คุณสามารถเลือกสไตล์ของเศษส่วนด้วย MathFractionTypes.

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 เพื่อสร้างรากกำลังสอง, รากกำลังสาม หรือรากอื่น ๆ. องค์ประกอบปัจจุบันจะเป็นฐาน, และอาร์กิวเมนต์จะเป็นดีกรี.

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 ดีกรี 2 ปรากฏ. ใน 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()
เพิ่มฟังก์ชันและขีดจำกัด
ใช้ asArgumentOfFunction หรือ function สำหรับฟังก์ชันเช่น sin(x), log(x), หรือชื่อฟังก์ชันที่กำหนดเอง. สำหรับขีดจำกัด, ใส่ lim ใน MathLimit หรือใช้ setLowerLimit.

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‑ary และอินทิแกรล
ใช้ 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‑ary ใช้สำหรับตัวดำเนินการขนาดใหญ่ที่มีขีดจำกัดเสริมได้. ตัวดำเนินการธรรมดาเช่น +, -, และ = มักจะเพิ่มเป็น 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()
เพิ่มอาเรย์สมการ
ใช้ toMathArray เมื่อคุณต้องการสมการที่จัดแนวกันหรือสต็กแนวตั้งของนิพจน์.

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 pt. มีเพียงการจัดแนวอาเรย์ที่เปลี่ยน.
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 อยู่ข้าง ๆ แต่ละอาเรย์.

กำหนดระยะห่างแถว
setRowSpacingRule เลือกกฎการเว้นระยะสำหรับแถวภายในอาเรย์. setRowSpacing ให้ค่าตัวเลขสำหรับกฎการเว้นระยะที่กำหนดโดยตรงและจะถูกละเว้นโดยกฎสำเร็จรูป.
สำหรับการเว้นระยะที่แม่นยำ, ค่าจะวัดเป็น point. สำหรับการเว้นระยะตามสัดส่วน, ค่าจะวัดเป็นครึ่งบรรทัด: ค่า 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 จัดตำแหน่งแนวนอนของย่อหน้า Math ทั้งหมด; มันไม่ได้แทนที่การตั้งค่าการจัดแนวแนวตั้งของอาเรย์.
เพิ่มฟังก์ชันตรีโกณมิติ
ใช้ asArgumentOfFunction เมื่ออาร์กิวเมนต์เป็นองค์ประกอบปัจจุบันและชื่อฟังก์ชันทราบค่า.

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.

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 เพื่อวางนิพจน์ภายในตัวจำกัด. คุณสามารถกำหนดอักขระคั่นสำหรับนิพจน์ที่มีหลายองค์ประกอบได้.

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) อยู่สูงกว่ารอบนิพจน์.

เพิ่มกล่องขอบ
เรียก toBorderBox บนเศษส่วนเพื่อกรอบเนื้อหาทั้งหมด. MathBorderBox ที่คืนค่ามีสี่ด้านโดยค่าเริ่มต้น. ใช้ setHideTop, setHideBottom, setHideLeft, และ setHideRight เพื่อลบด้านที่ต้องการ.
ตัวอย่างวางสำเนาเศษส่วนเดียวกันหกครั้งบนสไลด์เดียว. เพื่อแสดงการตัด, ซ่อนสี่ด้านทั้งหมดและส่งค่า True ไปยัง setStrikethroughBottomLeftToTopRight เพื่อสร้างเส้นทแยงขึ้น (↗) หรือใช้ 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 อีกครั้ง, คุณยังคงแก้ไขเศษและส่วนล่างแยกกันได้.
ควบคุมการมองเห็นและการวางเลย์เอาต์ด้วย Phantom Element
ห่อหุ้มนิพจน์ด้วย MathPhantom เพื่อควบคุมการมองเห็นและพื้นที่ที่มันเพิ่มให้กับสมการแยกจากกัน. เมธอดของ MathPhantom ควบคุมดังต่อไปนี้:
| Method | Effect |
|---|---|
| setShow | ส่งค่า False เพื่อซ่อนนิพจน์. มิติต่าง ๆ ยังคงมีผลต่อการจัดวางจนกว่าจะตั้งค่าเมธอดต่อไปนี้เป็น True. |
| setZeroWidth | ปฏิบัติกว้างแนวนอนของนิพจน์เป็นศูนย์, ทำให้องค์ประกอบข้างเคียงเคลื่อนเข้ามาใกล้กัน. |
| setZeroAsc | ปฏิบัติส่วนสูงเหนือ baseline เป็นศูนย์. สิ่งนี้สามารถลดพื้นที่ที่สงวนไว้ด้านบนขององค์ประกอบข้างเคียง. |
| setZeroDesc | ปฏิบัติส่วนล่างใต้ baseline เป็นศูนย์. นี้สามารถทำให้บาร์ของเศษส่วนหรือเนื้อหาอื่น ๆ ใต้นิพจน์เคลื่อนเข้ามาใกล้กับเพื่อนบ้าน. |
Baseline คือเส้นอ้างอิงที่อักษรคณิตศาสตร์ทั่วไปวางบน. การซ่อนเนื้อหาไม่ได้ลบมิติออก, และการตั้งมิติเป็นศูนย์ไม่ได้ลบนิพจน์ออก. นิพจน์พื้นฐานยังคงเข้าถึงได้ผ่าน getBase. ส่งค่า True ไปยัง setShow เพื่อแสดงอีกครั้ง; การตั้งค่ามิติก็ยังคงเดิม.
ตัวอย่างด้านล่างสร้างห้าแบบข้างเคียง: เศษส่วนที่มองเห็น, เศษส่วนที่ซ่อนแต่สงวนพื้นที่, และสามเศษส่วนที่ซ่อนโดยตั้งค่ามิติหนึ่งเป็นศูนย์. แต่ละแบบใช้นิพจน์โดยรอบเดียวกันและ Cambria Math ขนาด 24 pt. รูปร่างสมการทั้งหมดอยู่ตำแหน่งบนเดียวกันโดยมีไกด์สีเทา. การใส่ phantom ในเศษของเศษส่วนภายนอกทำให้เห็นการเปลี่ยนแปลงการจัดวางแนวตั้งได้ง่ายขึ้น.
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()

จัดกลุ่มเทอม
ใช้ group เพื่อวางอักขระกลุ่มด้านบนหรือด้านล่างของนิพจน์. เพิ่มขีดจำกัดเพื่อระบุเทอมที่กลุ่ม.

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 วางเส้นบาร์เหนือองค์ประกอบคณิตศาสตร์.

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()
จัดรูปแบบข้อความในสมการที่มีอยู่
เพื่อจัดรูปแบบข้อความทั่วทั้งสมการที่มีอยู่, ให้เดินผ่านต้นไม้ของแต่ละ MathBlock. MathElementBase.getChildren คืนค่าเฉพาะลูกโดยตรง, ดังนั้นจึงต้องเรียกแบบเชิง-recursive เพื่อเข้าถึงข้อความภายในเศษส่วน, สคริปต์, ราดิกัล, และโครงสร้างซ้อนอื่น ๆ. ตรวจสอบทั้งองค์ประกอบปัจจุบันและลูกของมันเพื่อรวมองค์ประกอบเริ่มต้นเมื่อเป็น MathematicalText.
ตัวอย่างนี้สมมติว่า input.pptx มีสมการเช่น z = xᵢ / ∛y, โดยเศษส่วนมีตัวห้อยในส่วนบนและราดิกัลในส่วนล่าง. การประมวลผลเฉพาะองค์ประกอบระดับบนของบล็อกจะพลาดข้อความภายในเศษส่วน. ตัวอย่างค้นหา MathPortion ในเฟรมข้อความของสไลด์, เข้าถึงบล็อกผ่าน MathPortion.getMathParagraph, และใช้ setFontHeight เพื่อกำหนดขนาดฟอนต์เป็น 28 pt ผ่านเมธอด getFormat ของแต่ละข้อความ.
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, แล้วอัปเดต MathBlock ในย่อหน้านั้น.
สมการถูกบันทึกเป็นคณิตศาสตร์ PowerPoint ที่แก้ไขได้หรือไม่?
ใช่. เมื่อบันทึกเป็น PPTX, Aspose.Slides จะเขียนสมการเป็นเนื้อหา Office Math ที่แก้ไขได้.
สามารถส่งออกสมการเป็น LaTeX ได้หรือไม่?
ได้. รับ MathParagraph ของสมการจาก MathPortion, แล้วเรียก MathParagraph.toLatex เพื่อส่งออกโดยตรง. สำหรับตัวอย่างครบถ้วน, ดูที่ ส่งออกสมการคณิตศาสตร์จากงานนำเสนอใน Python.