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 if (isRequired("LyX"))
269 macros << lyx_def << '\n';
271 if (isRequired("lyxline"))
272 macros << lyxline_def << '\n';
274 if (isRequired("noun"))
275 macros << noun_def << '\n';
277 if (isRequired("lyxarrow"))
278 macros << lyxarrow_def << '\n';
281 if (isRequired("quotesinglbase"))
282 macros << quotesinglbase_def << '\n';
283 if (isRequired("quotedblbase"))
284 macros << quotedblbase_def << '\n';
285 if (isRequired("guilsinglleft"))
286 macros << guilsinglleft_def << '\n';
287 if (isRequired("guilsinglright"))
288 macros << guilsinglright_def << '\n';
289 if (isRequired("guillemotleft"))
290 macros << guillemotleft_def << '\n';
291 if (isRequired("guillemotright"))
292 macros << guillemotright_def << '\n';
295 if (isRequired("boldsymbol") && !isRequired("amsmath"))
296 macros << boldsymbol_def << '\n';
297 if (isRequired("binom") && !isRequired("amsmath"))
298 macros << binom_def << '\n';
299 if (isRequired("mathcircumflex"))
300 macros << mathcircumflex_def << '\n';
303 if (isRequired("NeedLyXMinipageIndent"))
304 macros << minipageindent_def;
305 if (isRequired("ParagraphLeftIndent"))
306 macros << paragraphleftindent_def;
307 if (isRequired("NeedLyXFootnoteCode"))
308 macros << floatingfootnote_def;
311 getFloatDefinitions(macros);
313 return macros.str().c_str();
317 string const LaTeXFeatures::getBabelOptions() const
321 for (LanguageList::const_iterator cit = UsedLanguages.begin();
322 cit != UsedLanguages.end(); ++cit)
323 if (!(*cit)->latex_options().empty())
324 tmp << (*cit)->latex_options() << '\n';
325 if (!params.language->latex_options().empty())
326 tmp << params.language->latex_options() << '\n';
328 return tmp.str().c_str();
332 string const LaTeXFeatures::getTClassPreamble() const
334 // the text class specific preamble
335 LyXTextClass const & tclass = textclasslist[params.textclass];
336 ostringstream tcpreamble;
338 tcpreamble << tclass.preamble();
340 set<string>::const_iterator cit = layout.begin();
341 set<string>::const_iterator end = layout.end();
342 for (; cit != end; ++cit) {
343 tcpreamble << tclass[*cit]->preamble();
346 return tcpreamble.str().c_str();
350 string const LaTeXFeatures::getLyXSGMLEntities() const
352 // Definition of entities used in the document that are LyX related.
353 ostringstream entities;
355 if (isRequired("lyxarrow")) {
356 entities << "<!ENTITY lyxarrow \"->\">" << '\n';
359 return entities.str().c_str();
363 string const LaTeXFeatures::getIncludedFiles(string const & fname) const
365 ostringstream sgmlpreamble;
366 string const basename = OnlyPath(fname);
368 FileMap::const_iterator end = IncludedFiles.end();
369 for (FileMap::const_iterator fi = IncludedFiles.begin();
371 sgmlpreamble << "\n<!ENTITY " << fi->first
372 << (IsSGMLFilename(fi->second) ? " SYSTEM \"" : " \"")
373 << MakeRelPath(fi->second, basename) << "\">";
375 return sgmlpreamble.str().c_str();
379 void LaTeXFeatures::showStruct() const {
380 lyxerr << "LyX needs the following commands when LaTeXing:"
381 << "\n***** Packages:" << getPackages()
382 << "\n***** Macros:" << getMacros()
383 << "\n***** Textclass stuff:" << getTClassPreamble()
384 << "\n***** done." << endl;
388 BufferParams const & LaTeXFeatures::bufferParams() const
394 void LaTeXFeatures::getFloatDefinitions(ostream & os) const
396 // Here we will output the code to create the needed float styles.
397 // We will try to do this as minimal as possible.
398 // \floatstyle{ruled}
399 // \newfloat{algorithm}{htbp}{loa}
400 // \floatname{algorithm}{Algorithm}
401 UsedFloats::const_iterator cit = usedFloats.begin();
402 UsedFloats::const_iterator end = usedFloats.end();
403 // ostringstream floats;
404 for (; cit != end; ++cit) {
405 Floating const & fl = floatList.getType((*cit));
407 // For builtin floats we do nothing.
408 if (fl.builtin()) continue;
410 // We have to special case "table" and "figure"
411 if (fl.type() == "tabular" || fl.type() == "figure") {
412 // Output code to modify "table" or "figure"
413 // but only if builtin == false
414 // and that have to be true at this point in the
416 string const type = fl.type();
417 string const placement = fl.placement();
418 string const style = fl.style();
419 if (!style.empty()) {
420 os << "\\floatstyle{" << style << "}\n"
421 << "\\restylefloat{" << type << "}\n";
423 if (!placement.empty()) {
424 os << "\\floatplacement{" << type << "}{"
425 << placement << "}\n";
428 // The other non builtin floats.
430 string const type = fl.type();
431 string const placement = fl.placement();
432 string const ext = fl.ext();
433 string const within = fl.within();
434 string const style = fl.style();
435 string const name = fl.name();
436 os << "\\floatstyle{" << style << "}\n"
437 << "\\newfloat{" << type << "}{" << placement
438 << "}{" << ext << "}";
440 os << "[" << within << "]";
442 << "\\floatname{" << type << "}{"
445 // What missing here is to code to minimalize the code
446 // output so that the same floatstyle will not be
447 // used several times, when the same style is still in