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.
20 #include "LayoutFile.h"
23 #include "TextClass.h"
25 #include "support/convert.h"
26 #include "support/FileName.h"
27 #include "support/filetools.h"
28 #include "support/lstrings.h"
30 #include "support/regex.h"
36 using namespace lyx::support;
41 // special columntypes
42 extern map<char, int> special_columns;
48 // CJK languages are handled in text.cpp, polyglossia languages are listed
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",
91 /// languages with danish quotes (.lyx names)
92 const char * const known_danish_quotes_languages[] = {"danish", 0};
94 /// languages with english quotes (.lyx names)
95 const char * const known_english_quotes_languages[] = {"american", "australian",
96 "bahasa", "bahasam", "brazilian", "canadian", "chinese-simplified", "english",
97 "esperanto", "hebrew", "irish", "korean", "newzealand", "portuguese", "scottish",
100 /// languages with french quotes (.lyx names)
101 const char * const known_french_quotes_languages[] = {"albanian",
102 "arabic_arabi", "arabic_arabtex", "basque", "canadien", "catalan", "french",
103 "galician", "greek", "italian", "norsk", "nynorsk", "polutonikogreek",
104 "russian", "spanish", "spanish-mexico", "turkish", "turkmen", "ukrainian",
107 /// languages with german quotes (.lyx names)
108 const char * const known_german_quotes_languages[] = {"austrian", "bulgarian",
109 "czech", "german", "icelandic", "lithuanian", "lowersorbian", "naustrian",
110 "ngerman", "serbian", "serbian-latin", "slovak", "slovene", "uppersorbian", 0};
112 /// languages with polish quotes (.lyx names)
113 const char * const known_polish_quotes_languages[] = {"afrikaans", "croatian",
114 "dutch", "estonian", "magyar", "polish", "romanian", 0};
116 /// languages with swedish quotes (.lyx names)
117 const char * const known_swedish_quotes_languages[] = {"finnish",
120 /// known language packages from the times before babel
121 const char * const known_old_language_packages[] = {"french", "frenchle",
122 "frenchpro", "german", "ngerman", "pmfrench", 0};
124 char const * const known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
126 const char * const known_roman_fonts[] = { "ae", "beraserif", "bookman",
127 "ccfonts", "chancery", "charter", "cmr", "fourier", "lmodern", "mathpazo",
128 "mathptmx", "newcent", "utopia", 0};
130 const char * const known_sans_fonts[] = { "avant", "berasans", "cmbr", "cmss",
131 "helvet", "lmss", 0};
133 const char * const known_typewriter_fonts[] = { "beramono", "cmtl", "cmtt",
134 "courier", "lmtt", "luximono", "fourier", "lmodern", "mathpazo", "mathptmx",
137 const char * const known_paper_sizes[] = { "a0paper", "b0paper", "c0paper",
138 "a1paper", "b1paper", "c1paper", "a2paper", "b2paper", "c2paper", "a3paper",
139 "b3paper", "c3paper", "a4paper", "b4paper", "c4paper", "a5paper", "b5paper",
140 "c5paper", "a6paper", "b6paper", "c6paper", "executivepaper", "legalpaper",
141 "letterpaper", "b0j", "b1j", "b2j", "b3j", "b4j", "b5j", "b6j", 0};
143 const char * const known_class_paper_sizes[] = { "a4paper", "a5paper",
144 "executivepaper", "legalpaper", "letterpaper", 0};
146 const char * const known_paper_margins[] = { "lmargin", "tmargin", "rmargin",
147 "bmargin", "headheight", "headsep", "footskip", "columnsep", 0};
149 const char * const known_coded_paper_margins[] = { "leftmargin", "topmargin",
150 "rightmargin", "bottommargin", "headheight", "headsep", "footskip",
153 /// commands that can start an \if...\else...\endif sequence
154 const char * const known_if_commands[] = {"if", "ifarydshln", "ifbraket",
155 "ifcancel", "ifcolortbl", "ifeurosym", "ifmarginnote", "ifmmode", "ifpdf",
156 "ifsidecap", "ifupgreek", 0};
158 const char * const known_basic_colors[] = {"blue", "black", "cyan", "green",
159 "magenta", "red", "white", "yellow", 0};
161 const char * const known_basic_color_codes[] = {"#0000ff", "#000000", "#00ffff", "#00ff00",
162 "#ff00ff", "#ff0000", "#ffffff", "#ffff00", 0};
164 /// conditional commands with three arguments like \@ifundefined{}{}{}
165 const char * const known_if_3arg_commands[] = {"@ifundefined", "IfFileExists",
168 /// packages that work only in xetex
169 /// polyglossia is handled separately
170 const char * const known_xetex_packages[] = {"arabxetex", "fixlatvian",
171 "fontbook", "fontwrap", "mathspec", "philokalia", "unisugar",
172 "xeCJK", "xecolor", "xecyr", "xeindex", "xepersian", "xunicode", 0};
174 /// packages that are automatically skipped if loaded by LyX
175 const char * const known_lyx_packages[] = {"amsbsy", "amsmath", "amssymb",
176 "amstext", "amsthm", "array", "babel", "booktabs", "calc", "CJK", "color", "float",
177 "fontspec", "graphicx", "hhline", "ifthen", "longtable", "makeidx", "multirow",
178 "nomencl", "pdfpages", "rotating", "rotfloat", "splitidx", "setspace",
179 "subscript", "textcomp", "ulem", "url", "varioref", "verbatim", "wrapfig",
182 // codes used to remove packages that are loaded automatically by LyX.
183 // Syntax: package_beg_sep<name>package_mid_sep<package loading code>package_end_sep
184 const char package_beg_sep = '\001';
185 const char package_mid_sep = '\002';
186 const char package_end_sep = '\003';
189 // returns true if at least one of the options in what has been found
190 bool handle_opt(vector<string> & opts, char const * const * what, string & target)
196 // the last language option is the document language (for babel and LyX)
197 // the last size option is the document font size
198 vector<string>::iterator it;
199 vector<string>::iterator position = opts.begin();
200 for (; *what; ++what) {
201 it = find(opts.begin(), opts.end(), *what);
202 if (it != opts.end()) {
203 if (it >= position) {
214 void delete_opt(vector<string> & opts, char const * const * what)
219 // remove found options from the list
220 // do this after handle_opt to avoid potential memory leaks
221 vector<string>::iterator it;
222 for (; *what; ++what) {
223 it = find(opts.begin(), opts.end(), *what);
224 if (it != opts.end())
231 * Split a package options string (keyval format) into a vector.
233 * authorformat=smallcaps,
235 * titleformat=colonsep,
236 * bibformat={tabular,ibidem,numbered}
238 vector<string> split_options(string const & input)
240 vector<string> options;
244 Token const & t = p.get_token();
245 if (t.asInput() == ",") {
246 options.push_back(trimSpaceAndEol(option));
248 } else if (t.asInput() == "=") {
251 if (p.next_token().asInput() == "{")
252 option += '{' + p.getArg('{', '}') + '}';
253 } else if (t.cat() != catSpace)
254 option += t.asInput();
258 options.push_back(trimSpaceAndEol(option));
265 * Retrieve a keyval option "name={value with=sign}" named \p name from
266 * \p options and return the value.
267 * The found option is also removed from \p options.
269 string process_keyval_opt(vector<string> & options, string name)
271 for (size_t i = 0; i < options.size(); ++i) {
272 vector<string> option;
273 split(options[i], option, '=');
274 if (option.size() < 2)
276 if (option[0] == name) {
277 options.erase(options.begin() + i);
278 option.erase(option.begin());
279 return join(option, "=");
285 } // anonymous namespace
289 * known polyglossia language names (including variants)
291 const char * const Preamble::polyglossia_languages[] = {
292 "albanian", "croatian", "hebrew", "norsk", "swedish", "amharic", "czech", "hindi",
293 "nynorsk", "syriac", "arabic", "danish", "icelandic", "occitan", "tamil",
294 "armenian", "divehi", "interlingua", "polish", "telugu", "asturian", "dutch",
295 "irish", "portuges", "thai", "bahasai", "english", "italian", "romanian", "turkish",
296 "bahasam", "esperanto", "lao", "russian", "turkmen", "basque", "estonian", "latin",
297 "samin", "ukrainian", "bengali", "farsi", "latvian", "sanskrit", "urdu", "brazil",
298 "brazilian", "finnish", "lithuanian", "scottish", "usorbian", "breton", "french",
299 "lsorbian", "serbian", "vietnamese", "bulgarian", "galician", "magyar", "slovak",
300 "welsh", "catalan", "german", "malayalam", "slovenian", "coptic", "greek",
301 "marathi", "spanish",
302 "american", "ancient", "australian", "british", "monotonic", "newzealand",
306 * the same as polyglossia_languages with .lyx names
307 * please keep this in sync with polyglossia_languages line by line!
309 const char * const Preamble::coded_polyglossia_languages[] = {
310 "albanian", "croatian", "hebrew", "norsk", "swedish", "amharic", "czech", "hindi",
311 "nynorsk", "syriac", "arabic_arabi", "danish", "icelandic", "occitan", "tamil",
312 "armenian", "divehi", "interlingua", "polish", "telugu", "asturian", "dutch",
313 "irish", "portuges", "thai", "bahasa", "english", "italian", "romanian", "turkish",
314 "bahasam", "esperanto", "lao", "russian", "turkmen", "basque", "estonian", "latin",
315 "samin", "ukrainian", "bengali", "farsi", "latvian", "sanskrit", "urdu", "brazilian",
316 "brazilian", "finnish", "lithuanian", "scottish", "uppersorbian", "breton", "french",
317 "lowersorbian", "serbian", "vietnamese", "bulgarian", "galician", "magyar", "slovak",
318 "welsh", "catalan", "ngerman", "malayalam", "slovene", "coptic", "greek",
319 "marathi", "spanish",
320 "american", "ancientgreek", "australian", "british", "greek", "newzealand",
321 "polutonikogreek", 0};
324 bool Preamble::indentParagraphs() const
326 return h_paragraph_separation == "indent";
330 bool Preamble::isPackageUsed(string const & package) const
332 return used_packages.find(package) != used_packages.end();
336 vector<string> Preamble::getPackageOptions(string const & package) const
338 map<string, vector<string> >::const_iterator it = used_packages.find(package);
339 if (it != used_packages.end())
341 return vector<string>();
345 void Preamble::registerAutomaticallyLoadedPackage(std::string const & package)
347 auto_packages.insert(package);
351 void Preamble::addModule(string const & module)
353 used_modules.push_back(module);
357 void Preamble::suppressDate(bool suppress)
360 h_suppress_date = "true";
362 h_suppress_date = "false";
366 void Preamble::registerAuthor(std::string const & name)
368 Author author(from_utf8(name), empty_docstring());
369 author.setUsed(true);
370 authors_.record(author);
371 h_tracking_changes = "true";
372 h_output_changes = "true";
376 Author const & Preamble::getAuthor(std::string const & name) const
378 Author author(from_utf8(name), empty_docstring());
379 for (AuthorList::Authors::const_iterator it = authors_.begin();
380 it != authors_.end(); ++it)
383 static Author const dummy;
388 void Preamble::add_package(string const & name, vector<string> & options)
390 // every package inherits the global options
391 if (used_packages.find(name) == used_packages.end())
392 used_packages[name] = split_options(h_options);
394 vector<string> & v = used_packages[name];
395 v.insert(v.end(), options.begin(), options.end());
396 if (name == "jurabib") {
397 // Don't output the order argument (see the cite command
398 // handling code in text.cpp).
399 vector<string>::iterator end =
400 remove(options.begin(), options.end(), "natbiborder");
401 end = remove(options.begin(), end, "jurabiborder");
402 options.erase(end, options.end());
409 // Given is a string like "scaled=0.9" or "Scale=0.9", return 0.9 * 100
410 bool scale_as_percentage(string const & scale, string & percentage)
412 string::size_type pos = scale.find('=');
413 if (pos != string::npos) {
414 string value = scale.substr(pos + 1);
415 if (isStrDbl(value)) {
416 percentage = convert<string>(100 * convert<double>(value));
424 string remove_braces(string const & value)
428 if (value[0] == '{' && value[value.length()-1] == '}')
429 return value.substr(1, value.length()-2);
433 } // anonymous namespace
436 Preamble::Preamble() : one_language(true), title_layout_found(false),
437 h_font_cjk_set(false)
441 h_biblio_style = "plain";
442 h_bibtex_command = "default";
443 h_cite_engine = "basic";
444 h_cite_engine_type = "numerical";
446 h_defskip = "medskip";
449 h_fontencoding = "default";
450 h_font_roman = "default";
451 h_font_sans = "default";
452 h_font_typewriter = "default";
453 h_font_math = "auto";
454 h_font_default_family = "default";
455 h_use_non_tex_fonts = "false";
457 h_font_osf = "false";
458 h_font_sf_scale = "100";
459 h_font_tt_scale = "100";
461 h_graphics = "default";
462 h_default_output_format = "default";
463 h_html_be_strict = "false";
464 h_html_css_as_file = "0";
465 h_html_math_output = "0";
467 h_index_command = "default";
468 h_inputencoding = "auto";
469 h_justification = "true";
470 h_language = "english";
471 h_language_package = "none";
473 h_maintain_unincluded_children = "false";
477 h_output_changes = "false";
479 //h_output_sync_macro
480 h_papercolumns = "1";
481 h_paperfontsize = "default";
482 h_paperorientation = "portrait";
483 h_paperpagestyle = "default";
485 h_papersize = "default";
486 h_paragraph_indentation = "default";
487 h_paragraph_separation = "indent";
492 h_pdf_bookmarks = "1";
493 h_pdf_bookmarksnumbered = "0";
494 h_pdf_bookmarksopen = "0";
495 h_pdf_bookmarksopenlevel = "1";
496 h_pdf_breaklinks = "0";
497 h_pdf_pdfborder = "0";
498 h_pdf_colorlinks = "0";
499 h_pdf_backref = "section";
500 h_pdf_pdfusetitle = "1";
502 //h_pdf_quoted_options;
503 h_quotes_language = "english";
506 h_spacing = "single";
507 h_suppress_date = "false";
508 h_textclass = "article";
510 h_tracking_changes = "false";
511 h_use_bibtopic = "false";
512 h_use_indices = "false";
513 h_use_geometry = "false";
514 h_use_default_options = "false";
515 h_use_hyperref = "false";
516 h_use_refstyle = "0";
517 h_use_packages["amsmath"] = "1";
518 h_use_packages["amssymb"] = "0";
519 h_use_packages["esint"] = "1";
520 h_use_packages["mhchem"] = "0";
521 h_use_packages["mathdots"] = "0";
522 h_use_packages["mathtools"] = "0";
523 h_use_packages["stackrel"] = "0";
524 h_use_packages["stmaryrd"] = "0";
525 h_use_packages["undertilde"] = "0";
529 void Preamble::handle_hyperref(vector<string> & options)
531 // FIXME swallow inputencoding changes that might surround the
532 // hyperref setup if it was written by LyX
533 h_use_hyperref = "true";
534 // swallow "unicode=true", since LyX does always write that
535 vector<string>::iterator it =
536 find(options.begin(), options.end(), "unicode=true");
537 if (it != options.end())
539 it = find(options.begin(), options.end(), "pdfusetitle");
540 if (it != options.end()) {
541 h_pdf_pdfusetitle = "1";
544 string bookmarks = process_keyval_opt(options, "bookmarks");
545 if (bookmarks == "true")
546 h_pdf_bookmarks = "1";
547 else if (bookmarks == "false")
548 h_pdf_bookmarks = "0";
549 if (h_pdf_bookmarks == "1") {
550 string bookmarksnumbered =
551 process_keyval_opt(options, "bookmarksnumbered");
552 if (bookmarksnumbered == "true")
553 h_pdf_bookmarksnumbered = "1";
554 else if (bookmarksnumbered == "false")
555 h_pdf_bookmarksnumbered = "0";
556 string bookmarksopen =
557 process_keyval_opt(options, "bookmarksopen");
558 if (bookmarksopen == "true")
559 h_pdf_bookmarksopen = "1";
560 else if (bookmarksopen == "false")
561 h_pdf_bookmarksopen = "0";
562 if (h_pdf_bookmarksopen == "1") {
563 string bookmarksopenlevel =
564 process_keyval_opt(options, "bookmarksopenlevel");
565 if (!bookmarksopenlevel.empty())
566 h_pdf_bookmarksopenlevel = bookmarksopenlevel;
569 string breaklinks = process_keyval_opt(options, "breaklinks");
570 if (breaklinks == "true")
571 h_pdf_breaklinks = "1";
572 else if (breaklinks == "false")
573 h_pdf_breaklinks = "0";
574 string pdfborder = process_keyval_opt(options, "pdfborder");
575 if (pdfborder == "{0 0 0}")
576 h_pdf_pdfborder = "1";
577 else if (pdfborder == "{0 0 1}")
578 h_pdf_pdfborder = "0";
579 string backref = process_keyval_opt(options, "backref");
580 if (!backref.empty())
581 h_pdf_backref = backref;
582 string colorlinks = process_keyval_opt(options, "colorlinks");
583 if (colorlinks == "true")
584 h_pdf_colorlinks = "1";
585 else if (colorlinks == "false")
586 h_pdf_colorlinks = "0";
587 string pdfpagemode = process_keyval_opt(options, "pdfpagemode");
588 if (!pdfpagemode.empty())
589 h_pdf_pagemode = pdfpagemode;
590 string pdftitle = process_keyval_opt(options, "pdftitle");
591 if (!pdftitle.empty()) {
592 h_pdf_title = remove_braces(pdftitle);
594 string pdfauthor = process_keyval_opt(options, "pdfauthor");
595 if (!pdfauthor.empty()) {
596 h_pdf_author = remove_braces(pdfauthor);
598 string pdfsubject = process_keyval_opt(options, "pdfsubject");
599 if (!pdfsubject.empty())
600 h_pdf_subject = remove_braces(pdfsubject);
601 string pdfkeywords = process_keyval_opt(options, "pdfkeywords");
602 if (!pdfkeywords.empty())
603 h_pdf_keywords = remove_braces(pdfkeywords);
604 if (!options.empty()) {
605 if (!h_pdf_quoted_options.empty())
606 h_pdf_quoted_options += ',';
607 h_pdf_quoted_options += join(options, ",");
613 void Preamble::handle_geometry(vector<string> & options)
615 h_use_geometry = "true";
616 vector<string>::iterator it;
618 if ((it = find(options.begin(), options.end(), "landscape")) != options.end()) {
619 h_paperorientation = "landscape";
623 // keyval version: "paper=letter"
624 string paper = process_keyval_opt(options, "paper");
626 h_papersize = paper + "paper";
627 // alternative version: "letterpaper"
628 handle_opt(options, known_paper_sizes, h_papersize);
629 delete_opt(options, known_paper_sizes);
631 char const * const * margin = known_paper_margins;
632 for (; *margin; ++margin) {
633 string value = process_keyval_opt(options, *margin);
634 if (!value.empty()) {
635 int k = margin - known_paper_margins;
636 string name = known_coded_paper_margins[k];
637 h_margins += '\\' + name + ' ' + value + '\n';
643 void Preamble::handle_package(Parser &p, string const & name,
644 string const & opts, bool in_lyx_preamble)
646 vector<string> options = split_options(opts);
647 add_package(name, options);
648 char const * const * where = 0;
650 if (is_known(name, known_xetex_packages)) {
652 h_use_non_tex_fonts = "true";
653 registerAutomaticallyLoadedPackage("fontspec");
654 if (h_inputencoding == "auto")
655 p.setEncoding("UTF-8");
659 if (is_known(name, known_roman_fonts))
662 if (name == "fourier") {
663 h_font_roman = "utopia";
664 // when font uses real small capitals
665 if (opts == "expert")
669 else if (name == "mathpazo")
670 h_font_roman = "palatino";
672 else if (name == "mathptmx")
673 h_font_roman = "times";
676 if (is_known(name, known_sans_fonts)) {
678 if (options.size() == 1) {
679 if (scale_as_percentage(opts, h_font_sf_scale))
685 if (is_known(name, known_typewriter_fonts)) {
686 // fourier can be set as roman font _only_
687 // fourier as typewriter is handled in handling of \ttdefault
688 if (name != "fourier") {
689 h_font_typewriter = name;
690 if (options.size() == 1) {
691 if (scale_as_percentage(opts, h_font_tt_scale))
697 // font uses old-style figure
701 // after the detection and handling of special cases, we can remove the
702 // fonts, otherwise they would appear in the preamble, see bug #7856
703 if (is_known(name, known_roman_fonts) || is_known(name, known_sans_fonts)
704 || is_known(name, known_typewriter_fonts))
707 else if (name == "amsmath" || name == "amssymb" ||
708 name == "esint" || name == "mhchem" || name == "mathdots" ||
709 name == "mathtools" || name == "stackrel" ||
710 name == "stmaryrd" || name == "undertilde")
711 h_use_packages[name] = "2";
713 else if (name == "babel") {
714 h_language_package = "default";
715 // One might think we would have to do nothing if babel is loaded
716 // without any options to prevent pollution of the preamble with this
717 // babel call in every roundtrip.
718 // But the user could have defined babel-specific things afterwards. So
719 // we need to keep it in the preamble to prevent cases like bug #7861.
721 // check if more than one option was used - used later for inputenc
722 if (options.begin() != options.end() - 1)
723 one_language = false;
724 // babel takes the last language of the option of its \usepackage
725 // call as document language. If there is no such language option, the
726 // last language in the documentclass options is used.
727 handle_opt(options, known_languages, h_language);
728 // translate the babel name to a LyX name
729 h_language = babel2lyx(h_language);
730 // for Japanese we assume EUC-JP as encoding
731 // but we cannot determine the exact encoding and thus output also a note
732 if (h_language == "japanese") {
733 h_inputencoding = "euc";
734 p.setEncoding("EUC-JP");
735 is_nonCJKJapanese = true;
736 // in this case babel can be removed from the preamble
737 registerAutomaticallyLoadedPackage("babel");
739 // If babel is called with options, LyX puts them by default into the
740 // document class options. This works for most languages, except
741 // for Latvian, Lithuanian, Mongolian, Turkmen and Vietnamese and
742 // perhaps in future others.
743 // Therefore keep the babel call as it is as the user might have
745 h_preamble << "\\usepackage[" << opts << "]{babel}\n";
747 delete_opt(options, known_languages);
750 h_preamble << "\\usepackage{babel}\n";
753 else if (name == "polyglossia") {
754 h_language_package = "default";
755 h_default_output_format = "pdf4";
756 h_use_non_tex_fonts = "true";
758 registerAutomaticallyLoadedPackage("xunicode");
759 if (h_inputencoding == "auto")
760 p.setEncoding("UTF-8");
763 else if (name == "CJK") {
764 // set the encoding to "auto" because it might be set to "default" by the babel handling
765 // and this would not be correct for CJK
766 if (h_inputencoding == "default")
767 h_inputencoding = "auto";
768 registerAutomaticallyLoadedPackage("CJK");
771 else if (name == "CJKutf8") {
772 h_inputencoding = "UTF8";
773 p.setEncoding("UTF-8");
774 registerAutomaticallyLoadedPackage("CJKutf8");
777 else if (name == "fontenc") {
778 h_fontencoding = getStringFromVector(options, ",");
779 /* We could do the following for better round trip support,
780 * but this makes the document less portable, so I skip it:
781 if (h_fontencoding == lyxrc.fontenc)
782 h_fontencoding = "global";
787 else if (name == "inputenc" || name == "luainputenc") {
788 // h_inputencoding is only set when there is not more than one
789 // inputenc option because otherwise h_inputencoding must be
790 // set to "auto" (the default encoding of the document language)
791 // Therefore check for the "," character.
792 // It is also only set when there is not more than one babel
794 if (opts.find(",") == string::npos && one_language == true)
795 h_inputencoding = opts;
796 if (!options.empty())
797 p.setEncoding(options.back(), Encoding::inputenc);
801 else if (name == "srcltx") {
804 h_output_sync_macro = "\\usepackage[" + opts + "]{srcltx}";
807 h_output_sync_macro = "\\usepackage{srcltx}";
810 else if (is_known(name, known_old_language_packages)) {
811 // known language packages from the times before babel
812 // if they are found and not also babel, they will be used as
813 // custom language package
814 h_language_package = "\\usepackage{" + name + "}";
817 else if (name == "prettyref")
818 ; // ignore this FIXME: Use the package separator mechanism instead
820 else if (name == "lyxskak") {
821 // ignore this and its options
822 const char * const o[] = {"ps", "mover", 0};
823 delete_opt(options, o);
826 else if (is_known(name, known_lyx_packages) && options.empty()) {
827 if (name == "splitidx")
828 h_use_indices = "true";
829 if (!in_lyx_preamble) {
830 h_preamble << package_beg_sep << name
831 << package_mid_sep << "\\usepackage{"
833 if (p.next_token().cat() == catNewline ||
834 (p.next_token().cat() == catSpace &&
835 p.next_next_token().cat() == catNewline))
837 h_preamble << package_end_sep;
841 else if (name == "geometry")
842 handle_geometry(options);
844 else if (name == "subfig")
845 ; // ignore this FIXME: Use the package separator mechanism instead
847 else if ((where = is_known(name, known_languages)))
848 h_language = known_coded_languages[where - known_languages];
850 else if (name == "natbib") {
851 h_biblio_style = "plainnat";
852 h_cite_engine = "natbib";
853 h_cite_engine_type = "authoryear";
854 vector<string>::iterator it =
855 find(options.begin(), options.end(), "authoryear");
856 if (it != options.end())
859 it = find(options.begin(), options.end(), "numbers");
860 if (it != options.end()) {
861 h_cite_engine_type = "numerical";
867 else if (name == "jurabib") {
868 h_biblio_style = "jurabib";
869 h_cite_engine = "jurabib";
870 h_cite_engine_type = "authoryear";
873 else if (name == "hyperref")
874 handle_hyperref(options);
876 else if (!in_lyx_preamble) {
878 h_preamble << "\\usepackage{" << name << '}';
880 h_preamble << "\\usepackage[" << opts << "]{"
884 if (p.next_token().cat() == catNewline ||
885 (p.next_token().cat() == catSpace &&
886 p.next_next_token().cat() == catNewline))
890 // We need to do something with the options...
891 if (!options.empty())
892 cerr << "Ignoring options '" << join(options, ",")
893 << "' of package " << name << '.' << endl;
895 // remove the whitespace
900 void Preamble::handle_if(Parser & p, bool in_lyx_preamble)
903 Token t = p.get_token();
904 if (t.cat() == catEscape &&
905 is_known(t.cs(), known_if_commands))
906 handle_if(p, in_lyx_preamble);
908 if (!in_lyx_preamble)
909 h_preamble << t.asInput();
910 if (t.cat() == catEscape && t.cs() == "fi")
917 bool Preamble::writeLyXHeader(ostream & os, bool subdoc)
919 // set the quote language
920 // LyX only knows the following quotes languages:
921 // english, swedish, german, polish, french and danish
922 // (quotes for "japanese" and "chinese-traditional" are missing because
923 // they wouldn't be useful: http://www.lyx.org/trac/ticket/6383)
924 // conversion list taken from
925 // http://en.wikipedia.org/wiki/Quotation_mark,_non-English_usage
926 // (quotes for kazakh and interlingua are unknown)
928 if (is_known(h_language, known_danish_quotes_languages))
929 h_quotes_language = "danish";
931 else if (is_known(h_language, known_french_quotes_languages))
932 h_quotes_language = "french";
934 else if (is_known(h_language, known_german_quotes_languages))
935 h_quotes_language = "german";
937 else if (is_known(h_language, known_polish_quotes_languages))
938 h_quotes_language = "polish";
940 else if (is_known(h_language, known_swedish_quotes_languages))
941 h_quotes_language = "swedish";
943 else if (is_known(h_language, known_english_quotes_languages))
944 h_quotes_language = "english";
946 if (contains(h_float_placement, "H"))
947 registerAutomaticallyLoadedPackage("float");
948 if (h_spacing != "single" && h_spacing != "default")
949 registerAutomaticallyLoadedPackage("setspace");
950 if (h_use_packages["amsmath"] == "2") {
951 // amsbsy and amstext are already provided by amsmath
952 registerAutomaticallyLoadedPackage("amsbsy");
953 registerAutomaticallyLoadedPackage("amstext");
956 // output the LyX file settings
957 os << "#LyX file created by tex2lyx " << PACKAGE_VERSION << "\n"
958 << "\\lyxformat " << LYX_FORMAT << '\n'
959 << "\\begin_document\n"
960 << "\\begin_header\n"
961 << "\\textclass " << h_textclass << "\n";
962 string const raw = subdoc ? empty_string() : h_preamble.str();
964 os << "\\begin_preamble\n";
965 for (string::size_type i = 0; i < raw.size(); ++i) {
966 if (raw[i] == package_beg_sep) {
967 // Here follows some package loading code that
968 // must be skipped if the package is loaded
970 string::size_type j = raw.find(package_mid_sep, i);
971 if (j == string::npos)
973 string::size_type k = raw.find(package_end_sep, j);
974 if (k == string::npos)
976 string const package = raw.substr(i + 1, j - i - 1);
977 string const replacement = raw.substr(j + 1, k - j - 1);
978 if (auto_packages.find(package) == auto_packages.end())
984 os << "\n\\end_preamble\n";
986 if (!h_options.empty())
987 os << "\\options " << h_options << "\n";
988 os << "\\use_default_options " << h_use_default_options << "\n";
989 if (!used_modules.empty()) {
990 os << "\\begin_modules\n";
991 vector<string>::const_iterator const end = used_modules.end();
992 vector<string>::const_iterator it = used_modules.begin();
993 for (; it != end; ++it)
995 os << "\\end_modules\n";
997 os << "\\maintain_unincluded_children " << h_maintain_unincluded_children << "\n"
998 << "\\language " << h_language << "\n"
999 << "\\language_package " << h_language_package << "\n"
1000 << "\\inputencoding " << h_inputencoding << "\n"
1001 << "\\fontencoding " << h_fontencoding << "\n"
1002 << "\\font_roman " << h_font_roman << "\n"
1003 << "\\font_sans " << h_font_sans << "\n"
1004 << "\\font_typewriter " << h_font_typewriter << "\n"
1005 << "\\font_math " << h_font_math << "\n"
1006 << "\\font_default_family " << h_font_default_family << "\n"
1007 << "\\use_non_tex_fonts " << h_use_non_tex_fonts << "\n"
1008 << "\\font_sc " << h_font_sc << "\n"
1009 << "\\font_osf " << h_font_osf << "\n"
1010 << "\\font_sf_scale " << h_font_sf_scale << "\n"
1011 << "\\font_tt_scale " << h_font_tt_scale << '\n';
1012 if (!h_font_cjk.empty())
1013 os << "\\font_cjk " << h_font_cjk << '\n';
1014 os << "\\graphics " << h_graphics << '\n'
1015 << "\\default_output_format " << h_default_output_format << "\n"
1016 << "\\output_sync " << h_output_sync << "\n";
1017 if (h_output_sync == "1")
1018 os << "\\output_sync_macro \"" << h_output_sync_macro << "\"\n";
1019 os << "\\bibtex_command " << h_bibtex_command << "\n"
1020 << "\\index_command " << h_index_command << "\n";
1021 if (!h_float_placement.empty())
1022 os << "\\float_placement " << h_float_placement << "\n";
1023 os << "\\paperfontsize " << h_paperfontsize << "\n"
1024 << "\\spacing " << h_spacing << "\n"
1025 << "\\use_hyperref " << h_use_hyperref << '\n';
1026 if (h_use_hyperref == "true") {
1027 if (!h_pdf_title.empty())
1028 os << "\\pdf_title \"" << h_pdf_title << "\"\n";
1029 if (!h_pdf_author.empty())
1030 os << "\\pdf_author \"" << h_pdf_author << "\"\n";
1031 if (!h_pdf_subject.empty())
1032 os << "\\pdf_subject \"" << h_pdf_subject << "\"\n";
1033 if (!h_pdf_keywords.empty())
1034 os << "\\pdf_keywords \"" << h_pdf_keywords << "\"\n";
1035 os << "\\pdf_bookmarks " << h_pdf_bookmarks << "\n"
1036 "\\pdf_bookmarksnumbered " << h_pdf_bookmarksnumbered << "\n"
1037 "\\pdf_bookmarksopen " << h_pdf_bookmarksopen << "\n"
1038 "\\pdf_bookmarksopenlevel " << h_pdf_bookmarksopenlevel << "\n"
1039 "\\pdf_breaklinks " << h_pdf_breaklinks << "\n"
1040 "\\pdf_pdfborder " << h_pdf_pdfborder << "\n"
1041 "\\pdf_colorlinks " << h_pdf_colorlinks << "\n"
1042 "\\pdf_backref " << h_pdf_backref << "\n"
1043 "\\pdf_pdfusetitle " << h_pdf_pdfusetitle << '\n';
1044 if (!h_pdf_pagemode.empty())
1045 os << "\\pdf_pagemode " << h_pdf_pagemode << '\n';
1046 if (!h_pdf_quoted_options.empty())
1047 os << "\\pdf_quoted_options \"" << h_pdf_quoted_options << "\"\n";
1049 os << "\\papersize " << h_papersize << "\n"
1050 << "\\use_geometry " << h_use_geometry << '\n';
1051 for (map<string, string>::const_iterator it = h_use_packages.begin();
1052 it != h_use_packages.end(); ++it)
1053 os << "\\use_package " << it->first << ' ' << it->second << '\n';
1054 os << "\\cite_engine " << h_cite_engine << '\n'
1055 << "\\cite_engine_type " << h_cite_engine_type << '\n'
1056 << "\\biblio_style " << h_biblio_style << "\n"
1057 << "\\use_bibtopic " << h_use_bibtopic << "\n"
1058 << "\\use_indices " << h_use_indices << "\n"
1059 << "\\paperorientation " << h_paperorientation << '\n'
1060 << "\\suppress_date " << h_suppress_date << '\n'
1061 << "\\justification " << h_justification << '\n'
1062 << "\\use_refstyle " << h_use_refstyle << '\n';
1063 if (!h_fontcolor.empty())
1064 os << "\\fontcolor " << h_fontcolor << '\n';
1065 if (!h_notefontcolor.empty())
1066 os << "\\notefontcolor " << h_notefontcolor << '\n';
1067 if (!h_backgroundcolor.empty())
1068 os << "\\backgroundcolor " << h_backgroundcolor << '\n';
1069 if (!h_boxbgcolor.empty())
1070 os << "\\boxbgcolor " << h_boxbgcolor << '\n';
1071 os << "\\index " << h_index << '\n'
1072 << "\\shortcut " << h_shortcut << '\n'
1073 << "\\color " << h_color << '\n'
1076 << "\\secnumdepth " << h_secnumdepth << "\n"
1077 << "\\tocdepth " << h_tocdepth << "\n"
1078 << "\\paragraph_separation " << h_paragraph_separation << "\n";
1079 if (h_paragraph_separation == "skip")
1080 os << "\\defskip " << h_defskip << "\n";
1082 os << "\\paragraph_indentation " << h_paragraph_indentation << "\n";
1083 os << "\\quotes_language " << h_quotes_language << "\n"
1084 << "\\papercolumns " << h_papercolumns << "\n"
1085 << "\\papersides " << h_papersides << "\n"
1086 << "\\paperpagestyle " << h_paperpagestyle << "\n";
1087 if (!h_listings_params.empty())
1088 os << "\\listings_params " << h_listings_params << "\n";
1089 os << "\\tracking_changes " << h_tracking_changes << "\n"
1090 << "\\output_changes " << h_output_changes << "\n"
1091 << "\\html_math_output " << h_html_math_output << "\n"
1092 << "\\html_css_as_file " << h_html_css_as_file << "\n"
1093 << "\\html_be_strict " << h_html_be_strict << "\n"
1095 << "\\end_header\n\n"
1096 << "\\begin_body\n";
1101 void Preamble::parse(Parser & p, string const & forceclass,
1102 TeX2LyXDocClass & tc)
1104 // initialize fixed types
1105 special_columns['D'] = 3;
1106 bool is_full_document = false;
1107 bool is_lyx_file = false;
1108 bool in_lyx_preamble = false;
1110 // determine whether this is a full document or a fragment for inclusion
1112 Token const & t = p.get_token();
1114 if (t.cat() == catEscape && t.cs() == "documentclass") {
1115 is_full_document = true;
1121 while (is_full_document && p.good()) {
1122 Token const & t = p.get_token();
1125 cerr << "t: " << t << "\n";
1131 if (!in_lyx_preamble &&
1132 (t.cat() == catLetter ||
1133 t.cat() == catSuper ||
1134 t.cat() == catSub ||
1135 t.cat() == catOther ||
1136 t.cat() == catMath ||
1137 t.cat() == catActive ||
1138 t.cat() == catBegin ||
1139 t.cat() == catEnd ||
1140 t.cat() == catAlign ||
1141 t.cat() == catParameter))
1142 h_preamble << t.cs();
1144 else if (!in_lyx_preamble &&
1145 (t.cat() == catSpace || t.cat() == catNewline))
1146 h_preamble << t.asInput();
1148 else if (t.cat() == catComment) {
1149 static regex const islyxfile("%% LyX .* created this file");
1150 static regex const usercommands("User specified LaTeX commands");
1152 string const comment = t.asInput();
1154 // magically switch encoding default if it looks like XeLaTeX
1155 static string const magicXeLaTeX =
1156 "% This document must be compiled with XeLaTeX ";
1157 if (comment.size() > magicXeLaTeX.size()
1158 && comment.substr(0, magicXeLaTeX.size()) == magicXeLaTeX
1159 && h_inputencoding == "auto") {
1160 cerr << "XeLaTeX comment found, switching to UTF8\n";
1161 h_inputencoding = "utf8";
1164 if (regex_search(comment, sub, islyxfile)) {
1166 in_lyx_preamble = true;
1167 } else if (is_lyx_file
1168 && regex_search(comment, sub, usercommands))
1169 in_lyx_preamble = false;
1170 else if (!in_lyx_preamble)
1171 h_preamble << t.asInput();
1174 else if (t.cs() == "pagestyle")
1175 h_paperpagestyle = p.verbatim_item();
1177 else if (t.cs() == "setdefaultlanguage") {
1179 // We don't yet care about non-language variant options
1180 // because LyX doesn't support this yet, see bug #8214
1182 string langopts = p.getOpt();
1183 // check if the option contains a variant, if yes, extract it
1184 string::size_type pos_var = langopts.find("variant");
1185 string::size_type i = langopts.find(',', pos_var);
1186 string::size_type k = langopts.find('=', pos_var);
1187 if (pos_var != string::npos){
1189 if (i == string::npos)
1190 variant = langopts.substr(k + 1, langopts.length() - k - 2);
1192 variant = langopts.substr(k + 1, i - k - 1);
1193 h_language = variant;
1197 h_language = p.verbatim_item();
1198 //finally translate the poyglossia name to a LyX name
1199 h_language = polyglossia2lyx(h_language);
1202 else if (t.cs() == "setotherlanguage") {
1203 // We don't yet care about the option because LyX doesn't
1204 // support this yet, see bug #8214
1205 p.hasOpt() ? p.getOpt() : string();
1209 else if (t.cs() == "setmainfont") {
1210 // we don't care about the option
1211 p.hasOpt() ? p.getOpt() : string();
1212 h_font_roman = p.getArg('{', '}');
1215 else if (t.cs() == "setsansfont" || t.cs() == "setmonofont") {
1216 // LyX currently only supports the scale option
1219 string fontopts = p.getArg('[', ']');
1220 // check if the option contains a scaling, if yes, extract it
1221 string::size_type pos = fontopts.find("Scale");
1222 if (pos != string::npos) {
1223 string::size_type i = fontopts.find(',', pos);
1224 if (i == string::npos)
1225 scale_as_percentage(fontopts.substr(pos + 1), scale);
1227 scale_as_percentage(fontopts.substr(pos, i - pos), scale);
1230 if (t.cs() == "setsansfont") {
1232 h_font_sf_scale = scale;
1233 h_font_sans = p.getArg('{', '}');
1236 h_font_tt_scale = scale;
1237 h_font_typewriter = p.getArg('{', '}');
1241 else if (t.cs() == "date") {
1242 string argument = p.getArg('{', '}');
1243 if (argument.empty())
1244 h_suppress_date = "true";
1246 h_preamble << t.asInput() << '{' << argument << '}';
1249 else if (t.cs() == "color") {
1250 string const space =
1251 (p.hasOpt() ? p.getOpt() : string());
1252 string argument = p.getArg('{', '}');
1253 // check the case that a standard color is used
1254 if (space.empty() && is_known(argument, known_basic_colors)) {
1255 h_fontcolor = rgbcolor2code(argument);
1256 preamble.registerAutomaticallyLoadedPackage("color");
1257 } else if (space.empty() && argument == "document_fontcolor")
1258 preamble.registerAutomaticallyLoadedPackage("color");
1259 // check the case that LyX's document_fontcolor is defined
1260 // but not used for \color
1262 h_preamble << t.asInput();
1264 h_preamble << space;
1265 h_preamble << '{' << argument << '}';
1266 // the color might already be set because \definecolor
1267 // is parsed before this
1272 else if (t.cs() == "pagecolor") {
1273 string argument = p.getArg('{', '}');
1274 // check the case that a standard color is used
1275 if (is_known(argument, known_basic_colors)) {
1276 h_backgroundcolor = rgbcolor2code(argument);
1277 } else if (argument == "page_backgroundcolor")
1278 preamble.registerAutomaticallyLoadedPackage("color");
1279 // check the case that LyX's page_backgroundcolor is defined
1280 // but not used for \pagecolor
1282 h_preamble << t.asInput() << '{' << argument << '}';
1283 // the color might already be set because \definecolor
1284 // is parsed before this
1285 h_backgroundcolor = "";
1289 else if (t.cs() == "makeatletter") {
1290 // LyX takes care of this
1291 p.setCatCode('@', catLetter);
1294 else if (t.cs() == "makeatother") {
1295 // LyX takes care of this
1296 p.setCatCode('@', catOther);
1299 else if (t.cs() == "newcommand" || t.cs() == "newcommandx"
1300 || t.cs() == "renewcommand" || t.cs() == "renewcommandx"
1301 || t.cs() == "providecommand" || t.cs() == "providecommandx"
1302 || t.cs() == "DeclareRobustCommand"
1303 || t.cs() == "DeclareRobustCommandx"
1304 || t.cs() == "ProvideTextCommandDefault"
1305 || t.cs() == "DeclareMathAccent") {
1307 if (p.next_token().character() == '*') {
1311 string const name = p.verbatim_item();
1312 string const opt1 = p.getFullOpt();
1313 string const opt2 = p.getFullOpt();
1314 string const body = p.verbatim_item();
1316 if (name == "\\rmdefault")
1317 if (is_known(body, known_roman_fonts))
1318 h_font_roman = body;
1319 if (name == "\\sfdefault")
1320 if (is_known(body, known_sans_fonts))
1322 if (name == "\\ttdefault")
1323 if (is_known(body, known_typewriter_fonts))
1324 h_font_typewriter = body;
1325 if (name == "\\familydefault") {
1326 string family = body;
1327 // remove leading "\"
1328 h_font_default_family = family.erase(0,1);
1331 // remove the lyxdot definition that is re-added by LyX
1333 if (name == "\\lyxdot")
1334 in_lyx_preamble = true;
1336 // Add the command to the known commands
1337 add_known_command(name, opt1, !opt2.empty(), from_utf8(body));
1339 // only non-lyxspecific stuff
1340 if (!in_lyx_preamble) {
1342 ss << '\\' << t.cs();
1345 ss << '{' << name << '}' << opt1 << opt2
1346 << '{' << body << "}";
1347 h_preamble << ss.str();
1349 ostream & out = in_preamble ? h_preamble : os;
1350 out << "\\" << t.cs() << "{" << name << "}"
1351 << opts << "{" << body << "}";
1356 else if (t.cs() == "documentclass") {
1357 vector<string>::iterator it;
1358 vector<string> opts = split_options(p.getArg('[', ']'));
1359 handle_opt(opts, known_fontsizes, h_paperfontsize);
1360 delete_opt(opts, known_fontsizes);
1361 // delete "pt" at the end
1362 string::size_type i = h_paperfontsize.find("pt");
1363 if (i != string::npos)
1364 h_paperfontsize.erase(i);
1365 // The documentclass options are always parsed before the options
1366 // of the babel call so that a language cannot overwrite the babel
1368 handle_opt(opts, known_languages, h_language);
1369 delete_opt(opts, known_languages);
1371 // paper orientation
1372 if ((it = find(opts.begin(), opts.end(), "landscape")) != opts.end()) {
1373 h_paperorientation = "landscape";
1377 if ((it = find(opts.begin(), opts.end(), "oneside"))
1382 if ((it = find(opts.begin(), opts.end(), "twoside"))
1388 if ((it = find(opts.begin(), opts.end(), "onecolumn"))
1390 h_papercolumns = "1";
1393 if ((it = find(opts.begin(), opts.end(), "twocolumn"))
1395 h_papercolumns = "2";
1399 // some size options are known to any document classes, other sizes
1400 // are handled by the \geometry command of the geometry package
1401 handle_opt(opts, known_class_paper_sizes, h_papersize);
1402 delete_opt(opts, known_class_paper_sizes);
1403 // the remaining options
1404 h_options = join(opts, ",");
1405 // FIXME This does not work for classes that have a
1406 // different name in LyX than in LaTeX
1407 h_textclass = p.getArg('{', '}');
1410 else if (t.cs() == "usepackage") {
1411 string const options = p.getArg('[', ']');
1412 string const name = p.getArg('{', '}');
1413 vector<string> vecnames;
1414 split(name, vecnames, ',');
1415 vector<string>::const_iterator it = vecnames.begin();
1416 vector<string>::const_iterator end = vecnames.end();
1417 for (; it != end; ++it)
1418 handle_package(p, trimSpaceAndEol(*it), options,
1422 else if (t.cs() == "inputencoding") {
1423 string const encoding = p.getArg('{','}');
1424 h_inputencoding = encoding;
1425 p.setEncoding(encoding, Encoding::inputenc);
1428 else if (t.cs() == "newenvironment") {
1429 string const name = p.getArg('{', '}');
1430 string const opt1 = p.getFullOpt();
1431 string const opt2 = p.getFullOpt();
1432 string const beg = p.verbatim_item();
1433 string const end = p.verbatim_item();
1434 if (!in_lyx_preamble) {
1435 h_preamble << "\\newenvironment{" << name
1436 << '}' << opt1 << opt2 << '{'
1437 << beg << "}{" << end << '}';
1439 add_known_environment(name, opt1, !opt2.empty(),
1440 from_utf8(beg), from_utf8(end));
1444 else if (t.cs() == "newtheorem") {
1445 string const name = p.getArg('{', '}');
1446 string const opt1 = p.getFullOpt();
1447 string const opt2 = p.getFullOpt();
1448 string const body = p.verbatim_item();
1449 string const opt3 = p.getFullOpt();
1451 add_known_theorem(name, opt1, !opt2.empty(),
1452 from_utf8("\\newtheorem{" + name + '}' +
1453 opt1 + opt2 + '{' + body + '}' + opt3));
1455 if (!in_lyx_preamble)
1456 h_preamble << "\\newtheorem{" << name << '}'
1457 << opt1 << opt2 << '{' << '}' << opt3;
1460 else if (t.cs() == "def") {
1461 string name = p.get_token().cs();
1462 // In fact, name may be more than the name:
1463 // In the test case of bug 8116
1464 // name == "csname SF@gobble@opt \endcsname".
1465 // Therefore, we need to use asInput() instead of cs().
1466 while (p.next_token().cat() != catBegin)
1467 name += p.get_token().asInput();
1468 if (!in_lyx_preamble)
1469 h_preamble << "\\def\\" << name << '{'
1470 << p.verbatim_item() << "}";
1473 else if (t.cs() == "newcolumntype") {
1474 string const name = p.getArg('{', '}');
1475 trimSpaceAndEol(name);
1477 string opts = p.getOpt();
1478 if (!opts.empty()) {
1479 istringstream is(string(opts, 1));
1482 special_columns[name[0]] = nargs;
1483 h_preamble << "\\newcolumntype{" << name << "}";
1485 h_preamble << "[" << nargs << "]";
1486 h_preamble << "{" << p.verbatim_item() << "}";
1489 else if (t.cs() == "setcounter") {
1490 string const name = p.getArg('{', '}');
1491 string const content = p.getArg('{', '}');
1492 if (name == "secnumdepth")
1493 h_secnumdepth = content;
1494 else if (name == "tocdepth")
1495 h_tocdepth = content;
1497 h_preamble << "\\setcounter{" << name << "}{" << content << "}";
1500 else if (t.cs() == "setlength") {
1501 string const name = p.verbatim_item();
1502 string const content = p.verbatim_item();
1503 // the paragraphs are only not indented when \parindent is set to zero
1504 if (name == "\\parindent" && content != "") {
1505 if (content[0] == '0')
1506 h_paragraph_separation = "skip";
1508 h_paragraph_indentation = translate_len(content);
1509 } else if (name == "\\parskip") {
1510 if (content == "\\smallskipamount")
1511 h_defskip = "smallskip";
1512 else if (content == "\\medskipamount")
1513 h_defskip = "medskip";
1514 else if (content == "\\bigskipamount")
1515 h_defskip = "bigskip";
1517 h_defskip = content;
1519 h_preamble << "\\setlength{" << name << "}{" << content << "}";
1522 else if (t.cs() == "onehalfspacing")
1523 h_spacing = "onehalf";
1525 else if (t.cs() == "doublespacing")
1526 h_spacing = "double";
1528 else if (t.cs() == "setstretch")
1529 h_spacing = "other " + p.verbatim_item();
1531 else if (t.cs() == "synctex") {
1532 // the scheme is \synctex=value
1533 // where value can only be "1" or "-1"
1534 h_output_sync = "1";
1535 // there can be any character behind the value (e.g. a linebreak or a '\'
1536 // therefore we extract it char by char
1538 string value = p.get_token().asInput();
1540 value += p.get_token().asInput();
1541 h_output_sync_macro = "\\synctex=" + value;
1544 else if (t.cs() == "begin") {
1545 string const name = p.getArg('{', '}');
1546 if (name == "document")
1548 h_preamble << "\\begin{" << name << "}";
1551 else if (t.cs() == "geometry") {
1552 vector<string> opts = split_options(p.getArg('{', '}'));
1553 handle_geometry(opts);
1556 else if (t.cs() == "definecolor") {
1557 string const color = p.getArg('{', '}');
1558 string const space = p.getArg('{', '}');
1559 string const value = p.getArg('{', '}');
1560 if (color == "document_fontcolor" && space == "rgb") {
1561 RGBColor c(RGBColorFromLaTeX(value));
1562 h_fontcolor = X11hexname(c);
1563 } else if (color == "note_fontcolor" && space == "rgb") {
1564 RGBColor c(RGBColorFromLaTeX(value));
1565 h_notefontcolor = X11hexname(c);
1566 } else if (color == "page_backgroundcolor" && space == "rgb") {
1567 RGBColor c(RGBColorFromLaTeX(value));
1568 h_backgroundcolor = X11hexname(c);
1569 } else if (color == "shadecolor" && space == "rgb") {
1570 RGBColor c(RGBColorFromLaTeX(value));
1571 h_boxbgcolor = X11hexname(c);
1573 h_preamble << "\\definecolor{" << color
1574 << "}{" << space << "}{" << value
1579 else if (t.cs() == "bibliographystyle")
1580 h_biblio_style = p.verbatim_item();
1582 else if (t.cs() == "jurabibsetup") {
1583 // FIXME p.getArg('{', '}') is most probably wrong (it
1584 // does not handle nested braces).
1585 // Use p.verbatim_item() instead.
1586 vector<string> jurabibsetup =
1587 split_options(p.getArg('{', '}'));
1588 // add jurabibsetup to the jurabib package options
1589 add_package("jurabib", jurabibsetup);
1590 if (!jurabibsetup.empty()) {
1591 h_preamble << "\\jurabibsetup{"
1592 << join(jurabibsetup, ",") << '}';
1596 else if (t.cs() == "hypersetup") {
1597 vector<string> hypersetup =
1598 split_options(p.verbatim_item());
1599 // add hypersetup to the hyperref package options
1600 handle_hyperref(hypersetup);
1601 if (!hypersetup.empty()) {
1602 h_preamble << "\\hypersetup{"
1603 << join(hypersetup, ",") << '}';
1607 else if (is_known(t.cs(), known_if_3arg_commands)) {
1608 // prevent misparsing of \usepackage if it is used
1609 // as an argument (see e.g. our own output of
1610 // \@ifundefined above)
1611 string const arg1 = p.verbatim_item();
1612 string const arg2 = p.verbatim_item();
1613 string const arg3 = p.verbatim_item();
1614 // test case \@ifundefined{date}{}{\date{}}
1615 if (t.cs() == "@ifundefined" && arg1 == "date" &&
1616 arg2.empty() && arg3 == "\\date{}") {
1617 h_suppress_date = "true";
1618 // older tex2lyx versions did output
1619 // \@ifundefined{definecolor}{\usepackage{color}}{}
1620 } else if (t.cs() == "@ifundefined" &&
1621 arg1 == "definecolor" &&
1622 arg2 == "\\usepackage{color}" &&
1624 if (!in_lyx_preamble)
1625 h_preamble << package_beg_sep
1628 << "\\@ifundefined{definecolor}{color}{}"
1631 //\@ifundefined{showcaptionsetup}{}{%
1632 // \PassOptionsToPackage{caption=false}{subfig}}
1633 // that LyX uses for subfloats
1634 } else if (t.cs() == "@ifundefined" &&
1635 arg1 == "showcaptionsetup" && arg2.empty()
1636 && arg3 == "%\n \\PassOptionsToPackage{caption=false}{subfig}") {
1638 } else if (!in_lyx_preamble) {
1639 h_preamble << t.asInput()
1640 << '{' << arg1 << '}'
1641 << '{' << arg2 << '}'
1642 << '{' << arg3 << '}';
1646 else if (is_known(t.cs(), known_if_commands)) {
1647 // must not parse anything in conditional code, since
1648 // LyX would output the parsed contents unconditionally
1649 if (!in_lyx_preamble)
1650 h_preamble << t.asInput();
1651 handle_if(p, in_lyx_preamble);
1654 else if (!t.cs().empty() && !in_lyx_preamble)
1655 h_preamble << '\\' << t.cs();
1658 // remove the whitespace
1661 // Force textclass if the user wanted it
1662 if (!forceclass.empty())
1663 h_textclass = forceclass;
1664 tc.setName(h_textclass);
1666 cerr << "Error: Could not read layout file for textclass \"" << h_textclass << "\"." << endl;
1669 if (h_papersides.empty()) {
1672 h_papersides = ss.str();
1675 // If the CJK package is used we cannot set the document language from
1676 // the babel options. Instead, we guess which language is used most
1677 // and set this one.
1678 default_language = h_language;
1679 if (is_full_document &&
1680 (auto_packages.find("CJK") != auto_packages.end() ||
1681 auto_packages.find("CJKutf8") != auto_packages.end())) {
1683 h_language = guessLanguage(p, default_language);
1689 string babel2lyx(string const & language)
1691 char const * const * where = is_known(language, known_languages);
1693 return known_coded_languages[where - known_languages];
1698 string Preamble::polyglossia2lyx(string const & language)
1700 char const * const * where = is_known(language, polyglossia_languages);
1702 return coded_polyglossia_languages[where - polyglossia_languages];
1707 string rgbcolor2code(string const & name)
1709 char const * const * where = is_known(name, known_basic_colors);
1711 // "red", "green" etc
1712 return known_basic_color_codes[where - known_basic_colors];
1714 // "255,0,0", "0,255,0" etc
1715 RGBColor c(RGBColorFromLaTeX(name));
1716 return X11hexname(c);