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 * ====================================================== */
13 #include "LaTeXFeatures.h"
17 #include "bufferparams.h"
18 #include "FloatList.h"
23 #include "support/filetools.h"
24 #include "support/lstrings.h"
26 using lyx::textclass_type;
35 LaTeXFeatures::LaTeXFeatures(BufferParams const & p)
40 void LaTeXFeatures::require(string const & name)
45 features.push_back(name);
49 void LaTeXFeatures::useLayout(string const & layoutname)
51 // Some code to avoid loops in dependency definition
53 const int maxlevel = 30;
54 if (level > maxlevel) {
55 lyxerr << "LaTeXFeatures::useLayout: maximum level of "
56 << "recursion attained by layout "
57 << layoutname << endl;
61 LyXTextClass const & tclass = params.getLyXTextClass();
62 if (tclass.hasLayout(layoutname)) {
63 // Is this layout already in usedLayouts?
64 list<string>::const_iterator cit = usedLayouts.begin();
65 list<string>::const_iterator end = usedLayouts.end();
66 for (; cit != end; ++cit) {
67 if (layoutname == *cit)
71 LyXLayout_ptr const & lyt = tclass[layoutname];
72 if (!lyt->depends_on().empty()) {
74 useLayout(lyt->depends_on());
77 usedLayouts.push_back(layoutname);
79 lyxerr << "LaTeXFeatures::useLayout: layout `"
80 << layoutname << "' does not exist in this class"
88 bool LaTeXFeatures::isRequired(string const & name) const
90 return find(features.begin(), features.end(), name) != features.end();
94 void LaTeXFeatures::addExternalPreamble(string const & pream)
96 externalPreambles += pream;
100 void LaTeXFeatures::useFloat(string const & name)
102 usedFloats.insert(name);
103 // We only need float.sty if we use non builtin floats, or if we
104 // use the "H" modifier. This includes modified table and
105 // figure floats. (Lgb)
106 Floating const & fl = params.getLyXTextClass().floats().getType(name);
107 if (!fl.type().empty() && !fl.builtin()) {
113 void LaTeXFeatures::useLanguage(Language const * lang)
115 UsedLanguages.insert(lang);
119 void LaTeXFeatures::includeFile(string const & key, string const & name)
121 IncludedFiles[key] = name;
125 bool LaTeXFeatures::hasLanguages() const
127 return !UsedLanguages.empty();
131 string LaTeXFeatures::getLanguages() const
133 ostringstream languages;
135 for (LanguageList::const_iterator cit =
136 UsedLanguages.begin();
137 cit != UsedLanguages.end();
139 languages << (*cit)->babel() << ',';
141 return STRCONV(languages.str());
145 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding) const
147 set<string> encodings;
148 for (LanguageList::const_iterator it =
149 UsedLanguages.begin();
150 it != UsedLanguages.end(); ++it)
151 if ((*it)->encoding()->LatexName() != doc_encoding)
152 encodings.insert((*it)->encoding()->LatexName());
158 char const * simplefeatures[] = {
174 int const nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
178 string const LaTeXFeatures::getPackages() const
180 ostringstream packages;
181 LyXTextClass const & tclass = params.getLyXTextClass();
185 // These are all the 'simple' includes. i.e
186 // packages which we just \usepackage{package}
188 for (int i = 0; i < nb_simplefeatures; ++i) {
189 if (isRequired(simplefeatures[i]))
190 packages << "\\usepackage{"
191 << simplefeatures[i] << "}\n";
195 // The rest of these packages are somewhat more complicated
199 if (isRequired("amsmath")
200 && ! tclass.provides(LyXTextClass::amsmath)) {
201 packages << "\\usepackage{amsmath}\n";
205 if (isRequired("color")) {
206 if (params.graphicsDriver == "default")
207 packages << "\\usepackage[usenames]{color}\n";
209 packages << "\\usepackage["
210 << params.graphicsDriver
216 if (isRequired("makeidx")) {
217 if (! tclass.provides(LyXTextClass::makeidx))
218 packages << "\\usepackage{makeidx}\n";
219 packages << "\\makeindex\n";
223 if (isRequired("graphicx") && params.graphicsDriver != "none") {
224 if (params.graphicsDriver == "default")
225 packages << "\\usepackage{graphicx}\n";
227 packages << "\\usepackage["
228 << params.graphicsDriver
233 // packages << "\\usepackage{algorithm}\n";
236 // lyxskak.sty --- newer chess support based on skak.sty
237 if (isRequired("chess")) {
238 packages << "\\usepackage[ps,mover]{lyxskak}\n";
242 if ((params.spacing.getSpace() != Spacing::Single
243 && !params.spacing.isDefault())
244 || isRequired("setspace")) {
245 packages << "\\usepackage{setspace}\n";
247 switch (params.spacing.getSpace()) {
248 case Spacing::Default:
249 case Spacing::Single:
250 // we dont use setspace.sty so dont print anything
251 //packages += "\\singlespacing\n";
253 case Spacing::Onehalf:
254 packages << "\\onehalfspacing\n";
256 case Spacing::Double:
257 packages << "\\doublespacing\n";
260 packages << "\\setstretch{"
261 << params.spacing.getValue() << "}\n";
266 if (isRequired("amssymb") || params.use_amsmath)
267 packages << "\\usepackage{amssymb}\n";
269 if (isRequired("url") && ! tclass.provides(LyXTextClass::url))
270 packages << "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
271 " {\\newcommand{\\url}{\\texttt}}\n";
275 if (isRequired("natbib") && ! tclass.provides(LyXTextClass::natbib)) {
276 packages << "\\usepackage[";
277 if (params.use_numerical_citations) {
278 packages << "numbers";
280 packages << "authoryear";
282 packages << "]{natbib}\n";
285 packages << externalPreambles;
287 return STRCONV(packages.str());
291 string const LaTeXFeatures::getMacros() const
293 ostringstream macros;
295 if (isRequired("LyX"))
296 macros << lyx_def << '\n';
298 if (isRequired("lyxline"))
299 macros << lyxline_def << '\n';
301 if (isRequired("noun"))
302 macros << noun_def << '\n';
304 if (isRequired("lyxarrow"))
305 macros << lyxarrow_def << '\n';
308 if (isRequired("quotesinglbase"))
309 macros << quotesinglbase_def << '\n';
310 if (isRequired("quotedblbase"))
311 macros << quotedblbase_def << '\n';
312 if (isRequired("guilsinglleft"))
313 macros << guilsinglleft_def << '\n';
314 if (isRequired("guilsinglright"))
315 macros << guilsinglright_def << '\n';
316 if (isRequired("guillemotleft"))
317 macros << guillemotleft_def << '\n';
318 if (isRequired("guillemotright"))
319 macros << guillemotright_def << '\n';
322 if (isRequired("boldsymbol") && !isRequired("amsmath"))
323 macros << boldsymbol_def << '\n';
324 if (isRequired("binom") && !isRequired("amsmath"))
325 macros << binom_def << '\n';
326 if (isRequired("mathcircumflex"))
327 macros << mathcircumflex_def << '\n';
330 if (isRequired("NeedLyXMinipageIndent"))
331 macros << minipageindent_def;
332 if (isRequired("ParagraphLeftIndent"))
333 macros << paragraphleftindent_def;
334 if (isRequired("NeedLyXFootnoteCode"))
335 macros << floatingfootnote_def;
337 // some problems with tex->html converters
338 if (isRequired("NeedTabularnewline"))
339 macros << tabularnewline_def;
342 getFloatDefinitions(macros);
344 return STRCONV(macros.str());
348 string const LaTeXFeatures::getBabelOptions() const
352 for (LanguageList::const_iterator cit = UsedLanguages.begin();
353 cit != UsedLanguages.end(); ++cit)
354 if (!(*cit)->latex_options().empty())
355 tmp << (*cit)->latex_options() << '\n';
356 if (!params.language->latex_options().empty())
357 tmp << params.language->latex_options() << '\n';
359 return STRCONV(tmp.str());
363 string const LaTeXFeatures::getTClassPreamble() const
365 // the text class specific preamble
366 LyXTextClass const & tclass = params.getLyXTextClass();
367 ostringstream tcpreamble;
369 tcpreamble << tclass.preamble();
371 list<string>::const_iterator cit = usedLayouts.begin();
372 list<string>::const_iterator end = usedLayouts.end();
373 for (; cit != end; ++cit) {
374 tcpreamble << tclass[*cit]->preamble();
377 return STRCONV(tcpreamble.str());
381 string const LaTeXFeatures::getLyXSGMLEntities() const
383 // Definition of entities used in the document that are LyX related.
384 ostringstream entities;
386 if (isRequired("lyxarrow")) {
387 entities << "<!ENTITY lyxarrow \"->\">" << '\n';
390 return STRCONV(entities.str());
394 string const LaTeXFeatures::getIncludedFiles(string const & fname) const
396 ostringstream sgmlpreamble;
397 string const basename = OnlyPath(fname);
399 FileMap::const_iterator end = IncludedFiles.end();
400 for (FileMap::const_iterator fi = IncludedFiles.begin();
402 sgmlpreamble << "\n<!ENTITY " << fi->first
403 << (IsSGMLFilename(fi->second) ? " SYSTEM \"" : " \"")
404 << MakeRelPath(fi->second, basename) << "\">";
406 return STRCONV(sgmlpreamble.str());
410 void LaTeXFeatures::showStruct() const {
411 lyxerr << "LyX needs the following commands when LaTeXing:"
412 << "\n***** Packages:" << getPackages()
413 << "\n***** Macros:" << getMacros()
414 << "\n***** Textclass stuff:" << getTClassPreamble()
415 << "\n***** done." << endl;
419 BufferParams const & LaTeXFeatures::bufferParams() const
425 void LaTeXFeatures::getFloatDefinitions(ostream & os) const
427 FloatList const & floats = params.getLyXTextClass().floats();
429 // Here we will output the code to create the needed float styles.
430 // We will try to do this as minimal as possible.
431 // \floatstyle{ruled}
432 // \newfloat{algorithm}{htbp}{loa}
433 // \floatname{algorithm}{Algorithm}
434 UsedFloats::const_iterator cit = usedFloats.begin();
435 UsedFloats::const_iterator end = usedFloats.end();
436 // ostringstream floats;
437 for (; cit != end; ++cit) {
438 Floating const & fl = floats.getType((*cit));
440 // For builtin floats we do nothing.
441 if (fl.builtin()) continue;
443 // We have to special case "table" and "figure"
444 if (fl.type() == "tabular" || fl.type() == "figure") {
445 // Output code to modify "table" or "figure"
446 // but only if builtin == false
447 // and that have to be true at this point in the
449 string const type = fl.type();
450 string const placement = fl.placement();
451 string const style = fl.style();
452 if (!style.empty()) {
453 os << "\\floatstyle{" << style << "}\n"
454 << "\\restylefloat{" << type << "}\n";
456 if (!placement.empty()) {
457 os << "\\floatplacement{" << type << "}{"
458 << placement << "}\n";
461 // The other non builtin floats.
463 string const type = fl.type();
464 string const placement = fl.placement();
465 string const ext = fl.ext();
466 string const within = fl.within();
467 string const style = fl.style();
468 string const name = fl.name();
469 os << "\\floatstyle{" << style << "}\n"
470 << "\\newfloat{" << type << "}{" << placement
471 << "}{" << ext << '}';
473 os << '[' << within << ']';
475 << "\\floatname{" << type << "}{"
478 // What missing here is to code to minimalize the code
479 // output so that the same floatstyle will not be
480 // used several times, when the same style is still in