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 "LaTeXFeatures.h"
21 #include "LaTeXFeatures.h"
22 #include "bufferparams.h"
24 #include "support/filetools.h"
25 #include "support/lstrings.h"
26 #include "FloatList.h"
33 using lyx::layout_type;
34 using lyx::textclass_type;
36 LaTeXFeatures::LaTeXFeatures(BufferParams const & p, layout_type n)
37 : layout(n, false), params(p)
41 void LaTeXFeatures::require(string const & name)
43 // INSET_GRAPHICS: remove this when InsetFig is thrown.
44 if (name == "graphics") {
45 features.push_back("graphicx");
46 features.push_back("graphics");
48 features.push_back(name);
52 void LaTeXFeatures::useLayout(std::vector<bool>::size_type const & idx)
58 bool LaTeXFeatures::isRequired(string const & name) const
60 FeaturesList::const_iterator i = std::find(features.begin(),
63 return i != features.end();
67 void LaTeXFeatures::addExternalPreamble(string const & pream)
69 externalPreambles += pream;
73 void LaTeXFeatures::useFloat(string const & name)
75 usedFloats.insert(name);
79 void LaTeXFeatures::useLanguage(Language const * lang)
81 UsedLanguages.insert(lang);
85 void LaTeXFeatures::includeFile(string const & key, string const & name)
87 IncludedFiles[key] = name;
91 bool LaTeXFeatures::hasLanguages()
93 return !UsedLanguages.empty();
97 string LaTeXFeatures::getLanguages() const
99 ostringstream languages;
101 for (LanguageList::const_iterator cit =
102 UsedLanguages.begin();
103 cit != UsedLanguages.end();
105 languages << (*cit)->babel() << ',';
107 return languages.str().c_str();
111 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding)
113 set<string> encodings;
114 for (LanguageList::const_iterator it =
115 UsedLanguages.begin();
116 it != UsedLanguages.end(); ++it)
117 if ((*it)->encoding()->LatexName() != doc_encoding)
118 encodings.insert((*it)->encoding()->LatexName());
123 string const LaTeXFeatures::getPackages() const
125 ostringstream packages;
126 LyXTextClass const & tclass =
127 textclasslist.TextClass(params.textclass);
131 * These are all the 'simple' includes. i.e
132 * packages which we just \usepackage{package}
136 if (isRequired("array"))
137 packages << "\\usepackage{array}\n";
140 if (isRequired("verbatim"))
141 packages << "\\usepackage{verbatim}\n";
144 if (isRequired("longtable"))
145 packages << "\\usepackage{longtable}\n";
148 if (isRequired("rotating"))
149 packages << "\\usepackage{rotating}\n";
153 if (isRequired("latexsym"))
154 packages << "\\usepackage{latexsym}\n";
157 if (isRequired("pifont"))
158 packages << "\\usepackage{pifont}\n";
161 if (isRequired("subfigure"))
162 packages << "\\usepackage{subfigure}\n";
165 if (isRequired("floatflt"))
166 packages << "\\usepackage{floatflt}\n";
170 if (isRequired("varioref"))
171 packages << "\\usepackage{varioref}\n";
174 if (isRequired("prettyref"))
175 packages << "\\usepackage{prettyref}\n";
178 * The rest of these packages are somewhat more complicated
183 if (isRequired("color")) {
184 if (params.graphicsDriver == "default")
185 packages << "\\usepackage{color}\n";
187 packages << "\\usepackage["
188 << params.graphicsDriver
193 if (isRequired("makeidx")) {
194 if (! tclass.provides(LyXTextClass::makeidx))
195 packages << "\\usepackage{makeidx}\n";
196 packages << "\\makeindex\n";
200 if (isRequired("graphicx") && params.graphicsDriver != "none") {
201 if (params.graphicsDriver == "default")
202 packages << "\\usepackage{graphicx}\n";
204 packages << "\\usepackage["
205 << params.graphicsDriver
209 // INSET_GRAPHICS: remove this when InsetFig is thrown.
211 if (isRequired("graphics") && params.graphicsDriver != "none") {
212 if (params.graphicsDriver == "default")
213 packages << "\\usepackage{graphics}\n";
215 packages << "\\usepackage["
216 << params.graphicsDriver
221 // packages << "\\usepackage{algorithm}\n";
224 // lyxskak.sty --- newer chess support based on skak.sty
225 if (isRequired("chess")) {
226 packages << "\\usepackage[ps,mover]{lyxskak}\n";
230 if ((params.spacing.getSpace() != Spacing::Single
231 && !params.spacing.isDefault())
232 || isRequired("setspace")) {
233 packages << "\\usepackage{setspace}\n";
235 switch (params.spacing.getSpace()) {
236 case Spacing::Default:
237 case Spacing::Single:
238 // we dont use setspace.sty so dont print anything
239 //packages += "\\singlespacing\n";
241 case Spacing::Onehalf:
242 packages << "\\onehalfspacing\n";
244 case Spacing::Double:
245 packages << "\\doublespacing\n";
248 packages << "\\setstretch{"
249 << params.spacing.getValue() << "}\n";
254 if (isRequired("amssymb") || params.use_amsmath)
255 packages << "\\usepackage{amssymb}\n";
257 if (isRequired("url") && ! tclass.provides(LyXTextClass::url))
258 packages << "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
259 " {\\newcommand{\\url}{\\texttt}}\n";
262 // We only need float.sty if we use non builtin floats, or if we
263 // use the "H" modifier. This includes modified table and
264 // figure floats. (Lgb)
265 if (!usedFloats.empty()) {
266 UsedFloats::const_iterator beg = usedFloats.begin();
267 UsedFloats::const_iterator end = usedFloats.end();
268 for (; beg != end; ++beg) {
269 Floating const & fl = floatList.getType((*beg));
270 if (!fl.type().empty() && !fl.builtin()) {
271 const_cast<LaTeXFeatures *>(this)->require("floats");
276 if (isRequired("floats")) {
277 packages << "\\usepackage{float}\n";
281 if (isRequired("natbib")) {
282 packages << "\\usepackage[";
283 if (params.use_numerical_citations) {
284 packages << "numbers";
286 packages << "authoryear";
288 packages << "]{natbib}\n";
291 packages << externalPreambles;
293 return packages.str().c_str();
297 string const LaTeXFeatures::getMacros() const
299 ostringstream macros;
301 // always include this
302 if (true || isRequired("lyx"))
303 macros << lyx_def << '\n';
305 if (isRequired("lyxline"))
306 macros << lyxline_def << '\n';
308 if (isRequired("noun"))
309 macros << noun_def << '\n';
311 if (isRequired("lyxarrow"))
312 macros << lyxarrow_def << '\n';
315 if (isRequired("quotesinglbase"))
316 macros << quotesinglbase_def << '\n';
317 if (isRequired("quotedblbase"))
318 macros << quotedblbase_def << '\n';
319 if (isRequired("guilsinglleft"))
320 macros << guilsinglleft_def << '\n';
321 if (isRequired("guilsinglright"))
322 macros << guilsinglright_def << '\n';
323 if (isRequired("guillemotleft"))
324 macros << guillemotleft_def << '\n';
325 if (isRequired("guillemotright"))
326 macros << guillemotright_def << '\n';
329 if (isRequired("boldsymbol") && !isRequired("amsstyle"))
330 macros << boldsymbol_def << '\n';
331 if (isRequired("binom") && !isRequired("amsstyle"))
332 macros << binom_def << '\n';
335 if (isRequired("NeedLyXMinipageIndent"))
336 macros << minipageindent_def;
337 if (isRequired("ParagraphIndent"))
338 macros << paragraphindent_def;
339 if (isRequired("NeedLyXFootnoteCode"))
340 macros << floatingfootnote_def;
343 getFloatDefinitions(macros);
345 for (LanguageList::const_iterator cit = UsedLanguages.begin();
346 cit != UsedLanguages.end(); ++cit)
347 if (!(*cit)->latex_options().empty())
348 macros << (*cit)->latex_options() << '\n';
349 if (!params.language->latex_options().empty())
350 macros << params.language->latex_options() << '\n';
352 return macros.str().c_str();
356 string const LaTeXFeatures::getTClassPreamble() const
358 // the text class specific preamble
359 LyXTextClass const & tclass = textclasslist.TextClass(params.textclass);
360 ostringstream tcpreamble;
362 tcpreamble << tclass.preamble();
364 for (layout_type i = 0; i < tclass.numLayouts(); ++i) {
366 tcpreamble << tclass[i].preamble();
370 return tcpreamble.str().c_str();
374 string const LaTeXFeatures::getLyXSGMLEntities() const
376 // Definition of entities used in the document that are LyX related.
377 ostringstream entities;
379 if (isRequired("lyxarrow")) {
380 entities << "<!ENTITY lyxarrow \"->\">" << '\n';
383 return entities.str().c_str();
387 string const LaTeXFeatures::getIncludedFiles(string const & fname) const
389 ostringstream sgmlpreamble;
390 string const basename = OnlyPath(fname);
392 FileMap::const_iterator end = IncludedFiles.end();
393 for (FileMap::const_iterator fi = IncludedFiles.begin();
395 sgmlpreamble << "\n<!ENTITY " << fi->first
396 << (IsSGMLFilename(fi->second) ? " SYSTEM \"" : " \"" )
397 << MakeRelPath(fi->second, basename) << "\">";
399 return sgmlpreamble.str().c_str();
403 void LaTeXFeatures::showStruct() const {
404 lyxerr << "LyX needs the following commands when LaTeXing:"
405 << "\n***** Packages:" << getPackages()
406 << "\n***** Macros:" << getMacros()
407 << "\n***** Textclass stuff:" << getTClassPreamble()
408 << "\n***** done." << endl;
412 BufferParams const & LaTeXFeatures::bufferParams() const
417 void LaTeXFeatures::getFloatDefinitions(std::ostream & os) const
419 // Here we will output the code to create the needed float styles.
420 // We will try to do this as minimal as possible.
421 // \floatstyle{ruled}
422 // \newfloat{algorithm}{htbp}{loa}
423 // \floatname{algorithm}{Algorithm}
424 UsedFloats::const_iterator cit = usedFloats.begin();
425 UsedFloats::const_iterator end = usedFloats.end();
426 // ostringstream floats;
427 for (; cit != end; ++cit) {
428 Floating const & fl = floatList.getType((*cit));
430 // For builtin floats we do nothing.
431 if (fl.builtin()) continue;
433 // We have to special case "table" and "figure"
434 if (fl.type() == "tabular" || fl.type() == "figure") {
435 // Output code to modify "table" or "figure"
436 // but only if builtin == false
437 // and that have to be true at this point in the
439 string const type = fl.type();
440 string const placement = fl.placement();
441 string const style = fl.style();
442 if (!style.empty()) {
443 os << "\\floatstyle{" << style << "}\n"
444 << "\\restylefloat{" << type << "}\n";
446 if (!placement.empty()) {
447 os << "\\floatplacement{" << type << "}{"
448 << placement << "}\n";
451 // The other non builtin floats.
453 string const type = fl.type();
454 string const placement = fl.placement();
455 string const ext = fl.ext();
456 string const within = fl.within();
457 string const style = fl.style();
458 string const name = fl.name();
459 os << "\\floatstyle{" << style << "}\n"
460 << "\\newfloat{" << type << "}{" << placement
461 << "}{" << ext << "}";
463 os << "[" << within << "]";
465 << "\\floatname{" << type << "}{"
468 // What missing here is to code to minimalize the code
469 // outputted so that the same floatstyle will not be
470 // used several times, when the same style is still in