从Spire.Doc 版本7.6.5开始,Spire.Doc支持添加Latex数学公式和符号到Word文档。 该文将详细介绍如何使用C#添加Latex 数学公式和符号到word 文档。
C#
//创建文档
Document doc = new Document();
//添加section
Section section = doc.AddSection();
//添加段落
Paragraph paragraph = section.AddParagraph();
//添加公式
OfficeMath officeMath = new OfficeMath(doc);
paragraph.Items.Add(officeMath);
officeMath.FromLatexMathCode("x^{2}+\\sqrt{x^{2}+1}=2");
Paragraph paragraph2 = section.AddParagraph();
OfficeMath officeMath1 = new OfficeMath(doc);
paragraph2.Items.Add(officeMath1);
officeMath1.FromLatexMathCode("\\forall x \\in X, \\quad \\exists y \\leq \\epsilon");
//添加符号
Paragraph paragraph3 = section.AddParagraph();
OfficeMath officeMath2 = new OfficeMath(doc);
paragraph3.Items.Add(officeMath2);
officeMath2.FromLatexMathCode(" \\alpha,\\beta, \\gamma, \\Gamma, \\pi, \\Pi, \\phi, \\varphi, \\mu, \\Phi");
//保存文档
doc.SaveToFile("Equation.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("Equation.docx");
VB.NET
Dim doc As Document = New Document
Dim section As Section = doc.AddSection
Dim paragraph As Paragraph = section.AddParagraph
Dim officeMath As OfficeMath = New OfficeMath(doc)
paragraph.Items.Add(officeMath)
officeMath.FromLatexMathCode("x^{2}+\sqrt{x^{2}+1}=2")
Dim paragraph2 As Paragraph = section.AddParagraph
Dim officeMath1 As OfficeMath = New OfficeMath(doc)
paragraph2.Items.Add(officeMath1)
officeMath1.FromLatexMathCode("\forall x \in X, \quad \exists y \leq \epsilon")
Dim paragraph3 As Paragraph = section.AddParagraph
Dim officeMath2 As OfficeMath = New OfficeMath(doc)
paragraph3.Items.Add(officeMath2)
officeMath2.FromLatexMathCode(" \alpha,\beta, \gamma, \Gamma, \pi, \Pi, \phi, \varphi, \mu, \Phi")
doc.SaveToFile("Equation.docx", FileFormat.Docx)
System.Diagnostics.Process.Start("Equation.docx")
效果图: