Document Solutions for Word
Features / Office Math
In This Topic
    Office Math
    In This Topic

    Office Math in a Word document is a combination of mathematical symbols or text. For example, A=πr^2 can be converted and written in a Word document as:

    DsWord allows you to read, add, edit, and display Office Math content in a Word document using OMathParagraph and OMath classes. OMathParagraph represents a paragraph that contains Office Math content, while OMath represents an inline Office Math zone and can be included in an OMathParagraph or a regular paragraph. Also, you can add OMathParagraph and OMath to a SimpleField, Hyperlink, BidirectionalOverride, and ControlContent in an inline content control.

    DsWord provides specialized classes such as OMathBarOMathBorderBoxOMathBoxOMathDelimiterOMathEquationArrayOMathFractionOMathFunction, etc. to represent the various math structures inside an OMath zone. These classes are derived from the common abstract OMathStruct base class. Each class implementation has its own strong structure of OMathElements. OMathElement represents an element inside the OMathStruct class instance. You can add OMathElement only to OMathDelimiter and OMathEquationArray class instances manually; in other OMathStruct implementations, their position is predefined.

    Besides Office Math content, OMath and OMathElement classes can contain the following objects: SimpleField, Hyperlink, and ContentControl. Also, these classes and OMathParagraph class can contain ContentMark and Run objects. When Run object is inside the Office Math zone (in OMath or OMathElement), its new property IsOMathRun sets to true. In this case, the new property OMathFormat became available for additional mathematical formatting through the OMathFormat class. The default appearance and behavior of Office Math content are available with the new OMathOptions property in Settings class through OMathOptions class.

    You can access the Office Math content using OMathParagraphsOMathsOMathStructsOMathElements and OMathMatrixRows properties of RangeBase class.

    Working with OMathElements

    OMathElements allow you to create various complex equations with different structures using accent, bar, border-box, box, delimiter, and so on. The following sections demonstrate the usage of such OMathElements.

    Accent

    An accent is a single Unicode symbol present over a base element. You can add an accent symbol to an equation using AddAccent method of OMath class.

    Refer to the following example code to add an accent over a fraction:

    C#
    Copy Code
    // Initialize GcWordDocument.
    var sampleDoc = new GcWordDocument();
    var om = sampleDoc.Body.AddParagraph().AddOMathParagraph().AddOMath();
                
    // Add accent to the equation.
    var accent = om.AddAccent("", "\u0308");
    accent.Base.AddFraction("2", "5", OMathFractionType.Bar);
    
    // Save the Word document.
    sampleDoc.Save("Accent.docx");

    Bar

    A bar is a horizontal line present over or below a base element. You can add a bar to an equation using AddBar method of OMath class. By default, the bar is added below the base element.

    Refer to the following example code to add a bar over a function:

    C#
    Copy Code
    // Initialize GcWordDocument.
    var sampleDoc = new GcWordDocument();
    var om = sampleDoc.Body.AddParagraph().AddOMathParagraph().AddOMath();
    
    // Add bar to the equation.
    var bar = om.AddBar();
    bar.Base.AddFunction("cos", "2x");
    bar.Position = OMathVerticalJustification.Top;
    
    // Save the Word document.
    sampleDoc.Save("Bar.docx");

    Border Box

    A border box is a border present around a base element with four sides. You can add a border box to an equation using AddBorderBox method of OMath class. By default, a box is drawn around the base element, but you can also add strikethrough in horizontal, vertical, and diagonal directions.

    Refer to the following example code to add a border box around an equation:

    C#
    Copy Code
    // Initialize GcWordDocument.
    var sampleDoc = new GcWordDocument();
    var om = sampleDoc.Body.AddParagraph().AddOMathParagraph().AddOMath();
    
    // Add borderbox to the equation.
    var box = om.AddBorderBox("2+2*");
    box.Base.AddFraction("3", "3", OMathFractionType.Bar);
    box.Base.AddRun(" = 5");
    
    // Save the Word document.
    sampleDoc.Save("BorderBox.docx");

    Box

    A box represents a box that groups components of an equation or other instance of mathematical text. You can add a box to an equation using AddBox method of OMath class.

    Refer to the following example code to add two runs with a box element between them:

    C#
    Copy Code
    // Initialize GcWordDocument.
    var sampleDoc = new GcWordDocument();
    var om = sampleDoc.Body.AddParagraph().AddOMathParagraph().AddOMath();
    
    // Add box to the equation.
    om.AddRun("a");
    var box = om.AddBox("==");
    om.AddRun("b");
    box.IsOperatorEmulator = true;
    
    // Save the Word document.
    sampleDoc.Save("Box.docx");

    Delimiter

    A delimiter is a mark or symbol that represents the beginning or end of separate elements of an equation with a designated separator between each element. You can add a delimiter to an equation using AddDelimiter method of OMath class. By default, the beginning and end characters will be parentheses, and the separator will be a pipe symbol.

    Refer to the following example code to add delimiters to an equation:

    C#
    Copy Code
    // Initialize GcWordDocument.
    var sampleDoc = new GcWordDocument();
    var om = sampleDoc.Body.AddParagraph().AddOMathParagraph().AddOMath();
    
    // Add delimiter to the equation.
    var delimiter = om.AddDelimiter(new string[] { "5", "7" }, "[", "]", "\\");
    delimiter.Items.Add().AddRadical("n", "7");
    
    // Save the Word document.
    sampleDoc.Save("Delimiter.docx");

    Equation Array

    An equation array is a one-dimensional vertical array consisting of one or more equations or expression elements. You can add an equation array using AddEquationArray method of OMath class.

    Refer to the following example code to add an equation array:

    C#
    Copy Code
    // Initialize GcWordDocument.
    var sampleDoc = new GcWordDocument();
    var om = sampleDoc.Body.AddParagraph().AddOMathParagraph().AddOMath();
    
    // Add equation array.
    var ea = om.AddEquationArray();
    ea.Items.Add().AddDelimiter(new string[] { "x", "y", "z" });
    ea.Items.Add().AddFraction("π", "2", OMathFractionType.Skewed);
    
    // Save the Word document.
    sampleDoc.Save("EquationArray.docx");

    Fraction

    A fraction consists of a numerator element and a denominator element separated by a fraction bar. You can add a fraction using AddFraction method of OMath class.

    Refer to the following example code to add a fraction:

    C#
    Copy Code
    // Initialize GcWordDocument.
    var sampleDoc = new GcWordDocument();
    var om = sampleDoc.Body.AddParagraph().AddOMathParagraph().AddOMath();
    
    // Add fraction.
    var fr = om.AddFraction();
    var func = fr.Numerator.AddFunction("sin", "");
    func.Argument.AddFraction("π", "3", OMathFractionType.Bar);
    fr.Denominator.AddRadical("e", "3");
    
    // Save the Word document.
    sampleDoc.Save("Fraction.docx");

    Function

    A function consists of a function name element and an argument element. You can add a function using AddFunction method of OMath class.

    Refer to the following example code to add a function:

    C#
    Copy Code
    // Initialize GcWordDocument.
    var sampleDoc = new GcWordDocument();
    var om = sampleDoc.Body.AddParagraph().AddOMathParagraph().AddOMath();
    
    // Add function.
    var f = om.AddFunction("cos", "");
    f.Argument.AddFraction("π", "2", OMathFractionType.Bar);
    
    // Save the Word document.
    sampleDoc.Save("Function.docx");

    Group Character

    A group character is a Unicode symbol present above or below the base element. You can add a group character using AddGroupCharacter method of OMath class.

    Refer to the following example code to add a group character to the function:

    C#
    Copy Code
    // Initialize GcWordDocument.
    var sampleDoc = new GcWordDocument();
    var om = sampleDoc.Body.AddParagraph().AddOMathParagraph().AddOMath();
    
    // Add group character.
    var gc = om.AddGroupCharacter("xyz", "\u23DE");
    gc.Position = OMathVerticalJustification.Top;
    gc.VerticalJustification = OMathVerticalJustification.Bottom;
    
    // Save the Word document.
    sampleDoc.Save("GroupCharacter.docx");

    Lower Limit

    A lower limit is the lowest value of any value or expression. You can add a lower limit using AddLimitLower method of OMath class.

    Refer to the following example code to add a lower limit to the function:

    C#
    Copy Code
    // Initialize GcWordDocument.
    var sampleDoc = new GcWordDocument();
    var om = sampleDoc.Body.AddParagraph().AddOMathParagraph().AddOMath();
    
    // Add lower limit.
    om.AddLimitLower("abc", "z");
    
    // Save the Word document.
    sampleDoc.Save("LowerLimit.docx");

    Upper Limit

    An upper limit is the highest value of any value or expression. You can add an upper limit using AddLimitUpper method of OMath class.

    Refer to the following example code to add an upper limit to the function:

    C#
    Copy Code
    // Initialize GcWordDocument.
    var sampleDoc = new GcWordDocument();
    var om = sampleDoc.Body.AddParagraph().AddOMathParagraph().AddOMath();
    
    // Add upper limit.
    om.AddLimitUpper("abc", "y");
    
    // Save the Word document.
    sampleDoc.Save("UpperLimit.docx");

    Matrix

    A matrix is a rectangular array of elements in one or more rows and columns. DsWord provides OMathMatrix class, which is a special type of OMathStruct. OMathMatrix represents a math matrix where you can add or remove rows and columns represented by OMathMatrixRow and OMathMatrixColumn, respectively. Each cell in OMathMatrix is OMathElement instance. You can add a matrix using AddMatrix method of OMath class.

    Refer to the following example code to add a matrix:

    C#
    Copy Code
    // Initialize GcWordDocument.
    var sampleDoc = new GcWordDocument();
    var om = sampleDoc.Body.AddParagraph().AddOMathParagraph().AddOMath();
    
    // Add matrix.
    om.AddMatrix(new int?[2, 2] { { 1, 2 }, { 3, 4 } });
    
    // Save the Word document.
    sampleDoc.Save("Matrix.docx");

    N-Array

    An N-Array consists of an n-ary operator character, a base element (or operand), and optional upper and lower limit elements. You can add an N-Array using AddNary method of OMath class.

    Refer to the following example code to add an N-Array:

    C#
    Copy Code
    // Initialize GcWordDocument.
    var sampleDoc = new GcWordDocument();
    var om = sampleDoc.Body.AddParagraph().AddOMathParagraph().AddOMath();
    
    // Add N-Array.
    om.AddNary("xⅆx", "0", "∞", "∭");
    
    // Save the Word document.
    sampleDoc.Save("Nary.docx");

    Phantom

    A phantom structure adds the spacing of the phantom base element without displaying that base and suppresses part of the glyph for spacing considerations. You can add a phantom structure using AddPhantom method of OMath class.

    Refer to the following example code to add a phantom structure:

    C#
    Copy Code
    // Initialize GcWordDocument.
    var sampleDoc = new GcWordDocument();
    var om = sampleDoc.Body.AddParagraph().AddOMathParagraph().AddOMath();
    
    // Add phantom structure.
    var del = om.AddDelimiter(null, "<", ">", "");
    var ph = del.Items.Add().AddPhantom("argument", false);
    ph.Show = false;
    
    // Save the Word document.
    sampleDoc.Save("Phantom.docx");

    Radical

    A radical symbol denotes the square root or nth root, and it creates a radical expression comprising a radicand and a degree element. You can add a radical using AddRadical method of OMath class.

    Refer to the following example code to add a radical expression:

    C#
    Copy Code
    // Initialize GcWordDocument.
    var sampleDoc = new GcWordDocument();
    var om = sampleDoc.Body.AddParagraph().AddOMathParagraph().AddOMath();
    
    // Add radical expression.
    var radical = om.AddRadical();
    radical.Degree.AddFraction("2", "3", OMathFractionType.Skewed);
    radical.Radicand.AddFunction("cos", "2x");
    
    // Save the Word document.
    sampleDoc.Save("Radical.docx");

    Subscript

    A subscript is a character, such as a number or letter, placed slightly below the normal line. You can add a subscript using AddSubscript method of OMath class.

    Refer to the following example code to add a subscript:

    C#
    Copy Code
    // Initialize GcWordDocument.
    var sampleDoc = new GcWordDocument();
    var om = sampleDoc.Body.AddParagraph().AddOMathParagraph().AddOMath();
    
    // Add subscript.
    var func = om.AddFunction("", "2x+1");
    func.Name.AddSubscript("log", "3");
    
    // Save the Word document.
    sampleDoc.Save("Subscript.docx");

    Superscript

    A superscript is a character, such as a number or letter, placed slightly above the normal line. You can add a superscript using AddSuperscript method of OMath class.

    Refer to the following example code to add a superscript:

    C#
    Copy Code
    // Initialize GcWordDocument.
    var sampleDoc = new GcWordDocument();
    var om = sampleDoc.Body.AddParagraph().AddOMathParagraph().AddOMath();
    
    // Add superscript.
    var func = om.AddFunction();
    func.Name.AddSuperscript("cos", "2");
    var frac = func.Argument.AddFraction();
    frac.Numerator.AddRun("7x");
    frac.Denominator.AddRun("2π");
    
    // Save the Word document.
    sampleDoc.Save("Superscript.docx");

    PreSubSuperscript

    A pre-sub-superscript structure consists of a base element, a reduced-size script element placed below and to the left, and a reduced-size script element placed above and to the left. You can add a pre-sub-superscript structure using AddPreSubSuperscript method of OMath class.

    Refer to the following example code to add a pre-sub-superscript structure:

    C#
    Copy Code
    // Initialize GcWordDocument.
    var sampleDoc = new GcWordDocument();
    var om = sampleDoc.Body.AddParagraph().AddOMathParagraph().AddOMath();
    
    // Add pre-sub-superscript structure.
    var pss = om.AddPreSubSuperscript("", "-x", "2x");
    pss.Base.AddRadical("n", "").HideDegree = true;
    
    // Save the Word document.
    sampleDoc.Save("PreSubSuperscript.docx");

    SubSuperscript

    A SubSuperscript consists of a base element, a reduced-size script element placed below and to the right, and a reduced-size script element placed above and to the right. You can add a SubSuperscript using AddSubSuperscript method of OMath class.

    Refer to the following example code to add a SubSuperscript:

    C#
    Copy Code
    // Initialize GcWordDocument.
    var sampleDoc = new GcWordDocument();
    var om = sampleDoc.Body.AddParagraph().AddOMathParagraph().AddOMath();
    
    // Add SubSuperscript.
    var subs = om.AddSubSuperscript("", "-x", "2x");
    subs.Base.AddRadical("n", "").HideDegree = true;
    
    // Save the Word document.
    sampleDoc.Save("SubSuperscript.docx");

    BuiltIn Equations

    You can add built-in equations supported by MS Word using Add method of RangeBase, OMathParagraph, OMath, and OMathElement classes that accept OMathBuiltInEquation enumeration value identifying the desired equation.

    Add BuiltIn Equation

    1. Access Office Math paragraph collection using OMathParagraphs property of RangeBase class.
    2. Add a builtIn equation using Add method of OMathParagraphCollection class and pass the builtin equation as a parameter by choosing the appropriate value from OMathBuiltInEquation enumeration .
    C#
    Copy Code
    // Initialize GcWordDocument.
    GcWordDocument doc = new GcWordDocument();
    
    // Add an Office Math paragraph with a built-in area of a circle equation.
    doc.Body.Paragraphs.Add().GetRange().OMathParagraphs.Add(OMathBuiltInEquation.AreaOfCircle);
    
    // Save the Word document.
    doc.Save("AreaofCircleBuiltin.docx");

    MathML Converter

    You can convert Office Math content to MathML and vice versa using MathMLConverter class. Also, it allows you to insert MathML directly into a RangeBase and converts the MathML to Office Math automatically.

    1. Access the Office Math paragraph collection using OMathParagraphs property of RangeBase class and add Office Math paragraph using Add method of OMathParagraphCollection class.
    2. Implement a text reader to read from a string using StringReader class.
    3. Create an XML reader to read MathML from the string using Create method of XmlReader class.
    4. Create a MathML converter and add the content to OMathParagraph using FromMathML method of MathMLConverter class.
    C#
    Copy Code
    // Initialize GcWordDocument.
    GcWordDocument doc = new GcWordDocument();
    
    // Add an Office Math paragraph.
    OMathParagraph oMathParagraph = doc.Body.Paragraphs.Add().GetRange().OMathParagraphs.Add();
    
    // Create an XML reader from the MathML area of the circle string.
    StringReader stringReader = new StringReader("<mml:math xmlns:mml=\"http://www.w3.org/1998/Math/MathML\"" +
        " xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\"><mml:mi>A</mml:mi><mml:mo>=</mml:mo>" +
        "<mml:mi>π</mml:mi><mml:msup><mml:mrow><mml:mi>r</mml:mi></mml:mrow><mml:mrow><mml:mn>2</mml:mn></mml:mrow></mml:msup></mml:math>");
    XmlReader xmlReader = XmlReader.Create(stringReader);
    
    // Create a MathML converter.
    MathMLConverter converter = new MathMLConverter();
    
    // Add the MathML area of the circle.
    converter.AddMathML(xmlReader, oMathParagraph);
    
    // Save the Word document.
    doc.Save("AreaofCircleMathml.docx");

    Limitation

    Export of Office Math content to PDF or images is not yet supported.