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.
63 quotesinglbase = false;
65 guilsinglleft = false;
66 guilsinglright = false;
67 guillemotleft = false;
68 guillemotright = false;
76 ParagraphIndent = false;
77 NeedLyXFootnoteCode = false;
78 NeedLyXMinipageIndent = false;
82 void LaTeXFeatures::require(string const & name)
84 if (name == "array") {
86 } else if (name == "color") {
88 } else if (name == "graphics") {
90 graphics = true;// INSET_GRAPHICS: remove this when InsetFig is thrown.
91 } else if (name == "setspace") {
93 } else if (name == "makeidx") {
95 } else if (name == "verbatim") {
97 } else if (name == "longtable") {
99 //} else if (name == "algorithm") {
101 } else if (name == "rotating") {
103 } else if (name == "amssymb") {
105 } else if (name == "latexsym") {
107 } else if (name == "pifont") {
109 } else if (name == "subfigure") {
111 } else if (name == "floatflt") {
113 } else if (name == "url") {
115 } else if (name == "varioref") {
117 } else if (name == "prettyref") {
119 } else if (name == "chess") {
121 } else if (name == "amsstyle") {
123 } else if (name == "boldsymbol") {
125 } else if (name == "binom") {
127 } else if (name == "natbib") {
133 string const LaTeXFeatures::getPackages() const
135 ostringstream packages;
136 LyXTextClass const & tclass =
137 textclasslist.TextClass(params.textclass);
141 packages << "\\usepackage{array}\n";
145 if (params.graphicsDriver == "default")
146 packages << "\\usepackage{color}\n";
148 packages << "\\usepackage["
149 << params.graphicsDriver
155 if (! tclass.provides(LyXTextClass::makeidx)
156 && params.language->babel() != "french") // french provides
158 // French should not be hardcoded. (Lgb)
159 packages << "\\usepackage{makeidx}\n";
160 packages << "\\makeindex\n";
164 if (graphicx && params.graphicsDriver != "none") {
165 if (params.graphicsDriver == "default")
166 packages << "\\usepackage{graphicx}\n";
168 packages << "\\usepackage["
169 << params.graphicsDriver
173 // INSET_GRAPHICS: remove this when InsetFig is thrown.
175 if (graphics && params.graphicsDriver != "none") {
176 if (params.graphicsDriver == "default")
177 packages << "\\usepackage{graphics}\n";
179 packages << "\\usepackage["
180 << params.graphicsDriver
186 packages << "\\usepackage{verbatim}\n";
189 // packages << "\\usepackage{algorithm}\n";
194 packages << "\\usepackage{lyxchess}\n";
198 if ((params.spacing.getSpace() != Spacing::Single
199 && !params.spacing.isDefault())
201 packages << "\\usepackage{setspace}\n";
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";
209 case Spacing::Onehalf:
210 packages << "\\onehalfspacing\n";
212 case Spacing::Double:
213 packages << "\\doublespacing\n";
216 packages << "\\setstretch{"
217 << params.spacing.getValue() << "}\n";
223 packages << "\\usepackage{longtable}\n";
227 packages << "\\usepackage{rotating}\n";
230 if (amssymb || params.use_amsmath)
231 packages << "\\usepackage{amssymb}\n";
235 packages << "\\usepackage{latexsym}\n";
239 packages << "\\usepackage{pifont}\n";
243 packages << "\\usepackage{subfigure}\n";
247 packages << "\\usepackage{floatflt}\n";
250 if (url && ! tclass.provides(LyXTextClass::url))
251 packages << "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
252 " {\\newcommand{\\url}{\\texttt}}\n";
256 packages << "\\usepackage{varioref}\n";
260 packages << "\\usepackage{prettyref}\n";
263 // We only need float.sty if we use non builtin floats. This includes
264 // modified table and figure floats. (Lgb)
265 if (!usedFloats.empty()) {
266 bool use_float = false;
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()) {
277 packages << "\\usepackage{float}\n";
282 packages << "\\usepackage[";
283 if (params.use_numerical_citations) {
284 packages << "numbers";
286 packages << "authoryear";
288 packages << "]{natbib}\n";
291 packages << externalPreambles;
293 return packages.str().c_str();
297 string const LaTeXFeatures::getMacros() const
299 ostringstream macros;
301 // always include this
303 macros << lyx_def << '\n';
306 macros << lyxline_def << '\n';
309 macros << noun_def << '\n';
313 macros << lyxarrow_def << '\n';
318 macros << quotesinglbase_def << '\n';
320 macros << quotedblbase_def << '\n';
322 macros << guilsinglleft_def << '\n';
324 macros << guilsinglright_def << '\n';
326 macros << guillemotleft_def << '\n';
328 macros << guillemotright_def << '\n';
331 if (boldsymbol && !amsstyle)
332 macros << boldsymbol_def << '\n';
333 if (binom && !amsstyle)
334 macros << binom_def << '\n';
337 if (NeedLyXMinipageIndent)
338 macros << minipageindent_def;
340 macros << paragraphindent_def;
341 if (NeedLyXFootnoteCode)
342 macros << floatingfootnote_def;
345 getFloatDefinitions(macros);
347 for (LanguageList::const_iterator cit = UsedLanguages.begin();
348 cit != UsedLanguages.end(); ++cit)
349 if (!(*cit)->latex_options().empty())
350 macros << (*cit)->latex_options() << '\n';
351 if (!params.language->latex_options().empty())
352 macros << params.language->latex_options() << '\n';
354 return macros.str().c_str();
358 string const LaTeXFeatures::getTClassPreamble() const
360 // the text class specific preamble
361 LyXTextClass const & tclass =
362 textclasslist.TextClass(params.textclass);
363 ostringstream tcpreamble;
365 tcpreamble << tclass.preamble();
367 for (unsigned int i = 0; i < tclass.numLayouts(); ++i) {
369 tcpreamble << tclass[i].preamble();
373 return tcpreamble.str().c_str();
377 string const LaTeXFeatures::getIncludedFiles(string const & fname) const
379 ostringstream sgmlpreamble;
380 string const basename = OnlyPath(fname);
382 FileMap::const_iterator end = IncludedFiles.end();
383 for (FileMap::const_iterator fi = IncludedFiles.begin();
385 sgmlpreamble << "\n<!ENTITY " << fi->first
386 << (IsSGMLFilename(fi->second) ? " SYSTEM \"" : " \"" )
387 << MakeRelPath(fi->second, basename) << "\">";
389 return sgmlpreamble.str().c_str();
393 void LaTeXFeatures::showStruct() const {
394 lyxerr << "LyX needs the following commands when LaTeXing:"
395 << "\n***** Packages:" << getPackages()
396 << "\n***** Macros:" << getMacros()
397 << "\n***** Textclass stuff:" << getTClassPreamble()
398 << "\n***** done." << endl;
402 BufferParams const & LaTeXFeatures::bufferParams() const
408 void LaTeXFeatures::getFloatDefinitions(std::ostream & os) const
410 // Here we will output the code to create the needed float styles.
411 // We will try to do this as minimal as possible.
412 // \floatstyle{ruled}
413 // \newfloat{algorithm}{htbp}{loa}
414 // \floatname{algorithm}{Algorithm}
415 UsedFloats::const_iterator cit = usedFloats.begin();
416 UsedFloats::const_iterator end = usedFloats.end();
417 // ostringstream floats;
418 for (; cit != end; ++cit) {
419 Floating const & fl = floatList.getType((*cit));
421 // For builtin floats we do nothing.
422 if (fl.builtin()) continue;
424 // We have to special case "table" and "figure"
425 if (fl.type() == "tabular" || fl.type() == "figure") {
426 // Output code to modify "table" or "figure"
427 // but only if builtin == false
428 // and that have to be true at this point in the
430 string const type = fl.type();
431 string const placement = fl.placement();
432 string const style = fl.style();
433 if (!style.empty()) {
434 os << "\\floatstyle{" << style << "}\n"
435 << "\\restylefloat{" << type << "}\n";
437 if (!placement.empty()) {
438 os << "\\floatplacement{" << type << "}{"
439 << placement << "}\n";
442 // The other non builtin floats.
444 string const type = fl.type();
445 string const placement = fl.placement();
446 string const ext = fl.ext();
447 string const within = fl.within();
448 string const style = fl.style();
449 string const name = fl.name();
450 os << "\\floatstyle{" << style << "}\n"
451 << "\\newfloat{" << type << "}{" << placement
452 << "}{" << ext << "}";
454 os << "[" << within << "]";
456 << "\\floatname{" << type << "}{"
459 // What missing here is to code to minimalize the code
460 // outputted so that the same flotastyle will not be
461 // used several times. when the same style is still in