Document Solutions for Word
Features / Office Math / Complex Equation Examples
In This Topic
    Complex Equation Examples
    In This Topic

    The following example codes demonstrate various complex equations that can be added using Office Math APIs of DsWord library:

    Equation 1

    C#
    Copy Code
    // Initialize GcWordDocument.
    var doc = new GcWordDocument();
    var om = doc.Body.AddParagraph().AddOMathParagraph().AddOMath();
    
    // Add f(x)=a0+ to the paragraph.
    om.AddRun("f");
    
    // Add delimiter. It inserts ( and ) before and after its items.
    om.AddDelimiter(new string[] { "x" });
    om.AddRun("=");
    om.AddSubscript("a", "0");
    om.AddRun("+");
    
    // Add sum from 1 till infinity.
    var nary = om.AddNary("", "n=1", "∞", "∑");
    nary.Grow = true;
    
    // Add delimeters "()".
    var deli = nary.Base.AddDelimiter();
    var item = deli.Items.Add();
    
    // Add an cos nπx/L.
    item.AddSubscript("a", "n");
    
    var func = item.AddFunction();
    func.Name.AddRun("cos").OMathFormat.IsNormalText = true;
    func.Argument.AddFraction("nπx", "L", null);
    
    // Add + bn sin nπx/L.
    item.AddRun("+");
    item.AddSubscript("b", "n");
    
    func = item.AddFunction("", "");
    func.Name.AddRun("sin").OMathFormat.IsNormalText = true;
    func.Argument.AddFraction("nπx", "L", null);
    
    // Save the Word document.
    doc.Save("Equation.docx");

    Equation 2

    C#
    Copy Code
    // Initialize GcWordDocument.
    var sampleDoc = new GcWordDocument();
    var om = sampleDoc.Body.AddParagraph().AddOMathParagraph().AddOMath();
    
    // Add ∃x.
    om.AddRun("∃x");
    
    // Add delimiter. It inserts ( and ) before and after its items.
    var item = om.AddDelimiter().Items.Add();
    
    // Add equation.
    item.AddRun("Person").OMathFormat.IsNormalText = true;
    item.AddDelimiter(new string[] { "x" });
    item.AddRun("∧∀y");
    item = item.AddDelimiter().Items.Add();
    item.AddRun("Time").OMathFormat.IsNormalText = true;
    item.AddDelimiter(new string[] { "y" });
    item.AddRun("→");
    item.AddRun("Happy").OMathFormat.IsNormalText = true;
    item.AddDelimiter(new string[] { "x,y" });
    
    // Save the Word document.
    sampleDoc.Save("Equation.docx");

     Equation 3

    C#
    Copy Code
    // Initialize GcWordDocument.
    var sampleDoc = new GcWordDocument();
    var om = sampleDoc.Body.AddParagraph().AddOMathParagraph().AddOMath();
    
    // Add equation.
    var func = om.AddFunction("sin", "α");
    func.Name.GetRange().Runs.First.Font.Italic = false;
    om.AddRun("±");
    func = om.AddFunction("sin", "β");
    func.Name.GetRange().Runs.First.Font.Italic = false;
    om.AddRun("=2");
    func = om.AddFunction("sin", "");
    func.Name.GetRange().Runs.First.Font.Italic = false;
    func.Argument.AddFraction("1", "2", null);
    func.Argument.AddDelimiter(new string[] { "α±β" });
    func = om.AddFunction("cos", "");
    func.Name.GetRange().Runs.First.Font.Italic = false;
    func.Argument.AddFraction("1", "2", null);
    func.Argument.AddDelimiter(new string[] { "α∓β" });
    
    // Save the Word document.
    sampleDoc.Save("Equation.docx");

     Equation 4

    C#
    Copy Code
    // Initialize GcWordDocument.
    var sampleDoc = new GcWordDocument();
    var om = sampleDoc.Body.AddParagraph().AddOMathParagraph().AddOMath();
    
    // Add equation.
    om.AddRun("Z");
    om.AddDelimiter(new string[] { "υ" });
    om.AddRun("=");
    var frac = om.AddFraction("1", "", null);
    var radical = frac.Denominator.AddRadical();
    radical.Radicand.AddRun("π");
    radical.HideDegree = true;
    var nary = om.AddNary("", "-∞", "∞", "∫");
    frac = nary.Base.AddFraction();
    var superscript = frac.Numerator.AddSuperscript("e", "");
    superscript.Superscript.AddSuperscript("-t", "2");
    frac.Numerator.AddRun("dt");
    frac.Denominator.AddRun("t-i");
    frac.Denominator.AddRun("υ").Font.Bidi = true;
    om.AddRun("=i");
    nary = om.AddNary("", "0", "∞", "∫");
    superscript = nary.Base.AddSuperscript("e", "-υt-");
    superscript.Superscript.AddSuperscript("t", "2");
    superscript.Superscript.AddRun("/4");
    nary.Base.AddRun("dt");
    
    // Save the Word document.
    sampleDoc.Save("Equation.docx");

     Equation 5

    C#
    Copy Code
    // Initialize GcWordDocument.
    var sampleDoc = new GcWordDocument();
    var om = sampleDoc.Body.AddParagraph().AddOMathParagraph().AddOMath();
    
    // Add first delimiter.
    var delimiter = om.AddDelimiter();
    var item = delimiter.Items.Add();
    
    // Add first matrix.
    var matrix = item.AddMatrix(3, 1);
    
    foreach (var column in matrix.Columns)
        column.HorizontalAlignment = OMathHorizontalAlignment.Center;
    matrix.Rows[0].Cells[0].AddRun("U");
    matrix.Rows[0].Cells[0].AddDelimiter(new string[] { "t" });
    matrix.Rows[1].Cells[0].AddRun("V");
    matrix.Rows[1].Cells[0].AddDelimiter(new string[] { "t" });
    matrix.Rows[2].Cells[0].AddRun("W");
    matrix.Rows[2].Cells[0].AddDelimiter(new string[] { "t" });
    
    om.AddRun("=");
                
    // Add second delimiter.
    delimiter = om.AddDelimiter();
    item = delimiter.Items.Add();
    
    // Add second matrix.
    matrix = item.AddMatrix(3, 3);
    foreach (var column in matrix.Columns)
        column.HorizontalAlignment = OMathHorizontalAlignment.Center;
    
    matrix.Rows[0].Cells[0].AddRun("1");
    matrix.Rows[0].Cells[1].AddRun("0");
    matrix.Rows[0].Cells[2].AddRun("0");
    
    matrix.Rows[1].Cells[0].AddRun("0");
    matrix.Rows[1].Cells[1].AddFunction("cos", "Rt").Name.GetRange().Runs.First.Font.Italic = false;
    matrix.Rows[1].Cells[2].AddRun("-");
    matrix.Rows[1].Cells[2].AddFunction("sin", "Rt").Name.GetRange().Runs.First.Font.Italic = false;
    
    matrix.Rows[2].Cells[0].AddRun("0");
    matrix.Rows[2].Cells[1].AddFunction("sin", "Rt").Name.GetRange().Runs.First.Font.Italic = false;
    matrix.Rows[2].Cells[2].AddFunction("cos", "Rt").Name.GetRange().Runs.First.Font.Italic = false;
    
    // Add third delimiter.
    delimiter = om.AddDelimiter();
    item = delimiter.Items.Add();
    
    // Add third matrix.
    matrix = item.AddMatrix(3, 1);
    
    foreach (var column in matrix.Columns)
        column.HorizontalAlignment = OMathHorizontalAlignment.Center;
    matrix.Rows[0].Cells[0].AddRun("U");
    matrix.Rows[0].Cells[0].AddDelimiter(new string[] { "0" });
    matrix.Rows[1].Cells[0].AddRun("V");
    matrix.Rows[1].Cells[0].AddDelimiter(new string[] { "0" });
    matrix.Rows[2].Cells[0].AddRun("W");
    matrix.Rows[2].Cells[0].AddDelimiter(new string[] { "0" });
    
    // Save the Word document.
    sampleDoc.Save("Equation.docx");