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"
34 #include "frontends/controllers/biblio.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 support::FileName const 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);
111 void LaTeXFeatures::useLayout(string const & layoutname)
113 // Some code to avoid loops in dependency definition
114 static int level = 0;
115 const int maxlevel = 30;
116 if (level > maxlevel) {
117 lyxerr << "LaTeXFeatures::useLayout: maximum level of "
118 << "recursion attained by layout "
119 << layoutname << endl;
123 LyXTextClass const & tclass = params_.getLyXTextClass();
124 if (tclass.hasLayout(layoutname)) {
125 // Is this layout already in usedLayouts?
126 list<string>::const_iterator cit = usedLayouts_.begin();
127 list<string>::const_iterator end = usedLayouts_.end();
128 for (; cit != end; ++cit) {
129 if (layoutname == *cit)
133 LyXLayout_ptr const & lyt = tclass[layoutname];
134 if (!lyt->depends_on().empty()) {
136 useLayout(lyt->depends_on());
139 usedLayouts_.push_back(layoutname);
141 lyxerr << "LaTeXFeatures::useLayout: layout `"
142 << layoutname << "' does not exist in this class"
150 bool LaTeXFeatures::isRequired(string const & name) const
152 return find(features_.begin(), features_.end(), name) != features_.end();
156 bool LaTeXFeatures::isAvailable(string const & name)
158 if (packages_.empty())
160 return find(packages_.begin(), packages_.end(), name) != packages_.end();
164 void LaTeXFeatures::addPreambleSnippet(string const & preamble)
166 FeaturesList::const_iterator begin = preamble_snippets_.begin();
167 FeaturesList::const_iterator end = preamble_snippets_.end();
168 if (find(begin, end, preamble) == end)
169 preamble_snippets_.push_back(preamble);
173 void LaTeXFeatures::useFloat(string const & name)
175 usedFloats_.insert(name);
176 // We only need float.sty if we use non builtin floats, or if we
177 // use the "H" modifier. This includes modified table and
178 // figure floats. (Lgb)
179 Floating const & fl = params_.getLyXTextClass().floats().getType(name);
180 if (!fl.type().empty() && !fl.builtin()) {
186 void LaTeXFeatures::useLanguage(Language const * lang)
188 UsedLanguages_.insert(lang);
192 void LaTeXFeatures::includeFile(docstring const & key, string const & name)
194 IncludedFiles_[key] = name;
198 bool LaTeXFeatures::hasLanguages() const
200 return !UsedLanguages_.empty();
204 string LaTeXFeatures::getLanguages() const
206 ostringstream languages;
208 for (LanguageList::const_iterator cit =
209 UsedLanguages_.begin();
210 cit != UsedLanguages_.end();
212 languages << (*cit)->babel() << ',';
213 return languages.str();
217 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding) const
219 set<string> encodings;
220 LanguageList::const_iterator it = UsedLanguages_.begin();
221 LanguageList::const_iterator end = UsedLanguages_.end();
222 for (; it != end; ++it)
223 // thailatex does not use the inputenc package, but sets up
224 // babel directly for tis620-0 encoding, therefore we must
225 // not add tis620-0 to the encoding set.
226 if ((*it)->encoding()->latexName() != doc_encoding &&
227 (*it)->encoding()->name() != "tis620-0")
228 encodings.insert((*it)->encoding()->latexName());
234 char const * simplefeatures[] = {
256 int const nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
261 string const LaTeXFeatures::getPackages() const
263 ostringstream packages;
264 LyXTextClass const & tclass = params_.getLyXTextClass();
267 // These are all the 'simple' includes. i.e
268 // packages which we just \usepackage{package}
270 for (int i = 0; i < nb_simplefeatures; ++i) {
271 if (isRequired(simplefeatures[i]))
272 packages << "\\usepackage{"
273 << simplefeatures[i] << "}\n";
277 // The rest of these packages are somewhat more complicated
281 if (isRequired("amsmath")
282 && !tclass.provides(LyXTextClass::amsmath)
283 && params_.use_amsmath != BufferParams::package_off) {
284 packages << "\\usepackage{amsmath}\n";
287 // wasysym is a simple feature, but it must be after amsmath if both
289 // wasysym redefines some integrals (e.g. iint) from amsmath. That
290 // leads to inconsistent integrals. We only load this package if
291 // esint is used, since esint redefines all relevant integral
292 // symbols from wasysym and amsmath.
293 // See http://bugzilla.lyx.org/show_bug.cgi?id=1942
294 if (isRequired("wasysym") && isRequired("esint") &&
295 params_.use_esint != BufferParams::package_off)
296 packages << "\\usepackage{wasysym}\n";
299 if (isRequired("color")) {
300 if (params_.graphicsDriver == "default")
301 packages << "\\usepackage{color}\n";
303 packages << "\\usepackage["
304 << params_.graphicsDriver
309 if (isRequired("makeidx")) {
310 if (!tclass.provides(LyXTextClass::makeidx))
311 packages << "\\usepackage{makeidx}\n";
312 packages << "\\makeindex\n";
316 if (isRequired("graphicx") && params_.graphicsDriver != "none") {
317 if (params_.graphicsDriver == "default")
318 packages << "\\usepackage{graphicx}\n";
320 packages << "\\usepackage["
321 << params_.graphicsDriver
324 // shadecolor for shaded
325 if (isRequired("framed")) {
326 RGBColor c = RGBColor(lcolor.getX11Name(LColor::shadedbg));
327 packages << "\\definecolor{shadecolor}{rgb}{"
328 << c.r/255 << ',' << c.g/255 << ',' << c.b/255 << "}\n";
332 // packages << "\\usepackage{algorithm}\n";
335 // lyxskak.sty --- newer chess support based on skak.sty
336 if (isRequired("chess")) {
337 packages << "\\usepackage[ps,mover]{lyxskak}\n";
341 if ((params_.spacing().getSpace() != Spacing::Single
342 && !params_.spacing().isDefault())
343 || isRequired("setspace")) {
344 packages << "\\usepackage{setspace}\n";
346 switch (params_.spacing().getSpace()) {
347 case Spacing::Default:
348 case Spacing::Single:
349 // we dont use setspace.sty so dont print anything
350 //packages += "\\singlespacing\n";
352 case Spacing::Onehalf:
353 packages << "\\onehalfspacing\n";
355 case Spacing::Double:
356 packages << "\\doublespacing\n";
359 packages << "\\setstretch{"
360 << params_.spacing().getValue() << "}\n";
365 if (isRequired("amssymb") || params_.use_amsmath == BufferParams::package_on)
366 packages << "\\usepackage{amssymb}\n";
368 // esint must be after amsmath and wasysym, since it will redeclare
369 // inconsistent integral symbols
370 if (isRequired("esint") && params_.use_esint != BufferParams::package_off)
371 packages << "\\usepackage{esint}\n";
374 if (isRequired("url") && ! tclass.provides(LyXTextClass::url))
375 packages << "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
376 " {\\newcommand{\\url}{\\texttt}}\n";
380 if (isRequired("natbib") && ! tclass.provides(LyXTextClass::natbib)) {
381 packages << "\\usepackage[";
382 if (params_.getEngine() == biblio::ENGINE_NATBIB_NUMERICAL) {
383 packages << "numbers";
385 packages << "authoryear";
387 packages << "]{natbib}\n";
390 // jurabib -- we need version 0.6 at least.
391 if (isRequired("jurabib")) {
392 packages << "\\usepackage{jurabib}[2004/01/25]\n";
395 // bibtopic -- the dot provides the aux file naming which
397 if (isRequired("bibtopic")) {
398 packages << "\\usepackage[dot]{bibtopic}\n";
401 if (isRequired("xy"))
402 packages << "\\usepackage[all]{xy}\n";
404 if (isRequired("nomencl")) {
405 packages << "\\usepackage{nomencl}[2005/09/22]\n"
406 << "\\makenomenclature\n";
409 return packages.str();
413 string const LaTeXFeatures::getMacros() const
415 ostringstream macros;
417 if (!preamble_snippets_.empty())
419 FeaturesList::const_iterator pit = preamble_snippets_.begin();
420 FeaturesList::const_iterator pend = preamble_snippets_.end();
421 for (; pit != pend; ++pit) {
422 macros << *pit << '\n';
425 if (isRequired("LyX"))
426 macros << lyx_def << '\n';
428 if (isRequired("lyxline"))
429 macros << lyxline_def << '\n';
431 if (isRequired("noun"))
432 macros << noun_def << '\n';
434 if (isRequired("lyxarrow"))
435 macros << lyxarrow_def << '\n';
438 if (isRequired("quotesinglbase"))
439 macros << quotesinglbase_def << '\n';
440 if (isRequired("quotedblbase"))
441 macros << quotedblbase_def << '\n';
442 if (isRequired("guilsinglleft"))
443 macros << guilsinglleft_def << '\n';
444 if (isRequired("guilsinglright"))
445 macros << guilsinglright_def << '\n';
446 if (isRequired("guillemotleft"))
447 macros << guillemotleft_def << '\n';
448 if (isRequired("guillemotright"))
449 macros << guillemotright_def << '\n';
452 if (isRequired("boldsymbol") && !isRequired("amsmath"))
453 macros << boldsymbol_def << '\n';
454 if (isRequired("binom") && !isRequired("amsmath"))
455 macros << binom_def << '\n';
456 if (isRequired("mathcircumflex"))
457 macros << mathcircumflex_def << '\n';
460 if (isRequired("ParagraphLeftIndent"))
461 macros << paragraphleftindent_def;
462 if (isRequired("NeedLyXFootnoteCode"))
463 macros << floatingfootnote_def;
465 // some problems with tex->html converters
466 if (isRequired("NeedTabularnewline"))
467 macros << tabularnewline_def;
469 // greyedout environment (note inset)
470 if (isRequired("lyxgreyedout"))
471 macros << lyxgreyedout_def;
473 if (isRequired("lyxdot"))
474 macros << lyxdot_def << '\n';
477 getFloatDefinitions(macros);
483 string const LaTeXFeatures::getBabelOptions() const
487 LanguageList::const_iterator it = UsedLanguages_.begin();
488 LanguageList::const_iterator end = UsedLanguages_.end();
489 for (; it != end; ++it)
490 if (!(*it)->latex_options().empty())
491 tmp << (*it)->latex_options() << '\n';
492 if (!params_.language->latex_options().empty())
493 tmp << params_.language->latex_options() << '\n';
499 docstring const LaTeXFeatures::getTClassPreamble() const
501 // the text class specific preamble
502 LyXTextClass const & tclass = params_.getLyXTextClass();
503 odocstringstream tcpreamble;
505 tcpreamble << tclass.preamble();
507 list<string>::const_iterator cit = usedLayouts_.begin();
508 list<string>::const_iterator end = usedLayouts_.end();
509 for (; cit != end; ++cit) {
510 tcpreamble << tclass[*cit]->preamble();
513 CharStyles::iterator cs = tclass.charstyles().begin();
514 CharStyles::iterator csend = tclass.charstyles().end();
515 for (; cs != csend; ++cs) {
516 if (isRequired(cs->name))
517 tcpreamble << cs->preamble;
520 return tcpreamble.str();
524 docstring const LaTeXFeatures::getLyXSGMLEntities() const
526 // Definition of entities used in the document that are LyX related.
527 odocstringstream entities;
529 if (isRequired("lyxarrow")) {
530 entities << "<!ENTITY lyxarrow \"->\">" << '\n';
533 return entities.str();
537 docstring const LaTeXFeatures::getIncludedFiles(string const & fname) const
539 odocstringstream sgmlpreamble;
541 docstring const basename(from_utf8(onlyPath(fname)));
543 FileMap::const_iterator end = IncludedFiles_.end();
544 for (FileMap::const_iterator fi = IncludedFiles_.begin();
547 sgmlpreamble << "\n<!ENTITY " << fi->first
548 << (isSGMLFilename(fi->second) ? " SYSTEM \"" : " \"")
549 << makeRelPath(from_utf8(fi->second), basename) << "\">";
551 return sgmlpreamble.str();
555 void LaTeXFeatures::showStruct() const {
556 lyxerr << "LyX needs the following commands when LaTeXing:"
557 << "\n***** Packages:" << getPackages()
558 << "\n***** Macros:" << getMacros()
559 << "\n***** Textclass stuff:" << to_utf8(getTClassPreamble())
560 << "\n***** done." << endl;
564 Buffer const & LaTeXFeatures::buffer() const
570 void LaTeXFeatures::setBuffer(Buffer const & buffer)
576 BufferParams const & LaTeXFeatures::bufferParams() const
582 void LaTeXFeatures::getFloatDefinitions(ostream & os) const
584 FloatList const & floats = params_.getLyXTextClass().floats();
586 // Here we will output the code to create the needed float styles.
587 // We will try to do this as minimal as possible.
588 // \floatstyle{ruled}
589 // \newfloat{algorithm}{htbp}{loa}
590 // \floatname{algorithm}{Algorithm}
591 UsedFloats::const_iterator cit = usedFloats_.begin();
592 UsedFloats::const_iterator end = usedFloats_.end();
593 // ostringstream floats;
594 for (; cit != end; ++cit) {
595 Floating const & fl = floats.getType((*cit));
597 // For builtin floats we do nothing.
598 if (fl.builtin()) continue;
600 // We have to special case "table" and "figure"
601 if (fl.type() == "tabular" || fl.type() == "figure") {
602 // Output code to modify "table" or "figure"
603 // but only if builtin == false
604 // and that have to be true at this point in the
606 string const type = fl.type();
607 string const placement = fl.placement();
608 string const style = fl.style();
609 if (!style.empty()) {
610 os << "\\floatstyle{" << style << "}\n"
611 << "\\restylefloat{" << type << "}\n";
613 if (!placement.empty()) {
614 os << "\\floatplacement{" << type << "}{"
615 << placement << "}\n";
618 // The other non builtin floats.
620 string const type = fl.type();
621 string const placement = fl.placement();
622 string const ext = fl.ext();
623 string const within = fl.within();
624 string const style = fl.style();
625 string const name = fl.name();
626 os << "\\floatstyle{" << style << "}\n"
627 << "\\newfloat{" << type << "}{" << placement
628 << "}{" << ext << '}';
630 os << '[' << within << ']';
632 << "\\floatname{" << type << "}{"
635 // What missing here is to code to minimalize the code
636 // output so that the same floatstyle will not be
637 // used several times, when the same style is still in