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