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

نظرة عامة

PowerPoint يخزن المعادلات كـ Office Math Markup Language (OMML). باستخدام Aspose.Slides for Java، يمكنك إنشاء نفس نوع محتوى الرياضيات برمجيًا: الكسور، الجذور، الدوال، الحدود، العوامل N-ary، المصفوفات، المصفوفات المتعددة، وكتل الرياضيات المنسقة.

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

PowerPoint Insert tab with the Equation command selected

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

A PowerPoint slide containing an editable math equation

Aspose.Slides يبني ذلك النص الرياضي من خلال ثلاثة كائنات رئيسية:

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

معظم الأمثلة أدناه تستخدم MathematicalText والطرق المتسلسلة من IMathElement للحفاظ على شفرة قصيرة وسهلة القراءة.

للمشاهد المتعلقة بتصدير MathML، راجع Export Math Equations from Presentations in Java.

إنشاء معادلة

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

The equation c squared equals a squared plus b squared

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 120);
    IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
            .get_Item(0).getPortions().get_Item(0)).getMathParagraph();

    IMathBlock 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 {
    presentation.dispose();
}

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

عندما تُبنى أجزاء المعادلة بشكل مستقل ككائنات IMathBlock، استخدم joinBlock لدمجها. المثال يبني c² = في الكتلة اليسرى وa² + b² في الكتلة اليمنى. setSuperscript يُنشئ صيغًا علوية قابلة للتحرير لجميع القوى الثلاث.

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape shape = slide.getShapes().addMathShape(20, 20, 600, 100);
    IMathPortion mathPortion = (IMathPortion) shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
    IMathParagraph mathParagraph = mathPortion.getMathParagraph();

    MathBlock leftBlock = new MathBlock();
    leftBlock.add(new MathematicalText("c").setSuperscript("2"));
    leftBlock.add(new MathematicalText("="));

    MathBlock rightBlock = new MathBlock();
    rightBlock.add(new MathematicalText("a").setSuperscript("2"));
    rightBlock.add(new MathematicalText("+"));
    rightBlock.add(new MathematicalText("b").setSuperscript("2"));

    IMathBlock combinedBlock = leftBlock.joinBlock(rightBlock);
    mathParagraph.add(combinedBlock);

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

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

استدعاء add كـ leftBlock.add(rightBlock) يحاول بدلاً من ذلك تضمين كتلة داخل أخرى مباشرةً ويسبب إلقاء InvalidOperationException.

إضافة كسر

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

A skewed math fraction showing one divided by x

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
    IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
            .get_Item(0).getPortions().get_Item(0)).getMathParagraph();

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

    mathParagraph.add(new MathBlock(fraction));

    presentation.save("fraction.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

للكسر المتراص، استخدم MathFractionTypes.Bar:

import com.aspose.slides.*;

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

إضافة جذور

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

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

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
    IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
            .get_Item(0).getPortions().get_Item(0)).getMathParagraph();

    IMathRadical radical = new MathematicalText("x")
            .radical("n");

    mathParagraph.add(new MathBlock(radical));

    presentation.save("radical.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

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

يمكن للجذر التربيعي إظهار رقم 2 صريحًا أو استخدام الترميز التقليدي √x. طريقة setHideDegree في IMathRadical تتحكم في الشكل الظاهر. كلا الشكلين يحتفظان بالدرجة 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 يكون مخفيًا.

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    
    IMathRadical radical = new MathematicalText("x").radical("2");

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

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
    IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
            .get_Item(0).getPortions().get_Item(0)).getMathParagraph();

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

    mathParagraph.add(new MathBlock(limit));

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

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

import com.aspose.slides.*;

IMathFunction customFunction = new MathematicalText("f").function("x + 1");

إضافة عوامل N-ary وتكاملات

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

A summation with lower and upper limits

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 120);
    IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
            .get_Item(0).getPortions().get_Item(0)).getMathParagraph();

    IMathBlock summationBase = new MathematicalText("x")
            .setSuperscript("k")
            .join(new MathematicalText("a").setSuperscript("n-k"));

    IMathNaryOperator summation = summationBase.nary(MathNaryOperatorTypes.Summation, "k=0", "n");

    mathParagraph.add(new MathBlock(summation));

    presentation.save("nary-operators.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

العوامل N-ary مخصصة للعوامل الكبيرة مع حدود اختيارية. العوامل البسيطة مثل +، -، و = عادةً ما تُضاف كـ MathematicalText وتُدمج في التعبير.

للتكامل، استخدم integral:

import com.aspose.slides.*;

IMathBlock integralBase = new MathematicalText("x").join(new MathematicalText("dx").toBox());
IMathNaryOperator integral = integralBase.integral(MathIntegralTypes.Simple, "0", "1");

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

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

A two-row math matrix with one empty cell

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 120);
    IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
            .get_Item(0).getPortions().get_Item(0)).getMathParagraph();

    MathMatrix 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 {
    presentation.dispose();
}

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

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

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

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

import com.aspose.slides.*;

Presentation presentation = new Presentation("matrix.pptx");
try {
    IAutoShape shape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
    IMathPortion portion = (IMathPortion) shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
    IMathMatrix matrix = (IMathMatrix) 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, 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"));
    for (int row = 0; row < matrix.getRowCount(); row++) {
        for (int column = 0; column < matrix.getColumnCount(); column++) {
            if (matrix.get_Item(row, column) == null) {
                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

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 140);
    IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
            .get_Item(0).getPortions().get_Item(0)).getMathParagraph();

    IMathArray equationArray = new MathematicalText("x")
            .join("y")
            .toMathArray();

    mathParagraph.add(new MathBlock(equationArray));

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

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

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

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

import com.aspose.slides.*;
import java.awt.Color;

class MathTextFactory {
    MathematicalText createText(String value) {
        MathematicalText text = new MathematicalText(value);
        text.getFormat().setFontHeight(24);
        text.getFormat().setLatinFont(new FontData("Cambria Math"));
        return text;
    }
}

MathTextFactory factory = new MathTextFactory();
Presentation presentation = new Presentation();
try {
    presentation.getSlideSize().setSize(780, 200, SlideSizeScaleType.DoNotScale);
    ISlide slide = presentation.getSlides().get_Item(0);
    
    int[] alignments = { MathVerticalAlignment.Top, MathVerticalAlignment.Center, MathVerticalAlignment.Bottom };
    String[] alignmentNames = { "Top", "Center", "Bottom" };

    for (int i = 0; i < alignments.length; i++) {
        IAutoShape label = slide.getShapes().addAutoShape(ShapeType.Rectangle, 20 + i * 260, 10, 240, 30);
        label.getFillFormat().setFillType(FillType.NoFill);
        label.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
        label.getTextFrame().setText(alignmentNames[i]);
        IPortionFormat labelFormat = label.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat();
        labelFormat.setFontHeight(16);
        labelFormat.getFillFormat().setFillType(FillType.Solid);
        labelFormat.getFillFormat().getSolidFillColor().setColor(Color.BLACK);

        IAutoShape mathShape = slide.getShapes().addMathShape(20 + i * 260, 60, 240, 130);
        mathShape.getTextFrame().getTextFrameFormat().setAnchoringType(TextAnchorType.Top);
        IMathParagraph mathParagraph = ((IMathPortion) mathShape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0)).getMathParagraph();
        mathParagraph.setJustification(MathJustification.LeftJustified);

        IMathElement[] rows = { factory.createText("x+y=3"), factory.createText("x-y=1"), factory.createText("x=2") };
        MathArray equationArray = new MathArray(rows[0]);
        equationArray.getArguments().add(rows[1]);
        equationArray.getArguments().add(rows[2]);
        equationArray.setBaseJustification(alignments[i]);
        MathematicalText leftText = factory.createText("A=");
        MathematicalText rightText = factory.createText("=B");
        IMathBlock equation = new MathBlock(leftText).join(equationArray).join(rightText);
        mathParagraph.add(equation);
    }

    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 سطر. عيّن الخصيصيتين معًا عند تحديد قيمة صريحة؛ تغيير القيمة الرقمية فقط لا يُحدِّد قاعدة التباعد.

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

import com.aspose.slides.*;

class MathTextFactory {
    MathematicalText createText(String value) {
        MathematicalText text = new MathematicalText(value);
        text.getFormat().setFontHeight(24);
        text.getFormat().setLatinFont(new FontData("Cambria Math"));
        return text;
    }
}

MathTextFactory factory = new MathTextFactory();
int[] rules = { MathRowSpacingRule.SingleLineGap, MathRowSpacingRule.OneAndAHalfLineGap, MathRowSpacingRule.TwoLineGap, MathRowSpacingRule.Exactly, MathRowSpacingRule.Multiple };
int[] spacingValues = { 0, 0, 0, 20, 3 };
String[] ruleNames = { "SingleLineGap", "OneAndAHalfLineGap", "TwoLineGap", "Exactly", "Multiple" };

for (int i = 0; i < rules.length; i++) {
    String fileName = "equation-array-" + ruleNames[i] + ".pptx";
    Presentation presentation = new Presentation();
    try {
        IAutoShape mathShape = presentation.getSlides().get_Item(0).getShapes().addMathShape(20, 20, 600, 300);
        IParagraph paragraph = mathShape.getTextFrame().getParagraphs().get_Item(0);
        paragraph.getParagraphFormat().setSpaceBefore(0);
        paragraph.getParagraphFormat().setSpaceAfter(0);
        IMathParagraph mathParagraph = ((IMathPortion) paragraph.getPortions().get_Item(0)).getMathParagraph();
        mathParagraph.setJustification(MathJustification.LeftJustified);

        IMathElement[] rows = { factory.createText("x+y=3"), factory.createText("x-y=1"), factory.createText("x=2") };
        MathArray equationArray = new MathArray(rows[0]);
        equationArray.getArguments().add(rows[1]);
        equationArray.getArguments().add(rows[2]);
        equationArray.setBaseJustification(MathVerticalAlignment.Center);
        equationArray.setRowSpacingRule(rules[i]);
        equationArray.setRowSpacing(spacingValues[i]);
        MathematicalText leftText = factory.createText("A=");
        MathematicalText rightText = factory.createText("=B");
        IMathBlock equation = new MathBlock(leftText).join(equationArray).join(rightText);
        mathParagraph.add(equation);

        presentation.save(fileName, SaveFormat.Pptx);
    } finally {
        presentation.dispose();
    }
}

تباعد صفوف المصفوفة منفصل عن تباعد الفقرات. setSpaceBefore و setSpaceAfter يضيفان مساحة حول الفقرة المحتوية، لا بين صفوف المصفوفة. بالمثل، IMathParagraph.setJustification يحدد موضع الفقرة الرياضية بالكامل أفقياً؛ ولا يُستبدل بإعداد المحاذاة الرأسية للمصفوفة.

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

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

The trigonometric function cos applied to 2x

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
    IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
            .get_Item(0).getPortions().get_Item(0)).getMathParagraph();

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

    mathParagraph.add(new MathBlock(cosine));

    presentation.save("trigonometric-function.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

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

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

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

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
    IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
            .get_Item(0).getPortions().get_Item(0)).getMathParagraph();

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

    mathParagraph.add(new MathBlock(scripts));

    presentation.save("subscript-superscript.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

محاذاة المؤشرات والصيغ العلوية اليمنى

عند انتماء كل من المؤشرين إلى نفس الأساس، استخدم MathRightSubSuperscriptElement. طريقة setAlignScripts تتحكم في مواضعهما الأفقية: false (الإعداد الافتراضي) يتركانهما يتبعان شكل الأساس، بينما true يَمحاذِهما أفقياً معًا.

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

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    presentation.getSlideSize().setSize(900, 250, SlideSizeScaleType.DoNotScale);
    ISlide slide = presentation.getSlides().get_Item(0);

    PortionFormat format = new PortionFormat();
    format.setFontHeight(72);
    format.setLatinFont(new FontData("Cambria Math"));

    for (int i = 0; i < 2; i++) {
        int left = 40 + i * 440;
        IAutoShape shape = slide.getShapes().addMathShape(left, 75, 380, 150);

        MathematicalText baseText = new MathematicalText("\uD835\uDC53", format);
        MathematicalText subscript = new MathematicalText("1", format);
        MathematicalText superscript = new MathematicalText("2", format);
        MathRightSubSuperscriptElement scripts = new MathRightSubSuperscriptElement(baseText, subscript, superscript);
        scripts.setAlignScripts(i == 1);

        MathPortion portion = (MathPortion) shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
        portion.getPortionFormat().setFontHeight(format.getFontHeight());
        portion.getPortionFormat().setLatinFont(format.getLatinFont());
        portion.getMathParagraph().add(new MathBlock(scripts));
    }

    presentation.save("aligned-scripts.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

Two editable expressions in PowerPoint with setAlignScripts set to false on the left and true on the right

في الجانب الأيسر، يبدأ المؤشر السفلي أكثر إلى اليسار من المؤشر العلوي، متبعًا شكل الأساس. في الجانب الأيمن، المؤشران يُحاذيان أفقياً معًا.

إضافة محددات

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

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

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
    IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
            .get_Item(0).getPortions().get_Item(0)).getMathParagraph();

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

    mathParagraph.add(new MathBlock(delimiter));

    presentation.save("delimiters.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

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

مرّر true إلى setGrowToMatchOperandHeight لتسمح للأقواس بالنمو حول مُعامل طويل، أو false للحفاظ على حجمها الطبيعي. استخدم setDelimiterShape مع إحدى قيمتي MathDelimiterShape. Centered يوسّط الأقواس على محور الرياضيات، الذي يمر عبر منتصف علامة المساواة. Match يضبطها لتطابق المحتوى المُحاط. كلا الإعدادين يؤثران فقط على المحددات. يبقى التعبير الداخلي كما هو.

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

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    for (int row = 0; row < 2; row++) {
        for (int column = 0; column < 2; column++) {
            IMathFraction fraction = new MathematicalText("a + b").divide("c + d");
            IMathSuperscriptElement power = new MathematicalText("x").setSuperscript(fraction);
            IMathDelimiter delimiter = power.enclose('(', ')');
            delimiter.setGrowToMatchOperandHeight(row == 0);
            delimiter.setDelimiterShape(column == 0 ? MathDelimiterShape.Centered : MathDelimiterShape.Match);

            IAutoShape mathShape = slide.getShapes().addMathShape(20 + 330 * column, 20 + 120 * row, 300, 100);
            MathPortion portion = (MathPortion) mathShape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
            portion.getPortionFormat().setFontHeight(32);
            IMathBlock 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 على كسر لإحاطة التعبير بالكامل. الـ IMathBorderBox المُعاد يُظهر الجوانب الأربعة افتراضيًا. استخدم setHideTop، setHideBottom، setHideLeft، و setHideRight لإزالة جوانب معينة.

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

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    presentation.getSlideSize().setSize(540, 220, SlideSizeScaleType.DoNotScale);
    ISlide slide = presentation.getSlides().get_Item(0);

    class FractionBuilder {
        IMathBorderBox addFraction(float x, float y, boolean hideBorder) {
            IMathFraction fraction = new MathematicalText("x+1").divide("y+2");
            IMathBorderBox box = fraction.toBorderBox();
            box.setHideTop(hideBorder);
            box.setHideBottom(hideBorder);
            box.setHideLeft(hideBorder);
            box.setHideRight(hideBorder);

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

    FractionBuilder builder = new FractionBuilder();
    builder.addFraction(10, 10, false);

    IMathBorderBox partial = builder.addFraction(190, 10, false);
    partial.setHideTop(true);
    partial.setHideRight(true);

    builder.addFraction(370, 10, true).setStrikethroughBottomLeftToTopRight(true);
    builder.addFraction(10, 120, true).setStrikethroughTopLeftToBottomRight(true);
    builder.addFraction(190, 120, true).setStrikethroughHorizontal(true);
    builder.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 للتحكم في رؤيته والمساحة التي يساهم بها في المعادلة بشكل مستقل. طرق IMathPhantom تتحكم فيما يلي:

Method Effect
setShow مرّر false لإخفاء التعبير. أبعاده لا تزال تساهم في التخطيط ما لم تستقبل الطرق التي تلي ذلك true.
setZeroWidth عالج العرض الأفقي للتعبير كصفر، مما يسمح للعناصر المجاورة بالاقتراب أكثر.
setZeroAsc عالج الارتفاع فوق الخط الأساسي كصفر. هذا قد يقلل المسافة المحجوزة فوق العناصر المجاورة.
setZeroDesc عالج النزول تحت الخط الأساسي كصفر. هذا قد يقترب شريط الكسر أو المحتوى الآخر أسفل التعبير من جيرانه.

الخط الأساسي هو الخط المرجعي الذي تجلس عليه الأحرف الرياضية العادية. إخفاء المحتوى لا يزيل أبعاده، ومعاملة بعدٍ كصفر لا تحذف التعبير. يبقى التعبير الأساسي متاحًا عبر getBase. مرّر true إلى setShow لعرضه مرة أخرى؛ تبقى إعدادات الأبعاد كما هي.

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

import com.aspose.slides.*;
import java.awt.Color;

class MathTextFactory {
    MathematicalText createText(String value) {
        MathematicalText text = new MathematicalText(value);
        text.getFormat().setFontHeight(24);
        text.getFormat().setLatinFont(new FontData("Cambria Math"));
        return text;
    }
}

MathTextFactory factory = new MathTextFactory();
Presentation presentation = new Presentation();
try {
    presentation.getSlideSize().setSize(1000, 180, SlideSizeScaleType.DoNotScale);
    ISlide slide = presentation.getSlides().get_Item(0);

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

    IAutoShape 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 (int i = 0; i < labels.length; i++) {
        int left = 10 + i * 200;

        IAutoShape label = slide.getShapes().addAutoShape(ShapeType.Rectangle, left, 10, 190, 30);
        label.getFillFormat().setFillType(FillType.NoFill);
        label.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
        label.getTextFrame().setText(labels[i]);

        IPortionFormat labelFormat = label.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat();
        labelFormat.setFontHeight(14);
        labelFormat.getFillFormat().setFillType(FillType.Solid);
        labelFormat.getFillFormat().getSolidFillColor().setColor(Color.BLACK);

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

        MathematicalText leftText = factory.createText("x+");
        MathematicalText rightText = factory.createText("+y");
        IMathBlock numerator = new MathBlock(leftText).join(phantom).join(rightText);
        MathematicalText denominator = factory.createText("z");
        MathFraction equation = new MathFraction(numerator, denominator);

        IAutoShape shape = slide.getShapes().addMathShape(left, 60, 190, 100);
        shape.getTextFrame().getTextFrameFormat().setAnchoringType(TextAnchorType.Top);

        IMathPortion mathPortion = (IMathPortion) shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
        IMathParagraph mathParagraph = mathPortion.getMathParagraph();
        mathParagraph.add(new MathBlock(equation));
    }

    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

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 120);
    IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
            .get_Item(0).getPortions().get_Item(0)).getMathParagraph();

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

    mathParagraph.add(new MathBlock(grouped));

    presentation.save("grouped-terms.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

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

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

A math expression ABC with an overbar

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
    IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
            .get_Item(0).getPortions().get_Item(0)).getMathParagraph();

    IMathBar overbar = new MathematicalText("ABC").overbar();

    mathParagraph.add(new MathBlock(overbar));

    presentation.save("overbar.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

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

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

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

import com.aspose.slides.*;

class MathTextFormatter {
    void formatMathText(IMathElement element) {
        if (element instanceof MathematicalText) {
            MathematicalText text = (MathematicalText) element;
            text.getFormat().setFontHeight(28);
        }

        for (IMathElement child : element.getChildren()) {
            formatMathText(child);
        }
    }
}

MathTextFormatter formatter = new MathTextFormatter();
Presentation presentation = new Presentation("input.pptx");
try {
    for (ISlide slide : presentation.getSlides()) {
        for (IShape shape : slide.getShapes()) {
            if (!(shape instanceof IAutoShape)) {
                continue;
            }

            IAutoShape autoShape = (IAutoShape) shape;
            if (autoShape.getTextFrame() == null) {
                continue;
            }

            for (IParagraph paragraph : autoShape.getTextFrame().getParagraphs()) {
                for (IPortion portion : paragraph.getPortions()) {
                    if (portion instanceof IMathPortion) {
                        IMathPortion mathPortion = (IMathPortion) portion;
                        for (IMathBlock block : mathPortion.getMathParagraph()) {
                            formatter.formatMathText(block);
                        }
                    }
                }
            }
        }
    }

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

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

المرجع السريع

Task Main API
إنشاء نص رياضي MathematicalText
دمج العناصر IMathElement.join
إنشاء كسور IMathElement.divide
إضافة صِيغَة علوية أو سفلية setSuperscript, setSubscript
إضافة دوال function, asArgumentOfFunction
إضافة جذور IMathElement.radical
إضافة حدود setLowerLimit, setUpperLimit
إضافة مؤشرات على الجانب الأيسر setSubSuperscriptOnTheLeft
إضافة جمعيات وتكاملات nary, integral
إضافة مصفوفات MathMatrix
إضافة مصفوفات معادلات toMathArray
إضافة محددات enclose
إضافة أشرطة وحدود overbar, toBorderBox
تجميع المصطلحات group

الأسئلة المتكررة

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

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

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

نعم. عند حفظ الملف بصيغة PPTX، يكتب Aspose.Slides المعادلة كمحتوى رياضي Office قابل للتحرير.

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

نعم. احصل على IMathParagraph للمعادلة من IMathPortion، واستدعِ IMathParagraph.toLatex لتصديره مباشرة. لمثال كامل، راجع Export Math Equations from Presentations in Java.