none
Recognized math functions when added in word document does not appear appropriately RRS feed

  • Question


  • I am using word interop dll to add the math functions (latex scripts)

    For greek characters I use the corresponding utf-16 code. Other latex scripts are respected. For eg. F_abc is represented as abc in subscript of F. The problem I face is, the recognized math functions - min, max if are present in my script as subscript they dont get placed at subscripts and appear as normal free text. e'g F_max gives F_max inword document.

    If I delete explicityl by opening the word document, from the settings - File-Options-Proofing-AutoCorrect Options-Math auto correct-Recognized Functions". Then only you get the result as expected. But with code, there is no option to delete the functions.  OMathAutoCorrect.Functions.Item has Add() but no Delete()


    Please provide a way in which I could solve this issue.

    Please refer below code :

    var   script = @"\delta  D_sin M_max M_min";

    script = script.Replace(@"\delta", "\u03B4");

    var range1 = objPara2.Range;

    range1.OMaths.Add(range1);

    OMathFunction myFunction = range1.OMaths[1].Functions.Add( range1, WdOMathFunctionType.wdOMathFunctionBox); OMathBox myBox = myFunction.Box;

    myBox.E.Range.Text = script;  myBox.E.BuildUp();

    Monday, December 17, 2018 9:51 AM

All replies

  • Hi PS20,

    Please try the following code:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Word = Microsoft.Office.Interop.Word;
    using System.Diagnostics;
    
    namespace VSTOInsertEquations
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Word.Application wdApplication = null;
                Process[] pl = Process.GetProcessesByName("WINWORD.exe");
                if (pl.Length > 0)
                {
                    wdApplication = (Word.Application)System.Runtime.InteropServices
                        .Marshal.GetActiveObject("Word.Application");
                }
                else
                {
                    wdApplication = new Word.Application();
                }
                if (wdApplication != null)
                {
                    Word.Document newDocument = wdApplication.Documents.Add();
                    //Following code will add a Nary Equation
                    Word.Range wdFunctionR = wdApplication.Selection.OMaths
                        .Add(wdApplication.Selection.Range);
                    Word.OMathFunction wdFunction = wdApplication.Selection
                        .OMaths[1].Functions.Add(wdApplication.Selection.Range,
                        Word.WdOMathFunctionType.wdOMathFunctionNary);                    
                    Word.OMathNary wdNary = wdFunction.Nary;
                    wdNary.Char = 8721;
                    wdNary.Grow = false;
                    wdNary.SubSupLim = false;
                    wdNary.HideSub = false;
                    wdNary.HideSup = false;
                    //Following code will setup value in Nary Function
                    Word.Selection wdSelection = wdApplication.Selection;
                    object unit = Word.WdUnits.wdCharacter;
                    object lu = Word.WdUnits.wdLine;
                    object count = 1;
                    object dcount = 2;
                    object tcount = 3;
                    wdSelection.MoveLeft(ref unit, ref count);
                    wdSelection.TypeText("11");
                    wdSelection.MoveLeft(ref unit, ref tcount);
                    wdSelection.TypeText("12");
                    wdSelection.MoveDown(ref lu, ref count);
                    wdSelection.TypeText("13");
                    wdNary.Application.Visible = true;
                }
            }
        }
    }

    For more information, please see the following links:

    Insert a mathematical formula in to a word document programmatically

    Programmatically adding math equations to Word document from C#

    Hopefully it helps you.

    Best Regards,

    Lina


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    • Proposed as answer by PSalvi_Shelke Wednesday, December 26, 2018 4:49 AM
    • Unproposed as answer by PSalvi_Shelke Wednesday, December 26, 2018 4:49 AM
    Tuesday, December 18, 2018 7:13 AM
  • Thank you for the reply but I m not looking for Nary functions. The solution I found is : the script  should have " " appended for the strings to be identified by OMath.

    The above code works for

    var   script = @"\delta  D_sin M_max M_min";

    script+=" ";

    • Proposed as answer by PSalvi_Shelke Wednesday, December 26, 2018 4:49 AM
    Wednesday, December 19, 2018 5:33 AM
  • Hi PS20,

    Sorry for the late reply.

    Please see the following links:

    Embed LaTeX math equations into Microsoft Word

    Trigonometric function script? [duplicate]

    Hopefully it helps you.

    Best Regards,

    Lina


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Tuesday, December 25, 2018 6:34 AM
  • The above 2 websites are really informative.

    I have found a way in which the latext scripts which are not identified by word as is, I replace that text with word understood syntax.

    E.g \delta when replaced with its equivalent utf-16 code, displays the greek alphabet delta in word.

    Wednesday, December 26, 2018 4:52 AM