+
+ string fromMathematicaName(string const & name)
+ {
+ if (name == "Sin") return "sin";
+ if (name == "Sinh") return "sinh";
+ if (name == "ArcSin") return "arcsin";
+ if (name == "Cos") return "cos";
+ if (name == "Cosh") return "cosh";
+ if (name == "ArcCos") return "arccos";
+ if (name == "Tan") return "tan";
+ if (name == "Tanh") return "tanh";
+ if (name == "ArcTan") return "arctan";
+ if (name == "Cot") return "cot";
+ if (name == "Coth") return "coth";
+ if (name == "Csc") return "csc";
+ if (name == "Sec") return "sec";
+ if (name == "Exp") return "exp";
+ if (name == "Log") return "log";
+ if (name == "Arg" ) return "arg";
+ if (name == "Det" ) return "det";
+ if (name == "GCD" ) return "gcd";
+ if (name == "Max" ) return "max";
+ if (name == "Min" ) return "min";
+ if (name == "Erf" ) return "erf";
+ if (name == "Erfc" ) return "erfc";
+ return name;
+ }
+
+
+ void prettifyMathematicaOutput(string & out, string const & macroName,
+ bool roman, bool translate)
+ {
+ string const macro = "\\" + macroName + "{";
+ string::size_type const len = macro.length();
+ string::size_type i = out.find(macro);
+
+ while (i != string::npos) {
+ string::size_type const j = get_matching_brace(out, i + len);
+ string const name = out.substr(i + len, j - i - len);
+ out = out.substr(0, i)
+ + (roman ? "\\mathrm{" : "")
+ + (translate ? fromMathematicaName(name) : name)
+ + out.substr(roman ? j : j + 1);
+ //lyxerr << "out: " << out << endl;
+ i = out.find(macro, i);
+ }
+ }
+
+
+ MathArray pipeThroughMathematica(string const &, MathArray const & ar)
+ {
+ ostringstream os;
+ MathematicaStream ms(os);
+ ms << ar;
+ string const expr = os.str();
+ string out;
+
+ lyxerr << "expr: '" << expr << "'" << endl;
+
+ string const full = "TeXForm[" + expr + "]";
+ out = captureOutput("math", full);
+ lyxerr << "out: '" << out << "'" << endl;
+
+ string::size_type pos1 = out.find("Out[1]//TeXForm= ");
+ string::size_type pos2 = out.find("In[2]:=");
+
+ if (pos1 == string::npos || pos2 == string::npos)
+ return MathArray();
+
+ // get everything from pos1+17 to pos2
+ out = out.substr(pos1 + 17, pos2 - pos1 - 17);
+ out = subst(subst(out, '\r', ' '), '\n', ' ');
+
+ // tries to make the result prettier
+ prettifyMathematicaOutput(out, "Mfunction", true, true);
+ prettifyMathematicaOutput(out, "Muserfunction", true, false);
+ prettifyMathematicaOutput(out, "Mvariable", false, false);
+
+ MathArray res;
+ mathed_parse_cell(res, out);
+ return res;
+ }
+