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[] = {
247 int const nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
252 string const LaTeXFeatures::getPackages() const
254 ostringstream packages;
255 LyXTextClass const & tclass = params_.getLyXTextClass();
258 // These are all the 'simple' includes. i.e
259 // packages which we just \usepackage{package}
261 for (int i = 0; i < nb_simplefeatures; ++i) {
262 if (isRequired(simplefeatures[i]))
263 packages << "\\usepackage{"
264 << simplefeatures[i] << "}\n";
268 // The rest of these packages are somewhat more complicated
272 if (isRequired("amsmath")
273 && !tclass.provides(LyXTextClass::amsmath)
274 && params_.use_amsmath != BufferParams::AMS_OFF) {
275 packages << "\\usepackage{amsmath}\n";
278 // wasysym is a simple feature, but it must be after amsmath if both
280 if (isRequired("wasysym"))
281 packages << "\\usepackage{wasysym}\n";
284 if (isRequired("color")) {
285 if (params_.graphicsDriver == "default")
286 packages << "\\usepackage{color}\n";
288 packages << "\\usepackage["
289 << params_.graphicsDriver
294 if (isRequired("makeidx")) {
295 if (! tclass.provides(LyXTextClass::makeidx))
296 packages << "\\usepackage{makeidx}\n";
297 packages << "\\makeindex\n";
301 if (isRequired("graphicx") && params_.graphicsDriver != "none") {
302 if (params_.graphicsDriver == "default")
303 packages << "\\usepackage{graphicx}\n";
305 packages << "\\usepackage["
306 << params_.graphicsDriver
311 // packages << "\\usepackage{algorithm}\n";
314 // lyxskak.sty --- newer chess support based on skak.sty
315 if (isRequired("chess")) {
316 packages << "\\usepackage[ps,mover]{lyxskak}\n";
320 if ((params_.spacing().getSpace() != Spacing::Single
321 && !params_.spacing().isDefault())
322 || isRequired("setspace")) {
323 packages << "\\usepackage{setspace}\n";
325 switch (params_.spacing().getSpace()) {
326 case Spacing::Default:
327 case Spacing::Single:
328 // we dont use setspace.sty so dont print anything
329 //packages += "\\singlespacing\n";
331 case Spacing::Onehalf:
332 packages << "\\onehalfspacing\n";
334 case Spacing::Double:
335 packages << "\\doublespacing\n";
338 packages << "\\setstretch{"
339 << params_.spacing().getValue() << "}\n";
344 if (isRequired("amssymb") || params_.use_amsmath == BufferParams::AMS_ON)
345 packages << "\\usepackage{amssymb}\n";
347 if (isRequired("url") && ! tclass.provides(LyXTextClass::url))
348 packages << "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
349 " {\\newcommand{\\url}{\\texttt}}\n";
353 if (isRequired("natbib") && ! tclass.provides(LyXTextClass::natbib)) {
354 packages << "\\usepackage[";
355 if (params_.cite_engine == biblio::ENGINE_NATBIB_NUMERICAL) {
356 packages << "numbers";
358 packages << "authoryear";
360 packages << "]{natbib}\n";
363 // jurabib -- we need version 0.6 at least.
364 if (isRequired("jurabib")) {
365 packages << "\\usepackage{jurabib}[2004/01/25]\n";
368 // bibtopic -- the dot provides the aux file naming which
370 if (isRequired("bibtopic")) {
371 packages << "\\usepackage[dot]{bibtopic}\n";
374 return packages.str();
378 string const LaTeXFeatures::getMacros() const
380 ostringstream macros;
382 if (!preamble_snippets_.empty())
384 FeaturesList::const_iterator pit = preamble_snippets_.begin();
385 FeaturesList::const_iterator pend = preamble_snippets_.end();
386 for (; pit != pend; ++pit) {
387 macros << *pit << '\n';
390 if (isRequired("LyX"))
391 macros << lyx_def << '\n';
393 if (isRequired("lyxline"))
394 macros << lyxline_def << '\n';
396 if (isRequired("noun"))
397 macros << noun_def << '\n';
399 if (isRequired("lyxarrow"))
400 macros << lyxarrow_def << '\n';
403 if (isRequired("quotesinglbase"))
404 macros << quotesinglbase_def << '\n';
405 if (isRequired("quotedblbase"))
406 macros << quotedblbase_def << '\n';
407 if (isRequired("guilsinglleft"))
408 macros << guilsinglleft_def << '\n';
409 if (isRequired("guilsinglright"))
410 macros << guilsinglright_def << '\n';
411 if (isRequired("guillemotleft"))
412 macros << guillemotleft_def << '\n';
413 if (isRequired("guillemotright"))
414 macros << guillemotright_def << '\n';
417 if (isRequired("boldsymbol") && !isRequired("amsmath"))
418 macros << boldsymbol_def << '\n';
419 if (isRequired("binom") && !isRequired("amsmath"))
420 macros << binom_def << '\n';
421 if (isRequired("mathcircumflex"))
422 macros << mathcircumflex_def << '\n';
425 if (isRequired("ParagraphLeftIndent"))
426 macros << paragraphleftindent_def;
427 if (isRequired("NeedLyXFootnoteCode"))
428 macros << floatingfootnote_def;
430 // some problems with tex->html converters
431 if (isRequired("NeedTabularnewline"))
432 macros << tabularnewline_def;
434 // greyedout environment (note inset)
435 if (isRequired("lyxgreyedout"))
436 macros << lyxgreyedout_def;
438 if (isRequired("lyxdot"))
439 macros << lyxdot_def << '\n';
442 getFloatDefinitions(macros);
448 string const LaTeXFeatures::getBabelOptions() const
452 LanguageList::const_iterator it = UsedLanguages_.begin();
453 LanguageList::const_iterator end = UsedLanguages_.end();
454 for (; it != end; ++it)
455 if (!(*it)->latex_options().empty())
456 tmp << (*it)->latex_options() << '\n';
457 if (!params_.language->latex_options().empty())
458 tmp << params_.language->latex_options() << '\n';
464 string const LaTeXFeatures::getTClassPreamble() const
466 // the text class specific preamble
467 LyXTextClass const & tclass = params_.getLyXTextClass();
468 ostringstream tcpreamble;
470 tcpreamble << tclass.preamble();
472 list<string>::const_iterator cit = usedLayouts_.begin();
473 list<string>::const_iterator end = usedLayouts_.end();
474 for (; cit != end; ++cit) {
475 tcpreamble << tclass[*cit]->preamble();
478 CharStyles::iterator cs = tclass.charstyles().begin();
479 CharStyles::iterator csend = tclass.charstyles().end();
480 for (; cs != csend; ++cs) {
481 if (isRequired(cs->name))
482 tcpreamble << cs->preamble;
485 return tcpreamble.str();
489 string const LaTeXFeatures::getLyXSGMLEntities() const
491 // Definition of entities used in the document that are LyX related.
492 ostringstream entities;
494 if (isRequired("lyxarrow")) {
495 entities << "<!ENTITY lyxarrow \"->\">" << '\n';
498 return entities.str();
502 string const LaTeXFeatures::getIncludedFiles(string const & fname) const
504 ostringstream sgmlpreamble;
505 string const basename = OnlyPath(fname);
507 FileMap::const_iterator end = IncludedFiles_.end();
508 for (FileMap::const_iterator fi = IncludedFiles_.begin();
510 sgmlpreamble << "\n<!ENTITY " << fi->first
511 << (IsSGMLFilename(fi->second) ? " SYSTEM \"" : " \"")
512 << MakeRelPath(fi->second, basename) << "\">";
514 return sgmlpreamble.str();
518 void LaTeXFeatures::showStruct() const {
519 lyxerr << "LyX needs the following commands when LaTeXing:"
520 << "\n***** Packages:" << getPackages()
521 << "\n***** Macros:" << getMacros()
522 << "\n***** Textclass stuff:" << getTClassPreamble()
523 << "\n***** done." << endl;
527 Buffer const & LaTeXFeatures::buffer() const
533 void LaTeXFeatures::setBuffer(Buffer const & buffer)
539 BufferParams const & LaTeXFeatures::bufferParams() const
545 void LaTeXFeatures::getFloatDefinitions(ostream & os) const
547 FloatList const & floats = params_.getLyXTextClass().floats();
549 // Here we will output the code to create the needed float styles.
550 // We will try to do this as minimal as possible.
551 // \floatstyle{ruled}
552 // \newfloat{algorithm}{htbp}{loa}
553 // \floatname{algorithm}{Algorithm}
554 UsedFloats::const_iterator cit = usedFloats_.begin();
555 UsedFloats::const_iterator end = usedFloats_.end();
556 // ostringstream floats;
557 for (; cit != end; ++cit) {
558 Floating const & fl = floats.getType((*cit));
560 // For builtin floats we do nothing.
561 if (fl.builtin()) continue;
563 // We have to special case "table" and "figure"
564 if (fl.type() == "tabular" || fl.type() == "figure") {
565 // Output code to modify "table" or "figure"
566 // but only if builtin == false
567 // and that have to be true at this point in the
569 string const type = fl.type();
570 string const placement = fl.placement();
571 string const style = fl.style();
572 if (!style.empty()) {
573 os << "\\floatstyle{" << style << "}\n"
574 << "\\restylefloat{" << type << "}\n";
576 if (!placement.empty()) {
577 os << "\\floatplacement{" << type << "}{"
578 << placement << "}\n";
581 // The other non builtin floats.
583 string const type = fl.type();
584 string const placement = fl.placement();
585 string const ext = fl.ext();
586 string const within = fl.within();
587 string const style = fl.style();
588 string const name = fl.name();
589 os << "\\floatstyle{" << style << "}\n"
590 << "\\newfloat{" << type << "}{" << placement
591 << "}{" << ext << '}';
593 os << '[' << within << ']';
595 << "\\floatname{" << type << "}{"
598 // What missing here is to code to minimalize the code
599 // output so that the same floatstyle will not be
600 // used several times, when the same style is still in