2 * \file LaTeXFeatures.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
10 * \author André Pönitz
12 * Full author contact details are available in file CREDITS.
17 #include "LaTeXFeatures.h"
21 #include "bufferparams.h"
22 #include "FloatList.h"
28 #include "support/filetools.h"
29 #include "support/lstrings.h"
31 using namespace lyx::support;
33 using lyx::textclass_type;
42 LaTeXFeatures::LaTeXFeatures(BufferParams const & p)
47 bool LaTeXFeatures::useBabel() const
49 return lyxrc.language_use_babel ||
50 bufferParams().language->lang() != lyxrc.default_language ||
55 void LaTeXFeatures::require(string const & name)
60 features.push_back(name);
64 void LaTeXFeatures::useLayout(string const & layoutname)
66 // Some code to avoid loops in dependency definition
68 const int maxlevel = 30;
69 if (level > maxlevel) {
70 lyxerr << "LaTeXFeatures::useLayout: maximum level of "
71 << "recursion attained by layout "
72 << layoutname << endl;
76 LyXTextClass const & tclass = params.getLyXTextClass();
77 if (tclass.hasLayout(layoutname)) {
78 // Is this layout already in usedLayouts?
79 list<string>::const_iterator cit = usedLayouts.begin();
80 list<string>::const_iterator end = usedLayouts.end();
81 for (; cit != end; ++cit) {
82 if (layoutname == *cit)
86 LyXLayout_ptr const & lyt = tclass[layoutname];
87 if (!lyt->depends_on().empty()) {
89 useLayout(lyt->depends_on());
92 usedLayouts.push_back(layoutname);
94 lyxerr << "LaTeXFeatures::useLayout: layout `"
95 << layoutname << "' does not exist in this class"
103 bool LaTeXFeatures::isRequired(string const & name) const
105 return find(features.begin(), features.end(), name) != features.end();
109 void LaTeXFeatures::addExternalPreamble(string const & pream)
111 externalPreambles += pream;
115 void LaTeXFeatures::useFloat(string const & name)
117 usedFloats.insert(name);
118 // We only need float.sty if we use non builtin floats, or if we
119 // use the "H" modifier. This includes modified table and
120 // figure floats. (Lgb)
121 Floating const & fl = params.getLyXTextClass().floats().getType(name);
122 if (!fl.type().empty() && !fl.builtin()) {
128 void LaTeXFeatures::useLanguage(Language const * lang)
130 UsedLanguages.insert(lang);
134 void LaTeXFeatures::includeFile(string const & key, string const & name)
136 IncludedFiles[key] = name;
140 bool LaTeXFeatures::hasLanguages() const
142 return !UsedLanguages.empty();
146 string LaTeXFeatures::getLanguages() const
148 ostringstream languages;
150 for (LanguageList::const_iterator cit =
151 UsedLanguages.begin();
152 cit != UsedLanguages.end();
154 languages << (*cit)->babel() << ',';
156 return STRCONV(languages.str());
160 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding) const
162 set<string> encodings;
163 for (LanguageList::const_iterator it =
164 UsedLanguages.begin();
165 it != UsedLanguages.end(); ++it)
166 if ((*it)->encoding()->LatexName() != doc_encoding)
167 encodings.insert((*it)->encoding()->LatexName());
173 char const * simplefeatures[] = {
189 int const nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
194 string const LaTeXFeatures::getPackages() const
196 ostringstream packages;
197 LyXTextClass const & tclass = params.getLyXTextClass();
200 // These are all the 'simple' includes. i.e
201 // packages which we just \usepackage{package}
203 for (int i = 0; i < nb_simplefeatures; ++i) {
204 if (isRequired(simplefeatures[i]))
205 packages << "\\usepackage{"
206 << simplefeatures[i] << "}\n";
210 // The rest of these packages are somewhat more complicated
214 if (isRequired("amsmath")
215 && !tclass.provides(LyXTextClass::amsmath)
216 && params.use_amsmath != BufferParams::AMS_OFF) {
217 packages << "\\usepackage{amsmath}\n";
221 if (isRequired("color")) {
222 if (params.graphicsDriver == "default")
223 packages << "\\usepackage[usenames]{color}\n";
225 packages << "\\usepackage["
226 << params.graphicsDriver
232 if (isRequired("makeidx")) {
233 if (! tclass.provides(LyXTextClass::makeidx))
234 packages << "\\usepackage{makeidx}\n";
235 packages << "\\makeindex\n";
239 if (isRequired("graphicx") && params.graphicsDriver != "none") {
240 if (params.graphicsDriver == "default")
241 packages << "\\usepackage{graphicx}\n";
243 packages << "\\usepackage["
244 << params.graphicsDriver
249 // packages << "\\usepackage{algorithm}\n";
252 // lyxskak.sty --- newer chess support based on skak.sty
253 if (isRequired("chess")) {
254 packages << "\\usepackage[ps,mover]{lyxskak}\n";
258 if ((params.spacing.getSpace() != Spacing::Single
259 && !params.spacing.isDefault())
260 || isRequired("setspace")) {
261 packages << "\\usepackage{setspace}\n";
263 switch (params.spacing.getSpace()) {
264 case Spacing::Default:
265 case Spacing::Single:
266 // we dont use setspace.sty so dont print anything
267 //packages += "\\singlespacing\n";
269 case Spacing::Onehalf:
270 packages << "\\onehalfspacing\n";
272 case Spacing::Double:
273 packages << "\\doublespacing\n";
276 packages << "\\setstretch{"
277 << params.spacing.getValue() << "}\n";
282 if (isRequired("amssymb") || params.use_amsmath == BufferParams::AMS_ON)
283 packages << "\\usepackage{amssymb}\n";
285 if (isRequired("url") && ! tclass.provides(LyXTextClass::url))
286 packages << "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
287 " {\\newcommand{\\url}{\\texttt}}\n";
291 if (isRequired("natbib") && ! tclass.provides(LyXTextClass::natbib)) {
292 packages << "\\usepackage[";
293 if (params.use_numerical_citations) {
294 packages << "numbers";
296 packages << "authoryear";
298 packages << "]{natbib}\n";
301 packages << externalPreambles;
303 return STRCONV(packages.str());
307 string const LaTeXFeatures::getMacros() const
309 ostringstream macros;
311 if (isRequired("LyX"))
312 macros << lyx_def << '\n';
314 if (isRequired("lyxline"))
315 macros << lyxline_def << '\n';
317 if (isRequired("noun"))
318 macros << noun_def << '\n';
320 if (isRequired("lyxarrow"))
321 macros << lyxarrow_def << '\n';
324 if (isRequired("quotesinglbase"))
325 macros << quotesinglbase_def << '\n';
326 if (isRequired("quotedblbase"))
327 macros << quotedblbase_def << '\n';
328 if (isRequired("guilsinglleft"))
329 macros << guilsinglleft_def << '\n';
330 if (isRequired("guilsinglright"))
331 macros << guilsinglright_def << '\n';
332 if (isRequired("guillemotleft"))
333 macros << guillemotleft_def << '\n';
334 if (isRequired("guillemotright"))
335 macros << guillemotright_def << '\n';
338 if (isRequired("boldsymbol") && !isRequired("amsmath"))
339 macros << boldsymbol_def << '\n';
340 if (isRequired("binom") && !isRequired("amsmath"))
341 macros << binom_def << '\n';
342 if (isRequired("mathcircumflex"))
343 macros << mathcircumflex_def << '\n';
346 if (isRequired("NeedLyXMinipageIndent"))
347 macros << minipageindent_def;
348 if (isRequired("ParagraphLeftIndent"))
349 macros << paragraphleftindent_def;
350 if (isRequired("NeedLyXFootnoteCode"))
351 macros << floatingfootnote_def;
353 // some problems with tex->html converters
354 if (isRequired("NeedTabularnewline"))
355 macros << tabularnewline_def;
357 // greyedout environment (note inset)
358 if (isRequired("lyxgreyedout"))
359 macros << lyxgreyedout_def;
362 getFloatDefinitions(macros);
364 return STRCONV(macros.str());
368 string const LaTeXFeatures::getBabelOptions() const
372 for (LanguageList::const_iterator cit = UsedLanguages.begin();
373 cit != UsedLanguages.end(); ++cit)
374 if (!(*cit)->latex_options().empty())
375 tmp << (*cit)->latex_options() << '\n';
376 if (!params.language->latex_options().empty())
377 tmp << params.language->latex_options() << '\n';
379 return STRCONV(tmp.str());
383 string const LaTeXFeatures::getTClassPreamble() const
385 // the text class specific preamble
386 LyXTextClass const & tclass = params.getLyXTextClass();
387 ostringstream tcpreamble;
389 tcpreamble << tclass.preamble();
391 list<string>::const_iterator cit = usedLayouts.begin();
392 list<string>::const_iterator end = usedLayouts.end();
393 for (; cit != end; ++cit) {
394 tcpreamble << tclass[*cit]->preamble();
397 return STRCONV(tcpreamble.str());
401 string const LaTeXFeatures::getLyXSGMLEntities() const
403 // Definition of entities used in the document that are LyX related.
404 ostringstream entities;
406 if (isRequired("lyxarrow")) {
407 entities << "<!ENTITY lyxarrow \"->\">" << '\n';
410 return STRCONV(entities.str());
414 string const LaTeXFeatures::getIncludedFiles(string const & fname) const
416 ostringstream sgmlpreamble;
417 string const basename = OnlyPath(fname);
419 FileMap::const_iterator end = IncludedFiles.end();
420 for (FileMap::const_iterator fi = IncludedFiles.begin();
422 sgmlpreamble << "\n<!ENTITY " << fi->first
423 << (IsSGMLFilename(fi->second) ? " SYSTEM \"" : " \"")
424 << MakeRelPath(fi->second, basename) << "\">";
426 return STRCONV(sgmlpreamble.str());
430 void LaTeXFeatures::showStruct() const {
431 lyxerr << "LyX needs the following commands when LaTeXing:"
432 << "\n***** Packages:" << getPackages()
433 << "\n***** Macros:" << getMacros()
434 << "\n***** Textclass stuff:" << getTClassPreamble()
435 << "\n***** done." << endl;
439 BufferParams const & LaTeXFeatures::bufferParams() const
445 void LaTeXFeatures::getFloatDefinitions(ostream & os) const
447 FloatList const & floats = params.getLyXTextClass().floats();
449 // Here we will output the code to create the needed float styles.
450 // We will try to do this as minimal as possible.
451 // \floatstyle{ruled}
452 // \newfloat{algorithm}{htbp}{loa}
453 // \floatname{algorithm}{Algorithm}
454 UsedFloats::const_iterator cit = usedFloats.begin();
455 UsedFloats::const_iterator end = usedFloats.end();
456 // ostringstream floats;
457 for (; cit != end; ++cit) {
458 Floating const & fl = floats.getType((*cit));
460 // For builtin floats we do nothing.
461 if (fl.builtin()) continue;
463 // We have to special case "table" and "figure"
464 if (fl.type() == "tabular" || fl.type() == "figure") {
465 // Output code to modify "table" or "figure"
466 // but only if builtin == false
467 // and that have to be true at this point in the
469 string const type = fl.type();
470 string const placement = fl.placement();
471 string const style = fl.style();
472 if (!style.empty()) {
473 os << "\\floatstyle{" << style << "}\n"
474 << "\\restylefloat{" << type << "}\n";
476 if (!placement.empty()) {
477 os << "\\floatplacement{" << type << "}{"
478 << placement << "}\n";
481 // The other non builtin floats.
483 string const type = fl.type();
484 string const placement = fl.placement();
485 string const ext = fl.ext();
486 string const within = fl.within();
487 string const style = fl.style();
488 string const name = fl.name();
489 os << "\\floatstyle{" << style << "}\n"
490 << "\\newfloat{" << type << "}{" << placement
491 << "}{" << ext << '}';
493 os << '[' << within << ']';
495 << "\\floatname{" << type << "}{"
498 // What missing here is to code to minimalize the code
499 // output so that the same floatstyle will not be
500 // used several times, when the same style is still in