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 return known_math_environments.find(name) != known_math_environments.end();
32 void parse_math(Parser & p, ostream & os, unsigned flags, const mode_type mode)
35 Token const & t = p.get_token();
38 cerr << "t: " << t << " flags: " << flags << "\n";
41 if (flags & FLAG_ITEM) {
42 if (t.cat() == catSpace)
46 if (t.cat() == catBegin) {
47 // skip the brace and collect everything to the next matching
49 flags |= FLAG_BRACE_LAST;
53 // handle only this single token, leave the loop if done
61 if (t.cat() == catMath) {
62 if (mode == MATHTEXT_MODE) {
63 // we are inside some text mode thingy, so opening new math is allowed
64 Token const & n = p.get_token();
65 if (n.cat() == catMath) {
66 // TeX's $$...$$ syntax for displayed math
68 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
70 p.get_token(); // skip the second '$' token
75 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
80 else if (flags & FLAG_SIMPLE) {
81 // this is the end of the formula
86 cerr << "\nmode: " << mode << endl;
87 p.error("something strange in the parser\n");
92 else if (t.cat() == catLetter ||
93 t.cat() == catSuper ||
95 t.cat() == catOther ||
96 t.cat() == catAlign ||
97 t.cat() == catActive ||
98 t.cat() == catParameter)
101 else if (t.cat() == catBegin) {
103 parse_math(p, os, FLAG_BRACE_LAST, mode);
107 else if (t.cat() == catEnd) {
108 if (flags & FLAG_BRACE_LAST)
110 os << "unexpected '}' in math\n";
113 else if (t.cat() == catComment) {
115 cerr << "Ignoring comment: " << t.asInput();
125 else if (t.cs() == "(") {
127 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
131 else if (t.cs() == "[") {
132 // special handling of a few common SW user quirks
134 //if (p.next_token().cs() ==
136 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
140 else if (t.cs() == "protect")
141 // ignore \\protect, will hopefully be re-added during output
144 else if (t.cs() == "begin") {
145 string const name = p.getArg('{', '}');
146 active_environments.push_back(name);
147 os << "\\begin{" << name << "}";
148 if (name == "tabular")
149 parse_math(p, os, FLAG_END, MATHTEXT_MODE);
151 parse_math(p, os, FLAG_END, mode);
152 os << "\\end{" << name << "}";
153 active_environments.pop_back();
156 else if (t.cs() == "end") {
157 if (flags & FLAG_END) {
158 // eat environment name
159 string const name = p.getArg('{', '}');
160 if (name != active_environment())
161 p.error("\\end{" + name + "} does not match \\begin{"
162 + active_environment() + "}");
165 p.error("found 'end' unexpectedly");
168 else if (t.cs() == ")") {
169 if (flags & FLAG_SIMPLE2)
171 p.error("found '\\)' unexpectedly");
174 else if (t.cs() == "]") {
175 if (flags & FLAG_EQUATION)
177 p.error("found '\\]' unexpectedly");
180 else if (t.cs() == "textrm" || t.cs() == "textsf" || t.cs() == "textbf"
181 || t.cs() == "texttt" || t.cs() == "textsc"
182 || t.cs() == "text" || t.cs() == "intertext") {
183 os << '\\' << t.cs() << '{';
184 parse_math(p, os, FLAG_ITEM, MATHTEXT_MODE);
188 else if (t.cs() == "mbox" || t.cs() == "fbox") {
189 os << "\\" << t.cs() << '{';
190 parse_math(p, os, FLAG_ITEM, MATHTEXT_MODE);
194 else if (t.cs() == "\"") {
195 string const name = p.verbatim_item();
196 if (name == "a") os << 'ä';
197 else if (name == "o") os << 'ö';
198 else if (name == "u") os << 'ü';
199 else if (name == "A") os << 'Ä';
200 else if (name == "O") os << 'Ö';
201 else if (name == "U") os << 'Ü';
202 else os << "\"{" << name << "}";
205 else if (t.cs() == "ss")
208 else if (t.cs() == "cr") {
209 // lyx can't handle \\cr
210 cerr << "Warning: Converting TeX '\\cr' to LaTeX '\\\\'."
218 if (flags & FLAG_LEAVE) {
219 flags &= ~FLAG_LEAVE;