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 bool is_display_math_env(string const & name)
32 CommandMap::const_iterator it = known_math_environments.find(name);
33 if (it != known_math_environments.end())
34 if (!it->second.empty())
35 return it->second.back() == displaymath;
40 void parse_math(Parser & p, ostream & os, unsigned flags, const mode_type mode)
43 Token const & t = p.get_token();
46 cerr << "t: " << t << " flags: " << flags << "\n";
49 if (flags & FLAG_ITEM) {
50 if (t.cat() == catSpace)
54 if (t.cat() == catBegin) {
55 // skip the brace and collect everything to the next matching
57 flags |= FLAG_BRACE_LAST;
61 // handle only this single token, leave the loop if done
69 if (t.cat() == catMath) {
70 if (mode == MATHTEXT_MODE) {
71 // we are inside some text mode thing, so opening new math is allowed
72 Token const & n = p.get_token();
73 if (n.cat() == catMath) {
74 // TeX's $$...$$ syntax for displayed math
76 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
78 p.get_token(); // skip the second '$' token
83 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
88 else if (flags & FLAG_SIMPLE) {
89 // this is the end of the formula
94 cerr << "\nmode: " << mode << endl;
95 p.error("something strange in the parser\n");
100 else if (t.cat() == catLetter ||
101 t.cat() == catSuper ||
103 t.cat() == catOther ||
104 t.cat() == catAlign ||
105 t.cat() == catActive ||
106 t.cat() == catParameter)
109 else if (t.cat() == catBegin) {
111 parse_math(p, os, FLAG_BRACE_LAST, mode);
115 else if (t.cat() == catEnd) {
116 if (flags & FLAG_BRACE_LAST)
118 os << "unexpected '}' in math\n";
121 else if (t.cat() == catComment) {
123 cerr << "Ignoring comment: " << t.asInput();
133 else if (t.cs() == "(") {
135 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
139 else if (t.cs() == "[") {
140 // special handling of a few common SW user quirks
142 //if (p.next_token().cs() ==
144 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
148 else if (t.cs() == "protect")
149 // ignore \\protect, will hopefully be re-added during output
152 else if (t.cs() == "begin") {
153 string const name = p.getArg('{', '}');
154 active_environments.push_back(name);
155 os << "\\begin{" << name << "}";
156 if (name == "tabular")
157 parse_math(p, os, FLAG_END, MATHTEXT_MODE);
159 parse_math(p, os, FLAG_END, mode);
160 os << "\\end{" << name << "}";
161 active_environments.pop_back();
164 else if (t.cs() == "end") {
165 if (flags & FLAG_END) {
166 // eat environment name
167 string const name = p.getArg('{', '}');
168 if (name != active_environment())
169 p.error("\\end{" + name + "} does not match \\begin{"
170 + active_environment() + "}");
173 p.error("found 'end' unexpectedly");
176 else if (t.cs() == ")") {
177 if (flags & FLAG_SIMPLE2)
179 p.error("found '\\)' unexpectedly");
182 else if (t.cs() == "]") {
183 if (flags & FLAG_EQUATION)
185 p.error("found '\\]' unexpectedly");
188 else if (t.cs() == "textrm" || t.cs() == "textsf" || t.cs() == "textbf"
189 || t.cs() == "texttt" || t.cs() == "textsc"
190 || t.cs() == "text" || t.cs() == "intertext") {
191 os << '\\' << t.cs() << '{';
192 parse_math(p, os, FLAG_ITEM, MATHTEXT_MODE);
196 else if (t.cs() == "tag") {
197 os << '\\' << t.cs();
198 if (p.next_token().asInput() == "*") {
203 parse_math(p, os, FLAG_ITEM, MATHTEXT_MODE);
207 else if (t.cs() == "mbox" || t.cs() == "fbox") {
208 os << "\\" << t.cs() << '{';
209 parse_math(p, os, FLAG_ITEM, MATHTEXT_MODE);
213 else if (t.cs() == "\"") {
214 string const name = p.verbatim_item();
215 if (name == "a") os << '\xe4';
216 else if (name == "o") os << '\xf6';
217 else if (name == "u") os << '\xfc';
218 else if (name == "A") os << '\xc4';
219 else if (name == "O") os << '\xd6';
220 else if (name == "U") os << '\xdc';
221 else os << "\"{" << name << "}";
224 else if (t.cs() == "ss")
227 else if (t.cs() == "cr") {
228 // lyx can't handle \\cr
229 cerr << "Warning: Converting TeX '\\cr' to LaTeX '\\\\'."
237 if (flags & FLAG_LEAVE) {
238 flags &= ~FLAG_LEAVE;