]> git.lyx.org Git - features.git/blob - src/LaTeXFeatures.C
Replace LString.h with support/std_string.h,
[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 "bufferparams.h"
21 #include "Floating.h"
22 #include "FloatList.h"
23 #include "language.h"
24 #include "encoding.h"
25 #include "support/std_sstream.h"
26
27 #include "support/filetools.h"
28
29 using namespace lyx::support;
30
31 using lyx::textclass_type;
32
33 using std::endl;
34 using std::list;
35 using std::set;
36 using std::find;
37 using std::ostream;
38
39
40 LaTeXFeatures::LaTeXFeatures(BufferParams const & p)
41         : params(p)
42 {}
43
44
45 bool LaTeXFeatures::useBabel() const
46 {
47         return lyxrc.language_use_babel ||
48                 bufferParams().language->lang() != lyxrc.default_language ||
49                 this->hasLanguages();
50 }
51
52
53 void LaTeXFeatures::require(string const & name)
54 {
55         if (isRequired(name))
56                 return;
57
58         features.push_back(name);
59 }
60
61
62 void LaTeXFeatures::useLayout(string const & layoutname)
63 {
64         // Some code to avoid loops in dependency definition
65         static int level = 0;
66         const int maxlevel = 30;
67         if (level > maxlevel) {
68                 lyxerr << "LaTeXFeatures::useLayout: maximum level of "
69                        << "recursion attained by layout "
70                        << layoutname << endl;
71                 return;
72         }
73
74         LyXTextClass const & tclass = params.getLyXTextClass();
75         if (tclass.hasLayout(layoutname)) {
76                 // Is this layout already in usedLayouts?
77                 list<string>::const_iterator cit = usedLayouts.begin();
78                 list<string>::const_iterator end = usedLayouts.end();
79                 for (; cit != end; ++cit) {
80                         if (layoutname == *cit)
81                                 return;
82                 }
83
84                 LyXLayout_ptr const & lyt = tclass[layoutname];
85                 if (!lyt->depends_on().empty()) {
86                         ++level;
87                         useLayout(lyt->depends_on());
88                         --level;
89                 }
90                 usedLayouts.push_back(layoutname);
91         } else {
92                 lyxerr << "LaTeXFeatures::useLayout: layout `"
93                        << layoutname << "' does not exist in this class"
94                        << endl;
95         }
96
97         --level;
98 }
99
100
101 bool LaTeXFeatures::isRequired(string const & name) const
102 {
103         return find(features.begin(), features.end(), name) != features.end();
104 }
105
106
107 void LaTeXFeatures::addExternalPreamble(string const & preamble)
108 {
109         FeaturesList::const_iterator begin = preamble_snippets.begin();
110         FeaturesList::const_iterator end   = preamble_snippets.end();
111         if (find(begin, end, preamble) == end)
112                 preamble_snippets.push_back(preamble);
113 }
114
115
116 void LaTeXFeatures::useFloat(string const & name)
117 {
118         usedFloats.insert(name);
119         // We only need float.sty if we use non builtin floats, or if we
120         // use the "H" modifier. This includes modified table and
121         // figure floats. (Lgb)
122         Floating const & fl = params.getLyXTextClass().floats().getType(name);
123         if (!fl.type().empty() && !fl.builtin()) {
124                 require("float");
125         }
126 }
127
128
129 void LaTeXFeatures::useLanguage(Language const * lang)
130 {
131         UsedLanguages.insert(lang);
132 }
133
134
135 void LaTeXFeatures::includeFile(string const & key, string const & name)
136 {
137         IncludedFiles[key] = name;
138 }
139
140
141 bool LaTeXFeatures::hasLanguages() const
142 {
143         return !UsedLanguages.empty();
144 }
145
146
147 string LaTeXFeatures::getLanguages() const
148 {
149         ostringstream languages;
150
151         for (LanguageList::const_iterator cit =
152                     UsedLanguages.begin();
153              cit != UsedLanguages.end();
154              ++cit)
155                 languages << (*cit)->babel() << ',';
156
157         return STRCONV(languages.str());
158 }
159
160
161 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding) const
162 {
163         set<string> encodings;
164         for (LanguageList::const_iterator it =
165                      UsedLanguages.begin();
166              it != UsedLanguages.end(); ++it)
167                 if ((*it)->encoding()->LatexName() != doc_encoding)
168                         encodings.insert((*it)->encoding()->LatexName());
169         return encodings;
170 }
171
172 namespace {
173
174 char const * simplefeatures[] = {
175         "array",
176         "verbatim",
177         "longtable",
178         "rotating",
179         "latexsym",
180         "pifont",
181         "subfigure",
182         "floatflt",
183         "varioref",
184         "prettyref",
185         "float",
186         "wasy",
187         "dvipost"
188 };
189
190 int const nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
191
192 }
193
194
195 string const LaTeXFeatures::getPackages() const
196 {
197         ostringstream packages;
198         LyXTextClass const & tclass = params.getLyXTextClass();
199
200         //
201         //  These are all the 'simple' includes.  i.e
202         //  packages which we just \usepackage{package}
203         //
204         for (int i = 0; i < nb_simplefeatures; ++i) {
205                 if (isRequired(simplefeatures[i]))
206                         packages << "\\usepackage{"
207                                  << simplefeatures[i] << "}\n";
208         }
209
210         //
211         // The rest of these packages are somewhat more complicated
212         // than those above.
213         //
214
215         if (isRequired("amsmath")
216             && !tclass.provides(LyXTextClass::amsmath)
217             && params.use_amsmath != BufferParams::AMS_OFF) {
218                 packages << "\\usepackage{amsmath}\n";
219         }
220
221         // color.sty
222         if (isRequired("color")) {
223                 if (params.graphicsDriver == "default")
224                         packages << "\\usepackage[usenames]{color}\n";
225                 else
226                         packages << "\\usepackage["
227                                  << params.graphicsDriver
228                                  << ",usenames"
229                                  << "]{color}\n";
230         }
231
232         // makeidx.sty
233         if (isRequired("makeidx")) {
234                 if (! tclass.provides(LyXTextClass::makeidx))
235                         packages << "\\usepackage{makeidx}\n";
236                 packages << "\\makeindex\n";
237         }
238
239         // graphicx.sty
240         if (isRequired("graphicx") && params.graphicsDriver != "none") {
241                 if (params.graphicsDriver == "default")
242                         packages << "\\usepackage{graphicx}\n";
243                 else
244                         packages << "\\usepackage["
245                                  << params.graphicsDriver
246                                  << "]{graphicx}\n";
247         }
248
249         //if (algorithm) {
250         //      packages << "\\usepackage{algorithm}\n";
251         //}
252
253         // lyxskak.sty --- newer chess support based on skak.sty
254         if (isRequired("chess")) {
255                 packages << "\\usepackage[ps,mover]{lyxskak}\n";
256         }
257
258         // setspace.sty
259         if ((params.spacing.getSpace() != Spacing::Single
260              && !params.spacing.isDefault())
261             || isRequired("setspace")) {
262                 packages << "\\usepackage{setspace}\n";
263         }
264         switch (params.spacing.getSpace()) {
265         case Spacing::Default:
266         case Spacing::Single:
267                 // we dont use setspace.sty so dont print anything
268                 //packages += "\\singlespacing\n";
269                 break;
270         case Spacing::Onehalf:
271                 packages << "\\onehalfspacing\n";
272                 break;
273         case Spacing::Double:
274                 packages << "\\doublespacing\n";
275                 break;
276         case Spacing::Other:
277                 packages << "\\setstretch{"
278                          << params.spacing.getValue() << "}\n";
279                 break;
280         }
281
282         // amssymb.sty
283         if (isRequired("amssymb") || params.use_amsmath == BufferParams::AMS_ON)
284                 packages << "\\usepackage{amssymb}\n";
285         // url.sty
286         if (isRequired("url") && ! tclass.provides(LyXTextClass::url))
287                 packages << "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
288                             "                      {\\newcommand{\\url}{\\texttt}}\n";
289
290         // float.sty
291         // natbib.sty
292         if (isRequired("natbib") && ! tclass.provides(LyXTextClass::natbib)) {
293                 packages << "\\usepackage[";
294                 if (params.use_numerical_citations) {
295                         packages << "numbers";
296                 } else {
297                         packages << "authoryear";
298                 }
299                 packages << "]{natbib}\n";
300         }
301
302         return STRCONV(packages.str());
303 }
304
305
306 string const LaTeXFeatures::getMacros() const
307 {
308         ostringstream macros;
309
310         if (!preamble_snippets.empty())
311                 macros << '\n';
312         FeaturesList::const_iterator pit  = preamble_snippets.begin();
313         FeaturesList::const_iterator pend = preamble_snippets.end();
314         for (; pit != pend; ++pit) {
315                 macros << *pit << '\n';
316         }
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 }