Thêm Phương Trình Toán Học vào Bản Trình Chiếu PowerPoint trong C++
Tổng quan
PowerPoint lưu các phương trình dưới dạng Office Math Markup Language (OMML). Với Aspose.Slides cho C++, bạn có thể tạo cùng loại nội dung toán học một cách lập trình: phân số, căn bậc, hàm, giới hạn, toán tử N-ary, ma trận, mảng và các khối toán học được định dạng.
Trong PowerPoint, người dùng thường thêm phương trình bằng cách vào Insert > Equation:

Kết quả là văn bản toán học có thể chỉnh sửa trên slide:

Aspose.Slides xây dựng văn bản toán học đó thông qua ba đối tượng chính:
- Một hình toán học, được tạo bằng AddMathShape, là hình chứa phương trình.
- MathPortion lưu trữ nội dung toán học trong khung văn bản của hình.
- MathParagraph chứa một hoặc nhiều đối tượng MathBlock.
Hầu hết các ví dụ bên dưới sử dụng MathematicalText và các phương thức fluent từ IMathElement để giữ cho mã ngắn gọn và dễ đọc.
Đối với các kịch bản xuất MathML, xem Export Math Equations from Presentations in C++.
Tạo một Phương trình
Ví dụ này tạo một hình toán học và thêm định lý Pythagoras:

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();
AddMathShape tạo một hình đã chứa sẵn một đoạn toán học. Truy cập MathPortion đầu tiên, lấy MathParagraph của nó và thêm các khối toán học hoặc các phần tử toán học vào đó.
Thêm Phân Số
Sử dụng Divide để tạo một phân số. Bạn có thể chọn kiểu phân số bằng MathFractionTypes.

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();
Đối với phân số chồng, sử dụng MathFractionTypes::Bar:
auto stackedFraction = System::MakeObject<MathematicalText>(u"x + 1")->Divide(u"y - 1", MathFractionTypes::Bar);
Thêm Căn Bậc
Sử dụng Radical để tạo căn bậc hai, căn bậc ba hoặc các căn khác. Phần tử hiện tại trở thành mẫu, và đối số trở thành 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 radical = System::MakeObject<MathematicalText>(u"x")
- >Radical(u"n");
mathParagraph->Add(System::MakeObject<MathBlock>(radical));
presentation->Save(u"radical.pptx", SaveFormat::Pptx);
presentation->Dispose();
Thêm Hàm và Giới Hạn
Sử dụng AsArgumentOfFunction hoặc Function cho các hàm như sin(x), log(x), hoặc tên hàm tùy chỉnh. Đối với giới hạn, đặt lim trong một MathLimit hoặc sử dụng SetLowerLimit.

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();
Đối với tên hàm tùy chỉnh, đặt tên hàm làm phần tử hiện tại:
auto customFunction = System::MakeObject<MathematicalText>(u"f")->Function(u"x + 1");
Thêm Toán Tử N-ary và Tích Phân
Sử dụng Nary cho các phép cộng, hợp, giao và các toán tử lớn khác. Sử dụng Integral cho tích phân. Cả hai phương pháp đều cho phép bạn đặt giới hạn dưới và trên.

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();
Các toán tử N-ary dành cho các toán tử lớn có thể có hoặc không có giới hạn. Các toán tử đơn giản như +, - và = thường được thêm dưới dạng MathematicalText và nối vào biểu thức.
Đối với tích phân, sử dụng 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");
Thêm Ma Trận
Sử dụng MathMatrix cho các hàng và cột. Ma trận không bao gồm dấu ngoặc đơn mặc định, vì vậy hãy bao quanh ma trận khi bạn cần dấu ngoặc tròn, ngoặc vuông hoặc ngoặc nhọn.

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();
Thêm Mảng Phương Trình
Sử dụng ToMathArray khi bạn cần các phương trình được căn chỉnh hoặc một chuỗi dọc các biểu thức.

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();
Thêm Hàm Lượng Giác
Sử dụng AsArgumentOfFunction khi đối số là phần tử hiện tại và tên hàm đã biết.

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();
Thêm Chỉ Số Dưới và Chỉ Số Trên
Sử dụng các công cụ phụ trợ chỉ số dưới và chỉ số trên cho các chỉ mục và lũy thừa. Khi các chỉ số cần xuất hiện phía bên trái của mẫu, sử dụng SetSubSuperscriptOnTheLeft.

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();
Thêm Dấu Phân Cách
Sử dụng Enclose để đặt một biểu thức bên trong dấu phân cách. Bạn cũng có thể đặt ký tự phân tách cho các biểu thức dấu phân cách có nhiều phần tử.

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();
Thêm Khung Viền
Sử dụng ToBorderBox khi phương trình cần được đóng khung.

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();
Nhóm Các Thuật Ngữ
Sử dụng Group để đặt ký tự nhóm phía trên hoặc phía dưới một biểu thức. Thêm một giới hạn để gắn nhãn cho các thuật ngữ đã nhóm.

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();
Định Dạng Các Phần Tử Toán Học
Chỉ sử dụng các công cụ định dạng khi chúng làm rõ công thức. Ví dụ, Overbar đặt một thanh phía trên một phần tử toán họ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 overbar = System::MakeObject<MathematicalText>(u"ABC")->Overbar();
mathParagraph->Add(System::MakeObject<MathBlock>(overbar));
presentation->Save(u"overbar.pptx", SaveFormat::Pptx);
presentation->Dispose();
Tham Khảo Nhanh
| Nhiệm vụ | Main API |
|---|---|
| Tạo văn bản toán học | MathematicalText |
| Kết hợp các phần tử | IMathElement.Join |
| Tạo phân số | IMathElement.Divide |
| Thêm chỉ số trên hoặc chỉ số dưới | SetSuperscript, SetSubscript |
| Thêm hàm | Function, AsArgumentOfFunction |
| Thêm căn bậc | IMathElement.Radical |
| Thêm giới hạn | SetLowerLimit, SetUpperLimit |
| Thêm chỉ số bên trái | SetSubSuperscriptOnTheLeft |
| Thêm phép cộng và tích phân | Nary, Integral |
| Thêm ma trận | MathMatrix |
| Thêm mảng phương trình | ToMathArray |
| Thêm dấu phân cách | Enclose |
| Thêm thanh và viền | Overbar, ToBorderBox |
| Nhóm các thuật ngữ | Group |
Câu hỏi thường gặp
Tôi có thể chỉnh sửa một phương trình PowerPoint hiện có không?
Có. Mở bản trình chiếu, tìm hình chứa một MathPortion, lấy MathParagraph của nó và cập nhật các khối toán học trong đoạn đó.
Các phương trình có được lưu dưới dạng toán học PowerPoint có thể chỉnh sửa không?
Có. Khi lưu dưới dạng PPTX, Aspose.Slides ghi phương trình dưới dạng nội dung Office math có thể chỉnh sửa.
Tôi có thể xuất phương trình sang LaTeX không?
Aspose.Slides xuất các phương trình toán học sang MathML. Nếu bạn cần LaTeX, hãy xuất sang MathML trước rồi chuyển đổi MathML bằng công cụ hỗ trợ định dạng LaTeX mục tiêu của bạn.