]> git.lyx.org Git - lyx.git/blob - src/LaTeXFeatures.C
Small fix.
[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
26 using std::endl;
27
28 LaTeXFeatures::LaTeXFeatures(BufferParams const & p, int n)
29         : layout(n, false), params(p)
30 {
31         // packages
32         array = false;
33         color = false;
34         graphics = false; // INSET_GRAPHICS: remove this when InsetFig is thrown.
35     graphicx = false;
36         setspace = false;
37         makeidx = false;
38         verbatim = false;
39         longtable = false;
40         algorithm = false;
41         rotating = false;
42         amssymb = false;
43         latexsym = false;
44         pifont = false;
45         subfigure = false;
46         floatflt = false;
47         url = false;
48         varioref = false;
49         prettyref = false;
50         chess = false;
51         
52         // commands
53         lyx = false;
54         lyxline = false;
55         noun = false;
56         lyxarrow = false;
57
58         // quotes
59         quotesinglbase = false;
60         quotedblbase = false;
61         guilsinglleft = false;
62         guilsinglright = false;
63         guillemotleft = false;
64         guillemotright = false;
65
66         // Math mode
67         amsstyle = false;
68         binom = false;
69         boldsymbol = false;
70     
71         // special features
72         LyXParagraphIndent = false;
73         NeedLyXFootnoteCode = false;
74         NeedLyXMinipageIndent = false;
75 }
76
77 void LaTeXFeatures::require(string const & name) {
78         if (name == "array") {
79                 array = true;
80         } else if (name == "color") {
81                 color = true;
82         } else if (name == "graphics") {
83                 graphicx = true;
84                 graphics = true;// INSET_GRAPHICS: remove this when InsetFig is thrown.
85         } else if (name == "setspace") {
86                 setspace = true;
87         } else if (name == "makeidx") {
88                 makeidx = true;
89         } else if (name == "verbatim") {
90                 verbatim = true;
91         } else if (name == "longtable") {
92                 longtable = true;
93         } else if (name == "algorithm") {
94                 algorithm = true;
95         } else if (name == "rotating") {
96                 rotating = true;
97         } else if (name == "amssymb") {
98                 amssymb = true;
99         } else if (name == "latexsym") {
100                 latexsym = true;
101         } else if (name == "pifont") {
102                 pifont = true;
103         } else if (name == "subfigure") {
104                 subfigure = true;
105         } else if (name == "floatflt") {
106                 floatflt = true;
107         } else if (name == "url") {
108                 url = true;
109         } else if (name == "varioref") {
110                 varioref = true;
111         } else if (name == "prettyref") {
112                 prettyref = true;
113         } else if (name == "chess") {
114                 chess = true;
115         } else if (name == "amsstyle") {
116                 amsstyle = true;
117         } else if (name == "boldsymbol") {
118                 boldsymbol = true;
119         } else if (name == "binom") {
120                 binom = true;
121         }
122 }
123
124 string LaTeXFeatures::getPackages()
125 {
126         string packages;
127         LyXTextClass const & tclass =
128                 textclasslist.TextClass(params.textclass);
129
130         // array-package
131         if (array)
132                 packages += "\\usepackage{array}\n";
133
134         // color.sty
135         if (color) {
136                 if (params.graphicsDriver == "default")
137                         packages += "\\usepackage{color}\n";
138                 else
139                         packages += "\\usepackage[" 
140                                 + params.graphicsDriver + "]{color}\n";
141         }
142                 
143         // makeidx.sty
144         if (makeidx) {
145                 if (! tclass.provides(LyXTextClass::makeidx)
146                     && params.language != "french") // french provides
147                                                     // \index !
148                         packages += "\\usepackage{makeidx}\n";
149                 packages += "\\makeindex\n";
150         }
151
152         // graphicx.sty
153         if (graphicx && params.graphicsDriver != "none") {
154                 if (params.graphicsDriver == "default")
155                         packages += "\\usepackage{graphicx}\n";
156                 else
157                         packages += "\\usepackage[" 
158                                 + params.graphicsDriver + "]{graphicx}\n";
159         }
160
161     // INSET_GRAPHICS: remove this when InsetFig is thrown.
162         // graphics.sty
163         if (graphics && params.graphicsDriver != "none") {
164                 if (params.graphicsDriver == "default")
165                         packages += "\\usepackage{graphics}\n";
166                 else
167                         packages += "\\usepackage[" 
168                                 + params.graphicsDriver + "]{graphics}\n";
169         }
170
171         // verbatim.sty
172         if (verbatim)
173                 packages += "\\usepackage{verbatim}\n";
174
175         if (algorithm) {
176                 packages += "\\usepackage{algorithm}\n";
177         }
178
179         // lyxchess.sty
180         if (chess) {
181                 packages += "\\usepackage{lyxchess}\n";
182         }
183
184         // setspace.sty
185         if ((params.spacing.getSpace() != Spacing::Single
186              && !params.spacing.isDefault())
187             || setspace) {
188                 packages += "\\usepackage{setspace}\n";
189         }
190         switch (params.spacing.getSpace()) {
191         case Spacing::Default:
192         case Spacing::Single:
193                 // we dont use setspace.sty so dont print anything
194                 //packages += "\\singlespacing\n";
195                 break;
196         case Spacing::Onehalf:
197                 packages += "\\onehalfspacing\n";
198                 break;
199         case Spacing::Double:
200                 packages += "\\doublespacing\n";
201                 break;
202         case Spacing::Other:
203 #ifdef HAVE_SSTREAM
204                 std::ostringstream value;
205 #else
206                 char val[30];
207                 ostrstream value(val, 30);
208                 
209 #endif
210                 value << params.spacing.getValue(); // setw?
211 #ifdef HAVE_SSTREAM
212                 packages += string("\\setstretch{") 
213                           + value.str().c_str() + "}\n";
214 #else
215                 value << '\0';
216                 packages += string("\\setstretch{") 
217                           + value.str() + "}\n";
218 #endif
219                 break;
220         }
221
222         //longtable.sty
223         if (longtable)
224                 packages += "\\usepackage{longtable}\n";
225
226         //rotating.sty
227         if (rotating)
228                 packages += "\\usepackage{rotating}\n";
229
230         // amssymb.sty
231         if (amssymb || params.use_amsmath)
232                 packages += "\\usepackage{amssymb}\n";
233
234         // latexsym.sty
235         if (latexsym)
236                 packages += "\\usepackage{latexsym}\n";
237
238         // pifont.sty
239         if (pifont)
240                 packages += "\\usepackage{pifont}\n";
241
242         // subfigure.sty
243         if (subfigure)
244                 packages += "\\usepackage{subfigure}\n";
245
246         // floatflt.sty
247         if (floatflt)
248                 packages += "\\usepackage{floatflt}\n";
249
250         // url.sty
251         if (url && ! tclass.provides(LyXTextClass::url))
252                 packages += "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
253                             "                      {\\newcommand{\\url}{\\texttt}}\n";
254
255         // varioref.sty
256         if (varioref)
257                 packages += "\\usepackage{varioref}\n";
258
259         // prettyref.sty
260         if (prettyref)
261                 packages += "\\usepackage{prettyref}\n";
262
263         // float.sty
264         // This is not correct and needs fixing.
265         // We don't need float.sty if we only use unchanged
266         // table and figure floats. (Lgb)
267         if (!usedFloats.empty())
268                 packages += "\\usepackage{float}\n";
269         
270         packages += externalPreambles;
271
272         return packages;
273 }
274
275
276 string LaTeXFeatures::getMacros()
277 {
278         string macros;
279
280         // always include this
281         if (true || lyx) 
282                 macros += lyx_def + '\n';
283
284         if (lyxline) 
285                 macros += lyxline_def + '\n';
286
287         if (noun) {
288                 macros += noun_def + '\n';
289         }
290
291         if (lyxarrow) {
292                 macros += lyxarrow_def + '\n';
293         }
294
295         // quotes. 
296         if (quotesinglbase)
297                 macros += quotesinglbase_def + '\n';
298         if (quotedblbase)
299                 macros += quotedblbase_def + '\n';
300         if (guilsinglleft)
301                 macros += guilsinglleft_def + '\n';
302         if (guilsinglright)
303                 macros += guilsinglright_def + '\n';
304         if (guillemotleft)
305                 macros += guillemotleft_def + '\n';
306         if (guillemotright)
307                 macros += guillemotright_def + '\n';
308     
309         // Math mode    
310         if (boldsymbol && !amsstyle)
311                 macros += boldsymbol_def + '\n';
312         if (binom && !amsstyle)
313                 macros += binom_def + '\n';
314
315         // other
316         if (NeedLyXMinipageIndent) 
317                 macros += minipageindent_def;
318         if (LyXParagraphIndent) 
319                 macros += paragraphindent_def;
320         if (NeedLyXFootnoteCode) 
321                 macros += floatingfootnote_def;
322
323         // floats
324         // Here we will output the code to create the needed float styles.
325         // We will try to do this as minimal as possible.
326         // \floatstyle{ruled}
327         // \newfloat{algorithm}{htbp}{loa}
328         // \floatname{algorithm}{Algorithm}
329         return macros;
330 }
331
332
333 string LaTeXFeatures::getTClassPreamble()
334 {
335         // the text class specific preamble 
336         LyXTextClass const & tclass =
337                 textclasslist.TextClass(params.textclass);
338         string tcpreamble = tclass.preamble();
339
340         for (unsigned int i = 0; i < tclass.numLayouts(); ++i) {
341                 if (layout[i]) {
342                         tcpreamble += tclass[i].preamble();
343                 }
344         }
345
346         return tcpreamble;
347 }       
348
349
350 string LaTeXFeatures::getIncludedFiles()
351 {
352         string sgmlpreamble;
353
354         for(FileMap::const_iterator fi=IncludedFiles.begin(); fi != IncludedFiles.end(); ++fi)
355                 sgmlpreamble += "\n<!entity " + fi->first + " system \"" + fi->second + "\">";
356
357         return sgmlpreamble;
358 }
359
360
361 void LaTeXFeatures::showStruct() {
362         lyxerr << "LyX needs the following commands when LaTeXing:"
363                << "\n***** Packages:" << getPackages()
364                << "\n***** Macros:" << getMacros()
365                << "\n***** Textclass stuff:" << getTClassPreamble()
366                << "\n***** done." << endl;
367 }
368
369
370 BufferParams const & LaTeXFeatures::bufferParams() const
371 {
372         return params;
373 }