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 return find(features.begin(), features.end(), name) != features.end();
98 void LaTeXFeatures::addExternalPreamble(string const & pream)
100 externalPreambles += pream;
104 void LaTeXFeatures::useFloat(string const & name)
106 usedFloats.insert(name);
107 // We only need float.sty if we use non builtin floats, or if we
108 // use the "H" modifier. This includes modified table and
109 // figure floats. (Lgb)
110 Floating const & fl = params.getLyXTextClass().floats().getType(name);
111 if (!fl.type().empty() && !fl.builtin()) {
117 void LaTeXFeatures::useLanguage(Language const * lang)
119 UsedLanguages.insert(lang);
123 void LaTeXFeatures::includeFile(string const & key, string const & name)
125 IncludedFiles[key] = name;
129 bool LaTeXFeatures::hasLanguages()
131 return !UsedLanguages.empty();
135 string LaTeXFeatures::getLanguages() const
137 ostringstream languages;
139 for (LanguageList::const_iterator cit =
140 UsedLanguages.begin();
141 cit != UsedLanguages.end();
143 languages << (*cit)->babel() << ',';
145 return STRCONV(languages.str());
149 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding)
151 set<string> encodings;
152 for (LanguageList::const_iterator it =
153 UsedLanguages.begin();
154 it != UsedLanguages.end(); ++it)
155 if ((*it)->encoding()->LatexName() != doc_encoding)
156 encodings.insert((*it)->encoding()->LatexName());
162 char const * simplefeatures[] = {
178 int const nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
182 string const LaTeXFeatures::getPackages() const
184 ostringstream packages;
185 LyXTextClass const & tclass = params.getLyXTextClass();
189 // These are all the 'simple' includes. i.e
190 // packages which we just \usepackage{package}
192 for (int i = 0; i < nb_simplefeatures; ++i) {
193 if (isRequired(simplefeatures[i]))
194 packages << "\\usepackage{"
195 << simplefeatures[i] << "}\n";
199 // The rest of these packages are somewhat more complicated
203 if (isRequired("amsmath")
204 && ! tclass.provides(LyXTextClass::amsmath)) {
205 packages << "\\usepackage{amsmath}\n";
209 if (isRequired("color")) {
210 if (params.graphicsDriver == "default")
211 packages << "\\usepackage[usenames]{color}\n";
213 packages << "\\usepackage["
214 << params.graphicsDriver
220 if (isRequired("makeidx")) {
221 if (! tclass.provides(LyXTextClass::makeidx))
222 packages << "\\usepackage{makeidx}\n";
223 packages << "\\makeindex\n";
227 if (isRequired("graphicx") && params.graphicsDriver != "none") {
228 if (params.graphicsDriver == "default")
229 packages << "\\usepackage{graphicx}\n";
231 packages << "\\usepackage["
232 << params.graphicsDriver
237 // packages << "\\usepackage{algorithm}\n";
240 // lyxskak.sty --- newer chess support based on skak.sty
241 if (isRequired("chess")) {
242 packages << "\\usepackage[ps,mover]{lyxskak}\n";
246 if ((params.spacing.getSpace() != Spacing::Single
247 && !params.spacing.isDefault())
248 || isRequired("setspace")) {
249 packages << "\\usepackage{setspace}\n";
251 switch (params.spacing.getSpace()) {
252 case Spacing::Default:
253 case Spacing::Single:
254 // we dont use setspace.sty so dont print anything
255 //packages += "\\singlespacing\n";
257 case Spacing::Onehalf:
258 packages << "\\onehalfspacing\n";
260 case Spacing::Double:
261 packages << "\\doublespacing\n";
264 packages << "\\setstretch{"
265 << params.spacing.getValue() << "}\n";
270 if (isRequired("amssymb") || params.use_amsmath)
271 packages << "\\usepackage{amssymb}\n";
273 if (isRequired("url") && ! tclass.provides(LyXTextClass::url))
274 packages << "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
275 " {\\newcommand{\\url}{\\texttt}}\n";
279 if (isRequired("natbib") && ! tclass.provides(LyXTextClass::natbib)) {
280 packages << "\\usepackage[";
281 if (params.use_numerical_citations) {
282 packages << "numbers";
284 packages << "authoryear";
286 packages << "]{natbib}\n";
289 packages << externalPreambles;
291 return STRCONV(packages.str());
295 string const LaTeXFeatures::getMacros() const
297 ostringstream macros;
299 if (isRequired("LyX"))
300 macros << lyx_def << '\n';
302 if (isRequired("lyxline"))
303 macros << lyxline_def << '\n';
305 if (isRequired("noun"))
306 macros << noun_def << '\n';
308 if (isRequired("lyxarrow"))
309 macros << lyxarrow_def << '\n';
312 if (isRequired("quotesinglbase"))
313 macros << quotesinglbase_def << '\n';
314 if (isRequired("quotedblbase"))
315 macros << quotedblbase_def << '\n';
316 if (isRequired("guilsinglleft"))
317 macros << guilsinglleft_def << '\n';
318 if (isRequired("guilsinglright"))
319 macros << guilsinglright_def << '\n';
320 if (isRequired("guillemotleft"))
321 macros << guillemotleft_def << '\n';
322 if (isRequired("guillemotright"))
323 macros << guillemotright_def << '\n';
326 if (isRequired("boldsymbol") && !isRequired("amsmath"))
327 macros << boldsymbol_def << '\n';
328 if (isRequired("binom") && !isRequired("amsmath"))
329 macros << binom_def << '\n';
330 if (isRequired("mathcircumflex"))
331 macros << mathcircumflex_def << '\n';
334 if (isRequired("NeedLyXMinipageIndent"))
335 macros << minipageindent_def;
336 if (isRequired("ParagraphLeftIndent"))
337 macros << paragraphleftindent_def;
338 if (isRequired("NeedLyXFootnoteCode"))
339 macros << floatingfootnote_def;
341 // some problems with tex->html converters
342 if (isRequired("NeedTabularnewline"))
343 macros << tabularnewline_def;
346 getFloatDefinitions(macros);
348 return STRCONV(macros.str());
352 string const LaTeXFeatures::getBabelOptions() const
356 for (LanguageList::const_iterator cit = UsedLanguages.begin();
357 cit != UsedLanguages.end(); ++cit)
358 if (!(*cit)->latex_options().empty())
359 tmp << (*cit)->latex_options() << '\n';
360 if (!params.language->latex_options().empty())
361 tmp << params.language->latex_options() << '\n';
363 return STRCONV(tmp.str());
367 string const LaTeXFeatures::getTClassPreamble() const
369 // the text class specific preamble
370 LyXTextClass const & tclass = params.getLyXTextClass();
371 ostringstream tcpreamble;
373 tcpreamble << tclass.preamble();
375 list<string>::const_iterator cit = usedLayouts.begin();
376 list<string>::const_iterator end = usedLayouts.end();
377 for (; cit != end; ++cit) {
378 tcpreamble << tclass[*cit]->preamble();
381 return STRCONV(tcpreamble.str());
385 string const LaTeXFeatures::getLyXSGMLEntities() const
387 // Definition of entities used in the document that are LyX related.
388 ostringstream entities;
390 if (isRequired("lyxarrow")) {
391 entities << "<!ENTITY lyxarrow \"->\">" << '\n';
394 return STRCONV(entities.str());
398 string const LaTeXFeatures::getIncludedFiles(string const & fname) const
400 ostringstream sgmlpreamble;
401 string const basename = OnlyPath(fname);
403 FileMap::const_iterator end = IncludedFiles.end();
404 for (FileMap::const_iterator fi = IncludedFiles.begin();
406 sgmlpreamble << "\n<!ENTITY " << fi->first
407 << (IsSGMLFilename(fi->second) ? " SYSTEM \"" : " \"")
408 << MakeRelPath(fi->second, basename) << "\">";
410 return STRCONV(sgmlpreamble.str());
414 void LaTeXFeatures::showStruct() const {
415 lyxerr << "LyX needs the following commands when LaTeXing:"
416 << "\n***** Packages:" << getPackages()
417 << "\n***** Macros:" << getMacros()
418 << "\n***** Textclass stuff:" << getTClassPreamble()
419 << "\n***** done." << endl;
423 BufferParams const & LaTeXFeatures::bufferParams() const
429 void LaTeXFeatures::getFloatDefinitions(ostream & os) const
431 FloatList const & floats = params.getLyXTextClass().floats();
433 // Here we will output the code to create the needed float styles.
434 // We will try to do this as minimal as possible.
435 // \floatstyle{ruled}
436 // \newfloat{algorithm}{htbp}{loa}
437 // \floatname{algorithm}{Algorithm}
438 UsedFloats::const_iterator cit = usedFloats.begin();
439 UsedFloats::const_iterator end = usedFloats.end();
440 // ostringstream floats;
441 for (; cit != end; ++cit) {
442 Floating const & fl = floats.getType((*cit));
444 // For builtin floats we do nothing.
445 if (fl.builtin()) continue;
447 // We have to special case "table" and "figure"
448 if (fl.type() == "tabular" || fl.type() == "figure") {
449 // Output code to modify "table" or "figure"
450 // but only if builtin == false
451 // and that have to be true at this point in the
453 string const type = fl.type();
454 string const placement = fl.placement();
455 string const style = fl.style();
456 if (!style.empty()) {
457 os << "\\floatstyle{" << style << "}\n"
458 << "\\restylefloat{" << type << "}\n";
460 if (!placement.empty()) {
461 os << "\\floatplacement{" << type << "}{"
462 << placement << "}\n";
465 // The other non builtin floats.
467 string const type = fl.type();
468 string const placement = fl.placement();
469 string const ext = fl.ext();
470 string const within = fl.within();
471 string const style = fl.style();
472 string const name = fl.name();
473 os << "\\floatstyle{" << style << "}\n"
474 << "\\newfloat{" << type << "}{" << placement
475 << "}{" << ext << '}';
477 os << '[' << within << ']';
479 << "\\floatname{" << type << "}{"
482 // What missing here is to code to minimalize the code
483 // output so that the same floatstyle will not be
484 // used several times, when the same style is still in