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 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, 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;
278 packages << "\\usepackage{float}\n";
283 packages << "\\usepackage[";
284 if (params.use_numerical_citations) {
285 packages << "numbers";
287 packages << "authoryear";
289 packages << "]{natbib}\n";
292 packages << externalPreambles;
294 return packages.str().c_str();
298 string const LaTeXFeatures::getMacros() const
300 ostringstream macros;
302 // always include this
304 macros << lyx_def << '\n';
307 macros << lyxline_def << '\n';
310 macros << noun_def << '\n';
314 macros << lyxarrow_def << '\n';
319 macros << quotesinglbase_def << '\n';
321 macros << quotedblbase_def << '\n';
323 macros << guilsinglleft_def << '\n';
325 macros << guilsinglright_def << '\n';
327 macros << guillemotleft_def << '\n';
329 macros << guillemotright_def << '\n';
332 if (boldsymbol && !amsstyle)
333 macros << boldsymbol_def << '\n';
334 if (binom && !amsstyle)
335 macros << binom_def << '\n';
338 if (NeedLyXMinipageIndent)
339 macros << minipageindent_def;
341 macros << paragraphindent_def;
342 if (NeedLyXFootnoteCode)
343 macros << floatingfootnote_def;
346 getFloatDefinitions(macros);
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';
355 return macros.str().c_str();
359 string const LaTeXFeatures::getTClassPreamble() const
361 // the text class specific preamble
362 LyXTextClass const & tclass =
363 textclasslist.TextClass(params.textclass);
364 ostringstream tcpreamble;
366 tcpreamble << tclass.preamble();
368 for (unsigned int i = 0; i < tclass.numLayouts(); ++i) {
370 tcpreamble << tclass[i].preamble();
374 return tcpreamble.str().c_str();
378 string const LaTeXFeatures::getIncludedFiles(string const & fname) const
380 ostringstream sgmlpreamble;
381 string const basename = OnlyPath(fname);
383 FileMap::const_iterator end = IncludedFiles.end();
384 for (FileMap::const_iterator fi = IncludedFiles.begin();
386 sgmlpreamble << "\n<!ENTITY " << fi->first
387 << (IsSGMLFilename(fi->second) ? " SYSTEM \"" : " \"" )
388 << MakeRelPath(fi->second, basename) << "\">";
390 return sgmlpreamble.str().c_str();
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;
403 BufferParams const & LaTeXFeatures::bufferParams() const
409 void LaTeXFeatures::getFloatDefinitions(std::ostream & os) const
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));
422 // For builtin floats we do nothing.
423 if (fl.builtin()) continue;
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
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";
438 if (!placement.empty()) {
439 os << "\\floatplacement{" << type << "}{"
440 << placement << "}\n";
443 // The other non builtin floats.
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 << "}";
455 os << "[" << within << "]";
457 << "\\floatname{" << type << "}{"
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