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 = "section";
171 string h_pdf_pdfusetitle = "1";
172 string h_pdf_pagemode;
173 string h_pdf_quoted_options;
174 string h_papersize = "default";
175 string h_use_geometry = "false";
176 string h_use_amsmath = "1";
177 string h_use_esint = "1";
178 string h_cite_engine = "basic";
179 string h_use_bibtopic = "false";
180 string h_paperorientation = "portrait";
181 string h_secnumdepth = "3";
182 string h_tocdepth = "3";
183 string h_paragraph_separation = "indent";
184 string h_defskip = "medskip";
185 string h_quotes_language = "english";
186 string h_papercolumns = "1";
187 string h_papersides = string();
188 string h_paperpagestyle = "default";
189 string h_listings_params;
190 string h_tracking_changes = "false";
191 string h_output_changes = "false";
192 string h_margins = "";
195 /// translates a babel language name to a LyX language name
196 string babel2lyx(string language)
198 if (language == "arabtex")
199 return "arabic_arabtex";
200 if (language == "arabic")
201 return "arabic_arabi";
202 if (language == "lsorbian")
203 return "lowersorbian";
204 if (language == "usorbian")
205 return "uppersorbian";
210 // returns true if at least one of the options in what has been found
211 bool handle_opt(vector<string> & opts, char const * const * what, string & target)
217 // the last language option is the document language (for babel and LyX)
218 // the last size option is the document font size
219 vector<string>::iterator it;
220 vector<string>::iterator position = opts.begin();
221 for (; *what; ++what) {
222 it = find(opts.begin(), opts.end(), *what);
223 if (it != opts.end()) {
224 if (it >= position) {
235 void delete_opt(vector<string> & opts, char const * const * what)
240 // remove found options from the list
241 // do this after handle_opt to avoid potential memory leaks
242 vector<string>::iterator it;
243 for (; *what; ++what) {
244 it = find(opts.begin(), opts.end(), *what);
245 if (it != opts.end())
252 * Split a package options string (keyval format) into a vector.
254 * authorformat=smallcaps,
256 * titleformat=colonsep,
257 * bibformat={tabular,ibidem,numbered}
259 vector<string> split_options(string const & input)
261 vector<string> options;
265 Token const & t = p.get_token();
266 if (t.asInput() == ",") {
267 options.push_back(trim(option));
269 } else if (t.asInput() == "=") {
272 if (p.next_token().asInput() == "{")
273 option += '{' + p.getArg('{', '}') + '}';
274 } else if (t.cat() != catSpace)
275 option += t.asInput();
279 options.push_back(trim(option));
286 * Add package \p name with options \p options to used_packages.
287 * Remove options from \p options that we don't want to output.
289 void add_package(string const & name, vector<string> & options)
291 // every package inherits the global options
292 if (used_packages.find(name) == used_packages.end())
293 used_packages[name] = split_options(h_options);
295 vector<string> & v = used_packages[name];
296 v.insert(v.end(), options.begin(), options.end());
297 if (name == "jurabib") {
298 // Don't output the order argument (see the cite command
299 // handling code in text.cpp).
300 vector<string>::iterator end =
301 remove(options.begin(), options.end(), "natbiborder");
302 end = remove(options.begin(), end, "jurabiborder");
303 options.erase(end, options.end());
308 // Given is a string like "scaled=0.9", return 0.9 * 100
309 string const scale_as_percentage(string const & scale)
311 string::size_type pos = scale.find('=');
312 if (pos != string::npos) {
313 string value = scale.substr(pos + 1);
315 return convert<string>(100 * convert<double>(value));
317 // If the input string didn't match our expectations.
318 // return the default value "100"
323 void handle_package(Parser &p, string const & name, string const & opts,
324 bool in_lyx_preamble)
326 vector<string> options = split_options(opts);
327 add_package(name, options);
331 if (is_known(name, known_roman_fonts)) {
336 if (name == "fourier") {
337 h_font_roman = "utopia";
338 // when font uses real small capitals
339 if (opts == "expert")
343 if (name == "mathpazo")
344 h_font_roman = "palatino";
346 if (name == "mathptmx")
347 h_font_roman = "times";
350 if (is_known(name, known_sans_fonts)) {
354 h_font_sf_scale = scale_as_percentage(scale);
359 if (is_known(name, known_typewriter_fonts)) {
360 h_font_typewriter = name;
363 h_font_tt_scale = scale_as_percentage(scale);
367 // font uses old-style figure
371 else if (name == "amsmath" || name == "amssymb")
374 else if (name == "esint")
377 else if (name == "babel" && !opts.empty()) {
378 // check if more than one option was used - used later for inputenc
379 // in case inputenc is parsed before babel, set the encoding to auto
380 if (options.begin() != options.end() - 1) {
381 one_language = false;
382 h_inputencoding = "auto";
384 // babel takes the last language of the option of its \usepackage
385 // call as document language. If there is no such language option, the
386 // last language in the documentclass options is used.
387 handle_opt(options, known_languages, h_language);
388 delete_opt(options, known_languages);
391 else if (name == "fontenc")
394 else if (name == "inputenc") {
395 // h_inputencoding is only set when there is not more than one
396 // inputenc option because otherwise h_inputencoding must be
397 // set to "auto" (the default encoding of the document language)
398 // Therefore check for the "," character.
399 // It is also only set when there is not more then one babel
400 // language option but this is handled in the routine for babel.
401 if (opts.find(",") == string::npos && one_language == true)
402 h_inputencoding = opts;
403 if (!options.empty())
404 p.setEncoding(options.back());
408 else if (name == "makeidx")
411 else if (name == "prettyref")
414 else if (name == "varioref")
417 else if (name == "verbatim")
420 else if (name == "nomencl")
423 else if (name == "textcomp")
426 else if (name == "url")
429 else if (LYX_FORMAT >= 408 && name == "subscript")
432 else if (name == "color") {
433 // with the following command this package is only loaded when needed for
434 // undefined colors, since we only support the predefined colors
435 h_preamble << "\\@ifundefined{definecolor}\n {\\usepackage{color}}{}\n";
438 else if (name == "graphicx")
441 else if (name == "setspace")
444 else if (name == "geometry")
445 ; // Ignore this, the geometry settings are made by the \geometry
446 // command. This command is handled below.
448 else if (is_known(name, known_languages))
451 else if (name == "natbib") {
452 h_cite_engine = "natbib_authoryear";
453 vector<string>::iterator it =
454 find(options.begin(), options.end(), "authoryear");
455 if (it != options.end())
458 it = find(options.begin(), options.end(), "numbers");
459 if (it != options.end()) {
460 h_cite_engine = "natbib_numerical";
466 else if (name == "jurabib")
467 h_cite_engine = "jurabib";
469 else if (!in_lyx_preamble) {
471 h_preamble << "\\usepackage{" << name << "}";
473 h_preamble << "\\usepackage[" << opts << "]{"
479 // We need to do something with the options...
480 if (!options.empty())
481 cerr << "Ignoring options '" << join(options, ",")
482 << "' of package " << name << '.' << endl;
484 // remove the whitespace
490 void end_preamble(ostream & os, TextClass const & /*textclass*/)
492 // merge synonym languages
493 if (is_known(h_language, known_bahasa_languages))
494 h_language = "bahasa";
495 else if (is_known(h_language, known_bahasam_languages))
496 h_language = "bahasam";
497 else if (is_known(h_language, known_brazilian_languages))
498 h_language = "brazilian";
499 else if (is_known(h_language, known_french_languages))
500 h_language = "french";
501 else if (is_known(h_language, known_german_languages))
502 h_language = "german";
503 else if (is_known(h_language, known_ngerman_languages))
504 h_language = "ngerman";
505 else if (is_known(h_language, known_portuguese_languages))
506 h_language = "portuguese";
507 else if (is_known(h_language, known_russian_languages))
508 h_language = "russian";
509 else if (is_known(h_language, known_ukrainian_languages))
510 h_language = "ukrainian";
512 // set the quote language
513 // LyX only knows the following quotes languages:
514 // english, swedish, german, polish, french and danish
515 // (quotes for "japanese" and "chinese-traditional" are missing because
516 // they wouldn't be useful: http://www.lyx.org/trac/ticket/6383)
517 // conversion list taken from
518 // http://en.wikipedia.org/wiki/Quotation_mark,_non-English_usage
519 // (quotes for kazakh and interlingua are unknown)
521 if (h_language == "danish")
522 h_quotes_language = "danish";
524 else if (is_known(h_language, known_french_quotes_languages)
525 || is_known(h_language, known_french_languages)
526 || is_known(h_language, known_russian_languages)
527 || is_known(h_language, known_ukrainian_languages))
528 h_quotes_language = "french";
530 else if (is_known(h_language, known_german_quotes_languages)
531 || is_known(h_language, known_german_languages)
532 || is_known(h_language, known_ngerman_languages))
533 h_quotes_language = "german";
535 else if (is_known(h_language, known_polish_quotes_languages))
536 h_quotes_language = "polish";
538 else if (is_known(h_language, known_swedish_quotes_languages))
539 h_quotes_language = "swedish";
541 else if (is_known(h_language, known_english_quotes_languages)
542 || is_known(h_language, known_bahasa_languages)
543 || is_known(h_language, known_bahasam_languages)
544 || is_known(h_language, known_brazilian_languages)
545 || is_known(h_language, known_portuguese_languages))
546 h_quotes_language = "english";
548 h_language = babel2lyx(h_language);
550 // output the LyX file settings
551 os << "#LyX file created by tex2lyx " << PACKAGE_VERSION << "\n"
552 << "\\lyxformat " << LYX_FORMAT << '\n'
553 << "\\begin_document\n"
554 << "\\begin_header\n"
555 << "\\textclass " << h_textclass << "\n";
556 if (!h_preamble.str().empty())
557 os << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n";
558 if (!h_options.empty())
559 os << "\\options " << h_options << "\n";
560 os << "\\use_default_options " << h_use_default_options << "\n"
561 << "\\language " << h_language << "\n"
562 << "\\inputencoding " << h_inputencoding << "\n"
563 << "\\font_roman " << h_font_roman << "\n"
564 << "\\font_sans " << h_font_sans << "\n"
565 << "\\font_typewriter " << h_font_typewriter << "\n"
566 << "\\font_default_family " << h_font_default_family << "\n"
567 << "\\font_sc " << h_font_sc << "\n"
568 << "\\font_osf " << h_font_osf << "\n"
569 << "\\font_sf_scale " << h_font_sf_scale << "\n"
570 << "\\font_tt_scale " << h_font_tt_scale << "\n"
571 << "\\graphics " << h_graphics << "\n";
572 if (!h_float_placement.empty())
573 os << "\\float_placement " << h_float_placement << "\n";
574 os << "\\paperfontsize " << h_paperfontsize << "\n"
575 << "\\spacing " << h_spacing << "\n"
576 << "\\use_hyperref " << h_use_hyperref << '\n';
577 if (h_use_hyperref == "1") {
578 if (!h_pdf_title.empty())
579 os << "\\pdf_title \"" << h_pdf_title << "\"\n";
580 if (!h_pdf_author.empty())
581 os << "\\pdf_author \"" << h_pdf_author << "\"\n";
582 if (!h_pdf_subject.empty())
583 os << "\\pdf_subject \"" << h_pdf_subject << "\"\n";
584 if (!h_pdf_keywords.empty())
585 os << "\\pdf_keywords \"" << h_pdf_keywords << "\"\n";
586 os << "\\pdf_bookmarks " << h_pdf_bookmarks << "\n"
587 "\\pdf_bookmarksnumbered " << h_pdf_bookmarksnumbered << "\n"
588 "\\pdf_bookmarksopen " << h_pdf_bookmarksopen << "\n"
589 "\\pdf_bookmarksopenlevel " << h_pdf_bookmarksopenlevel << "\n"
590 "\\pdf_breaklinks " << h_pdf_breaklinks << "\n"
591 "\\pdf_pdfborder " << h_pdf_pdfborder << "\n"
592 "\\pdf_colorlinks " << h_pdf_colorlinks << "\n"
593 "\\pdf_backref " << h_pdf_backref << "\n"
594 "\\pdf_pdfusetitle " << h_pdf_pdfusetitle << '\n';
595 if (!h_pdf_pagemode.empty())
596 os << "\\pdf_pagemode " << h_pdf_pagemode << '\n';
597 if (!h_pdf_quoted_options.empty())
598 os << "\\pdf_quoted_options \"" << h_pdf_quoted_options << "\"\n";
600 os << "\\papersize " << h_papersize << "\n"
601 << "\\use_geometry " << h_use_geometry << "\n"
602 << "\\use_amsmath " << h_use_amsmath << "\n"
603 << "\\use_esint " << h_use_esint << "\n"
604 << "\\cite_engine " << h_cite_engine << "\n"
605 << "\\use_bibtopic " << h_use_bibtopic << "\n"
606 << "\\paperorientation " << h_paperorientation << "\n"
608 << "\\secnumdepth " << h_secnumdepth << "\n"
609 << "\\tocdepth " << h_tocdepth << "\n"
610 << "\\paragraph_separation " << h_paragraph_separation << "\n"
611 << "\\defskip " << h_defskip << "\n"
612 << "\\quotes_language " << h_quotes_language << "\n"
613 << "\\papercolumns " << h_papercolumns << "\n"
614 << "\\papersides " << h_papersides << "\n"
615 << "\\paperpagestyle " << h_paperpagestyle << "\n";
616 if (!h_listings_params.empty())
617 os << "\\listings_params " << h_listings_params << "\n";
618 os << "\\tracking_changes " << h_tracking_changes << "\n"
619 << "\\output_changes " << h_output_changes << "\n"
620 << "\\end_header\n\n"
622 // clear preamble for subdocuments
626 } // anonymous namespace
628 void parse_preamble(Parser & p, ostream & os,
629 string const & forceclass, TeX2LyXDocClass & tc)
631 // initialize fixed types
632 special_columns['D'] = 3;
633 bool is_full_document = false;
634 bool is_lyx_file = false;
635 bool in_lyx_preamble = false;
637 // determine whether this is a full document or a fragment for inclusion
639 Token const & t = p.get_token();
641 if (t.cat() == catEscape && t.cs() == "documentclass") {
642 is_full_document = true;
648 while (is_full_document && p.good()) {
649 Token const & t = p.get_token();
652 cerr << "t: " << t << "\n";
658 if (!in_lyx_preamble &&
659 (t.cat() == catLetter ||
660 t.cat() == catSuper ||
662 t.cat() == catOther ||
663 t.cat() == catMath ||
664 t.cat() == catActive ||
665 t.cat() == catBegin ||
667 t.cat() == catAlign ||
668 t.cat() == catParameter))
669 h_preamble << t.cs();
671 else if (!in_lyx_preamble &&
672 (t.cat() == catSpace || t.cat() == catNewline))
673 h_preamble << t.asInput();
675 else if (t.cat() == catComment) {
676 // regex to parse comments (currently not used)
677 static regex const islyxfile("%% LyX .* created this file");
678 static regex const usercommands("User specified LaTeX commands");
680 string const comment = t.asInput();
682 // magically switch encoding default if it looks like XeLaTeX
683 static string const magicXeLaTeX =
684 "% This document must be compiled with XeLaTeX ";
685 if (comment.size() > magicXeLaTeX.size()
686 && comment.substr(0, magicXeLaTeX.size()) == magicXeLaTeX
687 && h_inputencoding == "auto") {
688 cerr << "XeLaTeX comment found, switching to UTF8\n";
689 h_inputencoding = "utf8";
692 if (regex_search(comment, sub, islyxfile)) {
694 in_lyx_preamble = true;
695 } else if (is_lyx_file
696 && regex_search(comment, sub, usercommands))
697 in_lyx_preamble = false;
698 else if (!in_lyx_preamble)
699 h_preamble << t.asInput();
702 else if (t.cs() == "pagestyle")
703 h_paperpagestyle = p.verbatim_item();
705 else if (t.cs() == "makeatletter") {
706 // LyX takes care of this
707 p.setCatCode('@', catLetter);
710 else if (t.cs() == "makeatother") {
711 // LyX takes care of this
712 p.setCatCode('@', catOther);
715 else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
716 || t.cs() == "providecommand"
717 || t.cs() == "DeclareRobustCommand"
718 || t.cs() == "ProvideTextCommandDefault"
719 || t.cs() == "DeclareMathAccent") {
721 if (p.next_token().character() == '*') {
725 string const name = p.verbatim_item();
726 string const opt1 = p.getOpt();
727 string const opt2 = p.getFullOpt();
728 string const body = p.verbatim_item();
730 if (name == "\\rmdefault")
731 if (is_known(body, known_roman_fonts))
733 if (name == "\\sfdefault")
734 if (is_known(body, known_sans_fonts))
736 if (name == "\\ttdefault")
737 if (is_known(body, known_typewriter_fonts))
738 h_font_typewriter = body;
739 if (name == "\\familydefault") {
740 string family = body;
741 // remove leading "\"
742 h_font_default_family = family.erase(0,1);
744 // only non-lyxspecific stuff
745 if (!in_lyx_preamble) {
747 ss << '\\' << t.cs();
750 ss << '{' << name << '}' << opt1 << opt2
751 << '{' << body << "}";
752 h_preamble << ss.str();
754 // Add the command to the known commands
755 add_known_command(name, opt1, !opt2.empty());
757 ostream & out = in_preamble ? h_preamble : os;
758 out << "\\" << t.cs() << "{" << name << "}"
759 << opts << "{" << body << "}";
764 else if (t.cs() == "documentclass") {
765 vector<string>::iterator it;
766 vector<string> opts = split_options(p.getArg('[', ']'));
767 handle_opt(opts, known_fontsizes, h_paperfontsize);
768 delete_opt(opts, known_fontsizes);
769 // delete "pt" at the end
770 string::size_type i = h_paperfontsize.find("pt");
771 if (i != string::npos)
772 h_paperfontsize.erase(i);
773 // The documentclass options are always parsed before the options
774 // of the babel call so that a language cannot overwrite the babel
776 handle_opt(opts, known_languages, h_language);
777 delete_opt(opts, known_languages);
780 if ((it = find(opts.begin(), opts.end(), "landscape")) != opts.end()) {
781 h_paperorientation = "landscape";
785 if ((it = find(opts.begin(), opts.end(), "oneside"))
790 if ((it = find(opts.begin(), opts.end(), "twoside"))
796 if ((it = find(opts.begin(), opts.end(), "onecolumn"))
798 h_papercolumns = "1";
801 if ((it = find(opts.begin(), opts.end(), "twocolumn"))
803 h_papercolumns = "2";
807 // some size options are know to any document classes, other sizes
808 // are handled by the \geometry command of the geometry package
809 handle_opt(opts, known_class_paper_sizes, h_papersize);
810 delete_opt(opts, known_class_paper_sizes);
811 // the remaining options
812 h_options = join(opts, ",");
813 h_textclass = p.getArg('{', '}');
816 else if (t.cs() == "usepackage") {
817 string const options = p.getArg('[', ']');
818 string const name = p.getArg('{', '}');
819 vector<string> vecnames;
820 split(name, vecnames, ',');
821 vector<string>::const_iterator it = vecnames.begin();
822 vector<string>::const_iterator end = vecnames.end();
823 for (; it != end; ++it)
824 handle_package(p, trim(*it), options,
828 else if (t.cs() == "inputencoding") {
829 string const encoding = p.getArg('{','}');
830 h_inputencoding = encoding;
831 p.setEncoding(encoding);
834 else if (t.cs() == "newenvironment") {
835 string const name = p.getArg('{', '}');
837 ss << "\\newenvironment{" << name << "}";
840 ss << '{' << p.verbatim_item() << '}';
841 ss << '{' << p.verbatim_item() << '}';
842 if (!in_lyx_preamble)
843 h_preamble << ss.str();
846 else if (t.cs() == "def") {
847 string name = p.get_token().cs();
848 while (p.next_token().cat() != catBegin)
849 name += p.get_token().cs();
850 if (!in_lyx_preamble)
851 h_preamble << "\\def\\" << name << '{'
852 << p.verbatim_item() << "}";
855 else if (t.cs() == "newcolumntype") {
856 string const name = p.getArg('{', '}');
859 string opts = p.getOpt();
861 istringstream is(string(opts, 1));
864 special_columns[name[0]] = nargs;
865 h_preamble << "\\newcolumntype{" << name << "}";
867 h_preamble << "[" << nargs << "]";
868 h_preamble << "{" << p.verbatim_item() << "}";
871 else if (t.cs() == "setcounter") {
872 string const name = p.getArg('{', '}');
873 string const content = p.getArg('{', '}');
874 if (name == "secnumdepth")
875 h_secnumdepth = content;
876 else if (name == "tocdepth")
877 h_tocdepth = content;
879 h_preamble << "\\setcounter{" << name << "}{" << content << "}";
882 else if (t.cs() == "setlength") {
883 string const name = p.verbatim_item();
884 string const content = p.verbatim_item();
885 // the paragraphs are only not indented when \parindent is set to zero
886 if (name == "\\parindent" && content != "") {
887 if (content[0] == '0')
888 h_paragraph_separation = "skip";
889 } else if (name == "\\parskip") {
890 if (content == "\\smallskipamount")
891 h_defskip = "smallskip";
892 else if (content == "\\medskipamount")
893 h_defskip = "medskip";
894 else if (content == "\\bigskipamount")
895 h_defskip = "bigskip";
899 h_preamble << "\\setlength{" << name << "}{" << content << "}";
902 else if (t.cs() == "onehalfspacing")
903 h_spacing = "onehalf";
905 else if (t.cs() == "doublespacing")
906 h_spacing = "double";
908 else if (t.cs() == "setstretch")
909 h_spacing = "other " + p.verbatim_item();
911 else if (t.cs() == "begin") {
912 string const name = p.getArg('{', '}');
913 if (name == "document")
915 h_preamble << "\\begin{" << name << "}";
918 else if (t.cs() == "geometry") {
919 h_use_geometry = "true";
920 vector<string> opts = split_options(p.getArg('{', '}'));
921 vector<string>::iterator it;
923 if ((it = find(opts.begin(), opts.end(), "landscape")) != opts.end()) {
924 h_paperorientation = "landscape";
928 handle_opt(opts, known_paper_sizes, h_papersize);
929 delete_opt(opts, known_paper_sizes);
931 char const * const * margin = known_paper_margins;
933 for (; *margin; ++margin) {
935 // search for the "=" in e.g. "lmargin=2cm" to get the value
936 for(size_t i = 0; i != opts.size(); i++) {
937 if (opts.at(i).find(*margin) != string::npos) {
938 string::size_type pos = opts.at(i).find("=");
939 string value = opts.at(i).substr(pos + 1);
940 string name = known_coded_paper_margins[k];
941 h_margins += "\\" + name + " " + value + "\n";
947 else if (t.cs() == "jurabibsetup") {
948 vector<string> jurabibsetup =
949 split_options(p.getArg('{', '}'));
950 // add jurabibsetup to the jurabib package options
951 add_package("jurabib", jurabibsetup);
952 if (!jurabibsetup.empty()) {
953 h_preamble << "\\jurabibsetup{"
954 << join(jurabibsetup, ",") << '}';
958 else if (!t.cs().empty() && !in_lyx_preamble)
959 h_preamble << '\\' << t.cs();
962 // remove the whitespace
965 // Force textclass if the user wanted it
966 if (!forceclass.empty())
967 h_textclass = forceclass;
968 if (noweb_mode && !prefixIs(h_textclass, "literate-"))
969 h_textclass.insert(0, "literate-");
970 FileName layoutfilename = libFileSearch("layouts", h_textclass, "layout");
971 if (layoutfilename.empty()) {
972 cerr << "Error: Could not find layout file for textclass \"" << h_textclass << "\"." << endl;
975 tc.read(layoutfilename);
976 if (h_papersides.empty()) {
979 h_papersides = ss.str();
981 end_preamble(os, tc);