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"
21 #include "bufferparams.h"
22 #include "FloatList.h"
28 #include "support/filetools.h"
29 #include "support/lstrings.h"
31 using namespace lyx::support;
33 using lyx::textclass_type;
42 LaTeXFeatures::LaTeXFeatures(BufferParams const & p)
47 bool LaTeXFeatures::useBabel() const
49 return lyxrc.language_use_babel ||
50 bufferParams().language->lang() != lyxrc.default_language ||
55 void LaTeXFeatures::require(string const & name)
60 features.push_back(name);
64 void LaTeXFeatures::useLayout(string const & layoutname)
66 // Some code to avoid loops in dependency definition
68 const int maxlevel = 30;
69 if (level > maxlevel) {
70 lyxerr << "LaTeXFeatures::useLayout: maximum level of "
71 << "recursion attained by layout "
72 << layoutname << endl;
76 LyXTextClass const & tclass = params.getLyXTextClass();
77 if (tclass.hasLayout(layoutname)) {
78 // Is this layout already in usedLayouts?
79 list<string>::const_iterator cit = usedLayouts.begin();
80 list<string>::const_iterator end = usedLayouts.end();
81 for (; cit != end; ++cit) {
82 if (layoutname == *cit)
86 LyXLayout_ptr const & lyt = tclass[layoutname];
87 if (!lyt->depends_on().empty()) {
89 useLayout(lyt->depends_on());
92 usedLayouts.push_back(layoutname);
94 lyxerr << "LaTeXFeatures::useLayout: layout `"
95 << layoutname << "' does not exist in this class"
103 bool LaTeXFeatures::isRequired(string const & name) const
105 return find(features.begin(), features.end(), name) != features.end();
109 void LaTeXFeatures::addExternalPreamble(string const & preamble)
111 FeaturesList::const_iterator begin = preamble_snippets.begin();
112 FeaturesList::const_iterator end = preamble_snippets.end();
113 if (find(begin, end, preamble) == end)
114 preamble_snippets.push_back(preamble);
118 void LaTeXFeatures::useFloat(string const & name)
120 usedFloats.insert(name);
121 // We only need float.sty if we use non builtin floats, or if we
122 // use the "H" modifier. This includes modified table and
123 // figure floats. (Lgb)
124 Floating const & fl = params.getLyXTextClass().floats().getType(name);
125 if (!fl.type().empty() && !fl.builtin()) {
131 void LaTeXFeatures::useLanguage(Language const * lang)
133 UsedLanguages.insert(lang);
137 void LaTeXFeatures::includeFile(string const & key, string const & name)
139 IncludedFiles[key] = name;
143 bool LaTeXFeatures::hasLanguages() const
145 return !UsedLanguages.empty();
149 string LaTeXFeatures::getLanguages() const
151 ostringstream languages;
153 for (LanguageList::const_iterator cit =
154 UsedLanguages.begin();
155 cit != UsedLanguages.end();
157 languages << (*cit)->babel() << ',';
159 return STRCONV(languages.str());
163 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding) const
165 set<string> encodings;
166 for (LanguageList::const_iterator it =
167 UsedLanguages.begin();
168 it != UsedLanguages.end(); ++it)
169 if ((*it)->encoding()->LatexName() != doc_encoding)
170 encodings.insert((*it)->encoding()->LatexName());
176 char const * simplefeatures[] = {
192 int const nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
197 string const LaTeXFeatures::getPackages() const
199 ostringstream packages;
200 LyXTextClass const & tclass = params.getLyXTextClass();
203 // These are all the 'simple' includes. i.e
204 // packages which we just \usepackage{package}
206 for (int i = 0; i < nb_simplefeatures; ++i) {
207 if (isRequired(simplefeatures[i]))
208 packages << "\\usepackage{"
209 << simplefeatures[i] << "}\n";
213 // The rest of these packages are somewhat more complicated
217 if (isRequired("amsmath")
218 && !tclass.provides(LyXTextClass::amsmath)
219 && params.use_amsmath != BufferParams::AMS_OFF) {
220 packages << "\\usepackage{amsmath}\n";
224 if (isRequired("color")) {
225 if (params.graphicsDriver == "default")
226 packages << "\\usepackage[usenames]{color}\n";
228 packages << "\\usepackage["
229 << params.graphicsDriver
235 if (isRequired("makeidx")) {
236 if (! tclass.provides(LyXTextClass::makeidx))
237 packages << "\\usepackage{makeidx}\n";
238 packages << "\\makeindex\n";
242 if (isRequired("graphicx") && params.graphicsDriver != "none") {
243 if (params.graphicsDriver == "default")
244 packages << "\\usepackage{graphicx}\n";
246 packages << "\\usepackage["
247 << params.graphicsDriver
252 // packages << "\\usepackage{algorithm}\n";
255 // lyxskak.sty --- newer chess support based on skak.sty
256 if (isRequired("chess")) {
257 packages << "\\usepackage[ps,mover]{lyxskak}\n";
261 if ((params.spacing.getSpace() != Spacing::Single
262 && !params.spacing.isDefault())
263 || isRequired("setspace")) {
264 packages << "\\usepackage{setspace}\n";
266 switch (params.spacing.getSpace()) {
267 case Spacing::Default:
268 case Spacing::Single:
269 // we dont use setspace.sty so dont print anything
270 //packages += "\\singlespacing\n";
272 case Spacing::Onehalf:
273 packages << "\\onehalfspacing\n";
275 case Spacing::Double:
276 packages << "\\doublespacing\n";
279 packages << "\\setstretch{"
280 << params.spacing.getValue() << "}\n";
285 if (isRequired("amssymb") || params.use_amsmath == BufferParams::AMS_ON)
286 packages << "\\usepackage{amssymb}\n";
288 if (isRequired("url") && ! tclass.provides(LyXTextClass::url))
289 packages << "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
290 " {\\newcommand{\\url}{\\texttt}}\n";
294 if (isRequired("natbib") && ! tclass.provides(LyXTextClass::natbib)) {
295 packages << "\\usepackage[";
296 if (params.use_numerical_citations) {
297 packages << "numbers";
299 packages << "authoryear";
301 packages << "]{natbib}\n";
304 FeaturesList::const_iterator pit = preamble_snippets.begin();
305 FeaturesList::const_iterator pend = preamble_snippets.end();
306 for (; pit != pend; ++pit) {
307 packages << *pit << '\n';
310 return STRCONV(packages.str());
314 string const LaTeXFeatures::getMacros() const
316 ostringstream macros;
318 if (isRequired("LyX"))
319 macros << lyx_def << '\n';
321 if (isRequired("lyxline"))
322 macros << lyxline_def << '\n';
324 if (isRequired("noun"))
325 macros << noun_def << '\n';
327 if (isRequired("lyxarrow"))
328 macros << lyxarrow_def << '\n';
331 if (isRequired("quotesinglbase"))
332 macros << quotesinglbase_def << '\n';
333 if (isRequired("quotedblbase"))
334 macros << quotedblbase_def << '\n';
335 if (isRequired("guilsinglleft"))
336 macros << guilsinglleft_def << '\n';
337 if (isRequired("guilsinglright"))
338 macros << guilsinglright_def << '\n';
339 if (isRequired("guillemotleft"))
340 macros << guillemotleft_def << '\n';
341 if (isRequired("guillemotright"))
342 macros << guillemotright_def << '\n';
345 if (isRequired("boldsymbol") && !isRequired("amsmath"))
346 macros << boldsymbol_def << '\n';
347 if (isRequired("binom") && !isRequired("amsmath"))
348 macros << binom_def << '\n';
349 if (isRequired("mathcircumflex"))
350 macros << mathcircumflex_def << '\n';
353 if (isRequired("NeedLyXMinipageIndent"))
354 macros << minipageindent_def;
355 if (isRequired("ParagraphLeftIndent"))
356 macros << paragraphleftindent_def;
357 if (isRequired("NeedLyXFootnoteCode"))
358 macros << floatingfootnote_def;
360 // some problems with tex->html converters
361 if (isRequired("NeedTabularnewline"))
362 macros << tabularnewline_def;
364 // greyedout environment (note inset)
365 if (isRequired("lyxgreyedout"))
366 macros << lyxgreyedout_def;
369 getFloatDefinitions(macros);
371 return STRCONV(macros.str());
375 string const LaTeXFeatures::getBabelOptions() const
379 for (LanguageList::const_iterator cit = UsedLanguages.begin();
380 cit != UsedLanguages.end(); ++cit)
381 if (!(*cit)->latex_options().empty())
382 tmp << (*cit)->latex_options() << '\n';
383 if (!params.language->latex_options().empty())
384 tmp << params.language->latex_options() << '\n';
386 return STRCONV(tmp.str());
390 string const LaTeXFeatures::getTClassPreamble() const
392 // the text class specific preamble
393 LyXTextClass const & tclass = params.getLyXTextClass();
394 ostringstream tcpreamble;
396 tcpreamble << tclass.preamble();
398 list<string>::const_iterator cit = usedLayouts.begin();
399 list<string>::const_iterator end = usedLayouts.end();
400 for (; cit != end; ++cit) {
401 tcpreamble << tclass[*cit]->preamble();
404 return STRCONV(tcpreamble.str());
408 string const LaTeXFeatures::getLyXSGMLEntities() const
410 // Definition of entities used in the document that are LyX related.
411 ostringstream entities;
413 if (isRequired("lyxarrow")) {
414 entities << "<!ENTITY lyxarrow \"->\">" << '\n';
417 return STRCONV(entities.str());
421 string const LaTeXFeatures::getIncludedFiles(string const & fname) const
423 ostringstream sgmlpreamble;
424 string const basename = OnlyPath(fname);
426 FileMap::const_iterator end = IncludedFiles.end();
427 for (FileMap::const_iterator fi = IncludedFiles.begin();
429 sgmlpreamble << "\n<!ENTITY " << fi->first
430 << (IsSGMLFilename(fi->second) ? " SYSTEM \"" : " \"")
431 << MakeRelPath(fi->second, basename) << "\">";
433 return STRCONV(sgmlpreamble.str());
437 void LaTeXFeatures::showStruct() const {
438 lyxerr << "LyX needs the following commands when LaTeXing:"
439 << "\n***** Packages:" << getPackages()
440 << "\n***** Macros:" << getMacros()
441 << "\n***** Textclass stuff:" << getTClassPreamble()
442 << "\n***** done." << endl;
446 BufferParams const & LaTeXFeatures::bufferParams() const
452 void LaTeXFeatures::getFloatDefinitions(ostream & os) const
454 FloatList const & floats = params.getLyXTextClass().floats();
456 // Here we will output the code to create the needed float styles.
457 // We will try to do this as minimal as possible.
458 // \floatstyle{ruled}
459 // \newfloat{algorithm}{htbp}{loa}
460 // \floatname{algorithm}{Algorithm}
461 UsedFloats::const_iterator cit = usedFloats.begin();
462 UsedFloats::const_iterator end = usedFloats.end();
463 // ostringstream floats;
464 for (; cit != end; ++cit) {
465 Floating const & fl = floats.getType((*cit));
467 // For builtin floats we do nothing.
468 if (fl.builtin()) continue;
470 // We have to special case "table" and "figure"
471 if (fl.type() == "tabular" || fl.type() == "figure") {
472 // Output code to modify "table" or "figure"
473 // but only if builtin == false
474 // and that have to be true at this point in the
476 string const type = fl.type();
477 string const placement = fl.placement();
478 string const style = fl.style();
479 if (!style.empty()) {
480 os << "\\floatstyle{" << style << "}\n"
481 << "\\restylefloat{" << type << "}\n";
483 if (!placement.empty()) {
484 os << "\\floatplacement{" << type << "}{"
485 << placement << "}\n";
488 // The other non builtin floats.
490 string const type = fl.type();
491 string const placement = fl.placement();
492 string const ext = fl.ext();
493 string const within = fl.within();
494 string const style = fl.style();
495 string const name = fl.name();
496 os << "\\floatstyle{" << style << "}\n"
497 << "\\newfloat{" << type << "}{" << placement
498 << "}{" << ext << '}';
500 os << '[' << within << ']';
502 << "\\floatname{" << type << "}{"
505 // What missing here is to code to minimalize the code
506 // output so that the same floatstyle will not be
507 // used several times, when the same style is still in