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"
20 #include "bufferparams.h"
22 #include "FloatList.h"
25 #include "support/std_sstream.h"
27 #include "support/filetools.h"
29 using namespace lyx::support;
31 using lyx::textclass_type;
40 LaTeXFeatures::LaTeXFeatures(BufferParams const & p)
45 bool LaTeXFeatures::useBabel() const
47 return lyxrc.language_use_babel ||
48 bufferParams().language->lang() != lyxrc.default_language ||
53 void LaTeXFeatures::require(string const & name)
58 features.push_back(name);
62 void LaTeXFeatures::useLayout(string const & layoutname)
64 // Some code to avoid loops in dependency definition
66 const int maxlevel = 30;
67 if (level > maxlevel) {
68 lyxerr << "LaTeXFeatures::useLayout: maximum level of "
69 << "recursion attained by layout "
70 << layoutname << endl;
74 LyXTextClass const & tclass = params.getLyXTextClass();
75 if (tclass.hasLayout(layoutname)) {
76 // Is this layout already in usedLayouts?
77 list<string>::const_iterator cit = usedLayouts.begin();
78 list<string>::const_iterator end = usedLayouts.end();
79 for (; cit != end; ++cit) {
80 if (layoutname == *cit)
84 LyXLayout_ptr const & lyt = tclass[layoutname];
85 if (!lyt->depends_on().empty()) {
87 useLayout(lyt->depends_on());
90 usedLayouts.push_back(layoutname);
92 lyxerr << "LaTeXFeatures::useLayout: layout `"
93 << layoutname << "' does not exist in this class"
101 bool LaTeXFeatures::isRequired(string const & name) const
103 return find(features.begin(), features.end(), name) != features.end();
107 void LaTeXFeatures::addExternalPreamble(string const & preamble)
109 FeaturesList::const_iterator begin = preamble_snippets.begin();
110 FeaturesList::const_iterator end = preamble_snippets.end();
111 if (find(begin, end, preamble) == end)
112 preamble_snippets.push_back(preamble);
116 void LaTeXFeatures::useFloat(string const & name)
118 usedFloats.insert(name);
119 // We only need float.sty if we use non builtin floats, or if we
120 // use the "H" modifier. This includes modified table and
121 // figure floats. (Lgb)
122 Floating const & fl = params.getLyXTextClass().floats().getType(name);
123 if (!fl.type().empty() && !fl.builtin()) {
129 void LaTeXFeatures::useLanguage(Language const * lang)
131 UsedLanguages.insert(lang);
135 void LaTeXFeatures::includeFile(string const & key, string const & name)
137 IncludedFiles[key] = name;
141 bool LaTeXFeatures::hasLanguages() const
143 return !UsedLanguages.empty();
147 string LaTeXFeatures::getLanguages() const
149 ostringstream languages;
151 for (LanguageList::const_iterator cit =
152 UsedLanguages.begin();
153 cit != UsedLanguages.end();
155 languages << (*cit)->babel() << ',';
157 return STRCONV(languages.str());
161 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding) const
163 set<string> encodings;
164 for (LanguageList::const_iterator it =
165 UsedLanguages.begin();
166 it != UsedLanguages.end(); ++it)
167 if ((*it)->encoding()->LatexName() != doc_encoding)
168 encodings.insert((*it)->encoding()->LatexName());
174 char const * simplefeatures[] = {
190 int const nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
195 string const LaTeXFeatures::getPackages() const
197 ostringstream packages;
198 LyXTextClass const & tclass = params.getLyXTextClass();
201 // These are all the 'simple' includes. i.e
202 // packages which we just \usepackage{package}
204 for (int i = 0; i < nb_simplefeatures; ++i) {
205 if (isRequired(simplefeatures[i]))
206 packages << "\\usepackage{"
207 << simplefeatures[i] << "}\n";
211 // The rest of these packages are somewhat more complicated
215 if (isRequired("amsmath")
216 && !tclass.provides(LyXTextClass::amsmath)
217 && params.use_amsmath != BufferParams::AMS_OFF) {
218 packages << "\\usepackage{amsmath}\n";
222 if (isRequired("color")) {
223 if (params.graphicsDriver == "default")
224 packages << "\\usepackage[usenames]{color}\n";
226 packages << "\\usepackage["
227 << params.graphicsDriver
233 if (isRequired("makeidx")) {
234 if (! tclass.provides(LyXTextClass::makeidx))
235 packages << "\\usepackage{makeidx}\n";
236 packages << "\\makeindex\n";
240 if (isRequired("graphicx") && params.graphicsDriver != "none") {
241 if (params.graphicsDriver == "default")
242 packages << "\\usepackage{graphicx}\n";
244 packages << "\\usepackage["
245 << params.graphicsDriver
250 // packages << "\\usepackage{algorithm}\n";
253 // lyxskak.sty --- newer chess support based on skak.sty
254 if (isRequired("chess")) {
255 packages << "\\usepackage[ps,mover]{lyxskak}\n";
259 if ((params.spacing.getSpace() != Spacing::Single
260 && !params.spacing.isDefault())
261 || isRequired("setspace")) {
262 packages << "\\usepackage{setspace}\n";
264 switch (params.spacing.getSpace()) {
265 case Spacing::Default:
266 case Spacing::Single:
267 // we dont use setspace.sty so dont print anything
268 //packages += "\\singlespacing\n";
270 case Spacing::Onehalf:
271 packages << "\\onehalfspacing\n";
273 case Spacing::Double:
274 packages << "\\doublespacing\n";
277 packages << "\\setstretch{"
278 << params.spacing.getValue() << "}\n";
283 if (isRequired("amssymb") || params.use_amsmath == BufferParams::AMS_ON)
284 packages << "\\usepackage{amssymb}\n";
286 if (isRequired("url") && ! tclass.provides(LyXTextClass::url))
287 packages << "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
288 " {\\newcommand{\\url}{\\texttt}}\n";
292 if (isRequired("natbib") && ! tclass.provides(LyXTextClass::natbib)) {
293 packages << "\\usepackage[";
294 if (params.use_numerical_citations) {
295 packages << "numbers";
297 packages << "authoryear";
299 packages << "]{natbib}\n";
302 return STRCONV(packages.str());
306 string const LaTeXFeatures::getMacros() const
308 ostringstream macros;
310 if (!preamble_snippets.empty())
312 FeaturesList::const_iterator pit = preamble_snippets.begin();
313 FeaturesList::const_iterator pend = preamble_snippets.end();
314 for (; pit != pend; ++pit) {
315 macros << *pit << '\n';
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