]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/math.C
b02a58c88a7dee0a8dc1488176d0a47741d1faa1
[lyx.git] / src / tex2lyx / math.C
1 /** The .tex to .lyx converter
2     \author André Pönitz (2003)
3  */
4
5 // {[(
6
7 #include <config.h>
8
9 #include "Lsstream.h"
10 #include "tex2lyx.h"
11
12 #include <iostream>
13 #include <string>
14 #include <vector>
15
16 using std::cerr;
17 using std::endl;
18 using std::ostream;
19 using std::string;
20 using std::vector;
21
22
23 bool is_math_env(string const & name)
24 {
25         static char const * known_math_envs[] = { "equation", "equation*",
26         "eqnarray", "eqnarray*", "align", "align*", 0};
27
28         for (char const ** what = known_math_envs; *what; ++what)
29                 if (*what == name)
30                         return true;
31         return false;
32 }
33
34
35 void parse_math(Parser & p, ostream & os, unsigned flags, const mode_type mode)
36 {
37         while (p.good()) {
38                 Token const & t = p.getToken();
39
40 #ifdef FILEDEBUG
41                 cerr << "t: " << t << " flags: " << flags << "\n";
42 #endif
43
44                 if (flags & FLAG_ITEM) {
45                         if (t.cat() == catSpace)
46                                 continue;
47
48                         flags &= ~FLAG_ITEM;
49                         if (t.cat() == catBegin) {
50                                 // skip the brace and collect everything to the next matching
51                                 // closing brace
52                                 flags |= FLAG_BRACE_LAST;
53                                 continue;
54                         }
55
56                         // handle only this single token, leave the loop if done
57                         flags |= FLAG_LEAVE;
58                 }
59
60
61                 //
62                 // cat codes
63                 //
64                 if (t.cat() == catMath) {
65                         if (mode == MATHTEXT_MODE) {
66                                 // we are inside some text mode thingy, so opening new math is allowed
67                                 Token const & n = p.getToken();
68                                 if (n.cat() == catMath) {
69                                         // TeX's $$...$$ syntax for displayed math
70                                         os << "\\[";
71                                         parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
72                                         os << "\\]";
73                                         p.getToken(); // skip the second '$' token
74                                 } else {
75                                         // simple $...$  stuff
76                                         p.putback();
77                                         os << '$';
78                                         parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
79                                         os << '$';
80                                 }
81                         }
82
83                         else if (flags & FLAG_SIMPLE) {
84                                 // this is the end of the formula
85                                 return;
86                         }
87
88                         else {
89                                 cerr << "\nmode: " << mode << endl;
90                                 p.error("something strange in the parser\n");
91                                 break;
92                         }
93                 }
94
95                 else if (t.cat() == catLetter ||
96                                t.cat() == catSpace ||
97                                t.cat() == catSuper ||
98                                t.cat() == catSub ||
99                                t.cat() == catOther ||
100                                t.cat() == catAlign ||
101                                t.cat() == catActive ||
102                                t.cat() == catParameter)
103                         os << t.character();
104
105                 else if (t.cat() == catNewline) {
106                         //if (p.nextToken().cat() == catNewline) {
107                         //      p.getToken();
108                         //      handle_par(os);
109                         //} else {
110                                 os << "\n "; // note the space
111                         //}
112                 }
113
114                 else if (t.cat() == catBegin) {
115                         os << '{';
116                         parse_math(p, os, FLAG_BRACE_LAST, mode);
117                         os << '}';
118                 }
119
120                 else if (t.cat() == catEnd) {
121                         if (flags & FLAG_BRACE_LAST)
122                                 return;
123                         os << "unexpected '}' in math\n";
124                 }
125
126                 else if (t.cat() == catComment)
127                         handle_comment(p);
128
129                 //
130                 // control sequences
131                 //
132
133                 else if (t.cs() == "(") {
134                         os << "\\(";
135                         parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
136                         os << "\\)";
137                 }
138
139                 else if (t.cs() == "[") {
140                         os << "\\[";
141                         parse_math(p, os, FLAG_EQUATION, MATH_MODE);
142                         os << "\\]";
143                 }
144
145                 else if (t.cs() == "protect")
146                         // ignore \\protect, will hopefully be re-added during output
147                         ;
148
149                 else if (t.cs() == "begin") {
150                         string const name = p.getArg('{', '}');
151                         active_environments_push(name);
152                         os << "\\begin{" << name << "}";
153                         if (name == "tabular")
154                                 parse_math(p, os, FLAG_END, MATHTEXT_MODE);
155                         else
156                                 parse_math(p, os, FLAG_END, mode);
157                         os << "\\end{" << name << "}";
158                 }
159
160                 else if (t.cs() == "end") {
161                         if (flags & FLAG_END) {
162                                 // eat environment name
163                                 string const name = p.getArg('{', '}');
164                                 if (name != curr_env())
165                                         p.error("\\end{" + name + "} does not match \\begin{"
166                                                 + curr_env() + "}");
167                                 active_environments_pop();
168                                 return;
169                         }
170                         p.error("found 'end' unexpectedly");
171                 }
172
173                 else if (t.cs() == ")") {
174                         if (flags & FLAG_SIMPLE2)
175                                 return;
176                         p.error("found '\\)' unexpectedly");
177                 }
178
179                 else if (t.cs() == "]") {
180                         if (flags & FLAG_EQUATION)
181                                 return;
182                         p.error("found '\\]' unexpectedly");
183                 }
184
185                 else if (t.cs() == "textrm" || t.cs() == "textsf" || t.cs() == "textbf"
186                                 || t.cs() == "texttt" || t.cs() == "textsc") {
187                         os << '\\' << t.cs() << '{';
188                         parse_math(p, os, FLAG_ITEM, MATHTEXT_MODE);
189                         os << '}';
190                 }
191
192                 else if (t.cs() == "mbox") {
193                         os << "\\mbox{";
194                         parse_math(p, os, FLAG_ITEM, MATHTEXT_MODE);
195                         os << '}';
196                 }
197
198                 else if (t.cs() == "\"") {
199                         string const name = p.verbatimItem();
200                              if (name == "a") os << 'ä';
201                         else if (name == "o") os << 'ö';
202                         else if (name == "u") os << 'ü';
203                         else if (name == "A") os << 'Ä';
204                         else if (name == "O") os << 'Ö';
205                         else if (name == "U") os << 'Ü';
206                         else os << "\"{" << name << "}";
207                 }
208
209                 else if (t.cs() == "ss")
210                         os << "ß";
211
212                 else 
213                         os << t.asInput();
214
215                 if (flags & FLAG_LEAVE) {
216                         flags &= ~FLAG_LEAVE;
217                         break;
218                 }
219         }
220 }
221
222
223
224
225 // }])