]> git.lyx.org Git - lyx.git/blob - src/LaTeXFeatures.cpp
da13ccb34785c9e7b045ce0ff88e8e4d9b6af3cf
[lyx.git] / src / LaTeXFeatures.cpp
1 /**
2  * \file LaTeXFeatures.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author José Matos
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author Jürgen Vigna
10  * \author André Pönitz
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16
17 #include "LaTeXFeatures.h"
18
19 #include "Buffer.h"
20 #include "BufferParams.h"
21 #include "ColorSet.h"
22 #include "Converter.h"
23 #include "Encoding.h"
24 #include "Floating.h"
25 #include "FloatList.h"
26 #include "Language.h"
27 #include "LaTeXPackages.h"
28 #include "Layout.h"
29 #include "Lexer.h"
30 #include "LyXRC.h"
31 #include "TextClass.h"
32
33 #include "insets/InsetLayout.h"
34
35 #include "support/debug.h"
36 #include "support/docstream.h"
37 #include "support/FileName.h"
38 #include "support/filetools.h"
39 #include "support/gettext.h"
40 #include "support/lstrings.h"
41
42 #include <algorithm>
43
44
45 using namespace std;
46 using namespace lyx::support;
47
48
49 namespace lyx {
50
51 /////////////////////////////////////////////////////////////////////
52 //
53 // Strings
54 //
55 /////////////////////////////////////////////////////////////////////
56
57 //\NeedsTeXFormat{LaTeX2e}
58 //\ProvidesPackage{lyx}[1996/01/11 LLE v0.2 (LyX LaTeX Extensions)]
59 //\message{LyX LaTeX Extensions (LLE v0.2) of 11-Jan-1996.}
60
61 static docstring const lyx_def = from_ascii(
62         "\\providecommand{\\LyX}{L\\kern-.1667em\\lower.25em\\hbox{Y}\\kern-.125emX\\@}");
63
64 static docstring const lyx_hyperref_def = from_ascii(
65         "\\providecommand{\\LyX}{\\texorpdfstring%\n"
66         "  {L\\kern-.1667em\\lower.25em\\hbox{Y}\\kern-.125emX\\@}\n"
67         "  {LyX}}");
68
69 static docstring const noun_def = from_ascii(
70         "\\newcommand{\\noun}[1]{\\textsc{#1}}");
71
72 static docstring const lyxarrow_def = from_ascii(
73         "\\DeclareRobustCommand*{\\lyxarrow}{%\n"
74         "\\@ifstar\n"
75         "{\\leavevmode\\,$\\triangleleft$\\,\\allowbreak}\n"
76         "{\\leavevmode\\,$\\triangleright$\\,\\allowbreak}}");
77
78 // for quotes without babel. This does not give perfect results, but
79 // anybody serious about non-english quotes should use babel (JMarc).
80
81 static docstring const quotedblbase_def = from_ascii(
82         "\\ProvideTextCommandDefault{\\quotedblbase}{%\n"
83         "  \\raisebox{-1.4ex}[1ex][.5ex]{\\textquotedblright}%\n"
84         "  \\penalty10000\\hskip0em\\relax%\n"
85         "}");
86
87 static docstring const quotesinglbase_def = from_ascii(
88         "\\ProvideTextCommandDefault{\\quotesinglbase}{%\n"
89         "  \\raisebox{-1.4ex}[1ex][.5ex]{\\textquoteright}%\n"
90         "  \\penalty10000\\hskip0em\\relax%\n"
91         "}");
92
93 static docstring const guillemotleft_def = from_ascii(
94         "\\ProvideTextCommandDefault{\\guillemotleft}{%\n"
95         "  {\\usefont{U}{lasy}{m}{n}\\char'50\\kern-.15em\\char'50}%\n"
96         "\\penalty10000\\hskip0pt\\relax%\n"
97         "}");
98
99 static docstring const guillemotright_def = from_ascii(
100         "\\ProvideTextCommandDefault{\\guillemotright}{%\n"
101         "  \\penalty10000\\hskip0pt%\n"
102         "  {\\usefont{U}{lasy}{m}{n}\\char'51\\kern-.15em\\char'51}%\n"
103         "}");
104
105 static docstring const guilsinglleft_def = from_ascii(
106         "\\ProvideTextCommandDefault{\\guilsinglleft}{%\n"
107         "  {\\usefont{U}{lasy}{m}{n}\\char'50}%\n"
108         "  \\penalty10000\\hskip0pt\\relax%\n"
109         "}");
110
111 static docstring const guilsinglright_def = from_ascii(
112         "\\ProvideTextCommandDefault{\\guilsinglright}{%\n"
113         "  \\penalty10000\\hskip0pt%\n"
114         "  {\\usefont{U}{lasy}{m}{n}\\char'51}%\n"
115         "}");
116
117 static docstring const paragraphleftindent_def = from_ascii(
118         "\\newenvironment{LyXParagraphLeftIndent}[1]%\n"
119         "{\n"
120         "  \\begin{list}{}{%\n"
121         "    \\setlength{\\topsep}{0pt}%\n"
122         "    \\addtolength{\\leftmargin}{#1}\n"
123 // ho hum, yet more things commented out with no hint as to why they
124 // weren't just removed
125 //      "%%    \\addtolength{\\leftmargin}{#1\\textwidth}\n"
126 //      "%%    \\setlength{\\textwidth}{#2\\textwidth}\n"
127 //      "%%    \\setlength\\listparindent\\parindent%\n"
128 //      "%%    \\setlength\\itemindent\\parindent%\n"
129         "    \\setlength{\\parsep}{0pt plus 1pt}%\n"
130         "  }\n"
131         "  \\item[]\n"
132         "}\n"
133         "{\\end{list}}\n");
134
135 static docstring const floatingfootnote_def = from_ascii(
136         "%% Special footnote code from the package 'stblftnt.sty'\n"
137         "%% Author: Robin Fairbairns -- Last revised Dec 13 1996\n"
138         "\\let\\SF@@footnote\\footnote\n"
139         "\\def\\footnote{\\ifx\\protect\\@typeset@protect\n"
140         "    \\expandafter\\SF@@footnote\n"
141         "  \\else\n"
142         "    \\expandafter\\SF@gobble@opt\n"
143         "  \\fi\n"
144         "}\n"
145         "\\expandafter\\def\\csname SF@gobble@opt \\endcsname{\\@ifnextchar[%]\n"
146         "  \\SF@gobble@twobracket\n"
147         "  \\@gobble\n"
148         "}\n"
149         "\\edef\\SF@gobble@opt{\\noexpand\\protect\n"
150         "  \\expandafter\\noexpand\\csname SF@gobble@opt \\endcsname}\n"
151         "\\def\\SF@gobble@twobracket[#1]#2{}\n");
152
153 static docstring const binom_def = from_ascii(
154         "%% Binom macro for standard LaTeX users\n"
155         "\\newcommand{\\binom}[2]{{#1 \\choose #2}}\n");
156
157 static docstring const mathcircumflex_def = from_ascii(
158         "%% For printing a cirumflex inside a formula\n"
159         "\\newcommand{\\mathcircumflex}[0]{\\mbox{\\^{}}}\n");
160
161 static docstring const tabularnewline_def = from_ascii(
162         "%% Because html converters don't know tabularnewline\n"
163         "\\providecommand{\\tabularnewline}{\\\\}\n");
164
165 static docstring const lyxgreyedout_def = from_ascii(
166         "%% The greyedout annotation environment\n"
167         "\\newenvironment{lyxgreyedout}\n"
168         "  {\\textcolor{note_fontcolor}\\bgroup\\ignorespaces}\n"
169         "  {\\ignorespacesafterend\\egroup}\n");
170
171 // We want to omit the file extension for includegraphics, but this does not
172 // work when the filename contains other dots.
173 // Idea from http://www.tex.ac.uk/cgi-bin/texfaq2html?label=unkgrfextn
174 static docstring const lyxdot_def = from_ascii(
175         "%% A simple dot to overcome graphicx limitations\n"
176         "\\newcommand{\\lyxdot}{.}\n");
177
178 static docstring const changetracking_dvipost_def = from_ascii(
179         "%% Change tracking with dvipost\n"
180         "\\dvipostlayout\n"
181         "\\dvipost{osstart color push Red}\n"
182         "\\dvipost{osend color pop}\n"
183         "\\dvipost{cbstart color push Blue}\n"
184         "\\dvipost{cbend color pop}\n"
185         "\\newcommand{\\lyxadded}[3]{\\changestart#3\\changeend}\n"
186         "\\newcommand{\\lyxdeleted}[3]{%\n"
187         "\\changestart\\overstrikeon#3\\overstrikeoff\\changeend}\n");
188
189 static docstring const changetracking_xcolor_ulem_def = from_ascii(
190         "%% Change tracking with ulem\n"
191         "\\newcommand{\\lyxadded}[3]{{\\color{lyxadded}{}#3}}\n"
192         "\\newcommand{\\lyxdeleted}[3]{{\\color{lyxdeleted}\\sout{#3}}}\n");
193
194 static docstring const changetracking_xcolor_ulem_hyperref_def = from_ascii(
195         "%% Change tracking with ulem\n"
196         "\\newcommand{\\lyxadded}[3]{{\\texorpdfstring{\\color{lyxadded}{}}{}#3}}\n"
197         "\\newcommand{\\lyxdeleted}[3]{{\\texorpdfstring{\\color{lyxdeleted}\\sout{#3}}{}}}\n");
198
199 static docstring const changetracking_none_def = from_ascii(
200         "\\newcommand{\\lyxadded}[3]{#3}\n"
201         "\\newcommand{\\lyxdeleted}[3]{}\n");
202
203 static docstring const textgreek_def = from_ascii(
204         "\\DeclareRobustCommand{\\greektext}{%\n"
205         "  \\fontencoding{LGR}\\selectfont\\def\\encodingdefault{LGR}}\n"
206         "\\DeclareRobustCommand{\\textgreek}[1]{\\leavevmode{\\greektext #1}}\n"
207         "\\DeclareFontEncoding{LGR}{}{}\n"
208         "\\DeclareTextSymbol{\\~}{LGR}{126}");
209
210 static docstring const textcyr_def = from_ascii(
211         "\\DeclareRobustCommand{\\cyrtext}{%\n"
212         "  \\fontencoding{T2A}\\selectfont\\def\\encodingdefault{T2A}}\n"
213         "\\DeclareRobustCommand{\\textcyr}[1]{\\leavevmode{\\cyrtext #1}}\n"
214         "\\AtBeginDocument{\\DeclareFontEncoding{T2A}{}{}}\n");
215
216 static docstring const lyxmathsym_def = from_ascii(
217         "\\newcommand{\\lyxmathsym}[1]{\\ifmmode\\begingroup\\def\\b@ld{bold}\n"
218         "  \\text{\\ifx\\math@version\\b@ld\\bfseries\\fi#1}\\endgroup\\else#1\\fi}\n");
219
220 static docstring const papersizedvi_def = from_ascii(
221         "\\special{papersize=\\the\\paperwidth,\\the\\paperheight}\n");
222
223 static docstring const papersizepdf_def = from_ascii(
224         "\\pdfpageheight\\paperheight\n"
225         "\\pdfpagewidth\\paperwidth\n");
226
227 static docstring const cedilla_def = from_ascii(
228         "\\newcommand{\\docedilla}[2]{\\underaccent{#1\\mathchar'30}{#2}}\n"
229         "\\newcommand{\\cedilla}[1]{\\mathpalette\\docedilla{#1}}\n");
230
231 static docstring const subring_def = from_ascii(
232         "\\newcommand{\\dosubring}[2]{\\underaccent{#1\\mathchar'27}{#2}}\n"
233         "\\newcommand{\\subring}[1]{\\mathpalette\\dosubring{#1}}\n");
234
235 static docstring const subdot_def = from_ascii(
236         "\\newcommand{\\dosubdot}[2]{\\underaccent{#1.}{#2}}\n"
237         "\\newcommand{\\subdot}[1]{\\mathpalette\\dosubdot{#1}}\n");
238
239 static docstring const subhat_def = from_ascii(
240         "\\newcommand{\\dosubhat}[2]{\\underaccent{#1\\mathchar'136}{#2}}\n"
241         "\\newcommand{\\subhat}[1]{\\mathpalette\\dosubhat{#1}}\n");
242
243 static docstring const subtilde_def = from_ascii(
244         "\\newcommand{\\dosubtilde}[2]{\\underaccent{#1\\mathchar'176}{#2}}\n"
245         "\\newcommand{\\subtilde}[1]{\\mathpalette\\dosubtilde{#1}}\n");
246
247 static docstring const dacute_def = from_ascii(
248         "\\DeclareMathAccent{\\dacute}{\\mathalpha}{operators}{'175}\n");
249
250 static docstring const tipasymb_def = from_ascii(
251         "\\DeclareFontEncoding{T3}{}{}\n"
252         "\\DeclareSymbolFont{tipasymb}{T3}{cmr}{m}{n}\n");
253
254 static docstring const dgrave_def = from_ascii(
255         "\\DeclareMathAccent{\\dgrave}{\\mathord}{tipasymb}{'15}\n");
256
257 static docstring const rcap_def = from_ascii(
258         "\\DeclareMathAccent{\\rcap}{\\mathord}{tipasymb}{'20}\n");
259
260 static docstring const ogonek_def = from_ascii(
261         "\\newcommand{\\doogonek}[2]{\\setbox0=\\hbox{$#1#2$}\\underaccent{#1\\mkern-6mu\n"
262         "  \\ifx#2O\\hskip0.5\\wd0\\else\\ifx#2U\\hskip0.5\\wd0\\else\\hskip\\wd0\\fi\\fi\n"
263         "  \\ifx#2o\\mkern-2mu\\else\\ifx#2e\\mkern-1mu\\fi\\fi\n"
264         "  \\mathchar\"0\\hexnumber@\\symtipasymb0C}{#2}}\n"
265         "\\newcommand{\\ogonek}[1]{\\mathpalette\\doogonek{#1}}\n");
266
267 static docstring const lyxref_def = from_ascii(
268                 "\\RS@ifundefined{subref}\n"
269                 "  {\\def\\RSsubtxt{section~}\\newref{sub}{name = \\RSsubtxt}}\n"
270                 "  {}\n"
271                 "\\RS@ifundefined{thmref}\n"
272                 "  {\\def\\RSthmtxt{theorem~}\\newref{thm}{name = \\RSthmtxt}}\n"
273                 "  {}\n"
274                 "\\RS@ifundefined{lemref}\n"
275                 "  {\\def\\RSlemtxt{lemma~}\\newref{lem}{name = \\RSlemtxt}}\n" 
276                 "  {}\n");
277
278
279 /////////////////////////////////////////////////////////////////////
280 //
281 // LaTeXFeatures
282 //
283 /////////////////////////////////////////////////////////////////////
284
285
286 LaTeXFeatures::LaTeXFeatures(Buffer const & b, BufferParams const & p,
287                              OutputParams const & r)
288         : buffer_(&b), params_(p), runparams_(r), in_float_(false)
289 {}
290
291
292 bool LaTeXFeatures::useBabel() const
293 {
294         if (usePolyglossia())
295                 return false;
296         if (bufferParams().lang_package == "default")
297                 return (lyxrc.language_package_selection != LyXRC::LP_NONE)
298                         || (bufferParams().language->lang() != lyxrc.default_language
299                             && !bufferParams().language->babel().empty())
300                         || this->hasLanguages();
301         return (bufferParams().lang_package != "none")
302                 || (bufferParams().language->lang() != lyxrc.default_language
303                     && !bufferParams().language->babel().empty())
304                 || this->hasLanguages();
305 }
306
307
308 bool LaTeXFeatures::usePolyglossia() const
309 {
310         if (bufferParams().lang_package == "default")
311                 return (lyxrc.language_package_selection == LyXRC::LP_AUTO)
312                         && isRequired("polyglossia")
313                         && isAvailable("polyglossia")
314                         && !params_.documentClass().provides("babel")
315                         && this->hasPolyglossiaLanguages();
316         return (bufferParams().lang_package == "auto")
317                 && isRequired("polyglossia")
318                 && isAvailable("polyglossia")
319                 && !params_.documentClass().provides("babel")
320                 && this->hasPolyglossiaLanguages();
321 }
322
323
324 void LaTeXFeatures::require(string const & name)
325 {
326         features_.insert(name);
327 }
328
329
330 void LaTeXFeatures::require(set<string> const & names)
331 {
332         features_.insert(names.begin(), names.end());
333 }
334
335
336 void LaTeXFeatures::useLayout(docstring const & layoutname)
337 {
338         // Some code to avoid loops in dependency definition
339         static int level = 0;
340         const int maxlevel = 30;
341         if (level > maxlevel) {
342                 lyxerr << "LaTeXFeatures::useLayout: maximum level of "
343                        << "recursion attained by layout "
344                        << to_utf8(layoutname) << endl;
345                 return;
346         }
347
348         DocumentClass const & tclass = params_.documentClass();
349         if (tclass.hasLayout(layoutname)) {
350                 // Is this layout already in usedLayouts?
351                 if (find(usedLayouts_.begin(), usedLayouts_.end(), layoutname)
352                     != usedLayouts_.end())
353                         return;
354
355                 Layout const & layout = tclass[layoutname];
356                 require(layout.requires());
357
358                 if (!layout.depends_on().empty()) {
359                         ++level;
360                         useLayout(layout.depends_on());
361                         --level;
362                 }
363                 usedLayouts_.push_back(layoutname);
364         } else {
365                 lyxerr << "LaTeXFeatures::useLayout: layout `"
366                        << to_utf8(layoutname) << "' does not exist in this class"
367                        << endl;
368         }
369
370         --level;
371 }
372
373
374 void LaTeXFeatures::useInsetLayout(InsetLayout const & lay)
375 {
376         docstring const & lname = lay.name();
377         DocumentClass const & tclass = params_.documentClass();
378
379         // this is a default inset layout, nothing useful here
380         if (!tclass.hasInsetLayout(lname))
381                 return;
382         // Is this layout already in usedInsetLayouts?
383         if (find(usedInsetLayouts_.begin(), usedInsetLayouts_.end(), lname)
384                         != usedInsetLayouts_.end())
385                 return;
386
387         require(lay.requires());
388         usedInsetLayouts_.push_back(lname);
389 }
390
391
392 bool LaTeXFeatures::isRequired(string const & name) const
393 {
394         return features_.find(name) != features_.end();
395 }
396
397
398 bool LaTeXFeatures::mustProvide(string const & name) const
399 {
400         return isRequired(name) && !params_.documentClass().provides(name);
401 }
402
403
404 bool LaTeXFeatures::isAvailable(string const & name)
405 {
406         string::size_type const i = name.find("->");
407         if (i != string::npos) {
408                 string const from = name.substr(0,i);
409                 string const to = name.substr(i+2);
410                 //LYXERR0("from=[" << from << "] to=[" << to << "]");
411                 return theConverters().isReachable(from, to);
412         }
413         return LaTeXPackages::isAvailable(name);
414 }
415
416
417 void LaTeXFeatures::addPreambleSnippet(string const & preamble)
418 {
419         SnippetList::const_iterator begin = preamble_snippets_.begin();
420         SnippetList::const_iterator end   = preamble_snippets_.end();
421         if (find(begin, end, preamble) == end)
422                 preamble_snippets_.push_back(preamble);
423 }
424
425
426 void LaTeXFeatures::addCSSSnippet(std::string const & snippet)
427 {
428         SnippetList::const_iterator begin = css_snippets_.begin();
429         SnippetList::const_iterator end   = css_snippets_.end();
430         if (find(begin, end, snippet) == end)
431                 css_snippets_.push_back(snippet);
432 }
433
434
435 void LaTeXFeatures::useFloat(string const & name, bool subfloat)
436 {
437         if (!usedFloats_[name])
438                 usedFloats_[name] = subfloat;
439         if (subfloat)
440                 require("subfig");
441         // We only need float.sty if we use non builtin floats, or if we
442         // use the "H" modifier. This includes modified table and
443         // figure floats. (Lgb)
444         Floating const & fl = params_.documentClass().floats().getType(name);
445         if (!fl.floattype().empty() && fl.usesFloatPkg()) {
446                 require("float");
447         }
448 }
449
450
451 void LaTeXFeatures::useLanguage(Language const * lang)
452 {
453         if (!lang->babel().empty())
454                 UsedLanguages_.insert(lang);
455         if (lang->lang() == "vietnamese")
456                 require("vietnamese");
457         // CJK languages do not have a babel name.
458         // They use the CJK package
459         if (lang->encoding()->package() == Encoding::CJK)
460                 require("CJK");
461         // japanese package is special
462         if (lang->encoding()->package() == Encoding::japanese)
463                 require("japanese");
464 }
465
466
467 void LaTeXFeatures::includeFile(docstring const & key, string const & name)
468 {
469         IncludedFiles_[key] = name;
470 }
471
472
473 bool LaTeXFeatures::hasLanguages() const
474 {
475         return !UsedLanguages_.empty();
476 }
477
478
479 bool LaTeXFeatures::hasPolyglossiaLanguages() const
480 {
481         LanguageList::const_iterator const begin = UsedLanguages_.begin();
482         for (LanguageList::const_iterator cit = begin;
483              cit != UsedLanguages_.end();
484              ++cit) {
485                 if ((*cit)->polyglossia().empty())
486                         return false;
487         }
488         return true;
489 }
490
491
492 string LaTeXFeatures::getLanguages() const
493 {
494         ostringstream languages;
495
496         LanguageList::const_iterator const begin = UsedLanguages_.begin();
497         for (LanguageList::const_iterator cit = begin;
498              cit != UsedLanguages_.end();
499              ++cit) {
500                 if (cit != begin)
501                         languages << ',';
502                 languages << (*cit)->babel();
503         }
504         return languages.str();
505 }
506
507
508 std::map<std::string, std::string> LaTeXFeatures::getPolyglossiaLanguages() const
509 {
510         std::map<std::string, std::string> languages;
511
512         LanguageList::const_iterator const begin = UsedLanguages_.begin();
513         for (LanguageList::const_iterator cit = begin;
514              cit != UsedLanguages_.end();
515              ++cit) {
516                 languages[(*cit)->polyglossia()] = (*cit)->polyglossiaOpts();
517         }
518         return languages;
519 }
520
521
522 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding) const
523 {
524         // This does only find encodings of languages supported by babel, but
525         // that does not matter since we don't have a language with an
526         // encoding supported by inputenc but without babel support.
527         set<string> encodings;
528         LanguageList::const_iterator it  = UsedLanguages_.begin();
529         LanguageList::const_iterator end = UsedLanguages_.end();
530         for (; it != end; ++it)
531                 if ((*it)->encoding()->latexName() != doc_encoding &&
532                     ((*it)->encoding()->package() == Encoding::inputenc
533                      || (*it)->encoding()->package() == Encoding::japanese))
534                         encodings.insert((*it)->encoding()->latexName());
535         return encodings;
536 }
537
538 namespace {
539
540 char const * simplefeatures[] = {
541 // note that the package order here will be the same in the LaTeX-output
542         "array",
543         "verbatim",
544         "longtable",
545         "rotating",
546         "latexsym",
547         "pifont",
548         // subfig is handled in BufferParams.cpp
549         "varioref",
550         "prettyref",
551         "refstyle",
552         /*For a successful cooperation of the `wrapfig' package with the
553           `float' package you should load the `wrapfig' package *after*
554           the `float' package. See the caption package documentation
555           for explanation.*/
556         "float",
557         "rotfloat",
558         "wrapfig",
559         "booktabs",
560         "dvipost",
561         "fancybox",
562         "calc",
563         "units",
564         "tipa",
565         "tipx",
566         "framed",
567         "soul",
568         "textcomp",
569         "pmboxdraw",
570         "bbding",
571         "ifsym",
572         "marvosym",
573         "txfonts",
574         "pxfonts",
575         "mathdesign",
576         "mathrsfs",
577         "mathabx",
578         "mathtools",
579         "ascii",
580         "url",
581         "covington",
582         "csquotes",
583         "enumitem",
584         "endnotes",
585         "hhline",
586         "ifthen",
587         // listings is handled in BufferParams.cpp
588         "bm",
589         "pdfpages",
590         "amscd",
591         "slashed",
592         "multirow",
593         "tfrupee"
594 };
595
596 int const nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
597
598 }
599
600
601 string const LaTeXFeatures::getColorOptions() const
602 {
603         ostringstream colors;
604
605         // Handling the color packages separately is needed to be able to load them
606         // before babel when hyperref is loaded with the colorlinks option
607         // for more info see Bufferparams.cpp
608
609         // [x]color.sty
610         if (mustProvide("color") || mustProvide("xcolor")) {
611                 string const package =
612                         (mustProvide("xcolor") ? "xcolor" : "color");
613                 if (params_.graphics_driver == "default"
614                         || params_.graphics_driver == "none")
615                         colors << "\\usepackage{" << package << "}\n";
616                 else
617                         colors << "\\usepackage["
618                                  << params_.graphics_driver
619                                  << "]{" << package << "}\n";
620         }
621
622         // pdfcolmk must be loaded after color
623         if (mustProvide("pdfcolmk"))
624                 colors << "\\usepackage{pdfcolmk}\n";
625
626         // the following 3 color commands must be set after color
627         // is loaded and before pdfpages, therefore add the command
628         // here define the set color
629         if (mustProvide("pagecolor")) {
630                 colors << "\\definecolor{page_backgroundcolor}{rgb}{";
631                 colors << outputLaTeXColor(params_.backgroundcolor) << "}\n";
632                 // set the page color
633                 colors << "\\pagecolor{page_backgroundcolor}\n";
634         }
635
636         if (mustProvide("fontcolor")) {
637                 colors << "\\definecolor{document_fontcolor}{rgb}{";
638                 colors << outputLaTeXColor(params_.fontcolor) << "}\n";
639                 // set the color
640                 colors << "\\color{document_fontcolor}\n";
641         }
642
643         if (mustProvide("lyxgreyedout")) {
644                 colors << "\\definecolor{note_fontcolor}{rgb}{";
645                 colors << outputLaTeXColor(params_.notefontcolor) << "}\n";
646                 // the color will be set together with the definition of
647                 // the lyxgreyedout environment (see lyxgreyedout_def)
648         }
649
650         // color for shaded boxes
651         if (isRequired("framed") && mustProvide("color")) {
652                 colors << "\\definecolor{shadecolor}{rgb}{";
653                 colors << outputLaTeXColor(params_.boxbgcolor) << "}\n";
654                 // this color is automatically used by the LaTeX-package "framed"
655         }
656
657         return colors.str();
658 }
659
660
661 string const LaTeXFeatures::getPackages() const
662 {
663         ostringstream packages;
664         DocumentClass const & tclass = params_.documentClass();
665
666         // FIXME: currently, we can only load packages and macros known
667         // to LyX.
668         // However, with the Require tag of layouts/custom insets,
669         // also unknown packages can be requested. They are silently
670         // swallowed now. We should change this eventually.
671
672         //
673         //  These are all the 'simple' includes.  i.e
674         //  packages which we just \usepackage{package}
675         //
676         for (int i = 0; i < nb_simplefeatures; ++i) {
677                 if (mustProvide(simplefeatures[i]))
678                         packages << "\\usepackage{"
679                                  << simplefeatures[i] << "}\n";
680         }
681
682         //
683         // The rest of these packages are somewhat more complicated
684         // than those above.
685         //
686
687         // if fontspec is used, AMS packages have to be loaded before
688         // fontspec (in BufferParams)
689         string const amsPackages = loadAMSPackages();
690         if (!params_.useNonTeXFonts && !amsPackages.empty())
691                 packages << amsPackages;
692
693         // fixltx2e must be loaded after amsthm, since amsthm produces an error with
694         // the redefined \[ command (bug 7233). Load it as early as possible, since
695         // other packages might profit from it.
696         if (mustProvide("fixltx2e"))
697                 packages << "\\usepackage{fixltx2e}\n";
698
699         // wasysym is a simple feature, but it must be after amsmath if both
700         // are used
701         // wasysym redefines some integrals (e.g. iint) from amsmath. That
702         // leads to inconsistent integrals. We only load this package if
703         // the document does not contain integrals (then isRequired("esint")
704         // is false) or if esint is used, since esint redefines all relevant
705         // integral symbols from wasysym and amsmath.
706         // See http://www.lyx.org/trac/ticket/1942
707         if (mustProvide("wasysym") &&
708             params_.use_package("wasysym") != BufferParams::package_off &&
709             (params_.use_package("esint") != BufferParams::package_off || !isRequired("esint")))
710                 packages << "\\usepackage{wasysym}\n";
711
712         // accents must be loaded after amsmath
713         if (mustProvide("accents") &&
714             params_.use_package("accents") != BufferParams::package_off)
715                 packages << "\\usepackage{accents}\n";
716
717         // mathdots must be loaded after amsmath
718         if (mustProvide("mathdots") &&
719                 params_.use_package("mathdots") != BufferParams::package_off)
720                 packages << "\\usepackage{mathdots}\n";
721
722         // yhmath must be loaded after amsmath
723         if (mustProvide("yhmath") &&
724             params_.use_package("yhmath") != BufferParams::package_off)
725                 packages << "\\usepackage{yhmath}\n";
726
727         if (mustProvide("undertilde") &&
728                 params_.use_package("undertilde") != BufferParams::package_off)
729                 packages << "\\usepackage{undertilde}\n";
730
731         if (mustProvide("cancel"))
732                 packages << "\\usepackage{cancel}\n";
733
734         // [x]color and pdfcolmk are handled in getColorOptions() above
735
736         // makeidx.sty
737         if (isRequired("makeidx") || isRequired("splitidx")) {
738                 if (!tclass.provides("makeidx") && !isRequired("splitidx"))
739                         packages << "\\usepackage{makeidx}\n";
740                 if (!tclass.provides("splitidx") && isRequired("splitidx"))
741                         packages << "\\usepackage{splitidx}\n";
742                 packages << "\\makeindex\n";
743         }
744
745         // graphicx.sty
746         if (mustProvide("graphicx") && params_.graphics_driver != "none") {
747                 if (params_.graphics_driver == "default")
748                         packages << "\\usepackage{graphicx}\n";
749                 else
750                         packages << "\\usepackage["
751                                  << params_.graphics_driver
752                                  << "]{graphicx}\n";
753         }
754
755         // lyxskak.sty --- newer chess support based on skak.sty
756         if (mustProvide("chess"))
757                 packages << "\\usepackage[ps,mover]{lyxskak}\n";
758
759         // setspace.sty
760         if (mustProvide("setspace") && !tclass.provides("SetSpace"))
761                 packages << "\\usepackage{setspace}\n";
762
763         // esint must be after amsmath and wasysym, since it will redeclare
764         // inconsistent integral symbols
765         if (mustProvide("esint") &&
766             params_.use_package("esint") != BufferParams::package_off)
767                 packages << "\\usepackage{esint}\n";
768
769         // natbib.sty
770         // Some classes load natbib themselves, but still allow (or even require)
771         // plain numeric citations (ReVTeX is such a case, see bug 5182).
772         // This special case is indicated by the "natbib-internal" key.
773         if (mustProvide("natbib") && !tclass.provides("natbib-internal")) {
774                 packages << "\\usepackage[";
775                 if (params_.citeEngineType() == ENGINE_TYPE_NUMERICAL)
776                         packages << "numbers";
777                 else
778                         packages << "authoryear";
779                 packages << "]{natbib}\n";
780         }
781
782         // jurabib -- we need version 0.6 at least.
783         if (mustProvide("jurabib"))
784                 packages << "\\usepackage{jurabib}[2004/01/25]\n";
785
786         // xargs -- we need version 1.09 at least
787         if (mustProvide("xargs"))
788                 packages << "\\usepackage{xargs}[2008/03/08]\n";
789
790         // bibtopic -- the dot provides the aux file naming which
791         // LyX can detect.
792         if (mustProvide("bibtopic"))
793                 packages << "\\usepackage[dot]{bibtopic}\n";
794
795         if (mustProvide("xy"))
796                 packages << "\\usepackage[all]{xy}\n";
797
798         if (mustProvide("feyn"))
799                 packages << "\\usepackage{feyn}\n"; //Diagram
800
801         if (mustProvide("ulem"))
802                 packages << "\\PassOptionsToPackage{normalem}{ulem}\n"
803                             "\\usepackage{ulem}\n";
804
805         if (mustProvide("mhchem") &&
806             params_.use_package("mhchem") != BufferParams::package_off)
807                 packages << "\\PassOptionsToPackage{version=3}{mhchem}\n"
808                             "\\usepackage{mhchem}\n";
809
810         if (mustProvide("nomencl")) {
811                 // Make it work with the new and old version of the package,
812                 // but don't use the compatibility option since it is
813                 // incompatible to other packages.
814                 packages << "\\usepackage{nomencl}\n"
815                             "% the following is useful when we have the old nomencl.sty package\n"
816                             "\\providecommand{\\printnomenclature}{\\printglossary}\n"
817                             "\\providecommand{\\makenomenclature}{\\makeglossary}\n"
818                             "\\makenomenclature\n";
819         }
820
821         // fixltx2e provides subscript
822         if (mustProvide("subscript") && !isRequired("fixltx2e"))
823                 packages << "\\usepackage{subscript}\n";
824
825         return packages.str();
826 }
827
828
829 string LaTeXFeatures::getPreambleSnippets() const
830 {
831         ostringstream snip;
832         SnippetList::const_iterator pit  = preamble_snippets_.begin();
833         SnippetList::const_iterator pend = preamble_snippets_.end();
834         for (; pit != pend; ++pit)
835                 snip << *pit << '\n';
836         return snip.str();
837 }
838
839
840 std::string LaTeXFeatures::getCSSSnippets() const
841 {
842         ostringstream snip;
843         SnippetList::const_iterator pit  = css_snippets_.begin();
844         SnippetList::const_iterator pend = css_snippets_.end();
845         for (; pit != pend; ++pit)
846                 snip << *pit << '\n';
847         return snip.str();
848 }
849
850
851 docstring const LaTeXFeatures::getMacros() const
852 {
853         odocstringstream macros;
854
855         if (!preamble_snippets_.empty()) {
856                 macros << '\n';
857                 macros << from_utf8(getPreambleSnippets());
858         }
859
860         if (mustProvide("papersize")) {
861                 if (runparams_.flavor == OutputParams::LATEX)
862                         macros << papersizedvi_def << '\n';
863                 else
864                         macros << papersizepdf_def << '\n';
865         }
866
867         if (mustProvide("LyX")) {
868                 if (isRequired("hyperref"))
869                         macros << lyx_hyperref_def << '\n';
870                 else
871                         macros << lyx_def << '\n';
872         }
873
874         if (mustProvide("noun"))
875                 macros << noun_def << '\n';
876
877         if (mustProvide("lyxarrow"))
878                 macros << lyxarrow_def << '\n';
879
880         if (!usePolyglossia() && mustProvide("textgreek")) {
881                 // Avoid a LaTeX error if times fonts are used and the grtimes
882                 // package is installed but actual fonts are not (bug 6469).
883                 if (params_.fonts_roman == "times")
884                         macros << subst(textgreek_def,
885                                         from_ascii("\\greektext #1"),
886                                         from_ascii("%\n  \\IfFileExists"
887                                                    "{grtm10.tfm}{}{\\fontfamily"
888                                                    "{cmr}}\\greektext #1"))
889                                << '\n';
890                 else
891                         macros << textgreek_def << '\n';
892         }
893
894         if (!usePolyglossia() && mustProvide("textcyr"))
895                 macros << textcyr_def << '\n';
896
897         if (mustProvide("lyxmathsym"))
898                 macros << lyxmathsym_def << '\n';
899
900         if (mustProvide("cedilla"))
901                 macros << cedilla_def << '\n';
902
903         if (mustProvide("subring"))
904                 macros << subring_def << '\n';
905
906         if (mustProvide("subdot"))
907                 macros << subdot_def << '\n';
908
909         if (mustProvide("subhat"))
910                 macros << subhat_def << '\n';
911
912         if (mustProvide("subtilde"))
913                 macros << subtilde_def << '\n';
914
915         if (mustProvide("dacute"))
916                 macros << dacute_def << '\n';
917
918         if (mustProvide("tipasymb"))
919                 macros << tipasymb_def << '\n';
920
921         if (mustProvide("dgrave"))
922                 macros << dgrave_def << '\n';
923
924         if (mustProvide("rcap"))
925                 macros << rcap_def << '\n';
926
927         if (mustProvide("ogonek"))
928                 macros << ogonek_def << '\n';
929
930         // quotes.
931         if (mustProvide("quotesinglbase"))
932                 macros << quotesinglbase_def << '\n';
933         if (mustProvide("quotedblbase"))
934                 macros << quotedblbase_def << '\n';
935         if (mustProvide("guilsinglleft"))
936                 macros << guilsinglleft_def << '\n';
937         if (mustProvide("guilsinglright"))
938                 macros << guilsinglright_def << '\n';
939         if (mustProvide("guillemotleft"))
940                 macros << guillemotleft_def << '\n';
941         if (mustProvide("guillemotright"))
942                 macros << guillemotright_def << '\n';
943
944         // Math mode
945         if (mustProvide("binom") && !isRequired("amsmath"))
946                 macros << binom_def << '\n';
947         if (mustProvide("mathcircumflex"))
948                 macros << mathcircumflex_def << '\n';
949
950         // other
951         if (mustProvide("ParagraphLeftIndent"))
952                 macros << paragraphleftindent_def;
953         if (mustProvide("NeedLyXFootnoteCode"))
954                 macros << floatingfootnote_def;
955
956         // some problems with tex->html converters
957         if (mustProvide("NeedTabularnewline"))
958                 macros << tabularnewline_def;
959
960         // greyed-out environment (note inset)
961         // the color is specified in the routine
962         // getColorOptions() to avoid LaTeX-package clashes
963         if (mustProvide("lyxgreyedout"))
964                 macros << lyxgreyedout_def;
965
966         if (mustProvide("lyxdot"))
967                 macros << lyxdot_def << '\n';
968
969         // floats
970         getFloatDefinitions(macros);
971
972         if (mustProvide("refstyle"))
973                 macros << lyxref_def << '\n';
974
975         // change tracking
976         if (mustProvide("ct-dvipost"))
977                 macros << changetracking_dvipost_def;
978
979         if (mustProvide("ct-xcolor-ulem")) {
980                 streamsize const prec = macros.precision(2);
981
982                 RGBColor cadd = rgbFromHexName(lcolor.getX11Name(Color_addedtext));
983                 macros << "\\providecolor{lyxadded}{rgb}{"
984                        << cadd.r / 255.0 << ',' << cadd.g / 255.0 << ',' << cadd.b / 255.0 << "}\n";
985
986                 RGBColor cdel = rgbFromHexName(lcolor.getX11Name(Color_deletedtext));
987                 macros << "\\providecolor{lyxdeleted}{rgb}{"
988                        << cdel.r / 255.0 << ',' << cdel.g / 255.0 << ',' << cdel.b / 255.0 << "}\n";
989
990                 macros.precision(prec);
991
992                 if (isRequired("hyperref"))
993                         macros << changetracking_xcolor_ulem_hyperref_def;
994                 else
995                         macros << changetracking_xcolor_ulem_def;
996         }
997
998         if (mustProvide("ct-none"))
999                 macros << changetracking_none_def;
1000
1001         return macros.str();
1002 }
1003
1004
1005 string const LaTeXFeatures::getBabelPresettings() const
1006 {
1007         ostringstream tmp;
1008
1009         LanguageList::const_iterator it  = UsedLanguages_.begin();
1010         LanguageList::const_iterator end = UsedLanguages_.end();
1011         for (; it != end; ++it)
1012                 if (!(*it)->babel_presettings().empty())
1013                         tmp << (*it)->babel_presettings() << '\n';
1014         if (!params_.language->babel_presettings().empty())
1015                 tmp << params_.language->babel_presettings() << '\n';
1016
1017         return tmp.str();
1018 }
1019
1020
1021 string const LaTeXFeatures::getBabelPostsettings() const
1022 {
1023         ostringstream tmp;
1024
1025         LanguageList::const_iterator it  = UsedLanguages_.begin();
1026         LanguageList::const_iterator end = UsedLanguages_.end();
1027         for (; it != end; ++it)
1028                 if (!(*it)->babel_postsettings().empty())
1029                         tmp << (*it)->babel_postsettings() << '\n';
1030         if (!params_.language->babel_postsettings().empty())
1031                 tmp << params_.language->babel_postsettings() << '\n';
1032
1033         return tmp.str();
1034 }
1035
1036
1037 bool LaTeXFeatures::needBabelLangOptions() const
1038 {
1039         if (!lyxrc.language_global_options || params_.language->asBabelOptions())
1040                 return true;
1041
1042         LanguageList::const_iterator it  = UsedLanguages_.begin();
1043         LanguageList::const_iterator end = UsedLanguages_.end();
1044         for (; it != end; ++it)
1045                 if ((*it)->asBabelOptions())
1046                         return true;
1047
1048         return false;
1049 }
1050
1051
1052 string const LaTeXFeatures::loadAMSPackages() const
1053 {
1054         ostringstream tmp;
1055         if (mustProvide("amsthm"))
1056                 tmp << "\\usepackage{amsthm}\n";
1057
1058         if (mustProvide("amsmath")
1059             && params_.use_package("amsmath") != BufferParams::package_off) {
1060                 tmp << "\\usepackage{amsmath}\n";
1061         } else {
1062                 // amsbsy and amstext are already provided by amsmath
1063                 if (mustProvide("amsbsy"))
1064                         tmp << "\\usepackage{amsbsy}\n";
1065                 if (mustProvide("amstext"))
1066                         tmp << "\\usepackage{amstext}\n";
1067         }
1068
1069         if (mustProvide("amssymb")
1070             || params_.use_package("amsmath") == BufferParams::package_on)
1071                 tmp << "\\usepackage{amssymb}\n";
1072
1073         return tmp.str();
1074 }
1075
1076
1077 docstring const LaTeXFeatures::getTClassPreamble() const
1078 {
1079         // the text class specific preamble
1080         DocumentClass const & tclass = params_.documentClass();
1081         odocstringstream tcpreamble;
1082
1083         tcpreamble << tclass.preamble();
1084
1085         list<docstring>::const_iterator cit = usedLayouts_.begin();
1086         list<docstring>::const_iterator end = usedLayouts_.end();
1087         for (; cit != end; ++cit)
1088                 tcpreamble << tclass[*cit].preamble();
1089
1090         cit = usedInsetLayouts_.begin();
1091         end = usedInsetLayouts_.end();
1092         TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1093         for (; cit != end; ++cit) {
1094                 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1095                 if (it == ils.end())
1096                         continue;
1097                 tcpreamble << it->second.preamble();
1098         }
1099
1100         return tcpreamble.str();
1101 }
1102
1103
1104 docstring const LaTeXFeatures::getTClassHTMLPreamble() const
1105 {
1106         DocumentClass const & tclass = params_.documentClass();
1107         odocstringstream tcpreamble;
1108
1109         tcpreamble << tclass.htmlpreamble();
1110
1111         list<docstring>::const_iterator cit = usedLayouts_.begin();
1112         list<docstring>::const_iterator end = usedLayouts_.end();
1113         for (; cit != end; ++cit)
1114                 tcpreamble << tclass[*cit].htmlpreamble();
1115
1116         cit = usedInsetLayouts_.begin();
1117         end = usedInsetLayouts_.end();
1118         TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1119         for (; cit != end; ++cit) {
1120                 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1121                 if (it == ils.end())
1122                         continue;
1123                 tcpreamble << it->second.htmlpreamble();
1124         }
1125
1126         return tcpreamble.str();
1127 }
1128
1129
1130 docstring const LaTeXFeatures::getTClassHTMLStyles() const
1131 {
1132         DocumentClass const & tclass = params_.documentClass();
1133         odocstringstream tcpreamble;
1134
1135         tcpreamble << tclass.htmlstyles();
1136
1137         list<docstring>::const_iterator cit = usedLayouts_.begin();
1138         list<docstring>::const_iterator end = usedLayouts_.end();
1139         for (; cit != end; ++cit)
1140                 tcpreamble << tclass[*cit].htmlstyle();
1141
1142         cit = usedInsetLayouts_.begin();
1143         end = usedInsetLayouts_.end();
1144         TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1145         for (; cit != end; ++cit) {
1146                 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1147                 if (it == ils.end())
1148                         continue;
1149                 tcpreamble << it->second.htmlstyle();
1150         }
1151
1152         return tcpreamble.str();
1153 }
1154
1155
1156 namespace {
1157 docstring const getFloatI18nPreamble(docstring const & type,
1158                         docstring const & name, Language const * lang,
1159                         Encoding const & enc, bool const polyglossia)
1160 {
1161         // Check whether name can be encoded in the buffer encoding
1162         bool encodable = true;
1163         for (size_t i = 0; i < name.size(); ++i) {
1164                 if (enc.latexChar(name[i], true)[0] != name[i]) {
1165                         encodable = false;
1166                         break;
1167                 }
1168         }
1169
1170         docstring const language = polyglossia ? from_ascii(lang->polyglossia())
1171                                                : from_ascii(lang->babel());
1172         docstring const langenc = from_ascii(lang->encoding()->iconvName());
1173         docstring const texenc = from_ascii(lang->encoding()->latexName());
1174         docstring const bufenc = from_ascii(enc.iconvName());
1175         docstring const s1 = docstring(1, 0xF0000);
1176         docstring const s2 = docstring(1, 0xF0001);
1177         docstring const translated = encodable ? name
1178                 : from_ascii("\\inputencoding{") + texenc + from_ascii("}")
1179                         + s1 + langenc + s2 + name + s1 + bufenc + s2;
1180
1181         odocstringstream os;
1182         os << "\\addto\\captions" << language
1183            << "{\\renewcommand{\\" << type << "name}{" << translated << "}}\n";
1184         return os.str();
1185 }
1186 }
1187
1188
1189 docstring const LaTeXFeatures::getTClassI18nPreamble(bool use_babel, bool use_polyglossia) const
1190 {
1191         DocumentClass const & tclass = params_.documentClass();
1192         // collect preamble snippets in a set to prevent multiple identical
1193         // commands (would happen if e.g. both theorem and theorem* are used)
1194         set<docstring> snippets;
1195         typedef LanguageList::const_iterator lang_it;
1196         lang_it const lbeg = UsedLanguages_.begin();
1197         lang_it const lend =  UsedLanguages_.end();
1198         list<docstring>::const_iterator cit = usedLayouts_.begin();
1199         list<docstring>::const_iterator end = usedLayouts_.end();
1200         for (; cit != end; ++cit) {
1201                 // language dependent commands (once per document)
1202                 snippets.insert(tclass[*cit].langpreamble(buffer().language(),
1203                                                 buffer().params().encoding(),
1204                                                 use_polyglossia));
1205                 // commands for language changing (for multilanguage documents)
1206                 if ((use_babel || use_polyglossia) && !UsedLanguages_.empty()) {
1207                         snippets.insert(tclass[*cit].babelpreamble(
1208                                                 buffer().language(),
1209                                                 buffer().params().encoding(),
1210                                                 use_polyglossia));
1211                         for (lang_it lit = lbeg; lit != lend; ++lit)
1212                                 snippets.insert(tclass[*cit].babelpreamble(
1213                                                 *lit,
1214                                                 buffer().params().encoding(),
1215                                                 use_polyglossia));
1216                 }
1217         }
1218         if ((use_babel || use_polyglossia) && !UsedLanguages_.empty()) {
1219                 FloatList const & floats = params_.documentClass().floats();
1220                 UsedFloats::const_iterator fit = usedFloats_.begin();
1221                 UsedFloats::const_iterator fend = usedFloats_.end();
1222                 for (; fit != fend; ++fit) {
1223                         Floating const & fl = floats.getType(fit->first);
1224                         // we assume builtin floats are translated
1225                         if (fl.isPredefined())
1226                                 continue;
1227                         docstring const type = from_ascii(fl.floattype());
1228                         docstring const flname = from_utf8(fl.name());
1229                         docstring name = buffer().language()->translateLayout(fl.name());
1230                         // only request translation if we have a real translation
1231                         // (that differs from the source)
1232                         if (flname != name)
1233                                 snippets.insert(getFloatI18nPreamble(
1234                                                 type, name, buffer().language(),
1235                                                 buffer().params().encoding(),
1236                                                 use_polyglossia));
1237                         for (lang_it lit = lbeg; lit != lend; ++lit) {
1238                                 string const code = (*lit)->code();
1239                                 name = (*lit)->translateLayout(fl.name());
1240                                 // we assume we have a suitable translation if
1241                                 // either the language is English (we need to
1242                                 // translate into English if English is a secondary
1243                                 // language) or if translateIfPossible returns
1244                                 // something different to the English source.
1245                                 bool const have_translation =
1246                                         (flname != name || contains(code, "en"));
1247                                 if (have_translation)
1248                                         snippets.insert(getFloatI18nPreamble(
1249                                                 type, name, *lit,
1250                                                 buffer().params().encoding(),
1251                                                 use_polyglossia));
1252                         }
1253                 }
1254         }
1255
1256         odocstringstream tcpreamble;
1257         set<docstring>::const_iterator const send = snippets.end();
1258         set<docstring>::const_iterator it = snippets.begin();
1259         for (; it != send; ++it)
1260                 tcpreamble << *it;
1261         return tcpreamble.str();
1262 }
1263
1264
1265 docstring const LaTeXFeatures::getLyXSGMLEntities() const
1266 {
1267         // Definition of entities used in the document that are LyX related.
1268         odocstringstream entities;
1269
1270         if (mustProvide("lyxarrow")) {
1271                 entities << "<!ENTITY lyxarrow \"-&gt;\">" << '\n';
1272         }
1273
1274         return entities.str();
1275 }
1276
1277
1278 docstring const LaTeXFeatures::getIncludedFiles(string const & fname) const
1279 {
1280         odocstringstream sgmlpreamble;
1281         // FIXME UNICODE
1282         docstring const basename(from_utf8(onlyPath(fname)));
1283
1284         FileMap::const_iterator end = IncludedFiles_.end();
1285         for (FileMap::const_iterator fi = IncludedFiles_.begin();
1286              fi != end; ++fi)
1287                 // FIXME UNICODE
1288                 sgmlpreamble << "\n<!ENTITY " << fi->first
1289                              << (isSGMLFileName(fi->second) ? " SYSTEM \"" : " \"")
1290                              << makeRelPath(from_utf8(fi->second), basename) << "\">";
1291
1292         return sgmlpreamble.str();
1293 }
1294
1295
1296 void LaTeXFeatures::showStruct() const
1297 {
1298         lyxerr << "LyX needs the following commands when LaTeXing:"
1299                << "\n***** Packages:" << getPackages()
1300                << "\n***** Macros:" << to_utf8(getMacros())
1301                << "\n***** Textclass stuff:" << to_utf8(getTClassPreamble())
1302                << "\n***** done." << endl;
1303 }
1304
1305
1306 Buffer const & LaTeXFeatures::buffer() const
1307 {
1308         return *buffer_;
1309 }
1310
1311
1312 void LaTeXFeatures::setBuffer(Buffer const & buffer)
1313 {
1314         buffer_ = &buffer;
1315 }
1316
1317
1318 BufferParams const & LaTeXFeatures::bufferParams() const
1319 {
1320         return params_;
1321 }
1322
1323
1324 void LaTeXFeatures::getFloatDefinitions(odocstream & os) const
1325 {
1326         FloatList const & floats = params_.documentClass().floats();
1327
1328         // Here we will output the code to create the needed float styles.
1329         // We will try to do this as minimal as possible.
1330         // \floatstyle{ruled}
1331         // \newfloat{algorithm}{htbp}{loa}
1332         // \providecommand{\algorithmname}{Algorithm}
1333         // \floatname{algorithm}{\protect\algorithmname}
1334         UsedFloats::const_iterator cit = usedFloats_.begin();
1335         UsedFloats::const_iterator end = usedFloats_.end();
1336         for (; cit != end; ++cit) {
1337                 Floating const & fl = floats.getType(cit->first);
1338
1339                 // For builtin floats we do nothing.
1340                 if (fl.isPredefined())
1341                         continue;
1342
1343                 // We have to special case "table" and "figure"
1344                 if (fl.floattype() == "tabular" || fl.floattype() == "figure") {
1345                         // Output code to modify "table" or "figure"
1346                         // but only if builtin == false
1347                         // and that have to be true at this point in the
1348                         // function.
1349                         docstring const type = from_ascii(fl.floattype());
1350                         docstring const placement = from_ascii(fl.placement());
1351                         docstring const style = from_ascii(fl.style());
1352                         if (!style.empty()) {
1353                                 os << "\\floatstyle{" << style << "}\n"
1354                                    << "\\restylefloat{" << type << "}\n";
1355                         }
1356                         if (!placement.empty()) {
1357                                 os << "\\floatplacement{" << type << "}{"
1358                                    << placement << "}\n";
1359                         }
1360                 } else {
1361                         // The other non builtin floats.
1362
1363                         docstring const type = from_ascii(fl.floattype());
1364                         docstring const placement = from_ascii(fl.placement());
1365                         docstring const ext = from_ascii(fl.ext());
1366                         docstring const within = from_ascii(fl.within());
1367                         docstring const style = from_ascii(fl.style());
1368                         docstring const name =
1369                                 buffer().language()->translateLayout(fl.name());
1370                         os << "\\floatstyle{" << style << "}\n"
1371                            << "\\newfloat{" << type << "}{" << placement
1372                            << "}{" << ext << '}';
1373                         if (!within.empty())
1374                                 os << '[' << within << ']';
1375                         os << '\n'
1376                            << "\\providecommand{\\" << type << "name}{"
1377                            << name << "}\n"
1378                            << "\\floatname{" << type << "}{\\protect\\"
1379                            << type << "name}\n";
1380
1381                         // What missing here is to code to minimalize the code
1382                         // output so that the same floatstyle will not be
1383                         // used several times, when the same style is still in
1384                         // effect. (Lgb)
1385                 }
1386                 if (cit->second)
1387                         os << "\n\\newsubfloat{" << from_ascii(fl.floattype()) << "}\n";
1388         }
1389 }
1390
1391
1392 void LaTeXFeatures::resolveAlternatives()
1393 {
1394         for (Features::iterator it = features_.begin(); it != features_.end();) {
1395                 if (contains(*it, '|')) {
1396                         vector<string> const alternatives = getVectorFromString(*it, "|");
1397                         vector<string>::const_iterator const end = alternatives.end();
1398                         vector<string>::const_iterator ita = alternatives.begin();
1399                         for (; ita != end; ++ita) {
1400                                 if (isRequired(*ita))
1401                                         break;
1402                         }
1403                         if (ita == end)
1404                                 require(alternatives.front());
1405                         features_.erase(it);
1406                         it = features_.begin();
1407                 } else
1408                         ++it;
1409         }
1410 }
1411
1412
1413 } // namespace lyx