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:
56 // "armenian" (needs special handling since not supported by standard babel)
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, armenian, 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", "turkmen", "ukraineb", "ukrainian",
81 "uppersorbian", "UKenglish", "USenglish", "usorbian", "vietnam", "welsh",
85 * the same as known_languages with .lyx names
86 * please keep this in sync with known_languages line by line!
88 const char * const known_coded_languages[] = {"french", "afrikaans", "albanian",
89 "american", "arabic_arabi", "arabic_arabtex", "austrian", "bahasa", "bahasa", "bahasam",
90 "basque", "belarusian", "brazilian", "brazilian", "breton", "british", "bulgarian",
91 "canadian", "canadien", "catalan", "croatian", "czech", "danish", "dutch",
92 "english", "esperanto", "estonian", "farsi", "finnish", "french", "french",
93 "french", "french", "french", "galician", "german", "german", "greek",
94 "hebrew", "magyar", "icelandic", "bahasa", "bahasa", "interlingua",
95 "irish", "italian", "kazakh", "latin", "latvian", "lithuanian", "lowersorbian",
96 "lowersorbian", "magyar", "bahasam", "bahasam", "mongolian", "naustrian", "english",
97 "ngerman", "ngerman", "norsk", "nynorsk", "polutonikogreek", "polish",
98 "portuguese", "portuguese", "romanian", "russian", "russian", "samin",
99 "scottish", "serbian", "serbian-latin", "slovak", "slovene", "spanish",
100 "swedish", "thai", "turkish", "turkmen", "ukrainian", "ukrainian",
101 "uppersorbian", "uppersorbian", "english", "english", "vietnamese", "welsh",
104 /// languages with english quotes (.lyx names)
105 const char * const known_english_quotes_languages[] = {"american", "bahasa",
106 "bahasam", "brazilian", "canadian", "chinese-simplified", "english",
107 "esperanto", "hebrew", "irish", "korean", "portuguese", "scottish", "thai", 0};
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", "turkmen", "ukrainian",
116 /// languages with german quotes (.lyx names)
117 const char * const known_german_quotes_languages[] = {"austrian", "bulgarian",
118 "czech", "german", "icelandic", "lithuanian", "lowersorbian", "naustrian",
119 "ngerman", "serbian", "serbian-latin", "slovak", "slovene", "uppersorbian", 0};
121 /// languages with polish quotes (.lyx names)
122 const char * const known_polish_quotes_languages[] = {"afrikaans", "croatian",
123 "dutch", "estonian", "magyar", "polish", "romanian", 0};
125 /// languages with swedish quotes (.lyx names)
126 const char * const known_swedish_quotes_languages[] = {"finnish",
129 char const * const known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
131 const char * const known_roman_fonts[] = { "ae", "bookman", "charter",
132 "cmr", "fourier", "lmodern", "mathpazo", "mathptmx", "newcent", 0};
134 const char * const known_sans_fonts[] = { "avant", "berasans", "cmbr", "cmss",
135 "helvet", "lmss", 0};
137 const char * const known_typewriter_fonts[] = { "beramono", "cmtl", "cmtt",
138 "courier", "lmtt", "luximono", "fourier", "lmodern", "mathpazo", "mathptmx",
141 const char * const known_paper_sizes[] = { "a3paper", "b3paper", "a4paper",
142 "b4paper", "a5paper", "b5paper", "executivepaper", "legalpaper",
145 const char * const known_class_paper_sizes[] = { "a4paper", "a5paper",
146 "executivepaper", "legalpaper", "letterpaper", 0};
148 const char * const known_paper_margins[] = { "lmargin", "tmargin", "rmargin",
149 "bmargin", "headheight", "headsep", "footskip", "columnsep", 0};
151 const char * const known_coded_paper_margins[] = { "leftmargin", "topmargin",
152 "rightmargin", "bottommargin", "headheight", "headsep", "footskip",
155 /// commands that can start an \if...\else...\endif sequence
156 const char * const known_if_commands[] = {"if", "ifarydshln", "ifbraket",
157 "ifcancel", "ifcolortbl", "ifeurosym", "ifmarginnote", "ifmmode", "ifpdf",
158 "ifsidecap", "ifupgreek", 0};
160 /// conditional commands with three arguments like \@ifundefined{}{}{}
161 const char * const known_if_3arg_commands[] = {"@ifundefined", "IfFileExists",
165 ostringstream h_preamble;
166 string h_textclass = "article";
167 string h_use_default_options = "false";
169 string h_language = "english";
170 string h_font_roman = "default";
171 string h_font_sans = "default";
172 string h_font_typewriter = "default";
173 string h_font_default_family = "default";
174 string h_font_sc = "false";
175 string h_font_osf = "false";
176 string h_font_sf_scale = "100";
177 string h_font_tt_scale = "100";
178 string h_graphics = "default";
179 string h_float_placement;
180 string h_paperfontsize = "default";
181 string h_spacing = "single";
182 string h_use_hyperref = "0";
185 string h_pdf_subject;
186 string h_pdf_keywords;
187 string h_pdf_bookmarks = "1";
188 string h_pdf_bookmarksnumbered = "0";
189 string h_pdf_bookmarksopen = "0";
190 string h_pdf_bookmarksopenlevel = "1";
191 string h_pdf_breaklinks = "0";
192 string h_pdf_pdfborder = "0";
193 string h_pdf_colorlinks = "0";
194 string h_pdf_backref = "section";
195 string h_pdf_pdfusetitle = "1";
196 string h_pdf_pagemode;
197 string h_pdf_quoted_options;
198 string h_papersize = "default";
199 string h_use_geometry = "false";
200 string h_use_amsmath = "1";
201 string h_use_esint = "1";
202 string h_use_mhchem = "0";
203 string h_cite_engine = "basic";
204 string h_use_bibtopic = "false";
205 string h_paperorientation = "portrait";
206 string h_notefontcolor;
207 string h_secnumdepth = "3";
208 string h_tocdepth = "3";
209 string h_paragraph_separation = "indent";
210 string h_defskip = "medskip";
211 string h_paragraph_indentation = "default";
212 string h_quotes_language = "english";
213 string h_papercolumns = "1";
215 string h_paperpagestyle = "default";
216 string h_listings_params;
217 string h_tracking_changes = "false";
218 string h_output_changes = "false";
219 string h_html_math_output = "0";
220 string h_html_be_strict = "false";
224 // returns true if at least one of the options in what has been found
225 bool handle_opt(vector<string> & opts, char const * const * what, string & target)
231 // the last language option is the document language (for babel and LyX)
232 // the last size option is the document font size
233 vector<string>::iterator it;
234 vector<string>::iterator position = opts.begin();
235 for (; *what; ++what) {
236 it = find(opts.begin(), opts.end(), *what);
237 if (it != opts.end()) {
238 if (it >= position) {
249 void delete_opt(vector<string> & opts, char const * const * what)
254 // remove found options from the list
255 // do this after handle_opt to avoid potential memory leaks
256 vector<string>::iterator it;
257 for (; *what; ++what) {
258 it = find(opts.begin(), opts.end(), *what);
259 if (it != opts.end())
266 * Split a package options string (keyval format) into a vector.
268 * authorformat=smallcaps,
270 * titleformat=colonsep,
271 * bibformat={tabular,ibidem,numbered}
273 vector<string> split_options(string const & input)
275 vector<string> options;
279 Token const & t = p.get_token();
280 if (t.asInput() == ",") {
281 options.push_back(trimSpaceAndEol(option));
283 } else if (t.asInput() == "=") {
286 if (p.next_token().asInput() == "{")
287 option += '{' + p.getArg('{', '}') + '}';
288 } else if (t.cat() != catSpace)
289 option += t.asInput();
293 options.push_back(trimSpaceAndEol(option));
300 * Retrieve a keyval option "name={value with=sign}" named \p name from
301 * \p options and return the value.
302 * The found option is also removed from \p options.
304 string process_keyval_opt(vector<string> & options, string name)
306 for (size_t i = 0; i < options.size(); ++i) {
307 vector<string> option;
308 split(options[i], option, '=');
309 if (option.size() < 2)
311 if (option[0] == name) {
312 options.erase(options.begin() + i);
313 option.erase(option.begin());
314 return join(option, "=");
322 * Add package \p name with options \p options to used_packages.
323 * Remove options from \p options that we don't want to output.
325 void add_package(string const & name, vector<string> & options)
327 // every package inherits the global options
328 if (used_packages.find(name) == used_packages.end())
329 used_packages[name] = split_options(h_options);
331 vector<string> & v = used_packages[name];
332 v.insert(v.end(), options.begin(), options.end());
333 if (name == "jurabib") {
334 // Don't output the order argument (see the cite command
335 // handling code in text.cpp).
336 vector<string>::iterator end =
337 remove(options.begin(), options.end(), "natbiborder");
338 end = remove(options.begin(), end, "jurabiborder");
339 options.erase(end, options.end());
344 // Given is a string like "scaled=0.9", return 0.9 * 100
345 string const scale_as_percentage(string const & scale)
347 string::size_type pos = scale.find('=');
348 if (pos != string::npos) {
349 string value = scale.substr(pos + 1);
351 return convert<string>(100 * convert<double>(value));
353 // If the input string didn't match our expectations.
354 // return the default value "100"
359 string remove_braces(string const & value)
363 if (value[0] == '{' && value[value.length()-1] == '}')
364 return value.substr(1, value.length()-2);
369 void handle_hyperref(vector<string> & options)
371 // FIXME swallow inputencoding changes that might surround the
372 // hyperref setup if it was written by LyX
373 h_use_hyperref = "1";
374 // swallow "unicode=true", since LyX does always write that
375 vector<string>::iterator it =
376 find(options.begin(), options.end(), "unicode=true");
377 if (it != options.end())
379 it = find(options.begin(), options.end(), "pdfusetitle");
380 if (it != options.end()) {
381 h_pdf_pdfusetitle = "1";
384 string bookmarks = process_keyval_opt(options, "bookmarks");
385 if (bookmarks == "true")
386 h_pdf_bookmarks = "1";
387 else if (bookmarks == "false")
388 h_pdf_bookmarks = "0";
389 if (h_pdf_bookmarks == "1") {
390 string bookmarksnumbered =
391 process_keyval_opt(options, "bookmarksnumbered");
392 if (bookmarksnumbered == "true")
393 h_pdf_bookmarksnumbered = "1";
394 else if (bookmarksnumbered == "false")
395 h_pdf_bookmarksnumbered = "0";
396 string bookmarksopen =
397 process_keyval_opt(options, "bookmarksopen");
398 if (bookmarksopen == "true")
399 h_pdf_bookmarksopen = "1";
400 else if (bookmarksopen == "false")
401 h_pdf_bookmarksopen = "0";
402 if (h_pdf_bookmarksopen == "1") {
403 string bookmarksopenlevel =
404 process_keyval_opt(options, "bookmarksopenlevel");
405 if (!bookmarksopenlevel.empty())
406 h_pdf_bookmarksopenlevel = bookmarksopenlevel;
409 string breaklinks = process_keyval_opt(options, "breaklinks");
410 if (breaklinks == "true")
411 h_pdf_breaklinks = "1";
412 else if (breaklinks == "false")
413 h_pdf_breaklinks = "0";
414 string pdfborder = process_keyval_opt(options, "pdfborder");
415 if (pdfborder == "{0 0 0}")
416 h_pdf_pdfborder = "1";
417 else if (pdfborder == "{0 0 1}")
418 h_pdf_pdfborder = "0";
419 string backref = process_keyval_opt(options, "backref");
420 if (!backref.empty())
421 h_pdf_backref = backref;
422 string colorlinks = process_keyval_opt(options, "colorlinks");
423 if (colorlinks == "true")
424 h_pdf_colorlinks = "1";
425 else if (colorlinks == "false")
426 h_pdf_colorlinks = "0";
427 string pdfpagemode = process_keyval_opt(options, "pdfpagemode");
428 if (!pdfpagemode.empty())
429 h_pdf_pagemode = pdfpagemode;
430 string pdftitle = process_keyval_opt(options, "pdftitle");
431 if (!pdftitle.empty()) {
432 h_pdf_title = remove_braces(pdftitle);
434 string pdfauthor = process_keyval_opt(options, "pdfauthor");
435 if (!pdfauthor.empty()) {
436 h_pdf_author = remove_braces(pdfauthor);
438 string pdfsubject = process_keyval_opt(options, "pdfsubject");
439 if (!pdfsubject.empty())
440 h_pdf_subject = remove_braces(pdfsubject);
441 string pdfkeywords = process_keyval_opt(options, "pdfkeywords");
442 if (!pdfkeywords.empty())
443 h_pdf_keywords = remove_braces(pdfkeywords);
444 if (!options.empty()) {
445 if (!h_pdf_quoted_options.empty())
446 h_pdf_quoted_options += ',';
447 h_pdf_quoted_options += join(options, ",");
453 void handle_package(Parser &p, string const & name, string const & opts,
454 bool in_lyx_preamble)
456 vector<string> options = split_options(opts);
457 add_package(name, options);
461 if (is_known(name, known_roman_fonts)) {
466 if (name == "fourier") {
467 h_font_roman = "utopia";
468 // when font uses real small capitals
469 if (opts == "expert")
473 if (name == "mathpazo")
474 h_font_roman = "palatino";
476 if (name == "mathptmx")
477 h_font_roman = "times";
480 if (is_known(name, known_sans_fonts)) {
484 h_font_sf_scale = scale_as_percentage(scale);
489 if (is_known(name, known_typewriter_fonts)) {
490 h_font_typewriter = name;
493 h_font_tt_scale = scale_as_percentage(scale);
497 // font uses old-style figure
501 else if (name == "amsmath" || name == "amssymb")
504 else if (name == "esint")
507 else if (name == "mhchem")
510 else if (name == "babel" && !opts.empty()) {
511 // check if more than one option was used - used later for inputenc
512 // in case inputenc is parsed before babel, set the encoding to auto
513 if (options.begin() != options.end() - 1) {
514 one_language = false;
515 h_inputencoding = "auto";
517 // babel takes the last language of the option of its \usepackage
518 // call as document language. If there is no such language option, the
519 // last language in the documentclass options is used.
520 handle_opt(options, known_languages, h_language);
521 delete_opt(options, known_languages);
524 else if (name == "fontenc")
527 else if (name == "inputenc" || name == "luainputenc") {
528 // h_inputencoding is only set when there is not more than one
529 // inputenc option because otherwise h_inputencoding must be
530 // set to "auto" (the default encoding of the document language)
531 // Therefore check for the "," character.
532 // It is also only set when there is not more then one babel
533 // language option but this is handled in the routine for babel.
534 if (opts.find(",") == string::npos && one_language == true)
535 h_inputencoding = opts;
536 if (!options.empty())
537 p.setEncoding(options.back());
541 else if (name == "makeidx")
544 else if (name == "prettyref")
547 else if (name == "varioref")
550 else if (name == "verbatim")
553 else if (name == "nomencl")
556 else if (name == "textcomp")
559 else if (name == "url")
562 else if (LYX_FORMAT >= 408 && name == "subscript")
565 else if (name == "color") {
566 // with the following command this package is only loaded when needed for
567 // undefined colors, since we only support the predefined colors
568 h_preamble << "\\@ifundefined{definecolor}\n {\\usepackage{color}}{}\n";
571 else if (name == "graphicx")
574 else if (name == "setspace")
577 else if (name == "geometry")
578 ; // Ignore this, the geometry settings are made by the \geometry
579 // command. This command is handled below.
581 else if (is_known(name, known_languages))
584 else if (name == "natbib") {
585 h_cite_engine = "natbib_authoryear";
586 vector<string>::iterator it =
587 find(options.begin(), options.end(), "authoryear");
588 if (it != options.end())
591 it = find(options.begin(), options.end(), "numbers");
592 if (it != options.end()) {
593 h_cite_engine = "natbib_numerical";
599 else if (name == "jurabib")
600 h_cite_engine = "jurabib";
602 else if (name == "hyperref")
603 handle_hyperref(options);
605 else if (!in_lyx_preamble) {
607 h_preamble << "\\usepackage{" << name << "}";
609 h_preamble << "\\usepackage[" << opts << "]{"
615 // We need to do something with the options...
616 if (!options.empty())
617 cerr << "Ignoring options '" << join(options, ",")
618 << "' of package " << name << '.' << endl;
620 // remove the whitespace
625 void handle_if(Parser & p, bool in_lyx_preamble)
628 Token t = p.get_token();
629 if (t.cat() == catEscape &&
630 is_known(t.cs(), known_if_commands))
631 handle_if(p, in_lyx_preamble);
633 if (!in_lyx_preamble)
634 h_preamble << t.asInput();
635 if (t.cat() == catEscape && t.cs() == "fi")
642 void end_preamble(ostream & os, TextClass const & /*textclass*/)
644 // translate from babel to LyX names
645 h_language = babel2lyx(h_language);
647 // set the quote language
648 // LyX only knows the following quotes languages:
649 // english, swedish, german, polish, french and danish
650 // (quotes for "japanese" and "chinese-traditional" are missing because
651 // they wouldn't be useful: http://www.lyx.org/trac/ticket/6383)
652 // conversion list taken from
653 // http://en.wikipedia.org/wiki/Quotation_mark,_non-English_usage
654 // (quotes for kazakh and interlingua are unknown)
656 if (h_language == "danish")
657 h_quotes_language = "danish";
659 else if (is_known(h_language, known_french_quotes_languages))
660 h_quotes_language = "french";
662 else if (is_known(h_language, known_german_quotes_languages))
663 h_quotes_language = "german";
665 else if (is_known(h_language, known_polish_quotes_languages))
666 h_quotes_language = "polish";
668 else if (is_known(h_language, known_swedish_quotes_languages))
669 h_quotes_language = "swedish";
671 else if (is_known(h_language, known_english_quotes_languages))
672 h_quotes_language = "english";
674 // output the LyX file settings
675 os << "#LyX file created by tex2lyx " << PACKAGE_VERSION << "\n"
676 << "\\lyxformat " << LYX_FORMAT << '\n'
677 << "\\begin_document\n"
678 << "\\begin_header\n"
679 << "\\textclass " << h_textclass << "\n";
680 if (!h_preamble.str().empty())
681 os << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n";
682 if (!h_options.empty())
683 os << "\\options " << h_options << "\n";
684 os << "\\use_default_options " << h_use_default_options << "\n"
685 << modules_placeholder
686 << "\\language " << h_language << "\n"
687 << "\\inputencoding " << h_inputencoding << "\n"
688 << "\\font_roman " << h_font_roman << "\n"
689 << "\\font_sans " << h_font_sans << "\n"
690 << "\\font_typewriter " << h_font_typewriter << "\n"
691 << "\\font_default_family " << h_font_default_family << "\n"
692 << "\\font_sc " << h_font_sc << "\n"
693 << "\\font_osf " << h_font_osf << "\n"
694 << "\\font_sf_scale " << h_font_sf_scale << "\n"
695 << "\\font_tt_scale " << h_font_tt_scale << "\n"
696 << "\\graphics " << h_graphics << "\n";
697 if (!h_float_placement.empty())
698 os << "\\float_placement " << h_float_placement << "\n";
699 os << "\\paperfontsize " << h_paperfontsize << "\n"
700 << "\\spacing " << h_spacing << "\n"
701 << "\\use_hyperref " << h_use_hyperref << '\n';
702 if (h_use_hyperref == "1") {
703 if (!h_pdf_title.empty())
704 os << "\\pdf_title \"" << h_pdf_title << "\"\n";
705 if (!h_pdf_author.empty())
706 os << "\\pdf_author \"" << h_pdf_author << "\"\n";
707 if (!h_pdf_subject.empty())
708 os << "\\pdf_subject \"" << h_pdf_subject << "\"\n";
709 if (!h_pdf_keywords.empty())
710 os << "\\pdf_keywords \"" << h_pdf_keywords << "\"\n";
711 os << "\\pdf_bookmarks " << h_pdf_bookmarks << "\n"
712 "\\pdf_bookmarksnumbered " << h_pdf_bookmarksnumbered << "\n"
713 "\\pdf_bookmarksopen " << h_pdf_bookmarksopen << "\n"
714 "\\pdf_bookmarksopenlevel " << h_pdf_bookmarksopenlevel << "\n"
715 "\\pdf_breaklinks " << h_pdf_breaklinks << "\n"
716 "\\pdf_pdfborder " << h_pdf_pdfborder << "\n"
717 "\\pdf_colorlinks " << h_pdf_colorlinks << "\n"
718 "\\pdf_backref " << h_pdf_backref << "\n"
719 "\\pdf_pdfusetitle " << h_pdf_pdfusetitle << '\n';
720 if (!h_pdf_pagemode.empty())
721 os << "\\pdf_pagemode " << h_pdf_pagemode << '\n';
722 if (!h_pdf_quoted_options.empty())
723 os << "\\pdf_quoted_options \"" << h_pdf_quoted_options << "\"\n";
725 os << "\\papersize " << h_papersize << "\n"
726 << "\\use_geometry " << h_use_geometry << "\n"
727 << "\\use_amsmath " << h_use_amsmath << "\n"
728 << "\\use_esint " << h_use_esint << "\n"
729 << "\\use_mhchem " << h_use_mhchem << "\n"
730 << "\\cite_engine " << h_cite_engine << "\n"
731 << "\\use_bibtopic " << h_use_bibtopic << "\n"
732 << "\\paperorientation " << h_paperorientation << '\n';
733 if (!h_notefontcolor.empty())
734 os << "\\notefontcolor " << h_notefontcolor << '\n';
736 << "\\secnumdepth " << h_secnumdepth << "\n"
737 << "\\tocdepth " << h_tocdepth << "\n"
738 << "\\paragraph_separation " << h_paragraph_separation << "\n";
739 if (h_paragraph_separation == "skip")
740 os << "\\defskip " << h_defskip << "\n";
742 os << "\\paragraph_indentation " << h_paragraph_indentation << "\n";
743 os << "\\quotes_language " << h_quotes_language << "\n"
744 << "\\papercolumns " << h_papercolumns << "\n"
745 << "\\papersides " << h_papersides << "\n"
746 << "\\paperpagestyle " << h_paperpagestyle << "\n";
747 if (!h_listings_params.empty())
748 os << "\\listings_params " << h_listings_params << "\n";
749 os << "\\tracking_changes " << h_tracking_changes << "\n"
750 << "\\output_changes " << h_output_changes << "\n"
751 << "\\html_math_output " << h_html_math_output << "\n"
752 << "\\html_be_strict " << h_html_be_strict << "\n"
753 << "\\end_header\n\n"
755 // clear preamble for subdocuments
759 } // anonymous namespace
762 void parse_preamble(Parser & p, ostream & os,
763 string const & forceclass, TeX2LyXDocClass & tc)
765 // initialize fixed types
766 special_columns['D'] = 3;
767 bool is_full_document = false;
768 bool is_lyx_file = false;
769 bool in_lyx_preamble = false;
771 // determine whether this is a full document or a fragment for inclusion
773 Token const & t = p.get_token();
775 if (t.cat() == catEscape && t.cs() == "documentclass") {
776 is_full_document = true;
782 while (is_full_document && p.good()) {
783 Token const & t = p.get_token();
786 cerr << "t: " << t << "\n";
792 if (!in_lyx_preamble &&
793 (t.cat() == catLetter ||
794 t.cat() == catSuper ||
796 t.cat() == catOther ||
797 t.cat() == catMath ||
798 t.cat() == catActive ||
799 t.cat() == catBegin ||
801 t.cat() == catAlign ||
802 t.cat() == catParameter))
803 h_preamble << t.cs();
805 else if (!in_lyx_preamble &&
806 (t.cat() == catSpace || t.cat() == catNewline))
807 h_preamble << t.asInput();
809 else if (t.cat() == catComment) {
810 static regex const islyxfile("%% LyX .* created this file");
811 static regex const usercommands("User specified LaTeX commands");
813 string const comment = t.asInput();
815 // magically switch encoding default if it looks like XeLaTeX
816 static string const magicXeLaTeX =
817 "% This document must be compiled with XeLaTeX ";
818 if (comment.size() > magicXeLaTeX.size()
819 && comment.substr(0, magicXeLaTeX.size()) == magicXeLaTeX
820 && h_inputencoding == "auto") {
821 cerr << "XeLaTeX comment found, switching to UTF8\n";
822 h_inputencoding = "utf8";
825 if (regex_search(comment, sub, islyxfile)) {
827 in_lyx_preamble = true;
828 } else if (is_lyx_file
829 && regex_search(comment, sub, usercommands))
830 in_lyx_preamble = false;
831 else if (!in_lyx_preamble)
832 h_preamble << t.asInput();
835 else if (t.cs() == "pagestyle")
836 h_paperpagestyle = p.verbatim_item();
838 else if (t.cs() == "makeatletter") {
839 // LyX takes care of this
840 p.setCatCode('@', catLetter);
843 else if (t.cs() == "makeatother") {
844 // LyX takes care of this
845 p.setCatCode('@', catOther);
848 else if (t.cs() == "newcommand" || t.cs() == "newcommandx"
849 || t.cs() == "renewcommand" || t.cs() == "renewcommandx"
850 || t.cs() == "providecommand" || t.cs() == "providecommandx"
851 || t.cs() == "DeclareRobustCommand"
852 || t.cs() == "DeclareRobustCommandx"
853 || t.cs() == "ProvideTextCommandDefault"
854 || t.cs() == "DeclareMathAccent") {
856 if (p.next_token().character() == '*') {
860 string const name = p.verbatim_item();
861 string const opt1 = p.getFullOpt();
862 string const opt2 = p.getFullOpt();
863 string const body = p.verbatim_item();
865 if (name == "\\rmdefault")
866 if (is_known(body, known_roman_fonts))
868 if (name == "\\sfdefault")
869 if (is_known(body, known_sans_fonts))
871 if (name == "\\ttdefault")
872 if (is_known(body, known_typewriter_fonts))
873 h_font_typewriter = body;
874 if (name == "\\familydefault") {
875 string family = body;
876 // remove leading "\"
877 h_font_default_family = family.erase(0,1);
880 // Add the command to the known commands
881 add_known_command(name, opt1, !opt2.empty(), from_utf8(body));
883 // only non-lyxspecific stuff
884 if (!in_lyx_preamble) {
886 ss << '\\' << t.cs();
889 ss << '{' << name << '}' << opt1 << opt2
890 << '{' << body << "}";
891 h_preamble << ss.str();
893 ostream & out = in_preamble ? h_preamble : os;
894 out << "\\" << t.cs() << "{" << name << "}"
895 << opts << "{" << body << "}";
900 else if (t.cs() == "documentclass") {
901 vector<string>::iterator it;
902 vector<string> opts = split_options(p.getArg('[', ']'));
903 handle_opt(opts, known_fontsizes, h_paperfontsize);
904 delete_opt(opts, known_fontsizes);
905 // delete "pt" at the end
906 string::size_type i = h_paperfontsize.find("pt");
907 if (i != string::npos)
908 h_paperfontsize.erase(i);
909 // The documentclass options are always parsed before the options
910 // of the babel call so that a language cannot overwrite the babel
912 handle_opt(opts, known_languages, h_language);
913 delete_opt(opts, known_languages);
916 if ((it = find(opts.begin(), opts.end(), "landscape")) != opts.end()) {
917 h_paperorientation = "landscape";
921 if ((it = find(opts.begin(), opts.end(), "oneside"))
926 if ((it = find(opts.begin(), opts.end(), "twoside"))
932 if ((it = find(opts.begin(), opts.end(), "onecolumn"))
934 h_papercolumns = "1";
937 if ((it = find(opts.begin(), opts.end(), "twocolumn"))
939 h_papercolumns = "2";
943 // some size options are know to any document classes, other sizes
944 // are handled by the \geometry command of the geometry package
945 handle_opt(opts, known_class_paper_sizes, h_papersize);
946 delete_opt(opts, known_class_paper_sizes);
947 // the remaining options
948 h_options = join(opts, ",");
949 // FIXME This does not work for classes that have a
950 // different name in LyX than in LaTeX
951 h_textclass = p.getArg('{', '}');
954 else if (t.cs() == "usepackage") {
955 string const options = p.getArg('[', ']');
956 string const name = p.getArg('{', '}');
957 vector<string> vecnames;
958 split(name, vecnames, ',');
959 vector<string>::const_iterator it = vecnames.begin();
960 vector<string>::const_iterator end = vecnames.end();
961 for (; it != end; ++it)
962 handle_package(p, trimSpaceAndEol(*it), options,
966 else if (t.cs() == "inputencoding") {
967 string const encoding = p.getArg('{','}');
968 h_inputencoding = encoding;
969 p.setEncoding(encoding);
972 else if (t.cs() == "newenvironment") {
973 string const name = p.getArg('{', '}');
974 string const opt1 = p.getFullOpt();
975 string const opt2 = p.getFullOpt();
976 string const beg = p.verbatim_item();
977 string const end = p.verbatim_item();
978 if (!in_lyx_preamble) {
979 h_preamble << "\\newenvironment{" << name
980 << '}' << opt1 << opt2 << '{'
981 << beg << "}{" << end << '}';
983 add_known_environment(name, opt1, !opt2.empty(),
984 from_utf8(beg), from_utf8(end));
988 else if (t.cs() == "def") {
989 string name = p.get_token().cs();
990 while (p.next_token().cat() != catBegin)
991 name += p.get_token().cs();
992 if (!in_lyx_preamble)
993 h_preamble << "\\def\\" << name << '{'
994 << p.verbatim_item() << "}";
997 else if (t.cs() == "newcolumntype") {
998 string const name = p.getArg('{', '}');
999 trimSpaceAndEol(name);
1001 string opts = p.getOpt();
1002 if (!opts.empty()) {
1003 istringstream is(string(opts, 1));
1006 special_columns[name[0]] = nargs;
1007 h_preamble << "\\newcolumntype{" << name << "}";
1009 h_preamble << "[" << nargs << "]";
1010 h_preamble << "{" << p.verbatim_item() << "}";
1013 else if (t.cs() == "setcounter") {
1014 string const name = p.getArg('{', '}');
1015 string const content = p.getArg('{', '}');
1016 if (name == "secnumdepth")
1017 h_secnumdepth = content;
1018 else if (name == "tocdepth")
1019 h_tocdepth = content;
1021 h_preamble << "\\setcounter{" << name << "}{" << content << "}";
1024 else if (t.cs() == "setlength") {
1025 string const name = p.verbatim_item();
1026 string const content = p.verbatim_item();
1027 // the paragraphs are only not indented when \parindent is set to zero
1028 if (name == "\\parindent" && content != "") {
1029 if (content[0] == '0')
1030 h_paragraph_separation = "skip";
1032 h_paragraph_indentation = translate_len(content);
1033 } else if (name == "\\parskip") {
1034 if (content == "\\smallskipamount")
1035 h_defskip = "smallskip";
1036 else if (content == "\\medskipamount")
1037 h_defskip = "medskip";
1038 else if (content == "\\bigskipamount")
1039 h_defskip = "bigskip";
1041 h_defskip = content;
1043 h_preamble << "\\setlength{" << name << "}{" << content << "}";
1046 else if (t.cs() == "onehalfspacing")
1047 h_spacing = "onehalf";
1049 else if (t.cs() == "doublespacing")
1050 h_spacing = "double";
1052 else if (t.cs() == "setstretch")
1053 h_spacing = "other " + p.verbatim_item();
1055 else if (t.cs() == "begin") {
1056 string const name = p.getArg('{', '}');
1057 if (name == "document")
1059 h_preamble << "\\begin{" << name << "}";
1062 else if (t.cs() == "geometry") {
1063 h_use_geometry = "true";
1064 vector<string> opts = split_options(p.getArg('{', '}'));
1065 vector<string>::iterator it;
1066 // paper orientation
1067 if ((it = find(opts.begin(), opts.end(), "landscape")) != opts.end()) {
1068 h_paperorientation = "landscape";
1072 handle_opt(opts, known_paper_sizes, h_papersize);
1073 delete_opt(opts, known_paper_sizes);
1075 char const * const * margin = known_paper_margins;
1077 for (; *margin; ++margin) {
1079 // search for the "=" in e.g. "lmargin=2cm" to get the value
1080 for(size_t i = 0; i != opts.size(); i++) {
1081 if (opts.at(i).find(*margin) != string::npos) {
1082 string::size_type pos = opts.at(i).find("=");
1083 string value = opts.at(i).substr(pos + 1);
1084 string name = known_coded_paper_margins[k];
1085 h_margins += "\\" + name + " " + value + "\n";
1091 else if (t.cs() == "definecolor") {
1092 string const color = p.getArg('{', '}');
1093 string const space = p.getArg('{', '}');
1094 string const value = p.getArg('{', '}');
1095 if (color == "note_fontcolor" && space == "rgb") {
1096 RGBColor c(RGBColorFromLaTeX(value));
1097 h_notefontcolor = X11hexname(c);
1099 h_preamble << "\\definecolor{" << color
1100 << "}{" << space << "}{" << value
1105 else if (t.cs() == "jurabibsetup") {
1106 // FIXME p.getArg('{', '}') is most probably wrong (it
1107 // does not handle nested braces).
1108 // Use p.verbatim_item() instead.
1109 vector<string> jurabibsetup =
1110 split_options(p.getArg('{', '}'));
1111 // add jurabibsetup to the jurabib package options
1112 add_package("jurabib", jurabibsetup);
1113 if (!jurabibsetup.empty()) {
1114 h_preamble << "\\jurabibsetup{"
1115 << join(jurabibsetup, ",") << '}';
1119 else if (t.cs() == "hypersetup") {
1120 vector<string> hypersetup =
1121 split_options(p.verbatim_item());
1122 // add hypersetup to the hyperref package options
1123 handle_hyperref(hypersetup);
1124 if (!hypersetup.empty()) {
1125 h_preamble << "\\hypersetup{"
1126 << join(hypersetup, ",") << '}';
1130 else if (is_known(t.cs(), known_if_3arg_commands)) {
1131 // prevent misparsing of \usepackage if it is used
1132 // as an argument (see e.g. our own output of
1133 // \@ifundefined above)
1134 string const arg1 = p.verbatim_item();
1135 string const arg2 = p.verbatim_item();
1136 string const arg3 = p.verbatim_item();
1137 if (!in_lyx_preamble) {
1138 h_preamble << t.asInput()
1139 << '{' << arg1 << '}'
1140 << '{' << arg2 << '}'
1141 << '{' << arg3 << '}';
1145 else if (is_known(t.cs(), known_if_commands)) {
1146 // must not parse anything in conditional code, since
1147 // LyX would output the parsed contents unconditionally
1148 if (!in_lyx_preamble)
1149 h_preamble << t.asInput();
1150 handle_if(p, in_lyx_preamble);
1153 else if (!t.cs().empty() && !in_lyx_preamble)
1154 h_preamble << '\\' << t.cs();
1157 // remove the whitespace
1160 // Force textclass if the user wanted it
1161 if (!forceclass.empty())
1162 h_textclass = forceclass;
1163 if (noweb_mode && !prefixIs(h_textclass, "literate-"))
1164 h_textclass.insert(0, "literate-");
1165 tc.setName(h_textclass);
1167 cerr << "Error: Could not read layout file for textclass \"" << h_textclass << "\"." << endl;
1170 if (h_papersides.empty()) {
1173 h_papersides = ss.str();
1175 end_preamble(os, tc);
1179 /// translates a babel language name to a LyX language name
1180 string babel2lyx(string const & language)
1182 char const * const * where = is_known(language, known_languages);
1184 return known_coded_languages[where - known_languages];