2 * \file LaTeXFeatures.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
10 * \author André Pönitz
12 * Full author contact details are available in file CREDITS.
17 #include "LaTeXFeatures.h"
19 #include "bufferparams.h"
23 #include "FloatList.h"
28 #include "support/filetools.h"
30 #include "support/std_sstream.h"
32 using lyx::support::IsSGMLFilename;
33 using lyx::support::MakeRelPath;
34 using lyx::support::OnlyPath;
41 using std::ostringstream;
44 namespace biblio = lyx::biblio;
47 LaTeXFeatures::LaTeXFeatures(Buffer const & b, BufferParams const & p, bool n)
48 : buffer_(&b), params_(p), nice_(n)
52 bool LaTeXFeatures::useBabel() const
54 return lyxrc.language_use_babel ||
55 bufferParams().language->lang() != lyxrc.default_language ||
60 void LaTeXFeatures::require(string const & name)
65 features_.push_back(name);
69 void LaTeXFeatures::useLayout(string const & layoutname)
71 // Some code to avoid loops in dependency definition
73 const int maxlevel = 30;
74 if (level > maxlevel) {
75 lyxerr << "LaTeXFeatures::useLayout: maximum level of "
76 << "recursion attained by layout "
77 << layoutname << endl;
81 LyXTextClass const & tclass = params_.getLyXTextClass();
82 if (tclass.hasLayout(layoutname)) {
83 // Is this layout already in usedLayouts?
84 list<string>::const_iterator cit = usedLayouts_.begin();
85 list<string>::const_iterator end = usedLayouts_.end();
86 for (; cit != end; ++cit) {
87 if (layoutname == *cit)
91 LyXLayout_ptr const & lyt = tclass[layoutname];
92 if (!lyt->depends_on().empty()) {
94 useLayout(lyt->depends_on());
97 usedLayouts_.push_back(layoutname);
99 lyxerr << "LaTeXFeatures::useLayout: layout `"
100 << layoutname << "' does not exist in this class"
108 bool LaTeXFeatures::isRequired(string const & name) const
110 return find(features_.begin(), features_.end(), name) != features_.end();
114 void LaTeXFeatures::addExternalPreamble(string const & preamble)
116 FeaturesList::const_iterator begin = preamble_snippets_.begin();
117 FeaturesList::const_iterator end = preamble_snippets_.end();
118 if (find(begin, end, preamble) == end)
119 preamble_snippets_.push_back(preamble);
123 void LaTeXFeatures::useFloat(string const & name)
125 usedFloats_.insert(name);
126 // We only need float.sty if we use non builtin floats, or if we
127 // use the "H" modifier. This includes modified table and
128 // figure floats. (Lgb)
129 Floating const & fl = params_.getLyXTextClass().floats().getType(name);
130 if (!fl.type().empty() && !fl.builtin()) {
136 void LaTeXFeatures::useLanguage(Language const * lang)
138 UsedLanguages_.insert(lang);
142 void LaTeXFeatures::includeFile(string const & key, string const & name)
144 IncludedFiles_[key] = name;
148 bool LaTeXFeatures::hasLanguages() const
150 return !UsedLanguages_.empty();
154 string LaTeXFeatures::getLanguages() const
156 ostringstream languages;
158 for (LanguageList::const_iterator cit =
159 UsedLanguages_.begin();
160 cit != UsedLanguages_.end();
162 languages << (*cit)->babel() << ',';
164 return languages.str();
168 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding) const
170 set<string> encodings;
171 LanguageList::const_iterator it = UsedLanguages_.begin();
172 LanguageList::const_iterator end = UsedLanguages_.end();
173 for (; it != end; ++it)
174 if ((*it)->encoding()->LatexName() != doc_encoding)
175 encodings.insert((*it)->encoding()->LatexName());
181 char const * simplefeatures[] = {
200 int const nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
205 string const LaTeXFeatures::getPackages() const
207 ostringstream packages;
208 LyXTextClass const & tclass = params_.getLyXTextClass();
211 // These are all the 'simple' includes. i.e
212 // packages which we just \usepackage{package}
214 for (int i = 0; i < nb_simplefeatures; ++i) {
215 if (isRequired(simplefeatures[i]))
216 packages << "\\usepackage{"
217 << simplefeatures[i] << "}\n";
221 // The rest of these packages are somewhat more complicated
225 if (isRequired("amsmath")
226 && !tclass.provides(LyXTextClass::amsmath)
227 && params_.use_amsmath != BufferParams::AMS_OFF) {
228 packages << "\\usepackage{amsmath}\n";
232 if (isRequired("color")) {
233 if (params_.graphicsDriver == "default")
234 packages << "\\usepackage[usenames]{color}\n";
236 packages << "\\usepackage["
237 << params_.graphicsDriver
243 if (isRequired("makeidx")) {
244 if (! tclass.provides(LyXTextClass::makeidx))
245 packages << "\\usepackage{makeidx}\n";
246 packages << "\\makeindex\n";
250 if (isRequired("graphicx") && params_.graphicsDriver != "none") {
251 if (params_.graphicsDriver == "default")
252 packages << "\\usepackage{graphicx}\n";
254 packages << "\\usepackage["
255 << params_.graphicsDriver
260 // packages << "\\usepackage{algorithm}\n";
263 // lyxskak.sty --- newer chess support based on skak.sty
264 if (isRequired("chess")) {
265 packages << "\\usepackage[ps,mover]{lyxskak}\n";
269 if ((params_.spacing().getSpace() != Spacing::Single
270 && !params_.spacing().isDefault())
271 || isRequired("setspace")) {
272 packages << "\\usepackage{setspace}\n";
274 switch (params_.spacing().getSpace()) {
275 case Spacing::Default:
276 case Spacing::Single:
277 // we dont use setspace.sty so dont print anything
278 //packages += "\\singlespacing\n";
280 case Spacing::Onehalf:
281 packages << "\\onehalfspacing\n";
283 case Spacing::Double:
284 packages << "\\doublespacing\n";
287 packages << "\\setstretch{"
288 << params_.spacing().getValue() << "}\n";
293 if (isRequired("amssymb") || params_.use_amsmath == BufferParams::AMS_ON)
294 packages << "\\usepackage{amssymb}\n";
296 if (isRequired("url") && ! tclass.provides(LyXTextClass::url))
297 packages << "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
298 " {\\newcommand{\\url}{\\texttt}}\n";
302 if (isRequired("natbib") && ! tclass.provides(LyXTextClass::natbib)) {
303 packages << "\\usepackage[";
304 if (params_.cite_engine == biblio::ENGINE_NATBIB_NUMERICAL) {
305 packages << "numbers";
307 packages << "authoryear";
309 packages << "]{natbib}\n";
312 // bibtopic -- the dot provides the aux file naming which
314 if (isRequired("bibtopic")) {
315 packages << "\\usepackage[dot]{bibtopic}\n";
318 return packages.str();
322 string const LaTeXFeatures::getMacros() const
324 ostringstream macros;
326 if (!preamble_snippets_.empty())
328 FeaturesList::const_iterator pit = preamble_snippets_.begin();
329 FeaturesList::const_iterator pend = preamble_snippets_.end();
330 for (; pit != pend; ++pit) {
331 macros << *pit << '\n';
334 if (isRequired("LyX"))
335 macros << lyx_def << '\n';
337 if (isRequired("lyxline"))
338 macros << lyxline_def << '\n';
340 if (isRequired("noun"))
341 macros << noun_def << '\n';
343 if (isRequired("lyxarrow"))
344 macros << lyxarrow_def << '\n';
347 if (isRequired("quotesinglbase"))
348 macros << quotesinglbase_def << '\n';
349 if (isRequired("quotedblbase"))
350 macros << quotedblbase_def << '\n';
351 if (isRequired("guilsinglleft"))
352 macros << guilsinglleft_def << '\n';
353 if (isRequired("guilsinglright"))
354 macros << guilsinglright_def << '\n';
355 if (isRequired("guillemotleft"))
356 macros << guillemotleft_def << '\n';
357 if (isRequired("guillemotright"))
358 macros << guillemotright_def << '\n';
361 if (isRequired("boldsymbol") && !isRequired("amsmath"))
362 macros << boldsymbol_def << '\n';
363 if (isRequired("binom") && !isRequired("amsmath"))
364 macros << binom_def << '\n';
365 if (isRequired("mathcircumflex"))
366 macros << mathcircumflex_def << '\n';
369 if (isRequired("ParagraphLeftIndent"))
370 macros << paragraphleftindent_def;
371 if (isRequired("NeedLyXFootnoteCode"))
372 macros << floatingfootnote_def;
374 // some problems with tex->html converters
375 if (isRequired("NeedTabularnewline"))
376 macros << tabularnewline_def;
378 // greyedout environment (note inset)
379 if (isRequired("lyxgreyedout"))
380 macros << lyxgreyedout_def;
382 if (isRequired("lyxdot"))
383 macros << lyxdot_def << '\n';
386 getFloatDefinitions(macros);
392 string const LaTeXFeatures::getBabelOptions() const
396 LanguageList::const_iterator it = UsedLanguages_.begin();
397 LanguageList::const_iterator end = UsedLanguages_.end();
398 for (; it != end; ++it)
399 if (!(*it)->latex_options().empty())
400 tmp << (*it)->latex_options() << '\n';
401 if (!params_.language->latex_options().empty())
402 tmp << params_.language->latex_options() << '\n';
408 string const LaTeXFeatures::getTClassPreamble() const
410 // the text class specific preamble
411 LyXTextClass const & tclass = params_.getLyXTextClass();
412 ostringstream tcpreamble;
414 tcpreamble << tclass.preamble();
416 list<string>::const_iterator cit = usedLayouts_.begin();
417 list<string>::const_iterator end = usedLayouts_.end();
418 for (; cit != end; ++cit) {
419 tcpreamble << tclass[*cit]->preamble();
422 CharStyles::iterator cs = tclass.charstyles().begin();
423 CharStyles::iterator csend = tclass.charstyles().end();
424 for (; cs != csend; ++cs) {
425 if (isRequired(cs->name))
426 tcpreamble << cs->preamble;
429 return tcpreamble.str();
433 string const LaTeXFeatures::getLyXSGMLEntities() const
435 // Definition of entities used in the document that are LyX related.
436 ostringstream entities;
438 if (isRequired("lyxarrow")) {
439 entities << "<!ENTITY lyxarrow \"->\">" << '\n';
442 return entities.str();
446 string const LaTeXFeatures::getIncludedFiles(string const & fname) const
448 ostringstream sgmlpreamble;
449 string const basename = OnlyPath(fname);
451 FileMap::const_iterator end = IncludedFiles_.end();
452 for (FileMap::const_iterator fi = IncludedFiles_.begin();
454 sgmlpreamble << "\n<!ENTITY " << fi->first
455 << (IsSGMLFilename(fi->second) ? " SYSTEM \"" : " \"")
456 << MakeRelPath(fi->second, basename) << "\">";
458 return sgmlpreamble.str();
462 void LaTeXFeatures::showStruct() const {
463 lyxerr << "LyX needs the following commands when LaTeXing:"
464 << "\n***** Packages:" << getPackages()
465 << "\n***** Macros:" << getMacros()
466 << "\n***** Textclass stuff:" << getTClassPreamble()
467 << "\n***** done." << endl;
471 Buffer const & LaTeXFeatures::buffer() const
477 void LaTeXFeatures::setBuffer(Buffer const & buffer)
483 BufferParams const & LaTeXFeatures::bufferParams() const
489 void LaTeXFeatures::getFloatDefinitions(ostream & os) const
491 FloatList const & floats = params_.getLyXTextClass().floats();
493 // Here we will output the code to create the needed float styles.
494 // We will try to do this as minimal as possible.
495 // \floatstyle{ruled}
496 // \newfloat{algorithm}{htbp}{loa}
497 // \floatname{algorithm}{Algorithm}
498 UsedFloats::const_iterator cit = usedFloats_.begin();
499 UsedFloats::const_iterator end = usedFloats_.end();
500 // ostringstream floats;
501 for (; cit != end; ++cit) {
502 Floating const & fl = floats.getType((*cit));
504 // For builtin floats we do nothing.
505 if (fl.builtin()) continue;
507 // We have to special case "table" and "figure"
508 if (fl.type() == "tabular" || fl.type() == "figure") {
509 // Output code to modify "table" or "figure"
510 // but only if builtin == false
511 // and that have to be true at this point in the
513 string const type = fl.type();
514 string const placement = fl.placement();
515 string const style = fl.style();
516 if (!style.empty()) {
517 os << "\\floatstyle{" << style << "}\n"
518 << "\\restylefloat{" << type << "}\n";
520 if (!placement.empty()) {
521 os << "\\floatplacement{" << type << "}{"
522 << placement << "}\n";
525 // The other non builtin floats.
527 string const type = fl.type();
528 string const placement = fl.placement();
529 string const ext = fl.ext();
530 string const within = fl.within();
531 string const style = fl.style();
532 string const name = fl.name();
533 os << "\\floatstyle{" << style << "}\n"
534 << "\\newfloat{" << type << "}{" << placement
535 << "}{" << ext << '}';
537 os << '[' << within << ']';
539 << "\\floatname{" << type << "}{"
542 // What missing here is to code to minimalize the code
543 // output so that the same floatstyle will not be
544 // used several times, when the same style is still in