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_font_roman = "default";
172 string h_font_sans = "default";
173 string h_font_typewriter = "default";
174 string h_font_default_family = "default";
175 string h_font_sc = "false";
176 string h_font_osf = "false";
177 string h_font_sf_scale = "100";
178 string h_font_tt_scale = "100";
179 string h_graphics = "default";
180 string h_float_placement;
181 string h_paperfontsize = "default";
182 string h_spacing = "single";
183 string h_use_hyperref = "0";
186 string h_pdf_subject;
187 string h_pdf_keywords;
188 string h_pdf_bookmarks = "1";
189 string h_pdf_bookmarksnumbered = "0";
190 string h_pdf_bookmarksopen = "0";
191 string h_pdf_bookmarksopenlevel = "1";
192 string h_pdf_breaklinks = "0";
193 string h_pdf_pdfborder = "0";
194 string h_pdf_colorlinks = "0";
195 string h_pdf_backref = "section";
196 string h_pdf_pdfusetitle = "1";
197 string h_pdf_pagemode;
198 string h_pdf_quoted_options;
199 string h_papersize = "default";
200 string h_use_geometry = "false";
201 string h_use_amsmath = "1";
202 string h_use_esint = "1";
203 string h_use_mhchem = "0";
204 string h_use_mathdots = "0";
205 string h_cite_engine = "basic";
206 string h_use_bibtopic = "false";
207 string h_paperorientation = "portrait";
208 string h_suppress_date = "false";
209 string h_use_refstyle = "0";
210 string h_notefontcolor;
211 string h_secnumdepth = "3";
212 string h_tocdepth = "3";
213 string h_defskip = "medskip";
214 string h_paragraph_indentation = "default";
215 string h_quotes_language = "english";
216 string h_papercolumns = "1";
218 string h_paperpagestyle = "default";
219 string h_listings_params;
220 string h_tracking_changes = "false";
221 string h_output_changes = "false";
222 string h_html_math_output = "0";
223 string h_html_be_strict = "false";
227 // returns true if at least one of the options in what has been found
228 bool handle_opt(vector<string> & opts, char const * const * what, string & target)
234 // the last language option is the document language (for babel and LyX)
235 // the last size option is the document font size
236 vector<string>::iterator it;
237 vector<string>::iterator position = opts.begin();
238 for (; *what; ++what) {
239 it = find(opts.begin(), opts.end(), *what);
240 if (it != opts.end()) {
241 if (it >= position) {
252 void delete_opt(vector<string> & opts, char const * const * what)
257 // remove found options from the list
258 // do this after handle_opt to avoid potential memory leaks
259 vector<string>::iterator it;
260 for (; *what; ++what) {
261 it = find(opts.begin(), opts.end(), *what);
262 if (it != opts.end())
269 * Split a package options string (keyval format) into a vector.
271 * authorformat=smallcaps,
273 * titleformat=colonsep,
274 * bibformat={tabular,ibidem,numbered}
276 vector<string> split_options(string const & input)
278 vector<string> options;
282 Token const & t = p.get_token();
283 if (t.asInput() == ",") {
284 options.push_back(trimSpaceAndEol(option));
286 } else if (t.asInput() == "=") {
289 if (p.next_token().asInput() == "{")
290 option += '{' + p.getArg('{', '}') + '}';
291 } else if (t.cat() != catSpace)
292 option += t.asInput();
296 options.push_back(trimSpaceAndEol(option));
303 * Retrieve a keyval option "name={value with=sign}" named \p name from
304 * \p options and return the value.
305 * The found option is also removed from \p options.
307 string process_keyval_opt(vector<string> & options, string name)
309 for (size_t i = 0; i < options.size(); ++i) {
310 vector<string> option;
311 split(options[i], option, '=');
312 if (option.size() < 2)
314 if (option[0] == name) {
315 options.erase(options.begin() + i);
316 option.erase(option.begin());
317 return join(option, "=");
325 * Add package \p name with options \p options to used_packages.
326 * Remove options from \p options that we don't want to output.
328 void add_package(string const & name, vector<string> & options)
330 // every package inherits the global options
331 if (used_packages.find(name) == used_packages.end())
332 used_packages[name] = split_options(h_options);
334 vector<string> & v = used_packages[name];
335 v.insert(v.end(), options.begin(), options.end());
336 if (name == "jurabib") {
337 // Don't output the order argument (see the cite command
338 // handling code in text.cpp).
339 vector<string>::iterator end =
340 remove(options.begin(), options.end(), "natbiborder");
341 end = remove(options.begin(), end, "jurabiborder");
342 options.erase(end, options.end());
347 // Given is a string like "scaled=0.9", return 0.9 * 100
348 string const scale_as_percentage(string const & scale)
350 string::size_type pos = scale.find('=');
351 if (pos != string::npos) {
352 string value = scale.substr(pos + 1);
354 return convert<string>(100 * convert<double>(value));
356 // If the input string didn't match our expectations.
357 // return the default value "100"
362 string remove_braces(string const & value)
366 if (value[0] == '{' && value[value.length()-1] == '}')
367 return value.substr(1, value.length()-2);
372 void handle_hyperref(vector<string> & options)
374 // FIXME swallow inputencoding changes that might surround the
375 // hyperref setup if it was written by LyX
376 h_use_hyperref = "1";
377 // swallow "unicode=true", since LyX does always write that
378 vector<string>::iterator it =
379 find(options.begin(), options.end(), "unicode=true");
380 if (it != options.end())
382 it = find(options.begin(), options.end(), "pdfusetitle");
383 if (it != options.end()) {
384 h_pdf_pdfusetitle = "1";
387 string bookmarks = process_keyval_opt(options, "bookmarks");
388 if (bookmarks == "true")
389 h_pdf_bookmarks = "1";
390 else if (bookmarks == "false")
391 h_pdf_bookmarks = "0";
392 if (h_pdf_bookmarks == "1") {
393 string bookmarksnumbered =
394 process_keyval_opt(options, "bookmarksnumbered");
395 if (bookmarksnumbered == "true")
396 h_pdf_bookmarksnumbered = "1";
397 else if (bookmarksnumbered == "false")
398 h_pdf_bookmarksnumbered = "0";
399 string bookmarksopen =
400 process_keyval_opt(options, "bookmarksopen");
401 if (bookmarksopen == "true")
402 h_pdf_bookmarksopen = "1";
403 else if (bookmarksopen == "false")
404 h_pdf_bookmarksopen = "0";
405 if (h_pdf_bookmarksopen == "1") {
406 string bookmarksopenlevel =
407 process_keyval_opt(options, "bookmarksopenlevel");
408 if (!bookmarksopenlevel.empty())
409 h_pdf_bookmarksopenlevel = bookmarksopenlevel;
412 string breaklinks = process_keyval_opt(options, "breaklinks");
413 if (breaklinks == "true")
414 h_pdf_breaklinks = "1";
415 else if (breaklinks == "false")
416 h_pdf_breaklinks = "0";
417 string pdfborder = process_keyval_opt(options, "pdfborder");
418 if (pdfborder == "{0 0 0}")
419 h_pdf_pdfborder = "1";
420 else if (pdfborder == "{0 0 1}")
421 h_pdf_pdfborder = "0";
422 string backref = process_keyval_opt(options, "backref");
423 if (!backref.empty())
424 h_pdf_backref = backref;
425 string colorlinks = process_keyval_opt(options, "colorlinks");
426 if (colorlinks == "true")
427 h_pdf_colorlinks = "1";
428 else if (colorlinks == "false")
429 h_pdf_colorlinks = "0";
430 string pdfpagemode = process_keyval_opt(options, "pdfpagemode");
431 if (!pdfpagemode.empty())
432 h_pdf_pagemode = pdfpagemode;
433 string pdftitle = process_keyval_opt(options, "pdftitle");
434 if (!pdftitle.empty()) {
435 h_pdf_title = remove_braces(pdftitle);
437 string pdfauthor = process_keyval_opt(options, "pdfauthor");
438 if (!pdfauthor.empty()) {
439 h_pdf_author = remove_braces(pdfauthor);
441 string pdfsubject = process_keyval_opt(options, "pdfsubject");
442 if (!pdfsubject.empty())
443 h_pdf_subject = remove_braces(pdfsubject);
444 string pdfkeywords = process_keyval_opt(options, "pdfkeywords");
445 if (!pdfkeywords.empty())
446 h_pdf_keywords = remove_braces(pdfkeywords);
447 if (!options.empty()) {
448 if (!h_pdf_quoted_options.empty())
449 h_pdf_quoted_options += ',';
450 h_pdf_quoted_options += join(options, ",");
456 void handle_package(Parser &p, string const & name, string const & opts,
457 bool in_lyx_preamble)
459 vector<string> options = split_options(opts);
460 add_package(name, options);
464 if (is_known(name, known_roman_fonts)) {
469 if (name == "fourier") {
470 h_font_roman = "utopia";
471 // when font uses real small capitals
472 if (opts == "expert")
476 if (name == "mathpazo")
477 h_font_roman = "palatino";
479 if (name == "mathptmx")
480 h_font_roman = "times";
483 if (is_known(name, known_sans_fonts)) {
487 h_font_sf_scale = scale_as_percentage(scale);
492 if (is_known(name, known_typewriter_fonts)) {
493 h_font_typewriter = name;
496 h_font_tt_scale = scale_as_percentage(scale);
500 // font uses old-style figure
504 else if (name == "amsmath" || name == "amssymb")
507 else if (name == "esint")
510 else if (name == "mhchem")
513 else if (name == "mathdots")
514 h_use_mathdots = "2";
516 else if (name == "babel" && !opts.empty()) {
517 // check if more than one option was used - used later for inputenc
518 // in case inputenc is parsed before babel, set the encoding to auto
519 if (options.begin() != options.end() - 1) {
520 one_language = false;
521 h_inputencoding = "auto";
523 // babel takes the last language of the option of its \usepackage
524 // call as document language. If there is no such language option, the
525 // last language in the documentclass options is used.
526 handle_opt(options, known_languages, h_language);
527 delete_opt(options, known_languages);
530 else if (name == "fontenc")
533 else if (name == "inputenc" || name == "luainputenc") {
534 // h_inputencoding is only set when there is not more than one
535 // inputenc option because otherwise h_inputencoding must be
536 // set to "auto" (the default encoding of the document language)
537 // Therefore check for the "," character.
538 // It is also only set when there is not more then one babel
539 // language option but this is handled in the routine for babel.
540 if (opts.find(",") == string::npos && one_language == true)
541 h_inputencoding = opts;
542 if (!options.empty())
543 p.setEncoding(options.back());
547 else if (name == "makeidx")
550 else if (name == "prettyref")
553 else if (name == "varioref")
556 else if (name == "verbatim")
559 else if (name == "nomencl")
562 else if (name == "textcomp")
565 else if (name == "url")
568 else if (name == "subscript")
571 else if (name == "color") {
572 // with the following command this package is only loaded when needed for
573 // undefined colors, since we only support the predefined colors
574 h_preamble << "\\@ifundefined{definecolor}\n {\\usepackage{color}}{}\n";
577 else if (name == "graphicx")
580 else if (name == "setspace")
583 else if (name == "geometry")
584 ; // Ignore this, the geometry settings are made by the \geometry
585 // command. This command is handled below.
587 else if (is_known(name, known_languages))
590 else if (name == "natbib") {
591 h_cite_engine = "natbib_authoryear";
592 vector<string>::iterator it =
593 find(options.begin(), options.end(), "authoryear");
594 if (it != options.end())
597 it = find(options.begin(), options.end(), "numbers");
598 if (it != options.end()) {
599 h_cite_engine = "natbib_numerical";
605 else if (name == "jurabib")
606 h_cite_engine = "jurabib";
608 else if (name == "hyperref")
609 handle_hyperref(options);
611 else if (!in_lyx_preamble) {
613 h_preamble << "\\usepackage{" << name << "}";
615 h_preamble << "\\usepackage[" << opts << "]{"
621 // We need to do something with the options...
622 if (!options.empty())
623 cerr << "Ignoring options '" << join(options, ",")
624 << "' of package " << name << '.' << endl;
626 // remove the whitespace
631 void handle_if(Parser & p, bool in_lyx_preamble)
634 Token t = p.get_token();
635 if (t.cat() == catEscape &&
636 is_known(t.cs(), known_if_commands))
637 handle_if(p, in_lyx_preamble);
639 if (!in_lyx_preamble)
640 h_preamble << t.asInput();
641 if (t.cat() == catEscape && t.cs() == "fi")
648 void end_preamble(ostream & os, TextClass const & /*textclass*/)
650 // translate from babel to LyX names
651 h_language = babel2lyx(h_language);
653 // set the quote language
654 // LyX only knows the following quotes languages:
655 // english, swedish, german, polish, french and danish
656 // (quotes for "japanese" and "chinese-traditional" are missing because
657 // they wouldn't be useful: http://www.lyx.org/trac/ticket/6383)
658 // conversion list taken from
659 // http://en.wikipedia.org/wiki/Quotation_mark,_non-English_usage
660 // (quotes for kazakh and interlingua are unknown)
662 if (h_language == "danish")
663 h_quotes_language = "danish";
665 else if (is_known(h_language, known_french_quotes_languages))
666 h_quotes_language = "french";
668 else if (is_known(h_language, known_german_quotes_languages))
669 h_quotes_language = "german";
671 else if (is_known(h_language, known_polish_quotes_languages))
672 h_quotes_language = "polish";
674 else if (is_known(h_language, known_swedish_quotes_languages))
675 h_quotes_language = "swedish";
677 else if (is_known(h_language, known_english_quotes_languages))
678 h_quotes_language = "english";
680 // output the LyX file settings
681 os << "#LyX file created by tex2lyx " << PACKAGE_VERSION << "\n"
682 << "\\lyxformat " << LYX_FORMAT << '\n'
683 << "\\begin_document\n"
684 << "\\begin_header\n"
685 << "\\textclass " << h_textclass << "\n";
686 if (!h_preamble.str().empty())
687 os << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n";
688 if (!h_options.empty())
689 os << "\\options " << h_options << "\n";
690 os << "\\use_default_options " << h_use_default_options << "\n"
691 << modules_placeholder
692 << "\\language " << h_language << "\n"
693 << "\\inputencoding " << h_inputencoding << "\n"
694 << "\\font_roman " << h_font_roman << "\n"
695 << "\\font_sans " << h_font_sans << "\n"
696 << "\\font_typewriter " << h_font_typewriter << "\n"
697 << "\\font_default_family " << h_font_default_family << "\n"
698 << "\\font_sc " << h_font_sc << "\n"
699 << "\\font_osf " << h_font_osf << "\n"
700 << "\\font_sf_scale " << h_font_sf_scale << "\n"
701 << "\\font_tt_scale " << h_font_tt_scale << "\n"
702 << "\\graphics " << h_graphics << "\n";
703 if (!h_float_placement.empty())
704 os << "\\float_placement " << h_float_placement << "\n";
705 os << "\\paperfontsize " << h_paperfontsize << "\n"
706 << "\\spacing " << h_spacing << "\n"
707 << "\\use_hyperref " << h_use_hyperref << '\n';
708 if (h_use_hyperref == "1") {
709 if (!h_pdf_title.empty())
710 os << "\\pdf_title \"" << h_pdf_title << "\"\n";
711 if (!h_pdf_author.empty())
712 os << "\\pdf_author \"" << h_pdf_author << "\"\n";
713 if (!h_pdf_subject.empty())
714 os << "\\pdf_subject \"" << h_pdf_subject << "\"\n";
715 if (!h_pdf_keywords.empty())
716 os << "\\pdf_keywords \"" << h_pdf_keywords << "\"\n";
717 os << "\\pdf_bookmarks " << h_pdf_bookmarks << "\n"
718 "\\pdf_bookmarksnumbered " << h_pdf_bookmarksnumbered << "\n"
719 "\\pdf_bookmarksopen " << h_pdf_bookmarksopen << "\n"
720 "\\pdf_bookmarksopenlevel " << h_pdf_bookmarksopenlevel << "\n"
721 "\\pdf_breaklinks " << h_pdf_breaklinks << "\n"
722 "\\pdf_pdfborder " << h_pdf_pdfborder << "\n"
723 "\\pdf_colorlinks " << h_pdf_colorlinks << "\n"
724 "\\pdf_backref " << h_pdf_backref << "\n"
725 "\\pdf_pdfusetitle " << h_pdf_pdfusetitle << '\n';
726 if (!h_pdf_pagemode.empty())
727 os << "\\pdf_pagemode " << h_pdf_pagemode << '\n';
728 if (!h_pdf_quoted_options.empty())
729 os << "\\pdf_quoted_options \"" << h_pdf_quoted_options << "\"\n";
731 os << "\\papersize " << h_papersize << "\n"
732 << "\\use_geometry " << h_use_geometry << "\n"
733 << "\\use_amsmath " << h_use_amsmath << "\n"
734 << "\\use_esint " << h_use_esint << "\n"
735 << "\\use_mhchem " << h_use_mhchem << "\n"
736 << "\\use_mathdots " << h_use_mathdots << "\n"
737 << "\\cite_engine " << h_cite_engine << "\n"
738 << "\\use_bibtopic " << h_use_bibtopic << "\n"
739 << "\\paperorientation " << h_paperorientation << '\n'
740 << "\\suppress_date " << h_suppress_date << '\n'
741 << "\\use_refstyle " << h_use_refstyle << '\n';
742 if (!h_notefontcolor.empty())
743 os << "\\notefontcolor " << h_notefontcolor << '\n';
745 << "\\secnumdepth " << h_secnumdepth << "\n"
746 << "\\tocdepth " << h_tocdepth << "\n"
747 << "\\paragraph_separation " << h_paragraph_separation << "\n";
748 if (h_paragraph_separation == "skip")
749 os << "\\defskip " << h_defskip << "\n";
751 os << "\\paragraph_indentation " << h_paragraph_indentation << "\n";
752 os << "\\quotes_language " << h_quotes_language << "\n"
753 << "\\papercolumns " << h_papercolumns << "\n"
754 << "\\papersides " << h_papersides << "\n"
755 << "\\paperpagestyle " << h_paperpagestyle << "\n";
756 if (!h_listings_params.empty())
757 os << "\\listings_params " << h_listings_params << "\n";
758 os << "\\tracking_changes " << h_tracking_changes << "\n"
759 << "\\output_changes " << h_output_changes << "\n"
760 << "\\html_math_output " << h_html_math_output << "\n"
761 << "\\html_be_strict " << h_html_be_strict << "\n"
762 << "\\end_header\n\n"
764 // clear preamble for subdocuments
768 } // anonymous namespace
771 void parse_preamble(Parser & p, ostream & os,
772 string const & forceclass, TeX2LyXDocClass & tc)
774 // initialize fixed types
775 special_columns['D'] = 3;
776 bool is_full_document = false;
777 bool is_lyx_file = false;
778 bool in_lyx_preamble = false;
780 // determine whether this is a full document or a fragment for inclusion
782 Token const & t = p.get_token();
784 if (t.cat() == catEscape && t.cs() == "documentclass") {
785 is_full_document = true;
791 while (is_full_document && p.good()) {
792 Token const & t = p.get_token();
795 cerr << "t: " << t << "\n";
801 if (!in_lyx_preamble &&
802 (t.cat() == catLetter ||
803 t.cat() == catSuper ||
805 t.cat() == catOther ||
806 t.cat() == catMath ||
807 t.cat() == catActive ||
808 t.cat() == catBegin ||
810 t.cat() == catAlign ||
811 t.cat() == catParameter))
812 h_preamble << t.cs();
814 else if (!in_lyx_preamble &&
815 (t.cat() == catSpace || t.cat() == catNewline))
816 h_preamble << t.asInput();
818 else if (t.cat() == catComment) {
819 static regex const islyxfile("%% LyX .* created this file");
820 static regex const usercommands("User specified LaTeX commands");
822 string const comment = t.asInput();
824 // magically switch encoding default if it looks like XeLaTeX
825 static string const magicXeLaTeX =
826 "% This document must be compiled with XeLaTeX ";
827 if (comment.size() > magicXeLaTeX.size()
828 && comment.substr(0, magicXeLaTeX.size()) == magicXeLaTeX
829 && h_inputencoding == "auto") {
830 cerr << "XeLaTeX comment found, switching to UTF8\n";
831 h_inputencoding = "utf8";
834 if (regex_search(comment, sub, islyxfile)) {
836 in_lyx_preamble = true;
837 } else if (is_lyx_file
838 && regex_search(comment, sub, usercommands))
839 in_lyx_preamble = false;
840 else if (!in_lyx_preamble)
841 h_preamble << t.asInput();
844 else if (t.cs() == "pagestyle")
845 h_paperpagestyle = p.verbatim_item();
847 else if (t.cs() == "makeatletter") {
848 // LyX takes care of this
849 p.setCatCode('@', catLetter);
852 else if (t.cs() == "makeatother") {
853 // LyX takes care of this
854 p.setCatCode('@', catOther);
857 else if (t.cs() == "newcommand" || t.cs() == "newcommandx"
858 || t.cs() == "renewcommand" || t.cs() == "renewcommandx"
859 || t.cs() == "providecommand" || t.cs() == "providecommandx"
860 || t.cs() == "DeclareRobustCommand"
861 || t.cs() == "DeclareRobustCommandx"
862 || t.cs() == "ProvideTextCommandDefault"
863 || t.cs() == "DeclareMathAccent") {
865 if (p.next_token().character() == '*') {
869 string const name = p.verbatim_item();
870 string const opt1 = p.getFullOpt();
871 string const opt2 = p.getFullOpt();
872 string const body = p.verbatim_item();
874 if (name == "\\rmdefault")
875 if (is_known(body, known_roman_fonts))
877 if (name == "\\sfdefault")
878 if (is_known(body, known_sans_fonts))
880 if (name == "\\ttdefault")
881 if (is_known(body, known_typewriter_fonts))
882 h_font_typewriter = body;
883 if (name == "\\familydefault") {
884 string family = body;
885 // remove leading "\"
886 h_font_default_family = family.erase(0,1);
889 // Add the command to the known commands
890 add_known_command(name, opt1, !opt2.empty(), from_utf8(body));
892 // only non-lyxspecific stuff
893 if (!in_lyx_preamble) {
895 ss << '\\' << t.cs();
898 ss << '{' << name << '}' << opt1 << opt2
899 << '{' << body << "}";
900 h_preamble << ss.str();
902 ostream & out = in_preamble ? h_preamble : os;
903 out << "\\" << t.cs() << "{" << name << "}"
904 << opts << "{" << body << "}";
909 else if (t.cs() == "documentclass") {
910 vector<string>::iterator it;
911 vector<string> opts = split_options(p.getArg('[', ']'));
912 handle_opt(opts, known_fontsizes, h_paperfontsize);
913 delete_opt(opts, known_fontsizes);
914 // delete "pt" at the end
915 string::size_type i = h_paperfontsize.find("pt");
916 if (i != string::npos)
917 h_paperfontsize.erase(i);
918 // The documentclass options are always parsed before the options
919 // of the babel call so that a language cannot overwrite the babel
921 handle_opt(opts, known_languages, h_language);
922 delete_opt(opts, known_languages);
925 if ((it = find(opts.begin(), opts.end(), "landscape")) != opts.end()) {
926 h_paperorientation = "landscape";
930 if ((it = find(opts.begin(), opts.end(), "oneside"))
935 if ((it = find(opts.begin(), opts.end(), "twoside"))
941 if ((it = find(opts.begin(), opts.end(), "onecolumn"))
943 h_papercolumns = "1";
946 if ((it = find(opts.begin(), opts.end(), "twocolumn"))
948 h_papercolumns = "2";
952 // some size options are know to any document classes, other sizes
953 // are handled by the \geometry command of the geometry package
954 handle_opt(opts, known_class_paper_sizes, h_papersize);
955 delete_opt(opts, known_class_paper_sizes);
956 // the remaining options
957 h_options = join(opts, ",");
958 // FIXME This does not work for classes that have a
959 // different name in LyX than in LaTeX
960 h_textclass = p.getArg('{', '}');
963 else if (t.cs() == "usepackage") {
964 string const options = p.getArg('[', ']');
965 string const name = p.getArg('{', '}');
966 vector<string> vecnames;
967 split(name, vecnames, ',');
968 vector<string>::const_iterator it = vecnames.begin();
969 vector<string>::const_iterator end = vecnames.end();
970 for (; it != end; ++it)
971 handle_package(p, trimSpaceAndEol(*it), options,
975 else if (t.cs() == "inputencoding") {
976 string const encoding = p.getArg('{','}');
977 h_inputencoding = encoding;
978 p.setEncoding(encoding);
981 else if (t.cs() == "newenvironment") {
982 string const name = p.getArg('{', '}');
983 string const opt1 = p.getFullOpt();
984 string const opt2 = p.getFullOpt();
985 string const beg = p.verbatim_item();
986 string const end = p.verbatim_item();
987 if (!in_lyx_preamble) {
988 h_preamble << "\\newenvironment{" << name
989 << '}' << opt1 << opt2 << '{'
990 << beg << "}{" << end << '}';
992 add_known_environment(name, opt1, !opt2.empty(),
993 from_utf8(beg), from_utf8(end));
997 else if (t.cs() == "def") {
998 string name = p.get_token().cs();
999 while (p.next_token().cat() != catBegin)
1000 name += p.get_token().cs();
1001 if (!in_lyx_preamble)
1002 h_preamble << "\\def\\" << name << '{'
1003 << p.verbatim_item() << "}";
1006 else if (t.cs() == "newcolumntype") {
1007 string const name = p.getArg('{', '}');
1008 trimSpaceAndEol(name);
1010 string opts = p.getOpt();
1011 if (!opts.empty()) {
1012 istringstream is(string(opts, 1));
1015 special_columns[name[0]] = nargs;
1016 h_preamble << "\\newcolumntype{" << name << "}";
1018 h_preamble << "[" << nargs << "]";
1019 h_preamble << "{" << p.verbatim_item() << "}";
1022 else if (t.cs() == "setcounter") {
1023 string const name = p.getArg('{', '}');
1024 string const content = p.getArg('{', '}');
1025 if (name == "secnumdepth")
1026 h_secnumdepth = content;
1027 else if (name == "tocdepth")
1028 h_tocdepth = content;
1030 h_preamble << "\\setcounter{" << name << "}{" << content << "}";
1033 else if (t.cs() == "setlength") {
1034 string const name = p.verbatim_item();
1035 string const content = p.verbatim_item();
1036 // the paragraphs are only not indented when \parindent is set to zero
1037 if (name == "\\parindent" && content != "") {
1038 if (content[0] == '0')
1039 h_paragraph_separation = "skip";
1041 h_paragraph_indentation = translate_len(content);
1042 } else if (name == "\\parskip") {
1043 if (content == "\\smallskipamount")
1044 h_defskip = "smallskip";
1045 else if (content == "\\medskipamount")
1046 h_defskip = "medskip";
1047 else if (content == "\\bigskipamount")
1048 h_defskip = "bigskip";
1050 h_defskip = content;
1052 h_preamble << "\\setlength{" << name << "}{" << content << "}";
1055 else if (t.cs() == "onehalfspacing")
1056 h_spacing = "onehalf";
1058 else if (t.cs() == "doublespacing")
1059 h_spacing = "double";
1061 else if (t.cs() == "setstretch")
1062 h_spacing = "other " + p.verbatim_item();
1064 else if (t.cs() == "begin") {
1065 string const name = p.getArg('{', '}');
1066 if (name == "document")
1068 h_preamble << "\\begin{" << name << "}";
1071 else if (t.cs() == "geometry") {
1072 h_use_geometry = "true";
1073 vector<string> opts = split_options(p.getArg('{', '}'));
1074 vector<string>::iterator it;
1075 // paper orientation
1076 if ((it = find(opts.begin(), opts.end(), "landscape")) != opts.end()) {
1077 h_paperorientation = "landscape";
1081 handle_opt(opts, known_paper_sizes, h_papersize);
1082 delete_opt(opts, known_paper_sizes);
1084 char const * const * margin = known_paper_margins;
1086 for (; *margin; ++margin) {
1088 // search for the "=" in e.g. "lmargin=2cm" to get the value
1089 for(size_t i = 0; i != opts.size(); i++) {
1090 if (opts.at(i).find(*margin) != string::npos) {
1091 string::size_type pos = opts.at(i).find("=");
1092 string value = opts.at(i).substr(pos + 1);
1093 string name = known_coded_paper_margins[k];
1094 h_margins += "\\" + name + " " + value + "\n";
1100 else if (t.cs() == "definecolor") {
1101 string const color = p.getArg('{', '}');
1102 string const space = p.getArg('{', '}');
1103 string const value = p.getArg('{', '}');
1104 if (color == "note_fontcolor" && space == "rgb") {
1105 RGBColor c(RGBColorFromLaTeX(value));
1106 h_notefontcolor = X11hexname(c);
1108 h_preamble << "\\definecolor{" << color
1109 << "}{" << space << "}{" << value
1114 else if (t.cs() == "jurabibsetup") {
1115 // FIXME p.getArg('{', '}') is most probably wrong (it
1116 // does not handle nested braces).
1117 // Use p.verbatim_item() instead.
1118 vector<string> jurabibsetup =
1119 split_options(p.getArg('{', '}'));
1120 // add jurabibsetup to the jurabib package options
1121 add_package("jurabib", jurabibsetup);
1122 if (!jurabibsetup.empty()) {
1123 h_preamble << "\\jurabibsetup{"
1124 << join(jurabibsetup, ",") << '}';
1128 else if (t.cs() == "hypersetup") {
1129 vector<string> hypersetup =
1130 split_options(p.verbatim_item());
1131 // add hypersetup to the hyperref package options
1132 handle_hyperref(hypersetup);
1133 if (!hypersetup.empty()) {
1134 h_preamble << "\\hypersetup{"
1135 << join(hypersetup, ",") << '}';
1139 else if (is_known(t.cs(), known_if_3arg_commands)) {
1140 // prevent misparsing of \usepackage if it is used
1141 // as an argument (see e.g. our own output of
1142 // \@ifundefined above)
1143 string const arg1 = p.verbatim_item();
1144 string const arg2 = p.verbatim_item();
1145 string const arg3 = p.verbatim_item();
1146 if (!in_lyx_preamble) {
1147 h_preamble << t.asInput()
1148 << '{' << arg1 << '}'
1149 << '{' << arg2 << '}'
1150 << '{' << arg3 << '}';
1154 else if (is_known(t.cs(), known_if_commands)) {
1155 // must not parse anything in conditional code, since
1156 // LyX would output the parsed contents unconditionally
1157 if (!in_lyx_preamble)
1158 h_preamble << t.asInput();
1159 handle_if(p, in_lyx_preamble);
1162 else if (!t.cs().empty() && !in_lyx_preamble)
1163 h_preamble << '\\' << t.cs();
1166 // remove the whitespace
1169 // Force textclass if the user wanted it
1170 if (!forceclass.empty())
1171 h_textclass = forceclass;
1172 if (noweb_mode && !prefixIs(h_textclass, "literate-"))
1173 h_textclass.insert(0, "literate-");
1174 tc.setName(h_textclass);
1176 cerr << "Error: Could not read layout file for textclass \"" << h_textclass << "\"." << endl;
1179 if (h_papersides.empty()) {
1182 h_papersides = ss.str();
1184 end_preamble(os, tc);
1188 /// translates a babel language name to a LyX language name
1189 string babel2lyx(string const & language)
1191 char const * const * where = is_known(language, known_languages);
1193 return known_coded_languages[where - known_languages];