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", "garamondx", "libertine",
128 "libertine-type1", "lmodern", "mathdesign", "mathpazo", "mathptmx", "newcent",
129 "tgbonum", "tgchorus", "tgpagella", "tgschola", "tgtermes", "utopia", 0};
131 const char * const known_sans_fonts[] = { "avant", "berasans", "biolinum-type1",
132 "cmbr", "cmss", "helvet", "kurier", "kurierl", "lmss", "tgadventor", "tgheros", 0};
134 const char * const known_kurier_fonts[] = { "kurier", "kurierl",
135 "kurier-condensed", "kurier-light-condensed", 0};
137 const char * const known_typewriter_fonts[] = { "beramono", "cmtl", "cmtt",
138 "courier", "lmtt", "luximono", "fourier", "lmodern", "mathpazo", "mathptmx",
139 "newcent", "tgcursor", "txtt", 0};
141 const char * const known_math_fonts[] = { "eulervm", "newtxmath", 0};
143 const char * const known_paper_sizes[] = { "a0paper", "b0paper", "c0paper",
144 "a1paper", "b1paper", "c1paper", "a2paper", "b2paper", "c2paper", "a3paper",
145 "b3paper", "c3paper", "a4paper", "b4paper", "c4paper", "a5paper", "b5paper",
146 "c5paper", "a6paper", "b6paper", "c6paper", "executivepaper", "legalpaper",
147 "letterpaper", "b0j", "b1j", "b2j", "b3j", "b4j", "b5j", "b6j", 0};
149 const char * const known_class_paper_sizes[] = { "a4paper", "a5paper",
150 "executivepaper", "legalpaper", "letterpaper", 0};
152 const char * const known_paper_margins[] = { "lmargin", "tmargin", "rmargin",
153 "bmargin", "headheight", "headsep", "footskip", "columnsep", 0};
155 const char * const known_coded_paper_margins[] = { "leftmargin", "topmargin",
156 "rightmargin", "bottommargin", "headheight", "headsep", "footskip",
159 /// commands that can start an \if...\else...\endif sequence
160 const char * const known_if_commands[] = {"if", "ifarydshln", "ifbraket",
161 "ifcancel", "ifcolortbl", "ifeurosym", "ifmarginnote", "ifmmode", "ifpdf",
162 "ifsidecap", "ifupgreek", 0};
164 const char * const known_basic_colors[] = {"blue", "black", "cyan", "green",
165 "magenta", "red", "white", "yellow", 0};
167 const char * const known_basic_color_codes[] = {"#0000ff", "#000000", "#00ffff",
168 "#00ff00", "#ff00ff", "#ff0000", "#ffffff", "#ffff00", 0};
170 /// conditional commands with three arguments like \@ifundefined{}{}{}
171 const char * const known_if_3arg_commands[] = {"@ifundefined", "IfFileExists",
174 /// packages that work only in xetex
175 /// polyglossia is handled separately
176 const char * const known_xetex_packages[] = {"arabxetex", "fixlatvian",
177 "fontbook", "fontwrap", "mathspec", "philokalia", "unisugar",
178 "xeCJK", "xecolor", "xecyr", "xeindex", "xepersian", "xunicode", 0};
180 /// packages that are automatically skipped if loaded by LyX
181 const char * const known_lyx_packages[] = {"amsbsy", "amsmath", "amssymb",
182 "amstext", "amsthm", "array", "babel", "booktabs", "calc", "CJK", "color",
183 "float", "fontspec", "graphicx", "hhline", "ifthen", "longtable", "makeidx",
184 "multirow", "nomencl", "pdfpages", "refstyle", "rotating", "rotfloat", "splitidx",
185 "setspace", "subscript", "textcomp", "tipa", "tipx", "tone", "ulem", "url",
186 "varioref", "verbatim", "wrapfig", "xunicode", 0};
188 // used for the handling of \newindex
189 int index_number = 0;
191 // codes used to remove packages that are loaded automatically by LyX.
192 // Syntax: package_beg_sep<name>package_mid_sep<package loading code>package_end_sep
193 const char package_beg_sep = '\001';
194 const char package_mid_sep = '\002';
195 const char package_end_sep = '\003';
198 // returns true if at least one of the options in what has been found
199 bool handle_opt(vector<string> & opts, char const * const * what, string & target)
205 // the last language option is the document language (for babel and LyX)
206 // the last size option is the document font size
207 vector<string>::iterator it;
208 vector<string>::iterator position = opts.begin();
209 for (; *what; ++what) {
210 it = find(opts.begin(), opts.end(), *what);
211 if (it != opts.end()) {
212 if (it >= position) {
223 void delete_opt(vector<string> & opts, char const * const * what)
228 // remove found options from the list
229 // do this after handle_opt to avoid potential memory leaks
230 vector<string>::iterator it;
231 for (; *what; ++what) {
232 it = find(opts.begin(), opts.end(), *what);
233 if (it != opts.end())
240 * Split a package options string (keyval format) into a vector.
242 * authorformat=smallcaps,
244 * titleformat=colonsep,
245 * bibformat={tabular,ibidem,numbered}
247 vector<string> split_options(string const & input)
249 vector<string> options;
253 Token const & t = p.get_token();
254 if (t.asInput() == ",") {
255 options.push_back(trimSpaceAndEol(option));
257 } else if (t.asInput() == "=") {
260 if (p.next_token().asInput() == "{")
261 option += '{' + p.getArg('{', '}') + '}';
262 } else if (t.cat() != catSpace)
263 option += t.asInput();
267 options.push_back(trimSpaceAndEol(option));
274 * Retrieve a keyval option "name={value with=sign}" named \p name from
275 * \p options and return the value.
276 * The found option is also removed from \p options.
278 string process_keyval_opt(vector<string> & options, string name)
280 for (size_t i = 0; i < options.size(); ++i) {
281 vector<string> option;
282 split(options[i], option, '=');
283 if (option.size() < 2)
285 if (option[0] == name) {
286 options.erase(options.begin() + i);
287 option.erase(option.begin());
288 return join(option, "=");
294 } // anonymous namespace
298 * known polyglossia language names (including variants)
300 const char * const Preamble::polyglossia_languages[] = {
301 "albanian", "croatian", "hebrew", "norsk", "swedish", "amharic", "czech", "hindi",
302 "nynorsk", "syriac", "arabic", "danish", "icelandic", "occitan", "tamil",
303 "armenian", "divehi", "interlingua", "polish", "telugu", "asturian", "dutch",
304 "irish", "portuges", "thai", "bahasai", "english", "italian", "romanian", "turkish",
305 "bahasam", "esperanto", "lao", "russian", "turkmen", "basque", "estonian", "latin",
306 "samin", "ukrainian", "bengali", "farsi", "latvian", "sanskrit", "urdu", "brazil",
307 "brazilian", "finnish", "lithuanian", "scottish", "usorbian", "breton", "french",
308 "lsorbian", "serbian", "vietnamese", "bulgarian", "galician", "magyar", "slovak",
309 "welsh", "catalan", "german", "malayalam", "slovenian", "coptic", "greek",
310 "marathi", "spanish",
311 "american", "ancient", "australian", "british", "monotonic", "newzealand",
315 * the same as polyglossia_languages with .lyx names
316 * please keep this in sync with polyglossia_languages line by line!
318 const char * const Preamble::coded_polyglossia_languages[] = {
319 "albanian", "croatian", "hebrew", "norsk", "swedish", "amharic", "czech", "hindi",
320 "nynorsk", "syriac", "arabic_arabi", "danish", "icelandic", "occitan", "tamil",
321 "armenian", "divehi", "interlingua", "polish", "telugu", "asturian", "dutch",
322 "irish", "portuges", "thai", "bahasa", "english", "italian", "romanian", "turkish",
323 "bahasam", "esperanto", "lao", "russian", "turkmen", "basque", "estonian", "latin",
324 "samin", "ukrainian", "bengali", "farsi", "latvian", "sanskrit", "urdu", "brazilian",
325 "brazilian", "finnish", "lithuanian", "scottish", "uppersorbian", "breton", "french",
326 "lowersorbian", "serbian", "vietnamese", "bulgarian", "galician", "magyar", "slovak",
327 "welsh", "catalan", "ngerman", "malayalam", "slovene", "coptic", "greek",
328 "marathi", "spanish",
329 "american", "ancientgreek", "australian", "british", "greek", "newzealand",
330 "polutonikogreek", 0};
333 bool Preamble::indentParagraphs() const
335 return h_paragraph_separation == "indent";
339 bool Preamble::isPackageUsed(string const & package) const
341 return used_packages.find(package) != used_packages.end();
345 vector<string> Preamble::getPackageOptions(string const & package) const
347 map<string, vector<string> >::const_iterator it = used_packages.find(package);
348 if (it != used_packages.end())
350 return vector<string>();
354 void Preamble::registerAutomaticallyLoadedPackage(std::string const & package)
356 auto_packages.insert(package);
360 void Preamble::addModule(string const & module)
362 used_modules.push_back(module);
366 void Preamble::suppressDate(bool suppress)
369 h_suppress_date = "true";
371 h_suppress_date = "false";
375 void Preamble::registerAuthor(std::string const & name)
377 Author author(from_utf8(name), empty_docstring());
378 author.setUsed(true);
379 authors_.record(author);
380 h_tracking_changes = "true";
381 h_output_changes = "true";
385 Author const & Preamble::getAuthor(std::string const & name) const
387 Author author(from_utf8(name), empty_docstring());
388 for (AuthorList::Authors::const_iterator it = authors_.begin();
389 it != authors_.end(); ++it)
392 static Author const dummy;
397 void Preamble::add_package(string const & name, vector<string> & options)
399 // every package inherits the global options
400 if (used_packages.find(name) == used_packages.end())
401 used_packages[name] = split_options(h_options);
403 vector<string> & v = used_packages[name];
404 v.insert(v.end(), options.begin(), options.end());
405 if (name == "jurabib") {
406 // Don't output the order argument (see the cite command
407 // handling code in text.cpp).
408 vector<string>::iterator end =
409 remove(options.begin(), options.end(), "natbiborder");
410 end = remove(options.begin(), end, "jurabiborder");
411 options.erase(end, options.end());
418 // Given is a string like "scaled=0.9" or "Scale=0.9", return 0.9 * 100
419 bool scale_as_percentage(string const & scale, string & percentage)
421 string::size_type pos = scale.find('=');
422 if (pos != string::npos) {
423 string value = scale.substr(pos + 1);
424 if (isStrDbl(value)) {
425 percentage = convert<string>(100 * convert<double>(value));
433 string remove_braces(string const & value)
437 if (value[0] == '{' && value[value.length()-1] == '}')
438 return value.substr(1, value.length()-2);
442 } // anonymous namespace
445 Preamble::Preamble() : one_language(true), title_layout_found(false),
446 h_font_cjk_set(false)
450 h_biblio_style = "plain";
451 h_bibtex_command = "default";
452 h_cite_engine = "basic";
453 h_cite_engine_type = "numerical";
455 h_defskip = "medskip";
458 h_fontencoding = "default";
459 h_font_roman = "default";
460 h_font_sans = "default";
461 h_font_typewriter = "default";
462 h_font_math = "auto";
463 h_font_default_family = "default";
464 h_use_non_tex_fonts = "false";
466 h_font_osf = "false";
467 h_font_sf_scale = "100";
468 h_font_tt_scale = "100";
470 h_graphics = "default";
471 h_default_output_format = "default";
472 h_html_be_strict = "false";
473 h_html_css_as_file = "0";
474 h_html_math_output = "0";
475 h_index[0] = "Index";
476 h_index_command = "default";
477 h_inputencoding = "auto";
478 h_justification = "true";
479 h_language = "english";
480 h_language_package = "none";
482 h_maintain_unincluded_children = "false";
486 h_output_changes = "false";
488 //h_output_sync_macro
489 h_papercolumns = "1";
490 h_paperfontsize = "default";
491 h_paperorientation = "portrait";
492 h_paperpagestyle = "default";
494 h_papersize = "default";
495 h_paragraph_indentation = "default";
496 h_paragraph_separation = "indent";
501 h_pdf_bookmarks = "1";
502 h_pdf_bookmarksnumbered = "0";
503 h_pdf_bookmarksopen = "0";
504 h_pdf_bookmarksopenlevel = "1";
505 h_pdf_breaklinks = "0";
506 h_pdf_pdfborder = "0";
507 h_pdf_colorlinks = "0";
508 h_pdf_backref = "section";
509 h_pdf_pdfusetitle = "1";
511 //h_pdf_quoted_options;
512 h_quotes_language = "english";
514 h_shortcut[0] = "idx";
515 h_spacing = "single";
516 h_suppress_date = "false";
517 h_textclass = "article";
519 h_tracking_changes = "false";
520 h_use_bibtopic = "false";
521 h_use_indices = "false";
522 h_use_geometry = "false";
523 h_use_default_options = "false";
524 h_use_hyperref = "false";
525 h_use_refstyle = "0";
526 h_use_packages["amsmath"] = "1";
527 h_use_packages["amssymb"] = "0";
528 h_use_packages["esint"] = "1";
529 h_use_packages["mhchem"] = "0";
530 h_use_packages["mathdots"] = "0";
531 h_use_packages["mathtools"] = "0";
532 h_use_packages["stackrel"] = "0";
533 h_use_packages["stmaryrd"] = "0";
534 h_use_packages["undertilde"] = "0";
538 void Preamble::handle_hyperref(vector<string> & options)
540 // FIXME swallow inputencoding changes that might surround the
541 // hyperref setup if it was written by LyX
542 h_use_hyperref = "true";
543 // swallow "unicode=true", since LyX does always write that
544 vector<string>::iterator it =
545 find(options.begin(), options.end(), "unicode=true");
546 if (it != options.end())
548 it = find(options.begin(), options.end(), "pdfusetitle");
549 if (it != options.end()) {
550 h_pdf_pdfusetitle = "1";
553 string bookmarks = process_keyval_opt(options, "bookmarks");
554 if (bookmarks == "true")
555 h_pdf_bookmarks = "1";
556 else if (bookmarks == "false")
557 h_pdf_bookmarks = "0";
558 if (h_pdf_bookmarks == "1") {
559 string bookmarksnumbered =
560 process_keyval_opt(options, "bookmarksnumbered");
561 if (bookmarksnumbered == "true")
562 h_pdf_bookmarksnumbered = "1";
563 else if (bookmarksnumbered == "false")
564 h_pdf_bookmarksnumbered = "0";
565 string bookmarksopen =
566 process_keyval_opt(options, "bookmarksopen");
567 if (bookmarksopen == "true")
568 h_pdf_bookmarksopen = "1";
569 else if (bookmarksopen == "false")
570 h_pdf_bookmarksopen = "0";
571 if (h_pdf_bookmarksopen == "1") {
572 string bookmarksopenlevel =
573 process_keyval_opt(options, "bookmarksopenlevel");
574 if (!bookmarksopenlevel.empty())
575 h_pdf_bookmarksopenlevel = bookmarksopenlevel;
578 string breaklinks = process_keyval_opt(options, "breaklinks");
579 if (breaklinks == "true")
580 h_pdf_breaklinks = "1";
581 else if (breaklinks == "false")
582 h_pdf_breaklinks = "0";
583 string pdfborder = process_keyval_opt(options, "pdfborder");
584 if (pdfborder == "{0 0 0}")
585 h_pdf_pdfborder = "1";
586 else if (pdfborder == "{0 0 1}")
587 h_pdf_pdfborder = "0";
588 string backref = process_keyval_opt(options, "backref");
589 if (!backref.empty())
590 h_pdf_backref = backref;
591 string colorlinks = process_keyval_opt(options, "colorlinks");
592 if (colorlinks == "true")
593 h_pdf_colorlinks = "1";
594 else if (colorlinks == "false")
595 h_pdf_colorlinks = "0";
596 string pdfpagemode = process_keyval_opt(options, "pdfpagemode");
597 if (!pdfpagemode.empty())
598 h_pdf_pagemode = pdfpagemode;
599 string pdftitle = process_keyval_opt(options, "pdftitle");
600 if (!pdftitle.empty()) {
601 h_pdf_title = remove_braces(pdftitle);
603 string pdfauthor = process_keyval_opt(options, "pdfauthor");
604 if (!pdfauthor.empty()) {
605 h_pdf_author = remove_braces(pdfauthor);
607 string pdfsubject = process_keyval_opt(options, "pdfsubject");
608 if (!pdfsubject.empty())
609 h_pdf_subject = remove_braces(pdfsubject);
610 string pdfkeywords = process_keyval_opt(options, "pdfkeywords");
611 if (!pdfkeywords.empty())
612 h_pdf_keywords = remove_braces(pdfkeywords);
613 if (!options.empty()) {
614 if (!h_pdf_quoted_options.empty())
615 h_pdf_quoted_options += ',';
616 h_pdf_quoted_options += join(options, ",");
622 void Preamble::handle_geometry(vector<string> & options)
624 h_use_geometry = "true";
625 vector<string>::iterator it;
627 if ((it = find(options.begin(), options.end(), "landscape")) != options.end()) {
628 h_paperorientation = "landscape";
632 // keyval version: "paper=letter"
633 string paper = process_keyval_opt(options, "paper");
635 h_papersize = paper + "paper";
636 // alternative version: "letterpaper"
637 handle_opt(options, known_paper_sizes, h_papersize);
638 delete_opt(options, known_paper_sizes);
640 char const * const * margin = known_paper_margins;
641 for (; *margin; ++margin) {
642 string value = process_keyval_opt(options, *margin);
643 if (!value.empty()) {
644 int k = margin - known_paper_margins;
645 string name = known_coded_paper_margins[k];
646 h_margins += '\\' + name + ' ' + value + '\n';
652 void Preamble::handle_package(Parser &p, string const & name,
653 string const & opts, bool in_lyx_preamble)
655 vector<string> options = split_options(opts);
656 add_package(name, options);
657 char const * const * where = 0;
659 if (is_known(name, known_xetex_packages)) {
661 h_use_non_tex_fonts = "true";
662 registerAutomaticallyLoadedPackage("fontspec");
663 if (h_inputencoding == "auto")
664 p.setEncoding("UTF-8");
668 if (is_known(name, known_roman_fonts))
671 if (name == "fourier") {
672 h_font_roman = "utopia";
673 // when font uses real small capitals
674 if (opts == "expert")
678 if (name == "garamondx") {
679 h_font_roman = "garamondx";
684 if (name == "libertine") {
685 h_font_roman = "libertine";
686 // this automatically invokes biolinum
687 h_font_sans = "biolinum";
690 else if (opts == "lining")
691 h_font_osf = "false";
694 if (name == "libertine-type1") {
695 h_font_roman = "libertine";
696 // NOTE: contrary to libertine.sty, libertine-type1
697 // does not automatically invoke biolinum
698 if (opts == "lining")
699 h_font_osf = "false";
700 else if (opts == "osf")
704 if (name == "mathdesign") {
705 if (opts.find("charter") != string::npos)
706 h_font_roman = "md-charter";
707 if (opts.find("garamond") != string::npos)
708 h_font_roman = "md-garamond";
709 if (opts.find("utopia") != string::npos)
710 h_font_roman = "md-utopia";
711 if (opts.find("expert") != string::npos) {
717 else if (name == "mathpazo")
718 h_font_roman = "palatino";
720 else if (name == "mathptmx")
721 h_font_roman = "times";
724 if (is_known(name, known_sans_fonts)) {
726 if (options.size() == 1) {
727 if (scale_as_percentage(opts, h_font_sf_scale))
732 if (name == "biolinum-type1")
733 h_font_sans = "biolinum";
736 if (is_known(name, known_typewriter_fonts)) {
737 // fourier can be set as roman font _only_
738 // fourier as typewriter is handled in handling of \ttdefault
739 if (name != "fourier") {
740 h_font_typewriter = name;
741 if (options.size() == 1) {
742 if (scale_as_percentage(opts, h_font_tt_scale))
748 // font uses old-style figure
753 if (is_known(name, known_math_fonts))
756 if (name == "newtxmath") {
758 h_font_math = "newtxmath";
759 else if (opts == "garamondx")
760 h_font_math = "garamondx-ntxm";
761 else if (opts == "libertine")
762 h_font_math = "libertine-ntxm";
763 else if (opts == "minion")
764 h_font_math = "minion-ntxm";
767 // after the detection and handling of special cases, we can remove the
768 // fonts, otherwise they would appear in the preamble, see bug #7856
769 if (is_known(name, known_roman_fonts) || is_known(name, known_sans_fonts)
770 || is_known(name, known_typewriter_fonts) || is_known(name, known_math_fonts))
773 else if (name == "amsmath" || name == "amssymb" ||
774 name == "esint" || name == "mhchem" || name == "mathdots" ||
775 name == "mathtools" || name == "stackrel" ||
776 name == "stmaryrd" || name == "undertilde")
777 h_use_packages[name] = "2";
779 else if (name == "babel") {
780 h_language_package = "default";
781 // One might think we would have to do nothing if babel is loaded
782 // without any options to prevent pollution of the preamble with this
783 // babel call in every roundtrip.
784 // But the user could have defined babel-specific things afterwards. So
785 // we need to keep it in the preamble to prevent cases like bug #7861.
787 // check if more than one option was used - used later for inputenc
788 if (options.begin() != options.end() - 1)
789 one_language = false;
790 // babel takes the last language of the option of its \usepackage
791 // call as document language. If there is no such language option, the
792 // last language in the documentclass options is used.
793 handle_opt(options, known_languages, h_language);
794 // translate the babel name to a LyX name
795 h_language = babel2lyx(h_language);
796 if (h_language == "japanese") {
797 // For Japanese, the encoding isn't indicated in the source
798 // file, and there's really not much we can do. We could
799 // 1) offer a list of possible encodings to choose from, or
800 // 2) determine the encoding of the file by inspecting it.
801 // For the time being, we leave the encoding alone so that
802 // we don't get iconv errors when making a wrong guess, and
803 // we will output a note at the top of the document
804 // explaining what to do.
805 Encoding const * const enc = encodings.fromIconvName(
806 p.getEncoding(), Encoding::japanese, false);
808 h_inputencoding = enc->name();
809 is_nonCJKJapanese = true;
810 // in this case babel can be removed from the preamble
811 registerAutomaticallyLoadedPackage("babel");
813 // If babel is called with options, LyX puts them by default into the
814 // document class options. This works for most languages, except
815 // for Latvian, Lithuanian, Mongolian, Turkmen and Vietnamese and
816 // perhaps in future others.
817 // Therefore keep the babel call as it is as the user might have
819 h_preamble << "\\usepackage[" << opts << "]{babel}\n";
821 delete_opt(options, known_languages);
824 h_preamble << "\\usepackage{babel}\n";
827 else if (name == "polyglossia") {
828 h_language_package = "default";
829 h_default_output_format = "pdf4";
830 h_use_non_tex_fonts = "true";
832 registerAutomaticallyLoadedPackage("xunicode");
833 if (h_inputencoding == "auto")
834 p.setEncoding("UTF-8");
837 else if (name == "CJK") {
838 // set the encoding to "auto" because it might be set to "default" by the babel handling
839 // and this would not be correct for CJK
840 if (h_inputencoding == "default")
841 h_inputencoding = "auto";
842 registerAutomaticallyLoadedPackage("CJK");
845 else if (name == "CJKutf8") {
846 h_inputencoding = "UTF8";
847 p.setEncoding("UTF-8");
848 registerAutomaticallyLoadedPackage("CJKutf8");
851 else if (name == "fontenc") {
852 h_fontencoding = getStringFromVector(options, ",");
853 /* We could do the following for better round trip support,
854 * but this makes the document less portable, so I skip it:
855 if (h_fontencoding == lyxrc.fontenc)
856 h_fontencoding = "global";
861 else if (name == "inputenc" || name == "luainputenc") {
862 // h_inputencoding is only set when there is not more than one
863 // inputenc option because otherwise h_inputencoding must be
864 // set to "auto" (the default encoding of the document language)
865 // Therefore check for the "," character.
866 // It is also only set when there is not more than one babel
868 if (opts.find(",") == string::npos && one_language == true)
869 h_inputencoding = opts;
870 if (!options.empty())
871 p.setEncoding(options.back(), Encoding::inputenc);
875 else if (name == "srcltx") {
878 h_output_sync_macro = "\\usepackage[" + opts + "]{srcltx}";
881 h_output_sync_macro = "\\usepackage{srcltx}";
884 else if (is_known(name, known_old_language_packages)) {
885 // known language packages from the times before babel
886 // if they are found and not also babel, they will be used as
887 // custom language package
888 h_language_package = "\\usepackage{" + name + "}";
891 else if (name == "prettyref")
892 ; // ignore this FIXME: Use the package separator mechanism instead
894 else if (name == "lyxskak") {
895 // ignore this and its options
896 const char * const o[] = {"ps", "mover", 0};
897 delete_opt(options, o);
900 else if (is_known(name, known_lyx_packages) && options.empty()) {
901 if (name == "splitidx")
902 h_use_indices = "true";
903 if (name == "refstyle")
904 h_use_refstyle = "1";
905 if (!in_lyx_preamble) {
906 h_preamble << package_beg_sep << name
907 << package_mid_sep << "\\usepackage{"
909 if (p.next_token().cat() == catNewline ||
910 (p.next_token().cat() == catSpace &&
911 p.next_next_token().cat() == catNewline))
913 h_preamble << package_end_sep;
917 else if (name == "geometry")
918 handle_geometry(options);
920 else if (name == "subfig")
921 ; // ignore this FIXME: Use the package separator mechanism instead
923 else if ((where = is_known(name, known_languages)))
924 h_language = known_coded_languages[where - known_languages];
926 else if (name == "natbib") {
927 h_biblio_style = "plainnat";
928 h_cite_engine = "natbib";
929 h_cite_engine_type = "authoryear";
930 vector<string>::iterator it =
931 find(options.begin(), options.end(), "authoryear");
932 if (it != options.end())
935 it = find(options.begin(), options.end(), "numbers");
936 if (it != options.end()) {
937 h_cite_engine_type = "numerical";
943 else if (name == "jurabib") {
944 h_biblio_style = "jurabib";
945 h_cite_engine = "jurabib";
946 h_cite_engine_type = "authoryear";
949 else if (name == "hyperref")
950 handle_hyperref(options);
952 else if (!in_lyx_preamble) {
954 h_preamble << "\\usepackage{" << name << '}';
956 h_preamble << "\\usepackage[" << opts << "]{"
960 if (p.next_token().cat() == catNewline ||
961 (p.next_token().cat() == catSpace &&
962 p.next_next_token().cat() == catNewline))
966 // We need to do something with the options...
967 if (!options.empty())
968 cerr << "Ignoring options '" << join(options, ",")
969 << "' of package " << name << '.' << endl;
971 // remove the whitespace
976 void Preamble::handle_if(Parser & p, bool in_lyx_preamble)
979 Token t = p.get_token();
980 if (t.cat() == catEscape &&
981 is_known(t.cs(), known_if_commands))
982 handle_if(p, in_lyx_preamble);
984 if (!in_lyx_preamble)
985 h_preamble << t.asInput();
986 if (t.cat() == catEscape && t.cs() == "fi")
993 bool Preamble::writeLyXHeader(ostream & os, bool subdoc)
995 // set the quote language
996 // LyX only knows the following quotes languages:
997 // english, swedish, german, polish, french and danish
998 // (quotes for "japanese" and "chinese-traditional" are missing because
999 // they wouldn't be useful: http://www.lyx.org/trac/ticket/6383)
1000 // conversion list taken from
1001 // http://en.wikipedia.org/wiki/Quotation_mark,_non-English_usage
1002 // (quotes for kazakh and interlingua are unknown)
1004 if (is_known(h_language, known_danish_quotes_languages))
1005 h_quotes_language = "danish";
1007 else if (is_known(h_language, known_french_quotes_languages))
1008 h_quotes_language = "french";
1010 else if (is_known(h_language, known_german_quotes_languages))
1011 h_quotes_language = "german";
1013 else if (is_known(h_language, known_polish_quotes_languages))
1014 h_quotes_language = "polish";
1016 else if (is_known(h_language, known_swedish_quotes_languages))
1017 h_quotes_language = "swedish";
1019 else if (is_known(h_language, known_english_quotes_languages))
1020 h_quotes_language = "english";
1022 if (contains(h_float_placement, "H"))
1023 registerAutomaticallyLoadedPackage("float");
1024 if (h_spacing != "single" && h_spacing != "default")
1025 registerAutomaticallyLoadedPackage("setspace");
1026 if (h_use_packages["amsmath"] == "2") {
1027 // amsbsy and amstext are already provided by amsmath
1028 registerAutomaticallyLoadedPackage("amsbsy");
1029 registerAutomaticallyLoadedPackage("amstext");
1032 // output the LyX file settings
1033 os << "#LyX file created by tex2lyx " << PACKAGE_VERSION << "\n"
1034 << "\\lyxformat " << LYX_FORMAT << '\n'
1035 << "\\begin_document\n"
1036 << "\\begin_header\n"
1037 << "\\textclass " << h_textclass << "\n";
1038 string const raw = subdoc ? empty_string() : h_preamble.str();
1040 os << "\\begin_preamble\n";
1041 for (string::size_type i = 0; i < raw.size(); ++i) {
1042 if (raw[i] == package_beg_sep) {
1043 // Here follows some package loading code that
1044 // must be skipped if the package is loaded
1046 string::size_type j = raw.find(package_mid_sep, i);
1047 if (j == string::npos)
1049 string::size_type k = raw.find(package_end_sep, j);
1050 if (k == string::npos)
1052 string const package = raw.substr(i + 1, j - i - 1);
1053 string const replacement = raw.substr(j + 1, k - j - 1);
1054 if (auto_packages.find(package) == auto_packages.end())
1060 os << "\n\\end_preamble\n";
1062 if (!h_options.empty())
1063 os << "\\options " << h_options << "\n";
1064 os << "\\use_default_options " << h_use_default_options << "\n";
1065 if (!used_modules.empty()) {
1066 os << "\\begin_modules\n";
1067 vector<string>::const_iterator const end = used_modules.end();
1068 vector<string>::const_iterator it = used_modules.begin();
1069 for (; it != end; ++it)
1071 os << "\\end_modules\n";
1073 os << "\\maintain_unincluded_children " << h_maintain_unincluded_children << "\n"
1074 << "\\language " << h_language << "\n"
1075 << "\\language_package " << h_language_package << "\n"
1076 << "\\inputencoding " << h_inputencoding << "\n"
1077 << "\\fontencoding " << h_fontencoding << "\n"
1078 << "\\font_roman " << h_font_roman << "\n"
1079 << "\\font_sans " << h_font_sans << "\n"
1080 << "\\font_typewriter " << h_font_typewriter << "\n"
1081 << "\\font_math " << h_font_math << "\n"
1082 << "\\font_default_family " << h_font_default_family << "\n"
1083 << "\\use_non_tex_fonts " << h_use_non_tex_fonts << "\n"
1084 << "\\font_sc " << h_font_sc << "\n"
1085 << "\\font_osf " << h_font_osf << "\n"
1086 << "\\font_sf_scale " << h_font_sf_scale << "\n"
1087 << "\\font_tt_scale " << h_font_tt_scale << '\n';
1088 if (!h_font_cjk.empty())
1089 os << "\\font_cjk " << h_font_cjk << '\n';
1090 os << "\\graphics " << h_graphics << '\n'
1091 << "\\default_output_format " << h_default_output_format << "\n"
1092 << "\\output_sync " << h_output_sync << "\n";
1093 if (h_output_sync == "1")
1094 os << "\\output_sync_macro \"" << h_output_sync_macro << "\"\n";
1095 os << "\\bibtex_command " << h_bibtex_command << "\n"
1096 << "\\index_command " << h_index_command << "\n";
1097 if (!h_float_placement.empty())
1098 os << "\\float_placement " << h_float_placement << "\n";
1099 os << "\\paperfontsize " << h_paperfontsize << "\n"
1100 << "\\spacing " << h_spacing << "\n"
1101 << "\\use_hyperref " << h_use_hyperref << '\n';
1102 if (h_use_hyperref == "true") {
1103 if (!h_pdf_title.empty())
1104 os << "\\pdf_title \"" << h_pdf_title << "\"\n";
1105 if (!h_pdf_author.empty())
1106 os << "\\pdf_author \"" << h_pdf_author << "\"\n";
1107 if (!h_pdf_subject.empty())
1108 os << "\\pdf_subject \"" << h_pdf_subject << "\"\n";
1109 if (!h_pdf_keywords.empty())
1110 os << "\\pdf_keywords \"" << h_pdf_keywords << "\"\n";
1111 os << "\\pdf_bookmarks " << h_pdf_bookmarks << "\n"
1112 "\\pdf_bookmarksnumbered " << h_pdf_bookmarksnumbered << "\n"
1113 "\\pdf_bookmarksopen " << h_pdf_bookmarksopen << "\n"
1114 "\\pdf_bookmarksopenlevel " << h_pdf_bookmarksopenlevel << "\n"
1115 "\\pdf_breaklinks " << h_pdf_breaklinks << "\n"
1116 "\\pdf_pdfborder " << h_pdf_pdfborder << "\n"
1117 "\\pdf_colorlinks " << h_pdf_colorlinks << "\n"
1118 "\\pdf_backref " << h_pdf_backref << "\n"
1119 "\\pdf_pdfusetitle " << h_pdf_pdfusetitle << '\n';
1120 if (!h_pdf_pagemode.empty())
1121 os << "\\pdf_pagemode " << h_pdf_pagemode << '\n';
1122 if (!h_pdf_quoted_options.empty())
1123 os << "\\pdf_quoted_options \"" << h_pdf_quoted_options << "\"\n";
1125 os << "\\papersize " << h_papersize << "\n"
1126 << "\\use_geometry " << h_use_geometry << '\n';
1127 for (map<string, string>::const_iterator it = h_use_packages.begin();
1128 it != h_use_packages.end(); ++it)
1129 os << "\\use_package " << it->first << ' ' << it->second << '\n';
1130 os << "\\cite_engine " << h_cite_engine << '\n'
1131 << "\\cite_engine_type " << h_cite_engine_type << '\n'
1132 << "\\biblio_style " << h_biblio_style << "\n"
1133 << "\\use_bibtopic " << h_use_bibtopic << "\n"
1134 << "\\use_indices " << h_use_indices << "\n"
1135 << "\\paperorientation " << h_paperorientation << '\n'
1136 << "\\suppress_date " << h_suppress_date << '\n'
1137 << "\\justification " << h_justification << '\n'
1138 << "\\use_refstyle " << h_use_refstyle << '\n';
1139 if (!h_fontcolor.empty())
1140 os << "\\fontcolor " << h_fontcolor << '\n';
1141 if (!h_notefontcolor.empty())
1142 os << "\\notefontcolor " << h_notefontcolor << '\n';
1143 if (!h_backgroundcolor.empty())
1144 os << "\\backgroundcolor " << h_backgroundcolor << '\n';
1145 if (!h_boxbgcolor.empty())
1146 os << "\\boxbgcolor " << h_boxbgcolor << '\n';
1147 if (index_number != 0)
1148 for (int i = 0; i < index_number; i++) {
1149 os << "\\index " << h_index[i] << '\n'
1150 << "\\shortcut " << h_shortcut[i] << '\n'
1151 << "\\color " << h_color << '\n'
1155 os << "\\index " << h_index[0] << '\n'
1156 << "\\shortcut " << h_shortcut[0] << '\n'
1157 << "\\color " << h_color << '\n'
1161 << "\\secnumdepth " << h_secnumdepth << "\n"
1162 << "\\tocdepth " << h_tocdepth << "\n"
1163 << "\\paragraph_separation " << h_paragraph_separation << "\n";
1164 if (h_paragraph_separation == "skip")
1165 os << "\\defskip " << h_defskip << "\n";
1167 os << "\\paragraph_indentation " << h_paragraph_indentation << "\n";
1168 os << "\\quotes_language " << h_quotes_language << "\n"
1169 << "\\papercolumns " << h_papercolumns << "\n"
1170 << "\\papersides " << h_papersides << "\n"
1171 << "\\paperpagestyle " << h_paperpagestyle << "\n";
1172 if (!h_listings_params.empty())
1173 os << "\\listings_params " << h_listings_params << "\n";
1174 os << "\\tracking_changes " << h_tracking_changes << "\n"
1175 << "\\output_changes " << h_output_changes << "\n"
1176 << "\\html_math_output " << h_html_math_output << "\n"
1177 << "\\html_css_as_file " << h_html_css_as_file << "\n"
1178 << "\\html_be_strict " << h_html_be_strict << "\n"
1180 << "\\end_header\n\n"
1181 << "\\begin_body\n";
1186 void Preamble::parse(Parser & p, string const & forceclass,
1187 TeX2LyXDocClass & tc)
1189 // initialize fixed types
1190 special_columns['D'] = 3;
1191 bool is_full_document = false;
1192 bool is_lyx_file = false;
1193 bool in_lyx_preamble = false;
1195 // determine whether this is a full document or a fragment for inclusion
1197 Token const & t = p.get_token();
1199 if (t.cat() == catEscape && t.cs() == "documentclass") {
1200 is_full_document = true;
1206 while (is_full_document && p.good()) {
1207 Token const & t = p.get_token();
1210 cerr << "t: " << t << "\n";
1216 if (!in_lyx_preamble &&
1217 (t.cat() == catLetter ||
1218 t.cat() == catSuper ||
1219 t.cat() == catSub ||
1220 t.cat() == catOther ||
1221 t.cat() == catMath ||
1222 t.cat() == catActive ||
1223 t.cat() == catBegin ||
1224 t.cat() == catEnd ||
1225 t.cat() == catAlign ||
1226 t.cat() == catParameter))
1227 h_preamble << t.cs();
1229 else if (!in_lyx_preamble &&
1230 (t.cat() == catSpace || t.cat() == catNewline))
1231 h_preamble << t.asInput();
1233 else if (t.cat() == catComment) {
1234 static regex const islyxfile("%% LyX .* created this file");
1235 static regex const usercommands("User specified LaTeX commands");
1237 string const comment = t.asInput();
1239 // magically switch encoding default if it looks like XeLaTeX
1240 static string const magicXeLaTeX =
1241 "% This document must be compiled with XeLaTeX ";
1242 if (comment.size() > magicXeLaTeX.size()
1243 && comment.substr(0, magicXeLaTeX.size()) == magicXeLaTeX
1244 && h_inputencoding == "auto") {
1245 cerr << "XeLaTeX comment found, switching to UTF8\n";
1246 h_inputencoding = "utf8";
1249 if (regex_search(comment, sub, islyxfile)) {
1251 in_lyx_preamble = true;
1252 } else if (is_lyx_file
1253 && regex_search(comment, sub, usercommands))
1254 in_lyx_preamble = false;
1255 else if (!in_lyx_preamble)
1256 h_preamble << t.asInput();
1259 else if (t.cs() == "pagestyle")
1260 h_paperpagestyle = p.verbatim_item();
1262 else if (t.cs() == "setdefaultlanguage") {
1264 // We don't yet care about non-language variant options
1265 // because LyX doesn't support this yet, see bug #8214
1267 string langopts = p.getOpt();
1268 // check if the option contains a variant, if yes, extract it
1269 string::size_type pos_var = langopts.find("variant");
1270 string::size_type i = langopts.find(',', pos_var);
1271 string::size_type k = langopts.find('=', pos_var);
1272 if (pos_var != string::npos){
1274 if (i == string::npos)
1275 variant = langopts.substr(k + 1, langopts.length() - k - 2);
1277 variant = langopts.substr(k + 1, i - k - 1);
1278 h_language = variant;
1282 h_language = p.verbatim_item();
1283 //finally translate the poyglossia name to a LyX name
1284 h_language = polyglossia2lyx(h_language);
1287 else if (t.cs() == "setotherlanguage") {
1288 // We don't yet care about the option because LyX doesn't
1289 // support this yet, see bug #8214
1290 p.hasOpt() ? p.getOpt() : string();
1294 else if (t.cs() == "setmainfont") {
1295 // we don't care about the option
1296 p.hasOpt() ? p.getOpt() : string();
1297 h_font_roman = p.getArg('{', '}');
1300 else if (t.cs() == "setsansfont" || t.cs() == "setmonofont") {
1301 // LyX currently only supports the scale option
1304 string fontopts = p.getArg('[', ']');
1305 // check if the option contains a scaling, if yes, extract it
1306 string::size_type pos = fontopts.find("Scale");
1307 if (pos != string::npos) {
1308 string::size_type i = fontopts.find(',', pos);
1309 if (i == string::npos)
1310 scale_as_percentage(fontopts.substr(pos + 1), scale);
1312 scale_as_percentage(fontopts.substr(pos, i - pos), scale);
1315 if (t.cs() == "setsansfont") {
1317 h_font_sf_scale = scale;
1318 h_font_sans = p.getArg('{', '}');
1321 h_font_tt_scale = scale;
1322 h_font_typewriter = p.getArg('{', '}');
1326 else if (t.cs() == "date") {
1327 string argument = p.getArg('{', '}');
1328 if (argument.empty())
1329 h_suppress_date = "true";
1331 h_preamble << t.asInput() << '{' << argument << '}';
1334 else if (t.cs() == "color") {
1335 string const space =
1336 (p.hasOpt() ? p.getOpt() : string());
1337 string argument = p.getArg('{', '}');
1338 // check the case that a standard color is used
1339 if (space.empty() && is_known(argument, known_basic_colors)) {
1340 h_fontcolor = rgbcolor2code(argument);
1341 preamble.registerAutomaticallyLoadedPackage("color");
1342 } else if (space.empty() && argument == "document_fontcolor")
1343 preamble.registerAutomaticallyLoadedPackage("color");
1344 // check the case that LyX's document_fontcolor is defined
1345 // but not used for \color
1347 h_preamble << t.asInput();
1349 h_preamble << space;
1350 h_preamble << '{' << argument << '}';
1351 // the color might already be set because \definecolor
1352 // is parsed before this
1357 else if (t.cs() == "pagecolor") {
1358 string argument = p.getArg('{', '}');
1359 // check the case that a standard color is used
1360 if (is_known(argument, known_basic_colors)) {
1361 h_backgroundcolor = rgbcolor2code(argument);
1362 } else if (argument == "page_backgroundcolor")
1363 preamble.registerAutomaticallyLoadedPackage("color");
1364 // check the case that LyX's page_backgroundcolor is defined
1365 // but not used for \pagecolor
1367 h_preamble << t.asInput() << '{' << argument << '}';
1368 // the color might already be set because \definecolor
1369 // is parsed before this
1370 h_backgroundcolor = "";
1374 else if (t.cs() == "makeatletter") {
1375 // LyX takes care of this
1376 p.setCatcode('@', catLetter);
1379 else if (t.cs() == "makeatother") {
1380 // LyX takes care of this
1381 p.setCatcode('@', catOther);
1384 else if (t.cs() == "makeindex") {
1385 // LyX will re-add this if a print index command is found
1389 else if (t.cs() == "newindex") {
1390 string const indexname = p.getArg('[', ']');
1391 string const shortcut = p.verbatim_item();
1392 if (!indexname.empty())
1393 h_index[index_number] = indexname;
1395 h_index[index_number] = shortcut;
1396 h_shortcut[index_number] = shortcut;
1401 else if (t.cs() == "RS@ifundefined") {
1402 string const name = p.verbatim_item();
1403 string const body1 = p.verbatim_item();
1404 string const body2 = p.verbatim_item();
1405 // store the in_lyx_preamble setting
1406 bool const was_in_lyx_preamble = in_lyx_preamble;
1407 if (name == "subref"
1409 || name == "lemref") {
1411 preamble.registerAutomaticallyLoadedPackage("refstyle");
1412 in_lyx_preamble = true;
1414 // only non-lyxspecific stuff
1415 if (!in_lyx_preamble) {
1417 ss << '\\' << t.cs();
1418 ss << '{' << name << '}'
1419 << '{' << body1 << '}'
1420 << '{' << body2 << '}';
1421 h_preamble << ss.str();
1423 // restore the in_lyx_preamble setting
1424 in_lyx_preamble = was_in_lyx_preamble;
1427 else if (t.cs() == "AtBeginDocument") {
1428 string const name = p.verbatim_item();
1429 // store the in_lyx_preamble setting
1430 bool const was_in_lyx_preamble = in_lyx_preamble;
1431 if (name == "\\providecommand\\partref[1]{\\ref{part:#1}}"
1432 || name == "\\providecommand\\chapref[1]{\\ref{chap:#1}}"
1433 || name == "\\providecommand\\secref[1]{\\ref{sec:#1}}"
1434 || name == "\\providecommand\\subref[1]{\\ref{sub:#1}}"
1435 || name == "\\providecommand\\parref[1]{\\ref{par:#1}}"
1436 || name == "\\providecommand\\figref[1]{\\ref{fig:#1}}"
1437 || name == "\\providecommand\\tabref[1]{\\ref{tab:#1}}"
1438 || name == "\\providecommand\\algref[1]{\\ref{alg:#1}}"
1439 || name == "\\providecommand\\fnref[1]{\\ref{fn:#1}}"
1440 || name == "\\providecommand\\enuref[1]{\\ref{enu:#1}}"
1441 || name == "\\providecommand\\eqref[1]{\\ref{eq:#1}}"
1442 || name == "\\providecommand\\lemref[1]{\\ref{lem:#1}}"
1443 || name == "\\providecommand\\thmref[1]{\\ref{thm:#1}}"
1444 || name == "\\providecommand\\corref[1]{\\ref{cor:#1}}"
1445 || name == "\\providecommand\\propref[1]{\\ref{prop:#1}}") {
1447 preamble.registerAutomaticallyLoadedPackage("refstyle");
1448 in_lyx_preamble = true;
1450 // only non-lyxspecific stuff
1451 if (!in_lyx_preamble) {
1453 ss << '\\' << t.cs();
1454 ss << '{' << name << '}';
1455 h_preamble << ss.str();
1457 // restore the in_lyx_preamble setting
1458 in_lyx_preamble = was_in_lyx_preamble;
1461 else if (t.cs() == "newcommand" || t.cs() == "newcommandx"
1462 || t.cs() == "renewcommand" || t.cs() == "renewcommandx"
1463 || t.cs() == "providecommand" || t.cs() == "providecommandx"
1464 || t.cs() == "DeclareRobustCommand"
1465 || t.cs() == "DeclareRobustCommandx"
1466 || t.cs() == "ProvideTextCommandDefault"
1467 || t.cs() == "DeclareMathAccent") {
1469 if (p.next_token().character() == '*') {
1473 string const name = p.verbatim_item();
1474 string const opt1 = p.getFullOpt();
1475 string const opt2 = p.getFullOpt();
1476 string const body = p.verbatim_item();
1477 // store the in_lyx_preamble setting
1478 bool const was_in_lyx_preamble = in_lyx_preamble;
1480 if (name == "\\rmdefault")
1481 if (is_known(body, known_roman_fonts)) {
1482 h_font_roman = body;
1484 in_lyx_preamble = true;
1486 if (name == "\\sfdefault")
1487 if (is_known(body, known_sans_fonts)) {
1490 in_lyx_preamble = true;
1492 if (name == "\\ttdefault")
1493 if (is_known(body, known_typewriter_fonts)) {
1494 h_font_typewriter = body;
1496 in_lyx_preamble = true;
1498 if (name == "\\familydefault") {
1499 string family = body;
1500 // remove leading "\"
1501 h_font_default_family = family.erase(0,1);
1503 in_lyx_preamble = true;
1505 if (name == "\\bfdefault")
1506 // LyX re-adds this if a kurier font is used
1507 if (is_known(h_font_sans, known_kurier_fonts) && body == "b") {
1509 in_lyx_preamble = true;
1512 // remove the lyxdot definition that is re-added by LyX
1514 if (name == "\\lyxdot") {
1516 in_lyx_preamble = true;
1519 // Add the command to the known commands
1520 add_known_command(name, opt1, !opt2.empty(), from_utf8(body));
1522 // only non-lyxspecific stuff
1523 if (!in_lyx_preamble) {
1525 ss << '\\' << t.cs();
1528 ss << '{' << name << '}' << opt1 << opt2
1529 << '{' << body << "}";
1530 h_preamble << ss.str();
1532 ostream & out = in_preamble ? h_preamble : os;
1533 out << "\\" << t.cs() << "{" << name << "}"
1534 << opts << "{" << body << "}";
1537 // restore the in_lyx_preamble setting
1538 in_lyx_preamble = was_in_lyx_preamble;
1541 else if (t.cs() == "edef"){
1542 // we only support this for kurier fonts
1543 string const command = p.next_token().asInput();
1545 if (command == "\\sfdefault") {
1547 if (h_font_sans == "kurier")
1548 h_font_sans = "kurier-condensed";
1549 if (h_font_sans == "kurierl")
1550 h_font_sans = "kurier-light-condensed";
1554 h_preamble << "\\edef" << command << "{" << p.getArg('{', '}') << "}\n";
1557 else if (t.cs() == "documentclass") {
1558 vector<string>::iterator it;
1559 vector<string> opts = split_options(p.getArg('[', ']'));
1560 handle_opt(opts, known_fontsizes, h_paperfontsize);
1561 delete_opt(opts, known_fontsizes);
1562 // delete "pt" at the end
1563 string::size_type i = h_paperfontsize.find("pt");
1564 if (i != string::npos)
1565 h_paperfontsize.erase(i);
1566 // The documentclass options are always parsed before the options
1567 // of the babel call so that a language cannot overwrite the babel
1569 handle_opt(opts, known_languages, h_language);
1570 delete_opt(opts, known_languages);
1572 // paper orientation
1573 if ((it = find(opts.begin(), opts.end(), "landscape")) != opts.end()) {
1574 h_paperorientation = "landscape";
1578 if ((it = find(opts.begin(), opts.end(), "oneside"))
1583 if ((it = find(opts.begin(), opts.end(), "twoside"))
1589 if ((it = find(opts.begin(), opts.end(), "onecolumn"))
1591 h_papercolumns = "1";
1594 if ((it = find(opts.begin(), opts.end(), "twocolumn"))
1596 h_papercolumns = "2";
1600 // some size options are known to any document classes, other sizes
1601 // are handled by the \geometry command of the geometry package
1602 handle_opt(opts, known_class_paper_sizes, h_papersize);
1603 delete_opt(opts, known_class_paper_sizes);
1604 // the remaining options
1605 h_options = join(opts, ",");
1606 // FIXME This does not work for classes that have a
1607 // different name in LyX than in LaTeX
1608 h_textclass = p.getArg('{', '}');
1612 else if (t.cs() == "usepackage") {
1613 string const options = p.getArg('[', ']');
1614 string const name = p.getArg('{', '}');
1615 vector<string> vecnames;
1616 split(name, vecnames, ',');
1617 vector<string>::const_iterator it = vecnames.begin();
1618 vector<string>::const_iterator end = vecnames.end();
1619 for (; it != end; ++it)
1620 handle_package(p, trimSpaceAndEol(*it), options,
1624 else if (t.cs() == "inputencoding") {
1625 string const encoding = p.getArg('{','}');
1626 h_inputencoding = encoding;
1627 p.setEncoding(encoding, Encoding::inputenc);
1630 else if (t.cs() == "newenvironment") {
1631 string const name = p.getArg('{', '}');
1632 string const opt1 = p.getFullOpt();
1633 string const opt2 = p.getFullOpt();
1634 string const beg = p.verbatim_item();
1635 string const end = p.verbatim_item();
1636 if (!in_lyx_preamble) {
1637 h_preamble << "\\newenvironment{" << name
1638 << '}' << opt1 << opt2 << '{'
1639 << beg << "}{" << end << '}';
1641 add_known_environment(name, opt1, !opt2.empty(),
1642 from_utf8(beg), from_utf8(end));
1646 else if (t.cs() == "newtheorem") {
1647 string const name = p.getArg('{', '}');
1648 string const opt1 = p.getFullOpt();
1649 string const opt2 = p.getFullOpt();
1650 string const body = p.verbatim_item();
1651 string const opt3 = p.getFullOpt();
1653 add_known_theorem(name, opt1, !opt2.empty(),
1654 from_utf8("\\newtheorem{" + name + '}' +
1655 opt1 + opt2 + '{' + body + '}' + opt3));
1657 if (!in_lyx_preamble)
1658 h_preamble << "\\newtheorem{" << name << '}'
1659 << opt1 << opt2 << '{' << '}' << opt3;
1662 else if (t.cs() == "def") {
1663 string name = p.get_token().cs();
1664 // In fact, name may be more than the name:
1665 // In the test case of bug 8116
1666 // name == "csname SF@gobble@opt \endcsname".
1667 // Therefore, we need to use asInput() instead of cs().
1668 while (p.next_token().cat() != catBegin)
1669 name += p.get_token().asInput();
1670 if (!in_lyx_preamble)
1671 h_preamble << "\\def\\" << name << '{'
1672 << p.verbatim_item() << "}";
1675 else if (t.cs() == "newcolumntype") {
1676 string const name = p.getArg('{', '}');
1677 trimSpaceAndEol(name);
1679 string opts = p.getOpt();
1680 if (!opts.empty()) {
1681 istringstream is(string(opts, 1));
1684 special_columns[name[0]] = nargs;
1685 h_preamble << "\\newcolumntype{" << name << "}";
1687 h_preamble << "[" << nargs << "]";
1688 h_preamble << "{" << p.verbatim_item() << "}";
1691 else if (t.cs() == "setcounter") {
1692 string const name = p.getArg('{', '}');
1693 string const content = p.getArg('{', '}');
1694 if (name == "secnumdepth")
1695 h_secnumdepth = content;
1696 else if (name == "tocdepth")
1697 h_tocdepth = content;
1699 h_preamble << "\\setcounter{" << name << "}{" << content << "}";
1702 else if (t.cs() == "setlength") {
1703 string const name = p.verbatim_item();
1704 string const content = p.verbatim_item();
1705 // the paragraphs are only not indented when \parindent is set to zero
1706 if (name == "\\parindent" && content != "") {
1707 if (content[0] == '0')
1708 h_paragraph_separation = "skip";
1710 h_paragraph_indentation = translate_len(content);
1711 } else if (name == "\\parskip") {
1712 if (content == "\\smallskipamount")
1713 h_defskip = "smallskip";
1714 else if (content == "\\medskipamount")
1715 h_defskip = "medskip";
1716 else if (content == "\\bigskipamount")
1717 h_defskip = "bigskip";
1719 h_defskip = content;
1721 h_preamble << "\\setlength{" << name << "}{" << content << "}";
1724 else if (t.cs() == "onehalfspacing")
1725 h_spacing = "onehalf";
1727 else if (t.cs() == "doublespacing")
1728 h_spacing = "double";
1730 else if (t.cs() == "setstretch")
1731 h_spacing = "other " + p.verbatim_item();
1733 else if (t.cs() == "synctex") {
1734 // the scheme is \synctex=value
1735 // where value can only be "1" or "-1"
1736 h_output_sync = "1";
1737 // there can be any character behind the value (e.g. a linebreak or a '\'
1738 // therefore we extract it char by char
1740 string value = p.get_token().asInput();
1742 value += p.get_token().asInput();
1743 h_output_sync_macro = "\\synctex=" + value;
1746 else if (t.cs() == "begin") {
1747 string const name = p.getArg('{', '}');
1748 if (name == "document")
1750 h_preamble << "\\begin{" << name << "}";
1753 else if (t.cs() == "geometry") {
1754 vector<string> opts = split_options(p.getArg('{', '}'));
1755 handle_geometry(opts);
1758 else if (t.cs() == "definecolor") {
1759 string const color = p.getArg('{', '}');
1760 string const space = p.getArg('{', '}');
1761 string const value = p.getArg('{', '}');
1762 if (color == "document_fontcolor" && space == "rgb") {
1763 RGBColor c(RGBColorFromLaTeX(value));
1764 h_fontcolor = X11hexname(c);
1765 } else if (color == "note_fontcolor" && space == "rgb") {
1766 RGBColor c(RGBColorFromLaTeX(value));
1767 h_notefontcolor = X11hexname(c);
1768 } else if (color == "page_backgroundcolor" && space == "rgb") {
1769 RGBColor c(RGBColorFromLaTeX(value));
1770 h_backgroundcolor = X11hexname(c);
1771 } else if (color == "shadecolor" && space == "rgb") {
1772 RGBColor c(RGBColorFromLaTeX(value));
1773 h_boxbgcolor = X11hexname(c);
1775 h_preamble << "\\definecolor{" << color
1776 << "}{" << space << "}{" << value
1781 else if (t.cs() == "bibliographystyle")
1782 h_biblio_style = p.verbatim_item();
1784 else if (t.cs() == "jurabibsetup") {
1785 // FIXME p.getArg('{', '}') is most probably wrong (it
1786 // does not handle nested braces).
1787 // Use p.verbatim_item() instead.
1788 vector<string> jurabibsetup =
1789 split_options(p.getArg('{', '}'));
1790 // add jurabibsetup to the jurabib package options
1791 add_package("jurabib", jurabibsetup);
1792 if (!jurabibsetup.empty()) {
1793 h_preamble << "\\jurabibsetup{"
1794 << join(jurabibsetup, ",") << '}';
1798 else if (t.cs() == "hypersetup") {
1799 vector<string> hypersetup =
1800 split_options(p.verbatim_item());
1801 // add hypersetup to the hyperref package options
1802 handle_hyperref(hypersetup);
1803 if (!hypersetup.empty()) {
1804 h_preamble << "\\hypersetup{"
1805 << join(hypersetup, ",") << '}';
1809 else if (is_known(t.cs(), known_if_3arg_commands)) {
1810 // prevent misparsing of \usepackage if it is used
1811 // as an argument (see e.g. our own output of
1812 // \@ifundefined above)
1813 string const arg1 = p.verbatim_item();
1814 string const arg2 = p.verbatim_item();
1815 string const arg3 = p.verbatim_item();
1816 // test case \@ifundefined{date}{}{\date{}}
1817 if (t.cs() == "@ifundefined" && arg1 == "date" &&
1818 arg2.empty() && arg3 == "\\date{}") {
1819 h_suppress_date = "true";
1820 // older tex2lyx versions did output
1821 // \@ifundefined{definecolor}{\usepackage{color}}{}
1822 } else if (t.cs() == "@ifundefined" &&
1823 arg1 == "definecolor" &&
1824 arg2 == "\\usepackage{color}" &&
1826 if (!in_lyx_preamble)
1827 h_preamble << package_beg_sep
1830 << "\\@ifundefined{definecolor}{color}{}"
1833 //\@ifundefined{showcaptionsetup}{}{%
1834 // \PassOptionsToPackage{caption=false}{subfig}}
1835 // that LyX uses for subfloats
1836 } else if (t.cs() == "@ifundefined" &&
1837 arg1 == "showcaptionsetup" && arg2.empty()
1838 && arg3 == "%\n \\PassOptionsToPackage{caption=false}{subfig}") {
1840 } else if (!in_lyx_preamble) {
1841 h_preamble << t.asInput()
1842 << '{' << arg1 << '}'
1843 << '{' << arg2 << '}'
1844 << '{' << arg3 << '}';
1848 else if (is_known(t.cs(), known_if_commands)) {
1849 // must not parse anything in conditional code, since
1850 // LyX would output the parsed contents unconditionally
1851 if (!in_lyx_preamble)
1852 h_preamble << t.asInput();
1853 handle_if(p, in_lyx_preamble);
1856 else if (!t.cs().empty() && !in_lyx_preamble)
1857 h_preamble << '\\' << t.cs();
1860 // remove the whitespace
1863 // Force textclass if the user wanted it
1864 if (!forceclass.empty())
1865 h_textclass = forceclass;
1866 tc.setName(h_textclass);
1868 cerr << "Error: Could not read layout file for textclass \"" << h_textclass << "\"." << endl;
1871 if (h_papersides.empty()) {
1874 h_papersides = ss.str();
1877 // If the CJK package is used we cannot set the document language from
1878 // the babel options. Instead, we guess which language is used most
1879 // and set this one.
1880 default_language = h_language;
1881 if (is_full_document &&
1882 (auto_packages.find("CJK") != auto_packages.end() ||
1883 auto_packages.find("CJKutf8") != auto_packages.end())) {
1885 h_language = guessLanguage(p, default_language);
1891 string babel2lyx(string const & language)
1893 char const * const * where = is_known(language, known_languages);
1895 return known_coded_languages[where - known_languages];
1900 string Preamble::polyglossia2lyx(string const & language)
1902 char const * const * where = is_known(language, polyglossia_languages);
1904 return coded_polyglossia_languages[where - polyglossia_languages];
1909 string rgbcolor2code(string const & name)
1911 char const * const * where = is_known(name, known_basic_colors);
1913 // "red", "green" etc
1914 return known_basic_color_codes[where - known_basic_colors];
1916 // "255,0,0", "0,255,0" etc
1917 RGBColor c(RGBColorFromLaTeX(name));
1918 return X11hexname(c);