]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/preamble.cpp
textsubscript and textsuperscript for tex2lyx
[lyx.git] / src / tex2lyx / preamble.cpp
1 /**
2  * \file preamble.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  * \author Uwe Stöhr
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 // {[(
13
14 #include <config.h>
15
16 #include "tex2lyx.h"
17
18 #include "LayoutFile.h"
19 #include "Layout.h"
20 #include "Lexer.h"
21 #include "TextClass.h"
22
23 #include "support/convert.h"
24 #include "support/FileName.h"
25 #include "support/filetools.h"
26 #include "support/lstrings.h"
27
28 #include "support/regex.h"
29
30 #include <algorithm>
31 #include <iostream>
32 #include <sstream>
33 #include <string>
34 #include <vector>
35 #include <map>
36
37 using namespace std;
38 using namespace lyx::support;
39
40
41 namespace lyx {
42
43 // special columntypes
44 extern map<char, int> special_columns;
45
46 map<string, vector<string> > used_packages;
47
48 // needed to handle encodings with babel
49 bool one_language = true;
50
51 namespace {
52
53 //add these to known_languages when updating to lyxformat 268:
54 //"chinese-simplified", "chinese-traditional", "japanese", "korean"
55 const char * const known_languages[] = { "afrikaans", "american", "arabic",
56 "austrian", "bahasa", "basque", "belarusian", "brazil", "brazilian", "breton",
57 "british", "bulgarian", "canadian", "canadien", "catalan", "croatian", "czech",
58 "danish", "dutch", "english", "esperanto", "estonian", "finnish", "francais",
59 "french", "frenchb", "frenchle", "frenchpro", "galician", "german", "germanb",
60 "greek", "hebrew", "icelandic", "irish", "italian", "kazakh", "lsorbian", "magyar",
61 "naustrian", "ngerman", "ngermanb", "norsk", "nynorsk", "polish", "portuges",
62 "portuguese", "romanian", "russian", "russianb", "scottish", "serbian", "slovak",
63 "slovene", "spanish", "swedish", "thai", "turkish", "ukraineb", "ukrainian",
64 "usorbian", "welsh", 0};
65
66 //add this when updating to lyxformat 305:
67 //bahasai, indonesian, and indon = equal to bahasa
68 //malay and meyalu = equal to bahasam
69 const char * const known_brazilian_languages[] = {"brazil", "brazilian", 0};
70 const char * const known_french_languages[] = {"french", "frenchb", "francais",
71                                                 "frenchle", "frenchpro", 0};
72 const char * const known_german_languages[] = {"german", "germanb", 0};
73 const char * const known_ngerman_languages[] = {"ngerman", "ngermanb", 0};
74 const char * const known_portuguese_languages[] = {"portuges", "portuguese", 0};
75 const char * const known_russian_languages[] = {"russian", "russianb", 0};
76 const char * const known_ukrainian_languages[] = {"ukrainian", "ukraineb", 0};
77
78 //add these to known_english_quotes_languages when updating to lyxformat 268:
79 //"chinese-simplified", "korean"
80 const char * const known_english_quotes_languages[] = {"american", "canadian",
81 "english", "esperanto", "hebrew", "irish", "scottish", "thai", 0};
82
83 //add this to known_french_quotes_languages when updating to lyxformat 327:
84 //"spanish-mexico"
85 const char * const known_french_quotes_languages[] = {"albanian", "arabic",
86 "basque", "canadien", "catalan", "galician", "greek", "italian", "norsk",
87 "nynorsk", "spanish", "turkish", 0};
88
89 const char * const known_german_quotes_languages[] = {"austrian", "bulgarian",
90 "czech", "icelandic", "lithuanian", "lsorbian", "naustrian", "serbian",
91 "serbian-latin", "slovak", "slovene", "usorbian",  0};
92
93 const char * const known_polish_quotes_languages[] = {"afrikaans", "croatian",
94 "dutch", "estonian", "magyar", "polish", "romanian", 0};
95
96 const char * const known_swedish_quotes_languages[] = {"bahasa", "finnish", 
97 "swedish", 0};
98
99 char const * const known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
100
101 const char * const known_roman_fonts[] = { "ae", "bookman", "charter",
102 "cmr", "fourier", "lmodern", "mathpazo", "mathptmx", "newcent", 0};
103
104 const char * const known_sans_fonts[] = { "avant", "berasans", "cmbr", "cmss",
105 "helvet", "lmss", 0};
106
107 const char * const known_typewriter_fonts[] = { "beramono", "cmtl", "cmtt",
108 "courier", "lmtt", "luximono", "fourier", "lmodern", "mathpazo", "mathptmx",
109 "newcent", 0};
110
111 const char * const known_paper_sizes[] = { "a3paper", "b3paper", "a4paper",
112 "b4paper", "a5paper", "b5paper", "executivepaper", "legalpaper",
113 "letterpaper", 0};
114
115 const char * const known_class_paper_sizes[] = { "a4paper", "a5paper",
116 "executivepaper", "legalpaper", "letterpaper", 0};
117
118 const char * const known_paper_margins[] = { "lmargin", "tmargin", "rmargin", 
119 "bmargin", "headheight", "headsep", "footskip", "columnsep", 0};
120
121 const char * const known_coded_paper_margins[] = { "leftmargin", "topmargin",
122 "rightmargin", "bottommargin", "headheight", "headsep", "footskip",
123 "columnsep", 0};
124
125 // default settings
126 ostringstream h_preamble;
127 string h_textclass               = "article";
128 string h_options                 = string();
129 string h_language                = "english";
130 string h_inputencoding           = "auto";
131 string h_font_roman              = "default";
132 string h_font_sans               = "default";
133 string h_font_typewriter         = "default";
134 string h_font_default_family     = "default";
135 string h_font_sc                 = "false";
136 string h_font_osf                = "false";
137 string h_font_sf_scale           = "100";
138 string h_font_tt_scale           = "100";
139 string h_graphics                = "default";
140 string h_paperfontsize           = "default";
141 string h_spacing                 = "single";
142 string h_papersize               = "default";
143 string h_use_geometry            = "false";
144 string h_use_amsmath             = "1";
145 string h_use_esint               = "1";
146 string h_cite_engine             = "basic";
147 string h_use_bibtopic            = "false";
148 string h_paperorientation        = "portrait";
149 string h_secnumdepth             = "3";
150 string h_tocdepth                = "3";
151 string h_paragraph_separation    = "indent";
152 string h_defskip                 = "medskip";
153 string h_quotes_language         = "english";
154 string h_papercolumns            = "1";
155 string h_papersides              = string();
156 string h_paperpagestyle          = "default";
157 string h_tracking_changes        = "false";
158 string h_output_changes          = "false";
159 string h_margins                 = "";
160
161 // returns true if at least one of the options in what has been found
162 bool handle_opt(vector<string> & opts, char const * const * what, string & target)
163 {
164         if (opts.empty())
165                 return false;
166
167         bool found = false;
168         // the last language option is the document language (for babel and LyX)
169         // the last size option is the document font size
170         vector<string>::iterator it;
171         vector<string>::iterator position = opts.begin();
172         for (; *what; ++what) {
173                 it = find(opts.begin(), opts.end(), *what);
174                 if (it != opts.end()) {
175                         if (it >= position) {
176                                 found = true;
177                                 target = *what;
178                                 position = it;
179                         }
180                 }
181         }
182         return found;
183 }
184
185
186 void delete_opt(vector<string> & opts, char const * const * what)
187 {
188         if (opts.empty())
189                 return;
190
191         // remove found options from the list
192         // do this after handle_opt to avoid potential memory leaks
193         vector<string>::iterator it;
194         for (; *what; ++what) {
195                 it = find(opts.begin(), opts.end(), *what);
196                 if (it != opts.end())
197                         opts.erase(it);
198         }
199 }
200
201
202 /*!
203  * Split a package options string (keyval format) into a vector.
204  * Example input:
205  *   authorformat=smallcaps,
206  *   commabeforerest,
207  *   titleformat=colonsep,
208  *   bibformat={tabular,ibidem,numbered}
209  */
210 vector<string> split_options(string const & input)
211 {
212         vector<string> options;
213         string option;
214         Parser p(input);
215         while (p.good()) {
216                 Token const & t = p.get_token();
217                 if (t.asInput() == ",") {
218                         options.push_back(trim(option));
219                         option.erase();
220                 } else if (t.asInput() == "=") {
221                         option += '=';
222                         p.skip_spaces(true);
223                         if (p.next_token().asInput() == "{")
224                                 option += '{' + p.getArg('{', '}') + '}';
225                 } else if (t.cat() != catSpace)
226                         option += t.asInput();
227         }
228
229         if (!option.empty())
230                 options.push_back(trim(option));
231
232         return options;
233 }
234
235
236 /*!
237  * Add package \p name with options \p options to used_packages.
238  * Remove options from \p options that we don't want to output.
239  */
240 void add_package(string const & name, vector<string> & options)
241 {
242         // every package inherits the global options
243         if (used_packages.find(name) == used_packages.end())
244                 used_packages[name] = split_options(h_options);
245
246         vector<string> & v = used_packages[name];
247         v.insert(v.end(), options.begin(), options.end());
248         if (name == "jurabib") {
249                 // Don't output the order argument (see the cite command
250                 // handling code in text.cpp).
251                 vector<string>::iterator end =
252                         remove(options.begin(), options.end(), "natbiborder");
253                 end = remove(options.begin(), end, "jurabiborder");
254                 options.erase(end, options.end());
255         }
256 }
257
258
259 // Given is a string like "scaled=0.9", return 0.9 * 100
260 string const scale_as_percentage(string const & scale)
261 {
262         string::size_type pos = scale.find('=');
263         if (pos != string::npos) {
264                 string value = scale.substr(pos + 1);
265                 if (isStrDbl(value))
266                         return convert<string>(100 * convert<double>(value));
267         }
268         // If the input string didn't match our expectations.
269         // return the default value "100"
270         return "100";
271 }
272
273
274 void handle_package(Parser &p, string const & name, string const & opts,
275                     bool in_lyx_preamble)
276 {
277         vector<string> options = split_options(opts);
278         add_package(name, options);
279         string scale;
280
281         // roman fonts
282         if (is_known(name, known_roman_fonts)) {
283                 h_font_roman = name;
284                 p.skip_spaces();
285         }
286
287         if (name == "fourier") {
288                 h_font_roman = "utopia";
289                 // when font uses real small capitals
290                 if (opts == "expert")
291                         h_font_sc = "true";
292         }
293
294         if (name == "mathpazo")
295                 h_font_roman = "palatino";
296
297         if (name == "mathptmx")
298                 h_font_roman = "times";
299
300         // sansserif fonts
301         if (is_known(name, known_sans_fonts)) {
302                 h_font_sans = name;
303                 if (!opts.empty()) {
304                         scale = opts;
305                         h_font_sf_scale = scale_as_percentage(scale);
306                 }
307         }
308
309         // typewriter fonts
310         if (is_known(name, known_typewriter_fonts)) {
311                 h_font_typewriter = name;
312                 if (!opts.empty()) {
313                         scale = opts;
314                         h_font_tt_scale = scale_as_percentage(scale);
315                 }
316         }
317
318         // font uses old-style figure
319         if (name == "eco")
320                 h_font_osf = "true";
321
322         else if (name == "amsmath" || name == "amssymb")
323                 h_use_amsmath = "2";
324
325         else if (name == "esint")
326                 h_use_esint = "2";
327
328         else if (name == "babel" && !opts.empty()) {
329                 // check if more than one option was used - used later for inputenc
330                 // in case inputenc is parsed before babel, set the encoding to auto
331                 if (options.begin() != options.end() - 1) {
332                         one_language = false;
333                         h_inputencoding = "auto";
334                 }
335                 // babel takes the last language of the option of its \usepackage
336                 // call as document language. If there is no such language option, the
337                 // last language in the documentclass options is used.
338                 handle_opt(options, known_languages, h_language);
339                 delete_opt(options, known_languages);
340         }
341
342         else if (name == "fontenc")
343                  ;// ignore this
344
345         else if (name == "inputenc") {
346                 // h_inputencoding is only set when there is not more than one
347                 // inputenc option because otherwise h_inputencoding must be
348                 // set to "auto" (the default encoding of the document language)
349                 // Therefore check for the "," character.
350                 // It is also only set when there is not more then one babel
351                 // language option but this is handled in the routine for babel.
352                 if (opts.find(",") == string::npos && one_language == true)
353                         h_inputencoding = opts;
354                 if (!options.empty())
355                         p.setEncoding(options.back());
356                 options.clear();
357         }
358
359         else if (name == "makeidx")
360                 ; // ignore this
361
362         else if (name == "prettyref")
363                 ; // ignore this
364
365         else if (name == "varioref")
366                 ; // ignore this
367
368         else if (name == "verbatim")
369                 ; // ignore this
370
371         else if (name == "nomencl")
372                 ; // ignore this
373
374         else if (name == "textcomp")
375                 ; // ignore this
376
377         else if (name == "url")
378                 ; // ignore this
379
380         else if (LYX_FORMAT >= 408 && name == "subscript")
381                 ; // ignore this
382
383         else if (name == "color") {
384                 // with the following command this package is only loaded when needed for
385                 // undefined colors, since we only support the predefined colors
386                 h_preamble << "\\@ifundefined{definecolor}\n {\\usepackage{color}}{}\n";
387         }
388
389         else if (name == "graphicx")
390                 ; // ignore this
391
392         else if (name == "setspace")
393                 ; // ignore this
394
395         else if (name == "geometry")
396                 ; // Ignore this, the geometry settings are made by the \geometry
397                   // command. This command is handled below.
398
399         else if (is_known(name, known_languages))
400                 h_language = name;
401
402         else if (name == "natbib") {
403                 h_cite_engine = "natbib_authoryear";
404                 vector<string>::iterator it =
405                         find(options.begin(), options.end(), "authoryear");
406                 if (it != options.end())
407                         options.erase(it);
408                 else {
409                         it = find(options.begin(), options.end(), "numbers");
410                         if (it != options.end()) {
411                                 h_cite_engine = "natbib_numerical";
412                                 options.erase(it);
413                         }
414                 }
415         }
416
417         else if (name == "jurabib")
418                 h_cite_engine = "jurabib";
419
420         else if (!in_lyx_preamble) {
421                 if (options.empty())
422                         h_preamble << "\\usepackage{" << name << "}";
423                 else {
424                         h_preamble << "\\usepackage[" << opts << "]{" 
425                                    << name << "}";
426                         options.clear();
427                 }
428         }
429
430         // We need to do something with the options...
431         if (!options.empty())
432                 cerr << "Ignoring options '" << join(options, ",")
433                      << "' of package " << name << '.' << endl;
434
435         // remove the whitespace
436         p.skip_spaces();
437 }
438
439
440
441 void end_preamble(ostream & os, TextClass const & /*textclass*/)
442 {
443         // merge synonym languages
444         if (is_known(h_language, known_brazilian_languages))
445                 h_language = "brazilian";
446         else if (is_known(h_language, known_french_languages))
447                 h_language = "french";
448         else if (is_known(h_language, known_german_languages))
449                 h_language = "german";
450         else if (is_known(h_language, known_ngerman_languages))
451                 h_language = "ngerman";
452         else if (is_known(h_language, known_portuguese_languages))
453                 h_language = "portuguese";
454         else if (is_known(h_language, known_russian_languages))
455                 h_language = "russian";
456         else if (is_known(h_language, known_ukrainian_languages))
457                 h_language = "ukrainian";
458
459         // set the quote language
460         // LyX only knows the following quotes languages:
461         // english, swedish, german, polish, french and danish
462         // (quotes for "japanese" and "chinese-traditional" are missing because
463         //  they wouldn't be useful: http://www.lyx.org/trac/ticket/6383)
464         // conversion list taken from
465         // http://en.wikipedia.org/wiki/Quotation_mark,_non-English_usage
466         // (quotes for kazakh and interlingua are unknown)
467         // danish
468         if (h_language == "danish")
469                 h_quotes_language = "danish";
470         // french
471         else if (is_known(h_language, known_french_quotes_languages)
472                 || is_known(h_language, known_french_languages)
473                 || is_known(h_language, known_russian_languages)
474                 || is_known(h_language, known_ukrainian_languages))
475                 h_quotes_language = "french";
476         // german
477         else if (is_known(h_language, known_german_quotes_languages)
478                 || is_known(h_language, known_german_languages)
479                 || is_known(h_language, known_ngerman_languages))
480                 h_quotes_language = "german";
481         // polish
482         else if (is_known(h_language, known_polish_quotes_languages))
483                 h_quotes_language = "polish";
484         // swedish
485         else if (is_known(h_language, known_swedish_quotes_languages))
486                 h_quotes_language = "swedish";
487         //english
488         else if (is_known(h_language, known_english_quotes_languages)
489                 || is_known(h_language, known_brazilian_languages)
490                 || is_known(h_language, known_portuguese_languages))
491                 h_quotes_language = "english";
492
493         // output the LyX file settings
494         os << "#LyX file created by tex2lyx " << PACKAGE_VERSION << "\n"
495            << "\\lyxformat " << LYX_FORMAT << '\n'
496            << "\\begin_document\n"
497            << "\\begin_header\n"
498            << "\\textclass " << h_textclass << "\n";
499         if (!h_preamble.str().empty())
500                 os << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n";
501         if (!h_options.empty())
502                 os << "\\options " << h_options << "\n";
503         os << "\\language " << h_language << "\n"
504            << "\\inputencoding " << h_inputencoding << "\n"
505            << "\\font_roman " << h_font_roman << "\n"
506            << "\\font_sans " << h_font_sans << "\n"
507            << "\\font_typewriter " << h_font_typewriter << "\n"
508            << "\\font_default_family " << h_font_default_family << "\n"
509            << "\\font_sc " << h_font_sc << "\n"
510            << "\\font_osf " << h_font_osf << "\n"
511            << "\\font_sf_scale " << h_font_sf_scale << "\n"
512            << "\\font_tt_scale " << h_font_tt_scale << "\n"
513            << "\\graphics " << h_graphics << "\n"
514            << "\\paperfontsize " << h_paperfontsize << "\n"
515            << "\\spacing " << h_spacing << "\n"
516            << "\\papersize " << h_papersize << "\n"
517            << "\\use_geometry " << h_use_geometry << "\n"
518            << "\\use_amsmath " << h_use_amsmath << "\n"
519            << "\\use_esint " << h_use_esint << "\n"
520            << "\\cite_engine " << h_cite_engine << "\n"
521            << "\\use_bibtopic " << h_use_bibtopic << "\n"
522            << "\\paperorientation " << h_paperorientation << "\n"
523            << h_margins
524            << "\\secnumdepth " << h_secnumdepth << "\n"
525            << "\\tocdepth " << h_tocdepth << "\n"
526            << "\\paragraph_separation " << h_paragraph_separation << "\n"
527            << "\\defskip " << h_defskip << "\n"
528            << "\\quotes_language " << h_quotes_language << "\n"
529            << "\\papercolumns " << h_papercolumns << "\n"
530            << "\\papersides " << h_papersides << "\n"
531            << "\\paperpagestyle " << h_paperpagestyle << "\n"
532            << "\\tracking_changes " << h_tracking_changes << "\n"
533            << "\\output_changes " << h_output_changes << "\n"
534            << "\\end_header\n\n"
535            << "\\begin_body\n";
536         // clear preamble for subdocuments
537         h_preamble.str("");
538 }
539
540 } // anonymous namespace
541
542 void parse_preamble(Parser & p, ostream & os, 
543         string const & forceclass, TeX2LyXDocClass & tc)
544 {
545         // initialize fixed types
546         special_columns['D'] = 3;
547         bool is_full_document = false;
548         bool is_lyx_file = false;
549         bool in_lyx_preamble = false;
550
551         // determine whether this is a full document or a fragment for inclusion
552         while (p.good()) {
553                 Token const & t = p.get_token();
554
555                 if (t.cat() == catEscape && t.cs() == "documentclass") {
556                         is_full_document = true;
557                         break;
558                 }
559         }
560         p.reset();
561
562         while (is_full_document && p.good()) {
563                 Token const & t = p.get_token();
564
565 #ifdef FILEDEBUG
566                 cerr << "t: " << t << "\n";
567 #endif
568
569                 //
570                 // cat codes
571                 //
572                 if (!in_lyx_preamble &&
573                     (t.cat() == catLetter ||
574                      t.cat() == catSuper ||
575                      t.cat() == catSub ||
576                      t.cat() == catOther ||
577                      t.cat() == catMath ||
578                      t.cat() == catActive ||
579                      t.cat() == catBegin ||
580                      t.cat() == catEnd ||
581                      t.cat() == catAlign ||
582                      t.cat() == catParameter))
583                         h_preamble << t.cs();
584
585                 else if (!in_lyx_preamble && 
586                          (t.cat() == catSpace || t.cat() == catNewline))
587                         h_preamble << t.asInput();
588
589                 else if (t.cat() == catComment) {
590                         // regex to parse comments (currently not used)
591                         static regex const islyxfile("%% LyX .* created this file");
592                         static regex const usercommands("User specified LaTeX commands");
593
594                         string const comment = t.asInput();
595
596                         // magically switch encoding default if it looks like XeLaTeX
597                         static string const magicXeLaTeX =
598                                 "% This document must be compiled with XeLaTeX ";
599                         if (comment.size() > magicXeLaTeX.size() 
600                                   && comment.substr(0, magicXeLaTeX.size()) == magicXeLaTeX
601                                   && h_inputencoding == "auto") {
602                                 cerr << "XeLaTeX comment found, switching to UTF8\n";
603                                 h_inputencoding = "utf8";
604                         }
605                         smatch sub;
606                         if (regex_search(comment, sub, islyxfile)) {
607                                 is_lyx_file = true;
608                                 in_lyx_preamble = true;
609                         } else if (is_lyx_file
610                                    && regex_search(comment, sub, usercommands))
611                                 in_lyx_preamble = false;
612                         else if (!in_lyx_preamble)
613                                 h_preamble << t.asInput();
614                 }
615
616                 else if (t.cs() == "pagestyle")
617                         h_paperpagestyle = p.verbatim_item();
618
619                 else if (t.cs() == "makeatletter") {
620                         // LyX takes care of this
621                         p.setCatCode('@', catLetter);
622                 }
623
624                 else if (t.cs() == "makeatother") {
625                         // LyX takes care of this
626                         p.setCatCode('@', catOther);
627                 }
628
629                 else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
630                             || t.cs() == "providecommand"
631                                 || t.cs() == "DeclareRobustCommand"
632                                 || t.cs() == "ProvideTextCommandDefault"
633                                 || t.cs() == "DeclareMathAccent") {
634                         bool star = false;
635                         if (p.next_token().character() == '*') {
636                                 p.get_token();
637                                 star = true;
638                         }
639                         string const name = p.verbatim_item();
640                         string const opt1 = p.getOpt();
641                         string const opt2 = p.getFullOpt();
642                         string const body = p.verbatim_item();
643                         // font settings
644                         if (name == "\\rmdefault")
645                                 if (is_known(body, known_roman_fonts))
646                                         h_font_roman = body;
647                         if (name == "\\sfdefault")
648                                 if (is_known(body, known_sans_fonts))
649                                         h_font_sans = body;
650                         if (name == "\\ttdefault")
651                                 if (is_known(body, known_typewriter_fonts))
652                                         h_font_typewriter = body;
653                         if (name == "\\familydefault") {
654                                 string family = body;
655                                 // remove leading "\"
656                                 h_font_default_family = family.erase(0,1);
657                         }
658                         // only non-lyxspecific stuff
659                         if (!in_lyx_preamble) {
660                                 ostringstream ss;
661                                 ss << '\\' << t.cs();
662                                 if (star)
663                                         ss << '*';
664                                 ss << '{' << name << '}' << opt1 << opt2
665                                    << '{' << body << "}";
666                                 h_preamble << ss.str();
667
668                                 // Add the command to the known commands
669                                 add_known_command(name, opt1, !opt2.empty());
670 /*
671                                 ostream & out = in_preamble ? h_preamble : os;
672                                 out << "\\" << t.cs() << "{" << name << "}"
673                                     << opts << "{" << body << "}";
674 */
675                         }
676                 }
677
678                 else if (t.cs() == "documentclass") {
679                         vector<string>::iterator it;
680                         vector<string> opts = split_options(p.getArg('[', ']'));
681                         handle_opt(opts, known_fontsizes, h_paperfontsize);
682                         delete_opt(opts, known_fontsizes);
683                         // delete "pt" at the end
684                         string::size_type i = h_paperfontsize.find("pt");
685                         if (i != string::npos)
686                                 h_paperfontsize.erase(i);
687                         // The documentclass options are always parsed before the options
688                         // of the babel call so that a language cannot overwrite the babel
689                         // options.
690                         handle_opt(opts, known_languages, h_language);
691                         delete_opt(opts, known_languages);
692                         
693                         // paper orientation
694                         if ((it = find(opts.begin(), opts.end(), "landscape")) != opts.end()) {
695                                 h_paperorientation = "landscape";
696                                 opts.erase(it);
697                         }
698                         // paper sides
699                         if ((it = find(opts.begin(), opts.end(), "oneside"))
700                                  != opts.end()) {
701                                 h_papersides = "1";
702                                 opts.erase(it);
703                         }
704                         if ((it = find(opts.begin(), opts.end(), "twoside"))
705                                  != opts.end()) {
706                                 h_papersides = "2";
707                                 opts.erase(it);
708                         }
709                         // paper columns
710                         if ((it = find(opts.begin(), opts.end(), "onecolumn"))
711                                  != opts.end()) {
712                                 h_papercolumns = "1";
713                                 opts.erase(it);
714                         }
715                         if ((it = find(opts.begin(), opts.end(), "twocolumn"))
716                                  != opts.end()) {
717                                 h_papercolumns = "2";
718                                 opts.erase(it);
719                         }
720                         // paper sizes
721                         // some size options are know to any document classes, other sizes
722                         // are handled by the \geometry command of the geometry package
723                         handle_opt(opts, known_class_paper_sizes, h_papersize);
724                         delete_opt(opts, known_class_paper_sizes);
725                         // the remaining options
726                         h_options = join(opts, ",");
727                         h_textclass = p.getArg('{', '}');
728                 }
729
730                 else if (t.cs() == "usepackage") {
731                         string const options = p.getArg('[', ']');
732                         string const name = p.getArg('{', '}');
733                         vector<string> vecnames;
734                         split(name, vecnames, ',');
735                         vector<string>::const_iterator it  = vecnames.begin();
736                         vector<string>::const_iterator end = vecnames.end();
737                         for (; it != end; ++it)
738                                 handle_package(p, trim(*it), options, 
739                                                in_lyx_preamble);
740                 }
741
742                 else if (t.cs() == "inputencoding") {
743                         string const encoding = p.getArg('{','}');
744                         h_inputencoding = encoding;
745                         p.setEncoding(encoding);
746                 }
747
748                 else if (t.cs() == "newenvironment") {
749                         string const name = p.getArg('{', '}');
750                         ostringstream ss;
751                         ss << "\\newenvironment{" << name << "}";
752                         ss << p.getOpt();
753                         ss << p.getOpt();
754                         ss << '{' << p.verbatim_item() << '}';
755                         ss << '{' << p.verbatim_item() << '}';
756                         if (!in_lyx_preamble)
757                                 h_preamble << ss.str();
758                 }
759
760                 else if (t.cs() == "def") {
761                         string name = p.get_token().cs();
762                         while (p.next_token().cat() != catBegin)
763                                 name += p.get_token().cs();
764                         if (!in_lyx_preamble)
765                                 h_preamble << "\\def\\" << name << '{'
766                                            << p.verbatim_item() << "}";
767                 }
768
769                 else if (t.cs() == "newcolumntype") {
770                         string const name = p.getArg('{', '}');
771                         trim(name);
772                         int nargs = 0;
773                         string opts = p.getOpt();
774                         if (!opts.empty()) {
775                                 istringstream is(string(opts, 1));
776                                 is >> nargs;
777                         }
778                         special_columns[name[0]] = nargs;
779                         h_preamble << "\\newcolumntype{" << name << "}";
780                         if (nargs)
781                                 h_preamble << "[" << nargs << "]";
782                         h_preamble << "{" << p.verbatim_item() << "}";
783                 }
784
785                 else if (t.cs() == "setcounter") {
786                         string const name = p.getArg('{', '}');
787                         string const content = p.getArg('{', '}');
788                         if (name == "secnumdepth")
789                                 h_secnumdepth = content;
790                         else if (name == "tocdepth")
791                                 h_tocdepth = content;
792                         else
793                                 h_preamble << "\\setcounter{" << name << "}{" << content << "}";
794                 }
795
796                 else if (t.cs() == "setlength") {
797                         string const name = p.verbatim_item();
798                         string const content = p.verbatim_item();
799                         // the paragraphs are only not indented when \parindent is set to zero
800                         if (name == "\\parindent" && content != "") {
801                                 if (content[0] == '0')
802                                         h_paragraph_separation = "skip";
803                         } else if (name == "\\parskip") {
804                                 if (content == "\\smallskipamount")
805                                         h_defskip = "smallskip";
806                                 else if (content == "\\medskipamount")
807                                         h_defskip = "medskip";
808                                 else if (content == "\\bigskipamount")
809                                         h_defskip = "bigskip";
810                                 else
811                                         h_defskip = content;
812                         } else
813                                 h_preamble << "\\setlength{" << name << "}{" << content << "}";
814                 }
815
816                 else if (t.cs() == "onehalfspacing")
817                         h_spacing = "onehalf";
818
819                 else if (t.cs() == "doublespacing")
820                         h_spacing = "double";
821
822                 else if (t.cs() == "setstretch")
823                         h_spacing = "other " + p.verbatim_item();
824
825                 else if (t.cs() == "begin") {
826                         string const name = p.getArg('{', '}');
827                         if (name == "document")
828                                 break;
829                         h_preamble << "\\begin{" << name << "}";
830                 }
831
832                 else if (t.cs() == "geometry") {
833                         h_use_geometry = "true";
834                         vector<string> opts = split_options(p.getArg('{', '}'));
835                         vector<string>::iterator it;
836                         // paper orientation
837                         if ((it = find(opts.begin(), opts.end(), "landscape")) != opts.end()) {
838                                 h_paperorientation = "landscape";
839                                 opts.erase(it);
840                         }
841                         // paper size
842                         handle_opt(opts, known_paper_sizes, h_papersize);
843                         delete_opt(opts, known_paper_sizes);
844                         // page margins
845                         char const * const * margin = known_paper_margins;
846                         int k = -1;
847                         for (; *margin; ++margin) {
848                                 k += 1;
849                                 // search for the "=" in e.g. "lmargin=2cm" to get the value
850                                 for(size_t i = 0; i != opts.size(); i++) {
851                                         if (opts.at(i).find(*margin) != string::npos) {
852                                                 string::size_type pos = opts.at(i).find("=");
853                                                 string value = opts.at(i).substr(pos + 1);
854                                                 string name = known_coded_paper_margins[k];
855                                                 h_margins += "\\" + name + " " + value + "\n";
856                                         }
857                                 }
858                         }
859                 }
860
861                 else if (t.cs() == "jurabibsetup") {
862                         vector<string> jurabibsetup =
863                                 split_options(p.getArg('{', '}'));
864                         // add jurabibsetup to the jurabib package options
865                         add_package("jurabib", jurabibsetup);
866                         if (!jurabibsetup.empty()) {
867                                 h_preamble << "\\jurabibsetup{"
868                                            << join(jurabibsetup, ",") << '}';
869                         }
870                 }
871
872                 else if (!t.cs().empty() && !in_lyx_preamble)
873                         h_preamble << '\\' << t.cs();
874         }
875
876         // remove the whitespace
877         p.skip_spaces();
878
879         // Force textclass if the user wanted it
880         if (!forceclass.empty())
881                 h_textclass = forceclass;
882         if (noweb_mode && !prefixIs(h_textclass, "literate-"))
883                 h_textclass.insert(0, "literate-");
884         FileName layoutfilename = libFileSearch("layouts", h_textclass, "layout");
885         if (layoutfilename.empty()) {
886                 cerr << "Error: Could not find layout file for textclass \"" << h_textclass << "\"." << endl;
887                 exit(1);
888         }
889         tc.read(layoutfilename);
890         if (h_papersides.empty()) {
891                 ostringstream ss;
892                 ss << tc.sides();
893                 h_papersides = ss.str();
894         }
895         end_preamble(os, tc);
896 }
897
898 // }])
899
900
901 } // namespace lyx