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 void LaTeXFeatures::require(string const & name)
46 features.push_back(name);
50 void LaTeXFeatures::useLayout(string const & layoutname)
52 // Some code to avoid loops in dependency definition
54 const int maxlevel = 30;
55 if (level > maxlevel) {
56 lyxerr << "LaTeXFeatures::useLayout: maximum level of "
57 << "recursion attained by layout "
58 << layoutname << endl;
62 LyXTextClass const & tclass = params.getLyXTextClass();
63 if (tclass.hasLayout(layoutname)) {
64 // Is this layout already in usedLayouts?
65 list<string>::const_iterator cit = usedLayouts.begin();
66 list<string>::const_iterator end = usedLayouts.end();
67 for (; cit != end; ++cit) {
68 if (layoutname == *cit)
72 LyXLayout_ptr const & lyt = tclass[layoutname];
73 if (!lyt->depends_on().empty()) {
75 useLayout(lyt->depends_on());
78 usedLayouts.push_back(layoutname);
80 lyxerr << "LaTeXFeatures::useLayout: layout `"
81 << layoutname << "' does not exist in this class"
89 bool LaTeXFeatures::isRequired(string const & name) const
91 return find(features.begin(), features.end(), name) != features.end();
95 void LaTeXFeatures::addExternalPreamble(string const & pream)
97 externalPreambles += pream;
101 void LaTeXFeatures::useFloat(string const & name)
103 usedFloats.insert(name);
104 // We only need float.sty if we use non builtin floats, or if we
105 // use the "H" modifier. This includes modified table and
106 // figure floats. (Lgb)
107 Floating const & fl = params.getLyXTextClass().floats().getType(name);
108 if (!fl.type().empty() && !fl.builtin()) {
114 void LaTeXFeatures::useLanguage(Language const * lang)
116 UsedLanguages.insert(lang);
120 void LaTeXFeatures::includeFile(string const & key, string const & name)
122 IncludedFiles[key] = name;
126 bool LaTeXFeatures::hasLanguages() const
128 return !UsedLanguages.empty();
132 string LaTeXFeatures::getLanguages() const
134 ostringstream languages;
136 for (LanguageList::const_iterator cit =
137 UsedLanguages.begin();
138 cit != UsedLanguages.end();
140 languages << (*cit)->babel() << ',';
142 return STRCONV(languages.str());
146 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding) const
148 set<string> encodings;
149 for (LanguageList::const_iterator it =
150 UsedLanguages.begin();
151 it != UsedLanguages.end(); ++it)
152 if ((*it)->encoding()->LatexName() != doc_encoding)
153 encodings.insert((*it)->encoding()->LatexName());
159 char const * simplefeatures[] = {
175 int const nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
180 string const LaTeXFeatures::getPackages() const
182 ostringstream packages;
183 LyXTextClass const & tclass = params.getLyXTextClass();
186 // These are all the 'simple' includes. i.e
187 // packages which we just \usepackage{package}
189 for (int i = 0; i < nb_simplefeatures; ++i) {
190 if (isRequired(simplefeatures[i]))
191 packages << "\\usepackage{"
192 << simplefeatures[i] << "}\n";
196 // The rest of these packages are somewhat more complicated
200 if (isRequired("amsmath")
201 && !tclass.provides(LyXTextClass::amsmath)
202 && params.use_amsmath != BufferParams::AMS_OFF) {
203 packages << "\\usepackage{amsmath}\n";
207 if (isRequired("color")) {
208 if (params.graphicsDriver == "default")
209 packages << "\\usepackage[usenames]{color}\n";
211 packages << "\\usepackage["
212 << params.graphicsDriver
218 if (isRequired("makeidx")) {
219 if (! tclass.provides(LyXTextClass::makeidx))
220 packages << "\\usepackage{makeidx}\n";
221 packages << "\\makeindex\n";
225 if (isRequired("graphicx") && params.graphicsDriver != "none") {
226 if (params.graphicsDriver == "default")
227 packages << "\\usepackage{graphicx}\n";
229 packages << "\\usepackage["
230 << params.graphicsDriver
235 // packages << "\\usepackage{algorithm}\n";
238 // lyxskak.sty --- newer chess support based on skak.sty
239 if (isRequired("chess")) {
240 packages << "\\usepackage[ps,mover]{lyxskak}\n";
244 if ((params.spacing.getSpace() != Spacing::Single
245 && !params.spacing.isDefault())
246 || isRequired("setspace")) {
247 packages << "\\usepackage{setspace}\n";
249 switch (params.spacing.getSpace()) {
250 case Spacing::Default:
251 case Spacing::Single:
252 // we dont use setspace.sty so dont print anything
253 //packages += "\\singlespacing\n";
255 case Spacing::Onehalf:
256 packages << "\\onehalfspacing\n";
258 case Spacing::Double:
259 packages << "\\doublespacing\n";
262 packages << "\\setstretch{"
263 << params.spacing.getValue() << "}\n";
268 if (isRequired("amssymb") || params.use_amsmath == BufferParams::AMS_ON)
269 packages << "\\usepackage{amssymb}\n";
271 if (isRequired("url") && ! tclass.provides(LyXTextClass::url))
272 packages << "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
273 " {\\newcommand{\\url}{\\texttt}}\n";
277 if (isRequired("natbib") && ! tclass.provides(LyXTextClass::natbib)) {
278 packages << "\\usepackage[";
279 if (params.use_numerical_citations) {
280 packages << "numbers";
282 packages << "authoryear";
284 packages << "]{natbib}\n";
287 packages << externalPreambles;
289 return STRCONV(packages.str());
293 string const LaTeXFeatures::getMacros() const
295 ostringstream macros;
297 if (isRequired("LyX"))
298 macros << lyx_def << '\n';
300 if (isRequired("lyxline"))
301 macros << lyxline_def << '\n';
303 if (isRequired("noun"))
304 macros << noun_def << '\n';
306 if (isRequired("lyxarrow"))
307 macros << lyxarrow_def << '\n';
310 if (isRequired("quotesinglbase"))
311 macros << quotesinglbase_def << '\n';
312 if (isRequired("quotedblbase"))
313 macros << quotedblbase_def << '\n';
314 if (isRequired("guilsinglleft"))
315 macros << guilsinglleft_def << '\n';
316 if (isRequired("guilsinglright"))
317 macros << guilsinglright_def << '\n';
318 if (isRequired("guillemotleft"))
319 macros << guillemotleft_def << '\n';
320 if (isRequired("guillemotright"))
321 macros << guillemotright_def << '\n';
324 if (isRequired("boldsymbol") && !isRequired("amsmath"))
325 macros << boldsymbol_def << '\n';
326 if (isRequired("binom") && !isRequired("amsmath"))
327 macros << binom_def << '\n';
328 if (isRequired("mathcircumflex"))
329 macros << mathcircumflex_def << '\n';
332 if (isRequired("NeedLyXMinipageIndent"))
333 macros << minipageindent_def;
334 if (isRequired("ParagraphLeftIndent"))
335 macros << paragraphleftindent_def;
336 if (isRequired("NeedLyXFootnoteCode"))
337 macros << floatingfootnote_def;
339 // some problems with tex->html converters
340 if (isRequired("NeedTabularnewline"))
341 macros << tabularnewline_def;
344 getFloatDefinitions(macros);
346 return STRCONV(macros.str());
350 string const LaTeXFeatures::getBabelOptions() const
354 for (LanguageList::const_iterator cit = UsedLanguages.begin();
355 cit != UsedLanguages.end(); ++cit)
356 if (!(*cit)->latex_options().empty())
357 tmp << (*cit)->latex_options() << '\n';
358 if (!params.language->latex_options().empty())
359 tmp << params.language->latex_options() << '\n';
361 return STRCONV(tmp.str());
365 string const LaTeXFeatures::getTClassPreamble() const
367 // the text class specific preamble
368 LyXTextClass const & tclass = params.getLyXTextClass();
369 ostringstream tcpreamble;
371 tcpreamble << tclass.preamble();
373 list<string>::const_iterator cit = usedLayouts.begin();
374 list<string>::const_iterator end = usedLayouts.end();
375 for (; cit != end; ++cit) {
376 tcpreamble << tclass[*cit]->preamble();
379 return STRCONV(tcpreamble.str());
383 string const LaTeXFeatures::getLyXSGMLEntities() const
385 // Definition of entities used in the document that are LyX related.
386 ostringstream entities;
388 if (isRequired("lyxarrow")) {
389 entities << "<!ENTITY lyxarrow \"->\">" << '\n';
392 return STRCONV(entities.str());
396 string const LaTeXFeatures::getIncludedFiles(string const & fname) const
398 ostringstream sgmlpreamble;
399 string const basename = OnlyPath(fname);
401 FileMap::const_iterator end = IncludedFiles.end();
402 for (FileMap::const_iterator fi = IncludedFiles.begin();
404 sgmlpreamble << "\n<!ENTITY " << fi->first
405 << (IsSGMLFilename(fi->second) ? " SYSTEM \"" : " \"")
406 << MakeRelPath(fi->second, basename) << "\">";
408 return STRCONV(sgmlpreamble.str());
412 void LaTeXFeatures::showStruct() const {
413 lyxerr << "LyX needs the following commands when LaTeXing:"
414 << "\n***** Packages:" << getPackages()
415 << "\n***** Macros:" << getMacros()
416 << "\n***** Textclass stuff:" << getTClassPreamble()
417 << "\n***** done." << endl;
421 BufferParams const & LaTeXFeatures::bufferParams() const
427 void LaTeXFeatures::getFloatDefinitions(ostream & os) const
429 FloatList const & floats = params.getLyXTextClass().floats();
431 // Here we will output the code to create the needed float styles.
432 // We will try to do this as minimal as possible.
433 // \floatstyle{ruled}
434 // \newfloat{algorithm}{htbp}{loa}
435 // \floatname{algorithm}{Algorithm}
436 UsedFloats::const_iterator cit = usedFloats.begin();
437 UsedFloats::const_iterator end = usedFloats.end();
438 // ostringstream floats;
439 for (; cit != end; ++cit) {
440 Floating const & fl = floats.getType((*cit));
442 // For builtin floats we do nothing.
443 if (fl.builtin()) continue;
445 // We have to special case "table" and "figure"
446 if (fl.type() == "tabular" || fl.type() == "figure") {
447 // Output code to modify "table" or "figure"
448 // but only if builtin == false
449 // and that have to be true at this point in the
451 string const type = fl.type();
452 string const placement = fl.placement();
453 string const style = fl.style();
454 if (!style.empty()) {
455 os << "\\floatstyle{" << style << "}\n"
456 << "\\restylefloat{" << type << "}\n";
458 if (!placement.empty()) {
459 os << "\\floatplacement{" << type << "}{"
460 << placement << "}\n";
463 // The other non builtin floats.
465 string const type = fl.type();
466 string const placement = fl.placement();
467 string const ext = fl.ext();
468 string const within = fl.within();
469 string const style = fl.style();
470 string const name = fl.name();
471 os << "\\floatstyle{" << style << "}\n"
472 << "\\newfloat{" << type << "}{" << placement
473 << "}{" << ext << '}';
475 os << '[' << within << ']';
477 << "\\floatname{" << type << "}{"
480 // What missing here is to code to minimalize the code
481 // output so that the same floatstyle will not be
482 // used several times, when the same style is still in