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"
37 using support::isSGMLFilename;
38 using support::libFileSearch;
39 using support::makeRelPath;
40 using support::onlyPath;
47 using std::ostringstream;
50 LaTeXFeatures::PackagesList LaTeXFeatures::packages_;
53 LaTeXFeatures::LaTeXFeatures(Buffer const & b, BufferParams const & p,
54 OutputParams const & r)
55 : buffer_(&b), params_(p), runparams_(r)
59 bool LaTeXFeatures::useBabel() const
61 return lyxrc.language_use_babel ||
62 bufferParams().language->lang() != lyxrc.default_language ||
67 void LaTeXFeatures::require(string const & name)
72 features_.push_back(name);
76 void LaTeXFeatures::getAvailable()
79 support::FileName const real_file = libFileSearch("", "packages.lst");
81 if (real_file.empty())
84 lex.setFile(real_file);
89 // Make sure that we are clean
92 bool finished = false;
94 while (lex.isOK() && !finished) {
96 case LyXLex::LEX_FEOF:
100 string const name = lex.getString();
101 PackagesList::const_iterator begin = packages_.begin();
102 PackagesList::const_iterator end = packages_.end();
103 if (find(begin, end, name) == end)
104 packages_.push_back(name);
112 void LaTeXFeatures::useLayout(string const & layoutname)
114 // Some code to avoid loops in dependency definition
115 static int level = 0;
116 const int maxlevel = 30;
117 if (level > maxlevel) {
118 lyxerr << "LaTeXFeatures::useLayout: maximum level of "
119 << "recursion attained by layout "
120 << layoutname << endl;
124 LyXTextClass const & tclass = params_.getLyXTextClass();
125 if (tclass.hasLayout(layoutname)) {
126 // Is this layout already in usedLayouts?
127 list<string>::const_iterator cit = usedLayouts_.begin();
128 list<string>::const_iterator end = usedLayouts_.end();
129 for (; cit != end; ++cit) {
130 if (layoutname == *cit)
134 LyXLayout_ptr const & lyt = tclass[layoutname];
135 if (!lyt->depends_on().empty()) {
137 useLayout(lyt->depends_on());
140 usedLayouts_.push_back(layoutname);
142 lyxerr << "LaTeXFeatures::useLayout: layout `"
143 << layoutname << "' does not exist in this class"
151 bool LaTeXFeatures::isRequired(string const & name) const
153 return find(features_.begin(), features_.end(), name) != features_.end();
157 bool LaTeXFeatures::isAvailable(string const & name) const
159 if (packages_.empty())
161 return find(packages_.begin(), packages_.end(), name) != packages_.end();
165 void LaTeXFeatures::addExternalPreamble(string const & preamble)
167 FeaturesList::const_iterator begin = preamble_snippets_.begin();
168 FeaturesList::const_iterator end = preamble_snippets_.end();
169 if (find(begin, end, preamble) == end)
170 preamble_snippets_.push_back(preamble);
174 void LaTeXFeatures::useFloat(string const & name)
176 usedFloats_.insert(name);
177 // We only need float.sty if we use non builtin floats, or if we
178 // use the "H" modifier. This includes modified table and
179 // figure floats. (Lgb)
180 Floating const & fl = params_.getLyXTextClass().floats().getType(name);
181 if (!fl.type().empty() && !fl.builtin()) {
187 void LaTeXFeatures::useLanguage(Language const * lang)
189 UsedLanguages_.insert(lang);
193 void LaTeXFeatures::includeFile(docstring const & key, string const & name)
195 IncludedFiles_[key] = name;
199 bool LaTeXFeatures::hasLanguages() const
201 return !UsedLanguages_.empty();
205 string LaTeXFeatures::getLanguages() const
207 ostringstream languages;
209 for (LanguageList::const_iterator cit =
210 UsedLanguages_.begin();
211 cit != UsedLanguages_.end();
213 languages << (*cit)->babel() << ',';
214 return languages.str();
218 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding) const
220 set<string> encodings;
221 LanguageList::const_iterator it = UsedLanguages_.begin();
222 LanguageList::const_iterator end = UsedLanguages_.end();
223 for (; it != end; ++it)
224 // thailatex does not use the inputenc package, but sets up
225 // babel directly for tis620-0 encoding, therefore we must
226 // not add tis620-0 to the encoding set.
227 if ((*it)->encoding()->latexName() != doc_encoding &&
228 (*it)->encoding()->name() != "tis620-0")
229 encodings.insert((*it)->encoding()->latexName());
235 char const * simplefeatures[] = {
257 int const nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
262 string const LaTeXFeatures::getPackages() const
264 ostringstream packages;
265 LyXTextClass const & tclass = params_.getLyXTextClass();
268 // These are all the 'simple' includes. i.e
269 // packages which we just \usepackage{package}
271 for (int i = 0; i < nb_simplefeatures; ++i) {
272 if (isRequired(simplefeatures[i]))
273 packages << "\\usepackage{"
274 << simplefeatures[i] << "}\n";
278 // The rest of these packages are somewhat more complicated
282 if (isRequired("amsmath")
283 && !tclass.provides(LyXTextClass::amsmath)
284 && params_.use_amsmath != BufferParams::package_off) {
285 packages << "\\usepackage{amsmath}\n";
288 // wasysym is a simple feature, but it must be after amsmath if both
290 // wasysym redefines some integrals (e.g. iint) from amsmath. That
291 // leads to inconsistent integrals. We only load this package if
292 // esint is used, since esint redefines all relevant integral
293 // symbols from wasysym and amsmath.
294 // See http://bugzilla.lyx.org/show_bug.cgi?id=1942
295 if (isRequired("wasysym") && isRequired("esint") &&
296 params_.use_esint != BufferParams::package_off)
297 packages << "\\usepackage{wasysym}\n";
300 if (isRequired("color")) {
301 if (params_.graphicsDriver == "default")
302 packages << "\\usepackage{color}\n";
304 packages << "\\usepackage["
305 << params_.graphicsDriver
310 if (isRequired("makeidx")) {
311 if (!tclass.provides(LyXTextClass::makeidx))
312 packages << "\\usepackage{makeidx}\n";
313 packages << "\\makeindex\n";
317 if (isRequired("graphicx") && params_.graphicsDriver != "none") {
318 if (params_.graphicsDriver == "default")
319 packages << "\\usepackage{graphicx}\n";
321 packages << "\\usepackage["
322 << params_.graphicsDriver
325 // shadecolor for shaded
326 if (isRequired("framed")) {
327 RGBColor c = RGBColor(lcolor.getX11Name(LColor::shadedbg));
328 packages << "\\definecolor{shadecolor}{rgb}{"
329 << c.r/255 << ',' << c.g/255 << ',' << c.b/255 << "}\n";
333 // packages << "\\usepackage{algorithm}\n";
336 // lyxskak.sty --- newer chess support based on skak.sty
337 if (isRequired("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 (isRequired("amssymb") || params_.use_amsmath == BufferParams::package_on)
367 packages << "\\usepackage{amssymb}\n";
369 // esint must be after amsmath and wasysym, since it will redeclare
370 // inconsistent integral symbols
371 if (isRequired("esint") && params_.use_esint != BufferParams::package_off)
372 packages << "\\usepackage{esint}\n";
375 if (isRequired("url") && ! tclass.provides(LyXTextClass::url))
376 packages << "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
377 " {\\newcommand{\\url}{\\texttt}}\n";
381 if (isRequired("natbib") && ! tclass.provides(LyXTextClass::natbib)) {
382 packages << "\\usepackage[";
383 if (params_.cite_engine == biblio::ENGINE_NATBIB_NUMERICAL) {
384 packages << "numbers";
386 packages << "authoryear";
388 packages << "]{natbib}\n";
391 // jurabib -- we need version 0.6 at least.
392 if (isRequired("jurabib")) {
393 packages << "\\usepackage{jurabib}[2004/01/25]\n";
396 // bibtopic -- the dot provides the aux file naming which
398 if (isRequired("bibtopic")) {
399 packages << "\\usepackage[dot]{bibtopic}\n";
402 if (isRequired("xy"))
403 packages << "\\usepackage[all]{xy}\n";
405 if (isRequired("nomencl")) {
406 packages << "\\usepackage{nomencl}[2005/09/22]\n"
407 << "\\makenomenclature\n";
410 return packages.str();
414 string const LaTeXFeatures::getMacros() const
416 ostringstream macros;
418 if (!preamble_snippets_.empty())
420 FeaturesList::const_iterator pit = preamble_snippets_.begin();
421 FeaturesList::const_iterator pend = preamble_snippets_.end();
422 for (; pit != pend; ++pit) {
423 macros << *pit << '\n';
426 if (isRequired("LyX"))
427 macros << lyx_def << '\n';
429 if (isRequired("lyxline"))
430 macros << lyxline_def << '\n';
432 if (isRequired("noun"))
433 macros << noun_def << '\n';
435 if (isRequired("lyxarrow"))
436 macros << lyxarrow_def << '\n';
439 if (isRequired("quotesinglbase"))
440 macros << quotesinglbase_def << '\n';
441 if (isRequired("quotedblbase"))
442 macros << quotedblbase_def << '\n';
443 if (isRequired("guilsinglleft"))
444 macros << guilsinglleft_def << '\n';
445 if (isRequired("guilsinglright"))
446 macros << guilsinglright_def << '\n';
447 if (isRequired("guillemotleft"))
448 macros << guillemotleft_def << '\n';
449 if (isRequired("guillemotright"))
450 macros << guillemotright_def << '\n';
453 if (isRequired("boldsymbol") && !isRequired("amsmath"))
454 macros << boldsymbol_def << '\n';
455 if (isRequired("binom") && !isRequired("amsmath"))
456 macros << binom_def << '\n';
457 if (isRequired("mathcircumflex"))
458 macros << mathcircumflex_def << '\n';
461 if (isRequired("ParagraphLeftIndent"))
462 macros << paragraphleftindent_def;
463 if (isRequired("NeedLyXFootnoteCode"))
464 macros << floatingfootnote_def;
466 // some problems with tex->html converters
467 if (isRequired("NeedTabularnewline"))
468 macros << tabularnewline_def;
470 // greyedout environment (note inset)
471 if (isRequired("lyxgreyedout"))
472 macros << lyxgreyedout_def;
474 if (isRequired("lyxdot"))
475 macros << lyxdot_def << '\n';
478 getFloatDefinitions(macros);
484 string const LaTeXFeatures::getBabelOptions() const
488 LanguageList::const_iterator it = UsedLanguages_.begin();
489 LanguageList::const_iterator end = UsedLanguages_.end();
490 for (; it != end; ++it)
491 if (!(*it)->latex_options().empty())
492 tmp << (*it)->latex_options() << '\n';
493 if (!params_.language->latex_options().empty())
494 tmp << params_.language->latex_options() << '\n';
500 docstring const LaTeXFeatures::getTClassPreamble() const
502 // the text class specific preamble
503 LyXTextClass const & tclass = params_.getLyXTextClass();
504 odocstringstream tcpreamble;
506 tcpreamble << tclass.preamble();
508 list<string>::const_iterator cit = usedLayouts_.begin();
509 list<string>::const_iterator end = usedLayouts_.end();
510 for (; cit != end; ++cit) {
511 tcpreamble << tclass[*cit]->preamble();
514 CharStyles::iterator cs = tclass.charstyles().begin();
515 CharStyles::iterator csend = tclass.charstyles().end();
516 for (; cs != csend; ++cs) {
517 if (isRequired(cs->name))
518 tcpreamble << cs->preamble;
521 return tcpreamble.str();
525 docstring const LaTeXFeatures::getLyXSGMLEntities() const
527 // Definition of entities used in the document that are LyX related.
528 odocstringstream entities;
530 if (isRequired("lyxarrow")) {
531 entities << "<!ENTITY lyxarrow \"->\">" << '\n';
534 return entities.str();
538 docstring const LaTeXFeatures::getIncludedFiles(string const & fname) const
540 odocstringstream sgmlpreamble;
541 string const basename = onlyPath(fname);
543 FileMap::const_iterator end = IncludedFiles_.end();
544 for (FileMap::const_iterator fi = IncludedFiles_.begin();
546 sgmlpreamble << "\n<!ENTITY " << fi->first
547 << (isSGMLFilename(fi->second) ? " SYSTEM \"" : " \"")
548 << from_ascii(makeRelPath(fi->second, basename)) << "\">";
550 return sgmlpreamble.str();
554 void LaTeXFeatures::showStruct() const {
555 lyxerr << "LyX needs the following commands when LaTeXing:"
556 << "\n***** Packages:" << getPackages()
557 << "\n***** Macros:" << getMacros()
558 << "\n***** Textclass stuff:" << to_utf8(getTClassPreamble())
559 << "\n***** done." << endl;
563 Buffer const & LaTeXFeatures::buffer() const
569 void LaTeXFeatures::setBuffer(Buffer const & buffer)
575 BufferParams const & LaTeXFeatures::bufferParams() const
581 void LaTeXFeatures::getFloatDefinitions(ostream & os) const
583 FloatList const & floats = params_.getLyXTextClass().floats();
585 // Here we will output the code to create the needed float styles.
586 // We will try to do this as minimal as possible.
587 // \floatstyle{ruled}
588 // \newfloat{algorithm}{htbp}{loa}
589 // \floatname{algorithm}{Algorithm}
590 UsedFloats::const_iterator cit = usedFloats_.begin();
591 UsedFloats::const_iterator end = usedFloats_.end();
592 // ostringstream floats;
593 for (; cit != end; ++cit) {
594 Floating const & fl = floats.getType((*cit));
596 // For builtin floats we do nothing.
597 if (fl.builtin()) continue;
599 // We have to special case "table" and "figure"
600 if (fl.type() == "tabular" || fl.type() == "figure") {
601 // Output code to modify "table" or "figure"
602 // but only if builtin == false
603 // and that have to be true at this point in the
605 string const type = fl.type();
606 string const placement = fl.placement();
607 string const style = fl.style();
608 if (!style.empty()) {
609 os << "\\floatstyle{" << style << "}\n"
610 << "\\restylefloat{" << type << "}\n";
612 if (!placement.empty()) {
613 os << "\\floatplacement{" << type << "}{"
614 << placement << "}\n";
617 // The other non builtin floats.
619 string const type = fl.type();
620 string const placement = fl.placement();
621 string const ext = fl.ext();
622 string const within = fl.within();
623 string const style = fl.style();
624 string const name = fl.name();
625 os << "\\floatstyle{" << style << "}\n"
626 << "\\newfloat{" << type << "}{" << placement
627 << "}{" << ext << '}';
629 os << '[' << within << ']';
631 << "\\floatname{" << type << "}{"
634 // What missing here is to code to minimalize the code
635 // output so that the same floatstyle will not be
636 // used several times, when the same style is still in