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"
24 #include "support/filetools.h"
25 #include "support/lstrings.h"
27 using lyx::textclass_type;
36 LaTeXFeatures::LaTeXFeatures(BufferParams const & p)
41 bool LaTeXFeatures::useBabel() const
43 return lyxrc.language_use_babel ||
44 bufferParams().language->lang() != lyxrc.default_language ||
49 void LaTeXFeatures::require(string const & name)
54 features.push_back(name);
58 void LaTeXFeatures::useLayout(string const & layoutname)
60 // Some code to avoid loops in dependency definition
62 const int maxlevel = 30;
63 if (level > maxlevel) {
64 lyxerr << "LaTeXFeatures::useLayout: maximum level of "
65 << "recursion attained by layout "
66 << layoutname << endl;
70 LyXTextClass const & tclass = params.getLyXTextClass();
71 if (tclass.hasLayout(layoutname)) {
72 // Is this layout already in usedLayouts?
73 list<string>::const_iterator cit = usedLayouts.begin();
74 list<string>::const_iterator end = usedLayouts.end();
75 for (; cit != end; ++cit) {
76 if (layoutname == *cit)
80 LyXLayout_ptr const & lyt = tclass[layoutname];
81 if (!lyt->depends_on().empty()) {
83 useLayout(lyt->depends_on());
86 usedLayouts.push_back(layoutname);
88 lyxerr << "LaTeXFeatures::useLayout: layout `"
89 << layoutname << "' does not exist in this class"
97 bool LaTeXFeatures::isRequired(string const & name) const
99 return find(features.begin(), features.end(), name) != features.end();
103 void LaTeXFeatures::addExternalPreamble(string const & pream)
105 externalPreambles += pream;
109 void LaTeXFeatures::useFloat(string const & name)
111 usedFloats.insert(name);
112 // We only need float.sty if we use non builtin floats, or if we
113 // use the "H" modifier. This includes modified table and
114 // figure floats. (Lgb)
115 Floating const & fl = params.getLyXTextClass().floats().getType(name);
116 if (!fl.type().empty() && !fl.builtin()) {
122 void LaTeXFeatures::useLanguage(Language const * lang)
124 UsedLanguages.insert(lang);
128 void LaTeXFeatures::includeFile(string const & key, string const & name)
130 IncludedFiles[key] = name;
134 bool LaTeXFeatures::hasLanguages() const
136 return !UsedLanguages.empty();
140 string LaTeXFeatures::getLanguages() const
142 ostringstream languages;
144 for (LanguageList::const_iterator cit =
145 UsedLanguages.begin();
146 cit != UsedLanguages.end();
148 languages << (*cit)->babel() << ',';
150 return STRCONV(languages.str());
154 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding) const
156 set<string> encodings;
157 for (LanguageList::const_iterator it =
158 UsedLanguages.begin();
159 it != UsedLanguages.end(); ++it)
160 if ((*it)->encoding()->LatexName() != doc_encoding)
161 encodings.insert((*it)->encoding()->LatexName());
167 char const * simplefeatures[] = {
183 int const nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
188 string const LaTeXFeatures::getPackages() const
190 ostringstream packages;
191 LyXTextClass const & tclass = params.getLyXTextClass();
194 // These are all the 'simple' includes. i.e
195 // packages which we just \usepackage{package}
197 for (int i = 0; i < nb_simplefeatures; ++i) {
198 if (isRequired(simplefeatures[i]))
199 packages << "\\usepackage{"
200 << simplefeatures[i] << "}\n";
204 // The rest of these packages are somewhat more complicated
208 if (isRequired("amsmath")
209 && !tclass.provides(LyXTextClass::amsmath)
210 && params.use_amsmath != BufferParams::AMS_OFF) {
211 packages << "\\usepackage{amsmath}\n";
215 if (isRequired("color")) {
216 if (params.graphicsDriver == "default")
217 packages << "\\usepackage[usenames]{color}\n";
219 packages << "\\usepackage["
220 << params.graphicsDriver
226 if (isRequired("makeidx")) {
227 if (! tclass.provides(LyXTextClass::makeidx))
228 packages << "\\usepackage{makeidx}\n";
229 packages << "\\makeindex\n";
233 if (isRequired("graphicx") && params.graphicsDriver != "none") {
234 if (params.graphicsDriver == "default")
235 packages << "\\usepackage{graphicx}\n";
237 packages << "\\usepackage["
238 << params.graphicsDriver
243 // packages << "\\usepackage{algorithm}\n";
246 // lyxskak.sty --- newer chess support based on skak.sty
247 if (isRequired("chess")) {
248 packages << "\\usepackage[ps,mover]{lyxskak}\n";
252 if ((params.spacing.getSpace() != Spacing::Single
253 && !params.spacing.isDefault())
254 || isRequired("setspace")) {
255 packages << "\\usepackage{setspace}\n";
257 switch (params.spacing.getSpace()) {
258 case Spacing::Default:
259 case Spacing::Single:
260 // we dont use setspace.sty so dont print anything
261 //packages += "\\singlespacing\n";
263 case Spacing::Onehalf:
264 packages << "\\onehalfspacing\n";
266 case Spacing::Double:
267 packages << "\\doublespacing\n";
270 packages << "\\setstretch{"
271 << params.spacing.getValue() << "}\n";
276 if (isRequired("amssymb") || params.use_amsmath == BufferParams::AMS_ON)
277 packages << "\\usepackage{amssymb}\n";
279 if (isRequired("url") && ! tclass.provides(LyXTextClass::url))
280 packages << "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
281 " {\\newcommand{\\url}{\\texttt}}\n";
285 if (isRequired("natbib") && ! tclass.provides(LyXTextClass::natbib)) {
286 packages << "\\usepackage[";
287 if (params.use_numerical_citations) {
288 packages << "numbers";
290 packages << "authoryear";
292 packages << "]{natbib}\n";
295 packages << externalPreambles;
297 return STRCONV(packages.str());
301 string const LaTeXFeatures::getMacros() const
303 ostringstream macros;
305 if (isRequired("LyX"))
306 macros << lyx_def << '\n';
308 if (isRequired("lyxline"))
309 macros << lyxline_def << '\n';
311 if (isRequired("noun"))
312 macros << noun_def << '\n';
314 if (isRequired("lyxarrow"))
315 macros << lyxarrow_def << '\n';
318 if (isRequired("quotesinglbase"))
319 macros << quotesinglbase_def << '\n';
320 if (isRequired("quotedblbase"))
321 macros << quotedblbase_def << '\n';
322 if (isRequired("guilsinglleft"))
323 macros << guilsinglleft_def << '\n';
324 if (isRequired("guilsinglright"))
325 macros << guilsinglright_def << '\n';
326 if (isRequired("guillemotleft"))
327 macros << guillemotleft_def << '\n';
328 if (isRequired("guillemotright"))
329 macros << guillemotright_def << '\n';
332 if (isRequired("boldsymbol") && !isRequired("amsmath"))
333 macros << boldsymbol_def << '\n';
334 if (isRequired("binom") && !isRequired("amsmath"))
335 macros << binom_def << '\n';
336 if (isRequired("mathcircumflex"))
337 macros << mathcircumflex_def << '\n';
340 if (isRequired("NeedLyXMinipageIndent"))
341 macros << minipageindent_def;
342 if (isRequired("ParagraphLeftIndent"))
343 macros << paragraphleftindent_def;
344 if (isRequired("NeedLyXFootnoteCode"))
345 macros << floatingfootnote_def;
347 // some problems with tex->html converters
348 if (isRequired("NeedTabularnewline"))
349 macros << tabularnewline_def;
352 getFloatDefinitions(macros);
354 return STRCONV(macros.str());
358 string const LaTeXFeatures::getBabelOptions() const
362 for (LanguageList::const_iterator cit = UsedLanguages.begin();
363 cit != UsedLanguages.end(); ++cit)
364 if (!(*cit)->latex_options().empty())
365 tmp << (*cit)->latex_options() << '\n';
366 if (!params.language->latex_options().empty())
367 tmp << params.language->latex_options() << '\n';
369 return STRCONV(tmp.str());
373 string const LaTeXFeatures::getTClassPreamble() const
375 // the text class specific preamble
376 LyXTextClass const & tclass = params.getLyXTextClass();
377 ostringstream tcpreamble;
379 tcpreamble << tclass.preamble();
381 list<string>::const_iterator cit = usedLayouts.begin();
382 list<string>::const_iterator end = usedLayouts.end();
383 for (; cit != end; ++cit) {
384 tcpreamble << tclass[*cit]->preamble();
387 return STRCONV(tcpreamble.str());
391 string const LaTeXFeatures::getLyXSGMLEntities() const
393 // Definition of entities used in the document that are LyX related.
394 ostringstream entities;
396 if (isRequired("lyxarrow")) {
397 entities << "<!ENTITY lyxarrow \"->\">" << '\n';
400 return STRCONV(entities.str());
404 string const LaTeXFeatures::getIncludedFiles(string const & fname) const
406 ostringstream sgmlpreamble;
407 string const basename = OnlyPath(fname);
409 FileMap::const_iterator end = IncludedFiles.end();
410 for (FileMap::const_iterator fi = IncludedFiles.begin();
412 sgmlpreamble << "\n<!ENTITY " << fi->first
413 << (IsSGMLFilename(fi->second) ? " SYSTEM \"" : " \"")
414 << MakeRelPath(fi->second, basename) << "\">";
416 return STRCONV(sgmlpreamble.str());
420 void LaTeXFeatures::showStruct() const {
421 lyxerr << "LyX needs the following commands when LaTeXing:"
422 << "\n***** Packages:" << getPackages()
423 << "\n***** Macros:" << getMacros()
424 << "\n***** Textclass stuff:" << getTClassPreamble()
425 << "\n***** done." << endl;
429 BufferParams const & LaTeXFeatures::bufferParams() const
435 void LaTeXFeatures::getFloatDefinitions(ostream & os) const
437 FloatList const & floats = params.getLyXTextClass().floats();
439 // Here we will output the code to create the needed float styles.
440 // We will try to do this as minimal as possible.
441 // \floatstyle{ruled}
442 // \newfloat{algorithm}{htbp}{loa}
443 // \floatname{algorithm}{Algorithm}
444 UsedFloats::const_iterator cit = usedFloats.begin();
445 UsedFloats::const_iterator end = usedFloats.end();
446 // ostringstream floats;
447 for (; cit != end; ++cit) {
448 Floating const & fl = floats.getType((*cit));
450 // For builtin floats we do nothing.
451 if (fl.builtin()) continue;
453 // We have to special case "table" and "figure"
454 if (fl.type() == "tabular" || fl.type() == "figure") {
455 // Output code to modify "table" or "figure"
456 // but only if builtin == false
457 // and that have to be true at this point in the
459 string const type = fl.type();
460 string const placement = fl.placement();
461 string const style = fl.style();
462 if (!style.empty()) {
463 os << "\\floatstyle{" << style << "}\n"
464 << "\\restylefloat{" << type << "}\n";
466 if (!placement.empty()) {
467 os << "\\floatplacement{" << type << "}{"
468 << placement << "}\n";
471 // The other non builtin floats.
473 string const type = fl.type();
474 string const placement = fl.placement();
475 string const ext = fl.ext();
476 string const within = fl.within();
477 string const style = fl.style();
478 string const name = fl.name();
479 os << "\\floatstyle{" << style << "}\n"
480 << "\\newfloat{" << type << "}{" << placement
481 << "}{" << ext << '}';
483 os << '[' << within << ']';
485 << "\\floatname{" << type << "}{"
488 // What missing here is to code to minimalize the code
489 // output so that the same floatstyle will not be
490 // used several times, when the same style is still in