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 && params.use_amsmath != BufferParams::AMS_OFF) {
202 packages << "\\usepackage{amsmath}\n";
206 if (isRequired("color")) {
207 if (params.graphicsDriver == "default")
208 packages << "\\usepackage[usenames]{color}\n";
210 packages << "\\usepackage["
211 << params.graphicsDriver
217 if (isRequired("makeidx")) {
218 if (! tclass.provides(LyXTextClass::makeidx))
219 packages << "\\usepackage{makeidx}\n";
220 packages << "\\makeindex\n";
224 if (isRequired("graphicx") && params.graphicsDriver != "none") {
225 if (params.graphicsDriver == "default")
226 packages << "\\usepackage{graphicx}\n";
228 packages << "\\usepackage["
229 << params.graphicsDriver
234 // packages << "\\usepackage{algorithm}\n";
237 // lyxskak.sty --- newer chess support based on skak.sty
238 if (isRequired("chess")) {
239 packages << "\\usepackage[ps,mover]{lyxskak}\n";
243 if ((params.spacing.getSpace() != Spacing::Single
244 && !params.spacing.isDefault())
245 || isRequired("setspace")) {
246 packages << "\\usepackage{setspace}\n";
248 switch (params.spacing.getSpace()) {
249 case Spacing::Default:
250 case Spacing::Single:
251 // we dont use setspace.sty so dont print anything
252 //packages += "\\singlespacing\n";
254 case Spacing::Onehalf:
255 packages << "\\onehalfspacing\n";
257 case Spacing::Double:
258 packages << "\\doublespacing\n";
261 packages << "\\setstretch{"
262 << params.spacing.getValue() << "}\n";
267 if (isRequired("amssymb") || params.use_amsmath == BufferParams::AMS_ON)
268 packages << "\\usepackage{amssymb}\n";
270 if (isRequired("url") && ! tclass.provides(LyXTextClass::url))
271 packages << "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
272 " {\\newcommand{\\url}{\\texttt}}\n";
276 if (isRequired("natbib") && ! tclass.provides(LyXTextClass::natbib)) {
277 packages << "\\usepackage[";
278 if (params.use_numerical_citations) {
279 packages << "numbers";
281 packages << "authoryear";
283 packages << "]{natbib}\n";
286 packages << externalPreambles;
288 return STRCONV(packages.str());
292 string const LaTeXFeatures::getMacros() const
294 ostringstream macros;
296 if (isRequired("LyX"))
297 macros << lyx_def << '\n';
299 if (isRequired("lyxline"))
300 macros << lyxline_def << '\n';
302 if (isRequired("noun"))
303 macros << noun_def << '\n';
305 if (isRequired("lyxarrow"))
306 macros << lyxarrow_def << '\n';
309 if (isRequired("quotesinglbase"))
310 macros << quotesinglbase_def << '\n';
311 if (isRequired("quotedblbase"))
312 macros << quotedblbase_def << '\n';
313 if (isRequired("guilsinglleft"))
314 macros << guilsinglleft_def << '\n';
315 if (isRequired("guilsinglright"))
316 macros << guilsinglright_def << '\n';
317 if (isRequired("guillemotleft"))
318 macros << guillemotleft_def << '\n';
319 if (isRequired("guillemotright"))
320 macros << guillemotright_def << '\n';
323 if (isRequired("boldsymbol") && !isRequired("amsmath"))
324 macros << boldsymbol_def << '\n';
325 if (isRequired("binom") && !isRequired("amsmath"))
326 macros << binom_def << '\n';
327 if (isRequired("mathcircumflex"))
328 macros << mathcircumflex_def << '\n';
331 if (isRequired("NeedLyXMinipageIndent"))
332 macros << minipageindent_def;
333 if (isRequired("ParagraphLeftIndent"))
334 macros << paragraphleftindent_def;
335 if (isRequired("NeedLyXFootnoteCode"))
336 macros << floatingfootnote_def;
338 // some problems with tex->html converters
339 if (isRequired("NeedTabularnewline"))
340 macros << tabularnewline_def;
343 getFloatDefinitions(macros);
345 return STRCONV(macros.str());
349 string const LaTeXFeatures::getBabelOptions() const
353 for (LanguageList::const_iterator cit = UsedLanguages.begin();
354 cit != UsedLanguages.end(); ++cit)
355 if (!(*cit)->latex_options().empty())
356 tmp << (*cit)->latex_options() << '\n';
357 if (!params.language->latex_options().empty())
358 tmp << params.language->latex_options() << '\n';
360 return STRCONV(tmp.str());
364 string const LaTeXFeatures::getTClassPreamble() const
366 // the text class specific preamble
367 LyXTextClass const & tclass = params.getLyXTextClass();
368 ostringstream tcpreamble;
370 tcpreamble << tclass.preamble();
372 list<string>::const_iterator cit = usedLayouts.begin();
373 list<string>::const_iterator end = usedLayouts.end();
374 for (; cit != end; ++cit) {
375 tcpreamble << tclass[*cit]->preamble();
378 return STRCONV(tcpreamble.str());
382 string const LaTeXFeatures::getLyXSGMLEntities() const
384 // Definition of entities used in the document that are LyX related.
385 ostringstream entities;
387 if (isRequired("lyxarrow")) {
388 entities << "<!ENTITY lyxarrow \"->\">" << '\n';
391 return STRCONV(entities.str());
395 string const LaTeXFeatures::getIncludedFiles(string const & fname) const
397 ostringstream sgmlpreamble;
398 string const basename = OnlyPath(fname);
400 FileMap::const_iterator end = IncludedFiles.end();
401 for (FileMap::const_iterator fi = IncludedFiles.begin();
403 sgmlpreamble << "\n<!ENTITY " << fi->first
404 << (IsSGMLFilename(fi->second) ? " SYSTEM \"" : " \"")
405 << MakeRelPath(fi->second, basename) << "\">";
407 return STRCONV(sgmlpreamble.str());
411 void LaTeXFeatures::showStruct() const {
412 lyxerr << "LyX needs the following commands when LaTeXing:"
413 << "\n***** Packages:" << getPackages()
414 << "\n***** Macros:" << getMacros()
415 << "\n***** Textclass stuff:" << getTClassPreamble()
416 << "\n***** done." << endl;
420 BufferParams const & LaTeXFeatures::bufferParams() const
426 void LaTeXFeatures::getFloatDefinitions(ostream & os) const
428 FloatList const & floats = params.getLyXTextClass().floats();
430 // Here we will output the code to create the needed float styles.
431 // We will try to do this as minimal as possible.
432 // \floatstyle{ruled}
433 // \newfloat{algorithm}{htbp}{loa}
434 // \floatname{algorithm}{Algorithm}
435 UsedFloats::const_iterator cit = usedFloats.begin();
436 UsedFloats::const_iterator end = usedFloats.end();
437 // ostringstream floats;
438 for (; cit != end; ++cit) {
439 Floating const & fl = floats.getType((*cit));
441 // For builtin floats we do nothing.
442 if (fl.builtin()) continue;
444 // We have to special case "table" and "figure"
445 if (fl.type() == "tabular" || fl.type() == "figure") {
446 // Output code to modify "table" or "figure"
447 // but only if builtin == false
448 // and that have to be true at this point in the
450 string const type = fl.type();
451 string const placement = fl.placement();
452 string const style = fl.style();
453 if (!style.empty()) {
454 os << "\\floatstyle{" << style << "}\n"
455 << "\\restylefloat{" << type << "}\n";
457 if (!placement.empty()) {
458 os << "\\floatplacement{" << type << "}{"
459 << placement << "}\n";
462 // The other non builtin floats.
464 string const type = fl.type();
465 string const placement = fl.placement();
466 string const ext = fl.ext();
467 string const within = fl.within();
468 string const style = fl.style();
469 string const name = fl.name();
470 os << "\\floatstyle{" << style << "}\n"
471 << "\\newfloat{" << type << "}{" << placement
472 << "}{" << ext << '}';
474 os << '[' << within << ']';
476 << "\\floatname{" << type << "}{"
479 // What missing here is to code to minimalize the code
480 // output so that the same floatstyle will not be
481 // used several times, when the same style is still in