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::mustProvide(string const & name) const
158 return isRequired(name) && !params_.getLyXTextClass().provides(name);
162 bool LaTeXFeatures::isAvailable(string const & name)
164 if (packages_.empty())
166 return find(packages_.begin(), packages_.end(), name) != packages_.end();
170 void LaTeXFeatures::addPreambleSnippet(string const & preamble)
172 FeaturesList::const_iterator begin = preamble_snippets_.begin();
173 FeaturesList::const_iterator end = preamble_snippets_.end();
174 if (find(begin, end, preamble) == end)
175 preamble_snippets_.push_back(preamble);
179 void LaTeXFeatures::useFloat(string const & name)
181 usedFloats_.insert(name);
182 // We only need float.sty if we use non builtin floats, or if we
183 // use the "H" modifier. This includes modified table and
184 // figure floats. (Lgb)
185 Floating const & fl = params_.getLyXTextClass().floats().getType(name);
186 if (!fl.type().empty() && !fl.builtin()) {
192 void LaTeXFeatures::useLanguage(Language const * lang)
194 UsedLanguages_.insert(lang);
198 void LaTeXFeatures::includeFile(docstring const & key, string const & name)
200 IncludedFiles_[key] = name;
204 bool LaTeXFeatures::hasLanguages() const
206 return !UsedLanguages_.empty();
210 string LaTeXFeatures::getLanguages() const
212 ostringstream languages;
214 for (LanguageList::const_iterator cit =
215 UsedLanguages_.begin();
216 cit != UsedLanguages_.end();
218 languages << (*cit)->babel() << ',';
219 return languages.str();
223 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding) const
225 set<string> encodings;
226 LanguageList::const_iterator it = UsedLanguages_.begin();
227 LanguageList::const_iterator end = UsedLanguages_.end();
228 for (; it != end; ++it)
229 // thailatex does not use the inputenc package, but sets up
230 // babel directly for tis620-0 encoding, therefore we must
231 // not add tis620-0 to the encoding set.
232 if ((*it)->encoding()->latexName() != doc_encoding &&
233 (*it)->encoding()->name() != "tis620-0")
234 encodings.insert((*it)->encoding()->latexName());
240 char const * simplefeatures[] = {
262 int const nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
267 string const LaTeXFeatures::getPackages() const
269 ostringstream packages;
270 LyXTextClass const & tclass = params_.getLyXTextClass();
273 // These are all the 'simple' includes. i.e
274 // packages which we just \usepackage{package}
276 for (int i = 0; i < nb_simplefeatures; ++i) {
277 if (mustProvide(simplefeatures[i]))
278 packages << "\\usepackage{"
279 << simplefeatures[i] << "}\n";
283 // The rest of these packages are somewhat more complicated
287 if (mustProvide("amsmath")
288 && params_.use_amsmath != BufferParams::package_off) {
289 packages << "\\usepackage{amsmath}\n";
292 // wasysym is a simple feature, but it must be after amsmath if both
294 // wasysym redefines some integrals (e.g. iint) from amsmath. That
295 // leads to inconsistent integrals. We only load this package if
296 // esint is used, since esint redefines all relevant integral
297 // symbols from wasysym and amsmath.
298 // See http://bugzilla.lyx.org/show_bug.cgi?id=1942
299 if (mustProvide("wasysym") && isRequired("esint") &&
300 params_.use_esint != BufferParams::package_off)
301 packages << "\\usepackage{wasysym}\n";
304 if (mustProvide("color")) {
305 if (params_.graphicsDriver == "default")
306 packages << "\\usepackage{color}\n";
308 packages << "\\usepackage["
309 << params_.graphicsDriver
314 if (isRequired("makeidx")) {
315 if (!tclass.provides("makeidx"))
316 packages << "\\usepackage{makeidx}\n";
317 packages << "\\makeindex\n";
321 if (mustProvide("graphicx") && params_.graphicsDriver != "none") {
322 if (params_.graphicsDriver == "default")
323 packages << "\\usepackage{graphicx}\n";
325 packages << "\\usepackage["
326 << params_.graphicsDriver
329 // shadecolor for shaded
330 if (mustProvide("framed")) {
331 RGBColor c = RGBColor(lcolor.getX11Name(LColor::shadedbg));
332 packages << "\\definecolor{shadecolor}{rgb}{"
333 << c.r/255 << ',' << c.g/255 << ',' << c.b/255 << "}\n";
336 // lyxskak.sty --- newer chess support based on skak.sty
337 if (mustProvide("chess")) {
338 packages << "\\usepackage[ps,mover]{lyxskak}\n";
342 if ((params_.spacing().getSpace() != Spacing::Single
343 && !params_.spacing().isDefault())
344 || isRequired("setspace")) {
345 packages << "\\usepackage{setspace}\n";
347 switch (params_.spacing().getSpace()) {
348 case Spacing::Default:
349 case Spacing::Single:
350 // we dont use setspace.sty so dont print anything
351 //packages += "\\singlespacing\n";
353 case Spacing::Onehalf:
354 packages << "\\onehalfspacing\n";
356 case Spacing::Double:
357 packages << "\\doublespacing\n";
360 packages << "\\setstretch{"
361 << params_.spacing().getValue() << "}\n";
366 if (mustProvide("amssymb")
367 || params_.use_amsmath == BufferParams::package_on)
368 packages << "\\usepackage{amssymb}\n";
370 // esint must be after amsmath and wasysym, since it will redeclare
371 // inconsistent integral symbols
372 if (mustProvide("esint")
373 && params_.use_esint != BufferParams::package_off)
374 packages << "\\usepackage{esint}\n";
377 if (mustProvide("url"))
378 packages << "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
379 " {\\newcommand{\\url}{\\texttt}}\n";
382 if (mustProvide("natbib")) {
383 packages << "\\usepackage[";
384 if (params_.getEngine() == biblio::ENGINE_NATBIB_NUMERICAL) {
385 packages << "numbers";
387 packages << "authoryear";
389 packages << "]{natbib}\n";
392 // jurabib -- we need version 0.6 at least.
393 if (mustProvide("jurabib")) {
394 packages << "\\usepackage{jurabib}[2004/01/25]\n";
397 // bibtopic -- the dot provides the aux file naming which
399 if (mustProvide("bibtopic")) {
400 packages << "\\usepackage[dot]{bibtopic}\n";
403 if (mustProvide("xy"))
404 packages << "\\usepackage[all]{xy}\n";
406 if (mustProvide("nomencl")) {
407 // Make it work with the new and old version of the package,
408 // but don't use the compatibility option since it is
409 // incompatible to other packages.
410 packages << "\\usepackage{nomencl}\n"
411 "% the following is useful when we have the old nomencl.sty package\n"
412 "\\providecommand{\\printnomenclature}{\\printglossary}\n"
413 "\\providecommand{\\makenomenclature}{\\makeglossary}\n"
414 "\\makenomenclature\n";
417 return packages.str();
421 string const LaTeXFeatures::getMacros() const
423 ostringstream macros;
425 if (!preamble_snippets_.empty())
427 FeaturesList::const_iterator pit = preamble_snippets_.begin();
428 FeaturesList::const_iterator pend = preamble_snippets_.end();
429 for (; pit != pend; ++pit) {
430 macros << *pit << '\n';
433 if (mustProvide("LyX"))
434 macros << lyx_def << '\n';
436 if (mustProvide("lyxline"))
437 macros << lyxline_def << '\n';
439 if (mustProvide("noun"))
440 macros << noun_def << '\n';
442 if (mustProvide("lyxarrow"))
443 macros << lyxarrow_def << '\n';
446 if (mustProvide("quotesinglbase"))
447 macros << quotesinglbase_def << '\n';
448 if (mustProvide("quotedblbase"))
449 macros << quotedblbase_def << '\n';
450 if (mustProvide("guilsinglleft"))
451 macros << guilsinglleft_def << '\n';
452 if (mustProvide("guilsinglright"))
453 macros << guilsinglright_def << '\n';
454 if (mustProvide("guillemotleft"))
455 macros << guillemotleft_def << '\n';
456 if (mustProvide("guillemotright"))
457 macros << guillemotright_def << '\n';
460 if (mustProvide("boldsymbol") && !isRequired("amsmath"))
461 macros << boldsymbol_def << '\n';
462 if (mustProvide("binom") && !isRequired("amsmath"))
463 macros << binom_def << '\n';
464 if (mustProvide("mathcircumflex"))
465 macros << mathcircumflex_def << '\n';
468 if (mustProvide("ParagraphLeftIndent"))
469 macros << paragraphleftindent_def;
470 if (mustProvide("NeedLyXFootnoteCode"))
471 macros << floatingfootnote_def;
473 // some problems with tex->html converters
474 if (mustProvide("NeedTabularnewline"))
475 macros << tabularnewline_def;
477 // greyedout environment (note inset)
478 if (mustProvide("lyxgreyedout"))
479 macros << lyxgreyedout_def;
481 if (mustProvide("lyxdot"))
482 macros << lyxdot_def << '\n';
485 getFloatDefinitions(macros);
491 string const LaTeXFeatures::getBabelOptions() const
495 LanguageList::const_iterator it = UsedLanguages_.begin();
496 LanguageList::const_iterator end = UsedLanguages_.end();
497 for (; it != end; ++it)
498 if (!(*it)->latex_options().empty())
499 tmp << (*it)->latex_options() << '\n';
500 if (!params_.language->latex_options().empty())
501 tmp << params_.language->latex_options() << '\n';
507 docstring const LaTeXFeatures::getTClassPreamble() const
509 // the text class specific preamble
510 LyXTextClass const & tclass = params_.getLyXTextClass();
511 odocstringstream tcpreamble;
513 tcpreamble << tclass.preamble();
515 list<string>::const_iterator cit = usedLayouts_.begin();
516 list<string>::const_iterator end = usedLayouts_.end();
517 for (; cit != end; ++cit) {
518 tcpreamble << tclass[*cit]->preamble();
521 CharStyles::iterator cs = tclass.charstyles().begin();
522 CharStyles::iterator csend = tclass.charstyles().end();
523 for (; cs != csend; ++cs) {
524 if (isRequired(cs->name))
525 tcpreamble << cs->preamble;
528 return tcpreamble.str();
532 docstring const LaTeXFeatures::getLyXSGMLEntities() const
534 // Definition of entities used in the document that are LyX related.
535 odocstringstream entities;
537 if (mustProvide("lyxarrow")) {
538 entities << "<!ENTITY lyxarrow \"->\">" << '\n';
541 return entities.str();
545 docstring const LaTeXFeatures::getIncludedFiles(string const & fname) const
547 odocstringstream sgmlpreamble;
549 docstring const basename(from_utf8(onlyPath(fname)));
551 FileMap::const_iterator end = IncludedFiles_.end();
552 for (FileMap::const_iterator fi = IncludedFiles_.begin();
555 sgmlpreamble << "\n<!ENTITY " << fi->first
556 << (isSGMLFilename(fi->second) ? " SYSTEM \"" : " \"")
557 << makeRelPath(from_utf8(fi->second), basename) << "\">";
559 return sgmlpreamble.str();
563 void LaTeXFeatures::showStruct() const {
564 lyxerr << "LyX needs the following commands when LaTeXing:"
565 << "\n***** Packages:" << getPackages()
566 << "\n***** Macros:" << getMacros()
567 << "\n***** Textclass stuff:" << to_utf8(getTClassPreamble())
568 << "\n***** done." << endl;
572 Buffer const & LaTeXFeatures::buffer() const
578 void LaTeXFeatures::setBuffer(Buffer const & buffer)
584 BufferParams const & LaTeXFeatures::bufferParams() const
590 void LaTeXFeatures::getFloatDefinitions(ostream & os) const
592 FloatList const & floats = params_.getLyXTextClass().floats();
594 // Here we will output the code to create the needed float styles.
595 // We will try to do this as minimal as possible.
596 // \floatstyle{ruled}
597 // \newfloat{algorithm}{htbp}{loa}
598 // \floatname{algorithm}{Algorithm}
599 UsedFloats::const_iterator cit = usedFloats_.begin();
600 UsedFloats::const_iterator end = usedFloats_.end();
601 // ostringstream floats;
602 for (; cit != end; ++cit) {
603 Floating const & fl = floats.getType((*cit));
605 // For builtin floats we do nothing.
606 if (fl.builtin()) continue;
608 // We have to special case "table" and "figure"
609 if (fl.type() == "tabular" || fl.type() == "figure") {
610 // Output code to modify "table" or "figure"
611 // but only if builtin == false
612 // and that have to be true at this point in the
614 string const type = fl.type();
615 string const placement = fl.placement();
616 string const style = fl.style();
617 if (!style.empty()) {
618 os << "\\floatstyle{" << style << "}\n"
619 << "\\restylefloat{" << type << "}\n";
621 if (!placement.empty()) {
622 os << "\\floatplacement{" << type << "}{"
623 << placement << "}\n";
626 // The other non builtin floats.
628 string const type = fl.type();
629 string const placement = fl.placement();
630 string const ext = fl.ext();
631 string const within = fl.within();
632 string const style = fl.style();
633 string const name = fl.name();
634 os << "\\floatstyle{" << style << "}\n"
635 << "\\newfloat{" << type << "}{" << placement
636 << "}{" << ext << '}';
638 os << '[' << within << ']';
640 << "\\floatname{" << type << "}{"
643 // What missing here is to code to minimalize the code
644 // output so that the same floatstyle will not be
645 // used several times, when the same style is still in