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";
52 string h_paragraph_separation = "indent";
56 //add this to known_languages when updating to lyxformat 266:
57 // "armenian" (needs special handling since not supported by standard babel)
58 //add these to known_languages when updating to lyxformat 268:
59 //"chinese-simplified", "chinese-traditional", "japanese", "korean"
60 // Both changes require first that support for non-babel languages (CJK,
62 // add turkmen for lyxformat 383
64 * known babel language names (including synonyms)
65 * not in standard babel: arabic, arabtex, armenian, belarusian, serbian-latin, thai
66 * not yet supported by LyX: kurmanji
67 * please keep this in sync with known_coded_languages line by line!
69 const char * const known_languages[] = {"acadian", "afrikaans", "albanian",
70 "american", "arabic", "arabtex", "austrian", "bahasa", "bahasai", "bahasam",
71 "basque", "belarusian", "brazil", "brazilian", "breton", "british", "bulgarian",
72 "canadian", "canadien", "catalan", "croatian", "czech", "danish", "dutch",
73 "english", "esperanto", "estonian", "farsi", "finnish", "francais", "french",
74 "frenchb", "frenchle", "frenchpro", "galician", "german", "germanb", "greek",
75 "hebrew", "hungarian", "icelandic", "indon", "indonesian", "interlingua",
76 "irish", "italian", "kazakh", "latin", "latvian", "lithuanian", "lowersorbian",
77 "lsorbian", "magyar", "malay", "meyalu", "mongolian", "naustrian", "newzealand",
78 "ngerman", "ngermanb", "norsk", "nynorsk", "polutonikogreek", "polish",
79 "portuges", "portuguese", "romanian", "russian", "russianb", "samin",
80 "scottish", "serbian", "serbian-latin", "slovak", "slovene", "spanish",
81 "swedish", "thai", "turkish", "turkmen", "ukraineb", "ukrainian",
82 "uppersorbian", "UKenglish", "USenglish", "usorbian", "vietnam", "welsh",
86 * the same as known_languages with .lyx names
87 * please keep this in sync with known_languages line by line!
89 const char * const known_coded_languages[] = {"french", "afrikaans", "albanian",
90 "american", "arabic_arabi", "arabic_arabtex", "austrian", "bahasa", "bahasa", "bahasam",
91 "basque", "belarusian", "brazilian", "brazilian", "breton", "british", "bulgarian",
92 "canadian", "canadien", "catalan", "croatian", "czech", "danish", "dutch",
93 "english", "esperanto", "estonian", "farsi", "finnish", "french", "french",
94 "french", "french", "french", "galician", "german", "german", "greek",
95 "hebrew", "magyar", "icelandic", "bahasa", "bahasa", "interlingua",
96 "irish", "italian", "kazakh", "latin", "latvian", "lithuanian", "lowersorbian",
97 "lowersorbian", "magyar", "bahasam", "bahasam", "mongolian", "naustrian", "english",
98 "ngerman", "ngerman", "norsk", "nynorsk", "polutonikogreek", "polish",
99 "portuguese", "portuguese", "romanian", "russian", "russian", "samin",
100 "scottish", "serbian", "serbian-latin", "slovak", "slovene", "spanish",
101 "swedish", "thai", "turkish", "turkmen", "ukrainian", "ukrainian",
102 "uppersorbian", "uppersorbian", "english", "english", "vietnamese", "welsh",
105 /// languages with english quotes (.lyx names)
106 const char * const known_english_quotes_languages[] = {"american", "bahasa",
107 "bahasam", "brazilian", "canadian", "chinese-simplified", "english",
108 "esperanto", "hebrew", "irish", "korean", "portuguese", "scottish", "thai", 0};
110 /// languages with french quotes (.lyx names)
111 const char * const known_french_quotes_languages[] = {"albanian",
112 "arabic_arabi", "arabic_arabtex", "basque", "canadien", "catalan", "french",
113 "galician", "greek", "italian", "norsk", "nynorsk", "polutonikogreek",
114 "russian", "spanish", "spanish-mexico", "turkish", "turkmen", "ukrainian",
117 /// languages with german quotes (.lyx names)
118 const char * const known_german_quotes_languages[] = {"austrian", "bulgarian",
119 "czech", "german", "icelandic", "lithuanian", "lowersorbian", "naustrian",
120 "ngerman", "serbian", "serbian-latin", "slovak", "slovene", "uppersorbian", 0};
122 /// languages with polish quotes (.lyx names)
123 const char * const known_polish_quotes_languages[] = {"afrikaans", "croatian",
124 "dutch", "estonian", "magyar", "polish", "romanian", 0};
126 /// languages with swedish quotes (.lyx names)
127 const char * const known_swedish_quotes_languages[] = {"finnish",
130 char const * const known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
132 const char * const known_roman_fonts[] = { "ae", "bookman", "charter",
133 "cmr", "fourier", "lmodern", "mathpazo", "mathptmx", "newcent", 0};
135 const char * const known_sans_fonts[] = { "avant", "berasans", "cmbr", "cmss",
136 "helvet", "lmss", 0};
138 const char * const known_typewriter_fonts[] = { "beramono", "cmtl", "cmtt",
139 "courier", "lmtt", "luximono", "fourier", "lmodern", "mathpazo", "mathptmx",
142 const char * const known_paper_sizes[] = { "a3paper", "b3paper", "a4paper",
143 "b4paper", "a5paper", "b5paper", "executivepaper", "legalpaper",
146 const char * const known_class_paper_sizes[] = { "a4paper", "a5paper",
147 "executivepaper", "legalpaper", "letterpaper", 0};
149 const char * const known_paper_margins[] = { "lmargin", "tmargin", "rmargin",
150 "bmargin", "headheight", "headsep", "footskip", "columnsep", 0};
152 const char * const known_coded_paper_margins[] = { "leftmargin", "topmargin",
153 "rightmargin", "bottommargin", "headheight", "headsep", "footskip",
156 /// commands that can start an \if...\else...\endif sequence
157 const char * const known_if_commands[] = {"if", "ifarydshln", "ifbraket",
158 "ifcancel", "ifcolortbl", "ifeurosym", "ifmarginnote", "ifmmode", "ifpdf",
159 "ifsidecap", "ifupgreek", 0};
161 /// conditional commands with three arguments like \@ifundefined{}{}{}
162 const char * const known_if_3arg_commands[] = {"@ifundefined", "IfFileExists",
166 ostringstream h_preamble;
167 string h_textclass = "article";
168 string h_use_default_options = "false";
170 string h_language = "english";
171 string h_language_package = "default";
172 string h_font_roman = "default";
173 string h_font_sans = "default";
174 string h_font_typewriter = "default";
175 string h_font_default_family = "default";
176 string h_font_sc = "false";
177 string h_font_osf = "false";
178 string h_font_sf_scale = "100";
179 string h_font_tt_scale = "100";
180 string h_graphics = "default";
181 string h_float_placement;
182 string h_paperfontsize = "default";
183 string h_spacing = "single";
184 string h_use_hyperref = "0";
187 string h_pdf_subject;
188 string h_pdf_keywords;
189 string h_pdf_bookmarks = "1";
190 string h_pdf_bookmarksnumbered = "0";
191 string h_pdf_bookmarksopen = "0";
192 string h_pdf_bookmarksopenlevel = "1";
193 string h_pdf_breaklinks = "0";
194 string h_pdf_pdfborder = "0";
195 string h_pdf_colorlinks = "0";
196 string h_pdf_backref = "section";
197 string h_pdf_pdfusetitle = "1";
198 string h_pdf_pagemode;
199 string h_pdf_quoted_options;
200 string h_papersize = "default";
201 string h_use_geometry = "false";
202 string h_use_amsmath = "1";
203 string h_use_esint = "1";
204 string h_use_mhchem = "0";
205 string h_use_mathdots = "0";
206 string h_cite_engine = "basic";
207 string h_use_bibtopic = "false";
208 string h_paperorientation = "portrait";
209 string h_suppress_date = "false";
210 string h_use_refstyle = "0";
211 string h_notefontcolor;
212 string h_secnumdepth = "3";
213 string h_tocdepth = "3";
214 string h_defskip = "medskip";
215 string h_paragraph_indentation = "default";
216 string h_quotes_language = "english";
217 string h_papercolumns = "1";
219 string h_paperpagestyle = "default";
220 string h_listings_params;
221 string h_tracking_changes = "false";
222 string h_output_changes = "false";
223 string h_html_math_output = "0";
224 string h_html_css_as_file = "0";
225 string h_html_be_strict = "false";
229 // returns true if at least one of the options in what has been found
230 bool handle_opt(vector<string> & opts, char const * const * what, string & target)
236 // the last language option is the document language (for babel and LyX)
237 // the last size option is the document font size
238 vector<string>::iterator it;
239 vector<string>::iterator position = opts.begin();
240 for (; *what; ++what) {
241 it = find(opts.begin(), opts.end(), *what);
242 if (it != opts.end()) {
243 if (it >= position) {
254 void delete_opt(vector<string> & opts, char const * const * what)
259 // remove found options from the list
260 // do this after handle_opt to avoid potential memory leaks
261 vector<string>::iterator it;
262 for (; *what; ++what) {
263 it = find(opts.begin(), opts.end(), *what);
264 if (it != opts.end())
271 * Split a package options string (keyval format) into a vector.
273 * authorformat=smallcaps,
275 * titleformat=colonsep,
276 * bibformat={tabular,ibidem,numbered}
278 vector<string> split_options(string const & input)
280 vector<string> options;
284 Token const & t = p.get_token();
285 if (t.asInput() == ",") {
286 options.push_back(trimSpaceAndEol(option));
288 } else if (t.asInput() == "=") {
291 if (p.next_token().asInput() == "{")
292 option += '{' + p.getArg('{', '}') + '}';
293 } else if (t.cat() != catSpace)
294 option += t.asInput();
298 options.push_back(trimSpaceAndEol(option));
305 * Retrieve a keyval option "name={value with=sign}" named \p name from
306 * \p options and return the value.
307 * The found option is also removed from \p options.
309 string process_keyval_opt(vector<string> & options, string name)
311 for (size_t i = 0; i < options.size(); ++i) {
312 vector<string> option;
313 split(options[i], option, '=');
314 if (option.size() < 2)
316 if (option[0] == name) {
317 options.erase(options.begin() + i);
318 option.erase(option.begin());
319 return join(option, "=");
327 * Add package \p name with options \p options to used_packages.
328 * Remove options from \p options that we don't want to output.
330 void add_package(string const & name, vector<string> & options)
332 // every package inherits the global options
333 if (used_packages.find(name) == used_packages.end())
334 used_packages[name] = split_options(h_options);
336 vector<string> & v = used_packages[name];
337 v.insert(v.end(), options.begin(), options.end());
338 if (name == "jurabib") {
339 // Don't output the order argument (see the cite command
340 // handling code in text.cpp).
341 vector<string>::iterator end =
342 remove(options.begin(), options.end(), "natbiborder");
343 end = remove(options.begin(), end, "jurabiborder");
344 options.erase(end, options.end());
349 // Given is a string like "scaled=0.9", return 0.9 * 100
350 string const scale_as_percentage(string const & scale)
352 string::size_type pos = scale.find('=');
353 if (pos != string::npos) {
354 string value = scale.substr(pos + 1);
356 return convert<string>(100 * convert<double>(value));
358 // If the input string didn't match our expectations.
359 // return the default value "100"
364 string remove_braces(string const & value)
368 if (value[0] == '{' && value[value.length()-1] == '}')
369 return value.substr(1, value.length()-2);
374 void handle_hyperref(vector<string> & options)
376 // FIXME swallow inputencoding changes that might surround the
377 // hyperref setup if it was written by LyX
378 h_use_hyperref = "1";
379 // swallow "unicode=true", since LyX does always write that
380 vector<string>::iterator it =
381 find(options.begin(), options.end(), "unicode=true");
382 if (it != options.end())
384 it = find(options.begin(), options.end(), "pdfusetitle");
385 if (it != options.end()) {
386 h_pdf_pdfusetitle = "1";
389 string bookmarks = process_keyval_opt(options, "bookmarks");
390 if (bookmarks == "true")
391 h_pdf_bookmarks = "1";
392 else if (bookmarks == "false")
393 h_pdf_bookmarks = "0";
394 if (h_pdf_bookmarks == "1") {
395 string bookmarksnumbered =
396 process_keyval_opt(options, "bookmarksnumbered");
397 if (bookmarksnumbered == "true")
398 h_pdf_bookmarksnumbered = "1";
399 else if (bookmarksnumbered == "false")
400 h_pdf_bookmarksnumbered = "0";
401 string bookmarksopen =
402 process_keyval_opt(options, "bookmarksopen");
403 if (bookmarksopen == "true")
404 h_pdf_bookmarksopen = "1";
405 else if (bookmarksopen == "false")
406 h_pdf_bookmarksopen = "0";
407 if (h_pdf_bookmarksopen == "1") {
408 string bookmarksopenlevel =
409 process_keyval_opt(options, "bookmarksopenlevel");
410 if (!bookmarksopenlevel.empty())
411 h_pdf_bookmarksopenlevel = bookmarksopenlevel;
414 string breaklinks = process_keyval_opt(options, "breaklinks");
415 if (breaklinks == "true")
416 h_pdf_breaklinks = "1";
417 else if (breaklinks == "false")
418 h_pdf_breaklinks = "0";
419 string pdfborder = process_keyval_opt(options, "pdfborder");
420 if (pdfborder == "{0 0 0}")
421 h_pdf_pdfborder = "1";
422 else if (pdfborder == "{0 0 1}")
423 h_pdf_pdfborder = "0";
424 string backref = process_keyval_opt(options, "backref");
425 if (!backref.empty())
426 h_pdf_backref = backref;
427 string colorlinks = process_keyval_opt(options, "colorlinks");
428 if (colorlinks == "true")
429 h_pdf_colorlinks = "1";
430 else if (colorlinks == "false")
431 h_pdf_colorlinks = "0";
432 string pdfpagemode = process_keyval_opt(options, "pdfpagemode");
433 if (!pdfpagemode.empty())
434 h_pdf_pagemode = pdfpagemode;
435 string pdftitle = process_keyval_opt(options, "pdftitle");
436 if (!pdftitle.empty()) {
437 h_pdf_title = remove_braces(pdftitle);
439 string pdfauthor = process_keyval_opt(options, "pdfauthor");
440 if (!pdfauthor.empty()) {
441 h_pdf_author = remove_braces(pdfauthor);
443 string pdfsubject = process_keyval_opt(options, "pdfsubject");
444 if (!pdfsubject.empty())
445 h_pdf_subject = remove_braces(pdfsubject);
446 string pdfkeywords = process_keyval_opt(options, "pdfkeywords");
447 if (!pdfkeywords.empty())
448 h_pdf_keywords = remove_braces(pdfkeywords);
449 if (!options.empty()) {
450 if (!h_pdf_quoted_options.empty())
451 h_pdf_quoted_options += ',';
452 h_pdf_quoted_options += join(options, ",");
458 void handle_package(Parser &p, string const & name, string const & opts,
459 bool in_lyx_preamble)
461 vector<string> options = split_options(opts);
462 add_package(name, options);
466 if (is_known(name, known_roman_fonts)) {
471 if (name == "fourier") {
472 h_font_roman = "utopia";
473 // when font uses real small capitals
474 if (opts == "expert")
478 if (name == "mathpazo")
479 h_font_roman = "palatino";
481 if (name == "mathptmx")
482 h_font_roman = "times";
485 if (is_known(name, known_sans_fonts)) {
489 h_font_sf_scale = scale_as_percentage(scale);
494 if (is_known(name, known_typewriter_fonts)) {
495 h_font_typewriter = name;
498 h_font_tt_scale = scale_as_percentage(scale);
502 // font uses old-style figure
506 else if (name == "amsmath" || name == "amssymb")
509 else if (name == "esint")
512 else if (name == "mhchem")
515 else if (name == "mathdots")
516 h_use_mathdots = "2";
518 else if (name == "babel" && !opts.empty()) {
519 // check if more than one option was used - used later for inputenc
520 // in case inputenc is parsed before babel, set the encoding to auto
521 if (options.begin() != options.end() - 1) {
522 one_language = false;
523 h_inputencoding = "auto";
525 // babel takes the last language of the option of its \usepackage
526 // call as document language. If there is no such language option, the
527 // last language in the documentclass options is used.
528 handle_opt(options, known_languages, h_language);
529 delete_opt(options, known_languages);
532 else if (name == "fontenc")
535 else if (name == "inputenc" || name == "luainputenc") {
536 // h_inputencoding is only set when there is not more than one
537 // inputenc option because otherwise h_inputencoding must be
538 // set to "auto" (the default encoding of the document language)
539 // Therefore check for the "," character.
540 // It is also only set when there is not more then one babel
541 // language option but this is handled in the routine for babel.
542 if (opts.find(",") == string::npos && one_language == true)
543 h_inputencoding = opts;
544 if (!options.empty())
545 p.setEncoding(options.back());
549 else if (name == "makeidx")
552 else if (name == "prettyref")
555 else if (name == "varioref")
558 else if (name == "verbatim")
561 else if (name == "nomencl")
564 else if (name == "textcomp")
567 else if (name == "url")
570 else if (name == "subscript")
573 else if (name == "color") {
574 // with the following command this package is only loaded when needed for
575 // undefined colors, since we only support the predefined colors
576 h_preamble << "\\@ifundefined{definecolor}\n {\\usepackage{color}}{}\n";
579 else if (name == "graphicx")
582 else if (name == "setspace")
585 else if (name == "geometry")
586 ; // Ignore this, the geometry settings are made by the \geometry
587 // command. This command is handled below.
589 else if (is_known(name, known_languages))
592 else if (name == "natbib") {
593 h_cite_engine = "natbib_authoryear";
594 vector<string>::iterator it =
595 find(options.begin(), options.end(), "authoryear");
596 if (it != options.end())
599 it = find(options.begin(), options.end(), "numbers");
600 if (it != options.end()) {
601 h_cite_engine = "natbib_numerical";
607 else if (name == "jurabib")
608 h_cite_engine = "jurabib";
610 else if (name == "hyperref")
611 handle_hyperref(options);
613 else if (!in_lyx_preamble) {
615 h_preamble << "\\usepackage{" << name << "}";
617 h_preamble << "\\usepackage[" << opts << "]{"
623 // We need to do something with the options...
624 if (!options.empty())
625 cerr << "Ignoring options '" << join(options, ",")
626 << "' of package " << name << '.' << endl;
628 // remove the whitespace
633 void handle_if(Parser & p, bool in_lyx_preamble)
636 Token t = p.get_token();
637 if (t.cat() == catEscape &&
638 is_known(t.cs(), known_if_commands))
639 handle_if(p, in_lyx_preamble);
641 if (!in_lyx_preamble)
642 h_preamble << t.asInput();
643 if (t.cat() == catEscape && t.cs() == "fi")
650 void end_preamble(ostream & os, TextClass const & /*textclass*/)
652 // translate from babel to LyX names
653 h_language = babel2lyx(h_language);
655 // set the quote language
656 // LyX only knows the following quotes languages:
657 // english, swedish, german, polish, french and danish
658 // (quotes for "japanese" and "chinese-traditional" are missing because
659 // they wouldn't be useful: http://www.lyx.org/trac/ticket/6383)
660 // conversion list taken from
661 // http://en.wikipedia.org/wiki/Quotation_mark,_non-English_usage
662 // (quotes for kazakh and interlingua are unknown)
664 if (h_language == "danish")
665 h_quotes_language = "danish";
667 else if (is_known(h_language, known_french_quotes_languages))
668 h_quotes_language = "french";
670 else if (is_known(h_language, known_german_quotes_languages))
671 h_quotes_language = "german";
673 else if (is_known(h_language, known_polish_quotes_languages))
674 h_quotes_language = "polish";
676 else if (is_known(h_language, known_swedish_quotes_languages))
677 h_quotes_language = "swedish";
679 else if (is_known(h_language, known_english_quotes_languages))
680 h_quotes_language = "english";
682 // output the LyX file settings
683 os << "#LyX file created by tex2lyx " << PACKAGE_VERSION << "\n"
684 << "\\lyxformat " << LYX_FORMAT << '\n'
685 << "\\begin_document\n"
686 << "\\begin_header\n"
687 << "\\textclass " << h_textclass << "\n";
688 if (!h_preamble.str().empty())
689 os << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n";
690 if (!h_options.empty())
691 os << "\\options " << h_options << "\n";
692 os << "\\use_default_options " << h_use_default_options << "\n"
693 << modules_placeholder
694 << "\\language " << h_language << "\n"
695 << "\\language_package " << h_language_package << "\n"
696 << "\\inputencoding " << h_inputencoding << "\n"
697 << "\\font_roman " << h_font_roman << "\n"
698 << "\\font_sans " << h_font_sans << "\n"
699 << "\\font_typewriter " << h_font_typewriter << "\n"
700 << "\\font_default_family " << h_font_default_family << "\n"
701 << "\\font_sc " << h_font_sc << "\n"
702 << "\\font_osf " << h_font_osf << "\n"
703 << "\\font_sf_scale " << h_font_sf_scale << "\n"
704 << "\\font_tt_scale " << h_font_tt_scale << "\n"
705 << "\\graphics " << h_graphics << "\n";
706 if (!h_float_placement.empty())
707 os << "\\float_placement " << h_float_placement << "\n";
708 os << "\\paperfontsize " << h_paperfontsize << "\n"
709 << "\\spacing " << h_spacing << "\n"
710 << "\\use_hyperref " << h_use_hyperref << '\n';
711 if (h_use_hyperref == "1") {
712 if (!h_pdf_title.empty())
713 os << "\\pdf_title \"" << h_pdf_title << "\"\n";
714 if (!h_pdf_author.empty())
715 os << "\\pdf_author \"" << h_pdf_author << "\"\n";
716 if (!h_pdf_subject.empty())
717 os << "\\pdf_subject \"" << h_pdf_subject << "\"\n";
718 if (!h_pdf_keywords.empty())
719 os << "\\pdf_keywords \"" << h_pdf_keywords << "\"\n";
720 os << "\\pdf_bookmarks " << h_pdf_bookmarks << "\n"
721 "\\pdf_bookmarksnumbered " << h_pdf_bookmarksnumbered << "\n"
722 "\\pdf_bookmarksopen " << h_pdf_bookmarksopen << "\n"
723 "\\pdf_bookmarksopenlevel " << h_pdf_bookmarksopenlevel << "\n"
724 "\\pdf_breaklinks " << h_pdf_breaklinks << "\n"
725 "\\pdf_pdfborder " << h_pdf_pdfborder << "\n"
726 "\\pdf_colorlinks " << h_pdf_colorlinks << "\n"
727 "\\pdf_backref " << h_pdf_backref << "\n"
728 "\\pdf_pdfusetitle " << h_pdf_pdfusetitle << '\n';
729 if (!h_pdf_pagemode.empty())
730 os << "\\pdf_pagemode " << h_pdf_pagemode << '\n';
731 if (!h_pdf_quoted_options.empty())
732 os << "\\pdf_quoted_options \"" << h_pdf_quoted_options << "\"\n";
734 os << "\\papersize " << h_papersize << "\n"
735 << "\\use_geometry " << h_use_geometry << "\n"
736 << "\\use_amsmath " << h_use_amsmath << "\n"
737 << "\\use_esint " << h_use_esint << "\n"
738 << "\\use_mhchem " << h_use_mhchem << "\n"
739 << "\\use_mathdots " << h_use_mathdots << "\n"
740 << "\\cite_engine " << h_cite_engine << "\n"
741 << "\\use_bibtopic " << h_use_bibtopic << "\n"
742 << "\\paperorientation " << h_paperorientation << '\n'
743 << "\\suppress_date " << h_suppress_date << '\n'
744 << "\\use_refstyle " << h_use_refstyle << '\n';
745 if (!h_notefontcolor.empty())
746 os << "\\notefontcolor " << h_notefontcolor << '\n';
748 << "\\secnumdepth " << h_secnumdepth << "\n"
749 << "\\tocdepth " << h_tocdepth << "\n"
750 << "\\paragraph_separation " << h_paragraph_separation << "\n";
751 if (h_paragraph_separation == "skip")
752 os << "\\defskip " << h_defskip << "\n";
754 os << "\\paragraph_indentation " << h_paragraph_indentation << "\n";
755 os << "\\quotes_language " << h_quotes_language << "\n"
756 << "\\papercolumns " << h_papercolumns << "\n"
757 << "\\papersides " << h_papersides << "\n"
758 << "\\paperpagestyle " << h_paperpagestyle << "\n";
759 if (!h_listings_params.empty())
760 os << "\\listings_params " << h_listings_params << "\n";
761 os << "\\tracking_changes " << h_tracking_changes << "\n"
762 << "\\output_changes " << h_output_changes << "\n"
763 << "\\html_math_output " << h_html_math_output << "\n"
764 << "\\html_css_as_file " << h_html_css_as_file << "\n"
765 << "\\html_be_strict " << h_html_be_strict << "\n"
766 << "\\end_header\n\n"
768 // clear preamble for subdocuments
772 } // anonymous namespace
775 void parse_preamble(Parser & p, ostream & os,
776 string const & forceclass, TeX2LyXDocClass & tc)
778 // initialize fixed types
779 special_columns['D'] = 3;
780 bool is_full_document = false;
781 bool is_lyx_file = false;
782 bool in_lyx_preamble = false;
784 // determine whether this is a full document or a fragment for inclusion
786 Token const & t = p.get_token();
788 if (t.cat() == catEscape && t.cs() == "documentclass") {
789 is_full_document = true;
795 while (is_full_document && p.good()) {
796 Token const & t = p.get_token();
799 cerr << "t: " << t << "\n";
805 if (!in_lyx_preamble &&
806 (t.cat() == catLetter ||
807 t.cat() == catSuper ||
809 t.cat() == catOther ||
810 t.cat() == catMath ||
811 t.cat() == catActive ||
812 t.cat() == catBegin ||
814 t.cat() == catAlign ||
815 t.cat() == catParameter))
816 h_preamble << t.cs();
818 else if (!in_lyx_preamble &&
819 (t.cat() == catSpace || t.cat() == catNewline))
820 h_preamble << t.asInput();
822 else if (t.cat() == catComment) {
823 static regex const islyxfile("%% LyX .* created this file");
824 static regex const usercommands("User specified LaTeX commands");
826 string const comment = t.asInput();
828 // magically switch encoding default if it looks like XeLaTeX
829 static string const magicXeLaTeX =
830 "% This document must be compiled with XeLaTeX ";
831 if (comment.size() > magicXeLaTeX.size()
832 && comment.substr(0, magicXeLaTeX.size()) == magicXeLaTeX
833 && h_inputencoding == "auto") {
834 cerr << "XeLaTeX comment found, switching to UTF8\n";
835 h_inputencoding = "utf8";
838 if (regex_search(comment, sub, islyxfile)) {
840 in_lyx_preamble = true;
841 } else if (is_lyx_file
842 && regex_search(comment, sub, usercommands))
843 in_lyx_preamble = false;
844 else if (!in_lyx_preamble)
845 h_preamble << t.asInput();
848 else if (t.cs() == "pagestyle")
849 h_paperpagestyle = p.verbatim_item();
851 else if (t.cs() == "makeatletter") {
852 // LyX takes care of this
853 p.setCatCode('@', catLetter);
856 else if (t.cs() == "makeatother") {
857 // LyX takes care of this
858 p.setCatCode('@', catOther);
861 else if (t.cs() == "newcommand" || t.cs() == "newcommandx"
862 || t.cs() == "renewcommand" || t.cs() == "renewcommandx"
863 || t.cs() == "providecommand" || t.cs() == "providecommandx"
864 || t.cs() == "DeclareRobustCommand"
865 || t.cs() == "DeclareRobustCommandx"
866 || t.cs() == "ProvideTextCommandDefault"
867 || t.cs() == "DeclareMathAccent") {
869 if (p.next_token().character() == '*') {
873 string const name = p.verbatim_item();
874 string const opt1 = p.getFullOpt();
875 string const opt2 = p.getFullOpt();
876 string const body = p.verbatim_item();
878 if (name == "\\rmdefault")
879 if (is_known(body, known_roman_fonts))
881 if (name == "\\sfdefault")
882 if (is_known(body, known_sans_fonts))
884 if (name == "\\ttdefault")
885 if (is_known(body, known_typewriter_fonts))
886 h_font_typewriter = body;
887 if (name == "\\familydefault") {
888 string family = body;
889 // remove leading "\"
890 h_font_default_family = family.erase(0,1);
893 // Add the command to the known commands
894 add_known_command(name, opt1, !opt2.empty(), from_utf8(body));
896 // only non-lyxspecific stuff
897 if (!in_lyx_preamble) {
899 ss << '\\' << t.cs();
902 ss << '{' << name << '}' << opt1 << opt2
903 << '{' << body << "}";
904 h_preamble << ss.str();
906 ostream & out = in_preamble ? h_preamble : os;
907 out << "\\" << t.cs() << "{" << name << "}"
908 << opts << "{" << body << "}";
913 else if (t.cs() == "documentclass") {
914 vector<string>::iterator it;
915 vector<string> opts = split_options(p.getArg('[', ']'));
916 handle_opt(opts, known_fontsizes, h_paperfontsize);
917 delete_opt(opts, known_fontsizes);
918 // delete "pt" at the end
919 string::size_type i = h_paperfontsize.find("pt");
920 if (i != string::npos)
921 h_paperfontsize.erase(i);
922 // The documentclass options are always parsed before the options
923 // of the babel call so that a language cannot overwrite the babel
925 handle_opt(opts, known_languages, h_language);
926 delete_opt(opts, known_languages);
929 if ((it = find(opts.begin(), opts.end(), "landscape")) != opts.end()) {
930 h_paperorientation = "landscape";
934 if ((it = find(opts.begin(), opts.end(), "oneside"))
939 if ((it = find(opts.begin(), opts.end(), "twoside"))
945 if ((it = find(opts.begin(), opts.end(), "onecolumn"))
947 h_papercolumns = "1";
950 if ((it = find(opts.begin(), opts.end(), "twocolumn"))
952 h_papercolumns = "2";
956 // some size options are know to any document classes, other sizes
957 // are handled by the \geometry command of the geometry package
958 handle_opt(opts, known_class_paper_sizes, h_papersize);
959 delete_opt(opts, known_class_paper_sizes);
960 // the remaining options
961 h_options = join(opts, ",");
962 // FIXME This does not work for classes that have a
963 // different name in LyX than in LaTeX
964 h_textclass = p.getArg('{', '}');
967 else if (t.cs() == "usepackage") {
968 string const options = p.getArg('[', ']');
969 string const name = p.getArg('{', '}');
970 vector<string> vecnames;
971 split(name, vecnames, ',');
972 vector<string>::const_iterator it = vecnames.begin();
973 vector<string>::const_iterator end = vecnames.end();
974 for (; it != end; ++it)
975 handle_package(p, trimSpaceAndEol(*it), options,
979 else if (t.cs() == "inputencoding") {
980 string const encoding = p.getArg('{','}');
981 h_inputencoding = encoding;
982 p.setEncoding(encoding);
985 else if (t.cs() == "newenvironment") {
986 string const name = p.getArg('{', '}');
987 string const opt1 = p.getFullOpt();
988 string const opt2 = p.getFullOpt();
989 string const beg = p.verbatim_item();
990 string const end = p.verbatim_item();
991 if (!in_lyx_preamble) {
992 h_preamble << "\\newenvironment{" << name
993 << '}' << opt1 << opt2 << '{'
994 << beg << "}{" << end << '}';
996 add_known_environment(name, opt1, !opt2.empty(),
997 from_utf8(beg), from_utf8(end));
1001 else if (t.cs() == "def") {
1002 string name = p.get_token().cs();
1003 while (p.next_token().cat() != catBegin)
1004 name += p.get_token().cs();
1005 if (!in_lyx_preamble)
1006 h_preamble << "\\def\\" << name << '{'
1007 << p.verbatim_item() << "}";
1010 else if (t.cs() == "newcolumntype") {
1011 string const name = p.getArg('{', '}');
1012 trimSpaceAndEol(name);
1014 string opts = p.getOpt();
1015 if (!opts.empty()) {
1016 istringstream is(string(opts, 1));
1019 special_columns[name[0]] = nargs;
1020 h_preamble << "\\newcolumntype{" << name << "}";
1022 h_preamble << "[" << nargs << "]";
1023 h_preamble << "{" << p.verbatim_item() << "}";
1026 else if (t.cs() == "setcounter") {
1027 string const name = p.getArg('{', '}');
1028 string const content = p.getArg('{', '}');
1029 if (name == "secnumdepth")
1030 h_secnumdepth = content;
1031 else if (name == "tocdepth")
1032 h_tocdepth = content;
1034 h_preamble << "\\setcounter{" << name << "}{" << content << "}";
1037 else if (t.cs() == "setlength") {
1038 string const name = p.verbatim_item();
1039 string const content = p.verbatim_item();
1040 // the paragraphs are only not indented when \parindent is set to zero
1041 if (name == "\\parindent" && content != "") {
1042 if (content[0] == '0')
1043 h_paragraph_separation = "skip";
1045 h_paragraph_indentation = translate_len(content);
1046 } else if (name == "\\parskip") {
1047 if (content == "\\smallskipamount")
1048 h_defskip = "smallskip";
1049 else if (content == "\\medskipamount")
1050 h_defskip = "medskip";
1051 else if (content == "\\bigskipamount")
1052 h_defskip = "bigskip";
1054 h_defskip = content;
1056 h_preamble << "\\setlength{" << name << "}{" << content << "}";
1059 else if (t.cs() == "onehalfspacing")
1060 h_spacing = "onehalf";
1062 else if (t.cs() == "doublespacing")
1063 h_spacing = "double";
1065 else if (t.cs() == "setstretch")
1066 h_spacing = "other " + p.verbatim_item();
1068 else if (t.cs() == "begin") {
1069 string const name = p.getArg('{', '}');
1070 if (name == "document")
1072 h_preamble << "\\begin{" << name << "}";
1075 else if (t.cs() == "geometry") {
1076 h_use_geometry = "true";
1077 vector<string> opts = split_options(p.getArg('{', '}'));
1078 vector<string>::iterator it;
1079 // paper orientation
1080 if ((it = find(opts.begin(), opts.end(), "landscape")) != opts.end()) {
1081 h_paperorientation = "landscape";
1085 handle_opt(opts, known_paper_sizes, h_papersize);
1086 delete_opt(opts, known_paper_sizes);
1088 char const * const * margin = known_paper_margins;
1090 for (; *margin; ++margin) {
1092 // search for the "=" in e.g. "lmargin=2cm" to get the value
1093 for(size_t i = 0; i != opts.size(); i++) {
1094 if (opts.at(i).find(*margin) != string::npos) {
1095 string::size_type pos = opts.at(i).find("=");
1096 string value = opts.at(i).substr(pos + 1);
1097 string name = known_coded_paper_margins[k];
1098 h_margins += "\\" + name + " " + value + "\n";
1104 else if (t.cs() == "definecolor") {
1105 string const color = p.getArg('{', '}');
1106 string const space = p.getArg('{', '}');
1107 string const value = p.getArg('{', '}');
1108 if (color == "note_fontcolor" && space == "rgb") {
1109 RGBColor c(RGBColorFromLaTeX(value));
1110 h_notefontcolor = X11hexname(c);
1112 h_preamble << "\\definecolor{" << color
1113 << "}{" << space << "}{" << value
1118 else if (t.cs() == "jurabibsetup") {
1119 // FIXME p.getArg('{', '}') is most probably wrong (it
1120 // does not handle nested braces).
1121 // Use p.verbatim_item() instead.
1122 vector<string> jurabibsetup =
1123 split_options(p.getArg('{', '}'));
1124 // add jurabibsetup to the jurabib package options
1125 add_package("jurabib", jurabibsetup);
1126 if (!jurabibsetup.empty()) {
1127 h_preamble << "\\jurabibsetup{"
1128 << join(jurabibsetup, ",") << '}';
1132 else if (t.cs() == "hypersetup") {
1133 vector<string> hypersetup =
1134 split_options(p.verbatim_item());
1135 // add hypersetup to the hyperref package options
1136 handle_hyperref(hypersetup);
1137 if (!hypersetup.empty()) {
1138 h_preamble << "\\hypersetup{"
1139 << join(hypersetup, ",") << '}';
1143 else if (is_known(t.cs(), known_if_3arg_commands)) {
1144 // prevent misparsing of \usepackage if it is used
1145 // as an argument (see e.g. our own output of
1146 // \@ifundefined above)
1147 string const arg1 = p.verbatim_item();
1148 string const arg2 = p.verbatim_item();
1149 string const arg3 = p.verbatim_item();
1150 if (!in_lyx_preamble) {
1151 h_preamble << t.asInput()
1152 << '{' << arg1 << '}'
1153 << '{' << arg2 << '}'
1154 << '{' << arg3 << '}';
1158 else if (is_known(t.cs(), known_if_commands)) {
1159 // must not parse anything in conditional code, since
1160 // LyX would output the parsed contents unconditionally
1161 if (!in_lyx_preamble)
1162 h_preamble << t.asInput();
1163 handle_if(p, in_lyx_preamble);
1166 else if (!t.cs().empty() && !in_lyx_preamble)
1167 h_preamble << '\\' << t.cs();
1170 // remove the whitespace
1173 // Force textclass if the user wanted it
1174 if (!forceclass.empty())
1175 h_textclass = forceclass;
1176 if (noweb_mode && !prefixIs(h_textclass, "literate-"))
1177 h_textclass.insert(0, "literate-");
1178 tc.setName(h_textclass);
1180 cerr << "Error: Could not read layout file for textclass \"" << h_textclass << "\"." << endl;
1183 if (h_papersides.empty()) {
1186 h_papersides = ss.str();
1188 end_preamble(os, tc);
1192 /// translates a babel language name to a LyX language name
1193 string babel2lyx(string const & language)
1195 char const * const * where = is_known(language, known_languages);
1197 return known_coded_languages[where - known_languages];