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"
24 #include "FloatList.h"
31 #include "support/docstream.h"
32 #include "support/filetools.h"
38 using support::isSGMLFilename;
39 using support::libFileSearch;
40 using support::makeRelPath;
41 using support::onlyPath;
48 using std::ostringstream;
51 LaTeXFeatures::PackagesList LaTeXFeatures::packages_;
54 LaTeXFeatures::LaTeXFeatures(Buffer const & b, BufferParams const & p,
55 OutputParams const & r)
56 : buffer_(&b), params_(p), runparams_(r)
60 bool LaTeXFeatures::useBabel() const
62 return lyxrc.language_use_babel ||
63 bufferParams().language->lang() != lyxrc.default_language ||
68 void LaTeXFeatures::require(string const & name)
73 features_.push_back(name);
77 void LaTeXFeatures::getAvailable()
80 string real_file = libFileSearch("", "packages.lst");
82 if (real_file.empty())
85 lex.setFile(real_file);
90 // Make sure that we are clean
93 bool finished = false;
95 while (lex.isOK() && !finished) {
97 case LyXLex::LEX_FEOF:
101 string const name = lex.getString();
102 PackagesList::const_iterator begin = packages_.begin();
103 PackagesList::const_iterator end = packages_.end();
104 if (find(begin, end, name) == end)
105 packages_.push_back(name);
113 void LaTeXFeatures::useLayout(string const & layoutname)
115 // Some code to avoid loops in dependency definition
116 static int level = 0;
117 const int maxlevel = 30;
118 if (level > maxlevel) {
119 lyxerr << "LaTeXFeatures::useLayout: maximum level of "
120 << "recursion attained by layout "
121 << layoutname << endl;
125 LyXTextClass const & tclass = params_.getLyXTextClass();
126 if (tclass.hasLayout(layoutname)) {
127 // Is this layout already in usedLayouts?
128 list<string>::const_iterator cit = usedLayouts_.begin();
129 list<string>::const_iterator end = usedLayouts_.end();
130 for (; cit != end; ++cit) {
131 if (layoutname == *cit)
135 LyXLayout_ptr const & lyt = tclass[layoutname];
136 if (!lyt->depends_on().empty()) {
138 useLayout(lyt->depends_on());
141 usedLayouts_.push_back(layoutname);
143 lyxerr << "LaTeXFeatures::useLayout: layout `"
144 << layoutname << "' does not exist in this class"
152 bool LaTeXFeatures::isRequired(string const & name) const
154 return find(features_.begin(), features_.end(), name) != features_.end();
158 bool LaTeXFeatures::isAvailable(string const & name) const
160 if (packages_.empty())
162 return find(packages_.begin(), packages_.end(), name) != packages_.end();
166 void LaTeXFeatures::addExternalPreamble(string const & preamble)
168 FeaturesList::const_iterator begin = preamble_snippets_.begin();
169 FeaturesList::const_iterator end = preamble_snippets_.end();
170 if (find(begin, end, preamble) == end)
171 preamble_snippets_.push_back(preamble);
175 void LaTeXFeatures::useFloat(string const & name)
177 usedFloats_.insert(name);
178 // We only need float.sty if we use non builtin floats, or if we
179 // use the "H" modifier. This includes modified table and
180 // figure floats. (Lgb)
181 Floating const & fl = params_.getLyXTextClass().floats().getType(name);
182 if (!fl.type().empty() && !fl.builtin()) {
188 void LaTeXFeatures::useLanguage(Language const * lang)
190 UsedLanguages_.insert(lang);
194 void LaTeXFeatures::includeFile(docstring const & key, string const & name)
196 IncludedFiles_[key] = name;
200 bool LaTeXFeatures::hasLanguages() const
202 return !UsedLanguages_.empty();
206 string LaTeXFeatures::getLanguages() const
208 ostringstream languages;
210 for (LanguageList::const_iterator cit =
211 UsedLanguages_.begin();
212 cit != UsedLanguages_.end();
214 languages << (*cit)->babel() << ',';
215 return languages.str();
219 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding) const
221 set<string> encodings;
222 LanguageList::const_iterator it = UsedLanguages_.begin();
223 LanguageList::const_iterator end = UsedLanguages_.end();
224 for (; it != end; ++it)
225 if ((*it)->encoding()->latexName() != doc_encoding)
226 encodings.insert((*it)->encoding()->latexName());
232 char const * simplefeatures[] = {
253 int const nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
258 string const LaTeXFeatures::getPackages() const
260 ostringstream packages;
261 LyXTextClass const & tclass = params_.getLyXTextClass();
264 // These are all the 'simple' includes. i.e
265 // packages which we just \usepackage{package}
267 for (int i = 0; i < nb_simplefeatures; ++i) {
268 if (isRequired(simplefeatures[i]))
269 packages << "\\usepackage{"
270 << simplefeatures[i] << "}\n";
274 // The rest of these packages are somewhat more complicated
278 if (isRequired("amsmath")
279 && !tclass.provides(LyXTextClass::amsmath)
280 && params_.use_amsmath != BufferParams::AMS_OFF) {
281 packages << "\\usepackage{amsmath}\n";
284 // wasysym is a simple feature, but it must be after amsmath if both
286 if (isRequired("wasysym"))
287 packages << "\\usepackage{wasysym}\n";
290 if (isRequired("color")) {
291 if (params_.graphicsDriver == "default")
292 packages << "\\usepackage{color}\n";
294 packages << "\\usepackage["
295 << params_.graphicsDriver
300 if (isRequired("makeidx")) {
301 if (!tclass.provides(LyXTextClass::makeidx))
302 packages << "\\usepackage{makeidx}\n";
303 packages << "\\makeindex\n";
307 if (isRequired("graphicx") && params_.graphicsDriver != "none") {
308 if (params_.graphicsDriver == "default")
309 packages << "\\usepackage{graphicx}\n";
311 packages << "\\usepackage["
312 << params_.graphicsDriver
315 // shadecolor for shaded
316 if (isRequired("framed")) {
317 RGBColor c = RGBColor(lcolor.getX11Name(LColor::shadedbg));
318 packages << "\\definecolor{shadecolor}{rgb}{"
319 << c.r/255 << ',' << c.g/255 << ',' << c.b/255 << "}\n";
323 // packages << "\\usepackage{algorithm}\n";
326 // lyxskak.sty --- newer chess support based on skak.sty
327 if (isRequired("chess")) {
328 packages << "\\usepackage[ps,mover]{lyxskak}\n";
332 if ((params_.spacing().getSpace() != Spacing::Single
333 && !params_.spacing().isDefault())
334 || isRequired("setspace")) {
335 packages << "\\usepackage{setspace}\n";
337 switch (params_.spacing().getSpace()) {
338 case Spacing::Default:
339 case Spacing::Single:
340 // we dont use setspace.sty so dont print anything
341 //packages += "\\singlespacing\n";
343 case Spacing::Onehalf:
344 packages << "\\onehalfspacing\n";
346 case Spacing::Double:
347 packages << "\\doublespacing\n";
350 packages << "\\setstretch{"
351 << params_.spacing().getValue() << "}\n";
356 if (isRequired("amssymb") || params_.use_amsmath == BufferParams::AMS_ON)
357 packages << "\\usepackage{amssymb}\n";
359 if (isRequired("url") && ! tclass.provides(LyXTextClass::url))
360 packages << "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
361 " {\\newcommand{\\url}{\\texttt}}\n";
365 if (isRequired("natbib") && ! tclass.provides(LyXTextClass::natbib)) {
366 packages << "\\usepackage[";
367 if (params_.cite_engine == biblio::ENGINE_NATBIB_NUMERICAL) {
368 packages << "numbers";
370 packages << "authoryear";
372 packages << "]{natbib}\n";
375 // jurabib -- we need version 0.6 at least.
376 if (isRequired("jurabib")) {
377 packages << "\\usepackage{jurabib}[2004/01/25]\n";
380 // bibtopic -- the dot provides the aux file naming which
382 if (isRequired("bibtopic")) {
383 packages << "\\usepackage[dot]{bibtopic}\n";
386 if (isRequired("xy"))
387 packages << "\\usepackage[all]{xy}\n";
389 if (isRequired("nomencl")) {
390 packages << "\\usepackage{nomencl}[2005/09/22]\n"
391 << "\\makenomenclature\n";
394 return packages.str();
398 string const LaTeXFeatures::getMacros() const
400 ostringstream macros;
402 if (!preamble_snippets_.empty())
404 FeaturesList::const_iterator pit = preamble_snippets_.begin();
405 FeaturesList::const_iterator pend = preamble_snippets_.end();
406 for (; pit != pend; ++pit) {
407 macros << *pit << '\n';
410 if (isRequired("LyX"))
411 macros << lyx_def << '\n';
413 if (isRequired("lyxline"))
414 macros << lyxline_def << '\n';
416 if (isRequired("noun"))
417 macros << noun_def << '\n';
419 if (isRequired("lyxarrow"))
420 macros << lyxarrow_def << '\n';
423 if (isRequired("quotesinglbase"))
424 macros << quotesinglbase_def << '\n';
425 if (isRequired("quotedblbase"))
426 macros << quotedblbase_def << '\n';
427 if (isRequired("guilsinglleft"))
428 macros << guilsinglleft_def << '\n';
429 if (isRequired("guilsinglright"))
430 macros << guilsinglright_def << '\n';
431 if (isRequired("guillemotleft"))
432 macros << guillemotleft_def << '\n';
433 if (isRequired("guillemotright"))
434 macros << guillemotright_def << '\n';
437 if (isRequired("boldsymbol") && !isRequired("amsmath"))
438 macros << boldsymbol_def << '\n';
439 if (isRequired("binom") && !isRequired("amsmath"))
440 macros << binom_def << '\n';
441 if (isRequired("mathcircumflex"))
442 macros << mathcircumflex_def << '\n';
445 if (isRequired("ParagraphLeftIndent"))
446 macros << paragraphleftindent_def;
447 if (isRequired("NeedLyXFootnoteCode"))
448 macros << floatingfootnote_def;
450 // some problems with tex->html converters
451 if (isRequired("NeedTabularnewline"))
452 macros << tabularnewline_def;
454 // greyedout environment (note inset)
455 if (isRequired("lyxgreyedout"))
456 macros << lyxgreyedout_def;
458 if (isRequired("lyxdot"))
459 macros << lyxdot_def << '\n';
462 getFloatDefinitions(macros);
468 string const LaTeXFeatures::getBabelOptions() const
472 LanguageList::const_iterator it = UsedLanguages_.begin();
473 LanguageList::const_iterator end = UsedLanguages_.end();
474 for (; it != end; ++it)
475 if (!(*it)->latex_options().empty())
476 tmp << (*it)->latex_options() << '\n';
477 if (!params_.language->latex_options().empty())
478 tmp << params_.language->latex_options() << '\n';
484 docstring const LaTeXFeatures::getTClassPreamble() const
486 // the text class specific preamble
487 LyXTextClass const & tclass = params_.getLyXTextClass();
488 odocstringstream tcpreamble;
490 tcpreamble << tclass.preamble();
492 list<string>::const_iterator cit = usedLayouts_.begin();
493 list<string>::const_iterator end = usedLayouts_.end();
494 for (; cit != end; ++cit) {
495 tcpreamble << tclass[*cit]->preamble();
498 CharStyles::iterator cs = tclass.charstyles().begin();
499 CharStyles::iterator csend = tclass.charstyles().end();
500 for (; cs != csend; ++cs) {
501 if (isRequired(cs->name))
502 tcpreamble << cs->preamble;
505 return tcpreamble.str();
509 docstring const LaTeXFeatures::getLyXSGMLEntities() const
511 // Definition of entities used in the document that are LyX related.
512 odocstringstream entities;
514 if (isRequired("lyxarrow")) {
515 entities << "<!ENTITY lyxarrow \"->\">" << '\n';
518 return entities.str();
522 docstring const LaTeXFeatures::getIncludedFiles(string const & fname) const
524 odocstringstream sgmlpreamble;
525 string const basename = onlyPath(fname);
527 FileMap::const_iterator end = IncludedFiles_.end();
528 for (FileMap::const_iterator fi = IncludedFiles_.begin();
530 sgmlpreamble << "\n<!ENTITY " << fi->first
531 << (isSGMLFilename(fi->second) ? " SYSTEM \"" : " \"")
532 << from_ascii(makeRelPath(fi->second, basename)) << "\">";
534 return sgmlpreamble.str();
538 void LaTeXFeatures::showStruct() const {
539 lyxerr << "LyX needs the following commands when LaTeXing:"
540 << "\n***** Packages:" << getPackages()
541 << "\n***** Macros:" << getMacros()
542 << "\n***** Textclass stuff:" << to_utf8(getTClassPreamble())
543 << "\n***** done." << endl;
547 Buffer const & LaTeXFeatures::buffer() const
553 void LaTeXFeatures::setBuffer(Buffer const & buffer)
559 BufferParams const & LaTeXFeatures::bufferParams() const
565 void LaTeXFeatures::getFloatDefinitions(ostream & os) const
567 FloatList const & floats = params_.getLyXTextClass().floats();
569 // Here we will output the code to create the needed float styles.
570 // We will try to do this as minimal as possible.
571 // \floatstyle{ruled}
572 // \newfloat{algorithm}{htbp}{loa}
573 // \floatname{algorithm}{Algorithm}
574 UsedFloats::const_iterator cit = usedFloats_.begin();
575 UsedFloats::const_iterator end = usedFloats_.end();
576 // ostringstream floats;
577 for (; cit != end; ++cit) {
578 Floating const & fl = floats.getType((*cit));
580 // For builtin floats we do nothing.
581 if (fl.builtin()) continue;
583 // We have to special case "table" and "figure"
584 if (fl.type() == "tabular" || fl.type() == "figure") {
585 // Output code to modify "table" or "figure"
586 // but only if builtin == false
587 // and that have to be true at this point in the
589 string const type = fl.type();
590 string const placement = fl.placement();
591 string const style = fl.style();
592 if (!style.empty()) {
593 os << "\\floatstyle{" << style << "}\n"
594 << "\\restylefloat{" << type << "}\n";
596 if (!placement.empty()) {
597 os << "\\floatplacement{" << type << "}{"
598 << placement << "}\n";
601 // The other non builtin floats.
603 string const type = fl.type();
604 string const placement = fl.placement();
605 string const ext = fl.ext();
606 string const within = fl.within();
607 string const style = fl.style();
608 string const name = fl.name();
609 os << "\\floatstyle{" << style << "}\n"
610 << "\\newfloat{" << type << "}{" << placement
611 << "}{" << ext << '}';
613 os << '[' << within << ']';
615 << "\\floatname{" << type << "}{"
618 // What missing here is to code to minimalize the code
619 // output so that the same floatstyle will not be
620 // used several times, when the same style is still in