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 if (h_language == "japanese") {
731 // For Japanese, the encoding isn't indicated in the source
732 // file, and there's really not much we can do. We could
733 // 1) offer a list of possible encodings to choose from, or
734 // 2) determine the encoding of the file by inspecting it.
735 // For the time being, we leave the encoding alone so that
736 // we don't get iconv errors when making a wrong guess, and
737 // we will output a note at the top of the document
738 // explaining what to do.
739 Encoding const * const enc = encodings.fromIconvName(
740 p.getEncoding(), Encoding::japanese, true);
742 h_inputencoding = enc->name();
743 is_nonCJKJapanese = true;
744 // in this case babel can be removed from the preamble
745 registerAutomaticallyLoadedPackage("babel");
747 // If babel is called with options, LyX puts them by default into the
748 // document class options. This works for most languages, except
749 // for Latvian, Lithuanian, Mongolian, Turkmen and Vietnamese and
750 // perhaps in future others.
751 // Therefore keep the babel call as it is as the user might have
753 h_preamble << "\\usepackage[" << opts << "]{babel}\n";
755 delete_opt(options, known_languages);
758 h_preamble << "\\usepackage{babel}\n";
761 else if (name == "polyglossia") {
762 h_language_package = "default";
763 h_default_output_format = "pdf4";
764 h_use_non_tex_fonts = "true";
766 registerAutomaticallyLoadedPackage("xunicode");
767 if (h_inputencoding == "auto")
768 p.setEncoding("UTF-8");
771 else if (name == "CJK") {
772 // set the encoding to "auto" because it might be set to "default" by the babel handling
773 // and this would not be correct for CJK
774 if (h_inputencoding == "default")
775 h_inputencoding = "auto";
776 registerAutomaticallyLoadedPackage("CJK");
779 else if (name == "CJKutf8") {
780 h_inputencoding = "UTF8";
781 p.setEncoding("UTF-8");
782 registerAutomaticallyLoadedPackage("CJKutf8");
785 else if (name == "fontenc") {
786 h_fontencoding = getStringFromVector(options, ",");
787 /* We could do the following for better round trip support,
788 * but this makes the document less portable, so I skip it:
789 if (h_fontencoding == lyxrc.fontenc)
790 h_fontencoding = "global";
795 else if (name == "inputenc" || name == "luainputenc") {
796 // h_inputencoding is only set when there is not more than one
797 // inputenc option because otherwise h_inputencoding must be
798 // set to "auto" (the default encoding of the document language)
799 // Therefore check for the "," character.
800 // It is also only set when there is not more than one babel
802 if (opts.find(",") == string::npos && one_language == true)
803 h_inputencoding = opts;
804 if (!options.empty())
805 p.setEncoding(options.back(), Encoding::inputenc);
809 else if (name == "srcltx") {
812 h_output_sync_macro = "\\usepackage[" + opts + "]{srcltx}";
815 h_output_sync_macro = "\\usepackage{srcltx}";
818 else if (is_known(name, known_old_language_packages)) {
819 // known language packages from the times before babel
820 // if they are found and not also babel, they will be used as
821 // custom language package
822 h_language_package = "\\usepackage{" + name + "}";
825 else if (name == "prettyref")
826 ; // ignore this FIXME: Use the package separator mechanism instead
828 else if (name == "lyxskak") {
829 // ignore this and its options
830 const char * const o[] = {"ps", "mover", 0};
831 delete_opt(options, o);
834 else if (is_known(name, known_lyx_packages) && options.empty()) {
835 if (name == "splitidx")
836 h_use_indices = "true";
837 if (!in_lyx_preamble) {
838 h_preamble << package_beg_sep << name
839 << package_mid_sep << "\\usepackage{"
841 if (p.next_token().cat() == catNewline ||
842 (p.next_token().cat() == catSpace &&
843 p.next_next_token().cat() == catNewline))
845 h_preamble << package_end_sep;
849 else if (name == "geometry")
850 handle_geometry(options);
852 else if (name == "subfig")
853 ; // ignore this FIXME: Use the package separator mechanism instead
855 else if ((where = is_known(name, known_languages)))
856 h_language = known_coded_languages[where - known_languages];
858 else if (name == "natbib") {
859 h_biblio_style = "plainnat";
860 h_cite_engine = "natbib";
861 h_cite_engine_type = "authoryear";
862 vector<string>::iterator it =
863 find(options.begin(), options.end(), "authoryear");
864 if (it != options.end())
867 it = find(options.begin(), options.end(), "numbers");
868 if (it != options.end()) {
869 h_cite_engine_type = "numerical";
875 else if (name == "jurabib") {
876 h_biblio_style = "jurabib";
877 h_cite_engine = "jurabib";
878 h_cite_engine_type = "authoryear";
881 else if (name == "hyperref")
882 handle_hyperref(options);
884 else if (!in_lyx_preamble) {
886 h_preamble << "\\usepackage{" << name << '}';
888 h_preamble << "\\usepackage[" << opts << "]{"
892 if (p.next_token().cat() == catNewline ||
893 (p.next_token().cat() == catSpace &&
894 p.next_next_token().cat() == catNewline))
898 // We need to do something with the options...
899 if (!options.empty())
900 cerr << "Ignoring options '" << join(options, ",")
901 << "' of package " << name << '.' << endl;
903 // remove the whitespace
908 void Preamble::handle_if(Parser & p, bool in_lyx_preamble)
911 Token t = p.get_token();
912 if (t.cat() == catEscape &&
913 is_known(t.cs(), known_if_commands))
914 handle_if(p, in_lyx_preamble);
916 if (!in_lyx_preamble)
917 h_preamble << t.asInput();
918 if (t.cat() == catEscape && t.cs() == "fi")
925 bool Preamble::writeLyXHeader(ostream & os, bool subdoc)
927 // set the quote language
928 // LyX only knows the following quotes languages:
929 // english, swedish, german, polish, french and danish
930 // (quotes for "japanese" and "chinese-traditional" are missing because
931 // they wouldn't be useful: http://www.lyx.org/trac/ticket/6383)
932 // conversion list taken from
933 // http://en.wikipedia.org/wiki/Quotation_mark,_non-English_usage
934 // (quotes for kazakh and interlingua are unknown)
936 if (is_known(h_language, known_danish_quotes_languages))
937 h_quotes_language = "danish";
939 else if (is_known(h_language, known_french_quotes_languages))
940 h_quotes_language = "french";
942 else if (is_known(h_language, known_german_quotes_languages))
943 h_quotes_language = "german";
945 else if (is_known(h_language, known_polish_quotes_languages))
946 h_quotes_language = "polish";
948 else if (is_known(h_language, known_swedish_quotes_languages))
949 h_quotes_language = "swedish";
951 else if (is_known(h_language, known_english_quotes_languages))
952 h_quotes_language = "english";
954 if (contains(h_float_placement, "H"))
955 registerAutomaticallyLoadedPackage("float");
956 if (h_spacing != "single" && h_spacing != "default")
957 registerAutomaticallyLoadedPackage("setspace");
958 if (h_use_packages["amsmath"] == "2") {
959 // amsbsy and amstext are already provided by amsmath
960 registerAutomaticallyLoadedPackage("amsbsy");
961 registerAutomaticallyLoadedPackage("amstext");
964 // output the LyX file settings
965 os << "#LyX file created by tex2lyx " << PACKAGE_VERSION << "\n"
966 << "\\lyxformat " << LYX_FORMAT << '\n'
967 << "\\begin_document\n"
968 << "\\begin_header\n"
969 << "\\textclass " << h_textclass << "\n";
970 string const raw = subdoc ? empty_string() : h_preamble.str();
972 os << "\\begin_preamble\n";
973 for (string::size_type i = 0; i < raw.size(); ++i) {
974 if (raw[i] == package_beg_sep) {
975 // Here follows some package loading code that
976 // must be skipped if the package is loaded
978 string::size_type j = raw.find(package_mid_sep, i);
979 if (j == string::npos)
981 string::size_type k = raw.find(package_end_sep, j);
982 if (k == string::npos)
984 string const package = raw.substr(i + 1, j - i - 1);
985 string const replacement = raw.substr(j + 1, k - j - 1);
986 if (auto_packages.find(package) == auto_packages.end())
992 os << "\n\\end_preamble\n";
994 if (!h_options.empty())
995 os << "\\options " << h_options << "\n";
996 os << "\\use_default_options " << h_use_default_options << "\n";
997 if (!used_modules.empty()) {
998 os << "\\begin_modules\n";
999 vector<string>::const_iterator const end = used_modules.end();
1000 vector<string>::const_iterator it = used_modules.begin();
1001 for (; it != end; ++it)
1003 os << "\\end_modules\n";
1005 os << "\\maintain_unincluded_children " << h_maintain_unincluded_children << "\n"
1006 << "\\language " << h_language << "\n"
1007 << "\\language_package " << h_language_package << "\n"
1008 << "\\inputencoding " << h_inputencoding << "\n"
1009 << "\\fontencoding " << h_fontencoding << "\n"
1010 << "\\font_roman " << h_font_roman << "\n"
1011 << "\\font_sans " << h_font_sans << "\n"
1012 << "\\font_typewriter " << h_font_typewriter << "\n"
1013 << "\\font_math " << h_font_math << "\n"
1014 << "\\font_default_family " << h_font_default_family << "\n"
1015 << "\\use_non_tex_fonts " << h_use_non_tex_fonts << "\n"
1016 << "\\font_sc " << h_font_sc << "\n"
1017 << "\\font_osf " << h_font_osf << "\n"
1018 << "\\font_sf_scale " << h_font_sf_scale << "\n"
1019 << "\\font_tt_scale " << h_font_tt_scale << '\n';
1020 if (!h_font_cjk.empty())
1021 os << "\\font_cjk " << h_font_cjk << '\n';
1022 os << "\\graphics " << h_graphics << '\n'
1023 << "\\default_output_format " << h_default_output_format << "\n"
1024 << "\\output_sync " << h_output_sync << "\n";
1025 if (h_output_sync == "1")
1026 os << "\\output_sync_macro \"" << h_output_sync_macro << "\"\n";
1027 os << "\\bibtex_command " << h_bibtex_command << "\n"
1028 << "\\index_command " << h_index_command << "\n";
1029 if (!h_float_placement.empty())
1030 os << "\\float_placement " << h_float_placement << "\n";
1031 os << "\\paperfontsize " << h_paperfontsize << "\n"
1032 << "\\spacing " << h_spacing << "\n"
1033 << "\\use_hyperref " << h_use_hyperref << '\n';
1034 if (h_use_hyperref == "true") {
1035 if (!h_pdf_title.empty())
1036 os << "\\pdf_title \"" << h_pdf_title << "\"\n";
1037 if (!h_pdf_author.empty())
1038 os << "\\pdf_author \"" << h_pdf_author << "\"\n";
1039 if (!h_pdf_subject.empty())
1040 os << "\\pdf_subject \"" << h_pdf_subject << "\"\n";
1041 if (!h_pdf_keywords.empty())
1042 os << "\\pdf_keywords \"" << h_pdf_keywords << "\"\n";
1043 os << "\\pdf_bookmarks " << h_pdf_bookmarks << "\n"
1044 "\\pdf_bookmarksnumbered " << h_pdf_bookmarksnumbered << "\n"
1045 "\\pdf_bookmarksopen " << h_pdf_bookmarksopen << "\n"
1046 "\\pdf_bookmarksopenlevel " << h_pdf_bookmarksopenlevel << "\n"
1047 "\\pdf_breaklinks " << h_pdf_breaklinks << "\n"
1048 "\\pdf_pdfborder " << h_pdf_pdfborder << "\n"
1049 "\\pdf_colorlinks " << h_pdf_colorlinks << "\n"
1050 "\\pdf_backref " << h_pdf_backref << "\n"
1051 "\\pdf_pdfusetitle " << h_pdf_pdfusetitle << '\n';
1052 if (!h_pdf_pagemode.empty())
1053 os << "\\pdf_pagemode " << h_pdf_pagemode << '\n';
1054 if (!h_pdf_quoted_options.empty())
1055 os << "\\pdf_quoted_options \"" << h_pdf_quoted_options << "\"\n";
1057 os << "\\papersize " << h_papersize << "\n"
1058 << "\\use_geometry " << h_use_geometry << '\n';
1059 for (map<string, string>::const_iterator it = h_use_packages.begin();
1060 it != h_use_packages.end(); ++it)
1061 os << "\\use_package " << it->first << ' ' << it->second << '\n';
1062 os << "\\cite_engine " << h_cite_engine << '\n'
1063 << "\\cite_engine_type " << h_cite_engine_type << '\n'
1064 << "\\biblio_style " << h_biblio_style << "\n"
1065 << "\\use_bibtopic " << h_use_bibtopic << "\n"
1066 << "\\use_indices " << h_use_indices << "\n"
1067 << "\\paperorientation " << h_paperorientation << '\n'
1068 << "\\suppress_date " << h_suppress_date << '\n'
1069 << "\\justification " << h_justification << '\n'
1070 << "\\use_refstyle " << h_use_refstyle << '\n';
1071 if (!h_fontcolor.empty())
1072 os << "\\fontcolor " << h_fontcolor << '\n';
1073 if (!h_notefontcolor.empty())
1074 os << "\\notefontcolor " << h_notefontcolor << '\n';
1075 if (!h_backgroundcolor.empty())
1076 os << "\\backgroundcolor " << h_backgroundcolor << '\n';
1077 if (!h_boxbgcolor.empty())
1078 os << "\\boxbgcolor " << h_boxbgcolor << '\n';
1079 os << "\\index " << h_index << '\n'
1080 << "\\shortcut " << h_shortcut << '\n'
1081 << "\\color " << h_color << '\n'
1084 << "\\secnumdepth " << h_secnumdepth << "\n"
1085 << "\\tocdepth " << h_tocdepth << "\n"
1086 << "\\paragraph_separation " << h_paragraph_separation << "\n";
1087 if (h_paragraph_separation == "skip")
1088 os << "\\defskip " << h_defskip << "\n";
1090 os << "\\paragraph_indentation " << h_paragraph_indentation << "\n";
1091 os << "\\quotes_language " << h_quotes_language << "\n"
1092 << "\\papercolumns " << h_papercolumns << "\n"
1093 << "\\papersides " << h_papersides << "\n"
1094 << "\\paperpagestyle " << h_paperpagestyle << "\n";
1095 if (!h_listings_params.empty())
1096 os << "\\listings_params " << h_listings_params << "\n";
1097 os << "\\tracking_changes " << h_tracking_changes << "\n"
1098 << "\\output_changes " << h_output_changes << "\n"
1099 << "\\html_math_output " << h_html_math_output << "\n"
1100 << "\\html_css_as_file " << h_html_css_as_file << "\n"
1101 << "\\html_be_strict " << h_html_be_strict << "\n"
1103 << "\\end_header\n\n"
1104 << "\\begin_body\n";
1109 void Preamble::parse(Parser & p, string const & forceclass,
1110 TeX2LyXDocClass & tc)
1112 // initialize fixed types
1113 special_columns['D'] = 3;
1114 bool is_full_document = false;
1115 bool is_lyx_file = false;
1116 bool in_lyx_preamble = false;
1118 // determine whether this is a full document or a fragment for inclusion
1120 Token const & t = p.get_token();
1122 if (t.cat() == catEscape && t.cs() == "documentclass") {
1123 is_full_document = true;
1129 while (is_full_document && p.good()) {
1130 Token const & t = p.get_token();
1133 cerr << "t: " << t << "\n";
1139 if (!in_lyx_preamble &&
1140 (t.cat() == catLetter ||
1141 t.cat() == catSuper ||
1142 t.cat() == catSub ||
1143 t.cat() == catOther ||
1144 t.cat() == catMath ||
1145 t.cat() == catActive ||
1146 t.cat() == catBegin ||
1147 t.cat() == catEnd ||
1148 t.cat() == catAlign ||
1149 t.cat() == catParameter))
1150 h_preamble << t.cs();
1152 else if (!in_lyx_preamble &&
1153 (t.cat() == catSpace || t.cat() == catNewline))
1154 h_preamble << t.asInput();
1156 else if (t.cat() == catComment) {
1157 static regex const islyxfile("%% LyX .* created this file");
1158 static regex const usercommands("User specified LaTeX commands");
1160 string const comment = t.asInput();
1162 // magically switch encoding default if it looks like XeLaTeX
1163 static string const magicXeLaTeX =
1164 "% This document must be compiled with XeLaTeX ";
1165 if (comment.size() > magicXeLaTeX.size()
1166 && comment.substr(0, magicXeLaTeX.size()) == magicXeLaTeX
1167 && h_inputencoding == "auto") {
1168 cerr << "XeLaTeX comment found, switching to UTF8\n";
1169 h_inputencoding = "utf8";
1172 if (regex_search(comment, sub, islyxfile)) {
1174 in_lyx_preamble = true;
1175 } else if (is_lyx_file
1176 && regex_search(comment, sub, usercommands))
1177 in_lyx_preamble = false;
1178 else if (!in_lyx_preamble)
1179 h_preamble << t.asInput();
1182 else if (t.cs() == "pagestyle")
1183 h_paperpagestyle = p.verbatim_item();
1185 else if (t.cs() == "setdefaultlanguage") {
1187 // We don't yet care about non-language variant options
1188 // because LyX doesn't support this yet, see bug #8214
1190 string langopts = p.getOpt();
1191 // check if the option contains a variant, if yes, extract it
1192 string::size_type pos_var = langopts.find("variant");
1193 string::size_type i = langopts.find(',', pos_var);
1194 string::size_type k = langopts.find('=', pos_var);
1195 if (pos_var != string::npos){
1197 if (i == string::npos)
1198 variant = langopts.substr(k + 1, langopts.length() - k - 2);
1200 variant = langopts.substr(k + 1, i - k - 1);
1201 h_language = variant;
1205 h_language = p.verbatim_item();
1206 //finally translate the poyglossia name to a LyX name
1207 h_language = polyglossia2lyx(h_language);
1210 else if (t.cs() == "setotherlanguage") {
1211 // We don't yet care about the option because LyX doesn't
1212 // support this yet, see bug #8214
1213 p.hasOpt() ? p.getOpt() : string();
1217 else if (t.cs() == "setmainfont") {
1218 // we don't care about the option
1219 p.hasOpt() ? p.getOpt() : string();
1220 h_font_roman = p.getArg('{', '}');
1223 else if (t.cs() == "setsansfont" || t.cs() == "setmonofont") {
1224 // LyX currently only supports the scale option
1227 string fontopts = p.getArg('[', ']');
1228 // check if the option contains a scaling, if yes, extract it
1229 string::size_type pos = fontopts.find("Scale");
1230 if (pos != string::npos) {
1231 string::size_type i = fontopts.find(',', pos);
1232 if (i == string::npos)
1233 scale_as_percentage(fontopts.substr(pos + 1), scale);
1235 scale_as_percentage(fontopts.substr(pos, i - pos), scale);
1238 if (t.cs() == "setsansfont") {
1240 h_font_sf_scale = scale;
1241 h_font_sans = p.getArg('{', '}');
1244 h_font_tt_scale = scale;
1245 h_font_typewriter = p.getArg('{', '}');
1249 else if (t.cs() == "date") {
1250 string argument = p.getArg('{', '}');
1251 if (argument.empty())
1252 h_suppress_date = "true";
1254 h_preamble << t.asInput() << '{' << argument << '}';
1257 else if (t.cs() == "color") {
1258 string const space =
1259 (p.hasOpt() ? p.getOpt() : string());
1260 string argument = p.getArg('{', '}');
1261 // check the case that a standard color is used
1262 if (space.empty() && is_known(argument, known_basic_colors)) {
1263 h_fontcolor = rgbcolor2code(argument);
1264 preamble.registerAutomaticallyLoadedPackage("color");
1265 } else if (space.empty() && argument == "document_fontcolor")
1266 preamble.registerAutomaticallyLoadedPackage("color");
1267 // check the case that LyX's document_fontcolor is defined
1268 // but not used for \color
1270 h_preamble << t.asInput();
1272 h_preamble << space;
1273 h_preamble << '{' << argument << '}';
1274 // the color might already be set because \definecolor
1275 // is parsed before this
1280 else if (t.cs() == "pagecolor") {
1281 string argument = p.getArg('{', '}');
1282 // check the case that a standard color is used
1283 if (is_known(argument, known_basic_colors)) {
1284 h_backgroundcolor = rgbcolor2code(argument);
1285 } else if (argument == "page_backgroundcolor")
1286 preamble.registerAutomaticallyLoadedPackage("color");
1287 // check the case that LyX's page_backgroundcolor is defined
1288 // but not used for \pagecolor
1290 h_preamble << t.asInput() << '{' << argument << '}';
1291 // the color might already be set because \definecolor
1292 // is parsed before this
1293 h_backgroundcolor = "";
1297 else if (t.cs() == "makeatletter") {
1298 // LyX takes care of this
1299 p.setCatCode('@', catLetter);
1302 else if (t.cs() == "makeatother") {
1303 // LyX takes care of this
1304 p.setCatCode('@', catOther);
1307 else if (t.cs() == "newcommand" || t.cs() == "newcommandx"
1308 || t.cs() == "renewcommand" || t.cs() == "renewcommandx"
1309 || t.cs() == "providecommand" || t.cs() == "providecommandx"
1310 || t.cs() == "DeclareRobustCommand"
1311 || t.cs() == "DeclareRobustCommandx"
1312 || t.cs() == "ProvideTextCommandDefault"
1313 || t.cs() == "DeclareMathAccent") {
1315 if (p.next_token().character() == '*') {
1319 string const name = p.verbatim_item();
1320 string const opt1 = p.getFullOpt();
1321 string const opt2 = p.getFullOpt();
1322 string const body = p.verbatim_item();
1324 if (name == "\\rmdefault")
1325 if (is_known(body, known_roman_fonts))
1326 h_font_roman = body;
1327 if (name == "\\sfdefault")
1328 if (is_known(body, known_sans_fonts))
1330 if (name == "\\ttdefault")
1331 if (is_known(body, known_typewriter_fonts))
1332 h_font_typewriter = body;
1333 if (name == "\\familydefault") {
1334 string family = body;
1335 // remove leading "\"
1336 h_font_default_family = family.erase(0,1);
1339 // remove the lyxdot definition that is re-added by LyX
1341 if (name == "\\lyxdot")
1342 in_lyx_preamble = true;
1344 // Add the command to the known commands
1345 add_known_command(name, opt1, !opt2.empty(), from_utf8(body));
1347 // only non-lyxspecific stuff
1348 if (!in_lyx_preamble) {
1350 ss << '\\' << t.cs();
1353 ss << '{' << name << '}' << opt1 << opt2
1354 << '{' << body << "}";
1355 h_preamble << ss.str();
1357 ostream & out = in_preamble ? h_preamble : os;
1358 out << "\\" << t.cs() << "{" << name << "}"
1359 << opts << "{" << body << "}";
1364 else if (t.cs() == "documentclass") {
1365 vector<string>::iterator it;
1366 vector<string> opts = split_options(p.getArg('[', ']'));
1367 handle_opt(opts, known_fontsizes, h_paperfontsize);
1368 delete_opt(opts, known_fontsizes);
1369 // delete "pt" at the end
1370 string::size_type i = h_paperfontsize.find("pt");
1371 if (i != string::npos)
1372 h_paperfontsize.erase(i);
1373 // The documentclass options are always parsed before the options
1374 // of the babel call so that a language cannot overwrite the babel
1376 handle_opt(opts, known_languages, h_language);
1377 delete_opt(opts, known_languages);
1379 // paper orientation
1380 if ((it = find(opts.begin(), opts.end(), "landscape")) != opts.end()) {
1381 h_paperorientation = "landscape";
1385 if ((it = find(opts.begin(), opts.end(), "oneside"))
1390 if ((it = find(opts.begin(), opts.end(), "twoside"))
1396 if ((it = find(opts.begin(), opts.end(), "onecolumn"))
1398 h_papercolumns = "1";
1401 if ((it = find(opts.begin(), opts.end(), "twocolumn"))
1403 h_papercolumns = "2";
1407 // some size options are known to any document classes, other sizes
1408 // are handled by the \geometry command of the geometry package
1409 handle_opt(opts, known_class_paper_sizes, h_papersize);
1410 delete_opt(opts, known_class_paper_sizes);
1411 // the remaining options
1412 h_options = join(opts, ",");
1413 // FIXME This does not work for classes that have a
1414 // different name in LyX than in LaTeX
1415 h_textclass = p.getArg('{', '}');
1418 else if (t.cs() == "usepackage") {
1419 string const options = p.getArg('[', ']');
1420 string const name = p.getArg('{', '}');
1421 vector<string> vecnames;
1422 split(name, vecnames, ',');
1423 vector<string>::const_iterator it = vecnames.begin();
1424 vector<string>::const_iterator end = vecnames.end();
1425 for (; it != end; ++it)
1426 handle_package(p, trimSpaceAndEol(*it), options,
1430 else if (t.cs() == "inputencoding") {
1431 string const encoding = p.getArg('{','}');
1432 h_inputencoding = encoding;
1433 p.setEncoding(encoding, Encoding::inputenc);
1436 else if (t.cs() == "newenvironment") {
1437 string const name = p.getArg('{', '}');
1438 string const opt1 = p.getFullOpt();
1439 string const opt2 = p.getFullOpt();
1440 string const beg = p.verbatim_item();
1441 string const end = p.verbatim_item();
1442 if (!in_lyx_preamble) {
1443 h_preamble << "\\newenvironment{" << name
1444 << '}' << opt1 << opt2 << '{'
1445 << beg << "}{" << end << '}';
1447 add_known_environment(name, opt1, !opt2.empty(),
1448 from_utf8(beg), from_utf8(end));
1452 else if (t.cs() == "newtheorem") {
1453 string const name = p.getArg('{', '}');
1454 string const opt1 = p.getFullOpt();
1455 string const opt2 = p.getFullOpt();
1456 string const body = p.verbatim_item();
1457 string const opt3 = p.getFullOpt();
1459 add_known_theorem(name, opt1, !opt2.empty(),
1460 from_utf8("\\newtheorem{" + name + '}' +
1461 opt1 + opt2 + '{' + body + '}' + opt3));
1463 if (!in_lyx_preamble)
1464 h_preamble << "\\newtheorem{" << name << '}'
1465 << opt1 << opt2 << '{' << '}' << opt3;
1468 else if (t.cs() == "def") {
1469 string name = p.get_token().cs();
1470 // In fact, name may be more than the name:
1471 // In the test case of bug 8116
1472 // name == "csname SF@gobble@opt \endcsname".
1473 // Therefore, we need to use asInput() instead of cs().
1474 while (p.next_token().cat() != catBegin)
1475 name += p.get_token().asInput();
1476 if (!in_lyx_preamble)
1477 h_preamble << "\\def\\" << name << '{'
1478 << p.verbatim_item() << "}";
1481 else if (t.cs() == "newcolumntype") {
1482 string const name = p.getArg('{', '}');
1483 trimSpaceAndEol(name);
1485 string opts = p.getOpt();
1486 if (!opts.empty()) {
1487 istringstream is(string(opts, 1));
1490 special_columns[name[0]] = nargs;
1491 h_preamble << "\\newcolumntype{" << name << "}";
1493 h_preamble << "[" << nargs << "]";
1494 h_preamble << "{" << p.verbatim_item() << "}";
1497 else if (t.cs() == "setcounter") {
1498 string const name = p.getArg('{', '}');
1499 string const content = p.getArg('{', '}');
1500 if (name == "secnumdepth")
1501 h_secnumdepth = content;
1502 else if (name == "tocdepth")
1503 h_tocdepth = content;
1505 h_preamble << "\\setcounter{" << name << "}{" << content << "}";
1508 else if (t.cs() == "setlength") {
1509 string const name = p.verbatim_item();
1510 string const content = p.verbatim_item();
1511 // the paragraphs are only not indented when \parindent is set to zero
1512 if (name == "\\parindent" && content != "") {
1513 if (content[0] == '0')
1514 h_paragraph_separation = "skip";
1516 h_paragraph_indentation = translate_len(content);
1517 } else if (name == "\\parskip") {
1518 if (content == "\\smallskipamount")
1519 h_defskip = "smallskip";
1520 else if (content == "\\medskipamount")
1521 h_defskip = "medskip";
1522 else if (content == "\\bigskipamount")
1523 h_defskip = "bigskip";
1525 h_defskip = content;
1527 h_preamble << "\\setlength{" << name << "}{" << content << "}";
1530 else if (t.cs() == "onehalfspacing")
1531 h_spacing = "onehalf";
1533 else if (t.cs() == "doublespacing")
1534 h_spacing = "double";
1536 else if (t.cs() == "setstretch")
1537 h_spacing = "other " + p.verbatim_item();
1539 else if (t.cs() == "synctex") {
1540 // the scheme is \synctex=value
1541 // where value can only be "1" or "-1"
1542 h_output_sync = "1";
1543 // there can be any character behind the value (e.g. a linebreak or a '\'
1544 // therefore we extract it char by char
1546 string value = p.get_token().asInput();
1548 value += p.get_token().asInput();
1549 h_output_sync_macro = "\\synctex=" + value;
1552 else if (t.cs() == "begin") {
1553 string const name = p.getArg('{', '}');
1554 if (name == "document")
1556 h_preamble << "\\begin{" << name << "}";
1559 else if (t.cs() == "geometry") {
1560 vector<string> opts = split_options(p.getArg('{', '}'));
1561 handle_geometry(opts);
1564 else if (t.cs() == "definecolor") {
1565 string const color = p.getArg('{', '}');
1566 string const space = p.getArg('{', '}');
1567 string const value = p.getArg('{', '}');
1568 if (color == "document_fontcolor" && space == "rgb") {
1569 RGBColor c(RGBColorFromLaTeX(value));
1570 h_fontcolor = X11hexname(c);
1571 } else if (color == "note_fontcolor" && space == "rgb") {
1572 RGBColor c(RGBColorFromLaTeX(value));
1573 h_notefontcolor = X11hexname(c);
1574 } else if (color == "page_backgroundcolor" && space == "rgb") {
1575 RGBColor c(RGBColorFromLaTeX(value));
1576 h_backgroundcolor = X11hexname(c);
1577 } else if (color == "shadecolor" && space == "rgb") {
1578 RGBColor c(RGBColorFromLaTeX(value));
1579 h_boxbgcolor = X11hexname(c);
1581 h_preamble << "\\definecolor{" << color
1582 << "}{" << space << "}{" << value
1587 else if (t.cs() == "bibliographystyle")
1588 h_biblio_style = p.verbatim_item();
1590 else if (t.cs() == "jurabibsetup") {
1591 // FIXME p.getArg('{', '}') is most probably wrong (it
1592 // does not handle nested braces).
1593 // Use p.verbatim_item() instead.
1594 vector<string> jurabibsetup =
1595 split_options(p.getArg('{', '}'));
1596 // add jurabibsetup to the jurabib package options
1597 add_package("jurabib", jurabibsetup);
1598 if (!jurabibsetup.empty()) {
1599 h_preamble << "\\jurabibsetup{"
1600 << join(jurabibsetup, ",") << '}';
1604 else if (t.cs() == "hypersetup") {
1605 vector<string> hypersetup =
1606 split_options(p.verbatim_item());
1607 // add hypersetup to the hyperref package options
1608 handle_hyperref(hypersetup);
1609 if (!hypersetup.empty()) {
1610 h_preamble << "\\hypersetup{"
1611 << join(hypersetup, ",") << '}';
1615 else if (is_known(t.cs(), known_if_3arg_commands)) {
1616 // prevent misparsing of \usepackage if it is used
1617 // as an argument (see e.g. our own output of
1618 // \@ifundefined above)
1619 string const arg1 = p.verbatim_item();
1620 string const arg2 = p.verbatim_item();
1621 string const arg3 = p.verbatim_item();
1622 // test case \@ifundefined{date}{}{\date{}}
1623 if (t.cs() == "@ifundefined" && arg1 == "date" &&
1624 arg2.empty() && arg3 == "\\date{}") {
1625 h_suppress_date = "true";
1626 // older tex2lyx versions did output
1627 // \@ifundefined{definecolor}{\usepackage{color}}{}
1628 } else if (t.cs() == "@ifundefined" &&
1629 arg1 == "definecolor" &&
1630 arg2 == "\\usepackage{color}" &&
1632 if (!in_lyx_preamble)
1633 h_preamble << package_beg_sep
1636 << "\\@ifundefined{definecolor}{color}{}"
1639 //\@ifundefined{showcaptionsetup}{}{%
1640 // \PassOptionsToPackage{caption=false}{subfig}}
1641 // that LyX uses for subfloats
1642 } else if (t.cs() == "@ifundefined" &&
1643 arg1 == "showcaptionsetup" && arg2.empty()
1644 && arg3 == "%\n \\PassOptionsToPackage{caption=false}{subfig}") {
1646 } else if (!in_lyx_preamble) {
1647 h_preamble << t.asInput()
1648 << '{' << arg1 << '}'
1649 << '{' << arg2 << '}'
1650 << '{' << arg3 << '}';
1654 else if (is_known(t.cs(), known_if_commands)) {
1655 // must not parse anything in conditional code, since
1656 // LyX would output the parsed contents unconditionally
1657 if (!in_lyx_preamble)
1658 h_preamble << t.asInput();
1659 handle_if(p, in_lyx_preamble);
1662 else if (!t.cs().empty() && !in_lyx_preamble)
1663 h_preamble << '\\' << t.cs();
1666 // remove the whitespace
1669 // Force textclass if the user wanted it
1670 if (!forceclass.empty())
1671 h_textclass = forceclass;
1672 tc.setName(h_textclass);
1674 cerr << "Error: Could not read layout file for textclass \"" << h_textclass << "\"." << endl;
1677 if (h_papersides.empty()) {
1680 h_papersides = ss.str();
1683 // If the CJK package is used we cannot set the document language from
1684 // the babel options. Instead, we guess which language is used most
1685 // and set this one.
1686 default_language = h_language;
1687 if (is_full_document &&
1688 (auto_packages.find("CJK") != auto_packages.end() ||
1689 auto_packages.find("CJKutf8") != auto_packages.end())) {
1691 h_language = guessLanguage(p, default_language);
1697 string babel2lyx(string const & language)
1699 char const * const * where = is_known(language, known_languages);
1701 return known_coded_languages[where - known_languages];
1706 string Preamble::polyglossia2lyx(string const & language)
1708 char const * const * where = is_known(language, polyglossia_languages);
1710 return coded_polyglossia_languages[where - polyglossia_languages];
1715 string rgbcolor2code(string const & name)
1717 char const * const * where = is_known(name, known_basic_colors);
1719 // "red", "green" etc
1720 return known_basic_color_codes[where - known_basic_colors];
1722 // "255,0,0", "0,255,0" etc
1723 RGBColor c(RGBColorFromLaTeX(name));
1724 return X11hexname(c);