]> git.lyx.org Git - lyx.git/blob - src/LaTeXFeatures.cpp
528ef460ceca6f20c0a2e63cfd121986161fddfd
[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::useFloat(string const & name, bool subfloat)
427 {
428         if (!usedFloats_[name])
429                 usedFloats_[name] = subfloat;
430         if (subfloat)
431                 require("subfig");
432         // We only need float.sty if we use non builtin floats, or if we
433         // use the "H" modifier. This includes modified table and
434         // figure floats. (Lgb)
435         Floating const & fl = params_.documentClass().floats().getType(name);
436         if (!fl.floattype().empty() && fl.usesFloatPkg()) {
437                 require("float");
438         }
439 }
440
441
442 void LaTeXFeatures::useLanguage(Language const * lang)
443 {
444         if (!lang->babel().empty())
445                 UsedLanguages_.insert(lang);
446         if (lang->lang() == "vietnamese")
447                 require("vietnamese");
448         // CJK languages do not have a babel name.
449         // They use the CJK package
450         if (lang->encoding()->package() == Encoding::CJK)
451                 require("CJK");
452         // japanese package is special
453         if (lang->encoding()->package() == Encoding::japanese)
454                 require("japanese");
455 }
456
457
458 void LaTeXFeatures::includeFile(docstring const & key, string const & name)
459 {
460         IncludedFiles_[key] = name;
461 }
462
463
464 bool LaTeXFeatures::hasLanguages() const
465 {
466         return !UsedLanguages_.empty();
467 }
468
469
470 bool LaTeXFeatures::hasPolyglossiaLanguages() const
471 {
472         LanguageList::const_iterator const begin = UsedLanguages_.begin();
473         for (LanguageList::const_iterator cit = begin;
474              cit != UsedLanguages_.end();
475              ++cit) {
476                 if ((*cit)->polyglossia().empty())
477                         return false;
478         }
479         return true;
480 }
481
482
483 string LaTeXFeatures::getLanguages() const
484 {
485         ostringstream languages;
486
487         LanguageList::const_iterator const begin = UsedLanguages_.begin();
488         for (LanguageList::const_iterator cit = begin;
489              cit != UsedLanguages_.end();
490              ++cit) {
491                 if (cit != begin)
492                         languages << ',';
493                 languages << (*cit)->babel();
494         }
495         return languages.str();
496 }
497
498
499 std::map<std::string, std::string> LaTeXFeatures::getPolyglossiaLanguages() const
500 {
501         std::map<std::string, std::string> languages;
502
503         LanguageList::const_iterator const begin = UsedLanguages_.begin();
504         for (LanguageList::const_iterator cit = begin;
505              cit != UsedLanguages_.end();
506              ++cit) {
507                 languages[(*cit)->polyglossia()] = (*cit)->polyglossiaOpts();
508         }
509         return languages;
510 }
511
512
513 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding) const
514 {
515         // This does only find encodings of languages supported by babel, but
516         // that does not matter since we don't have a language with an
517         // encoding supported by inputenc but without babel support.
518         set<string> encodings;
519         LanguageList::const_iterator it  = UsedLanguages_.begin();
520         LanguageList::const_iterator end = UsedLanguages_.end();
521         for (; it != end; ++it)
522                 if ((*it)->encoding()->latexName() != doc_encoding &&
523                     ((*it)->encoding()->package() == Encoding::inputenc
524                      || (*it)->encoding()->package() == Encoding::japanese))
525                         encodings.insert((*it)->encoding()->latexName());
526         return encodings;
527 }
528
529 namespace {
530
531 char const * simplefeatures[] = {
532 // note that the package order here will be the same in the LaTeX-output
533         "array",
534         "verbatim",
535         "longtable",
536         "rotating",
537         "latexsym",
538         "pifont",
539         // subfig is handled in BufferParams.cpp
540         "varioref",
541         "prettyref",
542         "refstyle",
543         /*For a successful cooperation of the `wrapfig' package with the
544           `float' package you should load the `wrapfig' package *after*
545           the `float' package. See the caption package documentation
546           for explanation.*/
547         "float",
548         "rotfloat",
549         "wrapfig",
550         "booktabs",
551         "dvipost",
552         "fancybox",
553         "calc",
554         "units",
555         "tipa",
556         "tipx",
557         "framed",
558         "soul",
559         "textcomp",
560         "pmboxdraw",
561         "bbding",
562         "ifsym",
563         "marvosym",
564         "txfonts",
565         "pxfonts",
566         "mathdesign",
567         "mathrsfs",
568         "mathabx",
569         "ascii",
570         "url",
571         "covington",
572         "csquotes",
573         "enumitem",
574         "endnotes",
575         "hhline",
576         "ifthen",
577         // listings is handled in BufferParams.cpp
578         "bm",
579         "pdfpages",
580         "amscd",
581         "slashed",
582         "multirow",
583         "tfrupee"
584 };
585
586 int const nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
587
588 }
589
590
591 string const LaTeXFeatures::getColorOptions() const
592 {
593         ostringstream colors;
594
595         // Handling the color packages separately is needed to be able to load them
596         // before babel when hyperref is loaded with the colorlinks option
597         // for more info see Bufferparams.cpp
598
599         // [x]color.sty
600         if (mustProvide("color") || mustProvide("xcolor")) {
601                 string const package =
602                         (mustProvide("xcolor") ? "xcolor" : "color");
603                 if (params_.graphics_driver == "default"
604                         || params_.graphics_driver == "none")
605                         colors << "\\usepackage{" << package << "}\n";
606                 else
607                         colors << "\\usepackage["
608                                  << params_.graphics_driver
609                                  << "]{" << package << "}\n";
610         }
611
612         // pdfcolmk must be loaded after color
613         if (mustProvide("pdfcolmk"))
614                 colors << "\\usepackage{pdfcolmk}\n";
615
616         // the following 3 color commands must be set after color
617         // is loaded and before pdfpages, therefore add the command
618         // here define the set color
619         if (mustProvide("pagecolor")) {
620                 colors << "\\definecolor{page_backgroundcolor}{rgb}{";
621                 colors << outputLaTeXColor(params_.backgroundcolor) << "}\n";
622                 // set the page color
623                 colors << "\\pagecolor{page_backgroundcolor}\n";
624         }
625
626         if (mustProvide("fontcolor")) {
627                 colors << "\\definecolor{document_fontcolor}{rgb}{";
628                 colors << outputLaTeXColor(params_.fontcolor) << "}\n";
629                 // set the color
630                 colors << "\\color{document_fontcolor}\n";
631         }
632
633         if (mustProvide("lyxgreyedout")) {
634                 colors << "\\definecolor{note_fontcolor}{rgb}{";
635                 colors << outputLaTeXColor(params_.notefontcolor) << "}\n";
636                 // the color will be set together with the definition of
637                 // the lyxgreyedout environment (see lyxgreyedout_def)
638         }
639
640         // color for shaded boxes
641         if (isRequired("framed") && mustProvide("color")) {
642                 colors << "\\definecolor{shadecolor}{rgb}{";
643                 colors << outputLaTeXColor(params_.boxbgcolor) << "}\n";
644                 // this color is automatically used by the LaTeX-package "framed"
645         }
646
647         return colors.str();
648 }
649
650
651 string const LaTeXFeatures::getPackages() const
652 {
653         ostringstream packages;
654         DocumentClass const & tclass = params_.documentClass();
655
656         // FIXME: currently, we can only load packages and macros known
657         // to LyX.
658         // However, with the Require tag of layouts/custom insets,
659         // also unknown packages can be requested. They are silently
660         // swallowed now. We should change this eventually.
661
662         //
663         //  These are all the 'simple' includes.  i.e
664         //  packages which we just \usepackage{package}
665         //
666         for (int i = 0; i < nb_simplefeatures; ++i) {
667                 if (mustProvide(simplefeatures[i]))
668                         packages << "\\usepackage{"
669                                  << simplefeatures[i] << "}\n";
670         }
671
672         //
673         // The rest of these packages are somewhat more complicated
674         // than those above.
675         //
676
677         // if fontspec is used, AMS packages have to be loaded before
678         // fontspec (in BufferParams)
679         string const amsPackages = loadAMSPackages();
680         if (!params_.useNonTeXFonts && !amsPackages.empty())
681                 packages << amsPackages;
682
683         // fixltx2e must be loaded after amsthm, since amsthm produces an error with
684         // the redefined \[ command (bug 7233). Load is as early as possible, since
685         // other packages might profit from it.
686         if (mustProvide("fixltx2e"))
687                 packages << "\\usepackage{fixltx2e}\n";
688
689         // wasysym is a simple feature, but it must be after amsmath if both
690         // are used
691         // wasysym redefines some integrals (e.g. iint) from amsmath. That
692         // leads to inconsistent integrals. We only load this package if
693         // the document does not contain integrals (then isRequired("esint")
694         // is false) or if esint is used, since esint redefines all relevant
695         // integral symbols from wasysym and amsmath.
696         // See http://www.lyx.org/trac/ticket/1942
697         if (mustProvide("wasysym") &&
698             (params_.use_esint != BufferParams::package_off || !isRequired("esint")))
699                 packages << "\\usepackage{wasysym}\n";
700
701         // accents must be loaded after amsmath
702         if (mustProvide("accents"))
703                 packages << "\\usepackage{accents}\n";
704
705         // mathdots must be loaded after amsmath
706         if (mustProvide("mathdots") &&
707                 params_.use_mathdots != BufferParams::package_off)
708                 packages << "\\usepackage{mathdots}\n";
709
710         // yhmath must be loaded after amsmath
711         if (mustProvide("yhmath"))
712                 packages << "\\usepackage{yhmath}\n";
713
714         if (mustProvide("undertilde") &&
715                 params_.use_undertilde != BufferParams::package_off)
716                 packages << "\\usepackage{undertilde}\n";
717
718         // [x]color and pdfcolmk are handled in getColorOptions() above
719         
720         // makeidx.sty
721         if (isRequired("makeidx") || isRequired("splitidx")) {
722                 if (!tclass.provides("makeidx") && !isRequired("splitidx"))
723                         packages << "\\usepackage{makeidx}\n";
724                 if (!tclass.provides("splitidx") && isRequired("splitidx"))
725                         packages << "\\usepackage{splitidx}\n";
726                 packages << "\\makeindex\n";
727         }
728
729         // graphicx.sty
730         if (mustProvide("graphicx") && params_.graphics_driver != "none") {
731                 if (params_.graphics_driver == "default")
732                         packages << "\\usepackage{graphicx}\n";
733                 else
734                         packages << "\\usepackage["
735                                  << params_.graphics_driver
736                                  << "]{graphicx}\n";
737         }
738         
739         // lyxskak.sty --- newer chess support based on skak.sty
740         if (mustProvide("chess"))
741                 packages << "\\usepackage[ps,mover]{lyxskak}\n";
742
743         // setspace.sty
744         if (mustProvide("setspace") && !tclass.provides("SetSpace"))
745                 packages << "\\usepackage{setspace}\n";
746
747         // esint must be after amsmath and wasysym, since it will redeclare
748         // inconsistent integral symbols
749         if (mustProvide("esint") &&
750             params_.use_esint != BufferParams::package_off)
751                 packages << "\\usepackage{esint}\n";
752
753         // natbib.sty
754         // Some classes load natbib themselves, but still allow (or even require)
755         // plain numeric citations (ReVTeX is such a case, see bug 5182).
756         // This special case is indicated by the "natbib-internal" key.
757         if (mustProvide("natbib") && !tclass.provides("natbib-internal")) {
758                 packages << "\\usepackage[";
759                 if (params_.citeEngine() == ENGINE_NATBIB_NUMERICAL)
760                         packages << "numbers";
761                 else
762                         packages << "authoryear";
763                 packages << "]{natbib}\n";
764         }
765
766         // jurabib -- we need version 0.6 at least.
767         if (mustProvide("jurabib"))
768                 packages << "\\usepackage{jurabib}[2004/01/25]\n";
769         
770         // xargs -- we need version 1.09 at least
771         if (mustProvide("xargs"))
772                 packages << "\\usepackage{xargs}[2008/03/08]\n";
773
774         // bibtopic -- the dot provides the aux file naming which
775         // LyX can detect.
776         if (mustProvide("bibtopic"))
777                 packages << "\\usepackage[dot]{bibtopic}\n";
778
779         if (mustProvide("xy"))
780                 packages << "\\usepackage[all]{xy}\n";
781
782         if (mustProvide("feyn"))
783                 packages << "\\usepackage{feyn}\n"; //Diagram
784
785         if (mustProvide("ulem"))
786                 packages << "\\PassOptionsToPackage{normalem}{ulem}\n"
787                             "\\usepackage{ulem}\n";
788
789         if (params_.use_mhchem == BufferParams::package_on ||
790             (mustProvide("mhchem") &&
791              params_.use_mhchem != BufferParams::package_off))
792                 packages << "\\PassOptionsToPackage{version=3}{mhchem}\n"
793                             "\\usepackage{mhchem}\n";
794
795         if (mustProvide("nomencl")) {
796                 // Make it work with the new and old version of the package,
797                 // but don't use the compatibility option since it is
798                 // incompatible to other packages.
799                 packages << "\\usepackage{nomencl}\n"
800                             "% the following is useful when we have the old nomencl.sty package\n"
801                             "\\providecommand{\\printnomenclature}{\\printglossary}\n"
802                             "\\providecommand{\\makenomenclature}{\\makeglossary}\n"
803                             "\\makenomenclature\n";
804         }
805
806         // fixltx2e provides subscript
807         if (mustProvide("subscript") && !isRequired("fixltx2e"))
808                 packages << "\\usepackage{subscript}\n";
809
810         return packages.str();
811 }
812
813
814 string LaTeXFeatures::getPreambleSnippets() const 
815 {
816         ostringstream snip;
817         SnippetList::const_iterator pit  = preamble_snippets_.begin();
818         SnippetList::const_iterator pend = preamble_snippets_.end();
819         for (; pit != pend; ++pit)
820                 snip << *pit << '\n';
821         return snip.str();
822 }
823
824
825 docstring const LaTeXFeatures::getMacros() const
826 {
827         odocstringstream macros;
828
829         if (!preamble_snippets_.empty()) {
830                 macros << '\n';
831                 macros << from_utf8(getPreambleSnippets());
832         }
833
834         if (mustProvide("papersize")) {
835                 if (runparams_.flavor == OutputParams::LATEX)
836                         macros << papersizedvi_def << '\n';
837                 else
838                         macros << papersizepdf_def << '\n';
839         }
840
841         if (mustProvide("LyX")) {
842                 if (isRequired("hyperref"))
843                         macros << lyx_hyperref_def << '\n';
844                 else
845                         macros << lyx_def << '\n';
846         }
847
848         if (mustProvide("noun"))
849                 macros << noun_def << '\n';
850
851         if (mustProvide("lyxarrow"))
852                 macros << lyxarrow_def << '\n';
853
854         if (!usePolyglossia() && mustProvide("textgreek")) {
855                 // Avoid a LaTeX error if times fonts are used and the grtimes
856                 // package is installed but actual fonts are not (bug 6469).
857                 if (params_.fonts_roman == "times")
858                         macros << subst(textgreek_def,
859                                         from_ascii("\\greektext #1"),
860                                         from_ascii("%\n  \\IfFileExists"
861                                                    "{grtm10.tfm}{}{\\fontfamily"
862                                                    "{cmr}}\\greektext #1"))
863                                << '\n';
864                 else
865                         macros << textgreek_def << '\n';
866         }
867
868         if (!usePolyglossia() && mustProvide("textcyr"))
869                 macros << textcyr_def << '\n';
870
871         if (mustProvide("lyxmathsym"))
872                 macros << lyxmathsym_def << '\n';
873
874         if (mustProvide("cedilla"))
875                 macros << cedilla_def << '\n';
876
877         if (mustProvide("subring"))
878                 macros << subring_def << '\n';
879
880         if (mustProvide("subdot"))
881                 macros << subdot_def << '\n';
882
883         if (mustProvide("subhat"))
884                 macros << subhat_def << '\n';
885
886         if (mustProvide("subtilde"))
887                 macros << subtilde_def << '\n';
888
889         if (mustProvide("dacute"))
890                 macros << dacute_def << '\n';
891
892         if (mustProvide("tipasymb"))
893                 macros << tipasymb_def << '\n';
894
895         if (mustProvide("dgrave"))
896                 macros << dgrave_def << '\n';
897
898         if (mustProvide("rcap"))
899                 macros << rcap_def << '\n';
900
901         if (mustProvide("ogonek"))
902                 macros << ogonek_def << '\n';
903
904         // quotes.
905         if (mustProvide("quotesinglbase"))
906                 macros << quotesinglbase_def << '\n';
907         if (mustProvide("quotedblbase"))
908                 macros << quotedblbase_def << '\n';
909         if (mustProvide("guilsinglleft"))
910                 macros << guilsinglleft_def << '\n';
911         if (mustProvide("guilsinglright"))
912                 macros << guilsinglright_def << '\n';
913         if (mustProvide("guillemotleft"))
914                 macros << guillemotleft_def << '\n';
915         if (mustProvide("guillemotright"))
916                 macros << guillemotright_def << '\n';
917
918         // Math mode
919         if (mustProvide("binom") && !isRequired("amsmath"))
920                 macros << binom_def << '\n';
921         if (mustProvide("mathcircumflex"))
922                 macros << mathcircumflex_def << '\n';
923
924         // other
925         if (mustProvide("ParagraphLeftIndent"))
926                 macros << paragraphleftindent_def;
927         if (mustProvide("NeedLyXFootnoteCode"))
928                 macros << floatingfootnote_def;
929
930         // some problems with tex->html converters
931         if (mustProvide("NeedTabularnewline"))
932                 macros << tabularnewline_def;
933
934         // greyed-out environment (note inset)
935         // the color is specified in the routine
936         // getColorOptions() to avoid LaTeX-package clashes
937         if (mustProvide("lyxgreyedout"))
938                 macros << lyxgreyedout_def;
939
940         if (mustProvide("lyxdot"))
941                 macros << lyxdot_def << '\n';
942
943         // floats
944         getFloatDefinitions(macros);
945         
946         if (mustProvide("refstyle")) 
947                 macros << lyxref_def << '\n';   
948         
949         // change tracking
950         if (mustProvide("ct-dvipost"))
951                 macros << changetracking_dvipost_def;
952         
953         if (mustProvide("ct-xcolor-ulem")) {
954                 streamsize const prec = macros.precision(2);
955         
956                 RGBColor cadd = rgbFromHexName(lcolor.getX11Name(Color_addedtext));
957                 macros << "\\providecolor{lyxadded}{rgb}{"
958                        << cadd.r / 255.0 << ',' << cadd.g / 255.0 << ',' << cadd.b / 255.0 << "}\n";
959
960                 RGBColor cdel = rgbFromHexName(lcolor.getX11Name(Color_deletedtext));
961                 macros << "\\providecolor{lyxdeleted}{rgb}{"
962                        << cdel.r / 255.0 << ',' << cdel.g / 255.0 << ',' << cdel.b / 255.0 << "}\n";
963
964                 macros.precision(prec);
965                 
966                 if (isRequired("hyperref"))
967                         macros << changetracking_xcolor_ulem_hyperref_def;
968                 else
969                         macros << changetracking_xcolor_ulem_def;
970         }
971
972         if (mustProvide("ct-none"))
973                 macros << changetracking_none_def;
974
975         return macros.str();
976 }
977
978
979 string const LaTeXFeatures::getBabelPresettings() const
980 {
981         ostringstream tmp;
982
983         LanguageList::const_iterator it  = UsedLanguages_.begin();
984         LanguageList::const_iterator end = UsedLanguages_.end();
985         for (; it != end; ++it)
986                 if (!(*it)->babel_presettings().empty())
987                         tmp << (*it)->babel_presettings() << '\n';
988         if (!params_.language->babel_presettings().empty())
989                 tmp << params_.language->babel_presettings() << '\n';
990
991         return tmp.str();
992 }
993
994
995 string const LaTeXFeatures::getBabelPostsettings() const
996 {
997         ostringstream tmp;
998
999         LanguageList::const_iterator it  = UsedLanguages_.begin();
1000         LanguageList::const_iterator end = UsedLanguages_.end();
1001         for (; it != end; ++it)
1002                 if (!(*it)->babel_postsettings().empty())
1003                         tmp << (*it)->babel_postsettings() << '\n';
1004         if (!params_.language->babel_postsettings().empty())
1005                 tmp << params_.language->babel_postsettings() << '\n';
1006
1007         return tmp.str();
1008 }
1009
1010
1011 bool LaTeXFeatures::needBabelLangOptions() const
1012 {
1013         if (!lyxrc.language_global_options || params_.language->asBabelOptions())
1014                 return true;
1015
1016         LanguageList::const_iterator it  = UsedLanguages_.begin();
1017         LanguageList::const_iterator end = UsedLanguages_.end();
1018         for (; it != end; ++it)
1019                 if ((*it)->asBabelOptions())
1020                         return true;
1021
1022         return false;
1023 }
1024
1025
1026 string const LaTeXFeatures::loadAMSPackages() const
1027 {
1028         ostringstream tmp;
1029         if (mustProvide("amsthm"))
1030                 tmp << "\\usepackage{amsthm}\n";
1031
1032         if (mustProvide("amsmath")
1033             && params_.use_amsmath != BufferParams::package_off) {
1034                 tmp << "\\usepackage{amsmath}\n";
1035         } else {
1036                 // amsbsy and amstext are already provided by amsmath
1037                 if (mustProvide("amsbsy"))
1038                         tmp << "\\usepackage{amsbsy}\n";
1039                 if (mustProvide("amstext"))
1040                         tmp << "\\usepackage{amstext}\n";
1041         }
1042
1043         if (mustProvide("amssymb")
1044             || params_.use_amsmath == BufferParams::package_on)
1045                 tmp << "\\usepackage{amssymb}\n";
1046
1047         return tmp.str();
1048 }
1049
1050
1051 docstring const LaTeXFeatures::getTClassPreamble() const
1052 {
1053         // the text class specific preamble
1054         DocumentClass const & tclass = params_.documentClass();
1055         odocstringstream tcpreamble;
1056
1057         tcpreamble << tclass.preamble();
1058
1059         list<docstring>::const_iterator cit = usedLayouts_.begin();
1060         list<docstring>::const_iterator end = usedLayouts_.end();
1061         for (; cit != end; ++cit)
1062                 tcpreamble << tclass[*cit].preamble();
1063
1064         cit = usedInsetLayouts_.begin();
1065         end = usedInsetLayouts_.end();
1066         TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1067         for (; cit != end; ++cit) {
1068                 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1069                 if (it == ils.end())
1070                         continue;
1071                 tcpreamble << it->second.preamble();
1072         }
1073
1074         return tcpreamble.str();
1075 }
1076
1077
1078 docstring const LaTeXFeatures::getTClassHTMLPreamble() const 
1079 {
1080         DocumentClass const & tclass = params_.documentClass();
1081         odocstringstream tcpreamble;
1082
1083         tcpreamble << tclass.htmlpreamble();
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].htmlpreamble();
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.htmlpreamble();
1098         }
1099
1100         return tcpreamble.str();
1101 }
1102
1103
1104 docstring const LaTeXFeatures::getTClassHTMLStyles() const {
1105         DocumentClass const & tclass = params_.documentClass();
1106         odocstringstream tcpreamble;
1107
1108         list<docstring>::const_iterator cit = usedLayouts_.begin();
1109         list<docstring>::const_iterator end = usedLayouts_.end();
1110         for (; cit != end; ++cit)
1111                 tcpreamble << tclass[*cit].htmlstyle();
1112
1113         cit = usedInsetLayouts_.begin();
1114         end = usedInsetLayouts_.end();
1115         TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1116         for (; cit != end; ++cit) {
1117                 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1118                 if (it == ils.end())
1119                         continue;
1120                 tcpreamble << it->second.htmlstyle();
1121         }
1122
1123         return tcpreamble.str();
1124 }
1125
1126
1127 namespace {
1128 docstring const getFloatI18nPreamble(docstring const & type,
1129                         docstring const & name, Language const * lang,
1130                         Encoding const & enc, bool const polyglossia)
1131 {
1132         docstring const language = polyglossia ? from_ascii(lang->polyglossia())
1133                                                : from_ascii(lang->babel());
1134         docstring const langenc = from_ascii(lang->encoding()->iconvName());
1135         docstring const texenc = from_ascii(lang->encoding()->latexName());
1136         docstring const bufenc = from_ascii(enc.iconvName());
1137         docstring const s1 = docstring(1, 0xF0000);
1138         docstring const s2 = docstring(1, 0xF0001);
1139         docstring const translated = (langenc == bufenc) ? name
1140                 : from_ascii("\\inputencoding{") + texenc + from_ascii("}")
1141                         + s1 + langenc + s2 + translated + s1 + bufenc + s2;
1142
1143         odocstringstream os;
1144         os << "\\addto\\captions" << language
1145            << "{\\renewcommand{\\" << type << "name}{" << translated << "}}\n";
1146         return os.str();
1147 }
1148 }
1149
1150
1151 docstring const LaTeXFeatures::getTClassI18nPreamble(bool use_babel, bool use_polyglossia) const
1152 {
1153         DocumentClass const & tclass = params_.documentClass();
1154         // collect preamble snippets in a set to prevent multiple identical
1155         // commands (would happen if e.g. both theorem and theorem* are used)
1156         set<docstring> snippets;
1157         typedef LanguageList::const_iterator lang_it;
1158         lang_it const lbeg = UsedLanguages_.begin();
1159         lang_it const lend =  UsedLanguages_.end();
1160         list<docstring>::const_iterator cit = usedLayouts_.begin();
1161         list<docstring>::const_iterator end = usedLayouts_.end();
1162         for (; cit != end; ++cit) {
1163                 // language dependent commands (once per document)
1164                 snippets.insert(tclass[*cit].langpreamble(buffer().language(),
1165                                                 buffer().params().encoding(),
1166                                                 use_polyglossia));
1167                 // commands for language changing (for multilanguage documents)
1168                 if ((use_babel || use_polyglossia) && !UsedLanguages_.empty()) {
1169                         snippets.insert(tclass[*cit].babelpreamble(
1170                                                 buffer().language(),
1171                                                 buffer().params().encoding(),
1172                                                 use_polyglossia));
1173                         for (lang_it lit = lbeg; lit != lend; ++lit)
1174                                 snippets.insert(tclass[*cit].babelpreamble(
1175                                                 *lit,
1176                                                 buffer().params().encoding(),
1177                                                 use_polyglossia));
1178                 }
1179         }
1180         if ((use_babel || use_polyglossia) && !UsedLanguages_.empty()) {
1181                 FloatList const & floats = params_.documentClass().floats();
1182                 UsedFloats::const_iterator fit = usedFloats_.begin();
1183                 UsedFloats::const_iterator fend = usedFloats_.end();
1184                 for (; fit != fend; ++fit) {
1185                         Floating const & fl = floats.getType(fit->first);
1186                         // we assume builtin floats are translated 
1187                         if (fl.isPredefined())
1188                                 continue;
1189                         docstring const type = from_ascii(fl.floattype());
1190                         docstring const flname = from_utf8(fl.name());
1191                         docstring name = buffer().language()->translateLayout(fl.name());
1192                         // only request translation if we have a real translation
1193                         // (that differs from the source)
1194                         if (flname != name)
1195                                 snippets.insert(getFloatI18nPreamble(
1196                                                 type, name, buffer().language(),
1197                                                 buffer().params().encoding(),
1198                                                 use_polyglossia));
1199                         for (lang_it lit = lbeg; lit != lend; ++lit) {
1200                                 string const code = (*lit)->code();
1201                                 name = (*lit)->translateLayout(fl.name());
1202                                 // we assume we have a suitable translation if
1203                                 // either the language is English (we need to
1204                                 // translate into English if English is a secondary
1205                                 // language) or if translateIfPossible returns
1206                                 // something different to the English source.
1207                                 bool const have_translation =
1208                                         (flname != name || contains(code, "en"));
1209                                 if (have_translation)
1210                                         snippets.insert(getFloatI18nPreamble(
1211                                                 type, name, *lit,
1212                                                 buffer().params().encoding(),
1213                                                 use_polyglossia));
1214                         }
1215                 }
1216         }
1217
1218         odocstringstream tcpreamble;
1219         set<docstring>::const_iterator const send = snippets.end();
1220         set<docstring>::const_iterator it = snippets.begin();
1221         for (; it != send; ++it)
1222                 tcpreamble << *it;
1223         return tcpreamble.str();
1224 }
1225
1226
1227 docstring const LaTeXFeatures::getLyXSGMLEntities() const
1228 {
1229         // Definition of entities used in the document that are LyX related.
1230         odocstringstream entities;
1231
1232         if (mustProvide("lyxarrow")) {
1233                 entities << "<!ENTITY lyxarrow \"-&gt;\">" << '\n';
1234         }
1235
1236         return entities.str();
1237 }
1238
1239
1240 docstring const LaTeXFeatures::getIncludedFiles(string const & fname) const
1241 {
1242         odocstringstream sgmlpreamble;
1243         // FIXME UNICODE
1244         docstring const basename(from_utf8(onlyPath(fname)));
1245
1246         FileMap::const_iterator end = IncludedFiles_.end();
1247         for (FileMap::const_iterator fi = IncludedFiles_.begin();
1248              fi != end; ++fi)
1249                 // FIXME UNICODE
1250                 sgmlpreamble << "\n<!ENTITY " << fi->first
1251                              << (isSGMLFileName(fi->second) ? " SYSTEM \"" : " \"")
1252                              << makeRelPath(from_utf8(fi->second), basename) << "\">";
1253
1254         return sgmlpreamble.str();
1255 }
1256
1257
1258 void LaTeXFeatures::showStruct() const
1259 {
1260         lyxerr << "LyX needs the following commands when LaTeXing:"
1261                << "\n***** Packages:" << getPackages()
1262                << "\n***** Macros:" << to_utf8(getMacros())
1263                << "\n***** Textclass stuff:" << to_utf8(getTClassPreamble())
1264                << "\n***** done." << endl;
1265 }
1266
1267
1268 Buffer const & LaTeXFeatures::buffer() const
1269 {
1270         return *buffer_;
1271 }
1272
1273
1274 void LaTeXFeatures::setBuffer(Buffer const & buffer)
1275 {
1276         buffer_ = &buffer;
1277 }
1278
1279
1280 BufferParams const & LaTeXFeatures::bufferParams() const
1281 {
1282         return params_;
1283 }
1284
1285
1286 void LaTeXFeatures::getFloatDefinitions(odocstream & os) const
1287 {
1288         FloatList const & floats = params_.documentClass().floats();
1289
1290         // Here we will output the code to create the needed float styles.
1291         // We will try to do this as minimal as possible.
1292         // \floatstyle{ruled}
1293         // \newfloat{algorithm}{htbp}{loa}
1294         // \providecommand{\algorithmname}{Algorithm}
1295         // \floatname{algorithm}{\protect\algorithmname}
1296         UsedFloats::const_iterator cit = usedFloats_.begin();
1297         UsedFloats::const_iterator end = usedFloats_.end();
1298         for (; cit != end; ++cit) {
1299                 Floating const & fl = floats.getType(cit->first);
1300
1301                 // For builtin floats we do nothing.
1302                 if (fl.isPredefined())
1303                         continue;
1304
1305                 // We have to special case "table" and "figure"
1306                 if (fl.floattype() == "tabular" || fl.floattype() == "figure") {
1307                         // Output code to modify "table" or "figure"
1308                         // but only if builtin == false
1309                         // and that have to be true at this point in the
1310                         // function.
1311                         docstring const type = from_ascii(fl.floattype());
1312                         docstring const placement = from_ascii(fl.placement());
1313                         docstring const style = from_ascii(fl.style());
1314                         if (!style.empty()) {
1315                                 os << "\\floatstyle{" << style << "}\n"
1316                                    << "\\restylefloat{" << type << "}\n";
1317                         }
1318                         if (!placement.empty()) {
1319                                 os << "\\floatplacement{" << type << "}{"
1320                                    << placement << "}\n";
1321                         }
1322                 } else {
1323                         // The other non builtin floats.
1324
1325                         docstring const type = from_ascii(fl.floattype());
1326                         docstring const placement = from_ascii(fl.placement());
1327                         docstring const ext = from_ascii(fl.ext());
1328                         docstring const within = from_ascii(fl.within());
1329                         docstring const style = from_ascii(fl.style());
1330                         docstring const name =
1331                                 buffer().language()->translateLayout(fl.name());
1332                         os << "\\floatstyle{" << style << "}\n"
1333                            << "\\newfloat{" << type << "}{" << placement
1334                            << "}{" << ext << '}';
1335                         if (!within.empty())
1336                                 os << '[' << within << ']';
1337                         os << '\n'
1338                            << "\\providecommand{\\" << type << "name}{"
1339                            << name << "}\n"
1340                            << "\\floatname{" << type << "}{\\protect\\"
1341                            << type << "name}\n";
1342
1343                         // What missing here is to code to minimalize the code
1344                         // output so that the same floatstyle will not be
1345                         // used several times, when the same style is still in
1346                         // effect. (Lgb)
1347                 }
1348                 if (cit->second)
1349                         os << "\n\\newsubfloat{" << from_ascii(fl.floattype()) << "}\n";
1350         }
1351 }
1352
1353
1354 void LaTeXFeatures::resolveAlternatives()
1355 {
1356         for (Features::iterator it = features_.begin(); it != features_.end();) {
1357                 if (contains(*it, '|')) {
1358                         vector<string> const alternatives = getVectorFromString(*it, "|");
1359                         vector<string>::const_iterator const end = alternatives.end();
1360                         vector<string>::const_iterator ita = alternatives.begin();
1361                         for (; ita != end; ++ita) {
1362                                 if (isRequired(*ita))
1363                                         break;
1364                         }
1365                         if (ita == end)
1366                                 require(alternatives.front());
1367                         features_.erase(it);
1368                         it = features_.begin();
1369                 } else
1370                         ++it;
1371         }
1372 }
1373
1374
1375 } // namespace lyx