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.
26 bool is_math_env(string const & name)
28 static char const * known_math_envs[] = { "equation", "equation*",
29 "eqnarray", "eqnarray*", "align", "align*", "gather", "gather*",
30 "multline", "multline*", "math", "displaymath", "flalign", "flalign*",
31 // These require extra args
32 "alignat", "alignat*", "xalignat", "xalignat*", "xxalignat",
35 for (char const ** what = known_math_envs; *what; ++what)
42 void parse_math(Parser & p, ostream & os, unsigned flags, const mode_type mode)
45 Token const & t = p.get_token();
48 cerr << "t: " << t << " flags: " << flags << "\n";
51 if (flags & FLAG_ITEM) {
52 if (t.cat() == catSpace)
56 if (t.cat() == catBegin) {
57 // skip the brace and collect everything to the next matching
59 flags |= FLAG_BRACE_LAST;
63 // handle only this single token, leave the loop if done
71 if (t.cat() == catMath) {
72 if (mode == MATHTEXT_MODE) {
73 // we are inside some text mode thingy, so opening new math is allowed
74 Token const & n = p.get_token();
75 if (n.cat() == catMath) {
76 // TeX's $$...$$ syntax for displayed math
78 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
80 p.get_token(); // skip the second '$' token
85 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
90 else if (flags & FLAG_SIMPLE) {
91 // this is the end of the formula
96 cerr << "\nmode: " << mode << endl;
97 p.error("something strange in the parser\n");
102 else if (t.cat() == catLetter ||
103 t.cat() == catSuper ||
105 t.cat() == catOther ||
106 t.cat() == catAlign ||
107 t.cat() == catActive ||
108 t.cat() == catParameter)
111 else if (t.cat() == catBegin) {
113 parse_math(p, os, FLAG_BRACE_LAST, mode);
117 else if (t.cat() == catEnd) {
118 if (flags & FLAG_BRACE_LAST)
120 os << "unexpected '}' in math\n";
123 else if (t.cat() == catComment) {
125 cerr << "Ignoring comment: " << t.asInput();
135 else if (t.cs() == "(") {
137 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
141 else if (t.cs() == "[") {
142 // special handling of a few common SW user quirks
144 //if (p.next_token().cs() ==
146 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
150 else if (t.cs() == "protect")
151 // ignore \\protect, will hopefully be re-added during output
154 else if (t.cs() == "begin") {
155 string const name = p.getArg('{', '}');
156 active_environments.push_back(name);
157 os << "\\begin{" << name << "}";
158 if (name == "tabular")
159 parse_math(p, os, FLAG_END, MATHTEXT_MODE);
161 parse_math(p, os, FLAG_END, mode);
162 os << "\\end{" << name << "}";
163 active_environments.pop_back();
166 else if (t.cs() == "end") {
167 if (flags & FLAG_END) {
168 // eat environment name
169 string const name = p.getArg('{', '}');
170 if (name != active_environment())
171 p.error("\\end{" + name + "} does not match \\begin{"
172 + active_environment() + "}");
175 p.error("found 'end' unexpectedly");
178 else if (t.cs() == ")") {
179 if (flags & FLAG_SIMPLE2)
181 p.error("found '\\)' unexpectedly");
184 else if (t.cs() == "]") {
185 if (flags & FLAG_EQUATION)
187 p.error("found '\\]' unexpectedly");
190 else if (t.cs() == "textrm" || t.cs() == "textsf" || t.cs() == "textbf"
191 || t.cs() == "texttt" || t.cs() == "textsc"
192 || t.cs() == "text" || t.cs() == "intertext") {
193 os << '\\' << t.cs() << '{';
194 parse_math(p, os, FLAG_ITEM, MATHTEXT_MODE);
198 else if (t.cs() == "mbox" || t.cs() == "fbox") {
199 os << "\\" << t.cs() << '{';
200 parse_math(p, os, FLAG_ITEM, MATHTEXT_MODE);
204 else if (t.cs() == "\"") {
205 string const name = p.verbatim_item();
206 if (name == "a") os << 'ä';
207 else if (name == "o") os << 'ö';
208 else if (name == "u") os << 'ü';
209 else if (name == "A") os << 'Ä';
210 else if (name == "O") os << 'Ö';
211 else if (name == "U") os << 'Ü';
212 else os << "\"{" << name << "}";
215 else if (t.cs() == "ss")
221 if (flags & FLAG_LEAVE) {
222 flags &= ~FLAG_LEAVE;