1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 the LyX Team.
9 * ====================================================== */
14 #pragma implementation "LaTeXFeatures.h"
21 #include "LaTeXFeatures.h"
22 #include "bufferparams.h"
24 #include "support/filetools.h"
25 #include "support/lstrings.h"
26 #include "FloatList.h"
31 LaTeXFeatures::LaTeXFeatures(BufferParams const & p, LyXTextClass::size_type n)
32 : layout(n, false), params(p)
37 graphics = false; // INSET_GRAPHICS: remove this when InsetFig is thrown.
64 quotesinglbase = false;
66 guilsinglleft = false;
67 guilsinglright = false;
68 guillemotleft = false;
69 guillemotright = false;
77 ParagraphIndent = false;
78 NeedLyXFootnoteCode = false;
79 NeedLyXMinipageIndent = false;
83 void LaTeXFeatures::require(string const & name)
85 if (name == "array") {
87 } else if (name == "color") {
89 } else if (name == "graphics") {
91 graphics = true;// INSET_GRAPHICS: remove this when InsetFig is thrown.
92 } else if (name == "setspace") {
94 } else if (name == "makeidx") {
96 } else if (name == "verbatim") {
98 } else if (name == "longtable") {
100 //} else if (name == "algorithm") {
102 } else if (name == "rotating") {
104 } else if (name == "amssymb") {
106 } else if (name == "latexsym") {
108 } else if (name == "pifont") {
110 } else if (name == "subfigure") {
112 } else if (name == "floatflt") {
114 } else if (name == "url") {
116 } else if (name == "varioref") {
118 } else if (name == "prettyref") {
120 } else if (name == "chess") {
122 } else if (name == "amsstyle") {
124 } else if (name == "boldsymbol") {
126 } else if (name == "binom") {
128 } else if (name == "natbib") {
130 } else if (name == "float") {
136 string const LaTeXFeatures::getPackages() const
138 ostringstream packages;
139 LyXTextClass const & tclass =
140 textclasslist.TextClass(params.textclass);
144 packages << "\\usepackage{array}\n";
148 if (params.graphicsDriver == "default")
149 packages << "\\usepackage{color}\n";
151 packages << "\\usepackage["
152 << params.graphicsDriver
158 if (! tclass.provides(LyXTextClass::makeidx)
159 && params.language->babel() != "french") // french provides
161 // French should not be hardcoded. (Lgb)
162 packages << "\\usepackage{makeidx}\n";
163 packages << "\\makeindex\n";
167 if (graphicx && params.graphicsDriver != "none") {
168 if (params.graphicsDriver == "default")
169 packages << "\\usepackage{graphicx}\n";
171 packages << "\\usepackage["
172 << params.graphicsDriver
176 // INSET_GRAPHICS: remove this when InsetFig is thrown.
178 if (graphics && params.graphicsDriver != "none") {
179 if (params.graphicsDriver == "default")
180 packages << "\\usepackage{graphics}\n";
182 packages << "\\usepackage["
183 << params.graphicsDriver
189 packages << "\\usepackage{verbatim}\n";
192 // packages << "\\usepackage{algorithm}\n";
197 packages << "\\usepackage{lyxchess}\n";
201 if ((params.spacing.getSpace() != Spacing::Single
202 && !params.spacing.isDefault())
204 packages << "\\usepackage{setspace}\n";
206 switch (params.spacing.getSpace()) {
207 case Spacing::Default:
208 case Spacing::Single:
209 // we dont use setspace.sty so dont print anything
210 //packages += "\\singlespacing\n";
212 case Spacing::Onehalf:
213 packages << "\\onehalfspacing\n";
215 case Spacing::Double:
216 packages << "\\doublespacing\n";
219 packages << "\\setstretch{"
220 << params.spacing.getValue() << "}\n";
226 packages << "\\usepackage{longtable}\n";
230 packages << "\\usepackage{rotating}\n";
233 if (amssymb || params.use_amsmath)
234 packages << "\\usepackage{amssymb}\n";
238 packages << "\\usepackage{latexsym}\n";
242 packages << "\\usepackage{pifont}\n";
246 packages << "\\usepackage{subfigure}\n";
250 packages << "\\usepackage{floatflt}\n";
253 if (url && ! tclass.provides(LyXTextClass::url))
254 packages << "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
255 " {\\newcommand{\\url}{\\texttt}}\n";
259 packages << "\\usepackage{varioref}\n";
263 packages << "\\usepackage{prettyref}\n";
266 // We only need float.sty if we use non builtin floats, or if we
267 // use the "H" modifier. This includes modified table and
268 // figure floats. (Lgb)
269 if (!usedFloats.empty()) {
270 UsedFloats::const_iterator beg = usedFloats.begin();
271 UsedFloats::const_iterator end = usedFloats.end();
272 for (; beg != end; ++beg) {
273 Floating const & fl = floatList.getType((*beg));
274 if (!fl.type().empty() && !fl.builtin()) {
275 const_cast<LaTeXFeatures *>(this)->floats = true;
281 packages << "\\usepackage{float}\n";
286 packages << "\\usepackage[";
287 if (params.use_numerical_citations) {
288 packages << "numbers";
290 packages << "authoryear";
292 packages << "]{natbib}\n";
295 packages << externalPreambles;
297 return packages.str().c_str();
301 string const LaTeXFeatures::getMacros() const
303 ostringstream macros;
305 // always include this
307 macros << lyx_def << '\n';
310 macros << lyxline_def << '\n';
313 macros << noun_def << '\n';
317 macros << lyxarrow_def << '\n';
322 macros << quotesinglbase_def << '\n';
324 macros << quotedblbase_def << '\n';
326 macros << guilsinglleft_def << '\n';
328 macros << guilsinglright_def << '\n';
330 macros << guillemotleft_def << '\n';
332 macros << guillemotright_def << '\n';
335 if (boldsymbol && !amsstyle)
336 macros << boldsymbol_def << '\n';
337 if (binom && !amsstyle)
338 macros << binom_def << '\n';
341 if (NeedLyXMinipageIndent)
342 macros << minipageindent_def;
344 macros << paragraphindent_def;
345 if (NeedLyXFootnoteCode)
346 macros << floatingfootnote_def;
349 getFloatDefinitions(macros);
351 for (LanguageList::const_iterator cit = UsedLanguages.begin();
352 cit != UsedLanguages.end(); ++cit)
353 if (!(*cit)->latex_options().empty())
354 macros << (*cit)->latex_options() << '\n';
355 if (!params.language->latex_options().empty())
356 macros << params.language->latex_options() << '\n';
358 return macros.str().c_str();
362 string const LaTeXFeatures::getTClassPreamble() const
364 // the text class specific preamble
365 LyXTextClass const & tclass =
366 textclasslist.TextClass(params.textclass);
367 ostringstream tcpreamble;
369 tcpreamble << tclass.preamble();
371 for (unsigned int i = 0; i < tclass.numLayouts(); ++i) {
373 tcpreamble << tclass[i].preamble();
377 return tcpreamble.str().c_str();
381 string const LaTeXFeatures::getIncludedFiles(string const & fname) const
383 ostringstream sgmlpreamble;
384 string const basename = OnlyPath(fname);
386 FileMap::const_iterator end = IncludedFiles.end();
387 for (FileMap::const_iterator fi = IncludedFiles.begin();
389 sgmlpreamble << "\n<!ENTITY " << fi->first
390 << (IsSGMLFilename(fi->second) ? " SYSTEM \"" : " \"" )
391 << MakeRelPath(fi->second, basename) << "\">";
393 return sgmlpreamble.str().c_str();
397 void LaTeXFeatures::showStruct() const {
398 lyxerr << "LyX needs the following commands when LaTeXing:"
399 << "\n***** Packages:" << getPackages()
400 << "\n***** Macros:" << getMacros()
401 << "\n***** Textclass stuff:" << getTClassPreamble()
402 << "\n***** done." << endl;
406 BufferParams const & LaTeXFeatures::bufferParams() const
412 void LaTeXFeatures::getFloatDefinitions(std::ostream & os) const
414 // Here we will output the code to create the needed float styles.
415 // We will try to do this as minimal as possible.
416 // \floatstyle{ruled}
417 // \newfloat{algorithm}{htbp}{loa}
418 // \floatname{algorithm}{Algorithm}
419 UsedFloats::const_iterator cit = usedFloats.begin();
420 UsedFloats::const_iterator end = usedFloats.end();
421 // ostringstream floats;
422 for (; cit != end; ++cit) {
423 Floating const & fl = floatList.getType((*cit));
425 // For builtin floats we do nothing.
426 if (fl.builtin()) continue;
428 // We have to special case "table" and "figure"
429 if (fl.type() == "tabular" || fl.type() == "figure") {
430 // Output code to modify "table" or "figure"
431 // but only if builtin == false
432 // and that have to be true at this point in the
434 string const type = fl.type();
435 string const placement = fl.placement();
436 string const style = fl.style();
437 if (!style.empty()) {
438 os << "\\floatstyle{" << style << "}\n"
439 << "\\restylefloat{" << type << "}\n";
441 if (!placement.empty()) {
442 os << "\\floatplacement{" << type << "}{"
443 << placement << "}\n";
446 // The other non builtin floats.
448 string const type = fl.type();
449 string const placement = fl.placement();
450 string const ext = fl.ext();
451 string const within = fl.within();
452 string const style = fl.style();
453 string const name = fl.name();
454 os << "\\floatstyle{" << style << "}\n"
455 << "\\newfloat{" << type << "}{" << placement
456 << "}{" << ext << "}";
458 os << "[" << within << "]";
460 << "\\floatname{" << type << "}{"
463 // What missing here is to code to minimalize the code
464 // outputted so that the same flotastyle will not be
465 // used several times. when the same style is still in