OMath buildup() 转义字符
OMath buildup() escaped characters
我正在尝试通过 OMath 名称space 将方程式写入 Word,但无法正确生成转义字符。当我执行下面的控制台程序时,它正确地放置了分子和除数,但转义字符“\delta”与输入的完全一样,而不是变成小写 delta 的希腊符号。
如果我将光标设置在 \delta 中的 'a' 之后,然后按 space,它会转换。如果我从功能区单击 Professional,也会转换。
有人可以解释如何以编程方式创建转义字符并使其正确显示吗?
static void Main(string[] args)
{
string fName = @"C:\Users\Desktop\Doc_1.docx";
Word._Application myApp = new Word.Application();
myApp.Visible = true;
Word.Document myDoc = myApp.Documents.Open(fName);
Word.Range myFunctionR = myApp.Selection.OMaths.Add(myApp.Selection.Range);
Word.OMathFunction myFunction = myApp.Selection.OMaths[1].Functions.Add(
myApp.Selection.Range, Word.WdOMathFunctionType.wdOMathFunctionBox);
Word.OMathBox myBox = myFunction.Box;
myBox.E.Range.Text = @"\delta = (PL)/(AE)";
myBox.E.BuildUp();
}
如果我从 MS Word 中粘贴 MathML,它看起来像这样:
Correct/Desired 版本:
<mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math">
<mml:mi>δ</mml:mi><mml:mi>
<mml:mi> </mml:mi>
<mml:mo>=</mml:mo>
<mml:mfrac>
<mml:mrow>
<mml:mi>P</mml:mi>
<mml:mi>L</mml:mi>
</mml:mrow>
<mml:mrow>
<mml:mi>A</mml:mi>
<mml:mi>E</mml:mi>
</mml:mrow>
</mml:mfrac>
Incorrect/Programmatic 版本:
<mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math">
<mml:mo>\</mml:mo>
<mml:mi>d</mml:mi>
<mml:mi>e</mml:mi>
<mml:mi>l</mml:mi>
<mml:mi>t</mml:mi>
<mml:mi>a</mml:mi>
<mml:mi> </mml:mi>
<mml:mo>=</mml:mo>
<mml:mfrac>
<mml:mrow>
<mml:mi>P</mml:mi>
<mml:mi>L</mml:mi>
</mml:mrow>
<mml:mrow>
<mml:mi>A</mml:mi>
<mml:mi>E</mml:mi>
</mml:mrow>
</mml:mfrac>
我已经解决了,2 年后...以下方法将输入的转义文本转换为格式正确的等式。然后当 OMath.Buildup() 被调用时,它能够像人们期望的那样正确地格式化方程。
问题注意事项:我最初走的是 MathML 路径,这不一定是坏事,但与我最初的目标相悖。此路线使用 Word 的内置等式自动更正(花哨的查找和替换)来生成正确的 UTF 编码字符。
private string OMathAutoCorrect(string input)
{
if (string.IsNullOrEmpty(input)) return null;
Globals.ThisAddIn.Application.OMathAutoCorrect.UseOutsideOMath = true;
var retString = input;
foreach (OMathAutoCorrectEntry ac in Globals.ThisAddIn.Application.OMathAutoCorrect.Entries)
{
if (retString.Contains(ac.Name))
{
var matchIndex = retString.IndexOf(ac.Name);
var sb = new StringBuilder();
//Capture all data prior to the match:
sb.Append(retString.Substring(0, matchIndex));
//Add the Match
sb.Append(ac.Value);
//Capture all data after the match:
var indexAfterMatch = matchIndex + ac.Name.Length;
var remLength = retString.Length - indexAfterMatch;
sb.Append(retString.Substring(indexAfterMatch, remLength));
retString = sb.ToString();
}
Debug.WriteLine($"{ac.Name}, {ac.Value}, Char#: {ac.Value.Length}");
}
Globals.ThisAddIn.Application.OMathAutoCorrect.UseOutsideOMath = false;
return retString;
}
要替换字母,请使用以下代码
var script = @"\delta = (PL)/(AE)";
script = script.Replace("\delta", "\u03B4");}
您必须识别所有希腊字母并替换为等效的 UTF16 代码。
请找到以下网站以获取可用字母列表
https://www.fileformat.info/info/charset/UTF-16/list.htm
我正在尝试通过 OMath 名称space 将方程式写入 Word,但无法正确生成转义字符。当我执行下面的控制台程序时,它正确地放置了分子和除数,但转义字符“\delta”与输入的完全一样,而不是变成小写 delta 的希腊符号。
如果我将光标设置在 \delta 中的 'a' 之后,然后按 space,它会转换。如果我从功能区单击 Professional,也会转换。
有人可以解释如何以编程方式创建转义字符并使其正确显示吗?
static void Main(string[] args)
{
string fName = @"C:\Users\Desktop\Doc_1.docx";
Word._Application myApp = new Word.Application();
myApp.Visible = true;
Word.Document myDoc = myApp.Documents.Open(fName);
Word.Range myFunctionR = myApp.Selection.OMaths.Add(myApp.Selection.Range);
Word.OMathFunction myFunction = myApp.Selection.OMaths[1].Functions.Add(
myApp.Selection.Range, Word.WdOMathFunctionType.wdOMathFunctionBox);
Word.OMathBox myBox = myFunction.Box;
myBox.E.Range.Text = @"\delta = (PL)/(AE)";
myBox.E.BuildUp();
}
如果我从 MS Word 中粘贴 MathML,它看起来像这样:
Correct/Desired 版本:
<mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math">
<mml:mi>δ</mml:mi><mml:mi>
<mml:mi> </mml:mi>
<mml:mo>=</mml:mo>
<mml:mfrac>
<mml:mrow>
<mml:mi>P</mml:mi>
<mml:mi>L</mml:mi>
</mml:mrow>
<mml:mrow>
<mml:mi>A</mml:mi>
<mml:mi>E</mml:mi>
</mml:mrow>
</mml:mfrac>
Incorrect/Programmatic 版本:
<mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math">
<mml:mo>\</mml:mo>
<mml:mi>d</mml:mi>
<mml:mi>e</mml:mi>
<mml:mi>l</mml:mi>
<mml:mi>t</mml:mi>
<mml:mi>a</mml:mi>
<mml:mi> </mml:mi>
<mml:mo>=</mml:mo>
<mml:mfrac>
<mml:mrow>
<mml:mi>P</mml:mi>
<mml:mi>L</mml:mi>
</mml:mrow>
<mml:mrow>
<mml:mi>A</mml:mi>
<mml:mi>E</mml:mi>
</mml:mrow>
</mml:mfrac>
我已经解决了,2 年后...以下方法将输入的转义文本转换为格式正确的等式。然后当 OMath.Buildup() 被调用时,它能够像人们期望的那样正确地格式化方程。
问题注意事项:我最初走的是 MathML 路径,这不一定是坏事,但与我最初的目标相悖。此路线使用 Word 的内置等式自动更正(花哨的查找和替换)来生成正确的 UTF 编码字符。
private string OMathAutoCorrect(string input)
{
if (string.IsNullOrEmpty(input)) return null;
Globals.ThisAddIn.Application.OMathAutoCorrect.UseOutsideOMath = true;
var retString = input;
foreach (OMathAutoCorrectEntry ac in Globals.ThisAddIn.Application.OMathAutoCorrect.Entries)
{
if (retString.Contains(ac.Name))
{
var matchIndex = retString.IndexOf(ac.Name);
var sb = new StringBuilder();
//Capture all data prior to the match:
sb.Append(retString.Substring(0, matchIndex));
//Add the Match
sb.Append(ac.Value);
//Capture all data after the match:
var indexAfterMatch = matchIndex + ac.Name.Length;
var remLength = retString.Length - indexAfterMatch;
sb.Append(retString.Substring(indexAfterMatch, remLength));
retString = sb.ToString();
}
Debug.WriteLine($"{ac.Name}, {ac.Value}, Char#: {ac.Value.Length}");
}
Globals.ThisAddIn.Application.OMathAutoCorrect.UseOutsideOMath = false;
return retString;
}
要替换字母,请使用以下代码
var script = @"\delta = (PL)/(AE)";
script = script.Replace("\delta", "\u03B4");}
您必须识别所有希腊字母并替换为等效的 UTF16 代码。
请找到以下网站以获取可用字母列表 https://www.fileformat.info/info/charset/UTF-16/list.htm