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 turkmen for lyxformat 383
60 const char * const known_languages[] = { "afrikaans", "albanian", "american",
61 "arabic", "arabtex", "austrian", "bahasa", "bahasai", "bahasam", "basque",
62 "belarusian", "brazil", "brazilian", "breton", "british", "bulgarian",
63 "canadian", "canadien", "catalan", "croatian", "czech", "danish", "dutch",
64 "english", "esperanto", "estonian", "finnish", "francais", "french",
65 "frenchb", "frenchle", "frenchpro", "galician", "german", "germanb", "greek",
66 "hebrew", "icelandic", "indon", "indonesian", "interlingua", "irish",
67 "italian", "kazakh", "latin", "latvian", "lithuanian", "lsorbian", "magyar",
68 "malay", "meyalu", "mongolian", "naustrian", "ngerman", "ngermanb", "norsk",
69 "nynorsk", "polutonikogreek", "polish", "portuges", "portuguese", "romanian",
70 "russian", "russianb", "samin", "scottish", "serbian", "serbian-latin",
71 "slovak", "slovene", "spanish", "swedish", "thai", "turkish", "ukraineb",
72 "ukrainian", "usorbian", "vietnam", "welsh", 0};
74 const char * const known_bahasa_languages[] = {"bahasa", "bahasai",
75 "indon", "indonesian", 0};
76 const char * const known_bahasam_languages[] = {"bahasam", "malay",
78 const char * const known_brazilian_languages[] = {"brazil", "brazilian", 0};
79 const char * const known_french_languages[] = {"french", "frenchb", "francais",
80 "frenchle", "frenchpro", 0};
81 const char * const known_german_languages[] = {"german", "germanb", 0};
82 const char * const known_ngerman_languages[] = {"ngerman", "ngermanb", 0};
83 const char * const known_portuguese_languages[] = {"portuges", "portuguese", 0};
84 const char * const known_russian_languages[] = {"russian", "russianb", 0};
85 const char * const known_ukrainian_languages[] = {"ukrainian", "ukraineb", 0};
87 //add these to known_english_quotes_languages when updating to lyxformat 268:
88 //"chinese-simplified", "korean"
89 // This requires first that support for non-babel languages (CJK) is added.
90 const char * const known_english_quotes_languages[] = {"american", "canadian",
91 "english", "esperanto", "hebrew", "irish", "scottish", "thai", 0};
93 //add this to known_french_quotes_languages when updating to
94 //lyxformat 383: "turkmen"
95 const char * const known_french_quotes_languages[] = {"albanian", "arabic",
96 "basque", "canadien", "catalan", "galician", "greek", "italian", "norsk",
97 "nynorsk", "polutonikogreek", "spanish", "spanish-mexico", "turkish",
100 const char * const known_german_quotes_languages[] = {"austrian", "bulgarian",
101 "czech", "icelandic", "lithuanian", "lsorbian", "naustrian", "serbian",
102 "serbian-latin", "slovak", "slovene", "usorbian", 0};
104 const char * const known_polish_quotes_languages[] = {"afrikaans", "croatian",
105 "dutch", "estonian", "magyar", "polish", "romanian", 0};
107 const char * const known_swedish_quotes_languages[] = {"finnish",
110 char const * const known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
112 const char * const known_roman_fonts[] = { "ae", "bookman", "charter",
113 "cmr", "fourier", "lmodern", "mathpazo", "mathptmx", "newcent", 0};
115 const char * const known_sans_fonts[] = { "avant", "berasans", "cmbr", "cmss",
116 "helvet", "lmss", 0};
118 const char * const known_typewriter_fonts[] = { "beramono", "cmtl", "cmtt",
119 "courier", "lmtt", "luximono", "fourier", "lmodern", "mathpazo", "mathptmx",
122 const char * const known_paper_sizes[] = { "a3paper", "b3paper", "a4paper",
123 "b4paper", "a5paper", "b5paper", "executivepaper", "legalpaper",
126 const char * const known_class_paper_sizes[] = { "a4paper", "a5paper",
127 "executivepaper", "legalpaper", "letterpaper", 0};
129 const char * const known_paper_margins[] = { "lmargin", "tmargin", "rmargin",
130 "bmargin", "headheight", "headsep", "footskip", "columnsep", 0};
132 const char * const known_coded_paper_margins[] = { "leftmargin", "topmargin",
133 "rightmargin", "bottommargin", "headheight", "headsep", "footskip",
137 ostringstream h_preamble;
138 string h_textclass = "article";
139 string h_use_default_options = "false";
140 string h_options = string();
141 string h_language = "english";
142 string h_inputencoding = "auto";
143 string h_font_roman = "default";
144 string h_font_sans = "default";
145 string h_font_typewriter = "default";
146 string h_font_default_family = "default";
147 string h_font_sc = "false";
148 string h_font_osf = "false";
149 string h_font_sf_scale = "100";
150 string h_font_tt_scale = "100";
151 string h_graphics = "default";
152 string h_float_placement;
153 string h_paperfontsize = "default";
154 string h_spacing = "single";
155 string h_use_hyperref = "0";
158 string h_pdf_subject;
159 string h_pdf_keywords;
160 string h_pdf_bookmarks = "1";
161 string h_pdf_bookmarksnumbered = "0";
162 string h_pdf_bookmarksopen = "0";
163 string h_pdf_bookmarksopenlevel = "1";
164 string h_pdf_breaklinks = "0";
165 string h_pdf_pdfborder = "0";
166 string h_pdf_colorlinks = "0";
167 string h_pdf_backref = "section";
168 string h_pdf_pdfusetitle = "1";
169 string h_pdf_pagemode;
170 string h_pdf_quoted_options;
171 string h_papersize = "default";
172 string h_use_geometry = "false";
173 string h_use_amsmath = "1";
174 string h_use_esint = "1";
175 string h_cite_engine = "basic";
176 string h_use_bibtopic = "false";
177 string h_paperorientation = "portrait";
178 string h_secnumdepth = "3";
179 string h_tocdepth = "3";
180 string h_paragraph_separation = "indent";
181 string h_defskip = "medskip";
182 string h_paragraph_indentation = "default";
183 string h_quotes_language = "english";
184 string h_papercolumns = "1";
185 string h_papersides = string();
186 string h_paperpagestyle = "default";
187 string h_listings_params;
188 string h_tracking_changes = "false";
189 string h_output_changes = "false";
190 string h_margins = "";
193 /// translates a babel language name to a LyX language name
194 string babel2lyx(string language)
196 if (language == "arabtex")
197 return "arabic_arabtex";
198 if (language == "arabic")
199 return "arabic_arabi";
200 if (language == "lsorbian")
201 return "lowersorbian";
202 if (language == "usorbian")
203 return "uppersorbian";
208 // returns true if at least one of the options in what has been found
209 bool handle_opt(vector<string> & opts, char const * const * what, string & target)
215 // the last language option is the document language (for babel and LyX)
216 // the last size option is the document font size
217 vector<string>::iterator it;
218 vector<string>::iterator position = opts.begin();
219 for (; *what; ++what) {
220 it = find(opts.begin(), opts.end(), *what);
221 if (it != opts.end()) {
222 if (it >= position) {
233 void delete_opt(vector<string> & opts, char const * const * what)
238 // remove found options from the list
239 // do this after handle_opt to avoid potential memory leaks
240 vector<string>::iterator it;
241 for (; *what; ++what) {
242 it = find(opts.begin(), opts.end(), *what);
243 if (it != opts.end())
250 * Split a package options string (keyval format) into a vector.
252 * authorformat=smallcaps,
254 * titleformat=colonsep,
255 * bibformat={tabular,ibidem,numbered}
257 vector<string> split_options(string const & input)
259 vector<string> options;
263 Token const & t = p.get_token();
264 if (t.asInput() == ",") {
265 options.push_back(trim(option));
267 } else if (t.asInput() == "=") {
270 if (p.next_token().asInput() == "{")
271 option += '{' + p.getArg('{', '}') + '}';
272 } else if (t.cat() != catSpace)
273 option += t.asInput();
277 options.push_back(trim(option));
284 * Retrieve a keyval option "name={value with=sign}" named \p name from
285 * \p options and return the value.
286 * The found option is also removed from \p options.
288 string process_keyval_opt(vector<string> & options, string name)
290 for (size_t i = 0; i < options.size(); ++i) {
291 vector<string> option;
292 split(options[i], option, '=');
293 if (option.size() < 2)
295 if (option[0] == name) {
296 options.erase(options.begin() + i);
297 option.erase(option.begin());
298 return join(option, "=");
306 * Add package \p name with options \p options to used_packages.
307 * Remove options from \p options that we don't want to output.
309 void add_package(string const & name, vector<string> & options)
311 // every package inherits the global options
312 if (used_packages.find(name) == used_packages.end())
313 used_packages[name] = split_options(h_options);
315 vector<string> & v = used_packages[name];
316 v.insert(v.end(), options.begin(), options.end());
317 if (name == "jurabib") {
318 // Don't output the order argument (see the cite command
319 // handling code in text.cpp).
320 vector<string>::iterator end =
321 remove(options.begin(), options.end(), "natbiborder");
322 end = remove(options.begin(), end, "jurabiborder");
323 options.erase(end, options.end());
328 // Given is a string like "scaled=0.9", return 0.9 * 100
329 string const scale_as_percentage(string const & scale)
331 string::size_type pos = scale.find('=');
332 if (pos != string::npos) {
333 string value = scale.substr(pos + 1);
335 return convert<string>(100 * convert<double>(value));
337 // If the input string didn't match our expectations.
338 // return the default value "100"
343 string remove_braces(string const & value)
347 if (value[0] == '{' && value[value.length()-1] == '}')
348 return value.substr(1, value.length()-2);
353 void handle_hyperref(vector<string> & options)
355 // FIXME swallow inputencoding changes that might surround the
356 // hyperref setup if it was written by LyX
357 h_use_hyperref = "1";
358 // swallow "unicode=true", since LyX does always write that
359 vector<string>::iterator it =
360 find(options.begin(), options.end(), "unicode=true");
361 if (it != options.end())
363 it = find(options.begin(), options.end(), "pdfusetitle");
364 if (it != options.end()) {
365 h_pdf_pdfusetitle = "1";
368 string bookmarks = process_keyval_opt(options, "bookmarks");
369 if (bookmarks == "true")
370 h_pdf_bookmarks = "1";
371 else if (bookmarks == "false")
372 h_pdf_bookmarks = "0";
373 if (h_pdf_bookmarks == "1") {
374 string bookmarksnumbered =
375 process_keyval_opt(options, "bookmarksnumbered");
376 if (bookmarksnumbered == "true")
377 h_pdf_bookmarksnumbered = "1";
378 else if (bookmarksnumbered == "false")
379 h_pdf_bookmarksnumbered = "0";
380 string bookmarksopen =
381 process_keyval_opt(options, "bookmarksopen");
382 if (bookmarksopen == "true")
383 h_pdf_bookmarksopen = "1";
384 else if (bookmarksopen == "false")
385 h_pdf_bookmarksopen = "0";
386 if (h_pdf_bookmarksopen == "1") {
387 string bookmarksopenlevel =
388 process_keyval_opt(options, "bookmarksopenlevel");
389 if (!bookmarksopenlevel.empty())
390 h_pdf_bookmarksopenlevel = bookmarksopenlevel;
393 string breaklinks = process_keyval_opt(options, "breaklinks");
394 if (breaklinks == "true")
395 h_pdf_breaklinks = "1";
396 else if (breaklinks == "false")
397 h_pdf_breaklinks = "0";
398 string pdfborder = process_keyval_opt(options, "pdfborder");
399 if (pdfborder == "{0 0 0}")
400 h_pdf_pdfborder = "1";
401 else if (pdfborder == "{0 0 1}")
402 h_pdf_pdfborder = "0";
403 string backref = process_keyval_opt(options, "backref");
404 if (!backref.empty())
405 h_pdf_backref = backref;
406 string colorlinks = process_keyval_opt(options, "colorlinks");
407 if (colorlinks == "true")
408 h_pdf_colorlinks = "1";
409 else if (colorlinks == "false")
410 h_pdf_colorlinks = "0";
411 string pdfpagemode = process_keyval_opt(options, "pdfpagemode");
412 if (!pdfpagemode.empty())
413 h_pdf_pagemode = pdfpagemode;
414 string pdftitle = process_keyval_opt(options, "pdftitle");
415 if (!pdftitle.empty()) {
416 h_pdf_title = remove_braces(pdftitle);
418 string pdfauthor = process_keyval_opt(options, "pdfauthor");
419 if (!pdfauthor.empty()) {
420 h_pdf_author = remove_braces(pdfauthor);
422 string pdfsubject = process_keyval_opt(options, "pdfsubject");
423 if (!pdfsubject.empty())
424 h_pdf_subject = remove_braces(pdfsubject);
425 string pdfkeywords = process_keyval_opt(options, "pdfkeywords");
426 if (!pdfkeywords.empty())
427 h_pdf_keywords = remove_braces(pdfkeywords);
428 if (!options.empty()) {
429 if (!h_pdf_quoted_options.empty())
430 h_pdf_quoted_options += ',';
431 h_pdf_quoted_options += join(options, ",");
437 void handle_package(Parser &p, string const & name, string const & opts,
438 bool in_lyx_preamble)
440 vector<string> options = split_options(opts);
441 add_package(name, options);
445 if (is_known(name, known_roman_fonts)) {
450 if (name == "fourier") {
451 h_font_roman = "utopia";
452 // when font uses real small capitals
453 if (opts == "expert")
457 if (name == "mathpazo")
458 h_font_roman = "palatino";
460 if (name == "mathptmx")
461 h_font_roman = "times";
464 if (is_known(name, known_sans_fonts)) {
468 h_font_sf_scale = scale_as_percentage(scale);
473 if (is_known(name, known_typewriter_fonts)) {
474 h_font_typewriter = name;
477 h_font_tt_scale = scale_as_percentage(scale);
481 // font uses old-style figure
485 else if (name == "amsmath" || name == "amssymb")
488 else if (name == "esint")
491 else if (name == "babel" && !opts.empty()) {
492 // check if more than one option was used - used later for inputenc
493 // in case inputenc is parsed before babel, set the encoding to auto
494 if (options.begin() != options.end() - 1) {
495 one_language = false;
496 h_inputencoding = "auto";
498 // babel takes the last language of the option of its \usepackage
499 // call as document language. If there is no such language option, the
500 // last language in the documentclass options is used.
501 handle_opt(options, known_languages, h_language);
502 delete_opt(options, known_languages);
505 else if (name == "fontenc")
508 else if (name == "inputenc") {
509 // h_inputencoding is only set when there is not more than one
510 // inputenc option because otherwise h_inputencoding must be
511 // set to "auto" (the default encoding of the document language)
512 // Therefore check for the "," character.
513 // It is also only set when there is not more then one babel
514 // language option but this is handled in the routine for babel.
515 if (opts.find(",") == string::npos && one_language == true)
516 h_inputencoding = opts;
517 if (!options.empty())
518 p.setEncoding(options.back());
522 else if (name == "makeidx")
525 else if (name == "prettyref")
528 else if (name == "varioref")
531 else if (name == "verbatim")
534 else if (name == "nomencl")
537 else if (name == "textcomp")
540 else if (name == "url")
543 else if (LYX_FORMAT >= 408 && name == "subscript")
546 else if (name == "color") {
547 // with the following command this package is only loaded when needed for
548 // undefined colors, since we only support the predefined colors
549 h_preamble << "\\@ifundefined{definecolor}\n {\\usepackage{color}}{}\n";
552 else if (name == "graphicx")
555 else if (name == "setspace")
558 else if (name == "geometry")
559 ; // Ignore this, the geometry settings are made by the \geometry
560 // command. This command is handled below.
562 else if (is_known(name, known_languages))
565 else if (name == "natbib") {
566 h_cite_engine = "natbib_authoryear";
567 vector<string>::iterator it =
568 find(options.begin(), options.end(), "authoryear");
569 if (it != options.end())
572 it = find(options.begin(), options.end(), "numbers");
573 if (it != options.end()) {
574 h_cite_engine = "natbib_numerical";
580 else if (name == "jurabib")
581 h_cite_engine = "jurabib";
583 else if (name == "hyperref")
584 handle_hyperref(options);
586 else if (!in_lyx_preamble) {
588 h_preamble << "\\usepackage{" << name << "}";
590 h_preamble << "\\usepackage[" << opts << "]{"
596 // We need to do something with the options...
597 if (!options.empty())
598 cerr << "Ignoring options '" << join(options, ",")
599 << "' of package " << name << '.' << endl;
601 // remove the whitespace
607 void end_preamble(ostream & os, TextClass const & /*textclass*/)
609 // merge synonym languages
610 if (is_known(h_language, known_bahasa_languages))
611 h_language = "bahasa";
612 else if (is_known(h_language, known_bahasam_languages))
613 h_language = "bahasam";
614 else if (is_known(h_language, known_brazilian_languages))
615 h_language = "brazilian";
616 else if (is_known(h_language, known_french_languages))
617 h_language = "french";
618 else if (is_known(h_language, known_german_languages))
619 h_language = "german";
620 else if (is_known(h_language, known_ngerman_languages))
621 h_language = "ngerman";
622 else if (is_known(h_language, known_portuguese_languages))
623 h_language = "portuguese";
624 else if (is_known(h_language, known_russian_languages))
625 h_language = "russian";
626 else if (is_known(h_language, known_ukrainian_languages))
627 h_language = "ukrainian";
629 // set the quote language
630 // LyX only knows the following quotes languages:
631 // english, swedish, german, polish, french and danish
632 // (quotes for "japanese" and "chinese-traditional" are missing because
633 // they wouldn't be useful: http://www.lyx.org/trac/ticket/6383)
634 // conversion list taken from
635 // http://en.wikipedia.org/wiki/Quotation_mark,_non-English_usage
636 // (quotes for kazakh and interlingua are unknown)
638 if (h_language == "danish")
639 h_quotes_language = "danish";
641 else if (is_known(h_language, known_french_quotes_languages)
642 || is_known(h_language, known_french_languages)
643 || is_known(h_language, known_russian_languages)
644 || is_known(h_language, known_ukrainian_languages))
645 h_quotes_language = "french";
647 else if (is_known(h_language, known_german_quotes_languages)
648 || is_known(h_language, known_german_languages)
649 || is_known(h_language, known_ngerman_languages))
650 h_quotes_language = "german";
652 else if (is_known(h_language, known_polish_quotes_languages))
653 h_quotes_language = "polish";
655 else if (is_known(h_language, known_swedish_quotes_languages))
656 h_quotes_language = "swedish";
658 else if (is_known(h_language, known_english_quotes_languages)
659 || is_known(h_language, known_bahasa_languages)
660 || is_known(h_language, known_bahasam_languages)
661 || is_known(h_language, known_brazilian_languages)
662 || is_known(h_language, known_portuguese_languages))
663 h_quotes_language = "english";
665 h_language = babel2lyx(h_language);
667 // output the LyX file settings
668 os << "#LyX file created by tex2lyx " << PACKAGE_VERSION << "\n"
669 << "\\lyxformat " << LYX_FORMAT << '\n'
670 << "\\begin_document\n"
671 << "\\begin_header\n"
672 << "\\textclass " << h_textclass << "\n";
673 if (!h_preamble.str().empty())
674 os << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n";
675 if (!h_options.empty())
676 os << "\\options " << h_options << "\n";
677 os << "\\use_default_options " << h_use_default_options << "\n"
678 << "\\language " << h_language << "\n"
679 << "\\inputencoding " << h_inputencoding << "\n"
680 << "\\font_roman " << h_font_roman << "\n"
681 << "\\font_sans " << h_font_sans << "\n"
682 << "\\font_typewriter " << h_font_typewriter << "\n"
683 << "\\font_default_family " << h_font_default_family << "\n"
684 << "\\font_sc " << h_font_sc << "\n"
685 << "\\font_osf " << h_font_osf << "\n"
686 << "\\font_sf_scale " << h_font_sf_scale << "\n"
687 << "\\font_tt_scale " << h_font_tt_scale << "\n"
688 << "\\graphics " << h_graphics << "\n";
689 if (!h_float_placement.empty())
690 os << "\\float_placement " << h_float_placement << "\n";
691 os << "\\paperfontsize " << h_paperfontsize << "\n"
692 << "\\spacing " << h_spacing << "\n"
693 << "\\use_hyperref " << h_use_hyperref << '\n';
694 if (h_use_hyperref == "1") {
695 if (!h_pdf_title.empty())
696 os << "\\pdf_title \"" << h_pdf_title << "\"\n";
697 if (!h_pdf_author.empty())
698 os << "\\pdf_author \"" << h_pdf_author << "\"\n";
699 if (!h_pdf_subject.empty())
700 os << "\\pdf_subject \"" << h_pdf_subject << "\"\n";
701 if (!h_pdf_keywords.empty())
702 os << "\\pdf_keywords \"" << h_pdf_keywords << "\"\n";
703 os << "\\pdf_bookmarks " << h_pdf_bookmarks << "\n"
704 "\\pdf_bookmarksnumbered " << h_pdf_bookmarksnumbered << "\n"
705 "\\pdf_bookmarksopen " << h_pdf_bookmarksopen << "\n"
706 "\\pdf_bookmarksopenlevel " << h_pdf_bookmarksopenlevel << "\n"
707 "\\pdf_breaklinks " << h_pdf_breaklinks << "\n"
708 "\\pdf_pdfborder " << h_pdf_pdfborder << "\n"
709 "\\pdf_colorlinks " << h_pdf_colorlinks << "\n"
710 "\\pdf_backref " << h_pdf_backref << "\n"
711 "\\pdf_pdfusetitle " << h_pdf_pdfusetitle << '\n';
712 if (!h_pdf_pagemode.empty())
713 os << "\\pdf_pagemode " << h_pdf_pagemode << '\n';
714 if (!h_pdf_quoted_options.empty())
715 os << "\\pdf_quoted_options \"" << h_pdf_quoted_options << "\"\n";
717 os << "\\papersize " << h_papersize << "\n"
718 << "\\use_geometry " << h_use_geometry << "\n"
719 << "\\use_amsmath " << h_use_amsmath << "\n"
720 << "\\use_esint " << h_use_esint << "\n"
721 << "\\cite_engine " << h_cite_engine << "\n"
722 << "\\use_bibtopic " << h_use_bibtopic << "\n"
723 << "\\paperorientation " << h_paperorientation << "\n"
725 << "\\secnumdepth " << h_secnumdepth << "\n"
726 << "\\tocdepth " << h_tocdepth << "\n"
727 << "\\paragraph_separation " << h_paragraph_separation << "\n";
728 if (LYX_FORMAT < 365 || h_paragraph_separation == "skip")
729 os << "\\defskip " << h_defskip << "\n";
731 os << "\\paragraph_indentation " << h_paragraph_indentation << "\n";
732 os << "\\quotes_language " << h_quotes_language << "\n"
733 << "\\papercolumns " << h_papercolumns << "\n"
734 << "\\papersides " << h_papersides << "\n"
735 << "\\paperpagestyle " << h_paperpagestyle << "\n";
736 if (!h_listings_params.empty())
737 os << "\\listings_params " << h_listings_params << "\n";
738 os << "\\tracking_changes " << h_tracking_changes << "\n"
739 << "\\output_changes " << h_output_changes << "\n"
740 << "\\end_header\n\n"
742 // clear preamble for subdocuments
746 } // anonymous namespace
748 void parse_preamble(Parser & p, ostream & os,
749 string const & forceclass, TeX2LyXDocClass & tc)
751 // initialize fixed types
752 special_columns['D'] = 3;
753 bool is_full_document = false;
754 bool is_lyx_file = false;
755 bool in_lyx_preamble = false;
757 // determine whether this is a full document or a fragment for inclusion
759 Token const & t = p.get_token();
761 if (t.cat() == catEscape && t.cs() == "documentclass") {
762 is_full_document = true;
768 while (is_full_document && p.good()) {
769 Token const & t = p.get_token();
772 cerr << "t: " << t << "\n";
778 if (!in_lyx_preamble &&
779 (t.cat() == catLetter ||
780 t.cat() == catSuper ||
782 t.cat() == catOther ||
783 t.cat() == catMath ||
784 t.cat() == catActive ||
785 t.cat() == catBegin ||
787 t.cat() == catAlign ||
788 t.cat() == catParameter))
789 h_preamble << t.cs();
791 else if (!in_lyx_preamble &&
792 (t.cat() == catSpace || t.cat() == catNewline))
793 h_preamble << t.asInput();
795 else if (t.cat() == catComment) {
796 // regex to parse comments (currently not used)
797 static regex const islyxfile("%% LyX .* created this file");
798 static regex const usercommands("User specified LaTeX commands");
800 string const comment = t.asInput();
802 // magically switch encoding default if it looks like XeLaTeX
803 static string const magicXeLaTeX =
804 "% This document must be compiled with XeLaTeX ";
805 if (comment.size() > magicXeLaTeX.size()
806 && comment.substr(0, magicXeLaTeX.size()) == magicXeLaTeX
807 && h_inputencoding == "auto") {
808 cerr << "XeLaTeX comment found, switching to UTF8\n";
809 h_inputencoding = "utf8";
812 if (regex_search(comment, sub, islyxfile)) {
814 in_lyx_preamble = true;
815 } else if (is_lyx_file
816 && regex_search(comment, sub, usercommands))
817 in_lyx_preamble = false;
818 else if (!in_lyx_preamble)
819 h_preamble << t.asInput();
822 else if (t.cs() == "pagestyle")
823 h_paperpagestyle = p.verbatim_item();
825 else if (t.cs() == "makeatletter") {
826 // LyX takes care of this
827 p.setCatCode('@', catLetter);
830 else if (t.cs() == "makeatother") {
831 // LyX takes care of this
832 p.setCatCode('@', catOther);
835 else if (t.cs() == "newcommand" || t.cs() == "renewcommand"
836 || t.cs() == "providecommand"
837 || t.cs() == "DeclareRobustCommand"
838 || t.cs() == "ProvideTextCommandDefault"
839 || t.cs() == "DeclareMathAccent") {
841 if (p.next_token().character() == '*') {
845 string const name = p.verbatim_item();
846 string const opt1 = p.getOpt();
847 string const opt2 = p.getFullOpt();
848 string const body = p.verbatim_item();
850 if (name == "\\rmdefault")
851 if (is_known(body, known_roman_fonts))
853 if (name == "\\sfdefault")
854 if (is_known(body, known_sans_fonts))
856 if (name == "\\ttdefault")
857 if (is_known(body, known_typewriter_fonts))
858 h_font_typewriter = body;
859 if (name == "\\familydefault") {
860 string family = body;
861 // remove leading "\"
862 h_font_default_family = family.erase(0,1);
864 // only non-lyxspecific stuff
865 if (!in_lyx_preamble) {
867 ss << '\\' << t.cs();
870 ss << '{' << name << '}' << opt1 << opt2
871 << '{' << body << "}";
872 h_preamble << ss.str();
874 // Add the command to the known commands
875 add_known_command(name, opt1, !opt2.empty());
877 ostream & out = in_preamble ? h_preamble : os;
878 out << "\\" << t.cs() << "{" << name << "}"
879 << opts << "{" << body << "}";
884 else if (t.cs() == "documentclass") {
885 vector<string>::iterator it;
886 vector<string> opts = split_options(p.getArg('[', ']'));
887 handle_opt(opts, known_fontsizes, h_paperfontsize);
888 delete_opt(opts, known_fontsizes);
889 // delete "pt" at the end
890 string::size_type i = h_paperfontsize.find("pt");
891 if (i != string::npos)
892 h_paperfontsize.erase(i);
893 // The documentclass options are always parsed before the options
894 // of the babel call so that a language cannot overwrite the babel
896 handle_opt(opts, known_languages, h_language);
897 delete_opt(opts, known_languages);
900 if ((it = find(opts.begin(), opts.end(), "landscape")) != opts.end()) {
901 h_paperorientation = "landscape";
905 if ((it = find(opts.begin(), opts.end(), "oneside"))
910 if ((it = find(opts.begin(), opts.end(), "twoside"))
916 if ((it = find(opts.begin(), opts.end(), "onecolumn"))
918 h_papercolumns = "1";
921 if ((it = find(opts.begin(), opts.end(), "twocolumn"))
923 h_papercolumns = "2";
927 // some size options are know to any document classes, other sizes
928 // are handled by the \geometry command of the geometry package
929 handle_opt(opts, known_class_paper_sizes, h_papersize);
930 delete_opt(opts, known_class_paper_sizes);
931 // the remaining options
932 h_options = join(opts, ",");
933 h_textclass = p.getArg('{', '}');
936 else if (t.cs() == "usepackage") {
937 string const options = p.getArg('[', ']');
938 string const name = p.getArg('{', '}');
939 vector<string> vecnames;
940 split(name, vecnames, ',');
941 vector<string>::const_iterator it = vecnames.begin();
942 vector<string>::const_iterator end = vecnames.end();
943 for (; it != end; ++it)
944 handle_package(p, trim(*it), options,
948 else if (t.cs() == "inputencoding") {
949 string const encoding = p.getArg('{','}');
950 h_inputencoding = encoding;
951 p.setEncoding(encoding);
954 else if (t.cs() == "newenvironment") {
955 string const name = p.getArg('{', '}');
957 ss << "\\newenvironment{" << name << "}";
960 ss << '{' << p.verbatim_item() << '}';
961 ss << '{' << p.verbatim_item() << '}';
962 if (!in_lyx_preamble)
963 h_preamble << ss.str();
966 else if (t.cs() == "def") {
967 string name = p.get_token().cs();
968 while (p.next_token().cat() != catBegin)
969 name += p.get_token().cs();
970 if (!in_lyx_preamble)
971 h_preamble << "\\def\\" << name << '{'
972 << p.verbatim_item() << "}";
975 else if (t.cs() == "newcolumntype") {
976 string const name = p.getArg('{', '}');
979 string opts = p.getOpt();
981 istringstream is(string(opts, 1));
984 special_columns[name[0]] = nargs;
985 h_preamble << "\\newcolumntype{" << name << "}";
987 h_preamble << "[" << nargs << "]";
988 h_preamble << "{" << p.verbatim_item() << "}";
991 else if (t.cs() == "setcounter") {
992 string const name = p.getArg('{', '}');
993 string const content = p.getArg('{', '}');
994 if (name == "secnumdepth")
995 h_secnumdepth = content;
996 else if (name == "tocdepth")
997 h_tocdepth = content;
999 h_preamble << "\\setcounter{" << name << "}{" << content << "}";
1002 else if (t.cs() == "setlength") {
1003 string const name = p.verbatim_item();
1004 string const content = p.verbatim_item();
1005 // the paragraphs are only not indented when \parindent is set to zero
1006 if (name == "\\parindent" && content != "") {
1007 if (content[0] == '0')
1008 h_paragraph_separation = "skip";
1009 else if (LYX_FORMAT >= 365)
1010 h_paragraph_indentation = translate_len(content);
1012 h_preamble << "\\setlength{" << name
1013 << "}{" << content << "}";
1014 } else if (name == "\\parskip") {
1015 if (content == "\\smallskipamount")
1016 h_defskip = "smallskip";
1017 else if (content == "\\medskipamount")
1018 h_defskip = "medskip";
1019 else if (content == "\\bigskipamount")
1020 h_defskip = "bigskip";
1022 h_defskip = content;
1024 h_preamble << "\\setlength{" << name << "}{" << content << "}";
1027 else if (t.cs() == "onehalfspacing")
1028 h_spacing = "onehalf";
1030 else if (t.cs() == "doublespacing")
1031 h_spacing = "double";
1033 else if (t.cs() == "setstretch")
1034 h_spacing = "other " + p.verbatim_item();
1036 else if (t.cs() == "begin") {
1037 string const name = p.getArg('{', '}');
1038 if (name == "document")
1040 h_preamble << "\\begin{" << name << "}";
1043 else if (t.cs() == "geometry") {
1044 h_use_geometry = "true";
1045 vector<string> opts = split_options(p.getArg('{', '}'));
1046 vector<string>::iterator it;
1047 // paper orientation
1048 if ((it = find(opts.begin(), opts.end(), "landscape")) != opts.end()) {
1049 h_paperorientation = "landscape";
1053 handle_opt(opts, known_paper_sizes, h_papersize);
1054 delete_opt(opts, known_paper_sizes);
1056 char const * const * margin = known_paper_margins;
1058 for (; *margin; ++margin) {
1060 // search for the "=" in e.g. "lmargin=2cm" to get the value
1061 for(size_t i = 0; i != opts.size(); i++) {
1062 if (opts.at(i).find(*margin) != string::npos) {
1063 string::size_type pos = opts.at(i).find("=");
1064 string value = opts.at(i).substr(pos + 1);
1065 string name = known_coded_paper_margins[k];
1066 h_margins += "\\" + name + " " + value + "\n";
1072 else if (t.cs() == "jurabibsetup") {
1073 // FIXME p.getArg('{', '}') is most probably wrong (it
1074 // does not handle nested braces).
1075 // Use p.verbatim_item() instead.
1076 vector<string> jurabibsetup =
1077 split_options(p.getArg('{', '}'));
1078 // add jurabibsetup to the jurabib package options
1079 add_package("jurabib", jurabibsetup);
1080 if (!jurabibsetup.empty()) {
1081 h_preamble << "\\jurabibsetup{"
1082 << join(jurabibsetup, ",") << '}';
1086 else if (t.cs() == "hypersetup") {
1087 vector<string> hypersetup =
1088 split_options(p.verbatim_item());
1089 // add hypersetup to the hyperref package options
1090 handle_hyperref(hypersetup);
1091 if (!hypersetup.empty()) {
1092 h_preamble << "\\hypersetup{"
1093 << join(hypersetup, ",") << '}';
1097 else if (!t.cs().empty() && !in_lyx_preamble)
1098 h_preamble << '\\' << t.cs();
1101 // remove the whitespace
1104 // Force textclass if the user wanted it
1105 if (!forceclass.empty())
1106 h_textclass = forceclass;
1107 if (noweb_mode && !prefixIs(h_textclass, "literate-"))
1108 h_textclass.insert(0, "literate-");
1109 FileName layoutfilename = libFileSearch("layouts", h_textclass, "layout");
1110 if (layoutfilename.empty()) {
1111 cerr << "Error: Could not find layout file for textclass \"" << h_textclass << "\"." << endl;
1114 tc.read(layoutfilename);
1115 if (h_papersides.empty()) {
1118 h_papersides = ss.str();
1120 end_preamble(os, tc);