1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 the LyX Team.
9 * ====================================================== */
14 #pragma implementation
17 #include "LaTeXFeatures.h"
21 #include "bufferparams.h"
22 #include "FloatList.h"
27 #include "support/filetools.h"
28 #include "support/lstrings.h"
30 using lyx::textclass_type;
39 LaTeXFeatures::LaTeXFeatures(BufferParams const & p)
44 void LaTeXFeatures::require(string const & name)
49 features.push_back(name);
53 void LaTeXFeatures::useLayout(string const & layoutname)
55 // Some code to avoid loops in dependency definition
57 const int maxlevel = 30;
58 if (level > maxlevel) {
59 lyxerr << "LaTeXFeatures::useLayout: maximum level of "
60 << "recursion attained by layout "
61 << layoutname << endl;
65 LyXTextClass tclass = params.getLyXTextClass();
66 if (tclass.hasLayout(layoutname)) {
67 // Is this layout already in usedLayouts?
68 list<string>::const_iterator cit = usedLayouts.begin();
69 list<string>::const_iterator end = usedLayouts.end();
70 for (; cit != end; ++cit) {
71 if (layoutname == *cit)
75 LyXLayout_ptr lyt = tclass[layoutname];
76 if (!lyt->depends_on().empty()) {
78 useLayout(lyt->depends_on());
81 usedLayouts.push_back(layoutname);
83 lyxerr << "LaTeXFeatures::useLayout: layout `"
84 << layoutname << "' does not exist in this class"
92 bool LaTeXFeatures::isRequired(string const & name) const
94 FeaturesList::const_iterator i = find(features.begin(),
97 return i != features.end();
101 void LaTeXFeatures::addExternalPreamble(string const & pream)
103 externalPreambles += pream;
107 void LaTeXFeatures::useFloat(string const & name)
109 usedFloats.insert(name);
110 // We only need float.sty if we use non builtin floats, or if we
111 // use the "H" modifier. This includes modified table and
112 // figure floats. (Lgb)
113 Floating const & fl = floatList.getType(name);
114 if (!fl.type().empty() && !fl.builtin()) {
120 void LaTeXFeatures::useLanguage(Language const * lang)
122 UsedLanguages.insert(lang);
126 void LaTeXFeatures::includeFile(string const & key, string const & name)
128 IncludedFiles[key] = name;
132 bool LaTeXFeatures::hasLanguages()
134 return !UsedLanguages.empty();
138 string LaTeXFeatures::getLanguages() const
140 ostringstream languages;
142 for (LanguageList::const_iterator cit =
143 UsedLanguages.begin();
144 cit != UsedLanguages.end();
146 languages << (*cit)->babel() << ',';
148 return languages.str().c_str();
152 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding)
154 set<string> encodings;
155 for (LanguageList::const_iterator it =
156 UsedLanguages.begin();
157 it != UsedLanguages.end(); ++it)
158 if ((*it)->encoding()->LatexName() != doc_encoding)
159 encodings.insert((*it)->encoding()->LatexName());
165 char const * simplefeatures[] = {
180 const int nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
184 string const LaTeXFeatures::getPackages() const
186 ostringstream packages;
187 LyXTextClass const & tclass = params.getLyXTextClass();
191 // These are all the 'simple' includes. i.e
192 // packages which we just \usepackage{package}
194 for (int i = 0; i < nb_simplefeatures; ++i) {
195 if (isRequired(simplefeatures[i]))
196 packages << "\\usepackage{"
197 << simplefeatures[i] << "}\n";
201 // The rest of these packages are somewhat more complicated
205 if (isRequired("amsmath")
206 && ! tclass.provides(LyXTextClass::amsmath)) {
207 packages << "\\usepackage{amsmath}\n";
211 if (isRequired("color")) {
212 if (params.graphicsDriver == "default")
213 packages << "\\usepackage{color}\n";
215 packages << "\\usepackage["
216 << params.graphicsDriver
221 if (isRequired("makeidx")) {
222 if (! tclass.provides(LyXTextClass::makeidx))
223 packages << "\\usepackage{makeidx}\n";
224 packages << "\\makeindex\n";
228 if (isRequired("graphicx") && params.graphicsDriver != "none") {
229 if (params.graphicsDriver == "default")
230 packages << "\\usepackage{graphicx}\n";
232 packages << "\\usepackage["
233 << params.graphicsDriver
238 // packages << "\\usepackage{algorithm}\n";
241 // lyxskak.sty --- newer chess support based on skak.sty
242 if (isRequired("chess")) {
243 packages << "\\usepackage[ps,mover]{lyxskak}\n";
247 if ((params.spacing.getSpace() != Spacing::Single
248 && !params.spacing.isDefault())
249 || isRequired("setspace")) {
250 packages << "\\usepackage{setspace}\n";
252 switch (params.spacing.getSpace()) {
253 case Spacing::Default:
254 case Spacing::Single:
255 // we dont use setspace.sty so dont print anything
256 //packages += "\\singlespacing\n";
258 case Spacing::Onehalf:
259 packages << "\\onehalfspacing\n";
261 case Spacing::Double:
262 packages << "\\doublespacing\n";
265 packages << "\\setstretch{"
266 << params.spacing.getValue() << "}\n";
271 if (isRequired("amssymb") || params.use_amsmath)
272 packages << "\\usepackage{amssymb}\n";
274 if (isRequired("url") && ! tclass.provides(LyXTextClass::url))
275 packages << "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
276 " {\\newcommand{\\url}{\\texttt}}\n";
280 if (isRequired("natbib") && ! tclass.provides(LyXTextClass::natbib)) {
281 packages << "\\usepackage[";
282 if (params.use_numerical_citations) {
283 packages << "numbers";
285 packages << "authoryear";
287 packages << "]{natbib}\n";
290 packages << externalPreambles;
292 return packages.str().c_str();
296 string const LaTeXFeatures::getMacros() const
298 ostringstream macros;
300 if (isRequired("LyX"))
301 macros << lyx_def << '\n';
303 if (isRequired("lyxline"))
304 macros << lyxline_def << '\n';
306 if (isRequired("noun"))
307 macros << noun_def << '\n';
309 if (isRequired("lyxarrow"))
310 macros << lyxarrow_def << '\n';
313 if (isRequired("quotesinglbase"))
314 macros << quotesinglbase_def << '\n';
315 if (isRequired("quotedblbase"))
316 macros << quotedblbase_def << '\n';
317 if (isRequired("guilsinglleft"))
318 macros << guilsinglleft_def << '\n';
319 if (isRequired("guilsinglright"))
320 macros << guilsinglright_def << '\n';
321 if (isRequired("guillemotleft"))
322 macros << guillemotleft_def << '\n';
323 if (isRequired("guillemotright"))
324 macros << guillemotright_def << '\n';
327 if (isRequired("boldsymbol") && !isRequired("amsmath"))
328 macros << boldsymbol_def << '\n';
329 if (isRequired("binom") && !isRequired("amsmath"))
330 macros << binom_def << '\n';
331 if (isRequired("mathcircumflex"))
332 macros << mathcircumflex_def << '\n';
335 if (isRequired("NeedLyXMinipageIndent"))
336 macros << minipageindent_def;
337 if (isRequired("ParagraphLeftIndent"))
338 macros << paragraphleftindent_def;
339 if (isRequired("NeedLyXFootnoteCode"))
340 macros << floatingfootnote_def;
343 getFloatDefinitions(macros);
345 return macros.str().c_str();
349 string const LaTeXFeatures::getBabelOptions() const
353 for (LanguageList::const_iterator cit = UsedLanguages.begin();
354 cit != UsedLanguages.end(); ++cit)
355 if (!(*cit)->latex_options().empty())
356 tmp << (*cit)->latex_options() << '\n';
357 if (!params.language->latex_options().empty())
358 tmp << params.language->latex_options() << '\n';
360 return tmp.str().c_str();
364 string const LaTeXFeatures::getTClassPreamble() const
366 // the text class specific preamble
367 LyXTextClass const & tclass = params.getLyXTextClass();
368 ostringstream tcpreamble;
370 tcpreamble << tclass.preamble();
372 list<string>::const_iterator cit = usedLayouts.begin();
373 list<string>::const_iterator end = usedLayouts.end();
374 for (; cit != end; ++cit) {
375 tcpreamble << tclass[*cit]->preamble();
378 return tcpreamble.str().c_str();
382 string const LaTeXFeatures::getLyXSGMLEntities() const
384 // Definition of entities used in the document that are LyX related.
385 ostringstream entities;
387 if (isRequired("lyxarrow")) {
388 entities << "<!ENTITY lyxarrow \"->\">" << '\n';
391 return entities.str().c_str();
395 string const LaTeXFeatures::getIncludedFiles(string const & fname) const
397 ostringstream sgmlpreamble;
398 string const basename = OnlyPath(fname);
400 FileMap::const_iterator end = IncludedFiles.end();
401 for (FileMap::const_iterator fi = IncludedFiles.begin();
403 sgmlpreamble << "\n<!ENTITY " << fi->first
404 << (IsSGMLFilename(fi->second) ? " SYSTEM \"" : " \"")
405 << MakeRelPath(fi->second, basename) << "\">";
407 return sgmlpreamble.str().c_str();
411 void LaTeXFeatures::showStruct() const {
412 lyxerr << "LyX needs the following commands when LaTeXing:"
413 << "\n***** Packages:" << getPackages()
414 << "\n***** Macros:" << getMacros()
415 << "\n***** Textclass stuff:" << getTClassPreamble()
416 << "\n***** done." << endl;
420 BufferParams const & LaTeXFeatures::bufferParams() const
426 void LaTeXFeatures::getFloatDefinitions(ostream & os) const
428 // Here we will output the code to create the needed float styles.
429 // We will try to do this as minimal as possible.
430 // \floatstyle{ruled}
431 // \newfloat{algorithm}{htbp}{loa}
432 // \floatname{algorithm}{Algorithm}
433 UsedFloats::const_iterator cit = usedFloats.begin();
434 UsedFloats::const_iterator end = usedFloats.end();
435 // ostringstream floats;
436 for (; cit != end; ++cit) {
437 Floating const & fl = floatList.getType((*cit));
439 // For builtin floats we do nothing.
440 if (fl.builtin()) continue;
442 // We have to special case "table" and "figure"
443 if (fl.type() == "tabular" || fl.type() == "figure") {
444 // Output code to modify "table" or "figure"
445 // but only if builtin == false
446 // and that have to be true at this point in the
448 string const type = fl.type();
449 string const placement = fl.placement();
450 string const style = fl.style();
451 if (!style.empty()) {
452 os << "\\floatstyle{" << style << "}\n"
453 << "\\restylefloat{" << type << "}\n";
455 if (!placement.empty()) {
456 os << "\\floatplacement{" << type << "}{"
457 << placement << "}\n";
460 // The other non builtin floats.
462 string const type = fl.type();
463 string const placement = fl.placement();
464 string const ext = fl.ext();
465 string const within = fl.within();
466 string const style = fl.style();
467 string const name = fl.name();
468 os << "\\floatstyle{" << style << "}\n"
469 << "\\newfloat{" << type << "}{" << placement
470 << "}{" << ext << "}";
472 os << "[" << within << "]";
474 << "\\floatname{" << type << "}{"
477 // What missing here is to code to minimalize the code
478 // output so that the same floatstyle will not be
479 // used several times, when the same style is still in