]> git.lyx.org Git - features.git/blob - src/LaTeXFeatures.C
use stringstream, remove some old footnote handling
[features.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() const
131 {
132         ostringstream 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
147                                  << "]{color}\n";
148         }
149                 
150         // makeidx.sty
151         if (makeidx) {
152                 if (! tclass.provides(LyXTextClass::makeidx)
153                     && params.language->babel() != "french") // french provides
154                                                              // \index !
155                         packages << "\\usepackage{makeidx}\n";
156                 packages << "\\makeindex\n";
157         }
158
159         // graphicx.sty
160         if (graphicx && params.graphicsDriver != "none") {
161                 if (params.graphicsDriver == "default")
162                         packages << "\\usepackage{graphicx}\n";
163                 else
164                         packages << "\\usepackage[" 
165                                  << params.graphicsDriver
166                                  << "]{graphicx}\n";
167         }
168
169         // INSET_GRAPHICS: remove this when InsetFig is thrown.
170         // graphics.sty
171         if (graphics && params.graphicsDriver != "none") {
172                 if (params.graphicsDriver == "default")
173                         packages << "\\usepackage{graphics}\n";
174                 else
175                         packages << "\\usepackage[" 
176                                  << params.graphicsDriver
177                                  << "]{graphics}\n";
178         }
179
180         // verbatim.sty
181         if (verbatim)
182                 packages << "\\usepackage{verbatim}\n";
183
184         if (algorithm) {
185                 packages << "\\usepackage{algorithm}\n";
186         }
187
188         // lyxchess.sty
189         if (chess) {
190                 packages << "\\usepackage{lyxchess}\n";
191         }
192
193         // setspace.sty
194         if ((params.spacing.getSpace() != Spacing::Single
195              && !params.spacing.isDefault())
196             || setspace) {
197                 packages << "\\usepackage{setspace}\n";
198         }
199         switch (params.spacing.getSpace()) {
200         case Spacing::Default:
201         case Spacing::Single:
202                 // we dont use setspace.sty so dont print anything
203                 //packages += "\\singlespacing\n";
204                 break;
205         case Spacing::Onehalf:
206                 packages << "\\onehalfspacing\n";
207                 break;
208         case Spacing::Double:
209                 packages << "\\doublespacing\n";
210                 break;
211         case Spacing::Other:
212                 packages << "\\setstretch{"
213                          << params.spacing.getValue() << "}\n";
214                 break;
215         }
216
217         //longtable.sty
218         if (longtable)
219                 packages << "\\usepackage{longtable}\n";
220
221         //rotating.sty
222         if (rotating)
223                 packages << "\\usepackage{rotating}\n";
224
225         // amssymb.sty
226         if (amssymb || params.use_amsmath)
227                 packages << "\\usepackage{amssymb}\n";
228
229         // latexsym.sty
230         if (latexsym)
231                 packages << "\\usepackage{latexsym}\n";
232
233         // pifont.sty
234         if (pifont)
235                 packages << "\\usepackage{pifont}\n";
236
237         // subfigure.sty
238         if (subfigure)
239                 packages << "\\usepackage{subfigure}\n";
240
241         // floatflt.sty
242         if (floatflt)
243                 packages << "\\usepackage{floatflt}\n";
244
245         // url.sty
246         if (url && ! tclass.provides(LyXTextClass::url))
247                 packages << "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
248                             "                      {\\newcommand{\\url}{\\texttt}}\n";
249
250         // varioref.sty
251         if (varioref)
252                 packages << "\\usepackage{varioref}\n";
253
254         // prettyref.sty
255         if (prettyref)
256                 packages << "\\usepackage{prettyref}\n";
257
258         // float.sty
259         // We only need float.sty if we use non builtin floats. This includes
260         // modified table and figure floats. (Lgb)
261         if (!usedFloats.empty()) {
262                 bool use_float = false;
263                 UsedFloats::const_iterator beg = usedFloats.begin();
264                 UsedFloats::const_iterator end = usedFloats.end();
265                 for (; beg != end; ++beg) {
266                         Floating const & fl = floatList.getType((*beg));
267                         if (!fl.type().empty() && !fl.builtin()) {
268                                 use_float = true;
269                                 break;
270                         }
271                 }
272                 if (use_float)
273                         packages << "\\usepackage{float}\n";
274         }
275         
276         packages << externalPreambles;
277
278         return packages.str().c_str();
279 }
280
281
282 string const LaTeXFeatures::getMacros() const
283 {
284         ostringstream macros;
285
286         // always include this
287         if (true || lyx) 
288                 macros << lyx_def << '\n';
289
290         if (lyxline) 
291                 macros << lyxline_def << '\n';
292
293         if (noun) {
294                 macros << noun_def << '\n';
295         }
296
297         if (lyxarrow) {
298                 macros << lyxarrow_def << '\n';
299         }
300
301         // quotes. 
302         if (quotesinglbase)
303                 macros << quotesinglbase_def << '\n';
304         if (quotedblbase)
305                 macros << quotedblbase_def << '\n';
306         if (guilsinglleft)
307                 macros << guilsinglleft_def << '\n';
308         if (guilsinglright)
309                 macros << guilsinglright_def << '\n';
310         if (guillemotleft)
311                 macros << guillemotleft_def << '\n';
312         if (guillemotright)
313                 macros << guillemotright_def << '\n';
314     
315         // Math mode    
316         if (boldsymbol && !amsstyle)
317                 macros << boldsymbol_def << '\n';
318         if (binom && !amsstyle)
319                 macros << binom_def << '\n';
320
321         // other
322         if (NeedLyXMinipageIndent) 
323                 macros << minipageindent_def;
324         if (LyXParagraphIndent) 
325                 macros << paragraphindent_def;
326         if (NeedLyXFootnoteCode) 
327                 macros << floatingfootnote_def;
328
329         // floats
330         getFloatDefinitions(macros);
331
332 #if 0
333         // Here we will output the code to create the needed float styles.
334         // We will try to do this as minimal as possible.
335         // \floatstyle{ruled}
336         // \newfloat{algorithm}{htbp}{loa}
337         // \floatname{algorithm}{Algorithm}
338         UsedFloats::const_iterator cit = usedFloats.begin();
339         UsedFloats::const_iterator end = usedFloats.end();
340         ostringstream floats;
341         for (; cit != end; ++cit) {
342                 Floating const & fl = floatList.getType((*cit));
343
344                 // For builtin floats we do nothing.
345                 if (fl.builtin()) continue;
346                 
347                 // We have to special case "table" and "figure"
348                 if (fl.type() == "tabular" || fl.type() == "figure") {
349                         // Output code to modify "table" or "figure"
350                         // but only if builtin == false
351                 } else {
352                         // The other non builtin floats.
353
354                         string type = fl.type();
355                         string placement = fl.placement();
356                         string ext = fl.ext();
357                         string within = fl.within();
358                         string style = fl.style();
359                         string name = fl.name();
360                         floats << "\\floatstyle{" << style << "}\n"
361                                << "\\newfloat{" << type << "}{" << placement
362                                << "}{" << ext << "}";
363                         if (!within.empty())
364                                 floats << "[" << within << "]";
365                         floats << "\n"
366                                << "\\floatname{" << type << "}{"
367                                << name << "}\n";
368
369                         // What missing here is to code to minimalize the code
370                         // outputted so that the same flotastyle will not be
371                         // used several times. when the same style is still in
372                         // effect. (Lgb)
373                 }
374         }
375         macros += floats.str().c_str();
376 #endif
377         
378         for (LanguageList::const_iterator cit = UsedLanguages.begin();
379              cit != UsedLanguages.end(); ++cit)
380                 if (!(*cit)->latex_options().empty())
381                         macros << (*cit)->latex_options() << '\n';
382         if (!params.language->latex_options().empty())
383                 macros << params.language->latex_options() << '\n';
384
385         return macros.str().c_str();
386 }
387
388
389 string const LaTeXFeatures::getTClassPreamble() const
390 {
391         // the text class specific preamble 
392         LyXTextClass const & tclass =
393                 textclasslist.TextClass(params.textclass);
394         ostringstream tcpreamble;
395         
396         tcpreamble << tclass.preamble();
397
398         for (unsigned int i = 0; i < tclass.numLayouts(); ++i) {
399                 if (layout[i]) {
400                         tcpreamble << tclass[i].preamble();
401                 }
402         }
403
404         return tcpreamble.str().c_str();
405 }       
406
407
408 string const LaTeXFeatures::getIncludedFiles(string const & fname) const
409 {
410         ostringstream sgmlpreamble;
411         string const basename = OnlyPath(fname);
412
413         FileMap::const_iterator end = IncludedFiles.end();
414         for (FileMap::const_iterator fi = IncludedFiles.begin();
415              fi != end; ++fi)
416                 sgmlpreamble << "\n<!ENTITY " << fi->first
417                              << (IsSGMLFilename(fi->second) ? " SYSTEM \"" : " \"" )
418                              << MakeRelPath(fi->second,basename) << "\">";
419
420         return sgmlpreamble.str().c_str();
421 }
422
423
424 void LaTeXFeatures::showStruct() const{
425         lyxerr << "LyX needs the following commands when LaTeXing:"
426                << "\n***** Packages:" << getPackages()
427                << "\n***** Macros:" << getMacros()
428                << "\n***** Textclass stuff:" << getTClassPreamble()
429                << "\n***** done." << endl;
430 }
431
432
433 BufferParams const & LaTeXFeatures::bufferParams() const
434 {
435         return params;
436 }
437
438
439 void LaTeXFeatures::getFloatDefinitions(ostream & os) const
440 {
441         // Here we will output the code to create the needed float styles.
442         // We will try to do this as minimal as possible.
443         // \floatstyle{ruled}
444         // \newfloat{algorithm}{htbp}{loa}
445         // \floatname{algorithm}{Algorithm}
446         UsedFloats::const_iterator cit = usedFloats.begin();
447         UsedFloats::const_iterator end = usedFloats.end();
448         // ostringstream floats;
449         for (; cit != end; ++cit) {
450                 Floating const & fl = floatList.getType((*cit));
451                 
452                 // For builtin floats we do nothing.
453                 if (fl.builtin()) continue;
454                 
455                 // We have to special case "table" and "figure"
456                 if (fl.type() == "tabular" || fl.type() == "figure") {
457                         // Output code to modify "table" or "figure"
458                         // but only if builtin == false
459                         // and that have to be true at this point in the
460                         // function.
461                         string const type = fl.type();
462                         string const placement = fl.placement();
463                         string const style = fl.style();
464                         if (!style.empty()) {
465                                 os << "\\floatstyle{" << style << "}\n"
466                                    << "\\restylefloat{" << type << "}\n";
467                         }
468                         if (!placement.empty()) {
469                                 os << "\\floatplacement{" << type << "}{"
470                                    << placement << "}\n";
471                         }
472                 } else {
473                         // The other non builtin floats.
474                         
475                         string const type = fl.type();
476                         string const placement = fl.placement();
477                         string const ext = fl.ext();
478                         string const within = fl.within();
479                         string const style = fl.style();
480                         string const name = fl.name();
481                         os << "\\floatstyle{" << style << "}\n"
482                            << "\\newfloat{" << type << "}{" << placement
483                            << "}{" << ext << "}";
484                         if (!within.empty())
485                                 os << "[" << within << "]";
486                         os << "\n"
487                            << "\\floatname{" << type << "}{"
488                            << name << "}\n";
489                         
490                         // What missing here is to code to minimalize the code
491                         // outputted so that the same flotastyle will not be
492                         // used several times. when the same style is still in
493                         // effect. (Lgb)
494                 }
495         }
496 }