เพิ่มสมการคณิตศาสตร์ในงานนำเสนอ PowerPoint ด้วย C++

ภาพรวม

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

ใน PowerPoint ผู้ใช้โดยทั่วไปเพิ่มสมการจาก Insert > Equation:

แท็บ Insert ของ PowerPoint พร้อมคำสั่ง Equation ที่เลือก

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

สไลด์ PowerPoint ที่มีสมการคณิตศาสตร์ที่แก้ไขได้

Aspose.Slides สร้างข้อความคณิตศาสตร์นั้นผ่านสามอ็อบเจ็กต์หลัก:

  • รูปร่างคณิตศาสตร์ที่สร้างด้วย AddMathShape, เป็นรูปร่างที่บรรจุสมการ
  • MathPortion เก็บเนื้อหาคณิตศาสตร์ภายในกรอบข้อความของรูปร่าง
  • MathParagraph มีหนึ่งหรือหลายอ็อบเจ็กต์ MathBlock

ตัวอย่างส่วนใหญ่ด้านล่างใช้ MathematicalText และเมธอดแบบ fluent จาก IMathElement เพื่อทำให้โค้ดสั้นและอ่านง่าย

สำหรับสถานการณ์การส่งออก MathML ดูที่ Export Math Equations from Presentations in C++

สร้างสมการ

ตัวอย่างนี้สร้างรูปร่างคณิตศาสตร์และเพิ่มทฤษฎีบทพีทาโกรัส:

สมการ c² = a² + b²

auto presentation = System::MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);

auto mathShape = slide->get_Shapes()->AddMathShape(20.0f, 20.0f, 700.0f, 120.0f);
auto mathPortion = System::ExplicitCast<MathPortion>(mathShape->get_TextFrame()->get_Paragraph(0)->get_Portion(0));
auto mathParagraph = mathPortion->get_MathParagraph();

auto equation = System::MakeObject<MathematicalText>(u"c")
        - >SetSuperscript(u"2")
        - >Join(u"=")
        - >Join(System::MakeObject<MathematicalText>(u"a")->SetSuperscript(u"2"))
        - >Join(u"+")
        - >Join(System::MakeObject<MathematicalText>(u"b")->SetSuperscript(u"2"));

mathParagraph->Add(equation);

presentation->Save(u"pythagorean-theorem.pptx", SaveFormat::Pptx);
presentation->Dispose();

เพิ่มส่วนเศษส่วน

ใช้ Divide เพื่อสร้างส่วนเศษส่วน คุณสามารถเลือกสไตล์ส่วนเศษส่วนด้วย MathFractionTypes

ส่วนเศษส่วนเอียงที่แสดง 1 ÷ x

auto presentation = System::MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);

auto mathShape = slide->get_Shapes()->AddMathShape(20.0f, 20.0f, 700.0f, 100.0f);
auto mathPortion = System::ExplicitCast<MathPortion>(mathShape->get_TextFrame()->get_Paragraph(0)->get_Portion(0));
auto mathParagraph = mathPortion->get_MathParagraph();

auto fraction = System::MakeObject<MathematicalText>(u"1")
        - >Divide(u"x", MathFractionTypes::Skewed);

mathParagraph->Add(System::MakeObject<MathBlock>(fraction));

presentation->Save(u"fraction.pptx", SaveFormat::Pptx);
presentation->Dispose();

สำหรับส่วนเศษส่วนแบบซ้อนกัน ใช้ MathFractionTypes::Bar:

auto stackedFraction = System::MakeObject<MathematicalText>(u"x + 1")->Divide(u"y - 1", MathFractionTypes::Bar);

เพิ่มราก

ใช้ Radical เพื่อสร้างรากที่สอง, รากที่สาม หรือรากอื่น ๆ ส่วนที่เป็นฐานคืออิลิเมนต์ปัจจุบัน, ส่วนอาร์กิวเมนต์คือลำดับของราก

นิพจน์ราก n‑th ที่มี x อยู่ใต้สัญลักษณ์ราก

auto presentation = System::MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);

auto mathShape = slide->get_Shapes()->AddMathShape(20.0f, 20.0f, 700.0f, 100.0f);
auto mathPortion = System::ExplicitCast<MathPortion>(mathShape->get_TextFrame()->get_Paragraph(0)->get_Portion(0));
auto mathParagraph = mathPortion->get_MathParagraph();

auto radical = System::MakeObject<MathematicalText>(u"x")
        - >Radical(u"n");

mathParagraph->Add(System::MakeObject<MathBlock>(radical));

presentation->Save(u"radical.pptx", SaveFormat::Pptx);
presentation->Dispose();

เพิ่มฟังก์ชันและขีดจำกัด

ใช้ AsArgumentOfFunction หรือ Function สำหรับฟังก์ชันเช่น sin(x), log(x) หรือชื่อฟังก์ชันที่กำหนดเอง สำหรับขีดจำกัด ให้ใส่ lim ใน MathLimit หรือใช้ SetLowerLimit

ขีดจำกัดของ x เมื่อ x เข้าใกล้อนันต์

auto presentation = System::MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);

auto mathShape = slide->get_Shapes()->AddMathShape(20.0f, 20.0f, 700.0f, 100.0f);
auto mathPortion = System::ExplicitCast<MathPortion>(mathShape->get_TextFrame()->get_Paragraph(0)->get_Portion(0));
auto mathParagraph = mathPortion->get_MathParagraph();

auto limit = System::MakeObject<MathematicalText>(u"lim")
        - >SetLowerLimit(u"x→∞")
        - >Function(u"x");

mathParagraph->Add(System::MakeObject<MathBlock>(limit));

presentation->Save(u"functions-and-limits.pptx", SaveFormat::Pptx);
presentation->Dispose();

สำหรับชื่อฟังก์ชันที่กำหนดเอง ให้ทำให้ชื่อฟังก์ชันเป็นอิลิเมนต์ปัจจุบัน:

auto customFunction = System::MakeObject<MathematicalText>(u"f")->Function(u"x + 1");

เพิ่มตัวดำเนินการ N-ary และการบูรณาการ

ใช้ Nary สำหรับการบวกแบบรวม, ยูเนียน, อินเทอร์เซคชัน และตัวดำเนินการขนาดใหญ่อื่น ๆ ใช้ Integral สำหรับการบูรณาการ ทั้งสองเมธอดให้คุณตั้งค่าขีดจำกัดล่างและบนได้

การบวกที่มีขีดจำกัดล่างและบน

auto presentation = System::MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);

auto mathShape = slide->get_Shapes()->AddMathShape(20.0f, 20.0f, 700.0f, 120.0f);
auto mathPortion = System::ExplicitCast<MathPortion>(mathShape->get_TextFrame()->get_Paragraph(0)->get_Portion(0));
auto mathParagraph = mathPortion->get_MathParagraph();

auto summationBase = System::MakeObject<MathematicalText>(u"x")
        - >SetSuperscript(u"k")
        - >Join(System::MakeObject<MathematicalText>(u"a")->SetSuperscript(u"n-k"));

auto summation = summationBase->Nary(MathNaryOperatorTypes::Summation, u"k=0", u"n");

mathParagraph->Add(System::MakeObject<MathBlock>(summation));

presentation->Save(u"nary-operators.pptx", SaveFormat::Pptx);
presentation->Dispose();

ตัวดำเนินการ N-ary ใช้สำหรับตัวดำเนินการขนาดใหญ่ที่อาจมีขีดจำกัด ตัวดำเนินการง่ายเช่น +, -, และ = มักเพิ่มเป็น MathematicalText แล้วต่อเข้ากับนิพจน์

สำหรับการบูรณาการ ใช้ Integral:

auto integralBase = System::MakeObject<MathematicalText>(u"x")->Join(System::MakeObject<MathematicalText>(u"dx")->ToBox());
auto integral = integralBase->Integral(MathIntegralTypes::Simple, u"0", u"1");

เพิ่มเมทริกซ์

ใช้ MathMatrix สำหรับแถวและคอลัมน์ เมทริกซ์โดยปกติไม่รวมวงเล็บ ดังนั้นให้ใส่วงเล็บ, โค้ง, หรือปีกเมื่อจำเป็น

เมทริกซ์คณิตศาสตร์สองแถวที่มีเซลล์ว่างหนึ่งช่อง

auto presentation = System::MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);

auto mathShape = slide->get_Shapes()->AddMathShape(20.0f, 20.0f, 700.0f, 120.0f);
auto mathPortion = System::ExplicitCast<MathPortion>(mathShape->get_TextFrame()->get_Paragraph(0)->get_Portion(0));
auto mathParagraph = mathPortion->get_MathParagraph();

auto matrix = System::MakeObject<MathMatrix>(2, 3);
matrix->idx_set(0, 0, System::MakeObject<MathematicalText>(u"1"));
matrix->idx_set(0, 1, System::MakeObject<MathematicalText>(u"x"));
matrix->idx_set(1, 0, System::MakeObject<MathematicalText>(u"x"));
matrix->idx_set(1, 1, System::MakeObject<MathematicalText>(u"2"));
matrix->idx_set(1, 2, System::MakeObject<MathematicalText>(u"y"));

mathParagraph->Add(System::MakeObject<MathBlock>(matrix));

presentation->Save(u"matrix.pptx", SaveFormat::Pptx);
presentation->Dispose();

เพิ่มอาเรย์สมการ

ใช้ ToMathArray เมื่อคุณต้องการสมการที่จัดเรียงหรือสแต็กแนวตั้งของนิพจน์

อาเรย์คณิตศาสตร์แนวตั้งที่มี x อยู่เหนือ y

auto presentation = System::MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);

auto mathShape = slide->get_Shapes()->AddMathShape(20.0f, 20.0f, 700.0f, 140.0f);
auto mathPortion = System::ExplicitCast<MathPortion>(mathShape->get_TextFrame()->get_Paragraph(0)->get_Portion(0));
auto mathParagraph = mathPortion->get_MathParagraph();

auto equationArray = System::MakeObject<MathematicalText>(u"x")
        - >Join(u"y")
        - >ToMathArray();

mathParagraph->Add(System::MakeObject<MathBlock>(equationArray));

presentation->Save(u"equation-array.pptx", SaveFormat::Pptx);
presentation->Dispose();

เพิ่มฟังก์ชันตรีโกณมิติ

ใช้ AsArgumentOfFunction เมื่ออาร์กิวเมนต์คืออิลิเมนต์ปัจจุบันและชื่อฟังก์ชันรู้จักแล้ว

ฟังก์ชันตรีโกณมิติ cos ที่ใช้กับ 2x

auto presentation = System::MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);

auto mathShape = slide->get_Shapes()->AddMathShape(20.0f, 20.0f, 700.0f, 100.0f);
auto mathPortion = System::ExplicitCast<MathPortion>(mathShape->get_TextFrame()->get_Paragraph(0)->get_Portion(0));
auto mathParagraph = mathPortion->get_MathParagraph();

auto cosine = System::MakeObject<MathematicalText>(u"2x")
        - >AsArgumentOfFunction(MathFunctionsOfOneArgument::Cos);

mathParagraph->Add(System::MakeObject<MathBlock>(cosine));

presentation->Save(u"trigonometric-function.pptx", SaveFormat::Pptx);
presentation->Dispose();

เพิ่มตัวห้อยและตัวบน

ใช้ตัวช่วยสำหรับ subscript และ superscript เพื่อจัดทำดัชนีและพาวเวอร์ เมื่อดัชนีต้องปรากฏทางซ้ายของฐาน ให้ใช้ SetSubSuperscriptOnTheLeft

อักษร Y ตัวพิมพ์ใหญ่ที่มี subscript ด้านซ้าย 1 และ superscript n

auto presentation = System::MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);

auto mathShape = slide->get_Shapes()->AddMathShape(20.0f, 20.0f, 700.0f, 100.0f);
auto mathPortion = System::ExplicitCast<MathPortion>(mathShape->get_TextFrame()->get_Paragraph(0)->get_Portion(0));
auto mathParagraph = mathPortion->get_MathParagraph();

auto scripts = System::MakeObject<MathematicalText>(u"Y")
        - >SetSubSuperscriptOnTheLeft(u"1", u"n");

mathParagraph->Add(System::MakeObject<MathBlock>(scripts));

presentation->Save(u"subscript-superscript.pptx", SaveFormat::Pptx);
presentation->Dispose();

เพิ่มตัวจำกัด

ใช้ Enclose เพื่อใส่นิพจน์ภายในตัวจำกัด คุณยังสามารถตั้งค่าอักขระคั่นสำหรับนิพจน์ที่มีหลายอิลิเมนต์

นิพจน์ที่มีตัวจำกัดประกอบด้วย x, y, และ z ที่คั่นด้วยเส้นตั้ง

auto presentation = System::MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);

auto mathShape = slide->get_Shapes()->AddMathShape(20.0f, 20.0f, 700.0f, 100.0f);
auto mathPortion = System::ExplicitCast<MathPortion>(mathShape->get_TextFrame()->get_Paragraph(0)->get_Portion(0));
auto mathParagraph = mathPortion->get_MathParagraph();

auto delimiter = System::MakeObject<MathematicalText>(u"x")
        - >Join(u"y")
        - >Join(u"z")
        - >Enclose(u'<', u'>', u'|');

mathParagraph->Add(System::MakeObject<MathBlock>(delimiter));

presentation->Save(u"delimiters.pptx", SaveFormat::Pptx);
presentation->Dispose();

เพิ่มกล่องกรอบ

ใช้ ToBorderBox เมื่อสมการเองควรอยู่ในกรอบ

สมการที่อยู่ในกล่องแสดง a² = b² + c²

auto presentation = System::MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);

auto mathShape = slide->get_Shapes()->AddMathShape(20.0f, 20.0f, 700.0f, 100.0f);
auto mathPortion = System::ExplicitCast<MathPortion>(mathShape->get_TextFrame()->get_Paragraph(0)->get_Portion(0));
auto mathParagraph = mathPortion->get_MathParagraph();

auto boxedEquation = System::MakeObject<MathematicalText>(u"a")
        - >SetSuperscript(u"2")
        - >Join(u"=")
        - >Join(System::MakeObject<MathematicalText>(u"b")->SetSuperscript(u"2"))
        - >Join(u"+")
        - >Join(System::MakeObject<MathematicalText>(u"c")->SetSuperscript(u"2"))
        - >ToBorderBox();

mathParagraph->Add(System::MakeObject<MathBlock>(boxedEquation));

presentation->Save(u"border-box.pptx", SaveFormat::Pptx);
presentation->Dispose();

จัดกลุ่มเทอม

ใช้ Group เพื่อวางอักขระการจัดกลุ่มเหนือหรือใต้สูตร เพิ่มขีดจำกัดเพื่อใส่ป้ายกำกับให้เทอมที่จัดกลุ่ม

นิพจน์ x + y ที่จัดกลุ่มพร้อมป้ายกำกับข้อความใด ๆ ด้านล่าง

auto presentation = System::MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);

auto mathShape = slide->get_Shapes()->AddMathShape(20.0f, 20.0f, 700.0f, 120.0f);
auto mathPortion = System::ExplicitCast<MathPortion>(mathShape->get_TextFrame()->get_Paragraph(0)->get_Portion(0));
auto mathParagraph = mathPortion->get_MathParagraph();

auto grouped = System::MakeObject<MathematicalText>(u"x + y")
        - >Group(u'\u23DF', MathTopBotPositions::Bottom, MathTopBotPositions::Top)
        - >SetLowerLimit(u"any text");

mathParagraph->Add(System::MakeObject<MathBlock>(grouped));

presentation->Save(u"grouped-terms.pptx", SaveFormat::Pptx);
presentation->Dispose();

จัดรูปแบบองค์ประกอบคณิตศาสตร์

ใช้ตัวช่วยการจัดรูปแบบเฉพาะที่ช่วยให้สูตรชัดเจน เช่น Overbar จะวางเส้นขาบนอิลิเมนต์คณิตศาสตร์

นิพจน์คณิตศาสตร์ ABC ที่มีเส้นขาบน

auto presentation = System::MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);

auto mathShape = slide->get_Shapes()->AddMathShape(20.0f, 20.0f, 700.0f, 100.0f);
auto mathPortion = System::ExplicitCast<MathPortion>(mathShape->get_TextFrame()->get_Paragraph(0)->get_Portion(0));
auto mathParagraph = mathPortion->get_MathParagraph();

auto overbar = System::MakeObject<MathematicalText>(u"ABC")->Overbar();

mathParagraph->Add(System::MakeObject<MathBlock>(overbar));

presentation->Save(u"overbar.pptx", SaveFormat::Pptx);
presentation->Dispose();

อ้างอิงอย่างรวดเร็ว

งาน 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, ดึง MathParagraph ของมัน, แล้วอัปเดต MathBlock ภายในพารากราฟนั้น

สมการถูกบันทึกเป็นคณิตศาสตร์ PowerPoint ที่แก้ไขได้หรือไม่?

ใช่ เมื่อบันทึกเป็น PPTX, Aspose.Slides จะเขียนสมการเป็นเนื้อหา Office Math ที่สามารถแก้ไขได้

ฉันสามารถส่งออกสมการเป็น LaTeX ได้หรือไม่?

Aspose.Slides ส่งออกสมการเป็น MathML หากต้องการ LaTeX ให้ส่งออกเป็น MathML ก่อน แล้วใช้เครื่องมือแปลง MathML ไปเป็น LaTeX ตามรูปแบบที่คุณต้องการ