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