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