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;
41 LaTeXFeatures::LaTeXFeatures(BufferParams const & p, layout_type n)
42 : layout(n, false), params(p)
46 void LaTeXFeatures::require(string const & name)
51 features.push_back(name);
55 void LaTeXFeatures::useLayout(vector<bool>::size_type const & idx)
61 bool LaTeXFeatures::isRequired(string const & name) const
63 FeaturesList::const_iterator i = find(features.begin(),
66 return i != features.end();
70 void LaTeXFeatures::addExternalPreamble(string const & pream)
72 externalPreambles += pream;
76 void LaTeXFeatures::useFloat(string const & name)
78 usedFloats.insert(name);
79 // We only need float.sty if we use non builtin floats, or if we
80 // use the "H" modifier. This includes modified table and
81 // figure floats. (Lgb)
82 Floating const & fl = floatList.getType(name);
83 if (!fl.type().empty() && !fl.builtin()) {
89 void LaTeXFeatures::useLanguage(Language const * lang)
91 UsedLanguages.insert(lang);
95 void LaTeXFeatures::includeFile(string const & key, string const & name)
97 IncludedFiles[key] = name;
101 bool LaTeXFeatures::hasLanguages()
103 return !UsedLanguages.empty();
107 string LaTeXFeatures::getLanguages() const
109 ostringstream languages;
111 for (LanguageList::const_iterator cit =
112 UsedLanguages.begin();
113 cit != UsedLanguages.end();
115 languages << (*cit)->babel() << ',';
117 return languages.str().c_str();
121 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding)
123 set<string> encodings;
124 for (LanguageList::const_iterator it =
125 UsedLanguages.begin();
126 it != UsedLanguages.end(); ++it)
127 if ((*it)->encoding()->LatexName() != doc_encoding)
128 encodings.insert((*it)->encoding()->LatexName());
134 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 =
156 textclasslist.TextClass(params.textclass);
160 * These are all the 'simple' includes. i.e
161 * packages which we just \usepackage{package}
163 for (int i = 0 ; i < nb_simplefeatures ; ++i) {
164 if (isRequired(simplefeatures[i]))
165 packages << "\\usepackage{"
171 * The rest of these packages are somewhat more complicated
175 if (isRequired("amsmath")
176 && ! tclass.provides(LyXTextClass::amsmath)) {
177 packages << "\\usepackage{amsmath}\n";
181 if (isRequired("color")) {
182 if (params.graphicsDriver == "default")
183 packages << "\\usepackage{color}\n";
185 packages << "\\usepackage["
186 << params.graphicsDriver
191 if (isRequired("makeidx")) {
192 if (! tclass.provides(LyXTextClass::makeidx))
193 packages << "\\usepackage{makeidx}\n";
194 packages << "\\makeindex\n";
198 if (isRequired("graphicx") && params.graphicsDriver != "none") {
199 if (params.graphicsDriver == "default")
200 packages << "\\usepackage{graphicx}\n";
202 packages << "\\usepackage["
203 << params.graphicsDriver
208 // packages << "\\usepackage{algorithm}\n";
211 // lyxskak.sty --- newer chess support based on skak.sty
212 if (isRequired("chess")) {
213 packages << "\\usepackage[ps,mover]{lyxskak}\n";
217 if ((params.spacing.getSpace() != Spacing::Single
218 && !params.spacing.isDefault())
219 || isRequired("setspace")) {
220 packages << "\\usepackage{setspace}\n";
222 switch (params.spacing.getSpace()) {
223 case Spacing::Default:
224 case Spacing::Single:
225 // we dont use setspace.sty so dont print anything
226 //packages += "\\singlespacing\n";
228 case Spacing::Onehalf:
229 packages << "\\onehalfspacing\n";
231 case Spacing::Double:
232 packages << "\\doublespacing\n";
235 packages << "\\setstretch{"
236 << params.spacing.getValue() << "}\n";
241 if (isRequired("amssymb") || params.use_amsmath)
242 packages << "\\usepackage{amssymb}\n";
244 if (isRequired("url") && ! tclass.provides(LyXTextClass::url))
245 packages << "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
246 " {\\newcommand{\\url}{\\texttt}}\n";
250 if (isRequired("natbib")) {
251 packages << "\\usepackage[";
252 if (params.use_numerical_citations) {
253 packages << "numbers";
255 packages << "authoryear";
257 packages << "]{natbib}\n";
260 packages << externalPreambles;
262 return packages.str().c_str();
266 string const LaTeXFeatures::getMacros() const
268 ostringstream macros;
270 // always include this
271 if (true || isRequired("lyx"))
272 macros << lyx_def << '\n';
274 if (isRequired("lyxline"))
275 macros << lyxline_def << '\n';
277 if (isRequired("noun"))
278 macros << noun_def << '\n';
280 if (isRequired("lyxarrow"))
281 macros << lyxarrow_def << '\n';
284 if (isRequired("quotesinglbase"))
285 macros << quotesinglbase_def << '\n';
286 if (isRequired("quotedblbase"))
287 macros << quotedblbase_def << '\n';
288 if (isRequired("guilsinglleft"))
289 macros << guilsinglleft_def << '\n';
290 if (isRequired("guilsinglright"))
291 macros << guilsinglright_def << '\n';
292 if (isRequired("guillemotleft"))
293 macros << guillemotleft_def << '\n';
294 if (isRequired("guillemotright"))
295 macros << guillemotright_def << '\n';
298 if (isRequired("boldsymbol") && !isRequired("amsmath"))
299 macros << boldsymbol_def << '\n';
300 if (isRequired("binom") && !isRequired("amsmath"))
301 macros << binom_def << '\n';
304 if (isRequired("NeedLyXMinipageIndent"))
305 macros << minipageindent_def;
306 if (isRequired("ParagraphIndent"))
307 macros << paragraphindent_def;
308 if (isRequired("NeedLyXFootnoteCode"))
309 macros << floatingfootnote_def;
312 getFloatDefinitions(macros);
314 for (LanguageList::const_iterator cit = UsedLanguages.begin();
315 cit != UsedLanguages.end(); ++cit)
316 if (!(*cit)->latex_options().empty())
317 macros << (*cit)->latex_options() << '\n';
318 if (!params.language->latex_options().empty())
319 macros << params.language->latex_options() << '\n';
321 return macros.str().c_str();
325 string const LaTeXFeatures::getTClassPreamble() const
327 // the text class specific preamble
328 LyXTextClass const & tclass = textclasslist.TextClass(params.textclass);
329 ostringstream tcpreamble;
331 tcpreamble << tclass.preamble();
333 for (layout_type i = 0; i < tclass.numLayouts(); ++i) {
335 tcpreamble << tclass[i].preamble();
339 return tcpreamble.str().c_str();
343 string const LaTeXFeatures::getLyXSGMLEntities() const
345 // Definition of entities used in the document that are LyX related.
346 ostringstream entities;
348 if (isRequired("lyxarrow")) {
349 entities << "<!ENTITY lyxarrow \"->\">" << '\n';
352 return entities.str().c_str();
356 string const LaTeXFeatures::getIncludedFiles(string const & fname) const
358 ostringstream sgmlpreamble;
359 string const basename = OnlyPath(fname);
361 FileMap::const_iterator end = IncludedFiles.end();
362 for (FileMap::const_iterator fi = IncludedFiles.begin();
364 sgmlpreamble << "\n<!ENTITY " << fi->first
365 << (IsSGMLFilename(fi->second) ? " SYSTEM \"" : " \"")
366 << MakeRelPath(fi->second, basename) << "\">";
368 return sgmlpreamble.str().c_str();
372 void LaTeXFeatures::showStruct() const {
373 lyxerr << "LyX needs the following commands when LaTeXing:"
374 << "\n***** Packages:" << getPackages()
375 << "\n***** Macros:" << getMacros()
376 << "\n***** Textclass stuff:" << getTClassPreamble()
377 << "\n***** done." << endl;
381 BufferParams const & LaTeXFeatures::bufferParams() const
386 void LaTeXFeatures::getFloatDefinitions(ostream & os) const
388 // Here we will output the code to create the needed float styles.
389 // We will try to do this as minimal as possible.
390 // \floatstyle{ruled}
391 // \newfloat{algorithm}{htbp}{loa}
392 // \floatname{algorithm}{Algorithm}
393 UsedFloats::const_iterator cit = usedFloats.begin();
394 UsedFloats::const_iterator end = usedFloats.end();
395 // ostringstream floats;
396 for (; cit != end; ++cit) {
397 Floating const & fl = floatList.getType((*cit));
399 // For builtin floats we do nothing.
400 if (fl.builtin()) continue;
402 // We have to special case "table" and "figure"
403 if (fl.type() == "tabular" || fl.type() == "figure") {
404 // Output code to modify "table" or "figure"
405 // but only if builtin == false
406 // and that have to be true at this point in the
408 string const type = fl.type();
409 string const placement = fl.placement();
410 string const style = fl.style();
411 if (!style.empty()) {
412 os << "\\floatstyle{" << style << "}\n"
413 << "\\restylefloat{" << type << "}\n";
415 if (!placement.empty()) {
416 os << "\\floatplacement{" << type << "}{"
417 << placement << "}\n";
420 // The other non builtin floats.
422 string const type = fl.type();
423 string const placement = fl.placement();
424 string const ext = fl.ext();
425 string const within = fl.within();
426 string const style = fl.style();
427 string const name = fl.name();
428 os << "\\floatstyle{" << style << "}\n"
429 << "\\newfloat{" << type << "}{" << placement
430 << "}{" << ext << "}";
432 os << "[" << within << "]";
434 << "\\floatname{" << type << "}{"
437 // What missing here is to code to minimalize the code
438 // output so that the same floatstyle will not be
439 // used several times, when the same style is still in