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*", "gather", "gather*",
25 "multline", "multline*", 0};
27 for (char const ** what = known_math_envs; *what; ++what)
34 void parse_math(Parser & p, ostream & os, unsigned flags, const mode_type mode)
37 Token const & t = p.get_token();
40 cerr << "t: " << t << " flags: " << flags << "\n";
43 if (flags & FLAG_ITEM) {
44 if (t.cat() == catSpace)
48 if (t.cat() == catBegin) {
49 // skip the brace and collect everything to the next matching
51 flags |= FLAG_BRACE_LAST;
55 // handle only this single token, leave the loop if done
63 if (t.cat() == catMath) {
64 if (mode == MATHTEXT_MODE) {
65 // we are inside some text mode thingy, so opening new math is allowed
66 Token const & n = p.get_token();
67 if (n.cat() == catMath) {
68 // TeX's $$...$$ syntax for displayed math
70 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
72 p.get_token(); // skip the second '$' token
77 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
82 else if (flags & FLAG_SIMPLE) {
83 // this is the end of the formula
88 cerr << "\nmode: " << mode << endl;
89 p.error("something strange in the parser\n");
94 else if (t.cat() == catLetter ||
95 t.cat() == catSpace ||
96 t.cat() == catSuper ||
98 t.cat() == catOther ||
99 t.cat() == catAlign ||
100 t.cat() == catActive ||
101 t.cat() == catParameter)
104 else if (t.cat() == catNewline) {
105 //if (p.next_token().cat() == catNewline) {
109 os << "\n "; // note the space
113 else if (t.cat() == catBegin) {
115 parse_math(p, os, FLAG_BRACE_LAST, mode);
119 else if (t.cat() == catEnd) {
120 if (flags & FLAG_BRACE_LAST)
122 os << "unexpected '}' in math\n";
125 else if (t.cat() == catComment)
132 else if (t.cs() == "(") {
134 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
138 else if (t.cs() == "[") {
139 // special handling of a few common SW user quirks
141 //if (p.next_token().cs() ==
143 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
147 else if (t.cs() == "protect")
148 // ignore \\protect, will hopefully be re-added during output
151 else if (t.cs() == "begin") {
152 string const name = p.getArg('{', '}');
153 active_environments.push_back(name);
154 os << "\\begin{" << name << "}";
155 if (name == "tabular")
156 parse_math(p, os, FLAG_END, MATHTEXT_MODE);
158 parse_math(p, os, FLAG_END, mode);
159 os << "\\end{" << name << "}";
162 else if (t.cs() == "end") {
163 if (flags & FLAG_END) {
164 // eat environment name
165 string const name = p.getArg('{', '}');
166 if (name != active_environment())
167 p.error("\\end{" + name + "} does not match \\begin{"
168 + active_environment() + "}");
169 active_environments.pop_back();
172 p.error("found 'end' unexpectedly");
175 else if (t.cs() == ")") {
176 if (flags & FLAG_SIMPLE2)
178 p.error("found '\\)' unexpectedly");
181 else if (t.cs() == "]") {
182 if (flags & FLAG_EQUATION)
184 p.error("found '\\]' unexpectedly");
187 else if (t.cs() == "textrm" || t.cs() == "textsf" || t.cs() == "textbf"
188 || t.cs() == "texttt" || t.cs() == "textsc") {
189 os << '\\' << t.cs() << '{';
190 parse_math(p, os, FLAG_ITEM, MATHTEXT_MODE);
194 else if (t.cs() == "mbox") {
196 parse_math(p, os, FLAG_ITEM, MATHTEXT_MODE);
200 else if (t.cs() == "\"") {
201 string const name = p.verbatim_item();
202 if (name == "a") os << 'ä';
203 else if (name == "o") os << 'ö';
204 else if (name == "u") os << 'ü';
205 else if (name == "A") os << 'Ä';
206 else if (name == "O") os << 'Ö';
207 else if (name == "U") os << 'Ü';
208 else os << "\"{" << name << "}";
211 else if (t.cs() == "ss")
217 if (flags & FLAG_LEAVE) {
218 flags &= ~FLAG_LEAVE;