3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
29 bool is_math_env(string const & name)
31 return known_math_environments.find(name) != known_math_environments.end();
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() == catSuper ||
98 t.cat() == catOther ||
99 t.cat() == catAlign ||
100 t.cat() == catActive ||
101 t.cat() == catParameter)
104 else if (t.cat() == catBegin) {
106 parse_math(p, os, FLAG_BRACE_LAST, mode);
110 else if (t.cat() == catEnd) {
111 if (flags & FLAG_BRACE_LAST)
113 os << "unexpected '}' in math\n";
116 else if (t.cat() == catComment) {
118 cerr << "Ignoring comment: " << t.asInput();
128 else if (t.cs() == "(") {
130 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
134 else if (t.cs() == "[") {
135 // special handling of a few common SW user quirks
137 //if (p.next_token().cs() ==
139 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
143 else if (t.cs() == "protect")
144 // ignore \\protect, will hopefully be re-added during output
147 else if (t.cs() == "begin") {
148 string const name = p.getArg('{', '}');
149 active_environments.push_back(name);
150 os << "\\begin{" << name << "}";
151 if (name == "tabular")
152 parse_math(p, os, FLAG_END, MATHTEXT_MODE);
154 parse_math(p, os, FLAG_END, mode);
155 os << "\\end{" << name << "}";
156 active_environments.pop_back();
159 else if (t.cs() == "end") {
160 if (flags & FLAG_END) {
161 // eat environment name
162 string const name = p.getArg('{', '}');
163 if (name != active_environment())
164 p.error("\\end{" + name + "} does not match \\begin{"
165 + active_environment() + "}");
168 p.error("found 'end' unexpectedly");
171 else if (t.cs() == ")") {
172 if (flags & FLAG_SIMPLE2)
174 p.error("found '\\)' unexpectedly");
177 else if (t.cs() == "]") {
178 if (flags & FLAG_EQUATION)
180 p.error("found '\\]' unexpectedly");
183 else if (t.cs() == "textrm" || t.cs() == "textsf" || t.cs() == "textbf"
184 || t.cs() == "texttt" || t.cs() == "textsc"
185 || t.cs() == "text" || t.cs() == "intertext") {
186 os << '\\' << t.cs() << '{';
187 parse_math(p, os, FLAG_ITEM, MATHTEXT_MODE);
191 else if (t.cs() == "tag") {
192 os << '\\' << t.cs();
193 if (p.next_token().asInput() == "*") {
198 parse_math(p, os, FLAG_ITEM, MATHTEXT_MODE);
202 else if (t.cs() == "mbox" || t.cs() == "fbox") {
203 os << "\\" << t.cs() << '{';
204 parse_math(p, os, FLAG_ITEM, MATHTEXT_MODE);
208 else if (t.cs() == "\"") {
209 string const name = p.verbatim_item();
210 if (name == "a") os << '\xe4';
211 else if (name == "o") os << '\xf6';
212 else if (name == "u") os << '\xfc';
213 else if (name == "A") os << '\xc4';
214 else if (name == "O") os << '\xd6';
215 else if (name == "U") os << '\xdc';
216 else os << "\"{" << name << "}";
219 else if (t.cs() == "ss")
222 else if (t.cs() == "cr") {
223 // lyx can't handle \\cr
224 cerr << "Warning: Converting TeX '\\cr' to LaTeX '\\\\'."
232 if (flags & FLAG_LEAVE) {
233 flags &= ~FLAG_LEAVE;