]> git.lyx.org Git - lyx.git/blob - src/LaTeXFeatures.C
more code in the menu backend == less code in the menu frontends; add support for...
[lyx.git] / src / LaTeXFeatures.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2001 the LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #include "LaTeXFeatures.h"
14 #include "debug.h"
15 #include "lyx_sty.h"
16 #include "lyxrc.h"
17 #include "bufferparams.h"
18 #include "FloatList.h"
19 #include "language.h"
20 #include "encoding.h"
21 #include "LString.h"
22
23 #include "support/filetools.h"
24 #include "support/lstrings.h"
25
26 using lyx::textclass_type;
27
28 using std::endl;
29 using std::list;
30 using std::set;
31 using std::find;
32 using std::ostream;
33
34
35 LaTeXFeatures::LaTeXFeatures(BufferParams const & p)
36         : params(p)
37 {}
38
39
40 void LaTeXFeatures::require(string const & name)
41 {
42         if (isRequired(name))
43                 return;
44
45         features.push_back(name);
46 }
47
48
49 void LaTeXFeatures::useLayout(string const & layoutname)
50 {
51         // Some code to avoid loops in dependency definition
52         static int level = 0;
53         const int maxlevel = 30;
54         if (level > maxlevel) {
55                 lyxerr << "LaTeXFeatures::useLayout: maximum level of "
56                        << "recursion attained by layout "
57                        << layoutname << endl;
58                 return;
59         }
60
61         LyXTextClass const & tclass = params.getLyXTextClass();
62         if (tclass.hasLayout(layoutname)) {
63                 // Is this layout already in usedLayouts?
64                 list<string>::const_iterator cit = usedLayouts.begin();
65                 list<string>::const_iterator end = usedLayouts.end();
66                 for (; cit != end; ++cit) {
67                         if (layoutname == *cit)
68                                 return;
69                 }
70
71                 LyXLayout_ptr const & lyt = tclass[layoutname];
72                 if (!lyt->depends_on().empty()) {
73                         ++level;
74                         useLayout(lyt->depends_on());
75                         --level;
76                 }
77                 usedLayouts.push_back(layoutname);
78         } else {
79                 lyxerr << "LaTeXFeatures::useLayout: layout `"
80                        << layoutname << "' does not exist in this class"
81                        << endl;
82         }
83
84         --level;
85 }
86
87
88 bool LaTeXFeatures::isRequired(string const & name) const
89 {
90         return find(features.begin(), features.end(), name) != features.end();
91 }
92
93
94 void LaTeXFeatures::addExternalPreamble(string const & pream)
95 {
96         externalPreambles += pream;
97 }
98
99
100 void LaTeXFeatures::useFloat(string const & name)
101 {
102         usedFloats.insert(name);
103         // We only need float.sty if we use non builtin floats, or if we
104         // use the "H" modifier. This includes modified table and
105         // figure floats. (Lgb)
106         Floating const & fl = params.getLyXTextClass().floats().getType(name);
107         if (!fl.type().empty() && !fl.builtin()) {
108                 require("float");
109         }
110 }
111
112
113 void LaTeXFeatures::useLanguage(Language const * lang)
114 {
115         UsedLanguages.insert(lang);
116 }
117
118
119 void LaTeXFeatures::includeFile(string const & key, string const & name)
120 {
121         IncludedFiles[key] = name;
122 }
123
124
125 bool LaTeXFeatures::hasLanguages()
126 {
127         return !UsedLanguages.empty();
128 }
129
130
131 string LaTeXFeatures::getLanguages() const
132 {
133         ostringstream languages;
134
135         for (LanguageList::const_iterator cit =
136                     UsedLanguages.begin();
137              cit != UsedLanguages.end();
138              ++cit)
139                 languages << (*cit)->babel() << ',';
140
141         return STRCONV(languages.str());
142 }
143
144
145 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding)
146 {
147         set<string> encodings;
148         for (LanguageList::const_iterator it =
149                      UsedLanguages.begin();
150              it != UsedLanguages.end(); ++it)
151                 if ((*it)->encoding()->LatexName() != doc_encoding)
152                         encodings.insert((*it)->encoding()->LatexName());
153         return encodings;
154 }
155
156 namespace {
157
158 char const * simplefeatures[] = {
159         "array",
160         "verbatim",
161         "longtable",
162         "rotating",
163         "latexsym",
164         "pifont",
165         "subfigure",
166         "floatflt",
167         "varioref",
168         "prettyref",
169         "float",
170         "wasy",
171         "dvipost"
172 };
173
174 int const nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
175
176 }
177
178 string const LaTeXFeatures::getPackages() const
179 {
180         ostringstream packages;
181         LyXTextClass const & tclass = params.getLyXTextClass();
182
183
184         //
185         //  These are all the 'simple' includes.  i.e
186         //  packages which we just \usepackage{package}
187         //
188         for (int i = 0; i < nb_simplefeatures; ++i) {
189                 if (isRequired(simplefeatures[i]))
190                         packages << "\\usepackage{"
191                                  << simplefeatures[i] << "}\n";
192         }
193
194         //
195         // The rest of these packages are somewhat more complicated
196         // than those above.
197         //
198
199         if (isRequired("amsmath")
200             && ! tclass.provides(LyXTextClass::amsmath)) {
201                 packages << "\\usepackage{amsmath}\n";
202         }
203
204         // color.sty
205         if (isRequired("color")) {
206                 if (params.graphicsDriver == "default")
207                         packages << "\\usepackage[usenames]{color}\n";
208                 else
209                         packages << "\\usepackage["
210                                  << params.graphicsDriver
211                                  << ",usenames"
212                                  << "]{color}\n";
213         }
214
215         // makeidx.sty
216         if (isRequired("makeidx")) {
217                 if (! tclass.provides(LyXTextClass::makeidx))
218                         packages << "\\usepackage{makeidx}\n";
219                 packages << "\\makeindex\n";
220         }
221
222         // graphicx.sty
223         if (isRequired("graphicx") && params.graphicsDriver != "none") {
224                 if (params.graphicsDriver == "default")
225                         packages << "\\usepackage{graphicx}\n";
226                 else
227                         packages << "\\usepackage["
228                                  << params.graphicsDriver
229                                  << "]{graphicx}\n";
230         }
231
232         //if (algorithm) {
233         //      packages << "\\usepackage{algorithm}\n";
234         //}
235
236         // lyxskak.sty --- newer chess support based on skak.sty
237         if (isRequired("chess")) {
238                 packages << "\\usepackage[ps,mover]{lyxskak}\n";
239         }
240
241         // setspace.sty
242         if ((params.spacing.getSpace() != Spacing::Single
243              && !params.spacing.isDefault())
244             || isRequired("setspace")) {
245                 packages << "\\usepackage{setspace}\n";
246         }
247         switch (params.spacing.getSpace()) {
248         case Spacing::Default:
249         case Spacing::Single:
250                 // we dont use setspace.sty so dont print anything
251                 //packages += "\\singlespacing\n";
252                 break;
253         case Spacing::Onehalf:
254                 packages << "\\onehalfspacing\n";
255                 break;
256         case Spacing::Double:
257                 packages << "\\doublespacing\n";
258                 break;
259         case Spacing::Other:
260                 packages << "\\setstretch{"
261                          << params.spacing.getValue() << "}\n";
262                 break;
263         }
264
265         // amssymb.sty
266         if (isRequired("amssymb") || params.use_amsmath)
267                 packages << "\\usepackage{amssymb}\n";
268         // url.sty
269         if (isRequired("url") && ! tclass.provides(LyXTextClass::url))
270                 packages << "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
271                             "                      {\\newcommand{\\url}{\\texttt}}\n";
272
273         // float.sty
274         // natbib.sty
275         if (isRequired("natbib") && ! tclass.provides(LyXTextClass::natbib)) {
276                 packages << "\\usepackage[";
277                 if (params.use_numerical_citations) {
278                         packages << "numbers";
279                 } else {
280                         packages << "authoryear";
281                 }
282                 packages << "]{natbib}\n";
283         }
284
285         packages << externalPreambles;
286
287         return STRCONV(packages.str());
288 }
289
290
291 string const LaTeXFeatures::getMacros() const
292 {
293         ostringstream macros;
294
295         if (isRequired("LyX"))
296                 macros << lyx_def << '\n';
297
298         if (isRequired("lyxline"))
299                 macros << lyxline_def << '\n';
300
301         if (isRequired("noun"))
302                 macros << noun_def << '\n';
303
304         if (isRequired("lyxarrow"))
305                 macros << lyxarrow_def << '\n';
306
307         // quotes.
308         if (isRequired("quotesinglbase"))
309                 macros << quotesinglbase_def << '\n';
310         if (isRequired("quotedblbase"))
311                 macros << quotedblbase_def << '\n';
312         if (isRequired("guilsinglleft"))
313                 macros << guilsinglleft_def << '\n';
314         if (isRequired("guilsinglright"))
315                 macros << guilsinglright_def << '\n';
316         if (isRequired("guillemotleft"))
317                 macros << guillemotleft_def << '\n';
318         if (isRequired("guillemotright"))
319                 macros << guillemotright_def << '\n';
320
321         // Math mode
322         if (isRequired("boldsymbol") && !isRequired("amsmath"))
323                 macros << boldsymbol_def << '\n';
324         if (isRequired("binom") && !isRequired("amsmath"))
325                 macros << binom_def << '\n';
326         if (isRequired("mathcircumflex"))
327                 macros << mathcircumflex_def << '\n';
328
329         // other
330         if (isRequired("NeedLyXMinipageIndent"))
331                 macros << minipageindent_def;
332         if (isRequired("ParagraphLeftIndent"))
333                 macros << paragraphleftindent_def;
334         if (isRequired("NeedLyXFootnoteCode"))
335                 macros << floatingfootnote_def;
336
337         // some problems with tex->html converters
338         if (isRequired("NeedTabularnewline"))
339                 macros << tabularnewline_def;
340
341         // floats
342         getFloatDefinitions(macros);
343
344         return STRCONV(macros.str());
345 }
346
347
348 string const LaTeXFeatures::getBabelOptions() const
349 {
350         ostringstream tmp;
351
352         for (LanguageList::const_iterator cit = UsedLanguages.begin();
353              cit != UsedLanguages.end(); ++cit)
354                 if (!(*cit)->latex_options().empty())
355                         tmp << (*cit)->latex_options() << '\n';
356         if (!params.language->latex_options().empty())
357                 tmp << params.language->latex_options() << '\n';
358
359         return STRCONV(tmp.str());
360 }
361
362
363 string const LaTeXFeatures::getTClassPreamble() const
364 {
365         // the text class specific preamble
366         LyXTextClass const & tclass = params.getLyXTextClass();
367         ostringstream tcpreamble;
368
369         tcpreamble << tclass.preamble();
370
371         list<string>::const_iterator cit = usedLayouts.begin();
372         list<string>::const_iterator end = usedLayouts.end();
373         for (; cit != end; ++cit) {
374                 tcpreamble << tclass[*cit]->preamble();
375         }
376
377         return STRCONV(tcpreamble.str());
378 }
379
380
381 string const LaTeXFeatures::getLyXSGMLEntities() const
382 {
383         // Definition of entities used in the document that are LyX related.
384         ostringstream entities;
385
386         if (isRequired("lyxarrow")) {
387                 entities << "<!ENTITY lyxarrow \"-&gt;\">" << '\n';
388         }
389
390         return STRCONV(entities.str());
391 }
392
393
394 string const LaTeXFeatures::getIncludedFiles(string const & fname) const
395 {
396         ostringstream sgmlpreamble;
397         string const basename = OnlyPath(fname);
398
399         FileMap::const_iterator end = IncludedFiles.end();
400         for (FileMap::const_iterator fi = IncludedFiles.begin();
401              fi != end; ++fi)
402                 sgmlpreamble << "\n<!ENTITY " << fi->first
403                              << (IsSGMLFilename(fi->second) ? " SYSTEM \"" : " \"")
404                              << MakeRelPath(fi->second, basename) << "\">";
405
406         return STRCONV(sgmlpreamble.str());
407 }
408
409
410 void LaTeXFeatures::showStruct() const {
411         lyxerr << "LyX needs the following commands when LaTeXing:"
412                << "\n***** Packages:" << getPackages()
413                << "\n***** Macros:" << getMacros()
414                << "\n***** Textclass stuff:" << getTClassPreamble()
415                << "\n***** done." << endl;
416 }
417
418
419 BufferParams const & LaTeXFeatures::bufferParams() const
420 {
421         return params;
422 }
423
424
425 void LaTeXFeatures::getFloatDefinitions(ostream & os) const
426 {
427         FloatList const & floats = params.getLyXTextClass().floats();
428
429         // Here we will output the code to create the needed float styles.
430         // We will try to do this as minimal as possible.
431         // \floatstyle{ruled}
432         // \newfloat{algorithm}{htbp}{loa}
433         // \floatname{algorithm}{Algorithm}
434         UsedFloats::const_iterator cit = usedFloats.begin();
435         UsedFloats::const_iterator end = usedFloats.end();
436         // ostringstream floats;
437         for (; cit != end; ++cit) {
438                 Floating const & fl = floats.getType((*cit));
439
440                 // For builtin floats we do nothing.
441                 if (fl.builtin()) continue;
442
443                 // We have to special case "table" and "figure"
444                 if (fl.type() == "tabular" || fl.type() == "figure") {
445                         // Output code to modify "table" or "figure"
446                         // but only if builtin == false
447                         // and that have to be true at this point in the
448                         // function.
449                         string const type = fl.type();
450                         string const placement = fl.placement();
451                         string const style = fl.style();
452                         if (!style.empty()) {
453                                 os << "\\floatstyle{" << style << "}\n"
454                                    << "\\restylefloat{" << type << "}\n";
455                         }
456                         if (!placement.empty()) {
457                                 os << "\\floatplacement{" << type << "}{"
458                                    << placement << "}\n";
459                         }
460                 } else {
461                         // The other non builtin floats.
462
463                         string const type = fl.type();
464                         string const placement = fl.placement();
465                         string const ext = fl.ext();
466                         string const within = fl.within();
467                         string const style = fl.style();
468                         string const name = fl.name();
469                         os << "\\floatstyle{" << style << "}\n"
470                            << "\\newfloat{" << type << "}{" << placement
471                            << "}{" << ext << '}';
472                         if (!within.empty())
473                                 os << '[' << within << ']';
474                         os << '\n'
475                            << "\\floatname{" << type << "}{"
476                            << name << "}\n";
477
478                         // What missing here is to code to minimalize the code
479                         // output so that the same floatstyle will not be
480                         // used several times, when the same style is still in
481                         // effect. (Lgb)
482                 }
483         }
484 }