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