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 * ====================================================== */
13 #include "LaTeXFeatures.h"
17 #include "bufferparams.h"
18 #include "FloatList.h"
24 #include "support/filetools.h"
25 #include "support/lstrings.h"
27 using namespace lyx::support;
29 using lyx::textclass_type;
38 LaTeXFeatures::LaTeXFeatures(BufferParams const & p)
43 bool LaTeXFeatures::useBabel() const
45 return lyxrc.language_use_babel ||
46 bufferParams().language->lang() != lyxrc.default_language ||
51 void LaTeXFeatures::require(string const & name)
56 features.push_back(name);
60 void LaTeXFeatures::useLayout(string const & layoutname)
62 // Some code to avoid loops in dependency definition
64 const int maxlevel = 30;
65 if (level > maxlevel) {
66 lyxerr << "LaTeXFeatures::useLayout: maximum level of "
67 << "recursion attained by layout "
68 << layoutname << endl;
72 LyXTextClass const & tclass = params.getLyXTextClass();
73 if (tclass.hasLayout(layoutname)) {
74 // Is this layout already in usedLayouts?
75 list<string>::const_iterator cit = usedLayouts.begin();
76 list<string>::const_iterator end = usedLayouts.end();
77 for (; cit != end; ++cit) {
78 if (layoutname == *cit)
82 LyXLayout_ptr const & lyt = tclass[layoutname];
83 if (!lyt->depends_on().empty()) {
85 useLayout(lyt->depends_on());
88 usedLayouts.push_back(layoutname);
90 lyxerr << "LaTeXFeatures::useLayout: layout `"
91 << layoutname << "' does not exist in this class"
99 bool LaTeXFeatures::isRequired(string const & name) const
101 return find(features.begin(), features.end(), name) != features.end();
105 void LaTeXFeatures::addExternalPreamble(string const & pream)
107 externalPreambles += pream;
111 void LaTeXFeatures::useFloat(string const & name)
113 usedFloats.insert(name);
114 // We only need float.sty if we use non builtin floats, or if we
115 // use the "H" modifier. This includes modified table and
116 // figure floats. (Lgb)
117 Floating const & fl = params.getLyXTextClass().floats().getType(name);
118 if (!fl.type().empty() && !fl.builtin()) {
124 void LaTeXFeatures::useLanguage(Language const * lang)
126 UsedLanguages.insert(lang);
130 void LaTeXFeatures::includeFile(string const & key, string const & name)
132 IncludedFiles[key] = name;
136 bool LaTeXFeatures::hasLanguages() const
138 return !UsedLanguages.empty();
142 string LaTeXFeatures::getLanguages() const
144 ostringstream languages;
146 for (LanguageList::const_iterator cit =
147 UsedLanguages.begin();
148 cit != UsedLanguages.end();
150 languages << (*cit)->babel() << ',';
152 return STRCONV(languages.str());
156 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding) const
158 set<string> encodings;
159 for (LanguageList::const_iterator it =
160 UsedLanguages.begin();
161 it != UsedLanguages.end(); ++it)
162 if ((*it)->encoding()->LatexName() != doc_encoding)
163 encodings.insert((*it)->encoding()->LatexName());
169 char const * simplefeatures[] = {
185 int const nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
190 string const LaTeXFeatures::getPackages() const
192 ostringstream packages;
193 LyXTextClass const & tclass = params.getLyXTextClass();
196 // These are all the 'simple' includes. i.e
197 // packages which we just \usepackage{package}
199 for (int i = 0; i < nb_simplefeatures; ++i) {
200 if (isRequired(simplefeatures[i]))
201 packages << "\\usepackage{"
202 << simplefeatures[i] << "}\n";
206 // The rest of these packages are somewhat more complicated
210 if (isRequired("amsmath")
211 && !tclass.provides(LyXTextClass::amsmath)
212 && params.use_amsmath != BufferParams::AMS_OFF) {
213 packages << "\\usepackage{amsmath}\n";
217 if (isRequired("color")) {
218 if (params.graphicsDriver == "default")
219 packages << "\\usepackage[usenames]{color}\n";
221 packages << "\\usepackage["
222 << params.graphicsDriver
228 if (isRequired("makeidx")) {
229 if (! tclass.provides(LyXTextClass::makeidx))
230 packages << "\\usepackage{makeidx}\n";
231 packages << "\\makeindex\n";
235 if (isRequired("graphicx") && params.graphicsDriver != "none") {
236 if (params.graphicsDriver == "default")
237 packages << "\\usepackage{graphicx}\n";
239 packages << "\\usepackage["
240 << params.graphicsDriver
245 // packages << "\\usepackage{algorithm}\n";
248 // lyxskak.sty --- newer chess support based on skak.sty
249 if (isRequired("chess")) {
250 packages << "\\usepackage[ps,mover]{lyxskak}\n";
254 if ((params.spacing.getSpace() != Spacing::Single
255 && !params.spacing.isDefault())
256 || isRequired("setspace")) {
257 packages << "\\usepackage{setspace}\n";
259 switch (params.spacing.getSpace()) {
260 case Spacing::Default:
261 case Spacing::Single:
262 // we dont use setspace.sty so dont print anything
263 //packages += "\\singlespacing\n";
265 case Spacing::Onehalf:
266 packages << "\\onehalfspacing\n";
268 case Spacing::Double:
269 packages << "\\doublespacing\n";
272 packages << "\\setstretch{"
273 << params.spacing.getValue() << "}\n";
278 if (isRequired("amssymb") || params.use_amsmath == BufferParams::AMS_ON)
279 packages << "\\usepackage{amssymb}\n";
281 if (isRequired("url") && ! tclass.provides(LyXTextClass::url))
282 packages << "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
283 " {\\newcommand{\\url}{\\texttt}}\n";
287 if (isRequired("natbib") && ! tclass.provides(LyXTextClass::natbib)) {
288 packages << "\\usepackage[";
289 if (params.use_numerical_citations) {
290 packages << "numbers";
292 packages << "authoryear";
294 packages << "]{natbib}\n";
297 packages << externalPreambles;
299 return STRCONV(packages.str());
303 string const LaTeXFeatures::getMacros() const
305 ostringstream macros;
307 if (isRequired("LyX"))
308 macros << lyx_def << '\n';
310 if (isRequired("lyxline"))
311 macros << lyxline_def << '\n';
313 if (isRequired("noun"))
314 macros << noun_def << '\n';
316 if (isRequired("lyxarrow"))
317 macros << lyxarrow_def << '\n';
320 if (isRequired("quotesinglbase"))
321 macros << quotesinglbase_def << '\n';
322 if (isRequired("quotedblbase"))
323 macros << quotedblbase_def << '\n';
324 if (isRequired("guilsinglleft"))
325 macros << guilsinglleft_def << '\n';
326 if (isRequired("guilsinglright"))
327 macros << guilsinglright_def << '\n';
328 if (isRequired("guillemotleft"))
329 macros << guillemotleft_def << '\n';
330 if (isRequired("guillemotright"))
331 macros << guillemotright_def << '\n';
334 if (isRequired("boldsymbol") && !isRequired("amsmath"))
335 macros << boldsymbol_def << '\n';
336 if (isRequired("binom") && !isRequired("amsmath"))
337 macros << binom_def << '\n';
338 if (isRequired("mathcircumflex"))
339 macros << mathcircumflex_def << '\n';
342 if (isRequired("NeedLyXMinipageIndent"))
343 macros << minipageindent_def;
344 if (isRequired("ParagraphLeftIndent"))
345 macros << paragraphleftindent_def;
346 if (isRequired("NeedLyXFootnoteCode"))
347 macros << floatingfootnote_def;
349 // some problems with tex->html converters
350 if (isRequired("NeedTabularnewline"))
351 macros << tabularnewline_def;
354 getFloatDefinitions(macros);
356 return STRCONV(macros.str());
360 string const LaTeXFeatures::getBabelOptions() const
364 for (LanguageList::const_iterator cit = UsedLanguages.begin();
365 cit != UsedLanguages.end(); ++cit)
366 if (!(*cit)->latex_options().empty())
367 tmp << (*cit)->latex_options() << '\n';
368 if (!params.language->latex_options().empty())
369 tmp << params.language->latex_options() << '\n';
371 return STRCONV(tmp.str());
375 string const LaTeXFeatures::getTClassPreamble() const
377 // the text class specific preamble
378 LyXTextClass const & tclass = params.getLyXTextClass();
379 ostringstream tcpreamble;
381 tcpreamble << tclass.preamble();
383 list<string>::const_iterator cit = usedLayouts.begin();
384 list<string>::const_iterator end = usedLayouts.end();
385 for (; cit != end; ++cit) {
386 tcpreamble << tclass[*cit]->preamble();
389 return STRCONV(tcpreamble.str());
393 string const LaTeXFeatures::getLyXSGMLEntities() const
395 // Definition of entities used in the document that are LyX related.
396 ostringstream entities;
398 if (isRequired("lyxarrow")) {
399 entities << "<!ENTITY lyxarrow \"->\">" << '\n';
402 return STRCONV(entities.str());
406 string const LaTeXFeatures::getIncludedFiles(string const & fname) const
408 ostringstream sgmlpreamble;
409 string const basename = OnlyPath(fname);
411 FileMap::const_iterator end = IncludedFiles.end();
412 for (FileMap::const_iterator fi = IncludedFiles.begin();
414 sgmlpreamble << "\n<!ENTITY " << fi->first
415 << (IsSGMLFilename(fi->second) ? " SYSTEM \"" : " \"")
416 << MakeRelPath(fi->second, basename) << "\">";
418 return STRCONV(sgmlpreamble.str());
422 void LaTeXFeatures::showStruct() const {
423 lyxerr << "LyX needs the following commands when LaTeXing:"
424 << "\n***** Packages:" << getPackages()
425 << "\n***** Macros:" << getMacros()
426 << "\n***** Textclass stuff:" << getTClassPreamble()
427 << "\n***** done." << endl;
431 BufferParams const & LaTeXFeatures::bufferParams() const
437 void LaTeXFeatures::getFloatDefinitions(ostream & os) const
439 FloatList const & floats = params.getLyXTextClass().floats();
441 // Here we will output the code to create the needed float styles.
442 // We will try to do this as minimal as possible.
443 // \floatstyle{ruled}
444 // \newfloat{algorithm}{htbp}{loa}
445 // \floatname{algorithm}{Algorithm}
446 UsedFloats::const_iterator cit = usedFloats.begin();
447 UsedFloats::const_iterator end = usedFloats.end();
448 // ostringstream floats;
449 for (; cit != end; ++cit) {
450 Floating const & fl = floats.getType((*cit));
452 // For builtin floats we do nothing.
453 if (fl.builtin()) continue;
455 // We have to special case "table" and "figure"
456 if (fl.type() == "tabular" || fl.type() == "figure") {
457 // Output code to modify "table" or "figure"
458 // but only if builtin == false
459 // and that have to be true at this point in the
461 string const type = fl.type();
462 string const placement = fl.placement();
463 string const style = fl.style();
464 if (!style.empty()) {
465 os << "\\floatstyle{" << style << "}\n"
466 << "\\restylefloat{" << type << "}\n";
468 if (!placement.empty()) {
469 os << "\\floatplacement{" << type << "}{"
470 << placement << "}\n";
473 // The other non builtin floats.
475 string const type = fl.type();
476 string const placement = fl.placement();
477 string const ext = fl.ext();
478 string const within = fl.within();
479 string const style = fl.style();
480 string const name = fl.name();
481 os << "\\floatstyle{" << style << "}\n"
482 << "\\newfloat{" << type << "}{" << placement
483 << "}{" << ext << '}';
485 os << '[' << within << ']';
487 << "\\floatname{" << type << "}{"
490 // What missing here is to code to minimalize the code
491 // output so that the same floatstyle will not be
492 // used several times, when the same style is still in