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;
54 //add this to known_languages when updating to lyxformat 266:
56 //add these to known_languages when updating to lyxformat 268:
57 //"chinese-simplified", "chinese-traditional", "japanese", "korean"
58 // Both changes require first that support for non-babel languages (CJK,
60 // add turkmen for lyxformat 383
62 * known babel language names (including synonyms)
63 * not in standard babel: arabic, arabtex, belarusian, serbian-latin, thai
64 * not yet supported by LyX: kurmanji
65 * please keep this in sync with known_coded_languages line by line!
67 const char * const known_languages[] = {"acadian", "afrikaans", "albanian",
68 "american", "arabic", "arabtex", "austrian", "bahasa", "bahasai", "bahasam",
69 "basque", "belarusian", "brazil", "brazilian", "breton", "british", "bulgarian",
70 "canadian", "canadien", "catalan", "croatian", "czech", "danish", "dutch",
71 "english", "esperanto", "estonian", "farsi", "finnish", "francais", "french",
72 "frenchb", "frenchle", "frenchpro", "galician", "german", "germanb", "greek",
73 "hebrew", "hungarian", "icelandic", "indon", "indonesian", "interlingua",
74 "irish", "italian", "kazakh", "latin", "latvian", "lithuanian", "lowersorbian",
75 "lsorbian", "magyar", "malay", "meyalu", "mongolian", "naustrian", "newzealand",
76 "ngerman", "ngermanb", "norsk", "nynorsk", "polutonikogreek", "polish",
77 "portuges", "portuguese", "romanian", "russian", "russianb", "samin",
78 "scottish", "serbian", "serbian-latin", "slovak", "slovene", "spanish",
79 "swedish", "thai", "turkish", "ukraineb", "ukrainian", "uppersorbian",
80 "UKenglish", "USenglish", "usorbian", "vietnam", "welsh", 0};
83 * the same as known_languages with .lyx names
84 * please keep this in sync with known_languages line by line!
86 const char * const known_coded_languages[] = {"french", "afrikaans", "albanian",
87 "american", "arabic_arabi", "arabic_arabtex", "austrian", "bahasa", "bahasa", "bahasam",
88 "basque", "belarusian", "brazilian", "brazilian", "breton", "british", "bulgarian",
89 "canadian", "canadien", "catalan", "croatian", "czech", "danish", "dutch",
90 "english", "esperanto", "estonian", "farsi", "finnish", "french", "french",
91 "french", "french", "french", "galician", "german", "german", "greek",
92 "hebrew", "magyar", "icelandic", "bahasa", "bahasa", "interlingua",
93 "irish", "italian", "kazakh", "latin", "latvian", "lithuanian", "lowersorbian",
94 "lowersorbian", "magyar", "bahasam", "bahasam", "mongolian", "naustrian", "english",
95 "ngerman", "ngerman", "norsk", "nynorsk", "polutonikogreek", "polish",
96 "portuguese", "portuguese", "romanian", "russian", "russian", "samin",
97 "scottish", "serbian", "serbian-latin", "slovak", "slovene", "spanish",
98 "swedish", "thai", "turkish", "ukrainian", "ukrainian", "uppersorbian",
99 "uppersorbian", "english", "english", "vietnamese", "welsh", 0};
101 /// languages with english quotes (.lyx names)
102 const char * const known_english_quotes_languages[] = {"american", "bahasa",
103 "bahasam", "brazilian", "canadian", "chinese-simplified", "english",
104 "esperanto", "hebrew", "irish", "korean", "portuguese", "scottish", "thai", 0};
106 //add this to known_french_quotes_languages when updating to
107 //lyxformat 383: "turkmen"
108 /// languages with french quotes (.lyx names)
109 const char * const known_french_quotes_languages[] = {"albanian",
110 "arabic_arabi", "arabic_arabtex", "basque", "canadien", "catalan", "french",
111 "galician", "greek", "italian", "norsk", "nynorsk", "polutonikogreek",
112 "russian", "spanish", "spanish-mexico", "turkish", "ukrainian", "vietnamese", 0};
114 /// languages with german quotes (.lyx names)
115 const char * const known_german_quotes_languages[] = {"austrian", "bulgarian",
116 "czech", "german", "icelandic", "lithuanian", "lowersorbian", "naustrian",
117 "ngerman", "serbian", "serbian-latin", "slovak", "slovene", "uppersorbian", 0};
119 /// languages with polish quotes (.lyx names)
120 const char * const known_polish_quotes_languages[] = {"afrikaans", "croatian",
121 "dutch", "estonian", "magyar", "polish", "romanian", 0};
123 /// languages with swedish quotes (.lyx names)
124 const char * const known_swedish_quotes_languages[] = {"finnish",
127 char const * const known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
129 const char * const known_roman_fonts[] = { "ae", "bookman", "charter",
130 "cmr", "fourier", "lmodern", "mathpazo", "mathptmx", "newcent", 0};
132 const char * const known_sans_fonts[] = { "avant", "berasans", "cmbr", "cmss",
133 "helvet", "lmss", 0};
135 const char * const known_typewriter_fonts[] = { "beramono", "cmtl", "cmtt",
136 "courier", "lmtt", "luximono", "fourier", "lmodern", "mathpazo", "mathptmx",
139 const char * const known_paper_sizes[] = { "a3paper", "b3paper", "a4paper",
140 "b4paper", "a5paper", "b5paper", "executivepaper", "legalpaper",
143 const char * const known_class_paper_sizes[] = { "a4paper", "a5paper",
144 "executivepaper", "legalpaper", "letterpaper", 0};
146 const char * const known_paper_margins[] = { "lmargin", "tmargin", "rmargin",
147 "bmargin", "headheight", "headsep", "footskip", "columnsep", 0};
149 const char * const known_coded_paper_margins[] = { "leftmargin", "topmargin",
150 "rightmargin", "bottommargin", "headheight", "headsep", "footskip",
153 /// commands that can start an \if...\else...\endif sequence
154 const char * const known_if_commands[] = {"if", "ifarydshln", "ifbraket",
155 "ifcancel", "ifcolortbl", "ifeurosym", "ifmarginnote", "ifmmode", "ifpdf",
156 "ifsidecap", "ifupgreek", 0};
158 /// conditional commands with three arguments like \@ifundefined{}{}{}
159 const char * const known_if_3arg_commands[] = {"@ifundefined", "IfFileExists",
163 ostringstream h_preamble;
164 string h_textclass = "article";
165 string h_use_default_options = "false";
167 string h_language = "english";
168 string h_inputencoding = "auto";
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_cite_engine = "basic";
202 string h_use_bibtopic = "false";
203 string h_paperorientation = "portrait";
204 string h_notefontcolor;
205 string h_secnumdepth = "3";
206 string h_tocdepth = "3";
207 string h_paragraph_separation = "indent";
208 string h_defskip = "medskip";
209 string h_paragraph_indentation = "default";
210 string h_quotes_language = "english";
211 string h_papercolumns = "1";
213 string h_paperpagestyle = "default";
214 string h_listings_params;
215 string h_tracking_changes = "false";
216 string h_output_changes = "false";
220 // returns true if at least one of the options in what has been found
221 bool handle_opt(vector<string> & opts, char const * const * what, string & target)
227 // the last language option is the document language (for babel and LyX)
228 // the last size option is the document font size
229 vector<string>::iterator it;
230 vector<string>::iterator position = opts.begin();
231 for (; *what; ++what) {
232 it = find(opts.begin(), opts.end(), *what);
233 if (it != opts.end()) {
234 if (it >= position) {
245 void delete_opt(vector<string> & opts, char const * const * what)
250 // remove found options from the list
251 // do this after handle_opt to avoid potential memory leaks
252 vector<string>::iterator it;
253 for (; *what; ++what) {
254 it = find(opts.begin(), opts.end(), *what);
255 if (it != opts.end())
262 * Split a package options string (keyval format) into a vector.
264 * authorformat=smallcaps,
266 * titleformat=colonsep,
267 * bibformat={tabular,ibidem,numbered}
269 vector<string> split_options(string const & input)
271 vector<string> options;
275 Token const & t = p.get_token();
276 if (t.asInput() == ",") {
277 options.push_back(trim(option));
279 } else if (t.asInput() == "=") {
282 if (p.next_token().asInput() == "{")
283 option += '{' + p.getArg('{', '}') + '}';
284 } else if (t.cat() != catSpace)
285 option += t.asInput();
289 options.push_back(trim(option));
296 * Retrieve a keyval option "name={value with=sign}" named \p name from
297 * \p options and return the value.
298 * The found option is also removed from \p options.
300 string process_keyval_opt(vector<string> & options, string name)
302 for (size_t i = 0; i < options.size(); ++i) {
303 vector<string> option;
304 split(options[i], option, '=');
305 if (option.size() < 2)
307 if (option[0] == name) {
308 options.erase(options.begin() + i);
309 option.erase(option.begin());
310 return join(option, "=");
318 * Add package \p name with options \p options to used_packages.
319 * Remove options from \p options that we don't want to output.
321 void add_package(string const & name, vector<string> & options)
323 // every package inherits the global options
324 if (used_packages.find(name) == used_packages.end())
325 used_packages[name] = split_options(h_options);
327 vector<string> & v = used_packages[name];
328 v.insert(v.end(), options.begin(), options.end());
329 if (name == "jurabib") {
330 // Don't output the order argument (see the cite command
331 // handling code in text.cpp).
332 vector<string>::iterator end =
333 remove(options.begin(), options.end(), "natbiborder");
334 end = remove(options.begin(), end, "jurabiborder");
335 options.erase(end, options.end());
340 // Given is a string like "scaled=0.9", return 0.9 * 100
341 string const scale_as_percentage(string const & scale)
343 string::size_type pos = scale.find('=');
344 if (pos != string::npos) {
345 string value = scale.substr(pos + 1);
347 return convert<string>(100 * convert<double>(value));
349 // If the input string didn't match our expectations.
350 // return the default value "100"
355 string remove_braces(string const & value)
359 if (value[0] == '{' && value[value.length()-1] == '}')
360 return value.substr(1, value.length()-2);
365 void handle_hyperref(vector<string> & options)
367 // FIXME swallow inputencoding changes that might surround the
368 // hyperref setup if it was written by LyX
369 h_use_hyperref = "1";
370 // swallow "unicode=true", since LyX does always write that
371 vector<string>::iterator it =
372 find(options.begin(), options.end(), "unicode=true");
373 if (it != options.end())
375 it = find(options.begin(), options.end(), "pdfusetitle");
376 if (it != options.end()) {
377 h_pdf_pdfusetitle = "1";
380 string bookmarks = process_keyval_opt(options, "bookmarks");
381 if (bookmarks == "true")
382 h_pdf_bookmarks = "1";
383 else if (bookmarks == "false")
384 h_pdf_bookmarks = "0";
385 if (h_pdf_bookmarks == "1") {
386 string bookmarksnumbered =
387 process_keyval_opt(options, "bookmarksnumbered");
388 if (bookmarksnumbered == "true")
389 h_pdf_bookmarksnumbered = "1";
390 else if (bookmarksnumbered == "false")
391 h_pdf_bookmarksnumbered = "0";
392 string bookmarksopen =
393 process_keyval_opt(options, "bookmarksopen");
394 if (bookmarksopen == "true")
395 h_pdf_bookmarksopen = "1";
396 else if (bookmarksopen == "false")
397 h_pdf_bookmarksopen = "0";
398 if (h_pdf_bookmarksopen == "1") {
399 string bookmarksopenlevel =
400 process_keyval_opt(options, "bookmarksopenlevel");
401 if (!bookmarksopenlevel.empty())
402 h_pdf_bookmarksopenlevel = bookmarksopenlevel;
405 string breaklinks = process_keyval_opt(options, "breaklinks");
406 if (breaklinks == "true")
407 h_pdf_breaklinks = "1";
408 else if (breaklinks == "false")
409 h_pdf_breaklinks = "0";
410 string pdfborder = process_keyval_opt(options, "pdfborder");
411 if (pdfborder == "{0 0 0}")
412 h_pdf_pdfborder = "1";
413 else if (pdfborder == "{0 0 1}")
414 h_pdf_pdfborder = "0";
415 string backref = process_keyval_opt(options, "backref");
416 if (!backref.empty())
417 h_pdf_backref = backref;
418 string colorlinks = process_keyval_opt(options, "colorlinks");
419 if (colorlinks == "true")
420 h_pdf_colorlinks = "1";
421 else if (colorlinks == "false")
422 h_pdf_colorlinks = "0";
423 string pdfpagemode = process_keyval_opt(options, "pdfpagemode");
424 if (!pdfpagemode.empty())
425 h_pdf_pagemode = pdfpagemode;
426 string pdftitle = process_keyval_opt(options, "pdftitle");
427 if (!pdftitle.empty()) {
428 h_pdf_title = remove_braces(pdftitle);
430 string pdfauthor = process_keyval_opt(options, "pdfauthor");
431 if (!pdfauthor.empty()) {
432 h_pdf_author = remove_braces(pdfauthor);
434 string pdfsubject = process_keyval_opt(options, "pdfsubject");
435 if (!pdfsubject.empty())
436 h_pdf_subject = remove_braces(pdfsubject);
437 string pdfkeywords = process_keyval_opt(options, "pdfkeywords");
438 if (!pdfkeywords.empty())
439 h_pdf_keywords = remove_braces(pdfkeywords);
440 if (!options.empty()) {
441 if (!h_pdf_quoted_options.empty())
442 h_pdf_quoted_options += ',';
443 h_pdf_quoted_options += join(options, ",");
449 void handle_package(Parser &p, string const & name, string const & opts,
450 bool in_lyx_preamble)
452 vector<string> options = split_options(opts);
453 add_package(name, options);
457 if (is_known(name, known_roman_fonts)) {
462 if (name == "fourier") {
463 h_font_roman = "utopia";
464 // when font uses real small capitals
465 if (opts == "expert")
469 if (name == "mathpazo")
470 h_font_roman = "palatino";
472 if (name == "mathptmx")
473 h_font_roman = "times";
476 if (is_known(name, known_sans_fonts)) {
480 h_font_sf_scale = scale_as_percentage(scale);
485 if (is_known(name, known_typewriter_fonts)) {
486 h_font_typewriter = name;
489 h_font_tt_scale = scale_as_percentage(scale);
493 // font uses old-style figure
497 else if (name == "amsmath" || name == "amssymb")
500 else if (name == "esint")
503 else if (name == "babel" && !opts.empty()) {
504 // check if more than one option was used - used later for inputenc
505 // in case inputenc is parsed before babel, set the encoding to auto
506 if (options.begin() != options.end() - 1) {
507 one_language = false;
508 h_inputencoding = "auto";
510 // babel takes the last language of the option of its \usepackage
511 // call as document language. If there is no such language option, the
512 // last language in the documentclass options is used.
513 handle_opt(options, known_languages, h_language);
514 delete_opt(options, known_languages);
517 else if (name == "fontenc")
520 else if (name == "inputenc") {
521 // h_inputencoding is only set when there is not more than one
522 // inputenc option because otherwise h_inputencoding must be
523 // set to "auto" (the default encoding of the document language)
524 // Therefore check for the "," character.
525 // It is also only set when there is not more then one babel
526 // language option but this is handled in the routine for babel.
527 if (opts.find(",") == string::npos && one_language == true)
528 h_inputencoding = opts;
529 if (!options.empty())
530 p.setEncoding(options.back());
534 else if (name == "makeidx")
537 else if (name == "prettyref")
540 else if (name == "varioref")
543 else if (name == "verbatim")
546 else if (name == "nomencl")
549 else if (name == "textcomp")
552 else if (name == "url")
555 else if (LYX_FORMAT >= 408 && name == "subscript")
558 else if (name == "color") {
559 // with the following command this package is only loaded when needed for
560 // undefined colors, since we only support the predefined colors
561 h_preamble << "\\@ifundefined{definecolor}\n {\\usepackage{color}}{}\n";
564 else if (name == "graphicx")
567 else if (name == "setspace")
570 else if (name == "geometry")
571 ; // Ignore this, the geometry settings are made by the \geometry
572 // command. This command is handled below.
574 else if (is_known(name, known_languages))
577 else if (name == "natbib") {
578 h_cite_engine = "natbib_authoryear";
579 vector<string>::iterator it =
580 find(options.begin(), options.end(), "authoryear");
581 if (it != options.end())
584 it = find(options.begin(), options.end(), "numbers");
585 if (it != options.end()) {
586 h_cite_engine = "natbib_numerical";
592 else if (name == "jurabib")
593 h_cite_engine = "jurabib";
595 else if (name == "hyperref")
596 handle_hyperref(options);
598 else if (!in_lyx_preamble) {
600 h_preamble << "\\usepackage{" << name << "}";
602 h_preamble << "\\usepackage[" << opts << "]{"
608 // We need to do something with the options...
609 if (!options.empty())
610 cerr << "Ignoring options '" << join(options, ",")
611 << "' of package " << name << '.' << endl;
613 // remove the whitespace
618 void handle_if(Parser & p, bool in_lyx_preamble)
621 Token t = p.get_token();
622 if (t.cat() == catEscape &&
623 is_known(t.cs(), known_if_commands))
624 handle_if(p, in_lyx_preamble);
626 if (!in_lyx_preamble)
627 h_preamble << t.asInput();
628 if (t.cat() == catEscape && t.cs() == "fi")
635 void end_preamble(ostream & os, TextClass const & /*textclass*/)
637 // translate from babel to LyX names
638 h_language = babel2lyx(h_language);
640 // set the quote language
641 // LyX only knows the following quotes languages:
642 // english, swedish, german, polish, french and danish
643 // (quotes for "japanese" and "chinese-traditional" are missing because
644 // they wouldn't be useful: http://www.lyx.org/trac/ticket/6383)
645 // conversion list taken from
646 // http://en.wikipedia.org/wiki/Quotation_mark,_non-English_usage
647 // (quotes for kazakh and interlingua are unknown)
649 if (h_language == "danish")
650 h_quotes_language = "danish";
652 else if (is_known(h_language, known_french_quotes_languages))
653 h_quotes_language = "french";
655 else if (is_known(h_language, known_german_quotes_languages))
656 h_quotes_language = "german";
658 else if (is_known(h_language, known_polish_quotes_languages))
659 h_quotes_language = "polish";
661 else if (is_known(h_language, known_swedish_quotes_languages))
662 h_quotes_language = "swedish";
664 else if (is_known(h_language, known_english_quotes_languages))
665 h_quotes_language = "english";
667 // output the LyX file settings
668 os << "#LyX file created by tex2lyx " << PACKAGE_VERSION << "\n"
669 << "\\lyxformat " << LYX_FORMAT << '\n'
670 << "\\begin_document\n"
671 << "\\begin_header\n"
672 << "\\textclass " << h_textclass << "\n";
673 if (!h_preamble.str().empty())
674 os << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n";
675 if (!h_options.empty())
676 os << "\\options " << h_options << "\n";
677 os << "\\use_default_options " << h_use_default_options << "\n"
678 << modules_placeholder
679 << "\\language " << h_language << "\n"
680 << "\\inputencoding " << h_inputencoding << "\n"
681 << "\\font_roman " << h_font_roman << "\n"
682 << "\\font_sans " << h_font_sans << "\n"
683 << "\\font_typewriter " << h_font_typewriter << "\n"
684 << "\\font_default_family " << h_font_default_family << "\n"
685 << "\\font_sc " << h_font_sc << "\n"
686 << "\\font_osf " << h_font_osf << "\n"
687 << "\\font_sf_scale " << h_font_sf_scale << "\n"
688 << "\\font_tt_scale " << h_font_tt_scale << "\n"
689 << "\\graphics " << h_graphics << "\n";
690 if (!h_float_placement.empty())
691 os << "\\float_placement " << h_float_placement << "\n";
692 os << "\\paperfontsize " << h_paperfontsize << "\n"
693 << "\\spacing " << h_spacing << "\n"
694 << "\\use_hyperref " << h_use_hyperref << '\n';
695 if (h_use_hyperref == "1") {
696 if (!h_pdf_title.empty())
697 os << "\\pdf_title \"" << h_pdf_title << "\"\n";
698 if (!h_pdf_author.empty())
699 os << "\\pdf_author \"" << h_pdf_author << "\"\n";
700 if (!h_pdf_subject.empty())
701 os << "\\pdf_subject \"" << h_pdf_subject << "\"\n";
702 if (!h_pdf_keywords.empty())
703 os << "\\pdf_keywords \"" << h_pdf_keywords << "\"\n";
704 os << "\\pdf_bookmarks " << h_pdf_bookmarks << "\n"
705 "\\pdf_bookmarksnumbered " << h_pdf_bookmarksnumbered << "\n"
706 "\\pdf_bookmarksopen " << h_pdf_bookmarksopen << "\n"
707 "\\pdf_bookmarksopenlevel " << h_pdf_bookmarksopenlevel << "\n"
708 "\\pdf_breaklinks " << h_pdf_breaklinks << "\n"
709 "\\pdf_pdfborder " << h_pdf_pdfborder << "\n"
710 "\\pdf_colorlinks " << h_pdf_colorlinks << "\n"
711 "\\pdf_backref " << h_pdf_backref << "\n"
712 "\\pdf_pdfusetitle " << h_pdf_pdfusetitle << '\n';
713 if (!h_pdf_pagemode.empty())
714 os << "\\pdf_pagemode " << h_pdf_pagemode << '\n';
715 if (!h_pdf_quoted_options.empty())
716 os << "\\pdf_quoted_options \"" << h_pdf_quoted_options << "\"\n";
718 os << "\\papersize " << h_papersize << "\n"
719 << "\\use_geometry " << h_use_geometry << "\n"
720 << "\\use_amsmath " << h_use_amsmath << "\n"
721 << "\\use_esint " << h_use_esint << "\n"
722 << "\\cite_engine " << h_cite_engine << "\n"
723 << "\\use_bibtopic " << h_use_bibtopic << "\n"
724 << "\\paperorientation " << h_paperorientation << '\n';
725 if (LYX_FORMAT >= 382 && !h_notefontcolor.empty())
726 os << "\\notefontcolor " << h_notefontcolor << '\n';
728 << "\\secnumdepth " << h_secnumdepth << "\n"
729 << "\\tocdepth " << h_tocdepth << "\n"
730 << "\\paragraph_separation " << h_paragraph_separation << "\n";
731 if (LYX_FORMAT < 365 || h_paragraph_separation == "skip")
732 os << "\\defskip " << h_defskip << "\n";
734 os << "\\paragraph_indentation " << h_paragraph_indentation << "\n";
735 os << "\\quotes_language " << h_quotes_language << "\n"
736 << "\\papercolumns " << h_papercolumns << "\n"
737 << "\\papersides " << h_papersides << "\n"
738 << "\\paperpagestyle " << h_paperpagestyle << "\n";
739 if (!h_listings_params.empty())
740 os << "\\listings_params " << h_listings_params << "\n";
741 os << "\\tracking_changes " << h_tracking_changes << "\n"
742 << "\\output_changes " << h_output_changes << "\n"
743 << "\\end_header\n\n"
745 // clear preamble for subdocuments
749 } // anonymous namespace
752 void parse_preamble(Parser & p, ostream & os,
753 string const & forceclass, TeX2LyXDocClass & tc)
755 // initialize fixed types
756 special_columns['D'] = 3;
757 bool is_full_document = false;
758 bool is_lyx_file = false;
759 bool in_lyx_preamble = false;
761 // determine whether this is a full document or a fragment for inclusion
763 Token const & t = p.get_token();
765 if (t.cat() == catEscape && t.cs() == "documentclass") {
766 is_full_document = true;
772 while (is_full_document && p.good()) {
773 Token const & t = p.get_token();
776 cerr << "t: " << t << "\n";
782 if (!in_lyx_preamble &&
783 (t.cat() == catLetter ||
784 t.cat() == catSuper ||
786 t.cat() == catOther ||
787 t.cat() == catMath ||
788 t.cat() == catActive ||
789 t.cat() == catBegin ||
791 t.cat() == catAlign ||
792 t.cat() == catParameter))
793 h_preamble << t.cs();
795 else if (!in_lyx_preamble &&
796 (t.cat() == catSpace || t.cat() == catNewline))
797 h_preamble << t.asInput();
799 else if (t.cat() == catComment) {
800 static regex const islyxfile("%% LyX .* created this file");
801 static regex const usercommands("User specified LaTeX commands");
803 string const comment = t.asInput();
805 // magically switch encoding default if it looks like XeLaTeX
806 static string const magicXeLaTeX =
807 "% This document must be compiled with XeLaTeX ";
808 if (comment.size() > magicXeLaTeX.size()
809 && comment.substr(0, magicXeLaTeX.size()) == magicXeLaTeX
810 && h_inputencoding == "auto") {
811 cerr << "XeLaTeX comment found, switching to UTF8\n";
812 h_inputencoding = "utf8";
815 if (regex_search(comment, sub, islyxfile)) {
817 in_lyx_preamble = true;
818 } else if (is_lyx_file
819 && regex_search(comment, sub, usercommands))
820 in_lyx_preamble = false;
821 else if (!in_lyx_preamble)
822 h_preamble << t.asInput();
825 else if (t.cs() == "pagestyle")
826 h_paperpagestyle = p.verbatim_item();
828 else if (t.cs() == "makeatletter") {
829 // LyX takes care of this
830 p.setCatCode('@', catLetter);
833 else if (t.cs() == "makeatother") {
834 // LyX takes care of this
835 p.setCatCode('@', catOther);
838 else if (t.cs() == "newcommand" || t.cs() == "newcommandx"
839 || t.cs() == "renewcommand" || t.cs() == "renewcommandx"
840 || t.cs() == "providecommand" || t.cs() == "providecommandx"
841 || t.cs() == "DeclareRobustCommand"
842 || t.cs() == "DeclareRobustCommandx"
843 || t.cs() == "ProvideTextCommandDefault"
844 || t.cs() == "DeclareMathAccent") {
846 if (p.next_token().character() == '*') {
850 string const name = p.verbatim_item();
851 string const opt1 = p.getFullOpt();
852 string const opt2 = p.getFullOpt();
853 string const body = p.verbatim_item();
855 if (name == "\\rmdefault")
856 if (is_known(body, known_roman_fonts))
858 if (name == "\\sfdefault")
859 if (is_known(body, known_sans_fonts))
861 if (name == "\\ttdefault")
862 if (is_known(body, known_typewriter_fonts))
863 h_font_typewriter = body;
864 if (name == "\\familydefault") {
865 string family = body;
866 // remove leading "\"
867 h_font_default_family = family.erase(0,1);
870 // Add the command to the known commands
871 add_known_command(name, opt1, !opt2.empty(), from_utf8(body));
873 // only non-lyxspecific stuff
874 if (!in_lyx_preamble) {
876 ss << '\\' << t.cs();
879 ss << '{' << name << '}' << opt1 << opt2
880 << '{' << body << "}";
881 h_preamble << ss.str();
883 ostream & out = in_preamble ? h_preamble : os;
884 out << "\\" << t.cs() << "{" << name << "}"
885 << opts << "{" << body << "}";
890 else if (t.cs() == "documentclass") {
891 vector<string>::iterator it;
892 vector<string> opts = split_options(p.getArg('[', ']'));
893 handle_opt(opts, known_fontsizes, h_paperfontsize);
894 delete_opt(opts, known_fontsizes);
895 // delete "pt" at the end
896 string::size_type i = h_paperfontsize.find("pt");
897 if (i != string::npos)
898 h_paperfontsize.erase(i);
899 // The documentclass options are always parsed before the options
900 // of the babel call so that a language cannot overwrite the babel
902 handle_opt(opts, known_languages, h_language);
903 delete_opt(opts, known_languages);
906 if ((it = find(opts.begin(), opts.end(), "landscape")) != opts.end()) {
907 h_paperorientation = "landscape";
911 if ((it = find(opts.begin(), opts.end(), "oneside"))
916 if ((it = find(opts.begin(), opts.end(), "twoside"))
922 if ((it = find(opts.begin(), opts.end(), "onecolumn"))
924 h_papercolumns = "1";
927 if ((it = find(opts.begin(), opts.end(), "twocolumn"))
929 h_papercolumns = "2";
933 // some size options are know to any document classes, other sizes
934 // are handled by the \geometry command of the geometry package
935 handle_opt(opts, known_class_paper_sizes, h_papersize);
936 delete_opt(opts, known_class_paper_sizes);
937 // the remaining options
938 h_options = join(opts, ",");
939 // FIXME This does not work for classes that have a
940 // different name in LyX than in LaTeX
941 h_textclass = p.getArg('{', '}');
944 else if (t.cs() == "usepackage") {
945 string const options = p.getArg('[', ']');
946 string const name = p.getArg('{', '}');
947 vector<string> vecnames;
948 split(name, vecnames, ',');
949 vector<string>::const_iterator it = vecnames.begin();
950 vector<string>::const_iterator end = vecnames.end();
951 for (; it != end; ++it)
952 handle_package(p, trim(*it), options,
956 else if (t.cs() == "inputencoding") {
957 string const encoding = p.getArg('{','}');
958 h_inputencoding = encoding;
959 p.setEncoding(encoding);
962 else if (t.cs() == "newenvironment") {
963 string const name = p.getArg('{', '}');
964 string const opt1 = p.getFullOpt();
965 string const opt2 = p.getFullOpt();
966 string const beg = p.verbatim_item();
967 string const end = p.verbatim_item();
968 if (!in_lyx_preamble) {
969 h_preamble << "\\newenvironment{" << name
970 << '}' << opt1 << opt2 << '{'
971 << beg << "}{" << end << '}';
973 add_known_environment(name, opt1, !opt2.empty(),
974 from_utf8(beg), from_utf8(end));
978 else if (t.cs() == "def") {
979 string name = p.get_token().cs();
980 while (p.next_token().cat() != catBegin)
981 name += p.get_token().cs();
982 if (!in_lyx_preamble)
983 h_preamble << "\\def\\" << name << '{'
984 << p.verbatim_item() << "}";
987 else if (t.cs() == "newcolumntype") {
988 string const name = p.getArg('{', '}');
991 string opts = p.getOpt();
993 istringstream is(string(opts, 1));
996 special_columns[name[0]] = nargs;
997 h_preamble << "\\newcolumntype{" << name << "}";
999 h_preamble << "[" << nargs << "]";
1000 h_preamble << "{" << p.verbatim_item() << "}";
1003 else if (t.cs() == "setcounter") {
1004 string const name = p.getArg('{', '}');
1005 string const content = p.getArg('{', '}');
1006 if (name == "secnumdepth")
1007 h_secnumdepth = content;
1008 else if (name == "tocdepth")
1009 h_tocdepth = content;
1011 h_preamble << "\\setcounter{" << name << "}{" << content << "}";
1014 else if (t.cs() == "setlength") {
1015 string const name = p.verbatim_item();
1016 string const content = p.verbatim_item();
1017 // the paragraphs are only not indented when \parindent is set to zero
1018 if (name == "\\parindent" && content != "") {
1019 if (content[0] == '0')
1020 h_paragraph_separation = "skip";
1021 else if (LYX_FORMAT >= 365)
1022 h_paragraph_indentation = translate_len(content);
1024 h_preamble << "\\setlength{" << name
1025 << "}{" << content << "}";
1026 } else if (name == "\\parskip") {
1027 if (content == "\\smallskipamount")
1028 h_defskip = "smallskip";
1029 else if (content == "\\medskipamount")
1030 h_defskip = "medskip";
1031 else if (content == "\\bigskipamount")
1032 h_defskip = "bigskip";
1034 h_defskip = content;
1036 h_preamble << "\\setlength{" << name << "}{" << content << "}";
1039 else if (t.cs() == "onehalfspacing")
1040 h_spacing = "onehalf";
1042 else if (t.cs() == "doublespacing")
1043 h_spacing = "double";
1045 else if (t.cs() == "setstretch")
1046 h_spacing = "other " + p.verbatim_item();
1048 else if (t.cs() == "begin") {
1049 string const name = p.getArg('{', '}');
1050 if (name == "document")
1052 h_preamble << "\\begin{" << name << "}";
1055 else if (t.cs() == "geometry") {
1056 h_use_geometry = "true";
1057 vector<string> opts = split_options(p.getArg('{', '}'));
1058 vector<string>::iterator it;
1059 // paper orientation
1060 if ((it = find(opts.begin(), opts.end(), "landscape")) != opts.end()) {
1061 h_paperorientation = "landscape";
1065 handle_opt(opts, known_paper_sizes, h_papersize);
1066 delete_opt(opts, known_paper_sizes);
1068 char const * const * margin = known_paper_margins;
1070 for (; *margin; ++margin) {
1072 // search for the "=" in e.g. "lmargin=2cm" to get the value
1073 for(size_t i = 0; i != opts.size(); i++) {
1074 if (opts.at(i).find(*margin) != string::npos) {
1075 string::size_type pos = opts.at(i).find("=");
1076 string value = opts.at(i).substr(pos + 1);
1077 string name = known_coded_paper_margins[k];
1078 h_margins += "\\" + name + " " + value + "\n";
1084 else if (t.cs() == "definecolor") {
1085 string const color = p.getArg('{', '}');
1086 string const space = p.getArg('{', '}');
1087 string const value = p.getArg('{', '}');
1088 if (LYX_FORMAT >= 382 &&
1089 color == "note_fontcolor" && space == "rgb") {
1090 RGBColor c(RGBColorFromLaTeX(value));
1091 h_notefontcolor = X11hexname(c);
1093 h_preamble << "\\definecolor{" << color
1094 << "}{" << space << "}{" << value
1099 else if (t.cs() == "jurabibsetup") {
1100 // FIXME p.getArg('{', '}') is most probably wrong (it
1101 // does not handle nested braces).
1102 // Use p.verbatim_item() instead.
1103 vector<string> jurabibsetup =
1104 split_options(p.getArg('{', '}'));
1105 // add jurabibsetup to the jurabib package options
1106 add_package("jurabib", jurabibsetup);
1107 if (!jurabibsetup.empty()) {
1108 h_preamble << "\\jurabibsetup{"
1109 << join(jurabibsetup, ",") << '}';
1113 else if (t.cs() == "hypersetup") {
1114 vector<string> hypersetup =
1115 split_options(p.verbatim_item());
1116 // add hypersetup to the hyperref package options
1117 handle_hyperref(hypersetup);
1118 if (!hypersetup.empty()) {
1119 h_preamble << "\\hypersetup{"
1120 << join(hypersetup, ",") << '}';
1124 else if (is_known(t.cs(), known_if_3arg_commands)) {
1125 // prevent misparsing of \usepackage if it is used
1126 // as an argument (see e.g. our own output of
1127 // \@ifundefined above)
1128 string const arg1 = p.verbatim_item();
1129 string const arg2 = p.verbatim_item();
1130 string const arg3 = p.verbatim_item();
1131 if (!in_lyx_preamble) {
1132 h_preamble << t.asInput()
1133 << '{' << arg1 << '}'
1134 << '{' << arg2 << '}'
1135 << '{' << arg3 << '}';
1139 else if (is_known(t.cs(), known_if_commands)) {
1140 // must not parse anything in conditional code, since
1141 // LyX would output the parsed contents unconditionally
1142 if (!in_lyx_preamble)
1143 h_preamble << t.asInput();
1144 handle_if(p, in_lyx_preamble);
1147 else if (!t.cs().empty() && !in_lyx_preamble)
1148 h_preamble << '\\' << t.cs();
1151 // remove the whitespace
1154 // Force textclass if the user wanted it
1155 if (!forceclass.empty())
1156 h_textclass = forceclass;
1157 if (noweb_mode && !prefixIs(h_textclass, "literate-"))
1158 h_textclass.insert(0, "literate-");
1159 tc.setName(h_textclass);
1161 cerr << "Error: Could not read layout file for textclass \"" << h_textclass << "\"." << endl;
1164 if (h_papersides.empty()) {
1167 h_papersides = ss.str();
1169 end_preamble(os, tc);
1173 /// translates a babel language name to a LyX language name
1174 string babel2lyx(string const & language)
1176 char const * const * where = is_known(language, known_languages);
1178 return known_coded_languages[where - known_languages];