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.
24 bool is_math_env(string const & name)
26 return known_math_environments.find(name) != known_math_environments.end();
30 void parse_math(Parser & p, ostream & os, unsigned flags, const mode_type mode)
33 Token const & t = p.get_token();
36 cerr << "t: " << t << " flags: " << flags << "\n";
39 if (flags & FLAG_ITEM) {
40 if (t.cat() == catSpace)
44 if (t.cat() == catBegin) {
45 // skip the brace and collect everything to the next matching
47 flags |= FLAG_BRACE_LAST;
51 // handle only this single token, leave the loop if done
59 if (t.cat() == catMath) {
60 if (mode == MATHTEXT_MODE) {
61 // we are inside some text mode thing, so opening new math is allowed
62 Token const & n = p.get_token();
63 if (n.cat() == catMath) {
64 // TeX's $$...$$ syntax for displayed math
66 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
68 p.get_token(); // skip the second '$' token
73 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
78 else if (flags & FLAG_SIMPLE) {
79 // this is the end of the formula
84 cerr << "\nmode: " << mode << endl;
85 p.error("something strange in the parser\n");
90 else if (t.cat() == catLetter ||
91 t.cat() == catSuper ||
93 t.cat() == catOther ||
94 t.cat() == catAlign ||
95 t.cat() == catActive ||
96 t.cat() == catParameter)
99 else if (t.cat() == catBegin) {
101 parse_math(p, os, FLAG_BRACE_LAST, mode);
105 else if (t.cat() == catEnd) {
106 if (flags & FLAG_BRACE_LAST)
108 os << "unexpected '}' in math\n";
111 else if (t.cat() == catComment) {
113 cerr << "Ignoring comment: " << t.asInput();
123 else if (t.cs() == "(") {
125 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
129 else if (t.cs() == "[") {
130 // special handling of a few common SW user quirks
132 //if (p.next_token().cs() ==
134 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
138 else if (t.cs() == "protect")
139 // ignore \\protect, will hopefully be re-added during output
142 else if (t.cs() == "begin") {
143 string const name = p.getArg('{', '}');
144 active_environments.push_back(name);
145 os << "\\begin{" << name << "}";
146 if (name == "tabular")
147 parse_math(p, os, FLAG_END, MATHTEXT_MODE);
149 parse_math(p, os, FLAG_END, mode);
150 os << "\\end{" << name << "}";
151 active_environments.pop_back();
154 else if (t.cs() == "end") {
155 if (flags & FLAG_END) {
156 // eat environment name
157 string const name = p.getArg('{', '}');
158 if (name != active_environment())
159 p.error("\\end{" + name + "} does not match \\begin{"
160 + active_environment() + "}");
163 p.error("found 'end' unexpectedly");
166 else if (t.cs() == ")") {
167 if (flags & FLAG_SIMPLE2)
169 p.error("found '\\)' unexpectedly");
172 else if (t.cs() == "]") {
173 if (flags & FLAG_EQUATION)
175 p.error("found '\\]' unexpectedly");
178 else if (t.cs() == "textrm" || t.cs() == "textsf" || t.cs() == "textbf"
179 || t.cs() == "texttt" || t.cs() == "textsc"
180 || t.cs() == "text" || t.cs() == "intertext") {
181 os << '\\' << t.cs() << '{';
182 parse_math(p, os, FLAG_ITEM, MATHTEXT_MODE);
186 else if (t.cs() == "tag") {
187 os << '\\' << t.cs();
188 if (p.next_token().asInput() == "*") {
193 parse_math(p, os, FLAG_ITEM, MATHTEXT_MODE);
197 else if (t.cs() == "mbox" || t.cs() == "fbox") {
198 os << "\\" << t.cs() << '{';
199 parse_math(p, os, FLAG_ITEM, MATHTEXT_MODE);
203 else if (t.cs() == "\"") {
204 string const name = p.verbatim_item();
205 if (name == "a") os << '\xe4';
206 else if (name == "o") os << '\xf6';
207 else if (name == "u") os << '\xfc';
208 else if (name == "A") os << '\xc4';
209 else if (name == "O") os << '\xd6';
210 else if (name == "U") os << '\xdc';
211 else os << "\"{" << name << "}";
214 else if (t.cs() == "ss")
217 else if (t.cs() == "cr") {
218 // lyx can't handle \\cr
219 cerr << "Warning: Converting TeX '\\cr' to LaTeX '\\\\'."
227 if (flags & FLAG_LEAVE) {
228 flags &= ~FLAG_LEAVE;