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 "lyxtextclasslist.h"
23 #include "FloatList.h"
28 #include "support/filetools.h"
29 #include "support/lstrings.h"
31 using lyx::textclass_type;
40 LaTeXFeatures::LaTeXFeatures(BufferParams const & p)
45 void LaTeXFeatures::require(string const & name)
50 features.push_back(name);
54 void LaTeXFeatures::useLayout(string const & lyt)
60 bool LaTeXFeatures::isRequired(string const & name) const
62 FeaturesList::const_iterator i = find(features.begin(),
65 return i != features.end();
69 void LaTeXFeatures::addExternalPreamble(string const & pream)
71 externalPreambles += pream;
75 void LaTeXFeatures::useFloat(string const & name)
77 usedFloats.insert(name);
78 // We only need float.sty if we use non builtin floats, or if we
79 // use the "H" modifier. This includes modified table and
80 // figure floats. (Lgb)
81 Floating const & fl = floatList.getType(name);
82 if (!fl.type().empty() && !fl.builtin()) {
88 void LaTeXFeatures::useLanguage(Language const * lang)
90 UsedLanguages.insert(lang);
94 void LaTeXFeatures::includeFile(string const & key, string const & name)
96 IncludedFiles[key] = name;
100 bool LaTeXFeatures::hasLanguages()
102 return !UsedLanguages.empty();
106 string LaTeXFeatures::getLanguages() const
108 ostringstream languages;
110 for (LanguageList::const_iterator cit =
111 UsedLanguages.begin();
112 cit != UsedLanguages.end();
114 languages << (*cit)->babel() << ',';
116 return languages.str().c_str();
120 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding)
122 set<string> encodings;
123 for (LanguageList::const_iterator it =
124 UsedLanguages.begin();
125 it != UsedLanguages.end(); ++it)
126 if ((*it)->encoding()->LatexName() != doc_encoding)
127 encodings.insert((*it)->encoding()->LatexName());
133 char const * simplefeatures[] = {
148 const int nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
152 string const LaTeXFeatures::getPackages() const
154 ostringstream packages;
155 LyXTextClass const & tclass = textclasslist[params.textclass];
159 // These are all the 'simple' includes. i.e
160 // packages which we just \usepackage{package}
162 for (int i = 0; i < nb_simplefeatures; ++i) {
163 if (isRequired(simplefeatures[i]))
164 packages << "\\usepackage{"
165 << simplefeatures[i] << "}\n";
169 // The rest of these packages are somewhat more complicated
173 if (isRequired("amsmath")
174 && ! tclass.provides(LyXTextClass::amsmath)) {
175 packages << "\\usepackage{amsmath}\n";
179 if (isRequired("color")) {
180 if (params.graphicsDriver == "default")
181 packages << "\\usepackage{color}\n";
183 packages << "\\usepackage["
184 << params.graphicsDriver
189 if (isRequired("makeidx")) {
190 if (! tclass.provides(LyXTextClass::makeidx))
191 packages << "\\usepackage{makeidx}\n";
192 packages << "\\makeindex\n";
196 if (isRequired("graphicx") && params.graphicsDriver != "none") {
197 if (params.graphicsDriver == "default")
198 packages << "\\usepackage{graphicx}\n";
200 packages << "\\usepackage["
201 << params.graphicsDriver
206 // packages << "\\usepackage{algorithm}\n";
209 // lyxskak.sty --- newer chess support based on skak.sty
210 if (isRequired("chess")) {
211 packages << "\\usepackage[ps,mover]{lyxskak}\n";
215 if ((params.spacing.getSpace() != Spacing::Single
216 && !params.spacing.isDefault())
217 || isRequired("setspace")) {
218 packages << "\\usepackage{setspace}\n";
220 switch (params.spacing.getSpace()) {
221 case Spacing::Default:
222 case Spacing::Single:
223 // we dont use setspace.sty so dont print anything
224 //packages += "\\singlespacing\n";
226 case Spacing::Onehalf:
227 packages << "\\onehalfspacing\n";
229 case Spacing::Double:
230 packages << "\\doublespacing\n";
233 packages << "\\setstretch{"
234 << params.spacing.getValue() << "}\n";
239 if (isRequired("amssymb") || params.use_amsmath)
240 packages << "\\usepackage{amssymb}\n";
242 if (isRequired("url") && ! tclass.provides(LyXTextClass::url))
243 packages << "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
244 " {\\newcommand{\\url}{\\texttt}}\n";
248 if (isRequired("natbib") && ! tclass.provides(LyXTextClass::natbib)) {
249 packages << "\\usepackage[";
250 if (params.use_numerical_citations) {
251 packages << "numbers";
253 packages << "authoryear";
255 packages << "]{natbib}\n";
258 packages << externalPreambles;
260 return packages.str().c_str();
264 string const LaTeXFeatures::getMacros() const
266 ostringstream macros;
268 // always include this
269 if (true || isRequired("lyx"))
270 macros << lyx_def << '\n';
272 if (isRequired("lyxline"))
273 macros << lyxline_def << '\n';
275 if (isRequired("noun"))
276 macros << noun_def << '\n';
278 if (isRequired("lyxarrow"))
279 macros << lyxarrow_def << '\n';
282 if (isRequired("quotesinglbase"))
283 macros << quotesinglbase_def << '\n';
284 if (isRequired("quotedblbase"))
285 macros << quotedblbase_def << '\n';
286 if (isRequired("guilsinglleft"))
287 macros << guilsinglleft_def << '\n';
288 if (isRequired("guilsinglright"))
289 macros << guilsinglright_def << '\n';
290 if (isRequired("guillemotleft"))
291 macros << guillemotleft_def << '\n';
292 if (isRequired("guillemotright"))
293 macros << guillemotright_def << '\n';
296 if (isRequired("boldsymbol") && !isRequired("amsmath"))
297 macros << boldsymbol_def << '\n';
298 if (isRequired("binom") && !isRequired("amsmath"))
299 macros << binom_def << '\n';
300 if (isRequired("mathcircumflex"))
301 macros << mathcircumflex_def << '\n';
304 if (isRequired("NeedLyXMinipageIndent"))
305 macros << minipageindent_def;
306 if (isRequired("ParagraphLeftIndent"))
307 macros << paragraphleftindent_def;
308 if (isRequired("NeedLyXFootnoteCode"))
309 macros << floatingfootnote_def;
312 getFloatDefinitions(macros);
314 return macros.str().c_str();
318 string const LaTeXFeatures::getBabelOptions() const
322 for (LanguageList::const_iterator cit = UsedLanguages.begin();
323 cit != UsedLanguages.end(); ++cit)
324 if (!(*cit)->latex_options().empty())
325 tmp << (*cit)->latex_options() << '\n';
326 if (!params.language->latex_options().empty())
327 tmp << params.language->latex_options() << '\n';
329 return tmp.str().c_str();
333 string const LaTeXFeatures::getTClassPreamble() const
335 // the text class specific preamble
336 LyXTextClass const & tclass = textclasslist[params.textclass];
337 ostringstream tcpreamble;
339 tcpreamble << tclass.preamble();
341 set<string>::const_iterator cit = layout.begin();
342 set<string>::const_iterator end = layout.end();
343 for (; cit != end; ++cit) {
344 tcpreamble << tclass[*cit]->preamble();
347 return tcpreamble.str().c_str();
351 string const LaTeXFeatures::getLyXSGMLEntities() const
353 // Definition of entities used in the document that are LyX related.
354 ostringstream entities;
356 if (isRequired("lyxarrow")) {
357 entities << "<!ENTITY lyxarrow \"->\">" << '\n';
360 return entities.str().c_str();
364 string const LaTeXFeatures::getIncludedFiles(string const & fname) const
366 ostringstream sgmlpreamble;
367 string const basename = OnlyPath(fname);
369 FileMap::const_iterator end = IncludedFiles.end();
370 for (FileMap::const_iterator fi = IncludedFiles.begin();
372 sgmlpreamble << "\n<!ENTITY " << fi->first
373 << (IsSGMLFilename(fi->second) ? " SYSTEM \"" : " \"")
374 << MakeRelPath(fi->second, basename) << "\">";
376 return sgmlpreamble.str().c_str();
380 void LaTeXFeatures::showStruct() const {
381 lyxerr << "LyX needs the following commands when LaTeXing:"
382 << "\n***** Packages:" << getPackages()
383 << "\n***** Macros:" << getMacros()
384 << "\n***** Textclass stuff:" << getTClassPreamble()
385 << "\n***** done." << endl;
389 BufferParams const & LaTeXFeatures::bufferParams() const
395 void LaTeXFeatures::getFloatDefinitions(ostream & os) const
397 // Here we will output the code to create the needed float styles.
398 // We will try to do this as minimal as possible.
399 // \floatstyle{ruled}
400 // \newfloat{algorithm}{htbp}{loa}
401 // \floatname{algorithm}{Algorithm}
402 UsedFloats::const_iterator cit = usedFloats.begin();
403 UsedFloats::const_iterator end = usedFloats.end();
404 // ostringstream floats;
405 for (; cit != end; ++cit) {
406 Floating const & fl = floatList.getType((*cit));
408 // For builtin floats we do nothing.
409 if (fl.builtin()) continue;
411 // We have to special case "table" and "figure"
412 if (fl.type() == "tabular" || fl.type() == "figure") {
413 // Output code to modify "table" or "figure"
414 // but only if builtin == false
415 // and that have to be true at this point in the
417 string const type = fl.type();
418 string const placement = fl.placement();
419 string const style = fl.style();
420 if (!style.empty()) {
421 os << "\\floatstyle{" << style << "}\n"
422 << "\\restylefloat{" << type << "}\n";
424 if (!placement.empty()) {
425 os << "\\floatplacement{" << type << "}{"
426 << placement << "}\n";
429 // The other non builtin floats.
431 string const type = fl.type();
432 string const placement = fl.placement();
433 string const ext = fl.ext();
434 string const within = fl.within();
435 string const style = fl.style();
436 string const name = fl.name();
437 os << "\\floatstyle{" << style << "}\n"
438 << "\\newfloat{" << type << "}{" << placement
439 << "}{" << ext << "}";
441 os << "[" << within << "]";
443 << "\\floatname{" << type << "}{"
446 // What missing here is to code to minimalize the code
447 // output so that the same floatstyle will not be
448 // used several times, when the same style is still in