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