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::layout_type;
32 using lyx::textclass_type;
37 LaTeXFeatures::LaTeXFeatures(BufferParams const & p, layout_type n)
38 : layout(n, false), params(p)
42 void LaTeXFeatures::require(string const & name)
47 // INSET_GRAPHICS: remove this when InsetFig is thrown.
48 if (name == "graphics") {
49 features.push_back("graphicx");
50 features.push_back("graphics");
52 features.push_back(name);
56 void LaTeXFeatures::useLayout(std::vector<bool>::size_type const & idx)
62 bool LaTeXFeatures::isRequired(string const & name) const
64 FeaturesList::const_iterator i = std::find(features.begin(),
67 return i != features.end();
71 void LaTeXFeatures::addExternalPreamble(string const & pream)
73 externalPreambles += pream;
77 void LaTeXFeatures::useFloat(string const & name)
79 usedFloats.insert(name);
80 // We only need float.sty if we use non builtin floats, or if we
81 // use the "H" modifier. This includes modified table and
82 // figure floats. (Lgb)
83 Floating const & fl = floatList.getType(name);
84 if (!fl.type().empty() && !fl.builtin()) {
90 void LaTeXFeatures::useLanguage(Language const * lang)
92 UsedLanguages.insert(lang);
96 void LaTeXFeatures::includeFile(string const & key, string const & name)
98 IncludedFiles[key] = name;
102 bool LaTeXFeatures::hasLanguages()
104 return !UsedLanguages.empty();
108 string LaTeXFeatures::getLanguages() const
110 ostringstream languages;
112 for (LanguageList::const_iterator cit =
113 UsedLanguages.begin();
114 cit != UsedLanguages.end();
116 languages << (*cit)->babel() << ',';
118 return languages.str().c_str();
122 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding)
124 set<string> encodings;
125 for (LanguageList::const_iterator it =
126 UsedLanguages.begin();
127 it != UsedLanguages.end(); ++it)
128 if ((*it)->encoding()->LatexName() != doc_encoding)
129 encodings.insert((*it)->encoding()->LatexName());
135 char const * simplefeatures[] = {
149 const int nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
153 string const LaTeXFeatures::getPackages() const
155 ostringstream packages;
156 LyXTextClass const & tclass =
157 textclasslist.TextClass(params.textclass);
161 * These are all the 'simple' includes. i.e
162 * packages which we just \usepackage{package}
164 for (int i = 0 ; i < nb_simplefeatures ; ++i) {
165 if (isRequired(simplefeatures[i]))
166 packages << "\\usepackage{"
172 * The rest of these packages are somewhat more complicated
176 if (isRequired("amsmath")
177 && ! tclass.provides(LyXTextClass::amsmath)) {
178 packages << "\\usepackage{amsmath}\n";
182 if (isRequired("color")) {
183 if (params.graphicsDriver == "default")
184 packages << "\\usepackage{color}\n";
186 packages << "\\usepackage["
187 << params.graphicsDriver
192 if (isRequired("makeidx")) {
193 if (! tclass.provides(LyXTextClass::makeidx))
194 packages << "\\usepackage{makeidx}\n";
195 packages << "\\makeindex\n";
199 if (isRequired("graphicx") && params.graphicsDriver != "none") {
200 if (params.graphicsDriver == "default")
201 packages << "\\usepackage{graphicx}\n";
203 packages << "\\usepackage["
204 << params.graphicsDriver
208 // INSET_GRAPHICS: remove this when InsetFig is thrown.
210 if (isRequired("graphics") && params.graphicsDriver != "none") {
211 if (params.graphicsDriver == "default")
212 packages << "\\usepackage{graphics}\n";
214 packages << "\\usepackage["
215 << params.graphicsDriver
220 // packages << "\\usepackage{algorithm}\n";
223 // lyxskak.sty --- newer chess support based on skak.sty
224 if (isRequired("chess")) {
225 packages << "\\usepackage[ps,mover]{lyxskak}\n";
229 if ((params.spacing.getSpace() != Spacing::Single
230 && !params.spacing.isDefault())
231 || isRequired("setspace")) {
232 packages << "\\usepackage{setspace}\n";
234 switch (params.spacing.getSpace()) {
235 case Spacing::Default:
236 case Spacing::Single:
237 // we dont use setspace.sty so dont print anything
238 //packages += "\\singlespacing\n";
240 case Spacing::Onehalf:
241 packages << "\\onehalfspacing\n";
243 case Spacing::Double:
244 packages << "\\doublespacing\n";
247 packages << "\\setstretch{"
248 << params.spacing.getValue() << "}\n";
253 if (isRequired("amssymb") || params.use_amsmath)
254 packages << "\\usepackage{amssymb}\n";
256 if (isRequired("url") && ! tclass.provides(LyXTextClass::url))
257 packages << "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
258 " {\\newcommand{\\url}{\\texttt}}\n";
262 if (isRequired("natbib")) {
263 packages << "\\usepackage[";
264 if (params.use_numerical_citations) {
265 packages << "numbers";
267 packages << "authoryear";
269 packages << "]{natbib}\n";
272 packages << externalPreambles;
274 return packages.str().c_str();
278 string const LaTeXFeatures::getMacros() const
280 ostringstream macros;
282 // always include this
283 if (true || isRequired("lyx"))
284 macros << lyx_def << '\n';
286 if (isRequired("lyxline"))
287 macros << lyxline_def << '\n';
289 if (isRequired("noun"))
290 macros << noun_def << '\n';
292 if (isRequired("lyxarrow"))
293 macros << lyxarrow_def << '\n';
296 if (isRequired("quotesinglbase"))
297 macros << quotesinglbase_def << '\n';
298 if (isRequired("quotedblbase"))
299 macros << quotedblbase_def << '\n';
300 if (isRequired("guilsinglleft"))
301 macros << guilsinglleft_def << '\n';
302 if (isRequired("guilsinglright"))
303 macros << guilsinglright_def << '\n';
304 if (isRequired("guillemotleft"))
305 macros << guillemotleft_def << '\n';
306 if (isRequired("guillemotright"))
307 macros << guillemotright_def << '\n';
310 if (isRequired("boldsymbol") && !isRequired("amsmath"))
311 macros << boldsymbol_def << '\n';
312 if (isRequired("binom") && !isRequired("amsmath"))
313 macros << binom_def << '\n';
316 if (isRequired("NeedLyXMinipageIndent"))
317 macros << minipageindent_def;
318 if (isRequired("ParagraphIndent"))
319 macros << paragraphindent_def;
320 if (isRequired("NeedLyXFootnoteCode"))
321 macros << floatingfootnote_def;
324 getFloatDefinitions(macros);
326 for (LanguageList::const_iterator cit = UsedLanguages.begin();
327 cit != UsedLanguages.end(); ++cit)
328 if (!(*cit)->latex_options().empty())
329 macros << (*cit)->latex_options() << '\n';
330 if (!params.language->latex_options().empty())
331 macros << params.language->latex_options() << '\n';
333 return macros.str().c_str();
337 string const LaTeXFeatures::getTClassPreamble() const
339 // the text class specific preamble
340 LyXTextClass const & tclass = textclasslist.TextClass(params.textclass);
341 ostringstream tcpreamble;
343 tcpreamble << tclass.preamble();
345 for (layout_type i = 0; i < tclass.numLayouts(); ++i) {
347 tcpreamble << tclass[i].preamble();
351 return tcpreamble.str().c_str();
355 string const LaTeXFeatures::getLyXSGMLEntities() const
357 // Definition of entities used in the document that are LyX related.
358 ostringstream entities;
360 if (isRequired("lyxarrow")) {
361 entities << "<!ENTITY lyxarrow \"->\">" << '\n';
364 return entities.str().c_str();
368 string const LaTeXFeatures::getIncludedFiles(string const & fname) const
370 ostringstream sgmlpreamble;
371 string const basename = OnlyPath(fname);
373 FileMap::const_iterator end = IncludedFiles.end();
374 for (FileMap::const_iterator fi = IncludedFiles.begin();
376 sgmlpreamble << "\n<!ENTITY " << fi->first
377 << (IsSGMLFilename(fi->second) ? " SYSTEM \"" : " \"" )
378 << MakeRelPath(fi->second, basename) << "\">";
380 return sgmlpreamble.str().c_str();
384 void LaTeXFeatures::showStruct() const {
385 lyxerr << "LyX needs the following commands when LaTeXing:"
386 << "\n***** Packages:" << getPackages()
387 << "\n***** Macros:" << getMacros()
388 << "\n***** Textclass stuff:" << getTClassPreamble()
389 << "\n***** done." << endl;
393 BufferParams const & LaTeXFeatures::bufferParams() const
398 void LaTeXFeatures::getFloatDefinitions(std::ostream & os) const
400 // Here we will output the code to create the needed float styles.
401 // We will try to do this as minimal as possible.
402 // \floatstyle{ruled}
403 // \newfloat{algorithm}{htbp}{loa}
404 // \floatname{algorithm}{Algorithm}
405 UsedFloats::const_iterator cit = usedFloats.begin();
406 UsedFloats::const_iterator end = usedFloats.end();
407 // ostringstream floats;
408 for (; cit != end; ++cit) {
409 Floating const & fl = floatList.getType((*cit));
411 // For builtin floats we do nothing.
412 if (fl.builtin()) continue;
414 // We have to special case "table" and "figure"
415 if (fl.type() == "tabular" || fl.type() == "figure") {
416 // Output code to modify "table" or "figure"
417 // but only if builtin == false
418 // and that have to be true at this point in the
420 string const type = fl.type();
421 string const placement = fl.placement();
422 string const style = fl.style();
423 if (!style.empty()) {
424 os << "\\floatstyle{" << style << "}\n"
425 << "\\restylefloat{" << type << "}\n";
427 if (!placement.empty()) {
428 os << "\\floatplacement{" << type << "}{"
429 << placement << "}\n";
432 // The other non builtin floats.
434 string const type = fl.type();
435 string const placement = fl.placement();
436 string const ext = fl.ext();
437 string const within = fl.within();
438 string const style = fl.style();
439 string const name = fl.name();
440 os << "\\floatstyle{" << style << "}\n"
441 << "\\newfloat{" << type << "}{" << placement
442 << "}{" << ext << "}";
444 os << "[" << within << "]";
446 << "\\floatname{" << type << "}{"
449 // What missing here is to code to minimalize the code
450 // output so that the same floatstyle will not be
451 // used several times, when the same style is still in