]> git.lyx.org Git - lyx.git/blob - src/LaTeXFeatures.C
e0a1e9ce60b72878bb1e0f214f7640d039657a0d
[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(int n)
29         : layout(n, false)
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         
47         // commands
48         lyx = false;
49         lyxline = false;
50         noun = false;
51         lyxarrow = false;
52
53         // quotes
54         quotesinglbase = false;
55         quotedblbase = false;
56         guilsinglleft = false;
57         guilsinglright = false;
58         guillemotleft = false;
59         guillemotright = false;
60
61         // Math mode
62         amsstyle = false;
63         binom = false;
64         boldsymbol = false;
65     
66         // special features
67         LyXParagraphIndent = false;
68         NeedLyXFootnoteCode = false;
69         NeedLyXMinipageIndent = false;
70 }
71
72
73 string LaTeXFeatures::getPackages(BufferParams const & params)
74 {
75         string packages;
76         LyXTextClass const & tclass = textclasslist.TextClass(params.textclass);
77
78         // color.sty
79         if (color) {
80                 if (params.graphicsDriver == "default")
81                         packages += "\\usepackage{color}\n";
82                 else
83                         packages += "\\usepackage[" 
84                                 + params.graphicsDriver + "]{color}\n";
85         }
86                 
87         // makeidx.sty
88         if (makeidx) {
89                 if (! tclass.provides(LyXTextClass::makeidx)
90                     && params.language != "french") // french provides
91                                                     // \index !
92                         packages += "\\usepackage{makeidx}\n";
93                 packages += "\\makeindex\n";
94         }
95
96         // graphics.sty
97         if (graphics && params.graphicsDriver != "none") {
98                 if (params.graphicsDriver == "default")
99                         packages += "\\usepackage{graphics}\n";
100                 else
101                         packages += "\\usepackage[" 
102                                 + params.graphicsDriver + "]{graphics}\n";
103         }
104
105         //verbatim.sty
106         if (verbatim)
107                 packages += "\\usepackage{verbatim}\n";
108
109         if (algorithm) {
110                 packages += "\\usepackage{algorithm}\n";
111         }
112
113         // setspace.sty
114         if ((params.spacing.getSpace() != Spacing::Single)
115             || setspace) {
116                 packages += "\\usepackage{setspace}\n";
117         }
118         switch (params.spacing.getSpace()) {
119         case Spacing::Single:
120                 // we dont use setspace.sty so dont print anything
121                 //packages += "\\singlespacing\n";
122                 break;
123         case Spacing::Onehalf:
124                 packages += "\\onehalfspacing\n";
125                 break;
126         case Spacing::Double:
127                 packages += "\\doublespacing\n";
128                 break;
129         case Spacing::Other:
130                 //char value[30];
131                 //sprintf(value, "%.2f", params.spacing.getValue());
132 #ifdef HAVE_SSTREAM
133                 ostringstream value;
134 #else
135                 char val[30];
136                 ostrstream value(val, 30);
137                 
138 #endif
139                 value << params.spacing.getValue(); // setw?
140 #ifdef HAVE_SSTREAM
141                 packages += string("\\setstretch{") 
142                           + value.str().c_str() + "}\n";
143 #else
144                 value << '\0';
145                 packages += string("\\setstretch{") 
146                           + value.str() + "}\n";
147 #endif
148                 break;
149         }
150
151         //longtable.sty
152         if (longtable)
153                 packages += "\\usepackage{longtable}\n";
154
155         //rotating.sty
156         if (rotating)
157                 packages += "\\usepackage{rotating}\n";
158
159         // amssymb.sty
160         if (amssymb)
161                 packages += "\\usepackage{amssymb}\n";
162
163         // latexsym.sty
164         if (latexsym)
165                 packages += "\\usepackage{latexsym}\n";
166
167         // pifont.sty
168         if (pifont)
169                 packages += "\\usepackage{pifont}\n";
170
171         // subfigure.sty
172         if (subfigure)
173                 packages += "\\usepackage{subfigure}\n";
174
175         // floatflt.sty
176         if (floatflt)
177                 packages += "\\usepackage{floatflt}\n";
178
179         // url.sty
180         if (url && ! tclass.provides(LyXTextClass::url))
181                 packages += "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
182                             "                      {\\newcommand{\\url}{\\texttt}}\n";
183         
184         return packages;
185 }
186
187
188 string LaTeXFeatures::getMacros(BufferParams const & /* params */)
189 {
190         string macros;
191
192         // always include this
193         if (true || lyx) 
194                 macros += lyx_def + '\n';
195
196         if (lyxline) 
197                 macros += lyxline_def + '\n';
198
199         if (noun) {
200                 macros += noun_def + '\n';
201         }
202
203         if (lyxarrow) {
204                 macros += lyxarrow_def + '\n';
205         }
206
207         // quotes. 
208         if (quotesinglbase)
209                 macros += quotesinglbase_def + '\n';
210         if (quotedblbase)
211                 macros += quotedblbase_def + '\n';
212         if (guilsinglleft)
213                 macros += guilsinglleft_def + '\n';
214         if (guilsinglright)
215                 macros += guilsinglright_def + '\n';
216         if (guillemotleft)
217                 macros += guillemotleft_def + '\n';
218         if (guillemotright)
219                 macros += guillemotright_def + '\n';
220     
221         // Math mode    
222         if (boldsymbol && !amsstyle)
223                 macros += boldsymbol_def + '\n';
224         if (binom && !amsstyle)
225                 macros += binom_def + '\n';
226
227         // other
228         if (NeedLyXMinipageIndent) 
229                 macros += minipageindent_def;
230         if (LyXParagraphIndent) 
231                 macros += paragraphindent_def;
232         if (NeedLyXFootnoteCode) 
233                 macros += floatingfootnote_def;
234
235         return macros;
236 }
237
238
239 string LaTeXFeatures::getTClassPreamble(BufferParams const & params)
240 {
241         // the text class specific preamble 
242         LyXTextClass const & tclass = textclasslist.TextClass(params.textclass);
243         string tcpreamble = tclass.preamble();
244
245         for (unsigned int i = 0; i < tclass.numLayouts(); ++i) {
246                 if (layout[i]) {
247                         tcpreamble += tclass[i].preamble();
248                 }
249         }
250
251         return tcpreamble;
252 }       
253
254
255 void LaTeXFeatures::showStruct(BufferParams const & params) {
256         lyxerr << "LyX needs the following commands when LaTeXing:"
257         // packs
258                << "\n***** Packages:" << getPackages(params)
259                << "\n***** Macros:" << getMacros(params)
260                << "\n***** Textclass stuff:" << getTClassPreamble(params)
261                << "\n***** done." << endl;
262 }