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"
28 #include "support/filetools.h"
32 using lyx::support::IsSGMLFilename;
33 using lyx::support::MakeRelPath;
34 using lyx::support::OnlyPath;
41 using std::ostringstream;
44 namespace biblio = lyx::biblio;
47 LaTeXFeatures::LaTeXFeatures(Buffer const & b, BufferParams const & p, bool n)
48 : buffer_(&b), params_(p), nice_(n)
52 bool LaTeXFeatures::useBabel() const
54 return lyxrc.language_use_babel ||
55 bufferParams().language->lang() != lyxrc.default_language ||
60 void LaTeXFeatures::require(string const & name)
65 features_.push_back(name);
69 void LaTeXFeatures::useLayout(string const & layoutname)
71 // Some code to avoid loops in dependency definition
73 const int maxlevel = 30;
74 if (level > maxlevel) {
75 lyxerr << "LaTeXFeatures::useLayout: maximum level of "
76 << "recursion attained by layout "
77 << layoutname << endl;
81 LyXTextClass const & tclass = params_.getLyXTextClass();
82 if (tclass.hasLayout(layoutname)) {
83 // Is this layout already in usedLayouts?
84 list<string>::const_iterator cit = usedLayouts_.begin();
85 list<string>::const_iterator end = usedLayouts_.end();
86 for (; cit != end; ++cit) {
87 if (layoutname == *cit)
91 LyXLayout_ptr const & lyt = tclass[layoutname];
92 if (!lyt->depends_on().empty()) {
94 useLayout(lyt->depends_on());
97 usedLayouts_.push_back(layoutname);
99 lyxerr << "LaTeXFeatures::useLayout: layout `"
100 << layoutname << "' does not exist in this class"
108 bool LaTeXFeatures::isRequired(string const & name) const
110 return find(features_.begin(), features_.end(), name) != features_.end();
114 void LaTeXFeatures::addExternalPreamble(string const & preamble)
116 FeaturesList::const_iterator begin = preamble_snippets_.begin();
117 FeaturesList::const_iterator end = preamble_snippets_.end();
118 if (find(begin, end, preamble) == end)
119 preamble_snippets_.push_back(preamble);
123 void LaTeXFeatures::useFloat(string const & name)
125 usedFloats_.insert(name);
126 // We only need float.sty if we use non builtin floats, or if we
127 // use the "H" modifier. This includes modified table and
128 // figure floats. (Lgb)
129 Floating const & fl = params_.getLyXTextClass().floats().getType(name);
130 if (!fl.type().empty() && !fl.builtin()) {
136 void LaTeXFeatures::useLanguage(Language const * lang)
138 UsedLanguages_.insert(lang);
142 void LaTeXFeatures::includeFile(string const & key, string const & name)
144 IncludedFiles_[key] = name;
148 bool LaTeXFeatures::hasLanguages() const
150 return !UsedLanguages_.empty();
154 string LaTeXFeatures::getLanguages() const
156 ostringstream languages;
158 for (LanguageList::const_iterator cit =
159 UsedLanguages_.begin();
160 cit != UsedLanguages_.end();
162 languages << (*cit)->babel() << ',';
163 return languages.str();
167 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding) const
169 set<string> encodings;
170 LanguageList::const_iterator it = UsedLanguages_.begin();
171 LanguageList::const_iterator end = UsedLanguages_.end();
172 for (; it != end; ++it)
173 if ((*it)->encoding()->LatexName() != doc_encoding)
174 encodings.insert((*it)->encoding()->LatexName());
180 char const * simplefeatures[] = {
198 int const nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
203 string const LaTeXFeatures::getPackages() const
205 ostringstream packages;
206 LyXTextClass const & tclass = params_.getLyXTextClass();
209 // These are all the 'simple' includes. i.e
210 // packages which we just \usepackage{package}
212 for (int i = 0; i < nb_simplefeatures; ++i) {
213 if (isRequired(simplefeatures[i]))
214 packages << "\\usepackage{"
215 << simplefeatures[i] << "}\n";
219 // The rest of these packages are somewhat more complicated
223 if (isRequired("amsmath")
224 && !tclass.provides(LyXTextClass::amsmath)
225 && params_.use_amsmath != BufferParams::AMS_OFF) {
226 packages << "\\usepackage{amsmath}\n";
230 if (isRequired("color")) {
231 if (params_.graphicsDriver == "default")
232 packages << "\\usepackage[usenames]{color}\n";
234 packages << "\\usepackage["
235 << params_.graphicsDriver
241 if (isRequired("makeidx")) {
242 if (! tclass.provides(LyXTextClass::makeidx))
243 packages << "\\usepackage{makeidx}\n";
244 packages << "\\makeindex\n";
248 if (isRequired("graphicx") && params_.graphicsDriver != "none") {
249 if (params_.graphicsDriver == "default")
250 packages << "\\usepackage{graphicx}\n";
252 packages << "\\usepackage["
253 << params_.graphicsDriver
258 // packages << "\\usepackage{algorithm}\n";
261 // lyxskak.sty --- newer chess support based on skak.sty
262 if (isRequired("chess")) {
263 packages << "\\usepackage[ps,mover]{lyxskak}\n";
267 if ((params_.spacing().getSpace() != Spacing::Single
268 && !params_.spacing().isDefault())
269 || isRequired("setspace")) {
270 packages << "\\usepackage{setspace}\n";
272 switch (params_.spacing().getSpace()) {
273 case Spacing::Default:
274 case Spacing::Single:
275 // we dont use setspace.sty so dont print anything
276 //packages += "\\singlespacing\n";
278 case Spacing::Onehalf:
279 packages << "\\onehalfspacing\n";
281 case Spacing::Double:
282 packages << "\\doublespacing\n";
285 packages << "\\setstretch{"
286 << params_.spacing().getValue() << "}\n";
291 if (isRequired("amssymb") || params_.use_amsmath == BufferParams::AMS_ON)
292 packages << "\\usepackage{amssymb}\n";
294 if (isRequired("url") && ! tclass.provides(LyXTextClass::url))
295 packages << "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
296 " {\\newcommand{\\url}{\\texttt}}\n";
300 if (isRequired("natbib") && ! tclass.provides(LyXTextClass::natbib)) {
301 packages << "\\usepackage[";
302 if (params_.cite_engine == biblio::ENGINE_NATBIB_NUMERICAL) {
303 packages << "numbers";
305 packages << "authoryear";
307 packages << "]{natbib}\n";
310 // jurabib -- we need version 0.6 at least.
311 if (isRequired("jurabib")) {
312 packages << "\\usepackage{jurabib}[2004/01/25]\n";
315 // bibtopic -- the dot provides the aux file naming which
317 if (isRequired("bibtopic")) {
318 packages << "\\usepackage[dot]{bibtopic}\n";
321 return packages.str();
325 string const LaTeXFeatures::getMacros() const
327 ostringstream macros;
329 if (!preamble_snippets_.empty())
331 FeaturesList::const_iterator pit = preamble_snippets_.begin();
332 FeaturesList::const_iterator pend = preamble_snippets_.end();
333 for (; pit != pend; ++pit) {
334 macros << *pit << '\n';
337 if (isRequired("LyX"))
338 macros << lyx_def << '\n';
340 if (isRequired("lyxline"))
341 macros << lyxline_def << '\n';
343 if (isRequired("noun"))
344 macros << noun_def << '\n';
346 if (isRequired("lyxarrow"))
347 macros << lyxarrow_def << '\n';
350 if (isRequired("quotesinglbase"))
351 macros << quotesinglbase_def << '\n';
352 if (isRequired("quotedblbase"))
353 macros << quotedblbase_def << '\n';
354 if (isRequired("guilsinglleft"))
355 macros << guilsinglleft_def << '\n';
356 if (isRequired("guilsinglright"))
357 macros << guilsinglright_def << '\n';
358 if (isRequired("guillemotleft"))
359 macros << guillemotleft_def << '\n';
360 if (isRequired("guillemotright"))
361 macros << guillemotright_def << '\n';
364 if (isRequired("boldsymbol") && !isRequired("amsmath"))
365 macros << boldsymbol_def << '\n';
366 if (isRequired("binom") && !isRequired("amsmath"))
367 macros << binom_def << '\n';
368 if (isRequired("mathcircumflex"))
369 macros << mathcircumflex_def << '\n';
372 if (isRequired("ParagraphLeftIndent"))
373 macros << paragraphleftindent_def;
374 if (isRequired("NeedLyXFootnoteCode"))
375 macros << floatingfootnote_def;
377 // some problems with tex->html converters
378 if (isRequired("NeedTabularnewline"))
379 macros << tabularnewline_def;
381 // greyedout environment (note inset)
382 if (isRequired("lyxgreyedout"))
383 macros << lyxgreyedout_def;
385 if (isRequired("lyxdot"))
386 macros << lyxdot_def << '\n';
389 getFloatDefinitions(macros);
395 string const LaTeXFeatures::getBabelOptions() const
399 LanguageList::const_iterator it = UsedLanguages_.begin();
400 LanguageList::const_iterator end = UsedLanguages_.end();
401 for (; it != end; ++it)
402 if (!(*it)->latex_options().empty())
403 tmp << (*it)->latex_options() << '\n';
404 if (!params_.language->latex_options().empty())
405 tmp << params_.language->latex_options() << '\n';
411 string const LaTeXFeatures::getTClassPreamble() const
413 // the text class specific preamble
414 LyXTextClass const & tclass = params_.getLyXTextClass();
415 ostringstream tcpreamble;
417 tcpreamble << tclass.preamble();
419 list<string>::const_iterator cit = usedLayouts_.begin();
420 list<string>::const_iterator end = usedLayouts_.end();
421 for (; cit != end; ++cit) {
422 tcpreamble << tclass[*cit]->preamble();
425 CharStyles::iterator cs = tclass.charstyles().begin();
426 CharStyles::iterator csend = tclass.charstyles().end();
427 for (; cs != csend; ++cs) {
428 if (isRequired(cs->name))
429 tcpreamble << cs->preamble;
432 return tcpreamble.str();
436 string const LaTeXFeatures::getLyXSGMLEntities() const
438 // Definition of entities used in the document that are LyX related.
439 ostringstream entities;
441 if (isRequired("lyxarrow")) {
442 entities << "<!ENTITY lyxarrow \"->\">" << '\n';
445 return entities.str();
449 string const LaTeXFeatures::getIncludedFiles(string const & fname) const
451 ostringstream sgmlpreamble;
452 string const basename = OnlyPath(fname);
454 FileMap::const_iterator end = IncludedFiles_.end();
455 for (FileMap::const_iterator fi = IncludedFiles_.begin();
457 sgmlpreamble << "\n<!ENTITY " << fi->first
458 << (IsSGMLFilename(fi->second) ? " SYSTEM \"" : " \"")
459 << MakeRelPath(fi->second, basename) << "\">";
461 return sgmlpreamble.str();
465 void LaTeXFeatures::showStruct() const {
466 lyxerr << "LyX needs the following commands when LaTeXing:"
467 << "\n***** Packages:" << getPackages()
468 << "\n***** Macros:" << getMacros()
469 << "\n***** Textclass stuff:" << getTClassPreamble()
470 << "\n***** done." << endl;
474 Buffer const & LaTeXFeatures::buffer() const
480 void LaTeXFeatures::setBuffer(Buffer const & buffer)
486 BufferParams const & LaTeXFeatures::bufferParams() const
492 void LaTeXFeatures::getFloatDefinitions(ostream & os) const
494 FloatList const & floats = params_.getLyXTextClass().floats();
496 // Here we will output the code to create the needed float styles.
497 // We will try to do this as minimal as possible.
498 // \floatstyle{ruled}
499 // \newfloat{algorithm}{htbp}{loa}
500 // \floatname{algorithm}{Algorithm}
501 UsedFloats::const_iterator cit = usedFloats_.begin();
502 UsedFloats::const_iterator end = usedFloats_.end();
503 // ostringstream floats;
504 for (; cit != end; ++cit) {
505 Floating const & fl = floats.getType((*cit));
507 // For builtin floats we do nothing.
508 if (fl.builtin()) continue;
510 // We have to special case "table" and "figure"
511 if (fl.type() == "tabular" || fl.type() == "figure") {
512 // Output code to modify "table" or "figure"
513 // but only if builtin == false
514 // and that have to be true at this point in the
516 string const type = fl.type();
517 string const placement = fl.placement();
518 string const style = fl.style();
519 if (!style.empty()) {
520 os << "\\floatstyle{" << style << "}\n"
521 << "\\restylefloat{" << type << "}\n";
523 if (!placement.empty()) {
524 os << "\\floatplacement{" << type << "}{"
525 << placement << "}\n";
528 // The other non builtin floats.
530 string const type = fl.type();
531 string const placement = fl.placement();
532 string const ext = fl.ext();
533 string const within = fl.within();
534 string const style = fl.style();
535 string const name = fl.name();
536 os << "\\floatstyle{" << style << "}\n"
537 << "\\newfloat{" << type << "}{" << placement
538 << "}{" << ext << '}';
540 os << '[' << within << ']';
542 << "\\floatname{" << type << "}{"
545 // What missing here is to code to minimalize the code
546 // output so that the same floatstyle will not be
547 // used several times, when the same style is still in