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 const char * const known_languages[] = { "afrikaans", "american", "arabic",
60 "arabtex", "austrian", "bahasa", "basque", "belarusian", "brazil", "brazilian",
61 "breton", "british", "bulgarian", "canadian", "canadien", "catalan",
62 "croatian", "czech", "danish", "dutch", "english", "esperanto", "estonian",
63 "finnish", "francais", "french", "frenchb", "frenchle", "frenchpro",
64 "galician", "german", "germanb", "greek", "hebrew", "icelandic", "irish",
65 "italian", "kazakh", "latvian", "lithuanian", "lsorbian", "magyar",
66 "naustrian", "ngerman", "ngermanb", "norsk", "nynorsk", "polish", "portuges",
67 "portuguese", "romanian", "russian", "russianb", "scottish", "serbian", "slovak",
68 "slovene", "spanish", "swedish", "thai", "turkish", "ukraineb", "ukrainian",
69 "usorbian", "welsh", 0};
71 //add this when updating to lyxformat 305:
72 //bahasai, indonesian, and indon = equal to bahasa
73 //malay and meyalu = equal to bahasam
74 const char * const known_brazilian_languages[] = {"brazil", "brazilian", 0};
75 const char * const known_french_languages[] = {"french", "frenchb", "francais",
76 "frenchle", "frenchpro", 0};
77 const char * const known_german_languages[] = {"german", "germanb", 0};
78 const char * const known_ngerman_languages[] = {"ngerman", "ngermanb", 0};
79 const char * const known_portuguese_languages[] = {"portuges", "portuguese", 0};
80 const char * const known_russian_languages[] = {"russian", "russianb", 0};
81 const char * const known_ukrainian_languages[] = {"ukrainian", "ukraineb", 0};
83 //add these to known_english_quotes_languages when updating to lyxformat 268:
84 //"chinese-simplified", "korean"
85 // This requires first that support for non-babel languages (CJK) is added.
86 const char * const known_english_quotes_languages[] = {"american", "canadian",
87 "english", "esperanto", "hebrew", "irish", "scottish", "thai", 0};
89 //add this to known_french_quotes_languages when updating to lyxformat 327:
91 const char * const known_french_quotes_languages[] = {"albanian", "arabic",
92 "basque", "canadien", "catalan", "galician", "greek", "italian", "norsk",
93 "nynorsk", "spanish", "turkish", 0};
95 const char * const known_german_quotes_languages[] = {"austrian", "bulgarian",
96 "czech", "icelandic", "lithuanian", "lsorbian", "naustrian", "serbian",
97 "serbian-latin", "slovak", "slovene", "usorbian", 0};
99 const char * const known_polish_quotes_languages[] = {"afrikaans", "croatian",
100 "dutch", "estonian", "magyar", "polish", "romanian", 0};
102 const char * const known_swedish_quotes_languages[] = {"bahasa", "finnish",
105 char const * const known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
107 const char * const known_roman_fonts[] = { "ae", "bookman", "charter",
108 "cmr", "fourier", "lmodern", "mathpazo", "mathptmx", "newcent", 0};
110 const char * const known_sans_fonts[] = { "avant", "berasans", "cmbr", "cmss",
111 "helvet", "lmss", 0};
113 const char * const known_typewriter_fonts[] = { "beramono", "cmtl", "cmtt",
114 "courier", "lmtt", "luximono", "fourier", "lmodern", "mathpazo", "mathptmx",
117 const char * const known_paper_sizes[] = { "a3paper", "b3paper", "a4paper",
118 "b4paper", "a5paper", "b5paper", "executivepaper", "legalpaper",
121 const char * const known_class_paper_sizes[] = { "a4paper", "a5paper",
122 "executivepaper", "legalpaper", "letterpaper", 0};
124 const char * const known_paper_margins[] = { "lmargin", "tmargin", "rmargin",
125 "bmargin", "headheight", "headsep", "footskip", "columnsep", 0};
127 const char * const known_coded_paper_margins[] = { "leftmargin", "topmargin",
128 "rightmargin", "bottommargin", "headheight", "headsep", "footskip",
132 ostringstream h_preamble;
133 string h_textclass = "article";
134 string h_options = string();
135 string h_language = "english";
136 string h_inputencoding = "auto";
137 string h_font_roman = "default";
138 string h_font_sans = "default";
139 string h_font_typewriter = "default";
140 string h_font_default_family = "default";
141 string h_font_sc = "false";
142 string h_font_osf = "false";
143 string h_font_sf_scale = "100";
144 string h_font_tt_scale = "100";
145 string h_graphics = "default";
146 string h_float_placement;
147 string h_paperfontsize = "default";
148 string h_spacing = "single";
149 string h_use_hyperref = "0";
152 string h_pdf_subject;
153 string h_pdf_keywords;
154 string h_pdf_bookmarks = "1";
155 string h_pdf_bookmarksnumbered = "0";
156 string h_pdf_bookmarksopen = "0";
157 string h_pdf_bookmarksopenlevel = "1";
158 string h_pdf_breaklinks = "0";
159 string h_pdf_pdfborder = "0";
160 string h_pdf_colorlinks = "0";
161 string h_pdf_backref = "0";
162 string h_pdf_pagebackref = "0";
163 string h_pdf_pdfusetitle = "1";
164 string h_pdf_pagemode;
165 string h_pdf_quoted_options;
166 string h_papersize = "default";
167 string h_use_geometry = "false";
168 string h_use_amsmath = "1";
169 string h_use_esint = "1";
170 string h_cite_engine = "basic";
171 string h_use_bibtopic = "false";
172 string h_paperorientation = "portrait";
173 string h_secnumdepth = "3";
174 string h_tocdepth = "3";
175 string h_paragraph_separation = "indent";
176 string h_defskip = "medskip";
177 string h_quotes_language = "english";
178 string h_papercolumns = "1";
179 string h_papersides = string();
180 string h_paperpagestyle = "default";
181 string h_listings_params;
182 string h_tracking_changes = "false";
183 string h_output_changes = "false";
184 string h_margins = "";
187 /// translates a babel language name to a LyX language name
188 string babel2lyx(string language)
190 if (language == "arabtex")
191 return "arabic_arabtex";
192 if (language == "arabic")
193 return "arabic_arabi";
198 // returns true if at least one of the options in what has been found
199 bool handle_opt(vector<string> & opts, char const * const * what, string & target)
205 // the last language option is the document language (for babel and LyX)
206 // the last size option is the document font size
207 vector<string>::iterator it;
208 vector<string>::iterator position = opts.begin();
209 for (; *what; ++what) {
210 it = find(opts.begin(), opts.end(), *what);
211 if (it != opts.end()) {
212 if (it >= position) {
223 void delete_opt(vector<string> & opts, char const * const * what)
228 // remove found options from the list
229 // do this after handle_opt to avoid potential memory leaks
230 vector<string>::iterator it;
231 for (; *what; ++what) {
232 it = find(opts.begin(), opts.end(), *what);
233 if (it != opts.end())
240 * Split a package options string (keyval format) into a vector.
242 * authorformat=smallcaps,
244 * titleformat=colonsep,
245 * bibformat={tabular,ibidem,numbered}
247 vector<string> split_options(string const & input)
249 vector<string> options;
253 Token const & t = p.get_token();
254 if (t.asInput() == ",") {
255 options.push_back(trim(option));
257 } else if (t.asInput() == "=") {
260 if (p.next_token().asInput() == "{")
261 option += '{' + p.getArg('{', '}') + '}';
262 } else if (t.cat() != catSpace)
263 option += t.asInput();
267 options.push_back(trim(option));
274 * Add package \p name with options \p options to used_packages.
275 * Remove options from \p options that we don't want to output.
277 void add_package(string const & name, vector<string> & options)
279 // every package inherits the global options
280 if (used_packages.find(name) == used_packages.end())
281 used_packages[name] = split_options(h_options);
283 vector<string> & v = used_packages[name];
284 v.insert(v.end(), options.begin(), options.end());
285 if (name == "jurabib") {
286 // Don't output the order argument (see the cite command
287 // handling code in text.cpp).
288 vector<string>::iterator end =
289 remove(options.begin(), options.end(), "natbiborder");
290 end = remove(options.begin(), end, "jurabiborder");
291 options.erase(end, options.end());
296 // Given is a string like "scaled=0.9", return 0.9 * 100
297 string const scale_as_percentage(string const & scale)
299 string::size_type pos = scale.find('=');
300 if (pos != string::npos) {
301 string value = scale.substr(pos + 1);
303 return convert<string>(100 * convert<double>(value));
305 // If the input string didn't match our expectations.
306 // return the default value "100"
311 void handle_package(Parser &p, string const & name, string const & opts,
312 bool in_lyx_preamble)
314 vector<string> options = split_options(opts);
315 add_package(name, options);
319 if (is_known(name, known_roman_fonts)) {
324 if (name == "fourier") {
325 h_font_roman = "utopia";
326 // when font uses real small capitals
327 if (opts == "expert")
331 if (name == "mathpazo")
332 h_font_roman = "palatino";
334 if (name == "mathptmx")
335 h_font_roman = "times";
338 if (is_known(name, known_sans_fonts)) {
342 h_font_sf_scale = scale_as_percentage(scale);
347 if (is_known(name, known_typewriter_fonts)) {
348 h_font_typewriter = name;
351 h_font_tt_scale = scale_as_percentage(scale);
355 // font uses old-style figure
359 else if (name == "amsmath" || name == "amssymb")
362 else if (name == "esint")
365 else if (name == "babel" && !opts.empty()) {
366 // check if more than one option was used - used later for inputenc
367 // in case inputenc is parsed before babel, set the encoding to auto
368 if (options.begin() != options.end() - 1) {
369 one_language = false;
370 h_inputencoding = "auto";
372 // babel takes the last language of the option of its \usepackage
373 // call as document language. If there is no such language option, the
374 // last language in the documentclass options is used.
375 handle_opt(options, known_languages, h_language);
376 delete_opt(options, known_languages);
379 else if (name == "fontenc")
382 else if (name == "inputenc") {
383 // h_inputencoding is only set when there is not more than one
384 // inputenc option because otherwise h_inputencoding must be
385 // set to "auto" (the default encoding of the document language)
386 // Therefore check for the "," character.
387 // It is also only set when there is not more then one babel
388 // language option but this is handled in the routine for babel.
389 if (opts.find(",") == string::npos && one_language == true)
390 h_inputencoding = opts;
391 if (!options.empty())
392 p.setEncoding(options.back());
396 else if (name == "makeidx")
399 else if (name == "prettyref")
402 else if (name == "varioref")
405 else if (name == "verbatim")
408 else if (name == "nomencl")
411 else if (name == "textcomp")
414 else if (name == "url")
417 else if (LYX_FORMAT >= 408 && name == "subscript")
420 else if (name == "color") {
421 // with the following command this package is only loaded when needed for
422 // undefined colors, since we only support the predefined colors
423 h_preamble << "\\@ifundefined{definecolor}\n {\\usepackage{color}}{}\n";
426 else if (name == "graphicx")
429 else if (name == "setspace")
432 else if (name == "geometry")
433 ; // Ignore this, the geometry settings are made by the \geometry
434 // command. This command is handled below.
436 else if (is_known(name, known_languages))
439 else if (name == "natbib") {
440 h_cite_engine = "natbib_authoryear";
441 vector<string>::iterator it =
442 find(options.begin(), options.end(), "authoryear");
443 if (it != options.end())
446 it = find(options.begin(), options.end(), "numbers");
447 if (it != options.end()) {
448 h_cite_engine = "natbib_numerical";
454 else if (name == "jurabib")
455 h_cite_engine = "jurabib";
457 else if (!in_lyx_preamble) {
459 h_preamble << "\\usepackage{" << name << "}";
461 h_preamble << "\\usepackage[" << opts << "]{"
467 // We need to do something with the options...
468 if (!options.empty())
469 cerr << "Ignoring options '" << join(options, ",")
470 << "' of package " << name << '.' << endl;
472 // remove the whitespace
478 void end_preamble(ostream & os, TextClass const & /*textclass*/)
480 // merge synonym languages
481 if (is_known(h_language, known_brazilian_languages))
482 h_language = "brazilian";
483 else if (is_known(h_language, known_french_languages))
484 h_language = "french";
485 else if (is_known(h_language, known_german_languages))
486 h_language = "german";
487 else if (is_known(h_language, known_ngerman_languages))
488 h_language = "ngerman";
489 else if (is_known(h_language, known_portuguese_languages))
490 h_language = "portuguese";
491 else if (is_known(h_language, known_russian_languages))
492 h_language = "russian";
493 else if (is_known(h_language, known_ukrainian_languages))
494 h_language = "ukrainian";
496 // set the quote language
497 // LyX only knows the following quotes languages:
498 // english, swedish, german, polish, french and danish
499 // (quotes for "japanese" and "chinese-traditional" are missing because
500 // they wouldn't be useful: http://www.lyx.org/trac/ticket/6383)
501 // conversion list taken from
502 // http://en.wikipedia.org/wiki/Quotation_mark,_non-English_usage
503 // (quotes for kazakh and interlingua are unknown)
505 if (h_language == "danish")
506 h_quotes_language = "danish";
508 else if (is_known(h_language, known_french_quotes_languages)
509 || is_known(h_language, known_french_languages)
510 || is_known(h_language, known_russian_languages)
511 || is_known(h_language, known_ukrainian_languages))
512 h_quotes_language = "french";
514 else if (is_known(h_language, known_german_quotes_languages)
515 || is_known(h_language, known_german_languages)
516 || is_known(h_language, known_ngerman_languages))
517 h_quotes_language = "german";
519 else if (is_known(h_language, known_polish_quotes_languages))
520 h_quotes_language = "polish";
522 else if (is_known(h_language, known_swedish_quotes_languages))
523 h_quotes_language = "swedish";
525 else if (is_known(h_language, known_english_quotes_languages)
526 || is_known(h_language, known_brazilian_languages)
527 || is_known(h_language, known_portuguese_languages))
528 h_quotes_language = "english";
530 h_language = babel2lyx(h_language);
532 // output the LyX file settings
533 os << "#LyX file created by tex2lyx " << PACKAGE_VERSION << "\n"
534 << "\\lyxformat " << LYX_FORMAT << '\n'
535 << "\\begin_document\n"
536 << "\\begin_header\n"
537 << "\\textclass " << h_textclass << "\n";
538 if (!h_preamble.str().empty())
539 os << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n";
540 if (!h_options.empty())
541 os << "\\options " << h_options << "\n";
542 os << "\\language " << h_language << "\n"
543 << "\\inputencoding " << h_inputencoding << "\n"
544 << "\\font_roman " << h_font_roman << "\n"
545 << "\\font_sans " << h_font_sans << "\n"
546 << "\\font_typewriter " << h_font_typewriter << "\n"
547 << "\\font_default_family " << h_font_default_family << "\n"
548 << "\\font_sc " << h_font_sc << "\n"
549 << "\\font_osf " << h_font_osf << "\n"
550 << "\\font_sf_scale " << h_font_sf_scale << "\n"
551 << "\\font_tt_scale " << h_font_tt_scale << "\n"
552 << "\\graphics " << h_graphics << "\n";
553 if (!h_float_placement.empty())
554 os << "\\float_placement " << h_float_placement << "\n";
555 os << "\\paperfontsize " << h_paperfontsize << "\n"
556 << "\\spacing " << h_spacing << "\n"
557 << "\\use_hyperref " << h_use_hyperref << '\n';
558 if (h_use_hyperref == "1") {
559 if (!h_pdf_title.empty())
560 os << "\\pdf_title \"" << h_pdf_title << "\"\n";
561 if (!h_pdf_author.empty())
562 os << "\\pdf_author \"" << h_pdf_author << "\"\n";
563 if (!h_pdf_subject.empty())
564 os << "\\pdf_subject \"" << h_pdf_subject << "\"\n";
565 if (!h_pdf_keywords.empty())
566 os << "\\pdf_keywords \"" << h_pdf_keywords << "\"\n";
567 os << "\\pdf_bookmarks " << h_pdf_bookmarks << "\n"
568 "\\pdf_bookmarksnumbered " << h_pdf_bookmarksnumbered << "\n"
569 "\\pdf_bookmarksopen " << h_pdf_bookmarksopen << "\n"
570 "\\pdf_bookmarksopenlevel " << h_pdf_bookmarksopenlevel << "\n"
571 "\\pdf_breaklinks " << h_pdf_breaklinks << "\n"
572 "\\pdf_pdfborder " << h_pdf_pdfborder << "\n"
573 "\\pdf_colorlinks " << h_pdf_colorlinks << "\n"
574 "\\pdf_backref " << h_pdf_backref << "\n"
575 "\\pdf_pagebackref " << h_pdf_pagebackref << "\n"
576 "\\pdf_pdfusetitle " << h_pdf_pdfusetitle << '\n';
577 if (!h_pdf_pagemode.empty())
578 os << "\\pdf_pagemode " << h_pdf_pagemode << '\n';
579 if (!h_pdf_quoted_options.empty())
580 os << "\\pdf_quoted_options \"" << h_pdf_quoted_options << "\"\n";
582 os << "\\papersize " << h_papersize << "\n"
583 << "\\use_geometry " << h_use_geometry << "\n"
584 << "\\use_amsmath " << h_use_amsmath << "\n"
585 << "\\use_esint " << h_use_esint << "\n"
586 << "\\cite_engine " << h_cite_engine << "\n"
587 << "\\use_bibtopic " << h_use_bibtopic << "\n"
588 << "\\paperorientation " << h_paperorientation << "\n"
590 << "\\secnumdepth " << h_secnumdepth << "\n"
591 << "\\tocdepth " << h_tocdepth << "\n"
592 << "\\paragraph_separation " << h_paragraph_separation << "\n"
593 << "\\defskip " << h_defskip << "\n"
594 << "\\quotes_language " << h_quotes_language << "\n"
595 << "\\papercolumns " << h_papercolumns << "\n"
596 << "\\papersides " << h_papersides << "\n"
597 << "\\paperpagestyle " << h_paperpagestyle << "\n";
598 if (!h_listings_params.empty())
599 os << "\\listings_params " << h_listings_params << "\n";
600 os << "\\tracking_changes " << h_tracking_changes << "\n"
601 << "\\output_changes " << h_output_changes << "\n"
602 << "\\end_header\n\n"
604 // clear preamble for subdocuments
608 } // anonymous namespace
610 void parse_preamble(Parser & p, ostream & os,
611 string const & forceclass, TeX2LyXDocClass & tc)
613 // initialize fixed types
614 special_columns['D'] = 3;
615 bool is_full_document = false;
616 bool is_lyx_file = false;
617 bool in_lyx_preamble = false;
619 // determine whether this is a full document or a fragment for inclusion
621 Token const & t = p.get_token();
623 if (t.cat() == catEscape && t.cs() == "documentclass") {
624 is_full_document = true;
630 while (is_full_document && p.good()) {
631 Token const & t = p.get_token();
634 cerr << "t: " << t << "\n";
640 if (!in_lyx_preamble &&
641 (t.cat() == catLetter ||
642 t.cat() == catSuper ||
644 t.cat() == catOther ||
645 t.cat() == catMath ||
646 t.cat() == catActive ||
647 t.cat() == catBegin ||
649 t.cat() == catAlign ||
650 t.cat() == catParameter))
651 h_preamble << t.cs();
653 else if (!in_lyx_preamble &&
654 (t.cat() == catSpace || t.cat() == catNewline))
655 h_preamble << t.asInput();
657 else if (t.cat() == catComment) {
658 // regex to parse comments (currently not used)
659 static regex const islyxfile("%% LyX .* created this file");
660 static regex const usercommands("User specified LaTeX commands");
662 string const comment = t.asInput();
664 // magically switch encoding default if it looks like XeLaTeX
665 static string const magicXeLaTeX =
666 "% This document must be compiled with XeLaTeX ";
667 if (comment.size() > magicXeLaTeX.size()
668 && comment.substr(0, magicXeLaTeX.size()) == magicXeLaTeX
669 && h_inputencoding == "auto") {
670 cerr << "XeLaTeX comment found, switching to UTF8\n";
671 h_inputencoding = "utf8";
674 if (regex_search(comment, sub, islyxfile)) {
676 in_lyx_preamble = true;
677 } else if (is_lyx_file
678 && regex_search(comment, sub, usercommands))
679 in_lyx_preamble = false;
680 else if (!in_lyx_preamble)
681 h_preamble << t.asInput();
684 else if (t.cs() == "pagestyle")
685 h_paperpagestyle = p.verbatim_item();
687 else if (t.cs() == "makeatletter") {
688 // LyX takes care of this
689 p.setCatCode('@', catLetter);
692 else if (t.cs() == "makeatother") {
693 // LyX takes care of this
694 p.setCatCode('@', catOther);
697 else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
698 || t.cs() == "providecommand"
699 || t.cs() == "DeclareRobustCommand"
700 || t.cs() == "ProvideTextCommandDefault"
701 || t.cs() == "DeclareMathAccent") {
703 if (p.next_token().character() == '*') {
707 string const name = p.verbatim_item();
708 string const opt1 = p.getOpt();
709 string const opt2 = p.getFullOpt();
710 string const body = p.verbatim_item();
712 if (name == "\\rmdefault")
713 if (is_known(body, known_roman_fonts))
715 if (name == "\\sfdefault")
716 if (is_known(body, known_sans_fonts))
718 if (name == "\\ttdefault")
719 if (is_known(body, known_typewriter_fonts))
720 h_font_typewriter = body;
721 if (name == "\\familydefault") {
722 string family = body;
723 // remove leading "\"
724 h_font_default_family = family.erase(0,1);
726 // only non-lyxspecific stuff
727 if (!in_lyx_preamble) {
729 ss << '\\' << t.cs();
732 ss << '{' << name << '}' << opt1 << opt2
733 << '{' << body << "}";
734 h_preamble << ss.str();
736 // Add the command to the known commands
737 add_known_command(name, opt1, !opt2.empty());
739 ostream & out = in_preamble ? h_preamble : os;
740 out << "\\" << t.cs() << "{" << name << "}"
741 << opts << "{" << body << "}";
746 else if (t.cs() == "documentclass") {
747 vector<string>::iterator it;
748 vector<string> opts = split_options(p.getArg('[', ']'));
749 handle_opt(opts, known_fontsizes, h_paperfontsize);
750 delete_opt(opts, known_fontsizes);
751 // delete "pt" at the end
752 string::size_type i = h_paperfontsize.find("pt");
753 if (i != string::npos)
754 h_paperfontsize.erase(i);
755 // The documentclass options are always parsed before the options
756 // of the babel call so that a language cannot overwrite the babel
758 handle_opt(opts, known_languages, h_language);
759 delete_opt(opts, known_languages);
762 if ((it = find(opts.begin(), opts.end(), "landscape")) != opts.end()) {
763 h_paperorientation = "landscape";
767 if ((it = find(opts.begin(), opts.end(), "oneside"))
772 if ((it = find(opts.begin(), opts.end(), "twoside"))
778 if ((it = find(opts.begin(), opts.end(), "onecolumn"))
780 h_papercolumns = "1";
783 if ((it = find(opts.begin(), opts.end(), "twocolumn"))
785 h_papercolumns = "2";
789 // some size options are know to any document classes, other sizes
790 // are handled by the \geometry command of the geometry package
791 handle_opt(opts, known_class_paper_sizes, h_papersize);
792 delete_opt(opts, known_class_paper_sizes);
793 // the remaining options
794 h_options = join(opts, ",");
795 h_textclass = p.getArg('{', '}');
798 else if (t.cs() == "usepackage") {
799 string const options = p.getArg('[', ']');
800 string const name = p.getArg('{', '}');
801 vector<string> vecnames;
802 split(name, vecnames, ',');
803 vector<string>::const_iterator it = vecnames.begin();
804 vector<string>::const_iterator end = vecnames.end();
805 for (; it != end; ++it)
806 handle_package(p, trim(*it), options,
810 else if (t.cs() == "inputencoding") {
811 string const encoding = p.getArg('{','}');
812 h_inputencoding = encoding;
813 p.setEncoding(encoding);
816 else if (t.cs() == "newenvironment") {
817 string const name = p.getArg('{', '}');
819 ss << "\\newenvironment{" << name << "}";
822 ss << '{' << p.verbatim_item() << '}';
823 ss << '{' << p.verbatim_item() << '}';
824 if (!in_lyx_preamble)
825 h_preamble << ss.str();
828 else if (t.cs() == "def") {
829 string name = p.get_token().cs();
830 while (p.next_token().cat() != catBegin)
831 name += p.get_token().cs();
832 if (!in_lyx_preamble)
833 h_preamble << "\\def\\" << name << '{'
834 << p.verbatim_item() << "}";
837 else if (t.cs() == "newcolumntype") {
838 string const name = p.getArg('{', '}');
841 string opts = p.getOpt();
843 istringstream is(string(opts, 1));
846 special_columns[name[0]] = nargs;
847 h_preamble << "\\newcolumntype{" << name << "}";
849 h_preamble << "[" << nargs << "]";
850 h_preamble << "{" << p.verbatim_item() << "}";
853 else if (t.cs() == "setcounter") {
854 string const name = p.getArg('{', '}');
855 string const content = p.getArg('{', '}');
856 if (name == "secnumdepth")
857 h_secnumdepth = content;
858 else if (name == "tocdepth")
859 h_tocdepth = content;
861 h_preamble << "\\setcounter{" << name << "}{" << content << "}";
864 else if (t.cs() == "setlength") {
865 string const name = p.verbatim_item();
866 string const content = p.verbatim_item();
867 // the paragraphs are only not indented when \parindent is set to zero
868 if (name == "\\parindent" && content != "") {
869 if (content[0] == '0')
870 h_paragraph_separation = "skip";
871 } else if (name == "\\parskip") {
872 if (content == "\\smallskipamount")
873 h_defskip = "smallskip";
874 else if (content == "\\medskipamount")
875 h_defskip = "medskip";
876 else if (content == "\\bigskipamount")
877 h_defskip = "bigskip";
881 h_preamble << "\\setlength{" << name << "}{" << content << "}";
884 else if (t.cs() == "onehalfspacing")
885 h_spacing = "onehalf";
887 else if (t.cs() == "doublespacing")
888 h_spacing = "double";
890 else if (t.cs() == "setstretch")
891 h_spacing = "other " + p.verbatim_item();
893 else if (t.cs() == "begin") {
894 string const name = p.getArg('{', '}');
895 if (name == "document")
897 h_preamble << "\\begin{" << name << "}";
900 else if (t.cs() == "geometry") {
901 h_use_geometry = "true";
902 vector<string> opts = split_options(p.getArg('{', '}'));
903 vector<string>::iterator it;
905 if ((it = find(opts.begin(), opts.end(), "landscape")) != opts.end()) {
906 h_paperorientation = "landscape";
910 handle_opt(opts, known_paper_sizes, h_papersize);
911 delete_opt(opts, known_paper_sizes);
913 char const * const * margin = known_paper_margins;
915 for (; *margin; ++margin) {
917 // search for the "=" in e.g. "lmargin=2cm" to get the value
918 for(size_t i = 0; i != opts.size(); i++) {
919 if (opts.at(i).find(*margin) != string::npos) {
920 string::size_type pos = opts.at(i).find("=");
921 string value = opts.at(i).substr(pos + 1);
922 string name = known_coded_paper_margins[k];
923 h_margins += "\\" + name + " " + value + "\n";
929 else if (t.cs() == "jurabibsetup") {
930 vector<string> jurabibsetup =
931 split_options(p.getArg('{', '}'));
932 // add jurabibsetup to the jurabib package options
933 add_package("jurabib", jurabibsetup);
934 if (!jurabibsetup.empty()) {
935 h_preamble << "\\jurabibsetup{"
936 << join(jurabibsetup, ",") << '}';
940 else if (!t.cs().empty() && !in_lyx_preamble)
941 h_preamble << '\\' << t.cs();
944 // remove the whitespace
947 // Force textclass if the user wanted it
948 if (!forceclass.empty())
949 h_textclass = forceclass;
950 if (noweb_mode && !prefixIs(h_textclass, "literate-"))
951 h_textclass.insert(0, "literate-");
952 FileName layoutfilename = libFileSearch("layouts", h_textclass, "layout");
953 if (layoutfilename.empty()) {
954 cerr << "Error: Could not find layout file for textclass \"" << h_textclass << "\"." << endl;
957 tc.read(layoutfilename);
958 if (h_papersides.empty()) {
961 h_papersides = ss.str();
963 end_preamble(os, tc);