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