3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
18 #include "LayoutFile.h"
21 #include "TextClass.h"
23 #include "support/convert.h"
24 #include "support/FileName.h"
25 #include "support/filetools.h"
26 #include "support/lstrings.h"
28 #include "support/regex.h"
38 using namespace lyx::support;
43 // special columntypes
44 extern map<char, int> special_columns;
46 map<string, vector<string> > used_packages;
48 // needed to handle encodings with babel
49 bool one_language = true;
53 //add this to known_languages when updating to lyxformat 266:
55 //add these to known_languages when updating to lyxformat 268:
56 //"chinese-simplified", "chinese-traditional", "japanese", "korean"
57 // Both changes require first that support for non-babel languages (CJK,
59 // add polutonikogreek for lyxformat 338
60 // add mongolian for lyxformat 342
61 // add turkmen for lyxformat 383
62 const char * const known_languages[] = { "afrikaans", "albanian", "american",
63 "arabic", "arabtex", "austrian", "bahasa", "bahasai", "bahasam", "basque",
64 "belarusian", "brazil", "brazilian", "breton", "british", "bulgarian",
65 "canadian", "canadien", "catalan", "croatian", "czech", "danish", "dutch",
66 "english", "esperanto", "estonian", "finnish", "francais", "french",
67 "frenchb", "frenchle", "frenchpro", "galician", "german", "germanb", "greek",
68 "hebrew", "icelandic", "indon", "indonesian", "interlingua", "irish",
69 "italian", "kazakh", "latin", "latvian", "lithuanian", "lsorbian", "magyar",
70 "malay", "meyalu", "naustrian", "ngerman", "ngermanb", "norsk", "nynorsk",
71 "polish", "portuges", "portuguese", "romanian", "russian", "russianb",
72 "samin", "scottish", "serbian", "serbian-latin", "slovak", "slovene",
73 "spanish", "swedish", "thai", "turkish", "ukraineb", "ukrainian",
74 "usorbian", "vietnam", "welsh", 0};
76 const char * const known_bahasa_languages[] = {"bahasa", "bahasai",
77 "indon", "indonesian", 0};
78 const char * const known_bahasam_languages[] = {"bahasam", "malay",
80 const char * const known_brazilian_languages[] = {"brazil", "brazilian", 0};
81 const char * const known_french_languages[] = {"french", "frenchb", "francais",
82 "frenchle", "frenchpro", 0};
83 const char * const known_german_languages[] = {"german", "germanb", 0};
84 const char * const known_ngerman_languages[] = {"ngerman", "ngermanb", 0};
85 const char * const known_portuguese_languages[] = {"portuges", "portuguese", 0};
86 const char * const known_russian_languages[] = {"russian", "russianb", 0};
87 const char * const known_ukrainian_languages[] = {"ukrainian", "ukraineb", 0};
89 //add these to known_english_quotes_languages when updating to lyxformat 268:
90 //"chinese-simplified", "korean"
91 // This requires first that support for non-babel languages (CJK) is added.
92 const char * const known_english_quotes_languages[] = {"american", "canadian",
93 "english", "esperanto", "hebrew", "irish", "scottish", "thai", 0};
95 //add this to known_french_quotes_languages when updating to
96 // - lyxformat 327: "spanish-mexico"
97 // - lyxformat 338: "polutonikogreek"
98 // - lyxformat 383: "turkmen"
99 const char * const known_french_quotes_languages[] = {"albanian", "arabic",
100 "basque", "canadien", "catalan", "galician", "greek", "italian", "norsk",
101 "nynorsk", "spanish", "turkish", "vietnam", 0};
103 const char * const known_german_quotes_languages[] = {"austrian", "bulgarian",
104 "czech", "icelandic", "lithuanian", "lsorbian", "naustrian", "serbian",
105 "serbian-latin", "slovak", "slovene", "usorbian", 0};
107 const char * const known_polish_quotes_languages[] = {"afrikaans", "croatian",
108 "dutch", "estonian", "magyar", "polish", "romanian", 0};
110 const char * const known_swedish_quotes_languages[] = {"finnish",
113 char const * const known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
115 const char * const known_roman_fonts[] = { "ae", "bookman", "charter",
116 "cmr", "fourier", "lmodern", "mathpazo", "mathptmx", "newcent", 0};
118 const char * const known_sans_fonts[] = { "avant", "berasans", "cmbr", "cmss",
119 "helvet", "lmss", 0};
121 const char * const known_typewriter_fonts[] = { "beramono", "cmtl", "cmtt",
122 "courier", "lmtt", "luximono", "fourier", "lmodern", "mathpazo", "mathptmx",
125 const char * const known_paper_sizes[] = { "a3paper", "b3paper", "a4paper",
126 "b4paper", "a5paper", "b5paper", "executivepaper", "legalpaper",
129 const char * const known_class_paper_sizes[] = { "a4paper", "a5paper",
130 "executivepaper", "legalpaper", "letterpaper", 0};
132 const char * const known_paper_margins[] = { "lmargin", "tmargin", "rmargin",
133 "bmargin", "headheight", "headsep", "footskip", "columnsep", 0};
135 const char * const known_coded_paper_margins[] = { "leftmargin", "topmargin",
136 "rightmargin", "bottommargin", "headheight", "headsep", "footskip",
140 ostringstream h_preamble;
141 string h_textclass = "article";
142 string h_use_default_options = "false";
143 string h_options = string();
144 string h_language = "english";
145 string h_inputencoding = "auto";
146 string h_font_roman = "default";
147 string h_font_sans = "default";
148 string h_font_typewriter = "default";
149 string h_font_default_family = "default";
150 string h_font_sc = "false";
151 string h_font_osf = "false";
152 string h_font_sf_scale = "100";
153 string h_font_tt_scale = "100";
154 string h_graphics = "default";
155 string h_float_placement;
156 string h_paperfontsize = "default";
157 string h_spacing = "single";
158 string h_use_hyperref = "0";
161 string h_pdf_subject;
162 string h_pdf_keywords;
163 string h_pdf_bookmarks = "1";
164 string h_pdf_bookmarksnumbered = "0";
165 string h_pdf_bookmarksopen = "0";
166 string h_pdf_bookmarksopenlevel = "1";
167 string h_pdf_breaklinks = "0";
168 string h_pdf_pdfborder = "0";
169 string h_pdf_colorlinks = "0";
170 string h_pdf_backref = "0";
171 string h_pdf_pagebackref = "0";
172 string h_pdf_pdfusetitle = "1";
173 string h_pdf_pagemode;
174 string h_pdf_quoted_options;
175 string h_papersize = "default";
176 string h_use_geometry = "false";
177 string h_use_amsmath = "1";
178 string h_use_esint = "1";
179 string h_cite_engine = "basic";
180 string h_use_bibtopic = "false";
181 string h_paperorientation = "portrait";
182 string h_secnumdepth = "3";
183 string h_tocdepth = "3";
184 string h_paragraph_separation = "indent";
185 string h_defskip = "medskip";
186 string h_quotes_language = "english";
187 string h_papercolumns = "1";
188 string h_papersides = string();
189 string h_paperpagestyle = "default";
190 string h_listings_params;
191 string h_tracking_changes = "false";
192 string h_output_changes = "false";
193 string h_margins = "";
196 /// translates a babel language name to a LyX language name
197 string babel2lyx(string language)
199 if (language == "arabtex")
200 return "arabic_arabtex";
201 if (language == "arabic")
202 return "arabic_arabi";
203 if (language == "lsorbian")
204 return "lowersorbian";
205 if (language == "usorbian")
206 return "uppersorbian";
211 // returns true if at least one of the options in what has been found
212 bool handle_opt(vector<string> & opts, char const * const * what, string & target)
218 // the last language option is the document language (for babel and LyX)
219 // the last size option is the document font size
220 vector<string>::iterator it;
221 vector<string>::iterator position = opts.begin();
222 for (; *what; ++what) {
223 it = find(opts.begin(), opts.end(), *what);
224 if (it != opts.end()) {
225 if (it >= position) {
236 void delete_opt(vector<string> & opts, char const * const * what)
241 // remove found options from the list
242 // do this after handle_opt to avoid potential memory leaks
243 vector<string>::iterator it;
244 for (; *what; ++what) {
245 it = find(opts.begin(), opts.end(), *what);
246 if (it != opts.end())
253 * Split a package options string (keyval format) into a vector.
255 * authorformat=smallcaps,
257 * titleformat=colonsep,
258 * bibformat={tabular,ibidem,numbered}
260 vector<string> split_options(string const & input)
262 vector<string> options;
266 Token const & t = p.get_token();
267 if (t.asInput() == ",") {
268 options.push_back(trim(option));
270 } else if (t.asInput() == "=") {
273 if (p.next_token().asInput() == "{")
274 option += '{' + p.getArg('{', '}') + '}';
275 } else if (t.cat() != catSpace)
276 option += t.asInput();
280 options.push_back(trim(option));
287 * Add package \p name with options \p options to used_packages.
288 * Remove options from \p options that we don't want to output.
290 void add_package(string const & name, vector<string> & options)
292 // every package inherits the global options
293 if (used_packages.find(name) == used_packages.end())
294 used_packages[name] = split_options(h_options);
296 vector<string> & v = used_packages[name];
297 v.insert(v.end(), options.begin(), options.end());
298 if (name == "jurabib") {
299 // Don't output the order argument (see the cite command
300 // handling code in text.cpp).
301 vector<string>::iterator end =
302 remove(options.begin(), options.end(), "natbiborder");
303 end = remove(options.begin(), end, "jurabiborder");
304 options.erase(end, options.end());
309 // Given is a string like "scaled=0.9", return 0.9 * 100
310 string const scale_as_percentage(string const & scale)
312 string::size_type pos = scale.find('=');
313 if (pos != string::npos) {
314 string value = scale.substr(pos + 1);
316 return convert<string>(100 * convert<double>(value));
318 // If the input string didn't match our expectations.
319 // return the default value "100"
324 void handle_package(Parser &p, string const & name, string const & opts,
325 bool in_lyx_preamble)
327 vector<string> options = split_options(opts);
328 add_package(name, options);
332 if (is_known(name, known_roman_fonts)) {
337 if (name == "fourier") {
338 h_font_roman = "utopia";
339 // when font uses real small capitals
340 if (opts == "expert")
344 if (name == "mathpazo")
345 h_font_roman = "palatino";
347 if (name == "mathptmx")
348 h_font_roman = "times";
351 if (is_known(name, known_sans_fonts)) {
355 h_font_sf_scale = scale_as_percentage(scale);
360 if (is_known(name, known_typewriter_fonts)) {
361 h_font_typewriter = name;
364 h_font_tt_scale = scale_as_percentage(scale);
368 // font uses old-style figure
372 else if (name == "amsmath" || name == "amssymb")
375 else if (name == "esint")
378 else if (name == "babel" && !opts.empty()) {
379 // check if more than one option was used - used later for inputenc
380 // in case inputenc is parsed before babel, set the encoding to auto
381 if (options.begin() != options.end() - 1) {
382 one_language = false;
383 h_inputencoding = "auto";
385 // babel takes the last language of the option of its \usepackage
386 // call as document language. If there is no such language option, the
387 // last language in the documentclass options is used.
388 handle_opt(options, known_languages, h_language);
389 delete_opt(options, known_languages);
392 else if (name == "fontenc")
395 else if (name == "inputenc") {
396 // h_inputencoding is only set when there is not more than one
397 // inputenc option because otherwise h_inputencoding must be
398 // set to "auto" (the default encoding of the document language)
399 // Therefore check for the "," character.
400 // It is also only set when there is not more then one babel
401 // language option but this is handled in the routine for babel.
402 if (opts.find(",") == string::npos && one_language == true)
403 h_inputencoding = opts;
404 if (!options.empty())
405 p.setEncoding(options.back());
409 else if (name == "makeidx")
412 else if (name == "prettyref")
415 else if (name == "varioref")
418 else if (name == "verbatim")
421 else if (name == "nomencl")
424 else if (name == "textcomp")
427 else if (name == "url")
430 else if (LYX_FORMAT >= 408 && name == "subscript")
433 else if (name == "color") {
434 // with the following command this package is only loaded when needed for
435 // undefined colors, since we only support the predefined colors
436 h_preamble << "\\@ifundefined{definecolor}\n {\\usepackage{color}}{}\n";
439 else if (name == "graphicx")
442 else if (name == "setspace")
445 else if (name == "geometry")
446 ; // Ignore this, the geometry settings are made by the \geometry
447 // command. This command is handled below.
449 else if (is_known(name, known_languages))
452 else if (name == "natbib") {
453 h_cite_engine = "natbib_authoryear";
454 vector<string>::iterator it =
455 find(options.begin(), options.end(), "authoryear");
456 if (it != options.end())
459 it = find(options.begin(), options.end(), "numbers");
460 if (it != options.end()) {
461 h_cite_engine = "natbib_numerical";
467 else if (name == "jurabib")
468 h_cite_engine = "jurabib";
470 else if (!in_lyx_preamble) {
472 h_preamble << "\\usepackage{" << name << "}";
474 h_preamble << "\\usepackage[" << opts << "]{"
480 // We need to do something with the options...
481 if (!options.empty())
482 cerr << "Ignoring options '" << join(options, ",")
483 << "' of package " << name << '.' << endl;
485 // remove the whitespace
491 void end_preamble(ostream & os, TextClass const & /*textclass*/)
493 // merge synonym languages
494 if (is_known(h_language, known_bahasa_languages))
495 h_language = "bahasa";
496 else if (is_known(h_language, known_bahasam_languages))
497 h_language = "bahasam";
498 else if (is_known(h_language, known_brazilian_languages))
499 h_language = "brazilian";
500 else if (is_known(h_language, known_french_languages))
501 h_language = "french";
502 else if (is_known(h_language, known_german_languages))
503 h_language = "german";
504 else if (is_known(h_language, known_ngerman_languages))
505 h_language = "ngerman";
506 else if (is_known(h_language, known_portuguese_languages))
507 h_language = "portuguese";
508 else if (is_known(h_language, known_russian_languages))
509 h_language = "russian";
510 else if (is_known(h_language, known_ukrainian_languages))
511 h_language = "ukrainian";
513 // set the quote language
514 // LyX only knows the following quotes languages:
515 // english, swedish, german, polish, french and danish
516 // (quotes for "japanese" and "chinese-traditional" are missing because
517 // they wouldn't be useful: http://www.lyx.org/trac/ticket/6383)
518 // conversion list taken from
519 // http://en.wikipedia.org/wiki/Quotation_mark,_non-English_usage
520 // (quotes for kazakh and interlingua are unknown)
522 if (h_language == "danish")
523 h_quotes_language = "danish";
525 else if (is_known(h_language, known_french_quotes_languages)
526 || is_known(h_language, known_french_languages)
527 || is_known(h_language, known_russian_languages)
528 || is_known(h_language, known_ukrainian_languages))
529 h_quotes_language = "french";
531 else if (is_known(h_language, known_german_quotes_languages)
532 || is_known(h_language, known_german_languages)
533 || is_known(h_language, known_ngerman_languages))
534 h_quotes_language = "german";
536 else if (is_known(h_language, known_polish_quotes_languages))
537 h_quotes_language = "polish";
539 else if (is_known(h_language, known_swedish_quotes_languages))
540 h_quotes_language = "swedish";
542 else if (is_known(h_language, known_english_quotes_languages)
543 || is_known(h_language, known_bahasa_languages)
544 || is_known(h_language, known_bahasam_languages)
545 || is_known(h_language, known_brazilian_languages)
546 || is_known(h_language, known_portuguese_languages))
547 h_quotes_language = "english";
549 h_language = babel2lyx(h_language);
551 // output the LyX file settings
552 os << "#LyX file created by tex2lyx " << PACKAGE_VERSION << "\n"
553 << "\\lyxformat " << LYX_FORMAT << '\n'
554 << "\\begin_document\n"
555 << "\\begin_header\n"
556 << "\\textclass " << h_textclass << "\n";
557 if (!h_preamble.str().empty())
558 os << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n";
559 if (!h_options.empty())
560 os << "\\options " << h_options << "\n";
561 os << "\\use_default_options " << h_use_default_options << "\n"
562 << "\\language " << h_language << "\n"
563 << "\\inputencoding " << h_inputencoding << "\n"
564 << "\\font_roman " << h_font_roman << "\n"
565 << "\\font_sans " << h_font_sans << "\n"
566 << "\\font_typewriter " << h_font_typewriter << "\n"
567 << "\\font_default_family " << h_font_default_family << "\n"
568 << "\\font_sc " << h_font_sc << "\n"
569 << "\\font_osf " << h_font_osf << "\n"
570 << "\\font_sf_scale " << h_font_sf_scale << "\n"
571 << "\\font_tt_scale " << h_font_tt_scale << "\n"
572 << "\\graphics " << h_graphics << "\n";
573 if (!h_float_placement.empty())
574 os << "\\float_placement " << h_float_placement << "\n";
575 os << "\\paperfontsize " << h_paperfontsize << "\n"
576 << "\\spacing " << h_spacing << "\n"
577 << "\\use_hyperref " << h_use_hyperref << '\n';
578 if (h_use_hyperref == "1") {
579 if (!h_pdf_title.empty())
580 os << "\\pdf_title \"" << h_pdf_title << "\"\n";
581 if (!h_pdf_author.empty())
582 os << "\\pdf_author \"" << h_pdf_author << "\"\n";
583 if (!h_pdf_subject.empty())
584 os << "\\pdf_subject \"" << h_pdf_subject << "\"\n";
585 if (!h_pdf_keywords.empty())
586 os << "\\pdf_keywords \"" << h_pdf_keywords << "\"\n";
587 os << "\\pdf_bookmarks " << h_pdf_bookmarks << "\n"
588 "\\pdf_bookmarksnumbered " << h_pdf_bookmarksnumbered << "\n"
589 "\\pdf_bookmarksopen " << h_pdf_bookmarksopen << "\n"
590 "\\pdf_bookmarksopenlevel " << h_pdf_bookmarksopenlevel << "\n"
591 "\\pdf_breaklinks " << h_pdf_breaklinks << "\n"
592 "\\pdf_pdfborder " << h_pdf_pdfborder << "\n"
593 "\\pdf_colorlinks " << h_pdf_colorlinks << "\n"
594 "\\pdf_backref " << h_pdf_backref << "\n"
595 "\\pdf_pagebackref " << h_pdf_pagebackref << "\n"
596 "\\pdf_pdfusetitle " << h_pdf_pdfusetitle << '\n';
597 if (!h_pdf_pagemode.empty())
598 os << "\\pdf_pagemode " << h_pdf_pagemode << '\n';
599 if (!h_pdf_quoted_options.empty())
600 os << "\\pdf_quoted_options \"" << h_pdf_quoted_options << "\"\n";
602 os << "\\papersize " << h_papersize << "\n"
603 << "\\use_geometry " << h_use_geometry << "\n"
604 << "\\use_amsmath " << h_use_amsmath << "\n"
605 << "\\use_esint " << h_use_esint << "\n"
606 << "\\cite_engine " << h_cite_engine << "\n"
607 << "\\use_bibtopic " << h_use_bibtopic << "\n"
608 << "\\paperorientation " << h_paperorientation << "\n"
610 << "\\secnumdepth " << h_secnumdepth << "\n"
611 << "\\tocdepth " << h_tocdepth << "\n"
612 << "\\paragraph_separation " << h_paragraph_separation << "\n"
613 << "\\defskip " << h_defskip << "\n"
614 << "\\quotes_language " << h_quotes_language << "\n"
615 << "\\papercolumns " << h_papercolumns << "\n"
616 << "\\papersides " << h_papersides << "\n"
617 << "\\paperpagestyle " << h_paperpagestyle << "\n";
618 if (!h_listings_params.empty())
619 os << "\\listings_params " << h_listings_params << "\n";
620 os << "\\tracking_changes " << h_tracking_changes << "\n"
621 << "\\output_changes " << h_output_changes << "\n"
622 << "\\end_header\n\n"
624 // clear preamble for subdocuments
628 } // anonymous namespace
630 void parse_preamble(Parser & p, ostream & os,
631 string const & forceclass, TeX2LyXDocClass & tc)
633 // initialize fixed types
634 special_columns['D'] = 3;
635 bool is_full_document = false;
636 bool is_lyx_file = false;
637 bool in_lyx_preamble = false;
639 // determine whether this is a full document or a fragment for inclusion
641 Token const & t = p.get_token();
643 if (t.cat() == catEscape && t.cs() == "documentclass") {
644 is_full_document = true;
650 while (is_full_document && p.good()) {
651 Token const & t = p.get_token();
654 cerr << "t: " << t << "\n";
660 if (!in_lyx_preamble &&
661 (t.cat() == catLetter ||
662 t.cat() == catSuper ||
664 t.cat() == catOther ||
665 t.cat() == catMath ||
666 t.cat() == catActive ||
667 t.cat() == catBegin ||
669 t.cat() == catAlign ||
670 t.cat() == catParameter))
671 h_preamble << t.cs();
673 else if (!in_lyx_preamble &&
674 (t.cat() == catSpace || t.cat() == catNewline))
675 h_preamble << t.asInput();
677 else if (t.cat() == catComment) {
678 // regex to parse comments (currently not used)
679 static regex const islyxfile("%% LyX .* created this file");
680 static regex const usercommands("User specified LaTeX commands");
682 string const comment = t.asInput();
684 // magically switch encoding default if it looks like XeLaTeX
685 static string const magicXeLaTeX =
686 "% This document must be compiled with XeLaTeX ";
687 if (comment.size() > magicXeLaTeX.size()
688 && comment.substr(0, magicXeLaTeX.size()) == magicXeLaTeX
689 && h_inputencoding == "auto") {
690 cerr << "XeLaTeX comment found, switching to UTF8\n";
691 h_inputencoding = "utf8";
694 if (regex_search(comment, sub, islyxfile)) {
696 in_lyx_preamble = true;
697 } else if (is_lyx_file
698 && regex_search(comment, sub, usercommands))
699 in_lyx_preamble = false;
700 else if (!in_lyx_preamble)
701 h_preamble << t.asInput();
704 else if (t.cs() == "pagestyle")
705 h_paperpagestyle = p.verbatim_item();
707 else if (t.cs() == "makeatletter") {
708 // LyX takes care of this
709 p.setCatCode('@', catLetter);
712 else if (t.cs() == "makeatother") {
713 // LyX takes care of this
714 p.setCatCode('@', catOther);
717 else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
718 || t.cs() == "providecommand"
719 || t.cs() == "DeclareRobustCommand"
720 || t.cs() == "ProvideTextCommandDefault"
721 || t.cs() == "DeclareMathAccent") {
723 if (p.next_token().character() == '*') {
727 string const name = p.verbatim_item();
728 string const opt1 = p.getOpt();
729 string const opt2 = p.getFullOpt();
730 string const body = p.verbatim_item();
732 if (name == "\\rmdefault")
733 if (is_known(body, known_roman_fonts))
735 if (name == "\\sfdefault")
736 if (is_known(body, known_sans_fonts))
738 if (name == "\\ttdefault")
739 if (is_known(body, known_typewriter_fonts))
740 h_font_typewriter = body;
741 if (name == "\\familydefault") {
742 string family = body;
743 // remove leading "\"
744 h_font_default_family = family.erase(0,1);
746 // only non-lyxspecific stuff
747 if (!in_lyx_preamble) {
749 ss << '\\' << t.cs();
752 ss << '{' << name << '}' << opt1 << opt2
753 << '{' << body << "}";
754 h_preamble << ss.str();
756 // Add the command to the known commands
757 add_known_command(name, opt1, !opt2.empty());
759 ostream & out = in_preamble ? h_preamble : os;
760 out << "\\" << t.cs() << "{" << name << "}"
761 << opts << "{" << body << "}";
766 else if (t.cs() == "documentclass") {
767 vector<string>::iterator it;
768 vector<string> opts = split_options(p.getArg('[', ']'));
769 handle_opt(opts, known_fontsizes, h_paperfontsize);
770 delete_opt(opts, known_fontsizes);
771 // delete "pt" at the end
772 string::size_type i = h_paperfontsize.find("pt");
773 if (i != string::npos)
774 h_paperfontsize.erase(i);
775 // The documentclass options are always parsed before the options
776 // of the babel call so that a language cannot overwrite the babel
778 handle_opt(opts, known_languages, h_language);
779 delete_opt(opts, known_languages);
782 if ((it = find(opts.begin(), opts.end(), "landscape")) != opts.end()) {
783 h_paperorientation = "landscape";
787 if ((it = find(opts.begin(), opts.end(), "oneside"))
792 if ((it = find(opts.begin(), opts.end(), "twoside"))
798 if ((it = find(opts.begin(), opts.end(), "onecolumn"))
800 h_papercolumns = "1";
803 if ((it = find(opts.begin(), opts.end(), "twocolumn"))
805 h_papercolumns = "2";
809 // some size options are know to any document classes, other sizes
810 // are handled by the \geometry command of the geometry package
811 handle_opt(opts, known_class_paper_sizes, h_papersize);
812 delete_opt(opts, known_class_paper_sizes);
813 // the remaining options
814 h_options = join(opts, ",");
815 h_textclass = p.getArg('{', '}');
818 else if (t.cs() == "usepackage") {
819 string const options = p.getArg('[', ']');
820 string const name = p.getArg('{', '}');
821 vector<string> vecnames;
822 split(name, vecnames, ',');
823 vector<string>::const_iterator it = vecnames.begin();
824 vector<string>::const_iterator end = vecnames.end();
825 for (; it != end; ++it)
826 handle_package(p, trim(*it), options,
830 else if (t.cs() == "inputencoding") {
831 string const encoding = p.getArg('{','}');
832 h_inputencoding = encoding;
833 p.setEncoding(encoding);
836 else if (t.cs() == "newenvironment") {
837 string const name = p.getArg('{', '}');
839 ss << "\\newenvironment{" << name << "}";
842 ss << '{' << p.verbatim_item() << '}';
843 ss << '{' << p.verbatim_item() << '}';
844 if (!in_lyx_preamble)
845 h_preamble << ss.str();
848 else if (t.cs() == "def") {
849 string name = p.get_token().cs();
850 while (p.next_token().cat() != catBegin)
851 name += p.get_token().cs();
852 if (!in_lyx_preamble)
853 h_preamble << "\\def\\" << name << '{'
854 << p.verbatim_item() << "}";
857 else if (t.cs() == "newcolumntype") {
858 string const name = p.getArg('{', '}');
861 string opts = p.getOpt();
863 istringstream is(string(opts, 1));
866 special_columns[name[0]] = nargs;
867 h_preamble << "\\newcolumntype{" << name << "}";
869 h_preamble << "[" << nargs << "]";
870 h_preamble << "{" << p.verbatim_item() << "}";
873 else if (t.cs() == "setcounter") {
874 string const name = p.getArg('{', '}');
875 string const content = p.getArg('{', '}');
876 if (name == "secnumdepth")
877 h_secnumdepth = content;
878 else if (name == "tocdepth")
879 h_tocdepth = content;
881 h_preamble << "\\setcounter{" << name << "}{" << content << "}";
884 else if (t.cs() == "setlength") {
885 string const name = p.verbatim_item();
886 string const content = p.verbatim_item();
887 // the paragraphs are only not indented when \parindent is set to zero
888 if (name == "\\parindent" && content != "") {
889 if (content[0] == '0')
890 h_paragraph_separation = "skip";
891 } else if (name == "\\parskip") {
892 if (content == "\\smallskipamount")
893 h_defskip = "smallskip";
894 else if (content == "\\medskipamount")
895 h_defskip = "medskip";
896 else if (content == "\\bigskipamount")
897 h_defskip = "bigskip";
901 h_preamble << "\\setlength{" << name << "}{" << content << "}";
904 else if (t.cs() == "onehalfspacing")
905 h_spacing = "onehalf";
907 else if (t.cs() == "doublespacing")
908 h_spacing = "double";
910 else if (t.cs() == "setstretch")
911 h_spacing = "other " + p.verbatim_item();
913 else if (t.cs() == "begin") {
914 string const name = p.getArg('{', '}');
915 if (name == "document")
917 h_preamble << "\\begin{" << name << "}";
920 else if (t.cs() == "geometry") {
921 h_use_geometry = "true";
922 vector<string> opts = split_options(p.getArg('{', '}'));
923 vector<string>::iterator it;
925 if ((it = find(opts.begin(), opts.end(), "landscape")) != opts.end()) {
926 h_paperorientation = "landscape";
930 handle_opt(opts, known_paper_sizes, h_papersize);
931 delete_opt(opts, known_paper_sizes);
933 char const * const * margin = known_paper_margins;
935 for (; *margin; ++margin) {
937 // search for the "=" in e.g. "lmargin=2cm" to get the value
938 for(size_t i = 0; i != opts.size(); i++) {
939 if (opts.at(i).find(*margin) != string::npos) {
940 string::size_type pos = opts.at(i).find("=");
941 string value = opts.at(i).substr(pos + 1);
942 string name = known_coded_paper_margins[k];
943 h_margins += "\\" + name + " " + value + "\n";
949 else if (t.cs() == "jurabibsetup") {
950 vector<string> jurabibsetup =
951 split_options(p.getArg('{', '}'));
952 // add jurabibsetup to the jurabib package options
953 add_package("jurabib", jurabibsetup);
954 if (!jurabibsetup.empty()) {
955 h_preamble << "\\jurabibsetup{"
956 << join(jurabibsetup, ",") << '}';
960 else if (!t.cs().empty() && !in_lyx_preamble)
961 h_preamble << '\\' << t.cs();
964 // remove the whitespace
967 // Force textclass if the user wanted it
968 if (!forceclass.empty())
969 h_textclass = forceclass;
970 if (noweb_mode && !prefixIs(h_textclass, "literate-"))
971 h_textclass.insert(0, "literate-");
972 FileName layoutfilename = libFileSearch("layouts", h_textclass, "layout");
973 if (layoutfilename.empty()) {
974 cerr << "Error: Could not find layout file for textclass \"" << h_textclass << "\"." << endl;
977 tc.read(layoutfilename);
978 if (h_papersides.empty()) {
981 h_papersides = ss.str();
983 end_preamble(os, tc);