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