1 /** The .tex to .lyx converter
2 \author André Pönitz (2003)
21 bool is_math_env(string const & name)
23 static char const * known_math_envs[] = { "equation", "equation*",
24 "eqnarray", "eqnarray*", "align", "align*", 0};
26 for (char const ** what = known_math_envs; *what; ++what)
33 void parse_math(Parser & p, ostream & os, unsigned flags, const mode_type mode)
36 Token const & t = p.get_token();
39 cerr << "t: " << t << " flags: " << flags << "\n";
42 if (flags & FLAG_ITEM) {
43 if (t.cat() == catSpace)
47 if (t.cat() == catBegin) {
48 // skip the brace and collect everything to the next matching
50 flags |= FLAG_BRACE_LAST;
54 // handle only this single token, leave the loop if done
62 if (t.cat() == catMath) {
63 if (mode == MATHTEXT_MODE) {
64 // we are inside some text mode thingy, so opening new math is allowed
65 Token const & n = p.get_token();
66 if (n.cat() == catMath) {
67 // TeX's $$...$$ syntax for displayed math
69 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
71 p.get_token(); // skip the second '$' token
76 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
81 else if (flags & FLAG_SIMPLE) {
82 // this is the end of the formula
87 cerr << "\nmode: " << mode << endl;
88 p.error("something strange in the parser\n");
93 else if (t.cat() == catLetter ||
94 t.cat() == catSpace ||
95 t.cat() == catSuper ||
97 t.cat() == catOther ||
98 t.cat() == catAlign ||
99 t.cat() == catActive ||
100 t.cat() == catParameter)
103 else if (t.cat() == catNewline) {
104 //if (p.next_token().cat() == catNewline) {
108 os << "\n "; // note the space
112 else if (t.cat() == catBegin) {
114 parse_math(p, os, FLAG_BRACE_LAST, mode);
118 else if (t.cat() == catEnd) {
119 if (flags & FLAG_BRACE_LAST)
121 os << "unexpected '}' in math\n";
124 else if (t.cat() == catComment)
131 else if (t.cs() == "(") {
133 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
137 else if (t.cs() == "[") {
138 // special handling of a few common SW user quirks
140 //if (p.next_token().cs() ==
142 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
146 else if (t.cs() == "protect")
147 // ignore \\protect, will hopefully be re-added during output
150 else if (t.cs() == "begin") {
151 string const name = p.getArg('{', '}');
152 active_environments.push_back(name);
153 os << "\\begin{" << name << "}";
154 if (name == "tabular")
155 parse_math(p, os, FLAG_END, MATHTEXT_MODE);
157 parse_math(p, os, FLAG_END, mode);
158 os << "\\end{" << name << "}";
161 else if (t.cs() == "end") {
162 if (flags & FLAG_END) {
163 // eat environment name
164 string const name = p.getArg('{', '}');
165 if (name != active_environment())
166 p.error("\\end{" + name + "} does not match \\begin{"
167 + active_environment() + "}");
168 active_environments.pop_back();
171 p.error("found 'end' unexpectedly");
174 else if (t.cs() == ")") {
175 if (flags & FLAG_SIMPLE2)
177 p.error("found '\\)' unexpectedly");
180 else if (t.cs() == "]") {
181 if (flags & FLAG_EQUATION)
183 p.error("found '\\]' unexpectedly");
186 else if (t.cs() == "textrm" || t.cs() == "textsf" || t.cs() == "textbf"
187 || t.cs() == "texttt" || t.cs() == "textsc") {
188 os << '\\' << t.cs() << '{';
189 parse_math(p, os, FLAG_ITEM, MATHTEXT_MODE);
193 else if (t.cs() == "mbox") {
195 parse_math(p, os, FLAG_ITEM, MATHTEXT_MODE);
199 else if (t.cs() == "\"") {
200 string const name = p.verbatim_item();
201 if (name == "a") os << 'ä';
202 else if (name == "o") os << 'ö';
203 else if (name == "u") os << 'ü';
204 else if (name == "A") os << 'Ä';
205 else if (name == "O") os << 'Ö';
206 else if (name == "U") os << 'Ü';
207 else os << "\"{" << name << "}";
210 else if (t.cs() == "ss")
216 if (flags & FLAG_LEAVE) {
217 flags &= ~FLAG_LEAVE;