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.
18 #include "LayoutFile.h"
21 #include "TextClass.h"
23 #include "support/convert.h"
24 #include "support/FileName.h"
25 #include "support/filetools.h"
26 #include "support/lstrings.h"
28 #include "support/regex.h"
38 using namespace lyx::support;
43 // special columntypes
44 extern map<char, int> special_columns;
46 map<string, vector<string> > used_packages;
47 const char * const modules_placeholder = "\001modules\001";
49 // needed to handle encodings with babel
50 bool one_language = true;
51 string h_inputencoding = "auto";
55 //add this to known_languages when updating to lyxformat 266:
57 //add these to known_languages when updating to lyxformat 268:
58 //"chinese-simplified", "chinese-traditional", "japanese", "korean"
59 // Both changes require first that support for non-babel languages (CJK,
61 // add turkmen for lyxformat 383
63 * known babel language names (including synonyms)
64 * not in standard babel: arabic, arabtex, belarusian, serbian-latin, thai
65 * not yet supported by LyX: kurmanji
66 * please keep this in sync with known_coded_languages line by line!
68 const char * const known_languages[] = {"acadian", "afrikaans", "albanian",
69 "american", "arabic", "arabtex", "austrian", "bahasa", "bahasai", "bahasam",
70 "basque", "belarusian", "brazil", "brazilian", "breton", "british", "bulgarian",
71 "canadian", "canadien", "catalan", "croatian", "czech", "danish", "dutch",
72 "english", "esperanto", "estonian", "farsi", "finnish", "francais", "french",
73 "frenchb", "frenchle", "frenchpro", "galician", "german", "germanb", "greek",
74 "hebrew", "hungarian", "icelandic", "indon", "indonesian", "interlingua",
75 "irish", "italian", "kazakh", "latin", "latvian", "lithuanian", "lowersorbian",
76 "lsorbian", "magyar", "malay", "meyalu", "mongolian", "naustrian", "newzealand",
77 "ngerman", "ngermanb", "norsk", "nynorsk", "polutonikogreek", "polish",
78 "portuges", "portuguese", "romanian", "russian", "russianb", "samin",
79 "scottish", "serbian", "serbian-latin", "slovak", "slovene", "spanish",
80 "swedish", "thai", "turkish", "ukraineb", "ukrainian", "uppersorbian",
81 "UKenglish", "USenglish", "usorbian", "vietnam", "welsh", 0};
84 * the same as known_languages with .lyx names
85 * please keep this in sync with known_languages line by line!
87 const char * const known_coded_languages[] = {"french", "afrikaans", "albanian",
88 "american", "arabic_arabi", "arabic_arabtex", "austrian", "bahasa", "bahasa", "bahasam",
89 "basque", "belarusian", "brazilian", "brazilian", "breton", "british", "bulgarian",
90 "canadian", "canadien", "catalan", "croatian", "czech", "danish", "dutch",
91 "english", "esperanto", "estonian", "farsi", "finnish", "french", "french",
92 "french", "french", "french", "galician", "german", "german", "greek",
93 "hebrew", "magyar", "icelandic", "bahasa", "bahasa", "interlingua",
94 "irish", "italian", "kazakh", "latin", "latvian", "lithuanian", "lowersorbian",
95 "lowersorbian", "magyar", "bahasam", "bahasam", "mongolian", "naustrian", "english",
96 "ngerman", "ngerman", "norsk", "nynorsk", "polutonikogreek", "polish",
97 "portuguese", "portuguese", "romanian", "russian", "russian", "samin",
98 "scottish", "serbian", "serbian-latin", "slovak", "slovene", "spanish",
99 "swedish", "thai", "turkish", "ukrainian", "ukrainian", "uppersorbian",
100 "uppersorbian", "english", "english", "vietnamese", "welsh", 0};
102 /// languages with english quotes (.lyx names)
103 const char * const known_english_quotes_languages[] = {"american", "bahasa",
104 "bahasam", "brazilian", "canadian", "chinese-simplified", "english",
105 "esperanto", "hebrew", "irish", "korean", "portuguese", "scottish", "thai", 0};
107 //add this to known_french_quotes_languages when updating to
108 //lyxformat 383: "turkmen"
109 /// languages with french quotes (.lyx names)
110 const char * const known_french_quotes_languages[] = {"albanian",
111 "arabic_arabi", "arabic_arabtex", "basque", "canadien", "catalan", "french",
112 "galician", "greek", "italian", "norsk", "nynorsk", "polutonikogreek",
113 "russian", "spanish", "spanish-mexico", "turkish", "ukrainian", "vietnamese", 0};
115 /// languages with german quotes (.lyx names)
116 const char * const known_german_quotes_languages[] = {"austrian", "bulgarian",
117 "czech", "german", "icelandic", "lithuanian", "lowersorbian", "naustrian",
118 "ngerman", "serbian", "serbian-latin", "slovak", "slovene", "uppersorbian", 0};
120 /// languages with polish quotes (.lyx names)
121 const char * const known_polish_quotes_languages[] = {"afrikaans", "croatian",
122 "dutch", "estonian", "magyar", "polish", "romanian", 0};
124 /// languages with swedish quotes (.lyx names)
125 const char * const known_swedish_quotes_languages[] = {"finnish",
128 char const * const known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
130 const char * const known_roman_fonts[] = { "ae", "bookman", "charter",
131 "cmr", "fourier", "lmodern", "mathpazo", "mathptmx", "newcent", 0};
133 const char * const known_sans_fonts[] = { "avant", "berasans", "cmbr", "cmss",
134 "helvet", "lmss", 0};
136 const char * const known_typewriter_fonts[] = { "beramono", "cmtl", "cmtt",
137 "courier", "lmtt", "luximono", "fourier", "lmodern", "mathpazo", "mathptmx",
140 const char * const known_paper_sizes[] = { "a3paper", "b3paper", "a4paper",
141 "b4paper", "a5paper", "b5paper", "executivepaper", "legalpaper",
144 const char * const known_class_paper_sizes[] = { "a4paper", "a5paper",
145 "executivepaper", "legalpaper", "letterpaper", 0};
147 const char * const known_paper_margins[] = { "lmargin", "tmargin", "rmargin",
148 "bmargin", "headheight", "headsep", "footskip", "columnsep", 0};
150 const char * const known_coded_paper_margins[] = { "leftmargin", "topmargin",
151 "rightmargin", "bottommargin", "headheight", "headsep", "footskip",
154 /// commands that can start an \if...\else...\endif sequence
155 const char * const known_if_commands[] = {"if", "ifarydshln", "ifbraket",
156 "ifcancel", "ifcolortbl", "ifeurosym", "ifmarginnote", "ifmmode", "ifpdf",
157 "ifsidecap", "ifupgreek", 0};
159 /// conditional commands with three arguments like \@ifundefined{}{}{}
160 const char * const known_if_3arg_commands[] = {"@ifundefined", "IfFileExists",
164 ostringstream h_preamble;
165 string h_textclass = "article";
166 string h_use_default_options = "false";
168 string h_language = "english";
169 string h_font_roman = "default";
170 string h_font_sans = "default";
171 string h_font_typewriter = "default";
172 string h_font_default_family = "default";
173 string h_font_sc = "false";
174 string h_font_osf = "false";
175 string h_font_sf_scale = "100";
176 string h_font_tt_scale = "100";
177 string h_graphics = "default";
178 string h_float_placement;
179 string h_paperfontsize = "default";
180 string h_spacing = "single";
181 string h_use_hyperref = "0";
184 string h_pdf_subject;
185 string h_pdf_keywords;
186 string h_pdf_bookmarks = "1";
187 string h_pdf_bookmarksnumbered = "0";
188 string h_pdf_bookmarksopen = "0";
189 string h_pdf_bookmarksopenlevel = "1";
190 string h_pdf_breaklinks = "0";
191 string h_pdf_pdfborder = "0";
192 string h_pdf_colorlinks = "0";
193 string h_pdf_backref = "section";
194 string h_pdf_pdfusetitle = "1";
195 string h_pdf_pagemode;
196 string h_pdf_quoted_options;
197 string h_papersize = "default";
198 string h_use_geometry = "false";
199 string h_use_amsmath = "1";
200 string h_use_esint = "1";
201 string h_use_mhchem = "0";
202 string h_cite_engine = "basic";
203 string h_use_bibtopic = "false";
204 string h_paperorientation = "portrait";
205 string h_notefontcolor;
206 string h_secnumdepth = "3";
207 string h_tocdepth = "3";
208 string h_paragraph_separation = "indent";
209 string h_defskip = "medskip";
210 string h_paragraph_indentation = "default";
211 string h_quotes_language = "english";
212 string h_papercolumns = "1";
214 string h_paperpagestyle = "default";
215 string h_listings_params;
216 string h_tracking_changes = "false";
217 string h_output_changes = "false";
221 // returns true if at least one of the options in what has been found
222 bool handle_opt(vector<string> & opts, char const * const * what, string & target)
228 // the last language option is the document language (for babel and LyX)
229 // the last size option is the document font size
230 vector<string>::iterator it;
231 vector<string>::iterator position = opts.begin();
232 for (; *what; ++what) {
233 it = find(opts.begin(), opts.end(), *what);
234 if (it != opts.end()) {
235 if (it >= position) {
246 void delete_opt(vector<string> & opts, char const * const * what)
251 // remove found options from the list
252 // do this after handle_opt to avoid potential memory leaks
253 vector<string>::iterator it;
254 for (; *what; ++what) {
255 it = find(opts.begin(), opts.end(), *what);
256 if (it != opts.end())
263 * Split a package options string (keyval format) into a vector.
265 * authorformat=smallcaps,
267 * titleformat=colonsep,
268 * bibformat={tabular,ibidem,numbered}
270 vector<string> split_options(string const & input)
272 vector<string> options;
276 Token const & t = p.get_token();
277 if (t.asInput() == ",") {
278 options.push_back(trimSpaceAndEol(option));
280 } else if (t.asInput() == "=") {
283 if (p.next_token().asInput() == "{")
284 option += '{' + p.getArg('{', '}') + '}';
285 } else if (t.cat() != catSpace)
286 option += t.asInput();
290 options.push_back(trimSpaceAndEol(option));
297 * Retrieve a keyval option "name={value with=sign}" named \p name from
298 * \p options and return the value.
299 * The found option is also removed from \p options.
301 string process_keyval_opt(vector<string> & options, string name)
303 for (size_t i = 0; i < options.size(); ++i) {
304 vector<string> option;
305 split(options[i], option, '=');
306 if (option.size() < 2)
308 if (option[0] == name) {
309 options.erase(options.begin() + i);
310 option.erase(option.begin());
311 return join(option, "=");
319 * Add package \p name with options \p options to used_packages.
320 * Remove options from \p options that we don't want to output.
322 void add_package(string const & name, vector<string> & options)
324 // every package inherits the global options
325 if (used_packages.find(name) == used_packages.end())
326 used_packages[name] = split_options(h_options);
328 vector<string> & v = used_packages[name];
329 v.insert(v.end(), options.begin(), options.end());
330 if (name == "jurabib") {
331 // Don't output the order argument (see the cite command
332 // handling code in text.cpp).
333 vector<string>::iterator end =
334 remove(options.begin(), options.end(), "natbiborder");
335 end = remove(options.begin(), end, "jurabiborder");
336 options.erase(end, options.end());
341 // Given is a string like "scaled=0.9", return 0.9 * 100
342 string const scale_as_percentage(string const & scale)
344 string::size_type pos = scale.find('=');
345 if (pos != string::npos) {
346 string value = scale.substr(pos + 1);
348 return convert<string>(100 * convert<double>(value));
350 // If the input string didn't match our expectations.
351 // return the default value "100"
356 string remove_braces(string const & value)
360 if (value[0] == '{' && value[value.length()-1] == '}')
361 return value.substr(1, value.length()-2);
366 void handle_hyperref(vector<string> & options)
368 // FIXME swallow inputencoding changes that might surround the
369 // hyperref setup if it was written by LyX
370 h_use_hyperref = "1";
371 // swallow "unicode=true", since LyX does always write that
372 vector<string>::iterator it =
373 find(options.begin(), options.end(), "unicode=true");
374 if (it != options.end())
376 it = find(options.begin(), options.end(), "pdfusetitle");
377 if (it != options.end()) {
378 h_pdf_pdfusetitle = "1";
381 string bookmarks = process_keyval_opt(options, "bookmarks");
382 if (bookmarks == "true")
383 h_pdf_bookmarks = "1";
384 else if (bookmarks == "false")
385 h_pdf_bookmarks = "0";
386 if (h_pdf_bookmarks == "1") {
387 string bookmarksnumbered =
388 process_keyval_opt(options, "bookmarksnumbered");
389 if (bookmarksnumbered == "true")
390 h_pdf_bookmarksnumbered = "1";
391 else if (bookmarksnumbered == "false")
392 h_pdf_bookmarksnumbered = "0";
393 string bookmarksopen =
394 process_keyval_opt(options, "bookmarksopen");
395 if (bookmarksopen == "true")
396 h_pdf_bookmarksopen = "1";
397 else if (bookmarksopen == "false")
398 h_pdf_bookmarksopen = "0";
399 if (h_pdf_bookmarksopen == "1") {
400 string bookmarksopenlevel =
401 process_keyval_opt(options, "bookmarksopenlevel");
402 if (!bookmarksopenlevel.empty())
403 h_pdf_bookmarksopenlevel = bookmarksopenlevel;
406 string breaklinks = process_keyval_opt(options, "breaklinks");
407 if (breaklinks == "true")
408 h_pdf_breaklinks = "1";
409 else if (breaklinks == "false")
410 h_pdf_breaklinks = "0";
411 string pdfborder = process_keyval_opt(options, "pdfborder");
412 if (pdfborder == "{0 0 0}")
413 h_pdf_pdfborder = "1";
414 else if (pdfborder == "{0 0 1}")
415 h_pdf_pdfborder = "0";
416 string backref = process_keyval_opt(options, "backref");
417 if (!backref.empty())
418 h_pdf_backref = backref;
419 string colorlinks = process_keyval_opt(options, "colorlinks");
420 if (colorlinks == "true")
421 h_pdf_colorlinks = "1";
422 else if (colorlinks == "false")
423 h_pdf_colorlinks = "0";
424 string pdfpagemode = process_keyval_opt(options, "pdfpagemode");
425 if (!pdfpagemode.empty())
426 h_pdf_pagemode = pdfpagemode;
427 string pdftitle = process_keyval_opt(options, "pdftitle");
428 if (!pdftitle.empty()) {
429 h_pdf_title = remove_braces(pdftitle);
431 string pdfauthor = process_keyval_opt(options, "pdfauthor");
432 if (!pdfauthor.empty()) {
433 h_pdf_author = remove_braces(pdfauthor);
435 string pdfsubject = process_keyval_opt(options, "pdfsubject");
436 if (!pdfsubject.empty())
437 h_pdf_subject = remove_braces(pdfsubject);
438 string pdfkeywords = process_keyval_opt(options, "pdfkeywords");
439 if (!pdfkeywords.empty())
440 h_pdf_keywords = remove_braces(pdfkeywords);
441 if (!options.empty()) {
442 if (!h_pdf_quoted_options.empty())
443 h_pdf_quoted_options += ',';
444 h_pdf_quoted_options += join(options, ",");
450 void handle_package(Parser &p, string const & name, string const & opts,
451 bool in_lyx_preamble)
453 vector<string> options = split_options(opts);
454 add_package(name, options);
458 if (is_known(name, known_roman_fonts)) {
463 if (name == "fourier") {
464 h_font_roman = "utopia";
465 // when font uses real small capitals
466 if (opts == "expert")
470 if (name == "mathpazo")
471 h_font_roman = "palatino";
473 if (name == "mathptmx")
474 h_font_roman = "times";
477 if (is_known(name, known_sans_fonts)) {
481 h_font_sf_scale = scale_as_percentage(scale);
486 if (is_known(name, known_typewriter_fonts)) {
487 h_font_typewriter = name;
490 h_font_tt_scale = scale_as_percentage(scale);
494 // font uses old-style figure
498 else if (name == "amsmath" || name == "amssymb")
501 else if (name == "esint")
504 else if (name == "mhchem")
507 else if (name == "babel" && !opts.empty()) {
508 // check if more than one option was used - used later for inputenc
509 // in case inputenc is parsed before babel, set the encoding to auto
510 if (options.begin() != options.end() - 1) {
511 one_language = false;
512 h_inputencoding = "auto";
514 // babel takes the last language of the option of its \usepackage
515 // call as document language. If there is no such language option, the
516 // last language in the documentclass options is used.
517 handle_opt(options, known_languages, h_language);
518 delete_opt(options, known_languages);
521 else if (name == "fontenc")
524 else if (name == "inputenc" || name == "luainputenc") {
525 // h_inputencoding is only set when there is not more than one
526 // inputenc option because otherwise h_inputencoding must be
527 // set to "auto" (the default encoding of the document language)
528 // Therefore check for the "," character.
529 // It is also only set when there is not more then one babel
530 // language option but this is handled in the routine for babel.
531 if (opts.find(",") == string::npos && one_language == true)
532 h_inputencoding = opts;
533 if (!options.empty())
534 p.setEncoding(options.back());
538 else if (name == "makeidx")
541 else if (name == "prettyref")
544 else if (name == "varioref")
547 else if (name == "verbatim")
550 else if (name == "nomencl")
553 else if (name == "textcomp")
556 else if (name == "url")
559 else if (LYX_FORMAT >= 408 && name == "subscript")
562 else if (name == "color") {
563 // with the following command this package is only loaded when needed for
564 // undefined colors, since we only support the predefined colors
565 h_preamble << "\\@ifundefined{definecolor}\n {\\usepackage{color}}{}\n";
568 else if (name == "graphicx")
571 else if (name == "setspace")
574 else if (name == "geometry")
575 ; // Ignore this, the geometry settings are made by the \geometry
576 // command. This command is handled below.
578 else if (is_known(name, known_languages))
581 else if (name == "natbib") {
582 h_cite_engine = "natbib_authoryear";
583 vector<string>::iterator it =
584 find(options.begin(), options.end(), "authoryear");
585 if (it != options.end())
588 it = find(options.begin(), options.end(), "numbers");
589 if (it != options.end()) {
590 h_cite_engine = "natbib_numerical";
596 else if (name == "jurabib")
597 h_cite_engine = "jurabib";
599 else if (name == "hyperref")
600 handle_hyperref(options);
602 else if (!in_lyx_preamble) {
604 h_preamble << "\\usepackage{" << name << "}";
606 h_preamble << "\\usepackage[" << opts << "]{"
612 // We need to do something with the options...
613 if (!options.empty())
614 cerr << "Ignoring options '" << join(options, ",")
615 << "' of package " << name << '.' << endl;
617 // remove the whitespace
622 void handle_if(Parser & p, bool in_lyx_preamble)
625 Token t = p.get_token();
626 if (t.cat() == catEscape &&
627 is_known(t.cs(), known_if_commands))
628 handle_if(p, in_lyx_preamble);
630 if (!in_lyx_preamble)
631 h_preamble << t.asInput();
632 if (t.cat() == catEscape && t.cs() == "fi")
639 void end_preamble(ostream & os, TextClass const & /*textclass*/)
641 // translate from babel to LyX names
642 h_language = babel2lyx(h_language);
644 // set the quote language
645 // LyX only knows the following quotes languages:
646 // english, swedish, german, polish, french and danish
647 // (quotes for "japanese" and "chinese-traditional" are missing because
648 // they wouldn't be useful: http://www.lyx.org/trac/ticket/6383)
649 // conversion list taken from
650 // http://en.wikipedia.org/wiki/Quotation_mark,_non-English_usage
651 // (quotes for kazakh and interlingua are unknown)
653 if (h_language == "danish")
654 h_quotes_language = "danish";
656 else if (is_known(h_language, known_french_quotes_languages))
657 h_quotes_language = "french";
659 else if (is_known(h_language, known_german_quotes_languages))
660 h_quotes_language = "german";
662 else if (is_known(h_language, known_polish_quotes_languages))
663 h_quotes_language = "polish";
665 else if (is_known(h_language, known_swedish_quotes_languages))
666 h_quotes_language = "swedish";
668 else if (is_known(h_language, known_english_quotes_languages))
669 h_quotes_language = "english";
671 // output the LyX file settings
672 os << "#LyX file created by tex2lyx " << PACKAGE_VERSION << "\n"
673 << "\\lyxformat " << LYX_FORMAT << '\n'
674 << "\\begin_document\n"
675 << "\\begin_header\n"
676 << "\\textclass " << h_textclass << "\n";
677 if (!h_preamble.str().empty())
678 os << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n";
679 if (!h_options.empty())
680 os << "\\options " << h_options << "\n";
681 os << "\\use_default_options " << h_use_default_options << "\n"
682 << modules_placeholder
683 << "\\language " << h_language << "\n"
684 << "\\inputencoding " << h_inputencoding << "\n"
685 << "\\font_roman " << h_font_roman << "\n"
686 << "\\font_sans " << h_font_sans << "\n"
687 << "\\font_typewriter " << h_font_typewriter << "\n"
688 << "\\font_default_family " << h_font_default_family << "\n"
689 << "\\font_sc " << h_font_sc << "\n"
690 << "\\font_osf " << h_font_osf << "\n"
691 << "\\font_sf_scale " << h_font_sf_scale << "\n"
692 << "\\font_tt_scale " << h_font_tt_scale << "\n"
693 << "\\graphics " << h_graphics << "\n";
694 if (!h_float_placement.empty())
695 os << "\\float_placement " << h_float_placement << "\n";
696 os << "\\paperfontsize " << h_paperfontsize << "\n"
697 << "\\spacing " << h_spacing << "\n"
698 << "\\use_hyperref " << h_use_hyperref << '\n';
699 if (h_use_hyperref == "1") {
700 if (!h_pdf_title.empty())
701 os << "\\pdf_title \"" << h_pdf_title << "\"\n";
702 if (!h_pdf_author.empty())
703 os << "\\pdf_author \"" << h_pdf_author << "\"\n";
704 if (!h_pdf_subject.empty())
705 os << "\\pdf_subject \"" << h_pdf_subject << "\"\n";
706 if (!h_pdf_keywords.empty())
707 os << "\\pdf_keywords \"" << h_pdf_keywords << "\"\n";
708 os << "\\pdf_bookmarks " << h_pdf_bookmarks << "\n"
709 "\\pdf_bookmarksnumbered " << h_pdf_bookmarksnumbered << "\n"
710 "\\pdf_bookmarksopen " << h_pdf_bookmarksopen << "\n"
711 "\\pdf_bookmarksopenlevel " << h_pdf_bookmarksopenlevel << "\n"
712 "\\pdf_breaklinks " << h_pdf_breaklinks << "\n"
713 "\\pdf_pdfborder " << h_pdf_pdfborder << "\n"
714 "\\pdf_colorlinks " << h_pdf_colorlinks << "\n"
715 "\\pdf_backref " << h_pdf_backref << "\n"
716 "\\pdf_pdfusetitle " << h_pdf_pdfusetitle << '\n';
717 if (!h_pdf_pagemode.empty())
718 os << "\\pdf_pagemode " << h_pdf_pagemode << '\n';
719 if (!h_pdf_quoted_options.empty())
720 os << "\\pdf_quoted_options \"" << h_pdf_quoted_options << "\"\n";
722 os << "\\papersize " << h_papersize << "\n"
723 << "\\use_geometry " << h_use_geometry << "\n"
724 << "\\use_amsmath " << h_use_amsmath << "\n"
725 << "\\use_esint " << h_use_esint << "\n"
726 << "\\use_mhchem " << h_use_mhchem << "\n"
727 << "\\cite_engine " << h_cite_engine << "\n"
728 << "\\use_bibtopic " << h_use_bibtopic << "\n"
729 << "\\paperorientation " << h_paperorientation << '\n';
730 if (LYX_FORMAT >= 382 && !h_notefontcolor.empty())
731 os << "\\notefontcolor " << h_notefontcolor << '\n';
733 << "\\secnumdepth " << h_secnumdepth << "\n"
734 << "\\tocdepth " << h_tocdepth << "\n"
735 << "\\paragraph_separation " << h_paragraph_separation << "\n";
736 if (LYX_FORMAT < 365 || h_paragraph_separation == "skip")
737 os << "\\defskip " << h_defskip << "\n";
739 os << "\\paragraph_indentation " << h_paragraph_indentation << "\n";
740 os << "\\quotes_language " << h_quotes_language << "\n"
741 << "\\papercolumns " << h_papercolumns << "\n"
742 << "\\papersides " << h_papersides << "\n"
743 << "\\paperpagestyle " << h_paperpagestyle << "\n";
744 if (!h_listings_params.empty())
745 os << "\\listings_params " << h_listings_params << "\n";
746 os << "\\tracking_changes " << h_tracking_changes << "\n"
747 << "\\output_changes " << h_output_changes << "\n"
748 << "\\end_header\n\n"
750 // clear preamble for subdocuments
754 } // anonymous namespace
757 void parse_preamble(Parser & p, ostream & os,
758 string const & forceclass, TeX2LyXDocClass & tc)
760 // initialize fixed types
761 special_columns['D'] = 3;
762 bool is_full_document = false;
763 bool is_lyx_file = false;
764 bool in_lyx_preamble = false;
766 // determine whether this is a full document or a fragment for inclusion
768 Token const & t = p.get_token();
770 if (t.cat() == catEscape && t.cs() == "documentclass") {
771 is_full_document = true;
777 while (is_full_document && p.good()) {
778 Token const & t = p.get_token();
781 cerr << "t: " << t << "\n";
787 if (!in_lyx_preamble &&
788 (t.cat() == catLetter ||
789 t.cat() == catSuper ||
791 t.cat() == catOther ||
792 t.cat() == catMath ||
793 t.cat() == catActive ||
794 t.cat() == catBegin ||
796 t.cat() == catAlign ||
797 t.cat() == catParameter))
798 h_preamble << t.cs();
800 else if (!in_lyx_preamble &&
801 (t.cat() == catSpace || t.cat() == catNewline))
802 h_preamble << t.asInput();
804 else if (t.cat() == catComment) {
805 static regex const islyxfile("%% LyX .* created this file");
806 static regex const usercommands("User specified LaTeX commands");
808 string const comment = t.asInput();
810 // magically switch encoding default if it looks like XeLaTeX
811 static string const magicXeLaTeX =
812 "% This document must be compiled with XeLaTeX ";
813 if (comment.size() > magicXeLaTeX.size()
814 && comment.substr(0, magicXeLaTeX.size()) == magicXeLaTeX
815 && h_inputencoding == "auto") {
816 cerr << "XeLaTeX comment found, switching to UTF8\n";
817 h_inputencoding = "utf8";
820 if (regex_search(comment, sub, islyxfile)) {
822 in_lyx_preamble = true;
823 } else if (is_lyx_file
824 && regex_search(comment, sub, usercommands))
825 in_lyx_preamble = false;
826 else if (!in_lyx_preamble)
827 h_preamble << t.asInput();
830 else if (t.cs() == "pagestyle")
831 h_paperpagestyle = p.verbatim_item();
833 else if (t.cs() == "makeatletter") {
834 // LyX takes care of this
835 p.setCatCode('@', catLetter);
838 else if (t.cs() == "makeatother") {
839 // LyX takes care of this
840 p.setCatCode('@', catOther);
843 else if (t.cs() == "newcommand" || t.cs() == "newcommandx"
844 || t.cs() == "renewcommand" || t.cs() == "renewcommandx"
845 || t.cs() == "providecommand" || t.cs() == "providecommandx"
846 || t.cs() == "DeclareRobustCommand"
847 || t.cs() == "DeclareRobustCommandx"
848 || t.cs() == "ProvideTextCommandDefault"
849 || t.cs() == "DeclareMathAccent") {
851 if (p.next_token().character() == '*') {
855 string const name = p.verbatim_item();
856 string const opt1 = p.getFullOpt();
857 string const opt2 = p.getFullOpt();
858 string const body = p.verbatim_item();
860 if (name == "\\rmdefault")
861 if (is_known(body, known_roman_fonts))
863 if (name == "\\sfdefault")
864 if (is_known(body, known_sans_fonts))
866 if (name == "\\ttdefault")
867 if (is_known(body, known_typewriter_fonts))
868 h_font_typewriter = body;
869 if (name == "\\familydefault") {
870 string family = body;
871 // remove leading "\"
872 h_font_default_family = family.erase(0,1);
875 // Add the command to the known commands
876 add_known_command(name, opt1, !opt2.empty(), from_utf8(body));
878 // only non-lyxspecific stuff
879 if (!in_lyx_preamble) {
881 ss << '\\' << t.cs();
884 ss << '{' << name << '}' << opt1 << opt2
885 << '{' << body << "}";
886 h_preamble << ss.str();
888 ostream & out = in_preamble ? h_preamble : os;
889 out << "\\" << t.cs() << "{" << name << "}"
890 << opts << "{" << body << "}";
895 else if (t.cs() == "documentclass") {
896 vector<string>::iterator it;
897 vector<string> opts = split_options(p.getArg('[', ']'));
898 handle_opt(opts, known_fontsizes, h_paperfontsize);
899 delete_opt(opts, known_fontsizes);
900 // delete "pt" at the end
901 string::size_type i = h_paperfontsize.find("pt");
902 if (i != string::npos)
903 h_paperfontsize.erase(i);
904 // The documentclass options are always parsed before the options
905 // of the babel call so that a language cannot overwrite the babel
907 handle_opt(opts, known_languages, h_language);
908 delete_opt(opts, known_languages);
911 if ((it = find(opts.begin(), opts.end(), "landscape")) != opts.end()) {
912 h_paperorientation = "landscape";
916 if ((it = find(opts.begin(), opts.end(), "oneside"))
921 if ((it = find(opts.begin(), opts.end(), "twoside"))
927 if ((it = find(opts.begin(), opts.end(), "onecolumn"))
929 h_papercolumns = "1";
932 if ((it = find(opts.begin(), opts.end(), "twocolumn"))
934 h_papercolumns = "2";
938 // some size options are know to any document classes, other sizes
939 // are handled by the \geometry command of the geometry package
940 handle_opt(opts, known_class_paper_sizes, h_papersize);
941 delete_opt(opts, known_class_paper_sizes);
942 // the remaining options
943 h_options = join(opts, ",");
944 // FIXME This does not work for classes that have a
945 // different name in LyX than in LaTeX
946 h_textclass = p.getArg('{', '}');
949 else if (t.cs() == "usepackage") {
950 string const options = p.getArg('[', ']');
951 string const name = p.getArg('{', '}');
952 vector<string> vecnames;
953 split(name, vecnames, ',');
954 vector<string>::const_iterator it = vecnames.begin();
955 vector<string>::const_iterator end = vecnames.end();
956 for (; it != end; ++it)
957 handle_package(p, trimSpaceAndEol(*it), options,
961 else if (t.cs() == "inputencoding") {
962 string const encoding = p.getArg('{','}');
963 h_inputencoding = encoding;
964 p.setEncoding(encoding);
967 else if (t.cs() == "newenvironment") {
968 string const name = p.getArg('{', '}');
969 string const opt1 = p.getFullOpt();
970 string const opt2 = p.getFullOpt();
971 string const beg = p.verbatim_item();
972 string const end = p.verbatim_item();
973 if (!in_lyx_preamble) {
974 h_preamble << "\\newenvironment{" << name
975 << '}' << opt1 << opt2 << '{'
976 << beg << "}{" << end << '}';
978 add_known_environment(name, opt1, !opt2.empty(),
979 from_utf8(beg), from_utf8(end));
983 else if (t.cs() == "def") {
984 string name = p.get_token().cs();
985 while (p.next_token().cat() != catBegin)
986 name += p.get_token().cs();
987 if (!in_lyx_preamble)
988 h_preamble << "\\def\\" << name << '{'
989 << p.verbatim_item() << "}";
992 else if (t.cs() == "newcolumntype") {
993 string const name = p.getArg('{', '}');
994 trimSpaceAndEol(name);
996 string opts = p.getOpt();
998 istringstream is(string(opts, 1));
1001 special_columns[name[0]] = nargs;
1002 h_preamble << "\\newcolumntype{" << name << "}";
1004 h_preamble << "[" << nargs << "]";
1005 h_preamble << "{" << p.verbatim_item() << "}";
1008 else if (t.cs() == "setcounter") {
1009 string const name = p.getArg('{', '}');
1010 string const content = p.getArg('{', '}');
1011 if (name == "secnumdepth")
1012 h_secnumdepth = content;
1013 else if (name == "tocdepth")
1014 h_tocdepth = content;
1016 h_preamble << "\\setcounter{" << name << "}{" << content << "}";
1019 else if (t.cs() == "setlength") {
1020 string const name = p.verbatim_item();
1021 string const content = p.verbatim_item();
1022 // the paragraphs are only not indented when \parindent is set to zero
1023 if (name == "\\parindent" && content != "") {
1024 if (content[0] == '0')
1025 h_paragraph_separation = "skip";
1026 else if (LYX_FORMAT >= 365)
1027 h_paragraph_indentation = translate_len(content);
1029 h_preamble << "\\setlength{" << name
1030 << "}{" << content << "}";
1031 } else if (name == "\\parskip") {
1032 if (content == "\\smallskipamount")
1033 h_defskip = "smallskip";
1034 else if (content == "\\medskipamount")
1035 h_defskip = "medskip";
1036 else if (content == "\\bigskipamount")
1037 h_defskip = "bigskip";
1039 h_defskip = content;
1041 h_preamble << "\\setlength{" << name << "}{" << content << "}";
1044 else if (t.cs() == "onehalfspacing")
1045 h_spacing = "onehalf";
1047 else if (t.cs() == "doublespacing")
1048 h_spacing = "double";
1050 else if (t.cs() == "setstretch")
1051 h_spacing = "other " + p.verbatim_item();
1053 else if (t.cs() == "begin") {
1054 string const name = p.getArg('{', '}');
1055 if (name == "document")
1057 h_preamble << "\\begin{" << name << "}";
1060 else if (t.cs() == "geometry") {
1061 h_use_geometry = "true";
1062 vector<string> opts = split_options(p.getArg('{', '}'));
1063 vector<string>::iterator it;
1064 // paper orientation
1065 if ((it = find(opts.begin(), opts.end(), "landscape")) != opts.end()) {
1066 h_paperorientation = "landscape";
1070 handle_opt(opts, known_paper_sizes, h_papersize);
1071 delete_opt(opts, known_paper_sizes);
1073 char const * const * margin = known_paper_margins;
1075 for (; *margin; ++margin) {
1077 // search for the "=" in e.g. "lmargin=2cm" to get the value
1078 for(size_t i = 0; i != opts.size(); i++) {
1079 if (opts.at(i).find(*margin) != string::npos) {
1080 string::size_type pos = opts.at(i).find("=");
1081 string value = opts.at(i).substr(pos + 1);
1082 string name = known_coded_paper_margins[k];
1083 h_margins += "\\" + name + " " + value + "\n";
1089 else if (t.cs() == "definecolor") {
1090 string const color = p.getArg('{', '}');
1091 string const space = p.getArg('{', '}');
1092 string const value = p.getArg('{', '}');
1093 if (LYX_FORMAT >= 382 &&
1094 color == "note_fontcolor" && space == "rgb") {
1095 RGBColor c(RGBColorFromLaTeX(value));
1096 h_notefontcolor = X11hexname(c);
1098 h_preamble << "\\definecolor{" << color
1099 << "}{" << space << "}{" << value
1104 else if (t.cs() == "jurabibsetup") {
1105 // FIXME p.getArg('{', '}') is most probably wrong (it
1106 // does not handle nested braces).
1107 // Use p.verbatim_item() instead.
1108 vector<string> jurabibsetup =
1109 split_options(p.getArg('{', '}'));
1110 // add jurabibsetup to the jurabib package options
1111 add_package("jurabib", jurabibsetup);
1112 if (!jurabibsetup.empty()) {
1113 h_preamble << "\\jurabibsetup{"
1114 << join(jurabibsetup, ",") << '}';
1118 else if (t.cs() == "hypersetup") {
1119 vector<string> hypersetup =
1120 split_options(p.verbatim_item());
1121 // add hypersetup to the hyperref package options
1122 handle_hyperref(hypersetup);
1123 if (!hypersetup.empty()) {
1124 h_preamble << "\\hypersetup{"
1125 << join(hypersetup, ",") << '}';
1129 else if (is_known(t.cs(), known_if_3arg_commands)) {
1130 // prevent misparsing of \usepackage if it is used
1131 // as an argument (see e.g. our own output of
1132 // \@ifundefined above)
1133 string const arg1 = p.verbatim_item();
1134 string const arg2 = p.verbatim_item();
1135 string const arg3 = p.verbatim_item();
1136 if (!in_lyx_preamble) {
1137 h_preamble << t.asInput()
1138 << '{' << arg1 << '}'
1139 << '{' << arg2 << '}'
1140 << '{' << arg3 << '}';
1144 else if (is_known(t.cs(), known_if_commands)) {
1145 // must not parse anything in conditional code, since
1146 // LyX would output the parsed contents unconditionally
1147 if (!in_lyx_preamble)
1148 h_preamble << t.asInput();
1149 handle_if(p, in_lyx_preamble);
1152 else if (!t.cs().empty() && !in_lyx_preamble)
1153 h_preamble << '\\' << t.cs();
1156 // remove the whitespace
1159 // Force textclass if the user wanted it
1160 if (!forceclass.empty())
1161 h_textclass = forceclass;
1162 if (noweb_mode && !prefixIs(h_textclass, "literate-"))
1163 h_textclass.insert(0, "literate-");
1164 tc.setName(h_textclass);
1166 cerr << "Error: Could not read layout file for textclass \"" << h_textclass << "\"." << endl;
1169 if (h_papersides.empty()) {
1172 h_papersides = ss.str();
1174 end_preamble(os, tc);
1178 /// translates a babel language name to a LyX language name
1179 string babel2lyx(string const & language)
1181 char const * const * where = is_known(language, known_languages);
1183 return known_coded_languages[where - known_languages];