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