1 /** The .tex to .lyx converter
2 \author André Pönitz (2003)
23 bool is_math_env(string const & name)
25 static char const * known_math_envs[] = { "equation", "equation*",
26 "eqnarray", "eqnarray*", "align", "align*", 0};
28 for (char const ** what = known_math_envs; *what; ++what)
35 void parse_math(Parser & p, ostream & os, unsigned flags, const mode_type mode)
38 Token const & t = p.get_token();
41 cerr << "t: " << t << " flags: " << flags << "\n";
44 if (flags & FLAG_ITEM) {
45 if (t.cat() == catSpace)
49 if (t.cat() == catBegin) {
50 // skip the brace and collect everything to the next matching
52 flags |= FLAG_BRACE_LAST;
56 // handle only this single token, leave the loop if done
64 if (t.cat() == catMath) {
65 if (mode == MATHTEXT_MODE) {
66 // we are inside some text mode thingy, so opening new math is allowed
67 Token const & n = p.get_token();
68 if (n.cat() == catMath) {
69 // TeX's $$...$$ syntax for displayed math
71 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
73 p.get_token(); // skip the second '$' token
78 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
83 else if (flags & FLAG_SIMPLE) {
84 // this is the end of the formula
89 cerr << "\nmode: " << mode << endl;
90 p.error("something strange in the parser\n");
95 else if (t.cat() == catLetter ||
96 t.cat() == catSpace ||
97 t.cat() == catSuper ||
99 t.cat() == catOther ||
100 t.cat() == catAlign ||
101 t.cat() == catActive ||
102 t.cat() == catParameter)
105 else if (t.cat() == catNewline) {
106 //if (p.next_token().cat() == catNewline) {
110 os << "\n "; // note the space
114 else if (t.cat() == catBegin) {
116 parse_math(p, os, FLAG_BRACE_LAST, mode);
120 else if (t.cat() == catEnd) {
121 if (flags & FLAG_BRACE_LAST)
123 os << "unexpected '}' in math\n";
126 else if (t.cat() == catComment)
133 else if (t.cs() == "(") {
135 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
139 else if (t.cs() == "[") {
141 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
145 else if (t.cs() == "protect")
146 // ignore \\protect, will hopefully be re-added during output
149 else if (t.cs() == "begin") {
150 string const name = p.getArg('{', '}');
151 active_environments.push_back(name);
152 os << "\\begin{" << name << "}";
153 if (name == "tabular")
154 parse_math(p, os, FLAG_END, MATHTEXT_MODE);
156 parse_math(p, os, FLAG_END, mode);
157 os << "\\end{" << name << "}";
160 else if (t.cs() == "end") {
161 if (flags & FLAG_END) {
162 // eat environment name
163 string const name = p.getArg('{', '}');
164 if (name != active_environment())
165 p.error("\\end{" + name + "} does not match \\begin{"
166 + active_environment() + "}");
167 active_environments.pop_back();
170 p.error("found 'end' unexpectedly");
173 else if (t.cs() == ")") {
174 if (flags & FLAG_SIMPLE2)
176 p.error("found '\\)' unexpectedly");
179 else if (t.cs() == "]") {
180 if (flags & FLAG_EQUATION)
182 p.error("found '\\]' unexpectedly");
185 else if (t.cs() == "textrm" || t.cs() == "textsf" || t.cs() == "textbf"
186 || t.cs() == "texttt" || t.cs() == "textsc") {
187 os << '\\' << t.cs() << '{';
188 parse_math(p, os, FLAG_ITEM, MATHTEXT_MODE);
192 else if (t.cs() == "mbox") {
194 parse_math(p, os, FLAG_ITEM, MATHTEXT_MODE);
198 else if (t.cs() == "\"") {
199 string const name = p.verbatim_item();
200 if (name == "a") os << 'ä';
201 else if (name == "o") os << 'ö';
202 else if (name == "u") os << 'ü';
203 else if (name == "A") os << 'Ä';
204 else if (name == "O") os << 'Ö';
205 else if (name == "U") os << 'Ü';
206 else os << "\"{" << name << "}";
209 else if (t.cs() == "ss")
215 if (flags & FLAG_LEAVE) {
216 flags &= ~FLAG_LEAVE;