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