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"
30 #include "support/std_sstream.h"
32 using lyx::support::IsSGMLFilename;
33 using lyx::support::MakeRelPath;
34 using lyx::support::OnlyPath;
41 using std::ostringstream;
45 LaTeXFeatures::LaTeXFeatures(BufferParams const & p)
50 bool LaTeXFeatures::useBabel() const
52 return lyxrc.language_use_babel ||
53 bufferParams().language->lang() != lyxrc.default_language ||
58 void LaTeXFeatures::require(string const & name)
63 features.push_back(name);
67 void LaTeXFeatures::useLayout(string const & layoutname)
69 // Some code to avoid loops in dependency definition
71 const int maxlevel = 30;
72 if (level > maxlevel) {
73 lyxerr << "LaTeXFeatures::useLayout: maximum level of "
74 << "recursion attained by layout "
75 << layoutname << endl;
79 LyXTextClass const & tclass = params.getLyXTextClass();
80 if (tclass.hasLayout(layoutname)) {
81 // Is this layout already in usedLayouts?
82 list<string>::const_iterator cit = usedLayouts.begin();
83 list<string>::const_iterator end = usedLayouts.end();
84 for (; cit != end; ++cit) {
85 if (layoutname == *cit)
89 LyXLayout_ptr const & lyt = tclass[layoutname];
90 if (!lyt->depends_on().empty()) {
92 useLayout(lyt->depends_on());
95 usedLayouts.push_back(layoutname);
97 lyxerr << "LaTeXFeatures::useLayout: layout `"
98 << layoutname << "' does not exist in this class"
106 bool LaTeXFeatures::isRequired(string const & name) const
108 return find(features.begin(), features.end(), name) != features.end();
112 void LaTeXFeatures::addExternalPreamble(string const & preamble)
114 FeaturesList::const_iterator begin = preamble_snippets.begin();
115 FeaturesList::const_iterator end = preamble_snippets.end();
116 if (find(begin, end, preamble) == end)
117 preamble_snippets.push_back(preamble);
121 void LaTeXFeatures::useFloat(string const & name)
123 usedFloats.insert(name);
124 // We only need float.sty if we use non builtin floats, or if we
125 // use the "H" modifier. This includes modified table and
126 // figure floats. (Lgb)
127 Floating const & fl = params.getLyXTextClass().floats().getType(name);
128 if (!fl.type().empty() && !fl.builtin()) {
134 void LaTeXFeatures::useLanguage(Language const * lang)
136 UsedLanguages.insert(lang);
140 void LaTeXFeatures::includeFile(string const & key, string const & name)
142 IncludedFiles[key] = name;
146 bool LaTeXFeatures::hasLanguages() const
148 return !UsedLanguages.empty();
152 string LaTeXFeatures::getLanguages() const
154 ostringstream languages;
156 for (LanguageList::const_iterator cit =
157 UsedLanguages.begin();
158 cit != UsedLanguages.end();
160 languages << (*cit)->babel() << ',';
162 return languages.str();
166 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding) const
168 set<string> encodings;
169 for (LanguageList::const_iterator it =
170 UsedLanguages.begin();
171 it != UsedLanguages.end(); ++it)
172 if ((*it)->encoding()->LatexName() != doc_encoding)
173 encodings.insert((*it)->encoding()->LatexName());
179 char const * simplefeatures[] = {
195 int const nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
200 string const LaTeXFeatures::getPackages() const
202 ostringstream packages;
203 LyXTextClass const & tclass = params.getLyXTextClass();
206 // These are all the 'simple' includes. i.e
207 // packages which we just \usepackage{package}
209 for (int i = 0; i < nb_simplefeatures; ++i) {
210 if (isRequired(simplefeatures[i]))
211 packages << "\\usepackage{"
212 << simplefeatures[i] << "}\n";
216 // The rest of these packages are somewhat more complicated
220 if (isRequired("amsmath")
221 && !tclass.provides(LyXTextClass::amsmath)
222 && params.use_amsmath != BufferParams::AMS_OFF) {
223 packages << "\\usepackage{amsmath}\n";
227 if (isRequired("color")) {
228 if (params.graphicsDriver == "default")
229 packages << "\\usepackage[usenames]{color}\n";
231 packages << "\\usepackage["
232 << params.graphicsDriver
238 if (isRequired("makeidx")) {
239 if (! tclass.provides(LyXTextClass::makeidx))
240 packages << "\\usepackage{makeidx}\n";
241 packages << "\\makeindex\n";
245 if (isRequired("graphicx") && params.graphicsDriver != "none") {
246 if (params.graphicsDriver == "default")
247 packages << "\\usepackage{graphicx}\n";
249 packages << "\\usepackage["
250 << params.graphicsDriver
255 // packages << "\\usepackage{algorithm}\n";
258 // lyxskak.sty --- newer chess support based on skak.sty
259 if (isRequired("chess")) {
260 packages << "\\usepackage[ps,mover]{lyxskak}\n";
264 if ((params.spacing().getSpace() != Spacing::Single
265 && !params.spacing().isDefault())
266 || isRequired("setspace")) {
267 packages << "\\usepackage{setspace}\n";
269 switch (params.spacing().getSpace()) {
270 case Spacing::Default:
271 case Spacing::Single:
272 // we dont use setspace.sty so dont print anything
273 //packages += "\\singlespacing\n";
275 case Spacing::Onehalf:
276 packages << "\\onehalfspacing\n";
278 case Spacing::Double:
279 packages << "\\doublespacing\n";
282 packages << "\\setstretch{"
283 << params.spacing().getValue() << "}\n";
288 if (isRequired("amssymb") || params.use_amsmath == BufferParams::AMS_ON)
289 packages << "\\usepackage{amssymb}\n";
291 if (isRequired("url") && ! tclass.provides(LyXTextClass::url))
292 packages << "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
293 " {\\newcommand{\\url}{\\texttt}}\n";
297 if (isRequired("natbib") && ! tclass.provides(LyXTextClass::natbib)) {
298 packages << "\\usepackage[";
299 if (params.use_numerical_citations) {
300 packages << "numbers";
302 packages << "authoryear";
304 packages << "]{natbib}\n";
307 return packages.str();
311 string const LaTeXFeatures::getMacros() const
313 ostringstream macros;
315 if (!preamble_snippets.empty())
317 FeaturesList::const_iterator pit = preamble_snippets.begin();
318 FeaturesList::const_iterator pend = preamble_snippets.end();
319 for (; pit != pend; ++pit) {
320 macros << *pit << '\n';
323 if (isRequired("LyX"))
324 macros << lyx_def << '\n';
326 if (isRequired("lyxline"))
327 macros << lyxline_def << '\n';
329 if (isRequired("noun"))
330 macros << noun_def << '\n';
332 if (isRequired("lyxarrow"))
333 macros << lyxarrow_def << '\n';
336 if (isRequired("quotesinglbase"))
337 macros << quotesinglbase_def << '\n';
338 if (isRequired("quotedblbase"))
339 macros << quotedblbase_def << '\n';
340 if (isRequired("guilsinglleft"))
341 macros << guilsinglleft_def << '\n';
342 if (isRequired("guilsinglright"))
343 macros << guilsinglright_def << '\n';
344 if (isRequired("guillemotleft"))
345 macros << guillemotleft_def << '\n';
346 if (isRequired("guillemotright"))
347 macros << guillemotright_def << '\n';
350 if (isRequired("boldsymbol") && !isRequired("amsmath"))
351 macros << boldsymbol_def << '\n';
352 if (isRequired("binom") && !isRequired("amsmath"))
353 macros << binom_def << '\n';
354 if (isRequired("mathcircumflex"))
355 macros << mathcircumflex_def << '\n';
358 if (isRequired("NeedLyXMinipageIndent"))
359 macros << minipageindent_def;
360 if (isRequired("ParagraphLeftIndent"))
361 macros << paragraphleftindent_def;
362 if (isRequired("NeedLyXFootnoteCode"))
363 macros << floatingfootnote_def;
365 // some problems with tex->html converters
366 if (isRequired("NeedTabularnewline"))
367 macros << tabularnewline_def;
369 // greyedout environment (note inset)
370 if (isRequired("lyxgreyedout"))
371 macros << lyxgreyedout_def;
374 getFloatDefinitions(macros);
380 string const LaTeXFeatures::getBabelOptions() const
384 for (LanguageList::const_iterator cit = UsedLanguages.begin();
385 cit != UsedLanguages.end(); ++cit)
386 if (!(*cit)->latex_options().empty())
387 tmp << (*cit)->latex_options() << '\n';
388 if (!params.language->latex_options().empty())
389 tmp << params.language->latex_options() << '\n';
395 string const LaTeXFeatures::getTClassPreamble() const
397 // the text class specific preamble
398 LyXTextClass const & tclass = params.getLyXTextClass();
399 ostringstream tcpreamble;
401 tcpreamble << tclass.preamble();
403 list<string>::const_iterator cit = usedLayouts.begin();
404 list<string>::const_iterator end = usedLayouts.end();
405 for (; cit != end; ++cit) {
406 tcpreamble << tclass[*cit]->preamble();
409 return tcpreamble.str();
413 string const LaTeXFeatures::getLyXSGMLEntities() const
415 // Definition of entities used in the document that are LyX related.
416 ostringstream entities;
418 if (isRequired("lyxarrow")) {
419 entities << "<!ENTITY lyxarrow \"->\">" << '\n';
422 return entities.str();
426 string const LaTeXFeatures::getIncludedFiles(string const & fname) const
428 ostringstream sgmlpreamble;
429 string const basename = OnlyPath(fname);
431 FileMap::const_iterator end = IncludedFiles.end();
432 for (FileMap::const_iterator fi = IncludedFiles.begin();
434 sgmlpreamble << "\n<!ENTITY " << fi->first
435 << (IsSGMLFilename(fi->second) ? " SYSTEM \"" : " \"")
436 << MakeRelPath(fi->second, basename) << "\">";
438 return sgmlpreamble.str();
442 void LaTeXFeatures::showStruct() const {
443 lyxerr << "LyX needs the following commands when LaTeXing:"
444 << "\n***** Packages:" << getPackages()
445 << "\n***** Macros:" << getMacros()
446 << "\n***** Textclass stuff:" << getTClassPreamble()
447 << "\n***** done." << endl;
451 BufferParams const & LaTeXFeatures::bufferParams() const
457 void LaTeXFeatures::getFloatDefinitions(ostream & os) const
459 FloatList const & floats = params.getLyXTextClass().floats();
461 // Here we will output the code to create the needed float styles.
462 // We will try to do this as minimal as possible.
463 // \floatstyle{ruled}
464 // \newfloat{algorithm}{htbp}{loa}
465 // \floatname{algorithm}{Algorithm}
466 UsedFloats::const_iterator cit = usedFloats.begin();
467 UsedFloats::const_iterator end = usedFloats.end();
468 // ostringstream floats;
469 for (; cit != end; ++cit) {
470 Floating const & fl = floats.getType((*cit));
472 // For builtin floats we do nothing.
473 if (fl.builtin()) continue;
475 // We have to special case "table" and "figure"
476 if (fl.type() == "tabular" || fl.type() == "figure") {
477 // Output code to modify "table" or "figure"
478 // but only if builtin == false
479 // and that have to be true at this point in the
481 string const type = fl.type();
482 string const placement = fl.placement();
483 string const style = fl.style();
484 if (!style.empty()) {
485 os << "\\floatstyle{" << style << "}\n"
486 << "\\restylefloat{" << type << "}\n";
488 if (!placement.empty()) {
489 os << "\\floatplacement{" << type << "}{"
490 << placement << "}\n";
493 // The other non builtin floats.
495 string const type = fl.type();
496 string const placement = fl.placement();
497 string const ext = fl.ext();
498 string const within = fl.within();
499 string const style = fl.style();
500 string const name = fl.name();
501 os << "\\floatstyle{" << style << "}\n"
502 << "\\newfloat{" << type << "}{" << placement
503 << "}{" << ext << '}';
505 os << '[' << within << ']';
507 << "\\floatname{" << type << "}{"
510 // What missing here is to code to minimalize the code
511 // output so that the same floatstyle will not be
512 // used several times, when the same style is still in