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"
29 #include "support/filetools.h"
33 using lyx::support::isSGMLFilename;
34 using lyx::support::libFileSearch;
35 using lyx::support::makeRelPath;
36 using lyx::support::onlyPath;
43 using std::ostringstream;
46 namespace biblio = lyx::biblio;
49 LaTeXFeatures::PackagesList LaTeXFeatures::packages_;
52 LaTeXFeatures::LaTeXFeatures(Buffer const & b, BufferParams const & p,
53 OutputParams const & r)
54 : buffer_(&b), params_(p), runparams_(r)
58 bool LaTeXFeatures::useBabel() const
60 return lyxrc.language_use_babel ||
61 bufferParams().language->lang() != lyxrc.default_language ||
66 void LaTeXFeatures::require(string const & name)
71 features_.push_back(name);
75 void LaTeXFeatures::getAvailable()
78 string real_file = libFileSearch("", "packages.lst");
80 if (real_file.empty())
83 lex.setFile(real_file);
88 // Make sure that we are clean
91 bool finished = false;
93 while (lex.isOK() && !finished) {
95 case LyXLex::LEX_FEOF:
99 string const name = lex.getString();
100 PackagesList::const_iterator begin = packages_.begin();
101 PackagesList::const_iterator end = packages_.end();
102 if (find(begin, end, name) == end)
103 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) const
158 if (packages_.empty())
160 return find(packages_.begin(), packages_.end(), name) != packages_.end();
164 void LaTeXFeatures::addExternalPreamble(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(string 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 if ((*it)->encoding()->latexName() != doc_encoding)
224 encodings.insert((*it)->encoding()->latexName());
230 char const * simplefeatures[] = {
249 int const nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
254 string const LaTeXFeatures::getPackages() const
256 ostringstream packages;
257 LyXTextClass const & tclass = params_.getLyXTextClass();
260 // These are all the 'simple' includes. i.e
261 // packages which we just \usepackage{package}
263 for (int i = 0; i < nb_simplefeatures; ++i) {
264 if (isRequired(simplefeatures[i]))
265 packages << "\\usepackage{"
266 << simplefeatures[i] << "}\n";
270 // The rest of these packages are somewhat more complicated
274 if (isRequired("amsmath")
275 && !tclass.provides(LyXTextClass::amsmath)
276 && params_.use_amsmath != BufferParams::AMS_OFF) {
277 packages << "\\usepackage{amsmath}\n";
280 // wasysym is a simple feature, but it must be after amsmath if both
282 if (isRequired("wasysym"))
283 packages << "\\usepackage{wasysym}\n";
286 if (isRequired("color")) {
287 if (params_.graphicsDriver == "default")
288 packages << "\\usepackage{color}\n";
290 packages << "\\usepackage["
291 << params_.graphicsDriver
296 if (isRequired("makeidx")) {
297 if (! tclass.provides(LyXTextClass::makeidx))
298 packages << "\\usepackage{makeidx}\n";
299 packages << "\\makeindex\n";
303 if (isRequired("graphicx") && params_.graphicsDriver != "none") {
304 if (params_.graphicsDriver == "default")
305 packages << "\\usepackage{graphicx}\n";
307 packages << "\\usepackage["
308 << params_.graphicsDriver
313 // packages << "\\usepackage{algorithm}\n";
316 // lyxskak.sty --- newer chess support based on skak.sty
317 if (isRequired("chess")) {
318 packages << "\\usepackage[ps,mover]{lyxskak}\n";
322 if ((params_.spacing().getSpace() != Spacing::Single
323 && !params_.spacing().isDefault())
324 || isRequired("setspace")) {
325 packages << "\\usepackage{setspace}\n";
327 switch (params_.spacing().getSpace()) {
328 case Spacing::Default:
329 case Spacing::Single:
330 // we dont use setspace.sty so dont print anything
331 //packages += "\\singlespacing\n";
333 case Spacing::Onehalf:
334 packages << "\\onehalfspacing\n";
336 case Spacing::Double:
337 packages << "\\doublespacing\n";
340 packages << "\\setstretch{"
341 << params_.spacing().getValue() << "}\n";
346 if (isRequired("amssymb") || params_.use_amsmath == BufferParams::AMS_ON)
347 packages << "\\usepackage{amssymb}\n";
349 if (isRequired("url") && ! tclass.provides(LyXTextClass::url))
350 packages << "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
351 " {\\newcommand{\\url}{\\texttt}}\n";
355 if (isRequired("natbib") && ! tclass.provides(LyXTextClass::natbib)) {
356 packages << "\\usepackage[";
357 if (params_.cite_engine == biblio::ENGINE_NATBIB_NUMERICAL) {
358 packages << "numbers";
360 packages << "authoryear";
362 packages << "]{natbib}\n";
365 // jurabib -- we need version 0.6 at least.
366 if (isRequired("jurabib")) {
367 packages << "\\usepackage{jurabib}[2004/01/25]\n";
370 // bibtopic -- the dot provides the aux file naming which
372 if (isRequired("bibtopic")) {
373 packages << "\\usepackage[dot]{bibtopic}\n";
376 return packages.str();
380 string const LaTeXFeatures::getMacros() const
382 ostringstream macros;
384 if (!preamble_snippets_.empty())
386 FeaturesList::const_iterator pit = preamble_snippets_.begin();
387 FeaturesList::const_iterator pend = preamble_snippets_.end();
388 for (; pit != pend; ++pit) {
389 macros << *pit << '\n';
392 if (isRequired("LyX"))
393 macros << lyx_def << '\n';
395 if (isRequired("lyxline"))
396 macros << lyxline_def << '\n';
398 if (isRequired("noun"))
399 macros << noun_def << '\n';
401 if (isRequired("lyxarrow"))
402 macros << lyxarrow_def << '\n';
405 if (isRequired("quotesinglbase"))
406 macros << quotesinglbase_def << '\n';
407 if (isRequired("quotedblbase"))
408 macros << quotedblbase_def << '\n';
409 if (isRequired("guilsinglleft"))
410 macros << guilsinglleft_def << '\n';
411 if (isRequired("guilsinglright"))
412 macros << guilsinglright_def << '\n';
413 if (isRequired("guillemotleft"))
414 macros << guillemotleft_def << '\n';
415 if (isRequired("guillemotright"))
416 macros << guillemotright_def << '\n';
419 if (isRequired("boldsymbol") && !isRequired("amsmath"))
420 macros << boldsymbol_def << '\n';
421 if (isRequired("binom") && !isRequired("amsmath"))
422 macros << binom_def << '\n';
423 if (isRequired("mathcircumflex"))
424 macros << mathcircumflex_def << '\n';
427 if (isRequired("ParagraphLeftIndent"))
428 macros << paragraphleftindent_def;
429 if (isRequired("NeedLyXFootnoteCode"))
430 macros << floatingfootnote_def;
432 // some problems with tex->html converters
433 if (isRequired("NeedTabularnewline"))
434 macros << tabularnewline_def;
436 // greyedout environment (note inset)
437 if (isRequired("lyxgreyedout"))
438 macros << lyxgreyedout_def;
440 if (isRequired("lyxdot"))
441 macros << lyxdot_def << '\n';
444 getFloatDefinitions(macros);
450 string const LaTeXFeatures::getBabelOptions() const
454 LanguageList::const_iterator it = UsedLanguages_.begin();
455 LanguageList::const_iterator end = UsedLanguages_.end();
456 for (; it != end; ++it)
457 if (!(*it)->latex_options().empty())
458 tmp << (*it)->latex_options() << '\n';
459 if (!params_.language->latex_options().empty())
460 tmp << params_.language->latex_options() << '\n';
466 string const LaTeXFeatures::getTClassPreamble() const
468 // the text class specific preamble
469 LyXTextClass const & tclass = params_.getLyXTextClass();
470 ostringstream tcpreamble;
472 tcpreamble << tclass.preamble();
474 list<string>::const_iterator cit = usedLayouts_.begin();
475 list<string>::const_iterator end = usedLayouts_.end();
476 for (; cit != end; ++cit) {
477 tcpreamble << tclass[*cit]->preamble();
480 CharStyles::iterator cs = tclass.charstyles().begin();
481 CharStyles::iterator csend = tclass.charstyles().end();
482 for (; cs != csend; ++cs) {
483 if (isRequired(cs->name))
484 tcpreamble << cs->preamble;
487 return tcpreamble.str();
491 string const LaTeXFeatures::getLyXSGMLEntities() const
493 // Definition of entities used in the document that are LyX related.
494 ostringstream entities;
496 if (isRequired("lyxarrow")) {
497 entities << "<!ENTITY lyxarrow \"->\">" << '\n';
500 return entities.str();
504 string const LaTeXFeatures::getIncludedFiles(string const & fname) const
506 ostringstream sgmlpreamble;
507 string const basename = onlyPath(fname);
509 FileMap::const_iterator end = IncludedFiles_.end();
510 for (FileMap::const_iterator fi = IncludedFiles_.begin();
512 sgmlpreamble << "\n<!ENTITY " << fi->first
513 << (isSGMLFilename(fi->second) ? " SYSTEM \"" : " \"")
514 << makeRelPath(fi->second, basename) << "\">";
516 return sgmlpreamble.str();
520 void LaTeXFeatures::showStruct() const {
521 lyxerr << "LyX needs the following commands when LaTeXing:"
522 << "\n***** Packages:" << getPackages()
523 << "\n***** Macros:" << getMacros()
524 << "\n***** Textclass stuff:" << getTClassPreamble()
525 << "\n***** done." << endl;
529 Buffer const & LaTeXFeatures::buffer() const
535 void LaTeXFeatures::setBuffer(Buffer const & buffer)
541 BufferParams const & LaTeXFeatures::bufferParams() const
547 void LaTeXFeatures::getFloatDefinitions(ostream & os) const
549 FloatList const & floats = params_.getLyXTextClass().floats();
551 // Here we will output the code to create the needed float styles.
552 // We will try to do this as minimal as possible.
553 // \floatstyle{ruled}
554 // \newfloat{algorithm}{htbp}{loa}
555 // \floatname{algorithm}{Algorithm}
556 UsedFloats::const_iterator cit = usedFloats_.begin();
557 UsedFloats::const_iterator end = usedFloats_.end();
558 // ostringstream floats;
559 for (; cit != end; ++cit) {
560 Floating const & fl = floats.getType((*cit));
562 // For builtin floats we do nothing.
563 if (fl.builtin()) continue;
565 // We have to special case "table" and "figure"
566 if (fl.type() == "tabular" || fl.type() == "figure") {
567 // Output code to modify "table" or "figure"
568 // but only if builtin == false
569 // and that have to be true at this point in the
571 string const type = fl.type();
572 string const placement = fl.placement();
573 string const style = fl.style();
574 if (!style.empty()) {
575 os << "\\floatstyle{" << style << "}\n"
576 << "\\restylefloat{" << type << "}\n";
578 if (!placement.empty()) {
579 os << "\\floatplacement{" << type << "}{"
580 << placement << "}\n";
583 // The other non builtin floats.
585 string const type = fl.type();
586 string const placement = fl.placement();
587 string const ext = fl.ext();
588 string const within = fl.within();
589 string const style = fl.style();
590 string const name = fl.name();
591 os << "\\floatstyle{" << style << "}\n"
592 << "\\newfloat{" << type << "}{" << placement
593 << "}{" << ext << '}';
595 os << '[' << within << ']';
597 << "\\floatname{" << type << "}{"
600 // What missing here is to code to minimalize the code
601 // output so that the same floatstyle will not be
602 // used several times, when the same style is still in