// 插入斜分数线 static void AddFraction(Word.Application app) { app.Selection.OMaths.Add(app.Selection.Range); OMathFunction range = app.Selection.OMaths[1].Functions.Add(app.Selection.Range, WdOMathFunctionType.wdOMathFunctionFrac); range.Frac.Type = WdOMathFracType.wdOMathFracSkw; app.Selection.MoveLeft(WdUnits.wdCharacter, 2, WdMovementType.wdMove); }
// 插入左大括号 static void AddCurl(Word.Application app) { app.Selection.OMaths.Add(app.Selection.Range); OMathFunction range = app.Selection.OMaths[1].Functions.Add(app.Selection.Range, WdOMathFunctionType.wdOMathFunctionDelim, 1); range.Delim.BegChar = (int)'{'; range.Delim.SepChar = 0; range.Delim.EndChar = 0; range.Delim.Grow = true; range.Delim.Shape = WdOMathShapeType.wdOMathShapeCentered; app.Selection.MoveLeft(WdUnits.wdCharacter, 1, WdMovementType.wdMove); }
// 插入带上下限求和 static void AddSum(Word.Application app) { app.Selection.OMaths.Add(app.Selection.Range); OMathFunction range = app.Selection.OMaths[1].Functions.Add(app.Selection.Range, WdOMathFunctionType.wdOMathFunctionNary); range.Nary.Char = (int)'∑'; range.Nary.Grow = false; range.Nary.SubSupLim = false; // 写在上下方 range.Nary.HideSub = false; range.Nary.HideSub = false; app.Selection.MoveLeft(WdUnits.wdCharacter, 3, WdMovementType.wdMove); // 移动光标至求和下限 }
// 插入带字长等号 static void AddLongEqual(Word.Application app) { app.Selection.OMaths.Add(app.Selection.Range); app.Selection.TypeText(" "); // 与分数前面分开 OMathFunction range = app.Selection.OMaths[1].Functions.Add(app.Selection.Range, WdOMathFunctionType.wdOMathFunctionFrac); range.Frac.Type = WdOMathFracType.wdOMathFracBar; app.Selection.TypeText(" "); // 与分数后面分开 app.Selection.MoveLeft(WdUnits.wdCharacter, 2, WdMovementType.wdMove); app.Selection.TypeText(" "); // 分母占位 app.Selection.OMaths[1].BuildUp(); app.Selection.MoveUp(WdUnits.wdLine, 1, WdMovementType.wdMove); range = app.Selection.OMaths[1].Functions.Add(app.Selection.Range, WdOMathFunctionType.wdOMathFunctionBar); range.Bar.BarTop = false; app.Selection.MoveLeft(WdUnits.wdCharacter, 1, WdMovementType.wdMove); }
private void 矩阵粘贴_Click(object sender, RibbonControlEventArgs e) { string text = Clipboard.GetText(TextDataFormat.UnicodeText); bool same = true; // 每行元素个数是否相同 bool multiSpaces = text.Contains(" "); // 是否有连续空格 text = Regex.Replace(text, @"\s+\+\s+", "+"); // 去除运算符空白 text = Regex.Replace(text, @"\s+\-\s+", "-"); // 去除运算符空白 text = Regex.Replace(text, @"\s+\*\s+", "*"); // 去除运算符空白 text = Regex.Replace(text, @"\s+\/\s+", "/"); // 去除运算符空白 text = Regex.Replace(text, @"\s+\^\s+", "^"); // 去除运算符空白 text = Regex.Replace(text, @"[\n\r]+", "\n"); // 多换行转单换行 text = Regex.Replace(text, @"[^\S\n\r]+", " "); // 多空格转单空格 text = Regex.Replace(text, @"^\s+", ""); // 去除全文开头的空白 text = Regex.Replace(text, @"\n\s+", "\n"); // 去除中间每行开头的空白 text = Regex.Replace(text, @"\s+$", ""); // 去除全文结尾的空白 if (!text.Contains("[") && !text.Contains("]") && !text.Contains(",") && !text.Contains(";") && !(text.Contains("\n") && multiSpaces)) // 多行时,还要求有连续的空格 { same = false; // 不是多列矩阵 } text = Regex.Replace(text, @"[\[\]]", ""); // 去除方括号 if (text.Contains(";")) { text = text.Replace(";", "\n"); // 行分割 } string[] map = @"alpha beta chi delta epsilon phi gamma eta iota kappa lambda mu nu pi theta rho sigma tau upsilon omega xi psi zeta hbar".Split(); foreach (string alpha in map) { text = Regex.Replace(text, @"(?<![\w\\])" + alpha + @"(?![a-zA-Z])", @"\" + alpha); text = Regex.Replace(text, @"(?<=_)" + alpha + @"(?![a-zA-Z])", @"\" + alpha); } string[] lines = text.Split('\n'); int M = lines.Length; string[][] elements = new string[M][]; for (int i = 0; i < M; i++) { elements[i] = lines[i].Split(lines[i].Contains(",") ? ',' : ' '); // 有逗号则用逗号分隔,否则用空格分隔 if (elements[i].Length != elements[0].Length) { same = false; break; } } if (same == false) { // 每行元素不同,应视作每行只有一个 for (int i = 0; i < M; i++) { elements[i] = new string[] { lines[i] }; } } int N = elements[0].Length; // 分隔每个元素 for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { string element = elements[i][j].Trim(); // 去除空白 element = element.Replace("*", "\\cdot "); // 替换乘号 element = Regex.Replace(element, @"sqrt", "\\sqrt"); // 现成根号 element = Regex.Replace(element, @"([^\(\)\s\+\-\*\/\^]+)\^\(1/2\)", "\\sqrt($1)"); // 替换根号 element = Regex.Replace(element, @"(?'c'\()([^\(\)]+)(?'-c'\))(?(c)(?!))\^\(1/2\)", "\\sqrt($1)"); elements[i][j] = element; } } app.Selection.Text = ""; // 清空选区 app.Selection.OMaths.Add(app.Selection.Range); if (Control.ModifierKeys.HasFlag(Keys.Alt)) // 用方括号包起来 { OMathFunction range = app.Selection.OMaths[1].Functions.Add(app.Selection.Range, WdOMathFunctionType.wdOMathFunctionDelim, 1); range.Delim.BegChar = (int)'['; range.Delim.SepChar = 0; range.Delim.EndChar = (int)']'; range.Delim.Grow = true; range.Delim.Shape = WdOMathShapeType.wdOMathShapeCentered; app.Selection.MoveLeft(WdUnits.wdCharacter, 1, WdMovementType.wdMove); } app.Selection.OMaths[1].Functions.Add(app.Selection.Range, WdOMathFunctionType.wdOMathFunctionMat, M * N, N); app.Selection.MoveLeft(WdUnits.wdCharacter, M * N, WdMovementType.wdMove); for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { app.Selection.TypeText(elements[i][j]); app.Selection.MoveRight(WdUnits.wdCharacter, 1, WdMovementType.wdMove); } } app.Selection.OMaths.Linearize(); // 转为线性公式 SendKeys.Send("^="); // 模拟按键,转为专业公式 }