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[] = {
147 const int nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
151 string const LaTeXFeatures::getPackages() const
153 ostringstream packages;
154 LyXTextClass const & tclass = textclasslist[params.textclass];
158 * These are all the 'simple' includes. i.e
159 * packages which we just \usepackage{package}
161 for (int i = 0 ; i < nb_simplefeatures ; ++i) {
162 if (isRequired(simplefeatures[i]))
163 packages << "\\usepackage{"
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("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[params.textclass];
329 ostringstream tcpreamble;
331 tcpreamble << tclass.preamble();
333 set<string>::const_iterator cit = layout.begin();
334 set<string>::const_iterator end = layout.end();
335 for (; cit != end; ++cit) {
336 tcpreamble << tclass[*cit].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
387 void LaTeXFeatures::getFloatDefinitions(ostream & os) const
389 // Here we will output the code to create the needed float styles.
390 // We will try to do this as minimal as possible.
391 // \floatstyle{ruled}
392 // \newfloat{algorithm}{htbp}{loa}
393 // \floatname{algorithm}{Algorithm}
394 UsedFloats::const_iterator cit = usedFloats.begin();
395 UsedFloats::const_iterator end = usedFloats.end();
396 // ostringstream floats;
397 for (; cit != end; ++cit) {
398 Floating const & fl = floatList.getType((*cit));
400 // For builtin floats we do nothing.
401 if (fl.builtin()) continue;
403 // We have to special case "table" and "figure"
404 if (fl.type() == "tabular" || fl.type() == "figure") {
405 // Output code to modify "table" or "figure"
406 // but only if builtin == false
407 // and that have to be true at this point in the
409 string const type = fl.type();
410 string const placement = fl.placement();
411 string const style = fl.style();
412 if (!style.empty()) {
413 os << "\\floatstyle{" << style << "}\n"
414 << "\\restylefloat{" << type << "}\n";
416 if (!placement.empty()) {
417 os << "\\floatplacement{" << type << "}{"
418 << placement << "}\n";
421 // The other non builtin floats.
423 string const type = fl.type();
424 string const placement = fl.placement();
425 string const ext = fl.ext();
426 string const within = fl.within();
427 string const style = fl.style();
428 string const name = fl.name();
429 os << "\\floatstyle{" << style << "}\n"
430 << "\\newfloat{" << type << "}{" << placement
431 << "}{" << ext << "}";
433 os << "[" << within << "]";
435 << "\\floatname{" << type << "}{"
438 // What missing here is to code to minimalize the code
439 // output so that the same floatstyle will not be
440 // used several times, when the same style is still in