void TranslatePlain(string str, StringBuilder dst) { var i = 0; while (i < str.Length) { var c = str[i++]; if (TexFormulaParser.IsParserReserved(c)) { dst.Append("\\" + c); } else { dst.Append(c); } } }
void TranslateLatex(string str, StringBuilder dst) { var i = 0; char n = '\0'; while (i < str.Length) { var c = str[i++]; n = i < str.Length ? str[i] : '\0'; if (c == ' ') { if (!discardSpaces) { dst.Append(c); } } else if (c == '\\') { if (TexFormulaParser.IsParserReserved(n)) { dst.Append(c); continue; } else if (char.IsLetter(n)) { var cmd = TexFormulaParser.LookForAWord(str, ref i); TexFormulaParser.SkipWhiteSpace(str, ref i); // Symbol renames if (cmd == "to") { dst.Append("\\rightarrow"); continue; } if (i >= str.Length) { dst.Append("\\" + cmd); continue; } // Lim (or other funcs) goes to over/under if even there single script if (cmd == "lim") { c = str[i]; if (c == '^' || c == '_') { dst.Append("\\" + cmd + c); } else { dst.Append("\\" + cmd); } continue; } // color but the id in braces if (cmd == "color" && (str[i] == '{')) { var arg = TexFormulaParser.ReadGroup(str, ref i, '{', '}'); if (arg.IndexOf(' ') < 0) { dst.Append("\\color[" + arg + "]"); } else { dst.Append("\\color{" + arg + "}"); } continue; } // \over... or \under.... int ou = cmd.IndexOf("over") == 0 ? 2 : (cmd.IndexOf("under") == 0 ? 1 : 0); if (ou != 0) { var second = cmd.Substring(ou == 2 ? 4 : 5); if (second == "line") { dst.Append(ou == 2 ? "\\over" : "\\under"); continue; } c = str[i]; if (c == '{') { var arg1 = "{" + TexFormulaParser.ReadGroup(str, ref i, '{', '}') + "}"; if (second == "brace") { // \overbrace or \underbrace if (i + 1 < str.Length) { c = str[i++]; n = str[i]; if ((c == '^' || c == '_') && n == '{') { // If there's script we need to think another strategy var arg2 = TexFormulaParser.ReadGroup(str, ref i, '{', '}'); var arg3 = ou == 2 ? "\\lbrace" : "\\rbrace"; if (c == '^') { dst.Append("\\nfrac{\\size[.]{" + arg2 + "}__" + arg3 + "}{" + arg1 + "}"); } else { dst.Append("\\nfrac{" + arg1 + "}{\\size[.]{" + arg2 + "}^^" + arg3 + "}"); } continue; } else { i--; } } } dst.Append(arg1); dst.Append(ou == 2 ? "^^" : "__"); dst.Append("\\" + second); continue; } } //Default behav dst.Append("\\" + cmd); } } else { dst.Append(c); } } }