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