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.
25 bool is_math_env(string const & name)
27 return known_math_environments.find(name) != known_math_environments.end();
31 bool is_display_math_env(string const & name)
33 CommandMap::const_iterator it = known_math_environments.find(name);
34 if (it != known_math_environments.end())
35 if (!it->second.empty())
36 return it->second.back() == displaymath;
41 void parse_math(Parser & p, ostream & os, unsigned flags, const mode_type mode)
44 Token const & t = p.get_token();
47 cerr << "t: " << t << " flags: " << flags << "\n";
50 if (flags & FLAG_ITEM) {
51 if (t.cat() == catSpace)
55 if (t.cat() == catBegin) {
56 // skip the brace and collect everything to the next matching
58 flags |= FLAG_BRACE_LAST;
62 // handle only this single token, leave the loop if done
70 if (t.cat() == catMath) {
71 if (mode == MATHTEXT_MODE) {
72 // we are inside some text mode thing, so opening new math is allowed
73 Token const & n = p.get_token();
74 if (n.cat() == catMath) {
75 // TeX's $$...$$ syntax for displayed math
77 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
79 p.get_token(); // skip the second '$' token
84 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
89 else if (flags & FLAG_SIMPLE) {
90 // this is the end of the formula
95 cerr << "\nmode: " << mode << endl;
96 p.error("something strange in the parser\n");
101 else if (t.cat() == catLetter ||
102 t.cat() == catSuper ||
104 t.cat() == catOther ||
105 t.cat() == catAlign ||
106 t.cat() == catActive ||
107 t.cat() == catParameter)
110 else if (t.cat() == catBegin) {
112 parse_math(p, os, FLAG_BRACE_LAST, mode);
116 else if (t.cat() == catEnd) {
117 if (flags & FLAG_BRACE_LAST)
119 os << "unexpected '}' in math\n";
122 else if (t.cat() == catComment) {
134 else if (t.cs() == "(") {
136 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
140 else if (t.cs() == "[") {
141 // special handling of a few common SW user quirks
143 //if (p.next_token().cs() ==
145 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
149 else if (t.cs() == "protect")
150 // ignore \\protect, will hopefully be re-added during output
153 else if (t.cs() == "begin") {
154 string const name = p.getArg('{', '}');
155 active_environments.push_back(name);
156 os << "\\begin{" << name << "}";
157 if (name == "tabular")
158 parse_math(p, os, FLAG_END, MATHTEXT_MODE);
160 parse_math(p, os, FLAG_END, mode);
161 os << "\\end{" << name << "}";
162 active_environments.pop_back();
165 else if (t.cs() == "end") {
166 if (flags & FLAG_END) {
167 // eat environment name
168 string const name = p.getArg('{', '}');
169 if (name != active_environment())
170 p.error("\\end{" + name + "} does not match \\begin{"
171 + active_environment() + "}");
174 p.error("found 'end' unexpectedly");
177 else if (t.cs() == ")") {
178 if (flags & FLAG_SIMPLE2)
180 p.error("found '\\)' unexpectedly");
183 else if (t.cs() == "]") {
184 if (flags & FLAG_EQUATION)
186 p.error("found '\\]' unexpectedly");
189 else if (t.cs() == "textrm" || t.cs() == "textsf" || t.cs() == "textbf"
190 || t.cs() == "texttt" || t.cs() == "textsc"
191 || t.cs() == "text" || t.cs() == "intertext") {
192 os << '\\' << t.cs() << '{';
193 parse_math(p, os, FLAG_ITEM, MATHTEXT_MODE);
197 else if (t.cs() == "tag") {
198 os << '\\' << t.cs();
199 if (p.next_token().asInput() == "*") {
204 parse_math(p, os, FLAG_ITEM, MATHTEXT_MODE);
208 else if (t.cs() == "mbox" || t.cs() == "fbox") {
209 os << "\\" << t.cs() << '{';
210 parse_math(p, os, FLAG_ITEM, MATHTEXT_MODE);
214 else if (t.cs() == "\"") {
215 string const name = p.verbatim_item();
216 if (name == "a") os << '\xe4';
217 else if (name == "o") os << '\xf6';
218 else if (name == "u") os << '\xfc';
219 else if (name == "A") os << '\xc4';
220 else if (name == "O") os << '\xd6';
221 else if (name == "U") os << '\xdc';
222 else os << "\"{" << name << "}";
225 else if (t.cs() == "ss")
228 else if (t.cs() == "cr") {
229 // lyx can't handle \\cr
230 cerr << "Warning: Converting TeX '\\cr' to LaTeX '\\\\'."
235 else if (t.cs() == "vref" || t.cs() == "vpageref") {
237 preamble.registerAutomaticallyLoadedPackage("varioref");
240 else if (t.cs() == "textipa") {
242 preamble.registerAutomaticallyLoadedPackage("tipa");
248 if (flags & FLAG_LEAVE) {
249 flags &= ~FLAG_LEAVE;