إضافة معادلات رياضية إلى عروض PowerPoint في Python

نظرة عامة

PowerPoint يخزن المعادلات كلغة توثيق الرياضيات المكتبية (OMML). باستخدام Aspose.Slides للـ Python عبر .NET، يمكنك إنشاء نفس نوع محتوى الرياضيات برمجياً: الكسور، الجذور، الدوال، الحدود، المشغلات متعددة الحدود، المصفوفات، المصفوفات المتعددة الأبعاد، وكتل الرياضيات المنسقة.

في PowerPoint، يضيف المستخدمون عادةً المعادلات من Insert > Equation:

علامة تبويب إدراج في PowerPoint مع تحديد أمر المعادلة

النتيجة هي نص رياضي قابل للتحرير على الشريحة:

شريحة PowerPoint تحتوي على معادلة رياضية قابلة للتحرير

Aspose.Slides يُنشئ نص الرياضيات هذا عبر ثلاثة كائنات رئيسية:

  • شكل رياضي، يتم إنشاؤه باستخدام add_math_shape، وهو الشكل الذي يحتوي على المعادلة.
  • MathPortion يخزن محتوى الرياضيات داخل إطار نص الشكل.
  • MathParagraph يحتوي على كائن واحد أو أكثر من MathBlock.

معظم الأمثلة أدناه تستخدم MathematicalText والطرق المتسلسلة من MathElementBase للاحتفاظ بالكود قصيراً وسهل القراءة.

لحالات تصدير MathML، راجع Export Math Equations from Presentations in Python via .NET.

إنشاء معادلة

هذا المثال يستخدم add_math_shape لإنشاء شكل رياضي ويضيف نظرية فيثاغورس:

المعادلة c² = a² + b²

import aspose.slides as slides
import aspose.slides.mathtext as math

with slides.Presentation() as presentation:
    slide = presentation.slides[0]

    math_shape = slide.shapes.add_math_shape(20, 20, 700, 120)
    math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph

    equation = (
        math.MathematicalText("c")
        .set_superscript("2")
        .join("=")
        .join(math.MathematicalText("a").set_superscript("2"))
        .join("+")
        .join(math.MathematicalText("b").set_superscript("2"))
    )

    math_paragraph.add(equation)

    presentation.save("pythagorean-theorem.pptx", slides.export.SaveFormat.PPTX)

دمج كتل الرياضيات

عندما تُبنى أجزاء المعادلة بشكل مستقل ككائنات MathBlock، استخدم join_block لدمجها. المثال يُنشئ c² = في الكتلة اليسرى وa² + b² في الكتلة اليمنى. set_superscript يُنشئ أسس رياضية قابلة للتحرير لكل الثلاث قوى.

import aspose.slides as slides
import aspose.slides.mathtext as math

with slides.Presentation() as presentation:
    slide = presentation.slides[0]

    shape = slide.shapes.add_math_shape(20, 20, 600, 100)
    math_paragraph = shape.text_frame.paragraphs[0].portions[0].math_paragraph

    left_block = math.MathBlock()
    left_block.add(math.MathematicalText("c").set_superscript("2"))
    left_block.add(math.MathematicalText("="))

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

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

    presentation.save("equation.pptx", slides.export.SaveFormat.PPTX)

join_block يُعيد left_block نفسها، الآن تحتوي على الجزأين بالترتيب، ويترك right_block فارغاً. أضف الكتلة المدمجة إلى MathParagraph مرة واحدة فقط. بعد إعادة فتح equation.pptx، تحتوي الفقرة على كتلة واحدة تمثل c² = a² + b²، مع الحفاظ على جميع الأسس الثلاثة كأسس رياضية.

استدعاء add كـ left_block.add(right_block) يحاول بدلاً من ذلك وضع كتلة داخل أخرى مباشرةً ويؤدي إلى رفع RuntimeError يغلف InvalidOperationException في .NET.

إضافة كسور

استخدم divide لإنشاء كسر. يمكنك اختيار نمط الكسر عبر MathFractionTypes.

كسر رياضي مائل يُظهر 1 مقسومًا على x

import aspose.slides as slides
import aspose.slides.mathtext as math

with slides.Presentation() as presentation:
    slide = presentation.slides[0]

    math_shape = slide.shapes.add_math_shape(20, 20, 700, 100)
    math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph

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

    math_paragraph.add(math.MathBlock(fraction))

    presentation.save("fraction.pptx", slides.export.SaveFormat.PPTX)

لإنشاء كسر مكدس، استخدم MathFractionTypes.BAR:

import aspose.slides.mathtext as math

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

إضافة جذور

استخدم radical لإنشاء جذر تربيعي أو مكعب أو أي جذر آخر. العنصر الحالي يصبح القاعدة، والحجة تصبح الدرجة.

تعبير جذري من الدرجة n مع x تحت علامة الجذر

import aspose.slides as slides
import aspose.slides.mathtext as math

with slides.Presentation() as presentation:
    slide = presentation.slides[0]

    math_shape = slide.shapes.add_math_shape(20, 20, 700, 100)
    math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph

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

    math_paragraph.add(math.MathBlock(radical))

    presentation.save("radical.pptx", slides.export.SaveFormat.PPTX)

إظهار أو إخفاء درجة الجذر التربيعي

يمكن للجذر التربيعي إظهار الرقم 2 صراحةً أو استخدام الترميز التقليدي √x. خاصية hide_degree في MathRadical تتحكم في الشكل الظاهر. كلا الشكلين يحتفظان بالدرجة 2 والقاعدة x في المعادلة القابلة للتحرير.

الجذر التربيعي لـ x مع ظهور الدرجة 2 في اليسار وإخفاؤها في اليمين

المثال يحفظ الجذر التربيعي نفسه مرتين. الرقم 2 ظاهر في visible-degree.pptx ومخفي في hidden-degree.pptx.

import aspose.slides as slides
import aspose.slides.mathtext as math

with slides.Presentation() as presentation:
    slide = presentation.slides[0]
    
    radical = math.MathematicalText("x").radical("2")

    shape = slide.shapes.add_math_shape(20, 20, 200, 100)
    portion = shape.text_frame.paragraphs[0].portions[0]
    portion.math_paragraph.add(math.MathBlock(radical))

    radical.hide_degree = False
    presentation.save("visible-degree.pptx", slides.export.SaveFormat.PPTX)

    radical.hide_degree = True
    presentation.save("hidden-degree.pptx", slides.export.SaveFormat.PPTX)

إضافة دوال وحدود

استخدم as_argument_of_function أو function للدوال مثل sin(x), log(x), أو أسماء دوال مخصصة. للحدود، ضع lim داخل MathLimit أو استخدم set_lower_limit.

حد الـ x عندما يقترب x من اللانهاية

import aspose.slides as slides
import aspose.slides.mathtext as math

with slides.Presentation() as presentation:
    slide = presentation.slides[0]

    math_shape = slide.shapes.add_math_shape(20, 20, 700, 100)
    math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph

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

    math_paragraph.add(math.MathBlock(limit))

    presentation.save("functions-and-limits.pptx", slides.export.SaveFormat.PPTX)

لإنشاء اسم دالة مخصص، اجعل اسم الدالة هو العنصر الحالي ثم استدعِ function مع حجتها:

import aspose.slides.mathtext as math

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

إضافة مشغلات متعددة وعاملات تكامل

استخدم nary للجمعيات، الاتحادات، التقاطعات، وغيرها من المشغلات الكبيرة. استخدم integral للتكاملات. كلا الطريقتين تسمحان بتحديد الحدود السفلية والعلوية.

جمعية مع حدود سفلية وعليا

import aspose.slides as slides
import aspose.slides.mathtext as math

with slides.Presentation() as presentation:
    slide = presentation.slides[0]

    math_shape = slide.shapes.add_math_shape(20, 20, 700, 120)
    math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph

    summation_base = (
        math.MathematicalText("x")
        .set_superscript("k")
        .join(math.MathematicalText("a").set_superscript("n-k"))
    )

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

    math_paragraph.add(math.MathBlock(summation))

    presentation.save("nary-operators.pptx", slides.export.SaveFormat.PPTX)

المشغلات متعددة الحدود تُستعمل للمشغلات الكبيرة ذات حدود اختيارية. المشغلات البسيطة مثل +, -, و= تُضاف عادةً عبر MathematicalText وتُدمج في التعبير.

لإنشاء تكامل، استخدم integral:

import aspose.slides.mathtext as math

integral_base = math.MathematicalText("x").join(math.MathematicalText("dx").to_box())
integral = integral_base.integral(math.MathIntegralTypes.SIMPLE, "0", "1")

إضافة مصفوفات

استخدم MathMatrix للصفوف والأعمدة. لا تتضمن المصفوفات أقواسًا بشكل افتراضي، لذا يجب إحاطتها بأقواس أو مربعات أو أقواس معقوفة عند الحاجة.

مصفوفة رياضية ذات صفّين مع خلية فارغة واحدة

import aspose.slides as slides
import aspose.slides.mathtext as math

with slides.Presentation() as presentation:
    slide = presentation.slides[0]

    math_shape = slide.shapes.add_math_shape(20, 20, 700, 120)
    math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph

    matrix = math.MathMatrix(2, 3)

    matrix[0, 0] = math.MathematicalText("1")
    matrix[0, 1] = math.MathematicalText("x")
    matrix[1, 0] = math.MathematicalText("x")
    matrix[1, 1] = math.MathematicalText("2")
    matrix[1, 2] = math.MathematicalText("y")
    math_paragraph.add(math.MathBlock(matrix))

    presentation.save("matrix.pptx", slides.export.SaveFormat.PPTX)

تحرير مصفوفة موجودة

افترض أن درس الجبر الخطي يبدأ بمصفوفة المعاملات في matrix.pptx، التي حفظها المثال السابق. للتمرين التالي، احتفظ بالمعادلة الأولى، استبدل الثانية بمعادلتين جديدتين، وعدّل مجموعة المتغيرات. احصل على المصفوفة الحالية كـ MathMatrix لتحتفظ بالمعاملات التي لا تزال صالحة.

مصفوفة معاملية ذات صفّين الأصلية مع القيم 1، x، وخلية فارغة في الصف الأول وx، 2، y في الصف الثاني

مؤشرات الصفوف والأعمدة تبدأ من الصفر وتشير إلى المصفوفة كما هي عند كل استدعاء. إدراج صف قبل الفهرس 1 ينقل الصف الأصلي الثاني إلى الفهرس 2. الإدراج بعد الفهرس 2 يضيف صفًا. حذف الصف 2 يزيل المعادلة الأصلية الثانية وينقل الصف المضاف لأعلى موضع واحد. الأعمدة تتحرك بنفس الطريقة.

import aspose.slides as slides
import aspose.slides.mathtext as math

with slides.Presentation("matrix.pptx") as presentation:
    shape = presentation.slides[0].shapes[0]
    portion = shape.text_frame.paragraphs[0].portions[0]
    matrix = portion.math_paragraph[0][0]

    matrix.insert_row_before(1)
    matrix.insert_row_after(2)
    matrix.delete_row(2)

    matrix.insert_column_before(1)
    matrix.insert_column_after(3)
    matrix.delete_column(3)

    matrix[1, 0] = math.MathematicalText("12")
    matrix[1, 1] = math.MathematicalText("1")
    matrix[2, 2] = math.MathematicalText("10")
    matrix[2, 3] = math.MathematicalText("1")
    for row in range(matrix.row_count):
        for column in range(matrix.column_count):
            if matrix[row, column] is None:
                matrix[row, column] = math.MathematicalText("0")

    matrix.set_column_alignment(0, math.MathHorizontalAlignment.LEFT)
    matrix.set_columns_alignment(1, 3, math.MathHorizontalAlignment.RIGHT)

    presentation.save("updated-matrix.pptx", slides.export.SaveFormat.PPTX)

set_column_alignment يضبط محاذاة العمود الأول إلى اليسار. set_columns_alignment يضبط ثلاث أعمدة متتالية إلى اليمين، بدءًا من الفهرس 1. في updated-matrix.pptx، تحتوي المصفوفة على ثلاث معادلات وأربعة متغيرات. ظهرت معاملات الصف الأول الأصلية 1 وx الآن في العمودين الأول والثالث.

مصفوفة محدثة ذات ثلاثة صفوف تُظهر الصفوف 1، 0، x، 0؛ 12، 1، 0، 0؛ و0، 0، 10، 1

إضافة مصفوفات معادلات

استخدم to_math_array عندما تحتاج إلى معادلات محاذاة أو مجموعة رأسية من التعبيرات.

مصفوفة رياضية رأسية مع x فوق y

import aspose.slides as slides
import aspose.slides.mathtext as math

with slides.Presentation() as presentation:
    slide = presentation.slides[0]

    math_shape = slide.shapes.add_math_shape(20, 20, 700, 140)
    math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph

    equation_array = (
        math.MathematicalText("x")
        .join("y")
        .to_math_array()
    )

    math_paragraph.add(math.MathBlock(equation_array))

    presentation.save("equation-array.pptx", slides.export.SaveFormat.PPTX)

محاذاة مصفوفة مع العناصر المجاورة

MathArray.base_justification يتحكم في محاذاة عمودية للمصفوفة بالنسبة للعناصر الرياضية بجانبها. المثال أدناه يقارن المحاذاة مع الصف الأول، وسط المصفوفة، والصف الأخير.

المثال التالي ينشئ ثلاث مصفوفات متماثلة، كل منها بثلاث صفوف والعناصر المجاورة A= و=B. كل النصوص الرياضية تستخدم Cambria Math بحجم 24 نقطة. تتغير فقط محاذاة المصفوفة.

import aspose.slides as slides
import aspose.slides.mathtext as math


def create_text(value):
    text = math.MathematicalText(value)
    text.format.font_height = 24
    text.format.latin_font = slides.FontData("Cambria Math")
    return text


with slides.Presentation() as presentation:
    presentation.slide_size.set_size(780, 200, slides.SlideSizeScaleType.DO_NOT_SCALE)
    slide = presentation.slides[0]
    alignments = [math.MathVerticalAlignment.TOP, math.MathVerticalAlignment.CENTER, math.MathVerticalAlignment.BOTTOM]
    labels = ["Top", "Center", "Bottom"]

    for i, alignment in enumerate(alignments):
        label = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 20 + i * 260, 10, 240, 30)
        label.fill_format.fill_type = slides.FillType.NO_FILL
        label.line_format.fill_format.fill_type = slides.FillType.NO_FILL
        label.text_frame.text = labels[i]
        label_format = label.text_frame.paragraphs[0].portions[0].portion_format
        label_format.font_height = 16
        label_format.fill_format.fill_type = slides.FillType.SOLID
        label_format.fill_format.solid_fill_color.color = slides.Color.black

        math_shape = slide.shapes.add_math_shape(20 + i * 260, 60, 240, 130)
        math_shape.text_frame.text_frame_format.anchoring_type = slides.TextAnchorType.TOP
        math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
        math_paragraph.justification = math.MathJustification.LEFT_JUSTIFIED

        rows = [create_text("x+y=3"), create_text("x-y=1"), create_text("x=2")]
        equation_array = math.MathArray(rows)
        equation_array.base_justification = alignment
        left_text = create_text("A=")
        right_text = create_text("=B")
        equation = math.MathBlock(left_text).join(equation_array).join(right_text)
        math_paragraph.add(equation)

    presentation.save("equation-array-alignment.pptx", slides.export.SaveFormat.PPTX)

المقارنة التالية تُظهر العرض المحفوظ في PowerPoint. لاحظ موضع A= و=B بجانب كل مصفوفة.

مصفوفات معادلات في PowerPoint مع محاذاة أعلى، وسط، وأسفل وعناصر مجاورة متطابقة

ضبط تباعد الصفوف

row_spacing_rule يحدد قاعدة التباعد للصفوف داخل المصفوفة. row_spacing يوفّر قيمة عددية لقواعد التباعد الصريحة ويُهمل عندما تُختار القواعد المسبقة.

للتباعد الدقيق، تُقاس القيمة بالنقاط. للتباعد النسبي، تُقاس بالنصف سطر: القيمة 3 في المثال تعني 1.5 سطر. عيّن الخاصيتين معًا عند تحديد قيمة صريحة؛ تغيير القيمة العددية وحدها لا يختار قاعدة تباعد.

المثال أدناه يحفظ خمس عروض تقديمية بنفس المعادلات، والمحاذاة، وإعدادات الخط. الأول ثلاثة يستخدمون القواعد المسبقة؛ الأخيران يظهران قيمًا صريحة.

import aspose.slides as slides
import aspose.slides.mathtext as math


def create_text(value):
    text = math.MathematicalText(value)
    text.format.font_height = 24
    text.format.latin_font = slides.FontData("Cambria Math")
    return text


settings = [
    (math.MathRowSpacingRule.SINGLE_LINE_GAP, 0),
    (math.MathRowSpacingRule.ONE_AND_A_HALF_LINE_GAP, 0),
    (math.MathRowSpacingRule.TWO_LINE_GAP, 0),
    (math.MathRowSpacingRule.EXACTLY, 20),
    (math.MathRowSpacingRule.MULTIPLE, 3)
]

for rule, spacing in settings:
    file_name = f"equation-array-{rule.name}.pptx"
    with slides.Presentation() as presentation:
        math_shape = presentation.slides[0].shapes.add_math_shape(20, 20, 600, 300)
        paragraph = math_shape.text_frame.paragraphs[0]
        paragraph.paragraph_format.space_before = 0
        paragraph.paragraph_format.space_after = 0
        math_paragraph = paragraph.portions[0].math_paragraph
        math_paragraph.justification = math.MathJustification.LEFT_JUSTIFIED

        rows = [create_text("x+y=3"), create_text("x-y=1"), create_text("x=2")]
        equation_array = math.MathArray(rows)
        equation_array.base_justification = math.MathVerticalAlignment.CENTER
        equation_array.row_spacing_rule = rule
        equation_array.row_spacing = spacing
        left_text = create_text("A=")
        right_text = create_text("=B")
        equation = math.MathBlock(left_text).join(equation_array).join(right_text)
        math_paragraph.add(equation)

        presentation.save(file_name, slides.export.SaveFormat.PPTX)

تباعد صفوف المصفوفة منفصل عن تباعد الفقرات. space_before وspace_after يضيفان مساحة حول الفقرة المحتوية، وليس بين صفوف المصفوفة. بالمثل، MathParagraph.justification يحدد موضع الفقرة الرياضية بالكامل أفقياً؛ لا يستبدل إعداد المحاذاة العمودية للمصفوفة.

إضافة دوال مثلثية

استخدم as_argument_of_function عندما يكون الوسيط هو العنصر الحالي ويُعرف اسم الدالة.

دالة مثلثية cos مطبقة على 2x

import aspose.slides as slides
import aspose.slides.mathtext as math

with slides.Presentation() as presentation:
    slide = presentation.slides[0]

    math_shape = slide.shapes.add_math_shape(20, 20, 700, 100)
    math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph

    cosine = math.MathematicalText("2x").as_argument_of_function(
        math.MathFunctionsOfOneArgument.COS
    )

    math_paragraph.add(math.MathBlock(cosine))

    presentation.save("trigonometric-function.pptx", slides.export.SaveFormat.PPTX)

إضافة موضعات وصيغ علوية

استخدم المساعدات للموضعات والصيغ العلوية للفهارس والأسس. عندما يجب أن تظهر الفهارس على الجانب الأيسر من القاعدة، استخدم set_sub_superscript_on_the_left.

حرف Y كبير مع موضع سفلي 1 وصيغة علوية n على الجانب الأيسر

import aspose.slides as slides
import aspose.slides.mathtext as math

with slides.Presentation() as presentation:
    slide = presentation.slides[0]

    math_shape = slide.shapes.add_math_shape(20, 20, 700, 100)
    math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph

    scripts = math.MathematicalText("Y").set_sub_superscript_on_the_left("1", "n")

    math_paragraph.add(math.MathBlock(scripts))

    presentation.save("subscript-superscript.pptx", slides.export.SaveFormat.PPTX)

محاذاة الموضعين السفلي والعلوي إلى اليمين

عند انتماء كلا الموضعين لنفس القاعدة، استخدم MathRightSubSuperscriptElement. الخاصية align_scripts تتحكم في مواضعهما الأفقية: False (القيمة الافتراضية) يتركهما يتبعان شكل القاعدة، بينما True يوازن الموضعين أفقياً مع بعضهما.

هذا المثال يضع نسختين من نفس التعبير القابل للتحرير جنبًا إلى جنب. كلاهما يستخدم الحرف المائل الرياضي f (U+1D453)، موضع سفلي 1، موضع علوي 2، وCambria Math بحجم 72 نقطة. القاعدة المائلة تجعل الفارق مرئيًا في PowerPoint. ثبّت Cambria Math قبل تشغيل المثال.

import aspose.slides as slides
import aspose.slides.mathtext as math

with slides.Presentation() as presentation:
    presentation.slide_size.set_size(900, 250, slides.SlideSizeScaleType.DO_NOT_SCALE)
    slide = presentation.slides[0]

    text_format = slides.PortionFormat()
    text_format.font_height = 72
    text_format.latin_font = slides.FontData("Cambria Math")

    for i in range(2):
        left = 40 + i * 440
        shape = slide.shapes.add_math_shape(left, 75, 380, 150)

        base_text = math.MathematicalText("\U0001D453", text_format)
        subscript = math.MathematicalText("1", text_format)
        superscript = math.MathematicalText("2", text_format)
        scripts = math.MathRightSubSuperscriptElement(base_text, subscript, superscript)
        scripts.align_scripts = i == 1

        portion = shape.text_frame.paragraphs[0].portions[0]
        portion.portion_format.font_height = text_format.font_height
        portion.portion_format.latin_font = text_format.latin_font
        portion.math_paragraph.add(math.MathBlock(scripts))

    presentation.save("aligned-scripts.pptx", slides.export.SaveFormat.PPTX)

تعبيران قابلان للتحرير في PowerPoint مع align_scripts = False على اليسار وTrue على اليمين

على اليسار، يبدأ الموضع السفلي أبعد إلى اليسار من الموضع العلوي، متبعًا شكل القاعدة. على اليمين، يتوازن الموضعان أفقيًا مع بعضهما.

إضافة محددات

استخدم enclose لتضمين تعبير داخل محددات. يمكنك أيضًا تعيين حرف فاصل لتعبيرات المحدد التي تحتوي على عدة عناصر.

تعبير محدد يحتوي على x وy وz مفصول بأشرطة عمودية

import aspose.slides as slides
import aspose.slides.mathtext as math

with slides.Presentation() as presentation:
    slide = presentation.slides[0]

    math_shape = slide.shapes.add_math_shape(20, 20, 700, 100)
    math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph

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

    math_paragraph.add(math.MathBlock(delimiter))

    presentation.save("delimiters.pptx", slides.export.SaveFormat.PPTX)

حجم وتمحور المحددات

عيّن grow_to_match_operand_height إلى True للسماح للأقواس بالنمو حول معامل طويل، أو إلى False للحفاظ على حجمها الطبيعي. delimiter_shape يأخذ إحدى قيمتي MathDelimiterShape. CENTERED يوسّط الأقواس على محور الرياضيات، الذي يمر عبر وسط علامة المساواة. MATCH يوازنها مع المحتوى المحاط. كلا الإعدادين يؤثران فقط على المحددات. يبقى التعبير داخلها كما هو.

المثال يضع كسرًا في أس طرف x بحيث يمتد التعبير أعلى القاعدة أكثر من أسفله. جميع المعادلات الأربعة تستخدم نفس الأقواس وإعدادات الخط.

import aspose.slides as slides
import aspose.slides.mathtext as math


def add_expression(slide, left, top, grow, shape):
    fraction = math.MathematicalText("a + b").divide("c + d")
    power = math.MathematicalText("x").set_superscript(fraction)
    delimiter = power.enclose("(", ")")
    delimiter.grow_to_match_operand_height = grow
    delimiter.delimiter_shape = shape

    math_shape = slide.shapes.add_math_shape(left, top, 300, 100)
    portion = math_shape.text_frame.paragraphs[0].portions[0]
    portion.portion_format.font_height = 32
    equation = math.MathematicalText("y = ").join(delimiter)
    portion.math_paragraph.add(equation)


with slides.Presentation() as presentation:
    slide = presentation.slides[0]
    add_expression(slide, 20, 20, True, math.MathDelimiterShape.CENTERED)
    add_expression(slide, 350, 20, True, math.MathDelimiterShape.MATCH)
    add_expression(slide, 20, 140, False, math.MathDelimiterShape.CENTERED)
    add_expression(slide, 350, 140, False, math.MathDelimiterShape.MATCH)

    presentation.save("delimiter-sizing.pptx", slides.export.SaveFormat.PPTX)

في PowerPoint، قارن الصفوف لرؤية تأثير النمو. في الصف العلوي، تنمو الأقواس لتحتوي الأس وتبدو متماثلة لكلا الشكلين. في الصف السفلي، يكون النمو معطلاً. الأقواس الأصغر على اليسار (CENTERED) تُوسّط مع علامة المساواة، بينما تلك على اليمين (MATCH) ترتفع حول التعبير.

أربع معادلات في PowerPoint مع تمكين نمو المحددات أعلاه وتعطيله أدناه، وCENTERED على اليسار وMATCH على اليمين

إضافة صندوق حدود

استدعِ to_border_box على كسر لتأطير التعبير بأكله. الكائن المرتجع MathBorderBox يظهر الأربعة جوانب بشكل افتراضي. استخدم hide_top، hide_bottom، hide_left، وhide_right لإزالة جوانب فردية.

المثال يضع ست نسخ من نفس الكسر على شريحة واحدة. لإظهار الإلغاء، أخفِ الأربعة جوانب واضبط strikethrough_bottom_left_to_top_right للحصول على قطري صاعد (↗) أو strikethrough_top_left_to_bottom_right للحصول على قطري هابط (↘). يمكنك أيضًا استخدام strikethrough_horizontal أو strikethrough_vertical مع أي تركيبة من الجوانب المرئية.

import aspose.slides as slides
import aspose.slides.mathtext as math


def add_fraction(slide, x, y, hide_border=False):
    fraction = math.MathematicalText("x+1").divide("y+2")
    box = fraction.to_border_box()
    box.hide_top = box.hide_bottom = box.hide_left = box.hide_right = hide_border

    shape = slide.shapes.add_math_shape(x, y, 160, 90)
    portion = shape.text_frame.paragraphs[0].portions[0]
    portion.portion_format.font_height = 28
    portion.math_paragraph.add(math.MathBlock(box))
    return box


with slides.Presentation() as presentation:
    presentation.slide_size.set_size(540, 220, slides.SlideSizeScaleType.DO_NOT_SCALE)
    slide = presentation.slides[0]

    add_fraction(slide, 10, 10)

    partial = add_fraction(slide, 190, 10)
    partial.hide_top = True
    partial.hide_right = True

    add_fraction(slide, 370, 10, hide_border=True).strikethrough_bottom_left_to_top_right = True
    add_fraction(slide, 10, 120, hide_border=True).strikethrough_top_left_to_bottom_right = True
    add_fraction(slide, 190, 120, hide_border=True).strikethrough_horizontal = True
    add_fraction(slide, 370, 120).strikethrough_vertical = True

    presentation.save("border-box.pptx", slides.export.SaveFormat.PPTX)

ستة كسور قابلة للتحرير في PowerPoint. الصف العلوي: حدود كاملة، الجوانب السفلية واليسرى، قطري صاعد. الصف السفلي: قطري هابط، شطب أفقي، شطب عمودي مع حد كامل.

على هذا الكسر، يتقاطع الشطب الأفقي مع شريط الكسر. الشطب العمودي يقطع البسط والمقام بينما يبقى الحد المحيط مرئيًا. هذه الخطوط تُزيّن التعبير بأكمله. شطب النص العادي يُطبق على الأحرف داخل تشغيل نص. بعد إعادة فتح العرض في PowerPoint، لا يزال بإمكانك تحرير البسط والمقام بشكل مستقل.

التحكم في الرؤية والتخطيط باستخدام عناصر الفانتوم

اغلف تعبيرًا داخل MathPhantom للتحكم في رؤيته والمساحة التي يساهم بها في المعادلة بشكل مستقل. خصائصه تتحكم في ما يلي:

Property Effect
show ضبط إلى False لإخفاء التعبير. أبعاده ما زالت تُساهم في التخطيط ما لم تُضبط الخصائص المقابلة أدناه إلى True.
zero_width اعتبار عرض التعبير الأفقي صفرًا، مما يسمح للعناصر المجاورة بالاقتراب.
zero_asc اعتبار ارتفاع الصعود (الجزء فوق الخط الأساسي) صفرًا. هذا قد يقلل المسافة المحجوزة فوق العناصر المجاورة.
zero_desc اعتبار انخفاض الهبوط (الجزء تحت الخط الأساسي) صفرًا. هذا قد يقترب شريط الكسر أو محتوى آخر أسفل التعبير من جيرانه.

الخط الأساسي هو الخط المرجعي الذي تجلس عليه الحروف الرياضية العادية. إخفاء المحتوى لا يزيل أبعاده، وتعيين بُعد كصفر لا يحذف التعبير. يبقى التعبير الأساسي متاحًا عبر base. عُد بـ show إلى True لإظهاره مرة أخرى؛ تبقى إعدادات الأبعاد كما هي.

المثال أدناه يُنشئ خمس متغيّرات جنبًا إلى جنب: كسر مرئي، كسر مخفي يُحافظ على المسافة، وثلاثة كسور مخفية مع معالجة بعد واحد كصفر. كل متغيّر يستخدم نفس التعبير المحيط وCambria Math بحجم 24 نقطة. جميع أشكال المعادلة لها نفس الموضع العلوي، مُحدّدًا بالخط الرمادي. وضع الفانتوم في بسط كسر خارجي يجعل تغييرات التخطيط العمودي أسهل رؤيتها.

import aspose.slides as slides
import aspose.slides.mathtext as math


def create_text(value):
    text = math.MathematicalText(value)
    text.format.font_height = 24
    text.format.latin_font = slides.FontData("Cambria Math")
    return text


with slides.Presentation() as presentation:
    presentation.slide_size.set_size(1000, 180, slides.SlideSizeScaleType.DO_NOT_SCALE)
    slide = presentation.slides[0]
    labels = ["Visible", "Hidden: reserves space", "Hidden: ZeroWidth", "Hidden: ZeroAsc", "Hidden: ZeroDesc"]

    guide = slide.shapes.add_auto_shape(slides.ShapeType.LINE, 10, 60, 980, 0)
    guide.line_format.fill_format.fill_type = slides.FillType.SOLID
    guide.line_format.fill_format.solid_fill_color.color = slides.Color.light_gray

    for i, label_text in enumerate(labels):
        left = 10 + i * 200
        label = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, left, 10, 190, 30)
        label.fill_format.fill_type = slides.FillType.NO_FILL
        label.line_format.fill_format.fill_type = slides.FillType.NO_FILL
        label.text_frame.text = label_text
        label_format = label.text_frame.paragraphs[0].portions[0].portion_format
        label_format.font_height = 14
        label_format.fill_format.fill_type = slides.FillType.SOLID
        label_format.fill_format.solid_fill_color.color = slides.Color.black

        fraction_numerator = create_text("a+b")
        fraction_denominator = create_text("c+d")
        fraction = math.MathFraction(fraction_numerator, fraction_denominator)
        phantom = math.MathPhantom(fraction)
        phantom.show = i == 0
        phantom.zero_width = i == 2
        phantom.zero_asc = i == 3
        phantom.zero_desc = i == 4

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

        shape = slide.shapes.add_math_shape(left, 60, 190, 100)
        shape.text_frame.text_frame_format.anchoring_type = slides.TextAnchorType.TOP
        math_paragraph = shape.text_frame.paragraphs[0].portions[0].math_paragraph
        math_paragraph.add(math.MathBlock(equation))

    presentation.save("math-phantom.pptx", slides.export.SaveFormat.PPTX)

خمسة معادلات في PowerPoint تقارن كسرًا مرئيًا، كسرًا مخفيًا يحتفظ بالمسافة، وكُسورات مخفية مع تمكين ZeroWidth أو ZeroAsc أو ZeroDesc

تجميع المصطلحات

استخدم group لوضع حرف تجميع فوق أو تحت تعبير. أضف حدًا لتسمية المصطلحات المُجمعة.

التعبير x + y مُجمّع مع تسمية أي نص تحته

import aspose.slides as slides
import aspose.slides.mathtext as math

with slides.Presentation() as presentation:
    slide = presentation.slides[0]

    math_shape = slide.shapes.add_math_shape(20, 20, 700, 120)
    math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph

    grouped = (
        math.MathematicalText("x + y")
        .group(chr(0x23DF), math.MathTopBotPositions.BOTTOM, math.MathTopBotPositions.TOP)
        .set_lower_limit("any text")
    )

    math_paragraph.add(math.MathBlock(grouped))

    presentation.save("grouped-terms.pptx", slides.export.SaveFormat.PPTX)

تنسيق عناصر الرياضيات

استخدم المساعدات التنسيقية فقط حيث توضح الصيغة. على سبيل المثال، overbar يضع شريطًا فوق عنصر رياضي.

تعبير رياضي ABC مع شريط فوقه

import aspose.slides as slides
import aspose.slides.mathtext as math

with slides.Presentation() as presentation:
    slide = presentation.slides[0]

    math_shape = slide.shapes.add_math_shape(20, 20, 700, 100)
    math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph

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

    math_paragraph.add(math.MathBlock(overbar))

    presentation.save("overbar.pptx", slides.export.SaveFormat.PPTX)

تنسيق النص في معادلة موجودة

لتنسيق النص عبر معادلة موجودة، تجول شجرة عناصر كل كتلة رياضية. MathElementBase.get_children يُعيد الأطفال المباشرين فقط، لذا استدعِه بشكل متكرر للوصول إلى النص داخل الكسور، المواضع، الجذور، والهياكل المتداخلة الأخرى. تحقق من العنصر الحالي وكذلك أطفاله لتضمين العنصر الابتدائي عندما يكون MathematicalText.

يفترض هذا المثال أن input.pptx يحتوي على معادلة مثل z = xᵢ / ∛y، مع كسر يحتوي على موضع سفلي في البسط وجذر في المقام. معالجة العناصر العليا فقط سيتخطى النص داخل الكسر. يجد المثال أجزاء الرياضيات في إطارات نص الشرائح، يصل إلى كتلها عبر MathPortion.math_paragraph، ويضبط font_height إلى 28 نقطة عبر خاصية format لكل عنصر نصي.

import aspose.slides as slides
import aspose.slides.mathtext as math


def format_math_text(element):
    if isinstance(element, math.MathematicalText):
        element.format.font_height = 28

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


with slides.Presentation("input.pptx") as presentation:
    for slide in presentation.slides:
        for shape in slide.shapes:
            if not isinstance(shape, slides.AutoShape) or shape.text_frame is None:
                continue

            for paragraph in shape.text_frame.paragraphs:
                for portion in paragraph.portions:
                    if isinstance(portion, math.MathPortion):
                        for block in portion.math_paragraph:
                            format_math_text(block)

    presentation.save("formatted-equations.pptx", slides.export.SaveFormat.PPTX)

العرض المحفوظ يحتوي على تنسيق النص المحدث في كل مستوى من كل معادلة معالجة، مع الحفاظ على محتوى النص والبنية الرياضية.

مرجع سريع

Task Main API
إنشاء نص رياضي MathematicalText
دمج عناصر MathElementBase.join
إنشاء كسور MathElementBase.divide
إضافة صفة فوقية أو سفلى set_superscript, set_subscript
إضافة دوال function, as_argument_of_function
إضافة جذور radical
إضافة حدود set_lower_limit, set_upper_limit
إضافة موضع يساري set_sub_superscript_on_the_left
إضافة جمعيات وتكاملات nary, integral
إضافة مصفوفات MathMatrix
إضافة مصفوفات معادلات to_math_array
إضافة محددات enclose
إضافة أشرطة وحدود overbar, to_border_box
تجميع المصطلحات group

FAQ

هل يمكن تعديل معادلة PowerPoint موجودة؟

نعم. افتح العرض، ابحث عن الشكل الذي يحتوي على MathPortion، احصل على خاصية math_paragraph، وقم بتحديث كتل الرياضيات في تلك الفقرة.

هل تُحفظ المعادلات كرياضيات PowerPoint قابلة للتحرير؟

نعم. عند حفظ ملف PPTX، يكتب Aspose.Slides المعادلة كقائمة محتوى Office Math قابلة للتعديل.

هل يمكن تصدير المعادلات إلى LaTeX؟

نعم. احصل على MathParagraph من MathPortion، ثم استدعِ MathParagraph.to_latex لتصديره مباشرة. لمثال كامل، راجع Export Math Equations from Presentations in Python via .NET.