3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
19 #include "LayoutFile.h"
22 #include "TextClass.h"
24 #include "support/convert.h"
25 #include "support/FileName.h"
26 #include "support/filetools.h"
27 #include "support/lstrings.h"
29 #include "support/regex.h"
35 using namespace lyx::support;
40 // special columntypes
41 extern map<char, int> special_columns;
47 //add this to known_languages when updating to lyxformat 266:
48 // "armenian" (needs special handling since not supported by standard babel)
49 //add these to known_languages when updating to lyxformat 268:
50 //"chinese-simplified", "chinese-traditional", "japanese", "korean"
51 // Both changes require first that support for non-babel languages (CJK,
54 * known babel language names (including synonyms)
55 * not in standard babel: arabic, arabtex, armenian, belarusian, serbian-latin, thai
56 * not yet supported by LyX: kurmanji
57 * please keep this in sync with known_coded_languages line by line!
59 const char * const known_languages[] = {"acadian", "afrikaans", "albanian",
60 "american", "arabic", "arabtex", "austrian", "bahasa", "bahasai", "bahasam",
61 "basque", "belarusian", "brazil", "brazilian", "breton", "british", "bulgarian",
62 "canadian", "canadien", "catalan", "croatian", "czech", "danish", "dutch",
63 "english", "esperanto", "estonian", "farsi", "finnish", "francais", "french",
64 "frenchb", "frenchle", "frenchpro", "galician", "german", "germanb", "greek",
65 "hebrew", "hungarian", "icelandic", "indon", "indonesian", "interlingua",
66 "irish", "italian", "kazakh", "latin", "latvian", "lithuanian", "lowersorbian",
67 "lsorbian", "magyar", "malay", "meyalu", "mongolian", "naustrian", "newzealand",
68 "ngerman", "ngermanb", "norsk", "nynorsk", "polutonikogreek", "polish",
69 "portuges", "portuguese", "romanian", "russian", "russianb", "samin",
70 "scottish", "serbian", "serbian-latin", "slovak", "slovene", "spanish",
71 "swedish", "thai", "turkish", "turkmen", "ukraineb", "ukrainian",
72 "uppersorbian", "UKenglish", "USenglish", "usorbian", "vietnam", "welsh",
76 * the same as known_languages with .lyx names
77 * please keep this in sync with known_languages line by line!
79 const char * const known_coded_languages[] = {"french", "afrikaans", "albanian",
80 "american", "arabic_arabi", "arabic_arabtex", "austrian", "bahasa", "bahasa", "bahasam",
81 "basque", "belarusian", "brazilian", "brazilian", "breton", "british", "bulgarian",
82 "canadian", "canadien", "catalan", "croatian", "czech", "danish", "dutch",
83 "english", "esperanto", "estonian", "farsi", "finnish", "french", "french",
84 "french", "french", "french", "galician", "german", "german", "greek",
85 "hebrew", "magyar", "icelandic", "bahasa", "bahasa", "interlingua",
86 "irish", "italian", "kazakh", "latin", "latvian", "lithuanian", "lowersorbian",
87 "lowersorbian", "magyar", "bahasam", "bahasam", "mongolian", "naustrian", "english",
88 "ngerman", "ngerman", "norsk", "nynorsk", "polutonikogreek", "polish",
89 "portuguese", "portuguese", "romanian", "russian", "russian", "samin",
90 "scottish", "serbian", "serbian-latin", "slovak", "slovene", "spanish",
91 "swedish", "thai", "turkish", "turkmen", "ukrainian", "ukrainian",
92 "uppersorbian", "uppersorbian", "english", "english", "vietnamese", "welsh",
95 /// languages with english quotes (.lyx names)
96 const char * const known_english_quotes_languages[] = {"american", "bahasa",
97 "bahasam", "brazilian", "canadian", "chinese-simplified", "english",
98 "esperanto", "hebrew", "irish", "korean", "portuguese", "scottish", "thai", 0};
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 const char * const known_xetex_packages[] = {"arabxetex", "fixlatvian",
170 "fontbook", "fontwrap", "mathspec", "philokalia", "polyglossia", "unisugar",
171 "xeCJK", "xecolor", "xecyr", "xeindex", "xepersian", "xunicode", 0};
173 // codes used to remove packages that are loaded automatically by LyX.
174 // Syntax: package_beg_sep<name>package_mid_sep<package loading code>package_end_sep
175 const char package_beg_sep = '\001';
176 const char package_mid_sep = '\002';
177 const char package_end_sep = '\003';
180 // returns true if at least one of the options in what has been found
181 bool handle_opt(vector<string> & opts, char const * const * what, string & target)
187 // the last language option is the document language (for babel and LyX)
188 // the last size option is the document font size
189 vector<string>::iterator it;
190 vector<string>::iterator position = opts.begin();
191 for (; *what; ++what) {
192 it = find(opts.begin(), opts.end(), *what);
193 if (it != opts.end()) {
194 if (it >= position) {
205 void delete_opt(vector<string> & opts, char const * const * what)
210 // remove found options from the list
211 // do this after handle_opt to avoid potential memory leaks
212 vector<string>::iterator it;
213 for (; *what; ++what) {
214 it = find(opts.begin(), opts.end(), *what);
215 if (it != opts.end())
222 * Split a package options string (keyval format) into a vector.
224 * authorformat=smallcaps,
226 * titleformat=colonsep,
227 * bibformat={tabular,ibidem,numbered}
229 vector<string> split_options(string const & input)
231 vector<string> options;
235 Token const & t = p.get_token();
236 if (t.asInput() == ",") {
237 options.push_back(trimSpaceAndEol(option));
239 } else if (t.asInput() == "=") {
242 if (p.next_token().asInput() == "{")
243 option += '{' + p.getArg('{', '}') + '}';
244 } else if (t.cat() != catSpace)
245 option += t.asInput();
249 options.push_back(trimSpaceAndEol(option));
256 * Retrieve a keyval option "name={value with=sign}" named \p name from
257 * \p options and return the value.
258 * The found option is also removed from \p options.
260 string process_keyval_opt(vector<string> & options, string name)
262 for (size_t i = 0; i < options.size(); ++i) {
263 vector<string> option;
264 split(options[i], option, '=');
265 if (option.size() < 2)
267 if (option[0] == name) {
268 options.erase(options.begin() + i);
269 option.erase(option.begin());
270 return join(option, "=");
276 } // anonymous namespace
279 bool Preamble::indentParagraphs() const
281 return h_paragraph_separation == "indent";
285 bool Preamble::isPackageUsed(string const & package) const
287 return used_packages.find(package) != used_packages.end();
291 vector<string> Preamble::getPackageOptions(string const & package) const
293 map<string, vector<string> >::const_iterator it = used_packages.find(package);
294 if (it != used_packages.end())
296 return vector<string>();
300 void Preamble::registerAutomaticallyLoadedPackage(std::string const & package)
302 auto_packages.insert(package);
306 void Preamble::addModule(string const & module)
308 used_modules.push_back(module);
312 void Preamble::suppressDate(bool suppress)
315 h_suppress_date = "true";
317 h_suppress_date = "false";
321 void Preamble::registerAuthor(std::string const & name)
323 Author author(from_utf8(name), empty_docstring());
324 author.setUsed(true);
325 authors_.record(author);
326 h_tracking_changes = "true";
327 h_output_changes = "true";
331 Author const & Preamble::getAuthor(std::string const & name) const
333 Author author(from_utf8(name), empty_docstring());
334 for (AuthorList::Authors::const_iterator it = authors_.begin();
335 it != authors_.end(); it++)
338 static Author const dummy;
343 void Preamble::add_package(string const & name, vector<string> & options)
345 // every package inherits the global options
346 if (used_packages.find(name) == used_packages.end())
347 used_packages[name] = split_options(h_options);
349 vector<string> & v = used_packages[name];
350 v.insert(v.end(), options.begin(), options.end());
351 if (name == "jurabib") {
352 // Don't output the order argument (see the cite command
353 // handling code in text.cpp).
354 vector<string>::iterator end =
355 remove(options.begin(), options.end(), "natbiborder");
356 end = remove(options.begin(), end, "jurabiborder");
357 options.erase(end, options.end());
364 // Given is a string like "scaled=0.9", return 0.9 * 100
365 string const scale_as_percentage(string const & scale)
367 string::size_type pos = scale.find('=');
368 if (pos != string::npos) {
369 string value = scale.substr(pos + 1);
371 return convert<string>(100 * convert<double>(value));
373 // If the input string didn't match our expectations.
374 // return the default value "100"
379 string remove_braces(string const & value)
383 if (value[0] == '{' && value[value.length()-1] == '}')
384 return value.substr(1, value.length()-2);
388 } // anonymous namespace
391 Preamble::Preamble() : one_language(true)
395 h_cite_engine = "basic";
396 h_defskip = "medskip";
399 h_fontencoding = "default";
400 h_font_roman = "default";
401 h_font_sans = "default";
402 h_font_typewriter = "default";
403 h_font_default_family = "default";
405 h_font_osf = "false";
406 h_font_sf_scale = "100";
407 h_font_tt_scale = "100";
408 h_graphics = "default";
409 h_html_be_strict = "false";
410 h_html_css_as_file = "0";
411 h_html_math_output = "0";
412 h_inputencoding = "auto";
413 h_language = "english";
414 h_language_package = "none";
419 h_output_changes = "false";
420 h_papercolumns = "1";
421 h_paperfontsize = "default";
422 h_paperorientation = "portrait";
423 h_paperpagestyle = "default";
425 h_papersize = "default";
426 h_paragraph_indentation = "default";
427 h_paragraph_separation = "indent";
432 h_pdf_bookmarks = "1";
433 h_pdf_bookmarksnumbered = "0";
434 h_pdf_bookmarksopen = "0";
435 h_pdf_bookmarksopenlevel = "1";
436 h_pdf_breaklinks = "0";
437 h_pdf_pdfborder = "0";
438 h_pdf_colorlinks = "0";
439 h_pdf_backref = "section";
440 h_pdf_pdfusetitle = "1";
442 //h_pdf_quoted_options;
443 h_quotes_language = "english";
445 h_spacing = "single";
446 h_suppress_date = "false";
447 h_textclass = "article";
449 h_tracking_changes = "false";
450 h_use_bibtopic = "false";
451 h_use_geometry = "false";
453 h_use_default_options = "false";
455 h_use_hyperref = "0";
457 h_use_mathdots = "0";
458 h_use_refstyle = "0";
459 h_use_undertilde = "0";
463 void Preamble::handle_hyperref(vector<string> & options)
465 // FIXME swallow inputencoding changes that might surround the
466 // hyperref setup if it was written by LyX
467 h_use_hyperref = "1";
468 // swallow "unicode=true", since LyX does always write that
469 vector<string>::iterator it =
470 find(options.begin(), options.end(), "unicode=true");
471 if (it != options.end())
473 it = find(options.begin(), options.end(), "pdfusetitle");
474 if (it != options.end()) {
475 h_pdf_pdfusetitle = "1";
478 string bookmarks = process_keyval_opt(options, "bookmarks");
479 if (bookmarks == "true")
480 h_pdf_bookmarks = "1";
481 else if (bookmarks == "false")
482 h_pdf_bookmarks = "0";
483 if (h_pdf_bookmarks == "1") {
484 string bookmarksnumbered =
485 process_keyval_opt(options, "bookmarksnumbered");
486 if (bookmarksnumbered == "true")
487 h_pdf_bookmarksnumbered = "1";
488 else if (bookmarksnumbered == "false")
489 h_pdf_bookmarksnumbered = "0";
490 string bookmarksopen =
491 process_keyval_opt(options, "bookmarksopen");
492 if (bookmarksopen == "true")
493 h_pdf_bookmarksopen = "1";
494 else if (bookmarksopen == "false")
495 h_pdf_bookmarksopen = "0";
496 if (h_pdf_bookmarksopen == "1") {
497 string bookmarksopenlevel =
498 process_keyval_opt(options, "bookmarksopenlevel");
499 if (!bookmarksopenlevel.empty())
500 h_pdf_bookmarksopenlevel = bookmarksopenlevel;
503 string breaklinks = process_keyval_opt(options, "breaklinks");
504 if (breaklinks == "true")
505 h_pdf_breaklinks = "1";
506 else if (breaklinks == "false")
507 h_pdf_breaklinks = "0";
508 string pdfborder = process_keyval_opt(options, "pdfborder");
509 if (pdfborder == "{0 0 0}")
510 h_pdf_pdfborder = "1";
511 else if (pdfborder == "{0 0 1}")
512 h_pdf_pdfborder = "0";
513 string backref = process_keyval_opt(options, "backref");
514 if (!backref.empty())
515 h_pdf_backref = backref;
516 string colorlinks = process_keyval_opt(options, "colorlinks");
517 if (colorlinks == "true")
518 h_pdf_colorlinks = "1";
519 else if (colorlinks == "false")
520 h_pdf_colorlinks = "0";
521 string pdfpagemode = process_keyval_opt(options, "pdfpagemode");
522 if (!pdfpagemode.empty())
523 h_pdf_pagemode = pdfpagemode;
524 string pdftitle = process_keyval_opt(options, "pdftitle");
525 if (!pdftitle.empty()) {
526 h_pdf_title = remove_braces(pdftitle);
528 string pdfauthor = process_keyval_opt(options, "pdfauthor");
529 if (!pdfauthor.empty()) {
530 h_pdf_author = remove_braces(pdfauthor);
532 string pdfsubject = process_keyval_opt(options, "pdfsubject");
533 if (!pdfsubject.empty())
534 h_pdf_subject = remove_braces(pdfsubject);
535 string pdfkeywords = process_keyval_opt(options, "pdfkeywords");
536 if (!pdfkeywords.empty())
537 h_pdf_keywords = remove_braces(pdfkeywords);
538 if (!options.empty()) {
539 if (!h_pdf_quoted_options.empty())
540 h_pdf_quoted_options += ',';
541 h_pdf_quoted_options += join(options, ",");
547 void Preamble::handle_package(Parser &p, string const & name,
548 string const & opts, bool in_lyx_preamble)
550 vector<string> options = split_options(opts);
551 add_package(name, options);
554 if (is_known(name, known_xetex_packages))
558 if (is_known(name, known_roman_fonts)) {
563 if (name == "fourier") {
564 h_font_roman = "utopia";
565 // when font uses real small capitals
566 if (opts == "expert")
570 if (name == "mathpazo")
571 h_font_roman = "palatino";
573 if (name == "mathptmx")
574 h_font_roman = "times";
577 if (is_known(name, known_sans_fonts)) {
581 h_font_sf_scale = scale_as_percentage(scale);
586 if (is_known(name, known_typewriter_fonts)) {
587 // fourier can be set as roman font _only_
588 // fourier as typewriter is handled in handling of \ttdefault
589 if (name != "fourier") {
590 h_font_typewriter = name;
593 h_font_tt_scale = scale_as_percentage(scale);
598 // font uses old-style figure
602 // after the detection and handling of special cases, we can remove the
603 // fonts, otherwise they would appear in the preamble, see bug #7856
604 if (is_known(name, known_roman_fonts) || is_known(name, known_sans_fonts)
605 || is_known(name, known_typewriter_fonts))
608 else if (name == "amsmath" || name == "amssymb")
611 else if (name == "esint")
614 else if (name == "mhchem")
617 else if (name == "mathdots")
618 h_use_mathdots = "2";
620 else if (name == "undertilde")
621 h_use_undertilde = "2";
623 else if (name == "babel") {
624 h_language_package = "default";
625 // One might think we would have to do nothing if babel is loaded
626 // without any options to prevent pollution of the preamble with this
627 // babel call in every roundtrip.
628 // But the user could have defined babel-specific things afterwards. So
629 // we need to keep it in the preamble to prevent cases like bug #7861.
631 // check if more than one option was used - used later for inputenc
632 if (options.begin() != options.end() - 1)
633 one_language = false;
634 // babel takes the last language of the option of its \usepackage
635 // call as document language. If there is no such language option, the
636 // last language in the documentclass options is used.
637 handle_opt(options, known_languages, h_language);
638 // If babel is called with options, LyX puts them by default into the
639 // document class options. This works for most languages, except
640 // for Latvian, Lithuanian, Mongolian, Turkmen and Vietnamese and
641 // perhaps in future others.
642 // Therefore keep the babel call as it is as the user might have
644 h_preamble << "\\usepackage[" << opts << "]{babel}\n";
645 delete_opt(options, known_languages);
648 h_preamble << "\\usepackage{babel}\n";
651 else if (name == "fontenc") {
652 h_fontencoding = getStringFromVector(options, ",");
653 /* We could do the following for better round trip support,
654 * but this makes the document less portable, so I skip it:
655 if (h_fontencoding == lyxrc.fontenc)
656 h_fontencoding = "global";
661 else if (name == "inputenc" || name == "luainputenc") {
662 // h_inputencoding is only set when there is not more than one
663 // inputenc option because otherwise h_inputencoding must be
664 // set to "auto" (the default encoding of the document language)
665 // Therefore check for the "," character.
666 // It is also only set when there is not more than one babel
668 if (opts.find(",") == string::npos && one_language == true)
669 h_inputencoding = opts;
670 if (!options.empty())
671 p.setEncoding(options.back());
675 else if (is_known(name, known_old_language_packages)) {
676 // known language packages from the times before babel
677 // if they are found and not also babel, they will be used as
678 // cutom language package
679 h_language_package = "\\usepackage{" + name + "}";
682 else if (name == "makeidx")
685 else if (name == "prettyref")
688 else if (name == "varioref")
691 else if (name == "verbatim")
694 else if (name == "nomencl")
697 else if (name == "textcomp")
700 else if (name == "url")
703 else if (name == "color" || name == "subscript" || name == "ulem") {
704 if (!in_lyx_preamble)
705 h_preamble << package_beg_sep << name
706 << package_mid_sep << "\\usepackage{"
707 << name << '}' << package_end_sep;
710 else if (name == "graphicx")
713 else if (name == "setspace")
716 else if (name == "geometry")
717 ; // Ignore this, the geometry settings are made by the \geometry
718 // command. This command is handled below.
720 else if (name == "rotfloat")
723 else if (name == "wrapfig")
726 else if (name == "subfig")
729 else if (is_known(name, known_languages))
732 else if (name == "natbib") {
733 h_cite_engine = "natbib_authoryear";
734 vector<string>::iterator it =
735 find(options.begin(), options.end(), "authoryear");
736 if (it != options.end())
739 it = find(options.begin(), options.end(), "numbers");
740 if (it != options.end()) {
741 h_cite_engine = "natbib_numerical";
747 else if (name == "jurabib")
748 h_cite_engine = "jurabib";
750 else if (name == "hyperref")
751 handle_hyperref(options);
753 else if (!in_lyx_preamble) {
755 h_preamble << "\\usepackage{" << name << "}";
757 h_preamble << "\\usepackage[" << opts << "]{"
763 // We need to do something with the options...
764 if (!options.empty())
765 cerr << "Ignoring options '" << join(options, ",")
766 << "' of package " << name << '.' << endl;
768 // remove the whitespace
773 void Preamble::handle_if(Parser & p, bool in_lyx_preamble)
776 Token t = p.get_token();
777 if (t.cat() == catEscape &&
778 is_known(t.cs(), known_if_commands))
779 handle_if(p, in_lyx_preamble);
781 if (!in_lyx_preamble)
782 h_preamble << t.asInput();
783 if (t.cat() == catEscape && t.cs() == "fi")
790 bool Preamble::writeLyXHeader(ostream & os)
792 // translate from babel to LyX names
793 h_language = babel2lyx(h_language);
795 // set the quote language
796 // LyX only knows the following quotes languages:
797 // english, swedish, german, polish, french and danish
798 // (quotes for "japanese" and "chinese-traditional" are missing because
799 // they wouldn't be useful: http://www.lyx.org/trac/ticket/6383)
800 // conversion list taken from
801 // http://en.wikipedia.org/wiki/Quotation_mark,_non-English_usage
802 // (quotes for kazakh and interlingua are unknown)
804 if (h_language == "danish")
805 h_quotes_language = "danish";
807 else if (is_known(h_language, known_french_quotes_languages))
808 h_quotes_language = "french";
810 else if (is_known(h_language, known_german_quotes_languages))
811 h_quotes_language = "german";
813 else if (is_known(h_language, known_polish_quotes_languages))
814 h_quotes_language = "polish";
816 else if (is_known(h_language, known_swedish_quotes_languages))
817 h_quotes_language = "swedish";
819 else if (is_known(h_language, known_english_quotes_languages))
820 h_quotes_language = "english";
822 // output the LyX file settings
823 os << "#LyX file created by tex2lyx " << PACKAGE_VERSION << "\n"
824 << "\\lyxformat " << LYX_FORMAT << '\n'
825 << "\\begin_document\n"
826 << "\\begin_header\n"
827 << "\\textclass " << h_textclass << "\n";
828 string const raw = h_preamble.str();
830 os << "\\begin_preamble\n";
831 for (string::size_type i = 0; i < raw.size(); ++i) {
832 if (raw[i] == package_beg_sep) {
833 // Here follows some package loading code that
834 // must be skipped if the package is loaded
836 string::size_type j = raw.find(package_mid_sep, i);
837 if (j == string::npos)
839 string::size_type k = raw.find(package_end_sep, j);
840 if (k == string::npos)
842 string const package = raw.substr(i + 1, j - i - 1);
843 string const replacement = raw.substr(j + 1, k - j - 1);
844 if (auto_packages.find(package) == auto_packages.end())
850 os << "\n\\end_preamble\n";
852 if (!h_options.empty())
853 os << "\\options " << h_options << "\n";
854 os << "\\use_default_options " << h_use_default_options << "\n";
855 if (!used_modules.empty()) {
856 os << "\\begin_modules\n";
857 vector<string>::const_iterator const end = used_modules.end();
858 vector<string>::const_iterator it = used_modules.begin();
859 for (; it != end; it++)
861 os << "\\end_modules\n";
863 os << "\\language " << h_language << "\n"
864 << "\\language_package " << h_language_package << "\n"
865 << "\\inputencoding " << h_inputencoding << "\n"
866 << "\\fontencoding " << h_fontencoding << "\n"
867 << "\\font_roman " << h_font_roman << "\n"
868 << "\\font_sans " << h_font_sans << "\n"
869 << "\\font_typewriter " << h_font_typewriter << "\n"
870 << "\\font_default_family " << h_font_default_family << "\n"
871 << "\\font_sc " << h_font_sc << "\n"
872 << "\\font_osf " << h_font_osf << "\n"
873 << "\\font_sf_scale " << h_font_sf_scale << "\n"
874 << "\\font_tt_scale " << h_font_tt_scale << "\n"
875 << "\\graphics " << h_graphics << "\n";
876 if (!h_float_placement.empty())
877 os << "\\float_placement " << h_float_placement << "\n";
878 os << "\\paperfontsize " << h_paperfontsize << "\n"
879 << "\\spacing " << h_spacing << "\n"
880 << "\\use_hyperref " << h_use_hyperref << '\n';
881 if (h_use_hyperref == "1") {
882 if (!h_pdf_title.empty())
883 os << "\\pdf_title \"" << h_pdf_title << "\"\n";
884 if (!h_pdf_author.empty())
885 os << "\\pdf_author \"" << h_pdf_author << "\"\n";
886 if (!h_pdf_subject.empty())
887 os << "\\pdf_subject \"" << h_pdf_subject << "\"\n";
888 if (!h_pdf_keywords.empty())
889 os << "\\pdf_keywords \"" << h_pdf_keywords << "\"\n";
890 os << "\\pdf_bookmarks " << h_pdf_bookmarks << "\n"
891 "\\pdf_bookmarksnumbered " << h_pdf_bookmarksnumbered << "\n"
892 "\\pdf_bookmarksopen " << h_pdf_bookmarksopen << "\n"
893 "\\pdf_bookmarksopenlevel " << h_pdf_bookmarksopenlevel << "\n"
894 "\\pdf_breaklinks " << h_pdf_breaklinks << "\n"
895 "\\pdf_pdfborder " << h_pdf_pdfborder << "\n"
896 "\\pdf_colorlinks " << h_pdf_colorlinks << "\n"
897 "\\pdf_backref " << h_pdf_backref << "\n"
898 "\\pdf_pdfusetitle " << h_pdf_pdfusetitle << '\n';
899 if (!h_pdf_pagemode.empty())
900 os << "\\pdf_pagemode " << h_pdf_pagemode << '\n';
901 if (!h_pdf_quoted_options.empty())
902 os << "\\pdf_quoted_options \"" << h_pdf_quoted_options << "\"\n";
904 os << "\\papersize " << h_papersize << "\n"
905 << "\\use_geometry " << h_use_geometry << "\n"
906 << "\\use_amsmath " << h_use_amsmath << "\n"
907 << "\\use_esint " << h_use_esint << "\n"
908 << "\\use_mhchem " << h_use_mhchem << "\n"
909 << "\\use_mathdots " << h_use_mathdots << "\n"
910 << "\\use_undertilde " << h_use_undertilde << "\n"
911 << "\\cite_engine " << h_cite_engine << "\n"
912 << "\\use_bibtopic " << h_use_bibtopic << "\n"
913 << "\\paperorientation " << h_paperorientation << '\n'
914 << "\\suppress_date " << h_suppress_date << '\n'
915 << "\\use_refstyle " << h_use_refstyle << '\n';
916 if (!h_fontcolor.empty())
917 os << "\\fontcolor " << h_fontcolor << '\n';
918 if (!h_notefontcolor.empty())
919 os << "\\notefontcolor " << h_notefontcolor << '\n';
920 if (!h_backgroundcolor.empty())
921 os << "\\backgroundcolor " << h_backgroundcolor << '\n';
922 if (!h_boxbgcolor.empty())
923 os << "\\boxbgcolor " << h_boxbgcolor << '\n';
925 << "\\secnumdepth " << h_secnumdepth << "\n"
926 << "\\tocdepth " << h_tocdepth << "\n"
927 << "\\paragraph_separation " << h_paragraph_separation << "\n";
928 if (h_paragraph_separation == "skip")
929 os << "\\defskip " << h_defskip << "\n";
931 os << "\\paragraph_indentation " << h_paragraph_indentation << "\n";
932 os << "\\quotes_language " << h_quotes_language << "\n"
933 << "\\papercolumns " << h_papercolumns << "\n"
934 << "\\papersides " << h_papersides << "\n"
935 << "\\paperpagestyle " << h_paperpagestyle << "\n";
936 if (!h_listings_params.empty())
937 os << "\\listings_params " << h_listings_params << "\n";
938 os << "\\tracking_changes " << h_tracking_changes << "\n"
939 << "\\output_changes " << h_output_changes << "\n"
940 << "\\html_math_output " << h_html_math_output << "\n"
941 << "\\html_css_as_file " << h_html_css_as_file << "\n"
942 << "\\html_be_strict " << h_html_be_strict << "\n"
944 << "\\end_header\n\n"
946 // clear preamble for subdocuments
952 void Preamble::parse(Parser & p, string const & forceclass,
953 TeX2LyXDocClass & tc)
955 // initialize fixed types
956 special_columns['D'] = 3;
957 bool is_full_document = false;
958 bool is_lyx_file = false;
959 bool in_lyx_preamble = false;
961 // determine whether this is a full document or a fragment for inclusion
963 Token const & t = p.get_token();
965 if (t.cat() == catEscape && t.cs() == "documentclass") {
966 is_full_document = true;
972 while (is_full_document && p.good()) {
973 Token const & t = p.get_token();
976 cerr << "t: " << t << "\n";
982 if (!in_lyx_preamble &&
983 (t.cat() == catLetter ||
984 t.cat() == catSuper ||
986 t.cat() == catOther ||
987 t.cat() == catMath ||
988 t.cat() == catActive ||
989 t.cat() == catBegin ||
991 t.cat() == catAlign ||
992 t.cat() == catParameter))
993 h_preamble << t.cs();
995 else if (!in_lyx_preamble &&
996 (t.cat() == catSpace || t.cat() == catNewline))
997 h_preamble << t.asInput();
999 else if (t.cat() == catComment) {
1000 static regex const islyxfile("%% LyX .* created this file");
1001 static regex const usercommands("User specified LaTeX commands");
1003 string const comment = t.asInput();
1005 // magically switch encoding default if it looks like XeLaTeX
1006 static string const magicXeLaTeX =
1007 "% This document must be compiled with XeLaTeX ";
1008 if (comment.size() > magicXeLaTeX.size()
1009 && comment.substr(0, magicXeLaTeX.size()) == magicXeLaTeX
1010 && h_inputencoding == "auto") {
1011 cerr << "XeLaTeX comment found, switching to UTF8\n";
1012 h_inputencoding = "utf8";
1015 if (regex_search(comment, sub, islyxfile)) {
1017 in_lyx_preamble = true;
1018 } else if (is_lyx_file
1019 && regex_search(comment, sub, usercommands))
1020 in_lyx_preamble = false;
1021 else if (!in_lyx_preamble)
1022 h_preamble << t.asInput();
1025 else if (t.cs() == "pagestyle")
1026 h_paperpagestyle = p.verbatim_item();
1028 else if (t.cs() == "date") {
1029 string argument = p.getArg('{', '}');
1030 if (argument.empty())
1031 h_suppress_date = "true";
1033 h_preamble << t.asInput() << '{' << argument << '}';
1036 else if (t.cs() == "color") {
1037 string const space =
1038 (p.hasOpt() ? p.getOpt() : string());
1039 string argument = p.getArg('{', '}');
1040 // check the case that a standard color is used
1041 if (space.empty() && is_known(argument, known_basic_colors)) {
1042 h_fontcolor = rgbcolor2code(argument);
1043 preamble.registerAutomaticallyLoadedPackage("color");
1044 } else if (space.empty() && argument == "document_fontcolor")
1045 preamble.registerAutomaticallyLoadedPackage("color");
1046 // check the case that LyX's document_fontcolor is defined
1047 // but not used for \color
1049 h_preamble << t.asInput();
1051 h_preamble << space;
1052 h_preamble << '{' << argument << '}';
1053 // the color might already be set because \definecolor
1054 // is parsed before this
1059 else if (t.cs() == "pagecolor") {
1060 string argument = p.getArg('{', '}');
1061 // check the case that a standard color is used
1062 if (is_known(argument, known_basic_colors)) {
1063 h_backgroundcolor = rgbcolor2code(argument);
1064 } else if (argument == "page_backgroundcolor")
1065 preamble.registerAutomaticallyLoadedPackage("color");
1066 // check the case that LyX's page_backgroundcolor is defined
1067 // but not used for \pagecolor
1069 h_preamble << t.asInput() << '{' << argument << '}';
1070 // the color might already be set because \definecolor
1071 // is parsed before this
1072 h_backgroundcolor = "";
1076 else if (t.cs() == "makeatletter") {
1077 // LyX takes care of this
1078 p.setCatCode('@', catLetter);
1081 else if (t.cs() == "makeatother") {
1082 // LyX takes care of this
1083 p.setCatCode('@', catOther);
1086 else if (t.cs() == "newcommand" || t.cs() == "newcommandx"
1087 || t.cs() == "renewcommand" || t.cs() == "renewcommandx"
1088 || t.cs() == "providecommand" || t.cs() == "providecommandx"
1089 || t.cs() == "DeclareRobustCommand"
1090 || t.cs() == "DeclareRobustCommandx"
1091 || t.cs() == "ProvideTextCommandDefault"
1092 || t.cs() == "DeclareMathAccent") {
1094 if (p.next_token().character() == '*') {
1098 string const name = p.verbatim_item();
1099 string const opt1 = p.getFullOpt();
1100 string const opt2 = p.getFullOpt();
1101 string const body = p.verbatim_item();
1103 if (name == "\\rmdefault")
1104 if (is_known(body, known_roman_fonts))
1105 h_font_roman = body;
1106 if (name == "\\sfdefault")
1107 if (is_known(body, known_sans_fonts))
1109 if (name == "\\ttdefault")
1110 if (is_known(body, known_typewriter_fonts))
1111 h_font_typewriter = body;
1112 if (name == "\\familydefault") {
1113 string family = body;
1114 // remove leading "\"
1115 h_font_default_family = family.erase(0,1);
1118 // Add the command to the known commands
1119 add_known_command(name, opt1, !opt2.empty(), from_utf8(body));
1121 // only non-lyxspecific stuff
1122 if (!in_lyx_preamble) {
1124 ss << '\\' << t.cs();
1127 ss << '{' << name << '}' << opt1 << opt2
1128 << '{' << body << "}";
1129 h_preamble << ss.str();
1131 ostream & out = in_preamble ? h_preamble : os;
1132 out << "\\" << t.cs() << "{" << name << "}"
1133 << opts << "{" << body << "}";
1138 else if (t.cs() == "documentclass") {
1139 vector<string>::iterator it;
1140 vector<string> opts = split_options(p.getArg('[', ']'));
1141 handle_opt(opts, known_fontsizes, h_paperfontsize);
1142 delete_opt(opts, known_fontsizes);
1143 // delete "pt" at the end
1144 string::size_type i = h_paperfontsize.find("pt");
1145 if (i != string::npos)
1146 h_paperfontsize.erase(i);
1147 // The documentclass options are always parsed before the options
1148 // of the babel call so that a language cannot overwrite the babel
1150 handle_opt(opts, known_languages, h_language);
1151 delete_opt(opts, known_languages);
1153 // paper orientation
1154 if ((it = find(opts.begin(), opts.end(), "landscape")) != opts.end()) {
1155 h_paperorientation = "landscape";
1159 if ((it = find(opts.begin(), opts.end(), "oneside"))
1164 if ((it = find(opts.begin(), opts.end(), "twoside"))
1170 if ((it = find(opts.begin(), opts.end(), "onecolumn"))
1172 h_papercolumns = "1";
1175 if ((it = find(opts.begin(), opts.end(), "twocolumn"))
1177 h_papercolumns = "2";
1181 // some size options are know to any document classes, other sizes
1182 // are handled by the \geometry command of the geometry package
1183 handle_opt(opts, known_class_paper_sizes, h_papersize);
1184 delete_opt(opts, known_class_paper_sizes);
1185 // the remaining options
1186 h_options = join(opts, ",");
1187 // FIXME This does not work for classes that have a
1188 // different name in LyX than in LaTeX
1189 h_textclass = p.getArg('{', '}');
1192 else if (t.cs() == "usepackage") {
1193 string const options = p.getArg('[', ']');
1194 string const name = p.getArg('{', '}');
1195 vector<string> vecnames;
1196 split(name, vecnames, ',');
1197 vector<string>::const_iterator it = vecnames.begin();
1198 vector<string>::const_iterator end = vecnames.end();
1199 for (; it != end; ++it)
1200 handle_package(p, trimSpaceAndEol(*it), options,
1204 else if (t.cs() == "inputencoding") {
1205 string const encoding = p.getArg('{','}');
1206 h_inputencoding = encoding;
1207 p.setEncoding(encoding);
1210 else if (t.cs() == "newenvironment") {
1211 string const name = p.getArg('{', '}');
1212 string const opt1 = p.getFullOpt();
1213 string const opt2 = p.getFullOpt();
1214 string const beg = p.verbatim_item();
1215 string const end = p.verbatim_item();
1216 if (!in_lyx_preamble) {
1217 h_preamble << "\\newenvironment{" << name
1218 << '}' << opt1 << opt2 << '{'
1219 << beg << "}{" << end << '}';
1221 add_known_environment(name, opt1, !opt2.empty(),
1222 from_utf8(beg), from_utf8(end));
1226 else if (t.cs() == "def") {
1227 string name = p.get_token().cs();
1228 while (p.next_token().cat() != catBegin)
1229 name += p.get_token().cs();
1230 if (!in_lyx_preamble)
1231 h_preamble << "\\def\\" << name << '{'
1232 << p.verbatim_item() << "}";
1235 else if (t.cs() == "newcolumntype") {
1236 string const name = p.getArg('{', '}');
1237 trimSpaceAndEol(name);
1239 string opts = p.getOpt();
1240 if (!opts.empty()) {
1241 istringstream is(string(opts, 1));
1244 special_columns[name[0]] = nargs;
1245 h_preamble << "\\newcolumntype{" << name << "}";
1247 h_preamble << "[" << nargs << "]";
1248 h_preamble << "{" << p.verbatim_item() << "}";
1251 else if (t.cs() == "setcounter") {
1252 string const name = p.getArg('{', '}');
1253 string const content = p.getArg('{', '}');
1254 if (name == "secnumdepth")
1255 h_secnumdepth = content;
1256 else if (name == "tocdepth")
1257 h_tocdepth = content;
1259 h_preamble << "\\setcounter{" << name << "}{" << content << "}";
1262 else if (t.cs() == "setlength") {
1263 string const name = p.verbatim_item();
1264 string const content = p.verbatim_item();
1265 // the paragraphs are only not indented when \parindent is set to zero
1266 if (name == "\\parindent" && content != "") {
1267 if (content[0] == '0')
1268 h_paragraph_separation = "skip";
1270 h_paragraph_indentation = translate_len(content);
1271 } else if (name == "\\parskip") {
1272 if (content == "\\smallskipamount")
1273 h_defskip = "smallskip";
1274 else if (content == "\\medskipamount")
1275 h_defskip = "medskip";
1276 else if (content == "\\bigskipamount")
1277 h_defskip = "bigskip";
1279 h_defskip = content;
1281 h_preamble << "\\setlength{" << name << "}{" << content << "}";
1284 else if (t.cs() == "onehalfspacing")
1285 h_spacing = "onehalf";
1287 else if (t.cs() == "doublespacing")
1288 h_spacing = "double";
1290 else if (t.cs() == "setstretch")
1291 h_spacing = "other " + p.verbatim_item();
1293 else if (t.cs() == "begin") {
1294 string const name = p.getArg('{', '}');
1295 if (name == "document")
1297 h_preamble << "\\begin{" << name << "}";
1300 else if (t.cs() == "geometry") {
1301 h_use_geometry = "true";
1302 vector<string> opts = split_options(p.getArg('{', '}'));
1303 vector<string>::iterator it;
1304 // paper orientation
1305 if ((it = find(opts.begin(), opts.end(), "landscape")) != opts.end()) {
1306 h_paperorientation = "landscape";
1310 handle_opt(opts, known_paper_sizes, h_papersize);
1311 delete_opt(opts, known_paper_sizes);
1313 char const * const * margin = known_paper_margins;
1315 for (; *margin; ++margin) {
1317 // search for the "=" in e.g. "lmargin=2cm" to get the value
1318 for(size_t i = 0; i != opts.size(); i++) {
1319 if (opts.at(i).find(*margin) != string::npos) {
1320 string::size_type pos = opts.at(i).find("=");
1321 string value = opts.at(i).substr(pos + 1);
1322 string name = known_coded_paper_margins[k];
1323 h_margins += "\\" + name + " " + value + "\n";
1329 else if (t.cs() == "definecolor") {
1330 string const color = p.getArg('{', '}');
1331 string const space = p.getArg('{', '}');
1332 string const value = p.getArg('{', '}');
1333 if (color == "document_fontcolor" && space == "rgb") {
1334 RGBColor c(RGBColorFromLaTeX(value));
1335 h_fontcolor = X11hexname(c);
1336 } else if (color == "note_fontcolor" && space == "rgb") {
1337 RGBColor c(RGBColorFromLaTeX(value));
1338 h_notefontcolor = X11hexname(c);
1339 } else if (color == "page_backgroundcolor" && space == "rgb") {
1340 RGBColor c(RGBColorFromLaTeX(value));
1341 h_backgroundcolor = X11hexname(c);
1342 } else if (color == "shadecolor" && space == "rgb") {
1343 RGBColor c(RGBColorFromLaTeX(value));
1344 h_boxbgcolor = X11hexname(c);
1346 h_preamble << "\\definecolor{" << color
1347 << "}{" << space << "}{" << value
1352 else if (t.cs() == "jurabibsetup") {
1353 // FIXME p.getArg('{', '}') is most probably wrong (it
1354 // does not handle nested braces).
1355 // Use p.verbatim_item() instead.
1356 vector<string> jurabibsetup =
1357 split_options(p.getArg('{', '}'));
1358 // add jurabibsetup to the jurabib package options
1359 add_package("jurabib", jurabibsetup);
1360 if (!jurabibsetup.empty()) {
1361 h_preamble << "\\jurabibsetup{"
1362 << join(jurabibsetup, ",") << '}';
1366 else if (t.cs() == "hypersetup") {
1367 vector<string> hypersetup =
1368 split_options(p.verbatim_item());
1369 // add hypersetup to the hyperref package options
1370 handle_hyperref(hypersetup);
1371 if (!hypersetup.empty()) {
1372 h_preamble << "\\hypersetup{"
1373 << join(hypersetup, ",") << '}';
1377 else if (is_known(t.cs(), known_if_3arg_commands)) {
1378 // prevent misparsing of \usepackage if it is used
1379 // as an argument (see e.g. our own output of
1380 // \@ifundefined above)
1381 string const arg1 = p.verbatim_item();
1382 string const arg2 = p.verbatim_item();
1383 string const arg3 = p.verbatim_item();
1384 // test case \@ifundefined{date}{}{\date{}}
1385 if (t.cs() == "@ifundefined" && arg1 == "date" &&
1386 arg2.empty() && arg3 == "\\date{}") {
1387 h_suppress_date = "true";
1388 // older tex2lyx versions did output
1389 // \@ifundefined{definecolor}{\usepackage{color}}{}
1390 } else if (t.cs() == "@ifundefined" &&
1391 arg1 == "definecolor" &&
1392 arg2 == "\\usepackage{color}" &&
1394 if (!in_lyx_preamble)
1395 h_preamble << package_beg_sep
1398 << "\\@ifundefined{definecolor}{color}{}"
1401 //\@ifundefined{showcaptionsetup}{}{%
1402 // \PassOptionsToPackage{caption=false}{subfig}}
1403 // that LyX uses for subfloats
1404 } else if (t.cs() == "@ifundefined" &&
1405 arg1 == "showcaptionsetup" && arg2.empty()
1406 && arg3 == "%\n \\PassOptionsToPackage{caption=false}{subfig}") {
1408 } else if (!in_lyx_preamble) {
1409 h_preamble << t.asInput()
1410 << '{' << arg1 << '}'
1411 << '{' << arg2 << '}'
1412 << '{' << arg3 << '}';
1416 else if (is_known(t.cs(), known_if_commands)) {
1417 // must not parse anything in conditional code, since
1418 // LyX would output the parsed contents unconditionally
1419 if (!in_lyx_preamble)
1420 h_preamble << t.asInput();
1421 handle_if(p, in_lyx_preamble);
1424 else if (!t.cs().empty() && !in_lyx_preamble)
1425 h_preamble << '\\' << t.cs();
1428 // remove the whitespace
1431 // Force textclass if the user wanted it
1432 if (!forceclass.empty())
1433 h_textclass = forceclass;
1434 if (noweb_mode && !prefixIs(h_textclass, "literate-"))
1435 h_textclass.insert(0, "literate-");
1436 tc.setName(h_textclass);
1438 cerr << "Error: Could not read layout file for textclass \"" << h_textclass << "\"." << endl;
1441 if (h_papersides.empty()) {
1444 h_papersides = ss.str();
1449 string babel2lyx(string const & language)
1451 char const * const * where = is_known(language, known_languages);
1453 return known_coded_languages[where - known_languages];
1458 string rgbcolor2code(string const & name)
1460 char const * const * where = is_known(name, known_basic_colors);
1462 // "red", "green" etc
1463 return known_basic_color_codes[where - known_basic_colors];
1465 // "255,0,0", "0,255,0" etc
1466 RGBColor c(RGBColorFromLaTeX(name));
1467 return X11hexname(c);