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 const & 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 const & 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 = params.getLyXTextClass().floats().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;
342 // some problems with tex->html converters
343 if (isRequired("NeedTabularnewline"))
344 macros << tabularnewline_def;
347 getFloatDefinitions(macros);
349 return macros.str().c_str();
353 string const LaTeXFeatures::getBabelOptions() const
357 for (LanguageList::const_iterator cit = UsedLanguages.begin();
358 cit != UsedLanguages.end(); ++cit)
359 if (!(*cit)->latex_options().empty())
360 tmp << (*cit)->latex_options() << '\n';
361 if (!params.language->latex_options().empty())
362 tmp << params.language->latex_options() << '\n';
364 return tmp.str().c_str();
368 string const LaTeXFeatures::getTClassPreamble() const
370 // the text class specific preamble
371 LyXTextClass const & tclass = params.getLyXTextClass();
372 ostringstream tcpreamble;
374 tcpreamble << tclass.preamble();
376 list<string>::const_iterator cit = usedLayouts.begin();
377 list<string>::const_iterator end = usedLayouts.end();
378 for (; cit != end; ++cit) {
379 tcpreamble << tclass[*cit]->preamble();
382 return tcpreamble.str().c_str();
386 string const LaTeXFeatures::getLyXSGMLEntities() const
388 // Definition of entities used in the document that are LyX related.
389 ostringstream entities;
391 if (isRequired("lyxarrow")) {
392 entities << "<!ENTITY lyxarrow \"->\">" << '\n';
395 return entities.str().c_str();
399 string const LaTeXFeatures::getIncludedFiles(string const & fname) const
401 ostringstream sgmlpreamble;
402 string const basename = OnlyPath(fname);
404 FileMap::const_iterator end = IncludedFiles.end();
405 for (FileMap::const_iterator fi = IncludedFiles.begin();
407 sgmlpreamble << "\n<!ENTITY " << fi->first
408 << (IsSGMLFilename(fi->second) ? " SYSTEM \"" : " \"")
409 << MakeRelPath(fi->second, basename) << "\">";
411 return sgmlpreamble.str().c_str();
415 void LaTeXFeatures::showStruct() const {
416 lyxerr << "LyX needs the following commands when LaTeXing:"
417 << "\n***** Packages:" << getPackages()
418 << "\n***** Macros:" << getMacros()
419 << "\n***** Textclass stuff:" << getTClassPreamble()
420 << "\n***** done." << endl;
424 BufferParams const & LaTeXFeatures::bufferParams() const
430 void LaTeXFeatures::getFloatDefinitions(ostream & os) const
432 FloatList const & floats = params.getLyXTextClass().floats();
434 // Here we will output the code to create the needed float styles.
435 // We will try to do this as minimal as possible.
436 // \floatstyle{ruled}
437 // \newfloat{algorithm}{htbp}{loa}
438 // \floatname{algorithm}{Algorithm}
439 UsedFloats::const_iterator cit = usedFloats.begin();
440 UsedFloats::const_iterator end = usedFloats.end();
441 // ostringstream floats;
442 for (; cit != end; ++cit) {
443 Floating const & fl = floats.getType((*cit));
445 // For builtin floats we do nothing.
446 if (fl.builtin()) continue;
448 // We have to special case "table" and "figure"
449 if (fl.type() == "tabular" || fl.type() == "figure") {
450 // Output code to modify "table" or "figure"
451 // but only if builtin == false
452 // and that have to be true at this point in the
454 string const type = fl.type();
455 string const placement = fl.placement();
456 string const style = fl.style();
457 if (!style.empty()) {
458 os << "\\floatstyle{" << style << "}\n"
459 << "\\restylefloat{" << type << "}\n";
461 if (!placement.empty()) {
462 os << "\\floatplacement{" << type << "}{"
463 << placement << "}\n";
466 // The other non builtin floats.
468 string const type = fl.type();
469 string const placement = fl.placement();
470 string const ext = fl.ext();
471 string const within = fl.within();
472 string const style = fl.style();
473 string const name = fl.name();
474 os << "\\floatstyle{" << style << "}\n"
475 << "\\newfloat{" << type << "}{" << placement
476 << "}{" << ext << "}";
478 os << "[" << within << "]";
480 << "\\floatname{" << type << "}{"
483 // What missing here is to code to minimalize the code
484 // output so that the same floatstyle will not be
485 // used several times, when the same style is still in