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