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";
192 // lyxskak.sty --- newer chess support based on skak.sty
194 packages << "\\usepackage[ps,mover]{lyxskak}\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::getLyXSGMLEntities() const
380 // Definition of entities used in the document that are LyX related.
381 ostringstream entities;
384 entities << "<!ENTITY lyxarrow \"->\">"
388 return entities.str().c_str();
392 string const LaTeXFeatures::getIncludedFiles(string const & fname) const
394 ostringstream sgmlpreamble;
395 string const basename = OnlyPath(fname);
397 FileMap::const_iterator end = IncludedFiles.end();
398 for (FileMap::const_iterator fi = IncludedFiles.begin();
400 sgmlpreamble << "\n<!ENTITY " << fi->first
401 << (IsSGMLFilename(fi->second) ? " SYSTEM \"" : " \"" )
402 << MakeRelPath(fi->second, basename) << "\">";
404 return sgmlpreamble.str().c_str();
408 void LaTeXFeatures::showStruct() const {
409 lyxerr << "LyX needs the following commands when LaTeXing:"
410 << "\n***** Packages:" << getPackages()
411 << "\n***** Macros:" << getMacros()
412 << "\n***** Textclass stuff:" << getTClassPreamble()
413 << "\n***** done." << endl;
417 BufferParams const & LaTeXFeatures::bufferParams() const
423 void LaTeXFeatures::getFloatDefinitions(std::ostream & os) const
425 // Here we will output the code to create the needed float styles.
426 // We will try to do this as minimal as possible.
427 // \floatstyle{ruled}
428 // \newfloat{algorithm}{htbp}{loa}
429 // \floatname{algorithm}{Algorithm}
430 UsedFloats::const_iterator cit = usedFloats.begin();
431 UsedFloats::const_iterator end = usedFloats.end();
432 // ostringstream floats;
433 for (; cit != end; ++cit) {
434 Floating const & fl = floatList.getType((*cit));
436 // For builtin floats we do nothing.
437 if (fl.builtin()) continue;
439 // We have to special case "table" and "figure"
440 if (fl.type() == "tabular" || fl.type() == "figure") {
441 // Output code to modify "table" or "figure"
442 // but only if builtin == false
443 // and that have to be true at this point in the
445 string const type = fl.type();
446 string const placement = fl.placement();
447 string const style = fl.style();
448 if (!style.empty()) {
449 os << "\\floatstyle{" << style << "}\n"
450 << "\\restylefloat{" << type << "}\n";
452 if (!placement.empty()) {
453 os << "\\floatplacement{" << type << "}{"
454 << placement << "}\n";
457 // The other non builtin floats.
459 string const type = fl.type();
460 string const placement = fl.placement();
461 string const ext = fl.ext();
462 string const within = fl.within();
463 string const style = fl.style();
464 string const name = fl.name();
465 os << "\\floatstyle{" << style << "}\n"
466 << "\\newfloat{" << type << "}{" << placement
467 << "}{" << ext << "}";
469 os << "[" << within << "]";
471 << "\\floatname{" << type << "}{"
474 // What missing here is to code to minimalize the code
475 // outputted so that the same floatstyle will not be
476 // used several times, when the same style is still in