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.
19 #include "LayoutFile.h"
22 #include "TextClass.h"
24 #include "support/convert.h"
25 #include "support/FileName.h"
26 #include "support/filetools.h"
27 #include "support/lstrings.h"
29 #include "support/regex.h"
35 using namespace lyx::support;
40 // special columntypes
41 extern map<char, int> special_columns;
47 // "chinese-simplified", "chinese-traditional", "japanese-cjk", "korean"
48 // cannot be supported because it is impossible to determine the correct document
49 // language if CJK is used.
51 * known babel language names (including synonyms)
52 * not in standard babel: arabic, arabtex, armenian, belarusian, serbian-latin, thai
53 * please keep this in sync with known_coded_languages line by line!
55 const char * const known_languages[] = {"acadian", "afrikaans", "albanian",
56 "american", "arabic", "arabtex", "australian", "austrian", "bahasa", "bahasai",
57 "bahasam", "basque", "belarusian", "brazil", "brazilian", "breton", "british",
58 "bulgarian", "canadian", "canadien", "catalan", "croatian", "czech", "danish",
59 "dutch", "english", "esperanto", "estonian", "farsi", "finnish", "francais",
60 "french", "frenchb", "frenchle", "frenchpro", "galician", "german", "germanb",
61 "greek", "hebrew", "hungarian", "icelandic", "indon", "indonesian", "interlingua",
62 "irish", "italian", "japanese", "kazakh", "kurmanji", "latin", "latvian", "lithuanian",
63 "lowersorbian", "lsorbian", "magyar", "malay", "meyalu", "mongolian", "naustrian",
64 "newzealand", "ngerman", "ngermanb", "norsk", "nynorsk", "polutonikogreek", "polish",
65 "portuges", "portuguese", "romanian", "russian", "russianb", "samin",
66 "scottish", "serbian", "serbian-latin", "slovak", "slovene", "spanish",
67 "swedish", "thai", "turkish", "turkmen", "ukraineb", "ukrainian",
68 "uppersorbian", "UKenglish", "USenglish", "usorbian", "vietnam", "welsh",
72 * the same as known_languages with .lyx names
73 * please keep this in sync with known_languages line by line!
75 const char * const known_coded_languages[] = {"french", "afrikaans", "albanian",
76 "american", "arabic_arabi", "arabic_arabtex", "australian", "austrian", "bahasa", "bahasa",
77 "bahasam", "basque", "belarusian", "brazilian", "brazilian", "breton", "british",
78 "bulgarian", "canadian", "canadien", "catalan", "croatian", "czech", "danish",
79 "dutch", "english", "esperanto", "estonian", "farsi", "finnish", "french",
80 "french", "french", "french", "french", "galician", "german", "german",
81 "greek", "hebrew", "magyar", "icelandic", "bahasa", "bahasa", "interlingua",
82 "irish", "italian", "japanese", "kazakh", "kurmanji", "latin", "latvian", "lithuanian",
83 "lowersorbian", "lowersorbian", "magyar", "bahasam", "bahasam", "mongolian", "naustrian",
84 "newzealand", "ngerman", "ngerman", "norsk", "nynorsk", "polutonikogreek", "polish",
85 "portuguese", "portuguese", "romanian", "russian", "russian", "samin",
86 "scottish", "serbian", "serbian-latin", "slovak", "slovene", "spanish",
87 "swedish", "thai", "turkish", "turkmen", "ukrainian", "ukrainian",
88 "uppersorbian", "uppersorbian", "english", "english", "vietnamese", "welsh",
92 * known polyglossia language names (including variants)
94 const char * const polyglossia_languages[] = {
95 "albanian", "croatian", "hebrew", "norsk", "swedish", "amharic", "czech", "hindi",
96 "nynorsk", "syriac", "arabic", "danish", "icelandic", "occitan", "tamil",
97 "armenian", "divehi", "interlingua", "polish", "telugu", "asturian", "dutch",
98 "irish", "portuges", "thai", "bahasai", "english", "italian", "romanian", "turkish",
99 "bahasam", "esperanto", "lao", "russian", "turkmen", "basque", "estonian", "latin",
100 "samin", "ukrainian", "bengali", "farsi", "latvian", "sanskrit", "urdu", "brazil",
101 "brazilian", "finnish", "lithuanian", "scottish", "usorbian", "breton", "french",
102 "lsorbian", "serbian", "vietnamese", "bulgarian", "galician", "magyar", "slovak",
103 "welsh", "catalan", "german", "malayalam", "slovenian", "coptic", "greek",
104 "marathi", "spanish",
105 "american", "ancient", "australian", "british", "monotonic", "newzealand",
109 * the same as polyglossia_languages with .lyx names
110 * please keep this in sync with polyglossia_languages line by line!
112 const char * const coded_polyglossia_languages[] = {
113 "albanian", "croatian", "hebrew", "norsk", "swedish", "amharic", "czech", "hindi",
114 "nynorsk", "syriac", "arabic_arabi", "danish", "icelandic", "occitan", "tamil",
115 "armenian", "divehi", "interlingua", "polish", "telugu", "asturian", "dutch",
116 "irish", "portuges", "thai", "bahasa", "english", "italian", "romanian", "turkish",
117 "bahasam", "esperanto", "lao", "russian", "turkmen", "basque", "estonian", "latin",
118 "samin", "ukrainian", "bengali", "farsi", "latvian", "sanskrit", "urdu", "brazilian",
119 "brazilian", "finnish", "lithuanian", "scottish", "uppersorbian", "breton", "french",
120 "lowersorbian", "serbian", "vietnamese", "bulgarian", "galician", "magyar", "slovak",
121 "welsh", "catalan", "ngerman", "malayalam", "slovene", "coptic", "greek",
122 "marathi", "spanish",
123 "american", "ancientgreek", "australian", "british", "greek", "newzealand",
124 "polutonikogreek", 0};
126 /// languages with english quotes (.lyx names)
127 const char * const known_english_quotes_languages[] = {"american", "australian",
128 "bahasa", "bahasam", "brazilian", "canadian", "chinese-simplified", "english",
129 "esperanto", "hebrew", "irish", "korean", "newzealand", "portuguese", "scottish",
132 /// languages with french quotes (.lyx names)
133 const char * const known_french_quotes_languages[] = {"albanian",
134 "arabic_arabi", "arabic_arabtex", "basque", "canadien", "catalan", "french",
135 "galician", "greek", "italian", "norsk", "nynorsk", "polutonikogreek",
136 "russian", "spanish", "spanish-mexico", "turkish", "turkmen", "ukrainian",
139 /// languages with german quotes (.lyx names)
140 const char * const known_german_quotes_languages[] = {"austrian", "bulgarian",
141 "czech", "german", "icelandic", "lithuanian", "lowersorbian", "naustrian",
142 "ngerman", "serbian", "serbian-latin", "slovak", "slovene", "uppersorbian", 0};
144 /// languages with polish quotes (.lyx names)
145 const char * const known_polish_quotes_languages[] = {"afrikaans", "croatian",
146 "dutch", "estonian", "magyar", "polish", "romanian", 0};
148 /// languages with swedish quotes (.lyx names)
149 const char * const known_swedish_quotes_languages[] = {"finnish",
152 /// known language packages from the times before babel
153 const char * const known_old_language_packages[] = {"french", "frenchle",
154 "frenchpro", "german", "ngerman", "pmfrench", 0};
156 char const * const known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
158 const char * const known_roman_fonts[] = { "ae", "beraserif", "bookman",
159 "ccfonts", "chancery", "charter", "cmr", "fourier", "lmodern", "mathpazo",
160 "mathptmx", "newcent", "utopia", 0};
162 const char * const known_sans_fonts[] = { "avant", "berasans", "cmbr", "cmss",
163 "helvet", "lmss", 0};
165 const char * const known_typewriter_fonts[] = { "beramono", "cmtl", "cmtt",
166 "courier", "lmtt", "luximono", "fourier", "lmodern", "mathpazo", "mathptmx",
169 const char * const known_paper_sizes[] = { "a0paper", "b0paper", "c0paper",
170 "a1paper", "b1paper", "c1paper", "a2paper", "b2paper", "c2paper", "a3paper",
171 "b3paper", "c3paper", "a4paper", "b4paper", "c4paper", "a5paper", "b5paper",
172 "c5paper", "a6paper", "b6paper", "c6paper", "executivepaper", "legalpaper",
173 "letterpaper", "b0j", "b1j", "b2j", "b3j", "b4j", "b5j", "b6j", 0};
175 const char * const known_class_paper_sizes[] = { "a4paper", "a5paper",
176 "executivepaper", "legalpaper", "letterpaper", 0};
178 const char * const known_paper_margins[] = { "lmargin", "tmargin", "rmargin",
179 "bmargin", "headheight", "headsep", "footskip", "columnsep", 0};
181 const char * const known_coded_paper_margins[] = { "leftmargin", "topmargin",
182 "rightmargin", "bottommargin", "headheight", "headsep", "footskip",
185 /// commands that can start an \if...\else...\endif sequence
186 const char * const known_if_commands[] = {"if", "ifarydshln", "ifbraket",
187 "ifcancel", "ifcolortbl", "ifeurosym", "ifmarginnote", "ifmmode", "ifpdf",
188 "ifsidecap", "ifupgreek", 0};
190 const char * const known_basic_colors[] = {"blue", "black", "cyan", "green",
191 "magenta", "red", "white", "yellow", 0};
193 const char * const known_basic_color_codes[] = {"#0000ff", "#000000", "#00ffff", "#00ff00",
194 "#ff00ff", "#ff0000", "#ffffff", "#ffff00", 0};
196 /// conditional commands with three arguments like \@ifundefined{}{}{}
197 const char * const known_if_3arg_commands[] = {"@ifundefined", "IfFileExists",
200 /// packages that work only in xetex
201 /// polyglossia is handled separately
202 const char * const known_xetex_packages[] = {"arabxetex", "fixlatvian",
203 "fontbook", "fontwrap", "mathspec", "philokalia", "unisugar",
204 "xeCJK", "xecolor", "xecyr", "xeindex", "xepersian", "xunicode", 0};
206 /// packages that are automatically skipped if loaded by LyX
207 const char * const known_lyx_packages[] = {"amsbsy", "amsmath", "amssymb",
208 "amstext", "amsthm", "array", "babel", "booktabs", "calc", "CJK", "color", "float",
209 "fontspec", "graphicx", "hhline", "ifthen", "longtable", "makeidx", "multirow",
210 "nomencl", "pdfpages", "rotating", "rotfloat", "splitidx", "setspace",
211 "subscript", "textcomp", "ulem", "url", "varioref", "verbatim", "wrapfig",
214 // codes used to remove packages that are loaded automatically by LyX.
215 // Syntax: package_beg_sep<name>package_mid_sep<package loading code>package_end_sep
216 const char package_beg_sep = '\001';
217 const char package_mid_sep = '\002';
218 const char package_end_sep = '\003';
221 // returns true if at least one of the options in what has been found
222 bool handle_opt(vector<string> & opts, char const * const * what, string & target)
228 // the last language option is the document language (for babel and LyX)
229 // the last size option is the document font size
230 vector<string>::iterator it;
231 vector<string>::iterator position = opts.begin();
232 for (; *what; ++what) {
233 it = find(opts.begin(), opts.end(), *what);
234 if (it != opts.end()) {
235 if (it >= position) {
246 void delete_opt(vector<string> & opts, char const * const * what)
251 // remove found options from the list
252 // do this after handle_opt to avoid potential memory leaks
253 vector<string>::iterator it;
254 for (; *what; ++what) {
255 it = find(opts.begin(), opts.end(), *what);
256 if (it != opts.end())
263 * Split a package options string (keyval format) into a vector.
265 * authorformat=smallcaps,
267 * titleformat=colonsep,
268 * bibformat={tabular,ibidem,numbered}
270 vector<string> split_options(string const & input)
272 vector<string> options;
276 Token const & t = p.get_token();
277 if (t.asInput() == ",") {
278 options.push_back(trimSpaceAndEol(option));
280 } else if (t.asInput() == "=") {
283 if (p.next_token().asInput() == "{")
284 option += '{' + p.getArg('{', '}') + '}';
285 } else if (t.cat() != catSpace)
286 option += t.asInput();
290 options.push_back(trimSpaceAndEol(option));
297 * Retrieve a keyval option "name={value with=sign}" named \p name from
298 * \p options and return the value.
299 * The found option is also removed from \p options.
301 string process_keyval_opt(vector<string> & options, string name)
303 for (size_t i = 0; i < options.size(); ++i) {
304 vector<string> option;
305 split(options[i], option, '=');
306 if (option.size() < 2)
308 if (option[0] == name) {
309 options.erase(options.begin() + i);
310 option.erase(option.begin());
311 return join(option, "=");
317 } // anonymous namespace
320 bool Preamble::indentParagraphs() const
322 return h_paragraph_separation == "indent";
326 bool Preamble::isPackageUsed(string const & package) const
328 return used_packages.find(package) != used_packages.end();
332 vector<string> Preamble::getPackageOptions(string const & package) const
334 map<string, vector<string> >::const_iterator it = used_packages.find(package);
335 if (it != used_packages.end())
337 return vector<string>();
341 void Preamble::registerAutomaticallyLoadedPackage(std::string const & package)
343 auto_packages.insert(package);
347 void Preamble::addModule(string const & module)
349 used_modules.push_back(module);
353 void Preamble::suppressDate(bool suppress)
356 h_suppress_date = "true";
358 h_suppress_date = "false";
362 void Preamble::registerAuthor(std::string const & name)
364 Author author(from_utf8(name), empty_docstring());
365 author.setUsed(true);
366 authors_.record(author);
367 h_tracking_changes = "true";
368 h_output_changes = "true";
372 Author const & Preamble::getAuthor(std::string const & name) const
374 Author author(from_utf8(name), empty_docstring());
375 for (AuthorList::Authors::const_iterator it = authors_.begin();
376 it != authors_.end(); ++it)
379 static Author const dummy;
384 void Preamble::add_package(string const & name, vector<string> & options)
386 // every package inherits the global options
387 if (used_packages.find(name) == used_packages.end())
388 used_packages[name] = split_options(h_options);
390 vector<string> & v = used_packages[name];
391 v.insert(v.end(), options.begin(), options.end());
392 if (name == "jurabib") {
393 // Don't output the order argument (see the cite command
394 // handling code in text.cpp).
395 vector<string>::iterator end =
396 remove(options.begin(), options.end(), "natbiborder");
397 end = remove(options.begin(), end, "jurabiborder");
398 options.erase(end, options.end());
405 // Given is a string like "scaled=0.9", return 0.9 * 100
406 string const scale_as_percentage(string const & scale)
408 string::size_type pos = scale.find('=');
409 if (pos != string::npos) {
410 string value = scale.substr(pos + 1);
412 return convert<string>(100 * convert<double>(value));
414 // If the input string didn't match our expectations.
415 // return the default value "100"
420 string remove_braces(string const & value)
424 if (value[0] == '{' && value[value.length()-1] == '}')
425 return value.substr(1, value.length()-2);
429 } // anonymous namespace
432 Preamble::Preamble() : one_language(true), title_layout_found(false)
436 h_biblio_style = "plain";
437 h_bibtex_command = "default";
438 h_cite_engine = "basic";
439 h_cite_engine_type = "numerical";
441 h_defskip = "medskip";
444 h_fontencoding = "default";
445 h_font_roman = "default";
446 h_font_sans = "default";
447 h_font_typewriter = "default";
448 h_font_default_family = "default";
449 h_use_non_tex_fonts = "false";
451 h_font_osf = "false";
452 h_font_sf_scale = "100";
453 h_font_tt_scale = "100";
454 h_graphics = "default";
455 h_default_output_format = "default";
456 h_html_be_strict = "false";
457 h_html_css_as_file = "0";
458 h_html_math_output = "0";
460 h_index_command = "default";
461 h_inputencoding = "auto";
462 h_justification = "true";
463 h_language = "english";
464 h_language_package = "none";
466 h_maintain_unincluded_children = "false";
470 h_output_changes = "false";
472 //h_output_sync_macro
473 h_papercolumns = "1";
474 h_paperfontsize = "default";
475 h_paperorientation = "portrait";
476 h_paperpagestyle = "default";
478 h_papersize = "default";
479 h_paragraph_indentation = "default";
480 h_paragraph_separation = "indent";
485 h_pdf_bookmarks = "1";
486 h_pdf_bookmarksnumbered = "0";
487 h_pdf_bookmarksopen = "0";
488 h_pdf_bookmarksopenlevel = "1";
489 h_pdf_breaklinks = "0";
490 h_pdf_pdfborder = "0";
491 h_pdf_colorlinks = "0";
492 h_pdf_backref = "section";
493 h_pdf_pdfusetitle = "1";
495 //h_pdf_quoted_options;
496 h_quotes_language = "english";
499 h_spacing = "single";
500 h_suppress_date = "false";
501 h_textclass = "article";
503 h_tracking_changes = "false";
504 h_use_bibtopic = "false";
505 h_use_indices = "false";
506 h_use_geometry = "false";
507 h_use_default_options = "false";
508 h_use_hyperref = "0";
509 h_use_refstyle = "1";
510 h_use_packages["amsmath"] = "1";
511 h_use_packages["amssymb"] = "1";
512 h_use_packages["esint"] = "1";
513 h_use_packages["mhchem"] = "1";
514 h_use_packages["mathdots"] = "1";
515 h_use_packages["mathtools"] = "1";
516 h_use_packages["undertilde"] = "1";
520 void Preamble::handle_hyperref(vector<string> & options)
522 // FIXME swallow inputencoding changes that might surround the
523 // hyperref setup if it was written by LyX
524 h_use_hyperref = "1";
525 // swallow "unicode=true", since LyX does always write that
526 vector<string>::iterator it =
527 find(options.begin(), options.end(), "unicode=true");
528 if (it != options.end())
530 it = find(options.begin(), options.end(), "pdfusetitle");
531 if (it != options.end()) {
532 h_pdf_pdfusetitle = "1";
535 string bookmarks = process_keyval_opt(options, "bookmarks");
536 if (bookmarks == "true")
537 h_pdf_bookmarks = "1";
538 else if (bookmarks == "false")
539 h_pdf_bookmarks = "0";
540 if (h_pdf_bookmarks == "1") {
541 string bookmarksnumbered =
542 process_keyval_opt(options, "bookmarksnumbered");
543 if (bookmarksnumbered == "true")
544 h_pdf_bookmarksnumbered = "1";
545 else if (bookmarksnumbered == "false")
546 h_pdf_bookmarksnumbered = "0";
547 string bookmarksopen =
548 process_keyval_opt(options, "bookmarksopen");
549 if (bookmarksopen == "true")
550 h_pdf_bookmarksopen = "1";
551 else if (bookmarksopen == "false")
552 h_pdf_bookmarksopen = "0";
553 if (h_pdf_bookmarksopen == "1") {
554 string bookmarksopenlevel =
555 process_keyval_opt(options, "bookmarksopenlevel");
556 if (!bookmarksopenlevel.empty())
557 h_pdf_bookmarksopenlevel = bookmarksopenlevel;
560 string breaklinks = process_keyval_opt(options, "breaklinks");
561 if (breaklinks == "true")
562 h_pdf_breaklinks = "1";
563 else if (breaklinks == "false")
564 h_pdf_breaklinks = "0";
565 string pdfborder = process_keyval_opt(options, "pdfborder");
566 if (pdfborder == "{0 0 0}")
567 h_pdf_pdfborder = "1";
568 else if (pdfborder == "{0 0 1}")
569 h_pdf_pdfborder = "0";
570 string backref = process_keyval_opt(options, "backref");
571 if (!backref.empty())
572 h_pdf_backref = backref;
573 string colorlinks = process_keyval_opt(options, "colorlinks");
574 if (colorlinks == "true")
575 h_pdf_colorlinks = "1";
576 else if (colorlinks == "false")
577 h_pdf_colorlinks = "0";
578 string pdfpagemode = process_keyval_opt(options, "pdfpagemode");
579 if (!pdfpagemode.empty())
580 h_pdf_pagemode = pdfpagemode;
581 string pdftitle = process_keyval_opt(options, "pdftitle");
582 if (!pdftitle.empty()) {
583 h_pdf_title = remove_braces(pdftitle);
585 string pdfauthor = process_keyval_opt(options, "pdfauthor");
586 if (!pdfauthor.empty()) {
587 h_pdf_author = remove_braces(pdfauthor);
589 string pdfsubject = process_keyval_opt(options, "pdfsubject");
590 if (!pdfsubject.empty())
591 h_pdf_subject = remove_braces(pdfsubject);
592 string pdfkeywords = process_keyval_opt(options, "pdfkeywords");
593 if (!pdfkeywords.empty())
594 h_pdf_keywords = remove_braces(pdfkeywords);
595 if (!options.empty()) {
596 if (!h_pdf_quoted_options.empty())
597 h_pdf_quoted_options += ',';
598 h_pdf_quoted_options += join(options, ",");
604 void Preamble::handle_geometry(vector<string> & options)
606 h_use_geometry = "true";
607 vector<string>::iterator it;
609 if ((it = find(options.begin(), options.end(), "landscape")) != options.end()) {
610 h_paperorientation = "landscape";
614 // keyval version: "paper=letter"
615 string paper = process_keyval_opt(options, "paper");
617 h_papersize = paper + "paper";
618 // alternative version: "letterpaper"
619 handle_opt(options, known_paper_sizes, h_papersize);
620 delete_opt(options, known_paper_sizes);
622 char const * const * margin = known_paper_margins;
623 for (; *margin; ++margin) {
624 string value = process_keyval_opt(options, *margin);
625 if (!value.empty()) {
626 int k = margin - known_paper_margins;
627 string name = known_coded_paper_margins[k];
628 h_margins += '\\' + name + ' ' + value + '\n';
634 void Preamble::handle_package(Parser &p, string const & name,
635 string const & opts, bool in_lyx_preamble)
637 vector<string> options = split_options(opts);
638 add_package(name, options);
641 if (is_known(name, known_xetex_packages)) {
643 h_use_non_tex_fonts = "true";
644 registerAutomaticallyLoadedPackage("fontspec");
645 if (h_inputencoding == "auto")
646 p.setEncoding("utf8");
650 if (is_known(name, known_roman_fonts)) {
655 if (name == "fourier") {
656 h_font_roman = "utopia";
657 // when font uses real small capitals
658 if (opts == "expert")
662 else if (name == "mathpazo")
663 h_font_roman = "palatino";
665 else if (name == "mathptmx")
666 h_font_roman = "times";
669 if (is_known(name, known_sans_fonts)) {
673 h_font_sf_scale = scale_as_percentage(scale);
678 if (is_known(name, known_typewriter_fonts)) {
679 // fourier can be set as roman font _only_
680 // fourier as typewriter is handled in handling of \ttdefault
681 if (name != "fourier") {
682 h_font_typewriter = name;
685 h_font_tt_scale = scale_as_percentage(scale);
690 // font uses old-style figure
694 // after the detection and handling of special cases, we can remove the
695 // fonts, otherwise they would appear in the preamble, see bug #7856
696 if (is_known(name, known_roman_fonts) || is_known(name, known_sans_fonts)
697 || is_known(name, known_typewriter_fonts))
700 else if (name == "amsmath" || name == "amssymb" ||
701 name == "esint" || name == "mhchem" || name == "mathdots" ||
702 name == "mathtools" || name == "undertilde")
703 h_use_packages[name] = "2";
705 else if (name == "babel") {
706 h_language_package = "default";
707 // One might think we would have to do nothing if babel is loaded
708 // without any options to prevent pollution of the preamble with this
709 // babel call in every roundtrip.
710 // But the user could have defined babel-specific things afterwards. So
711 // we need to keep it in the preamble to prevent cases like bug #7861.
713 // check if more than one option was used - used later for inputenc
714 if (options.begin() != options.end() - 1)
715 one_language = false;
716 // babel takes the last language of the option of its \usepackage
717 // call as document language. If there is no such language option, the
718 // last language in the documentclass options is used.
719 handle_opt(options, known_languages, h_language);
720 // translate the babel name to a LyX name
721 h_language = babel2lyx(h_language);
722 // for Japanese we assume EUC-JP as encoding
723 // but we cannot determine the exact encoding and thus output also a note
724 if (h_language == "japanese") {
725 h_inputencoding = "euc";
726 p.setEncoding("EUC-JP");
727 is_nonCJKJapanese = true;
728 // in this case babel can be removed from the preamble
729 registerAutomaticallyLoadedPackage("babel");
731 // If babel is called with options, LyX puts them by default into the
732 // document class options. This works for most languages, except
733 // for Latvian, Lithuanian, Mongolian, Turkmen and Vietnamese and
734 // perhaps in future others.
735 // Therefore keep the babel call as it is as the user might have
737 h_preamble << "\\usepackage[" << opts << "]{babel}\n";
739 delete_opt(options, known_languages);
742 h_preamble << "\\usepackage{babel}\n";
745 else if (name == "polyglossia") {
746 h_language_package = "default";
747 h_default_output_format = "pdf4";
748 h_use_non_tex_fonts = "true";
750 registerAutomaticallyLoadedPackage("xunicode");
751 if (h_inputencoding == "auto")
752 p.setEncoding("utf8");
755 else if (name == "CJK") {
756 // It is impossible to determine the document language if CJK is used.
757 // All we can do is to notify the user that he has to set this by hisself.
759 // set the encoding to "auto" because it might be set to "default" by the babel handling
760 // and this would not be correct for CJK
761 h_inputencoding = "auto";
762 registerAutomaticallyLoadedPackage("CJK");
765 else if (name == "fontenc") {
766 h_fontencoding = getStringFromVector(options, ",");
767 /* We could do the following for better round trip support,
768 * but this makes the document less portable, so I skip it:
769 if (h_fontencoding == lyxrc.fontenc)
770 h_fontencoding = "global";
775 else if (name == "inputenc" || name == "luainputenc") {
776 // h_inputencoding is only set when there is not more than one
777 // inputenc option because otherwise h_inputencoding must be
778 // set to "auto" (the default encoding of the document language)
779 // Therefore check for the "," character.
780 // It is also only set when there is not more than one babel
782 if (opts.find(",") == string::npos && one_language == true)
783 h_inputencoding = opts;
784 if (!options.empty())
785 p.setEncoding(options.back());
789 else if (name == "srcltx") {
792 h_output_sync_macro = "\\usepackage[" + opts + "]{srcltx}";
795 h_output_sync_macro = "\\usepackage{srcltx}";
798 else if (is_known(name, known_old_language_packages)) {
799 // known language packages from the times before babel
800 // if they are found and not also babel, they will be used as
801 // custom language package
802 h_language_package = "\\usepackage{" + name + "}";
805 else if (name == "prettyref")
806 ; // ignore this FIXME: Use the package separator mechanism instead
808 else if (name == "lyxskak") {
809 // ignore this and its options
810 const char * const o[] = {"ps", "mover", 0};
811 delete_opt(options, o);
814 else if (is_known(name, known_lyx_packages) && options.empty()) {
815 if (name == "splitidx")
816 h_use_indices = "true";
817 if (!in_lyx_preamble)
818 h_preamble << package_beg_sep << name
819 << package_mid_sep << "\\usepackage{"
820 << name << "}\n" << package_end_sep;
823 else if (name == "geometry")
824 handle_geometry(options);
826 else if (name == "subfig")
827 ; // ignore this FIXME: Use the package separator mechanism instead
829 else if (is_known(name, known_languages))
832 else if (name == "natbib") {
833 h_biblio_style = "plainnat";
834 h_cite_engine = "natbib";
835 h_cite_engine_type = "authoryear";
836 vector<string>::iterator it =
837 find(options.begin(), options.end(), "authoryear");
838 if (it != options.end())
841 it = find(options.begin(), options.end(), "numbers");
842 if (it != options.end()) {
843 h_cite_engine_type = "numerical";
849 else if (name == "jurabib") {
850 h_biblio_style = "jurabib";
851 h_cite_engine = "jurabib";
852 h_cite_engine_type = "authoryear";
855 else if (name == "hyperref")
856 handle_hyperref(options);
858 else if (!in_lyx_preamble) {
860 h_preamble << "\\usepackage{" << name << "}\n";
862 h_preamble << "\\usepackage[" << opts << "]{"
868 // We need to do something with the options...
869 if (!options.empty())
870 cerr << "Ignoring options '" << join(options, ",")
871 << "' of package " << name << '.' << endl;
873 // remove the whitespace
878 void Preamble::handle_if(Parser & p, bool in_lyx_preamble)
881 Token t = p.get_token();
882 if (t.cat() == catEscape &&
883 is_known(t.cs(), known_if_commands))
884 handle_if(p, in_lyx_preamble);
886 if (!in_lyx_preamble)
887 h_preamble << t.asInput();
888 if (t.cat() == catEscape && t.cs() == "fi")
895 bool Preamble::writeLyXHeader(ostream & os, bool subdoc)
897 // set the quote language
898 // LyX only knows the following quotes languages:
899 // english, swedish, german, polish, french and danish
900 // (quotes for "japanese" and "chinese-traditional" are missing because
901 // they wouldn't be useful: http://www.lyx.org/trac/ticket/6383)
902 // conversion list taken from
903 // http://en.wikipedia.org/wiki/Quotation_mark,_non-English_usage
904 // (quotes for kazakh and interlingua are unknown)
906 if (h_language == "danish")
907 h_quotes_language = "danish";
909 else if (is_known(h_language, known_french_quotes_languages))
910 h_quotes_language = "french";
912 else if (is_known(h_language, known_german_quotes_languages))
913 h_quotes_language = "german";
915 else if (is_known(h_language, known_polish_quotes_languages))
916 h_quotes_language = "polish";
918 else if (is_known(h_language, known_swedish_quotes_languages))
919 h_quotes_language = "swedish";
921 else if (is_known(h_language, known_english_quotes_languages))
922 h_quotes_language = "english";
924 if (contains(h_float_placement, "H"))
925 registerAutomaticallyLoadedPackage("float");
926 if (h_spacing != "single" && h_spacing != "default")
927 registerAutomaticallyLoadedPackage("setspace");
928 if (h_use_packages["amsmath"] == "2") {
929 // amsbsy and amstext are already provided by amsmath
930 registerAutomaticallyLoadedPackage("amsbsy");
931 registerAutomaticallyLoadedPackage("amstext");
934 // output the LyX file settings
935 os << "#LyX file created by tex2lyx " << PACKAGE_VERSION << "\n"
936 << "\\lyxformat " << LYX_FORMAT << '\n'
937 << "\\begin_document\n"
938 << "\\begin_header\n"
939 << "\\textclass " << h_textclass << "\n";
940 string const raw = subdoc ? empty_string() : h_preamble.str();
942 os << "\\begin_preamble\n";
943 for (string::size_type i = 0; i < raw.size(); ++i) {
944 if (raw[i] == package_beg_sep) {
945 // Here follows some package loading code that
946 // must be skipped if the package is loaded
948 string::size_type j = raw.find(package_mid_sep, i);
949 if (j == string::npos)
951 string::size_type k = raw.find(package_end_sep, j);
952 if (k == string::npos)
954 string const package = raw.substr(i + 1, j - i - 1);
955 string const replacement = raw.substr(j + 1, k - j - 1);
956 if (auto_packages.find(package) == auto_packages.end())
962 os << "\n\\end_preamble\n";
964 if (!h_options.empty())
965 os << "\\options " << h_options << "\n";
966 os << "\\use_default_options " << h_use_default_options << "\n";
967 if (!used_modules.empty()) {
968 os << "\\begin_modules\n";
969 vector<string>::const_iterator const end = used_modules.end();
970 vector<string>::const_iterator it = used_modules.begin();
971 for (; it != end; ++it)
973 os << "\\end_modules\n";
975 os << "\\maintain_unincluded_children " << h_maintain_unincluded_children << "\n"
976 << "\\language " << h_language << "\n"
977 << "\\language_package " << h_language_package << "\n"
978 << "\\inputencoding " << h_inputencoding << "\n"
979 << "\\fontencoding " << h_fontencoding << "\n"
980 << "\\font_roman " << h_font_roman << "\n"
981 << "\\font_sans " << h_font_sans << "\n"
982 << "\\font_typewriter " << h_font_typewriter << "\n"
983 << "\\font_default_family " << h_font_default_family << "\n"
984 << "\\use_non_tex_fonts " << h_use_non_tex_fonts << "\n"
985 << "\\font_sc " << h_font_sc << "\n"
986 << "\\font_osf " << h_font_osf << "\n"
987 << "\\font_sf_scale " << h_font_sf_scale << "\n"
988 << "\\font_tt_scale " << h_font_tt_scale << "\n"
989 << "\\graphics " << h_graphics << "\n"
990 << "\\default_output_format " << h_default_output_format << "\n"
991 << "\\output_sync " << h_output_sync << "\n";
992 if (h_output_sync == "1")
993 os << "\\output_sync_macro \"" << h_output_sync_macro << "\"\n";
994 os << "\\bibtex_command " << h_bibtex_command << "\n"
995 << "\\index_command " << h_index_command << "\n";
996 if (!h_float_placement.empty())
997 os << "\\float_placement " << h_float_placement << "\n";
998 os << "\\paperfontsize " << h_paperfontsize << "\n"
999 << "\\spacing " << h_spacing << "\n"
1000 << "\\use_hyperref " << h_use_hyperref << '\n';
1001 if (h_use_hyperref == "1") {
1002 if (!h_pdf_title.empty())
1003 os << "\\pdf_title \"" << h_pdf_title << "\"\n";
1004 if (!h_pdf_author.empty())
1005 os << "\\pdf_author \"" << h_pdf_author << "\"\n";
1006 if (!h_pdf_subject.empty())
1007 os << "\\pdf_subject \"" << h_pdf_subject << "\"\n";
1008 if (!h_pdf_keywords.empty())
1009 os << "\\pdf_keywords \"" << h_pdf_keywords << "\"\n";
1010 os << "\\pdf_bookmarks " << h_pdf_bookmarks << "\n"
1011 "\\pdf_bookmarksnumbered " << h_pdf_bookmarksnumbered << "\n"
1012 "\\pdf_bookmarksopen " << h_pdf_bookmarksopen << "\n"
1013 "\\pdf_bookmarksopenlevel " << h_pdf_bookmarksopenlevel << "\n"
1014 "\\pdf_breaklinks " << h_pdf_breaklinks << "\n"
1015 "\\pdf_pdfborder " << h_pdf_pdfborder << "\n"
1016 "\\pdf_colorlinks " << h_pdf_colorlinks << "\n"
1017 "\\pdf_backref " << h_pdf_backref << "\n"
1018 "\\pdf_pdfusetitle " << h_pdf_pdfusetitle << '\n';
1019 if (!h_pdf_pagemode.empty())
1020 os << "\\pdf_pagemode " << h_pdf_pagemode << '\n';
1021 if (!h_pdf_quoted_options.empty())
1022 os << "\\pdf_quoted_options \"" << h_pdf_quoted_options << "\"\n";
1024 os << "\\papersize " << h_papersize << "\n"
1025 << "\\use_geometry " << h_use_geometry << '\n';
1026 for (map<string, string>::const_iterator it = h_use_packages.begin();
1027 it != h_use_packages.end(); ++it)
1028 os << "\\use_package " << it->first << ' ' << it->second << '\n';
1029 os << "\\cite_engine " << h_cite_engine << '\n'
1030 << "\\cite_engine_type " << h_cite_engine_type << '\n'
1031 << "\\biblio_style " << h_biblio_style << "\n"
1032 << "\\use_bibtopic " << h_use_bibtopic << "\n"
1033 << "\\use_indices " << h_use_indices << "\n"
1034 << "\\paperorientation " << h_paperorientation << '\n'
1035 << "\\suppress_date " << h_suppress_date << '\n'
1036 << "\\justification " << h_justification << '\n'
1037 << "\\use_refstyle " << h_use_refstyle << '\n';
1038 if (!h_fontcolor.empty())
1039 os << "\\fontcolor " << h_fontcolor << '\n';
1040 if (!h_notefontcolor.empty())
1041 os << "\\notefontcolor " << h_notefontcolor << '\n';
1042 if (!h_backgroundcolor.empty())
1043 os << "\\backgroundcolor " << h_backgroundcolor << '\n';
1044 if (!h_boxbgcolor.empty())
1045 os << "\\boxbgcolor " << h_boxbgcolor << '\n';
1046 os << "\\index " << h_index << '\n'
1047 << "\\shortcut " << h_shortcut << '\n'
1048 << "\\color " << h_color << '\n'
1051 << "\\secnumdepth " << h_secnumdepth << "\n"
1052 << "\\tocdepth " << h_tocdepth << "\n"
1053 << "\\paragraph_separation " << h_paragraph_separation << "\n";
1054 if (h_paragraph_separation == "skip")
1055 os << "\\defskip " << h_defskip << "\n";
1057 os << "\\paragraph_indentation " << h_paragraph_indentation << "\n";
1058 os << "\\quotes_language " << h_quotes_language << "\n"
1059 << "\\papercolumns " << h_papercolumns << "\n"
1060 << "\\papersides " << h_papersides << "\n"
1061 << "\\paperpagestyle " << h_paperpagestyle << "\n";
1062 if (!h_listings_params.empty())
1063 os << "\\listings_params " << h_listings_params << "\n";
1064 os << "\\tracking_changes " << h_tracking_changes << "\n"
1065 << "\\output_changes " << h_output_changes << "\n"
1066 << "\\html_math_output " << h_html_math_output << "\n"
1067 << "\\html_css_as_file " << h_html_css_as_file << "\n"
1068 << "\\html_be_strict " << h_html_be_strict << "\n"
1070 << "\\end_header\n\n"
1071 << "\\begin_body\n";
1076 void Preamble::parse(Parser & p, string const & forceclass,
1077 TeX2LyXDocClass & tc)
1079 // initialize fixed types
1080 special_columns['D'] = 3;
1081 bool is_full_document = false;
1082 bool is_lyx_file = false;
1083 bool in_lyx_preamble = false;
1085 // determine whether this is a full document or a fragment for inclusion
1087 Token const & t = p.get_token();
1089 if (t.cat() == catEscape && t.cs() == "documentclass") {
1090 is_full_document = true;
1096 while (is_full_document && p.good()) {
1097 Token const & t = p.get_token();
1100 cerr << "t: " << t << "\n";
1106 if (!in_lyx_preamble &&
1107 (t.cat() == catLetter ||
1108 t.cat() == catSuper ||
1109 t.cat() == catSub ||
1110 t.cat() == catOther ||
1111 t.cat() == catMath ||
1112 t.cat() == catActive ||
1113 t.cat() == catBegin ||
1114 t.cat() == catEnd ||
1115 t.cat() == catAlign ||
1116 t.cat() == catParameter))
1117 h_preamble << t.cs();
1119 else if (!in_lyx_preamble &&
1120 (t.cat() == catSpace || t.cat() == catNewline))
1121 h_preamble << t.asInput();
1123 else if (t.cat() == catComment) {
1124 static regex const islyxfile("%% LyX .* created this file");
1125 static regex const usercommands("User specified LaTeX commands");
1127 string const comment = t.asInput();
1129 // magically switch encoding default if it looks like XeLaTeX
1130 static string const magicXeLaTeX =
1131 "% This document must be compiled with XeLaTeX ";
1132 if (comment.size() > magicXeLaTeX.size()
1133 && comment.substr(0, magicXeLaTeX.size()) == magicXeLaTeX
1134 && h_inputencoding == "auto") {
1135 cerr << "XeLaTeX comment found, switching to UTF8\n";
1136 h_inputencoding = "utf8";
1139 if (regex_search(comment, sub, islyxfile)) {
1141 in_lyx_preamble = true;
1142 } else if (is_lyx_file
1143 && regex_search(comment, sub, usercommands))
1144 in_lyx_preamble = false;
1145 else if (!in_lyx_preamble)
1146 h_preamble << t.asInput();
1149 else if (t.cs() == "pagestyle")
1150 h_paperpagestyle = p.verbatim_item();
1152 else if (t.cs() == "setdefaultlanguage") {
1154 // We don't yet care about non-language variant options
1155 // because LyX doesn't support this yet, see bug #8214
1157 string langopts = p.getOpt();
1158 // check if the option contains a variant, if yes, extract it
1159 string::size_type pos_var = langopts.find("variant");
1160 string::size_type i = langopts.find(',', pos_var);
1161 if (pos_var != string::npos){
1163 if (i == string::npos)
1164 variant = langopts.substr(pos_var + 8, langopts.length() - pos_var - 9);
1166 variant = langopts.substr(pos_var + 8, i - pos_var - 8);
1167 h_language = variant;
1171 h_language = p.verbatim_item();
1172 //finally translate the poyglossia name to a LyX name
1173 h_language = polyglossia2lyx(h_language);
1176 else if (t.cs() == "setotherlanguage") {
1177 // We don't yet care about the option because LyX doesn't
1178 // support this yet, see bug #8214
1179 p.hasOpt() ? p.getOpt() : string();
1183 else if (t.cs() == "setmainfont") {
1184 // we don't care about the option
1185 p.hasOpt() ? p.getOpt() : string();
1186 h_font_roman = p.getArg('{', '}');
1189 else if (t.cs() == "setsansfont") {
1190 // we don't care about the option
1191 p.hasOpt() ? p.getOpt() : string();
1192 h_font_sans = p.getArg('{', '}');
1195 else if (t.cs() == "setmonofont") {
1196 // we don't care about the option
1197 p.hasOpt() ? p.getOpt() : string();
1198 h_font_typewriter = p.getArg('{', '}');
1201 else if (t.cs() == "date") {
1202 string argument = p.getArg('{', '}');
1203 if (argument.empty())
1204 h_suppress_date = "true";
1206 h_preamble << t.asInput() << '{' << argument << '}';
1209 else if (t.cs() == "color") {
1210 string const space =
1211 (p.hasOpt() ? p.getOpt() : string());
1212 string argument = p.getArg('{', '}');
1213 // check the case that a standard color is used
1214 if (space.empty() && is_known(argument, known_basic_colors)) {
1215 h_fontcolor = rgbcolor2code(argument);
1216 preamble.registerAutomaticallyLoadedPackage("color");
1217 } else if (space.empty() && argument == "document_fontcolor")
1218 preamble.registerAutomaticallyLoadedPackage("color");
1219 // check the case that LyX's document_fontcolor is defined
1220 // but not used for \color
1222 h_preamble << t.asInput();
1224 h_preamble << space;
1225 h_preamble << '{' << argument << '}';
1226 // the color might already be set because \definecolor
1227 // is parsed before this
1232 else if (t.cs() == "pagecolor") {
1233 string argument = p.getArg('{', '}');
1234 // check the case that a standard color is used
1235 if (is_known(argument, known_basic_colors)) {
1236 h_backgroundcolor = rgbcolor2code(argument);
1237 } else if (argument == "page_backgroundcolor")
1238 preamble.registerAutomaticallyLoadedPackage("color");
1239 // check the case that LyX's page_backgroundcolor is defined
1240 // but not used for \pagecolor
1242 h_preamble << t.asInput() << '{' << argument << '}';
1243 // the color might already be set because \definecolor
1244 // is parsed before this
1245 h_backgroundcolor = "";
1249 else if (t.cs() == "makeatletter") {
1250 // LyX takes care of this
1251 p.setCatCode('@', catLetter);
1254 else if (t.cs() == "makeatother") {
1255 // LyX takes care of this
1256 p.setCatCode('@', catOther);
1259 else if (t.cs() == "newcommand" || t.cs() == "newcommandx"
1260 || t.cs() == "renewcommand" || t.cs() == "renewcommandx"
1261 || t.cs() == "providecommand" || t.cs() == "providecommandx"
1262 || t.cs() == "DeclareRobustCommand"
1263 || t.cs() == "DeclareRobustCommandx"
1264 || t.cs() == "ProvideTextCommandDefault"
1265 || t.cs() == "DeclareMathAccent") {
1267 if (p.next_token().character() == '*') {
1271 string const name = p.verbatim_item();
1272 string const opt1 = p.getFullOpt();
1273 string const opt2 = p.getFullOpt();
1274 string const body = p.verbatim_item();
1276 if (name == "\\rmdefault")
1277 if (is_known(body, known_roman_fonts))
1278 h_font_roman = body;
1279 if (name == "\\sfdefault")
1280 if (is_known(body, known_sans_fonts))
1282 if (name == "\\ttdefault")
1283 if (is_known(body, known_typewriter_fonts))
1284 h_font_typewriter = body;
1285 if (name == "\\familydefault") {
1286 string family = body;
1287 // remove leading "\"
1288 h_font_default_family = family.erase(0,1);
1291 // remove the lyxdot definition that is re-added by LyX
1293 if (name == "\\lyxdot")
1294 in_lyx_preamble = true;
1296 // Add the command to the known commands
1297 add_known_command(name, opt1, !opt2.empty(), from_utf8(body));
1299 // only non-lyxspecific stuff
1300 if (!in_lyx_preamble) {
1302 ss << '\\' << t.cs();
1305 ss << '{' << name << '}' << opt1 << opt2
1306 << '{' << body << "}";
1307 h_preamble << ss.str();
1309 ostream & out = in_preamble ? h_preamble : os;
1310 out << "\\" << t.cs() << "{" << name << "}"
1311 << opts << "{" << body << "}";
1316 else if (t.cs() == "documentclass") {
1317 vector<string>::iterator it;
1318 vector<string> opts = split_options(p.getArg('[', ']'));
1319 handle_opt(opts, known_fontsizes, h_paperfontsize);
1320 delete_opt(opts, known_fontsizes);
1321 // delete "pt" at the end
1322 string::size_type i = h_paperfontsize.find("pt");
1323 if (i != string::npos)
1324 h_paperfontsize.erase(i);
1325 // The documentclass options are always parsed before the options
1326 // of the babel call so that a language cannot overwrite the babel
1328 handle_opt(opts, known_languages, h_language);
1329 delete_opt(opts, known_languages);
1331 // paper orientation
1332 if ((it = find(opts.begin(), opts.end(), "landscape")) != opts.end()) {
1333 h_paperorientation = "landscape";
1337 if ((it = find(opts.begin(), opts.end(), "oneside"))
1342 if ((it = find(opts.begin(), opts.end(), "twoside"))
1348 if ((it = find(opts.begin(), opts.end(), "onecolumn"))
1350 h_papercolumns = "1";
1353 if ((it = find(opts.begin(), opts.end(), "twocolumn"))
1355 h_papercolumns = "2";
1359 // some size options are known to any document classes, other sizes
1360 // are handled by the \geometry command of the geometry package
1361 handle_opt(opts, known_class_paper_sizes, h_papersize);
1362 delete_opt(opts, known_class_paper_sizes);
1363 // the remaining options
1364 h_options = join(opts, ",");
1365 // FIXME This does not work for classes that have a
1366 // different name in LyX than in LaTeX
1367 h_textclass = p.getArg('{', '}');
1370 else if (t.cs() == "usepackage") {
1371 string const options = p.getArg('[', ']');
1372 string const name = p.getArg('{', '}');
1373 vector<string> vecnames;
1374 split(name, vecnames, ',');
1375 vector<string>::const_iterator it = vecnames.begin();
1376 vector<string>::const_iterator end = vecnames.end();
1377 for (; it != end; ++it)
1378 handle_package(p, trimSpaceAndEol(*it), options,
1382 else if (t.cs() == "inputencoding") {
1383 string const encoding = p.getArg('{','}');
1384 h_inputencoding = encoding;
1385 p.setEncoding(encoding);
1388 else if (t.cs() == "newenvironment") {
1389 string const name = p.getArg('{', '}');
1390 string const opt1 = p.getFullOpt();
1391 string const opt2 = p.getFullOpt();
1392 string const beg = p.verbatim_item();
1393 string const end = p.verbatim_item();
1394 if (!in_lyx_preamble) {
1395 h_preamble << "\\newenvironment{" << name
1396 << '}' << opt1 << opt2 << '{'
1397 << beg << "}{" << end << '}';
1399 add_known_environment(name, opt1, !opt2.empty(),
1400 from_utf8(beg), from_utf8(end));
1404 else if (t.cs() == "def") {
1405 string name = p.get_token().cs();
1406 // In fact, name may be more than the name:
1407 // In the test case of bug 8116
1408 // name == "csname SF@gobble@opt \endcsname".
1409 // Therefore, we need to use asInput() instead of cs().
1410 while (p.next_token().cat() != catBegin)
1411 name += p.get_token().asInput();
1412 if (!in_lyx_preamble)
1413 h_preamble << "\\def\\" << name << '{'
1414 << p.verbatim_item() << "}";
1417 else if (t.cs() == "newcolumntype") {
1418 string const name = p.getArg('{', '}');
1419 trimSpaceAndEol(name);
1421 string opts = p.getOpt();
1422 if (!opts.empty()) {
1423 istringstream is(string(opts, 1));
1426 special_columns[name[0]] = nargs;
1427 h_preamble << "\\newcolumntype{" << name << "}";
1429 h_preamble << "[" << nargs << "]";
1430 h_preamble << "{" << p.verbatim_item() << "}";
1433 else if (t.cs() == "setcounter") {
1434 string const name = p.getArg('{', '}');
1435 string const content = p.getArg('{', '}');
1436 if (name == "secnumdepth")
1437 h_secnumdepth = content;
1438 else if (name == "tocdepth")
1439 h_tocdepth = content;
1441 h_preamble << "\\setcounter{" << name << "}{" << content << "}";
1444 else if (t.cs() == "setlength") {
1445 string const name = p.verbatim_item();
1446 string const content = p.verbatim_item();
1447 // the paragraphs are only not indented when \parindent is set to zero
1448 if (name == "\\parindent" && content != "") {
1449 if (content[0] == '0')
1450 h_paragraph_separation = "skip";
1452 h_paragraph_indentation = translate_len(content);
1453 } else if (name == "\\parskip") {
1454 if (content == "\\smallskipamount")
1455 h_defskip = "smallskip";
1456 else if (content == "\\medskipamount")
1457 h_defskip = "medskip";
1458 else if (content == "\\bigskipamount")
1459 h_defskip = "bigskip";
1461 h_defskip = content;
1463 h_preamble << "\\setlength{" << name << "}{" << content << "}";
1466 else if (t.cs() == "onehalfspacing")
1467 h_spacing = "onehalf";
1469 else if (t.cs() == "doublespacing")
1470 h_spacing = "double";
1472 else if (t.cs() == "setstretch")
1473 h_spacing = "other " + p.verbatim_item();
1475 else if (t.cs() == "synctex") {
1476 // the scheme is \synctex=value
1477 // where value can only be "1" or "-1"
1478 h_output_sync = "1";
1479 // there can be any character behind the value (e.g. a linebreak or a '\'
1480 // therefore we extract it char by char
1482 string value = p.get_token().asInput();
1484 value += p.get_token().asInput();
1485 h_output_sync_macro = "\\synctex=" + value;
1488 else if (t.cs() == "begin") {
1489 string const name = p.getArg('{', '}');
1490 if (name == "document")
1492 h_preamble << "\\begin{" << name << "}";
1495 else if (t.cs() == "geometry") {
1496 vector<string> opts = split_options(p.getArg('{', '}'));
1497 handle_geometry(opts);
1500 else if (t.cs() == "definecolor") {
1501 string const color = p.getArg('{', '}');
1502 string const space = p.getArg('{', '}');
1503 string const value = p.getArg('{', '}');
1504 if (color == "document_fontcolor" && space == "rgb") {
1505 RGBColor c(RGBColorFromLaTeX(value));
1506 h_fontcolor = X11hexname(c);
1507 } else if (color == "note_fontcolor" && space == "rgb") {
1508 RGBColor c(RGBColorFromLaTeX(value));
1509 h_notefontcolor = X11hexname(c);
1510 } else if (color == "page_backgroundcolor" && space == "rgb") {
1511 RGBColor c(RGBColorFromLaTeX(value));
1512 h_backgroundcolor = X11hexname(c);
1513 } else if (color == "shadecolor" && space == "rgb") {
1514 RGBColor c(RGBColorFromLaTeX(value));
1515 h_boxbgcolor = X11hexname(c);
1517 h_preamble << "\\definecolor{" << color
1518 << "}{" << space << "}{" << value
1523 else if (t.cs() == "bibliographystyle")
1524 h_biblio_style = p.verbatim_item();
1526 else if (t.cs() == "jurabibsetup") {
1527 // FIXME p.getArg('{', '}') is most probably wrong (it
1528 // does not handle nested braces).
1529 // Use p.verbatim_item() instead.
1530 vector<string> jurabibsetup =
1531 split_options(p.getArg('{', '}'));
1532 // add jurabibsetup to the jurabib package options
1533 add_package("jurabib", jurabibsetup);
1534 if (!jurabibsetup.empty()) {
1535 h_preamble << "\\jurabibsetup{"
1536 << join(jurabibsetup, ",") << '}';
1540 else if (t.cs() == "hypersetup") {
1541 vector<string> hypersetup =
1542 split_options(p.verbatim_item());
1543 // add hypersetup to the hyperref package options
1544 handle_hyperref(hypersetup);
1545 if (!hypersetup.empty()) {
1546 h_preamble << "\\hypersetup{"
1547 << join(hypersetup, ",") << '}';
1551 else if (is_known(t.cs(), known_if_3arg_commands)) {
1552 // prevent misparsing of \usepackage if it is used
1553 // as an argument (see e.g. our own output of
1554 // \@ifundefined above)
1555 string const arg1 = p.verbatim_item();
1556 string const arg2 = p.verbatim_item();
1557 string const arg3 = p.verbatim_item();
1558 // test case \@ifundefined{date}{}{\date{}}
1559 if (t.cs() == "@ifundefined" && arg1 == "date" &&
1560 arg2.empty() && arg3 == "\\date{}") {
1561 h_suppress_date = "true";
1562 // older tex2lyx versions did output
1563 // \@ifundefined{definecolor}{\usepackage{color}}{}
1564 } else if (t.cs() == "@ifundefined" &&
1565 arg1 == "definecolor" &&
1566 arg2 == "\\usepackage{color}" &&
1568 if (!in_lyx_preamble)
1569 h_preamble << package_beg_sep
1572 << "\\@ifundefined{definecolor}{color}{}"
1575 //\@ifundefined{showcaptionsetup}{}{%
1576 // \PassOptionsToPackage{caption=false}{subfig}}
1577 // that LyX uses for subfloats
1578 } else if (t.cs() == "@ifundefined" &&
1579 arg1 == "showcaptionsetup" && arg2.empty()
1580 && arg3 == "%\n \\PassOptionsToPackage{caption=false}{subfig}") {
1582 } else if (!in_lyx_preamble) {
1583 h_preamble << t.asInput()
1584 << '{' << arg1 << '}'
1585 << '{' << arg2 << '}'
1586 << '{' << arg3 << '}';
1590 else if (is_known(t.cs(), known_if_commands)) {
1591 // must not parse anything in conditional code, since
1592 // LyX would output the parsed contents unconditionally
1593 if (!in_lyx_preamble)
1594 h_preamble << t.asInput();
1595 handle_if(p, in_lyx_preamble);
1598 else if (!t.cs().empty() && !in_lyx_preamble)
1599 h_preamble << '\\' << t.cs();
1602 // remove the whitespace
1605 // Force textclass if the user wanted it
1606 if (!forceclass.empty())
1607 h_textclass = forceclass;
1608 if (noweb_mode && !prefixIs(h_textclass, "literate-"))
1609 h_textclass.insert(0, "literate-");
1610 tc.setName(h_textclass);
1612 cerr << "Error: Could not read layout file for textclass \"" << h_textclass << "\"." << endl;
1615 if (h_papersides.empty()) {
1618 h_papersides = ss.str();
1623 string babel2lyx(string const & language)
1625 char const * const * where = is_known(language, known_languages);
1627 return known_coded_languages[where - known_languages];
1632 string polyglossia2lyx(string const & language)
1634 char const * const * where = is_known(language, polyglossia_languages);
1636 return coded_polyglossia_languages[where - polyglossia_languages];
1641 string rgbcolor2code(string const & name)
1643 char const * const * where = is_known(name, known_basic_colors);
1645 // "red", "green" etc
1646 return known_basic_color_codes[where - known_basic_colors];
1648 // "255,0,0", "0,255,0" etc
1649 RGBColor c(RGBColorFromLaTeX(name));
1650 return X11hexname(c);