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 STRCONV(languages.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[] = {
181 int const nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
185 string const LaTeXFeatures::getPackages() const
187 ostringstream packages;
188 LyXTextClass const & tclass = params.getLyXTextClass();
192 // These are all the 'simple' includes. i.e
193 // packages which we just \usepackage{package}
195 for (int i = 0; i < nb_simplefeatures; ++i) {
196 if (isRequired(simplefeatures[i]))
197 packages << "\\usepackage{"
198 << simplefeatures[i] << "}\n";
202 // The rest of these packages are somewhat more complicated
206 if (isRequired("amsmath")
207 && ! tclass.provides(LyXTextClass::amsmath)) {
208 packages << "\\usepackage{amsmath}\n";
212 if (isRequired("color")) {
213 if (params.graphicsDriver == "default")
214 packages << "\\usepackage[usenames]{color}\n";
216 packages << "\\usepackage["
217 << params.graphicsDriver
223 if (isRequired("makeidx")) {
224 if (! tclass.provides(LyXTextClass::makeidx))
225 packages << "\\usepackage{makeidx}\n";
226 packages << "\\makeindex\n";
230 if (isRequired("graphicx") && params.graphicsDriver != "none") {
231 if (params.graphicsDriver == "default")
232 packages << "\\usepackage{graphicx}\n";
234 packages << "\\usepackage["
235 << params.graphicsDriver
240 // packages << "\\usepackage{algorithm}\n";
243 // lyxskak.sty --- newer chess support based on skak.sty
244 if (isRequired("chess")) {
245 packages << "\\usepackage[ps,mover]{lyxskak}\n";
249 if ((params.spacing.getSpace() != Spacing::Single
250 && !params.spacing.isDefault())
251 || isRequired("setspace")) {
252 packages << "\\usepackage{setspace}\n";
254 switch (params.spacing.getSpace()) {
255 case Spacing::Default:
256 case Spacing::Single:
257 // we dont use setspace.sty so dont print anything
258 //packages += "\\singlespacing\n";
260 case Spacing::Onehalf:
261 packages << "\\onehalfspacing\n";
263 case Spacing::Double:
264 packages << "\\doublespacing\n";
267 packages << "\\setstretch{"
268 << params.spacing.getValue() << "}\n";
273 if (isRequired("amssymb") || params.use_amsmath)
274 packages << "\\usepackage{amssymb}\n";
276 if (isRequired("url") && ! tclass.provides(LyXTextClass::url))
277 packages << "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
278 " {\\newcommand{\\url}{\\texttt}}\n";
282 if (isRequired("natbib") && ! tclass.provides(LyXTextClass::natbib)) {
283 packages << "\\usepackage[";
284 if (params.use_numerical_citations) {
285 packages << "numbers";
287 packages << "authoryear";
289 packages << "]{natbib}\n";
292 packages << externalPreambles;
294 return STRCONV(packages.str());
298 string const LaTeXFeatures::getMacros() const
300 ostringstream macros;
302 if (isRequired("LyX"))
303 macros << lyx_def << '\n';
305 if (isRequired("lyxline"))
306 macros << lyxline_def << '\n';
308 if (isRequired("noun"))
309 macros << noun_def << '\n';
311 if (isRequired("lyxarrow"))
312 macros << lyxarrow_def << '\n';
315 if (isRequired("quotesinglbase"))
316 macros << quotesinglbase_def << '\n';
317 if (isRequired("quotedblbase"))
318 macros << quotedblbase_def << '\n';
319 if (isRequired("guilsinglleft"))
320 macros << guilsinglleft_def << '\n';
321 if (isRequired("guilsinglright"))
322 macros << guilsinglright_def << '\n';
323 if (isRequired("guillemotleft"))
324 macros << guillemotleft_def << '\n';
325 if (isRequired("guillemotright"))
326 macros << guillemotright_def << '\n';
329 if (isRequired("boldsymbol") && !isRequired("amsmath"))
330 macros << boldsymbol_def << '\n';
331 if (isRequired("binom") && !isRequired("amsmath"))
332 macros << binom_def << '\n';
333 if (isRequired("mathcircumflex"))
334 macros << mathcircumflex_def << '\n';
337 if (isRequired("NeedLyXMinipageIndent"))
338 macros << minipageindent_def;
339 if (isRequired("ParagraphLeftIndent"))
340 macros << paragraphleftindent_def;
341 if (isRequired("NeedLyXFootnoteCode"))
342 macros << floatingfootnote_def;
344 // some problems with tex->html converters
345 if (isRequired("NeedTabularnewline"))
346 macros << tabularnewline_def;
349 getFloatDefinitions(macros);
351 return STRCONV(macros.str());
355 string const LaTeXFeatures::getBabelOptions() const
359 for (LanguageList::const_iterator cit = UsedLanguages.begin();
360 cit != UsedLanguages.end(); ++cit)
361 if (!(*cit)->latex_options().empty())
362 tmp << (*cit)->latex_options() << '\n';
363 if (!params.language->latex_options().empty())
364 tmp << params.language->latex_options() << '\n';
366 return STRCONV(tmp.str());
370 string const LaTeXFeatures::getTClassPreamble() const
372 // the text class specific preamble
373 LyXTextClass const & tclass = params.getLyXTextClass();
374 ostringstream tcpreamble;
376 tcpreamble << tclass.preamble();
378 list<string>::const_iterator cit = usedLayouts.begin();
379 list<string>::const_iterator end = usedLayouts.end();
380 for (; cit != end; ++cit) {
381 tcpreamble << tclass[*cit]->preamble();
384 return STRCONV(tcpreamble.str());
388 string const LaTeXFeatures::getLyXSGMLEntities() const
390 // Definition of entities used in the document that are LyX related.
391 ostringstream entities;
393 if (isRequired("lyxarrow")) {
394 entities << "<!ENTITY lyxarrow \"->\">" << '\n';
397 return STRCONV(entities.str());
401 string const LaTeXFeatures::getIncludedFiles(string const & fname) const
403 ostringstream sgmlpreamble;
404 string const basename = OnlyPath(fname);
406 FileMap::const_iterator end = IncludedFiles.end();
407 for (FileMap::const_iterator fi = IncludedFiles.begin();
409 sgmlpreamble << "\n<!ENTITY " << fi->first
410 << (IsSGMLFilename(fi->second) ? " SYSTEM \"" : " \"")
411 << MakeRelPath(fi->second, basename) << "\">";
413 return STRCONV(sgmlpreamble.str());
417 void LaTeXFeatures::showStruct() const {
418 lyxerr << "LyX needs the following commands when LaTeXing:"
419 << "\n***** Packages:" << getPackages()
420 << "\n***** Macros:" << getMacros()
421 << "\n***** Textclass stuff:" << getTClassPreamble()
422 << "\n***** done." << endl;
426 BufferParams const & LaTeXFeatures::bufferParams() const
432 void LaTeXFeatures::getFloatDefinitions(ostream & os) const
434 FloatList const & floats = params.getLyXTextClass().floats();
436 // Here we will output the code to create the needed float styles.
437 // We will try to do this as minimal as possible.
438 // \floatstyle{ruled}
439 // \newfloat{algorithm}{htbp}{loa}
440 // \floatname{algorithm}{Algorithm}
441 UsedFloats::const_iterator cit = usedFloats.begin();
442 UsedFloats::const_iterator end = usedFloats.end();
443 // ostringstream floats;
444 for (; cit != end; ++cit) {
445 Floating const & fl = floats.getType((*cit));
447 // For builtin floats we do nothing.
448 if (fl.builtin()) continue;
450 // We have to special case "table" and "figure"
451 if (fl.type() == "tabular" || fl.type() == "figure") {
452 // Output code to modify "table" or "figure"
453 // but only if builtin == false
454 // and that have to be true at this point in the
456 string const type = fl.type();
457 string const placement = fl.placement();
458 string const style = fl.style();
459 if (!style.empty()) {
460 os << "\\floatstyle{" << style << "}\n"
461 << "\\restylefloat{" << type << "}\n";
463 if (!placement.empty()) {
464 os << "\\floatplacement{" << type << "}{"
465 << placement << "}\n";
468 // The other non builtin floats.
470 string const type = fl.type();
471 string const placement = fl.placement();
472 string const ext = fl.ext();
473 string const within = fl.within();
474 string const style = fl.style();
475 string const name = fl.name();
476 os << "\\floatstyle{" << style << "}\n"
477 << "\\newfloat{" << type << "}{" << placement
478 << "}{" << ext << '}';
480 os << '[' << within << ']';
482 << "\\floatname{" << type << "}{"
485 // What missing here is to code to minimalize the code
486 // output so that the same floatstyle will not be
487 // used several times, when the same style is still in