2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1995 Matthias Ettrich
8 * Copyright 1995-2001 the LyX Team.
10 * ====================================================== */
15 #pragma implementation "LaTeXFeatures.h"
22 #include "LaTeXFeatures.h"
23 #include "bufferparams.h"
25 #include "support/filetools.h"
26 #include "FloatList.h"
31 LaTeXFeatures::LaTeXFeatures(BufferParams const & p, LyXTextClass::size_type n)
32 : layout(n, false), params(p)
37 graphics = false; // INSET_GRAPHICS: remove this when InsetFig is thrown.
62 quotesinglbase = false;
64 guilsinglleft = false;
65 guilsinglright = false;
66 guillemotleft = false;
67 guillemotright = false;
75 LyXParagraphIndent = false;
76 NeedLyXFootnoteCode = false;
77 NeedLyXMinipageIndent = false;
81 void LaTeXFeatures::require(string const & name)
83 if (name == "array") {
85 } else if (name == "color") {
87 } else if (name == "graphics") {
89 graphics = true;// INSET_GRAPHICS: remove this when InsetFig is thrown.
90 } else if (name == "setspace") {
92 } else if (name == "makeidx") {
94 } else if (name == "verbatim") {
96 } else if (name == "longtable") {
98 //} else if (name == "algorithm") {
100 } else if (name == "rotating") {
102 } else if (name == "amssymb") {
104 } else if (name == "latexsym") {
106 } else if (name == "pifont") {
108 } else if (name == "subfigure") {
110 } else if (name == "floatflt") {
112 } else if (name == "url") {
114 } else if (name == "varioref") {
116 } else if (name == "prettyref") {
118 } else if (name == "chess") {
120 } else if (name == "amsstyle") {
122 } else if (name == "boldsymbol") {
124 } else if (name == "binom") {
130 string const LaTeXFeatures::getPackages() const
132 ostringstream packages;
133 LyXTextClass const & tclass =
134 textclasslist.TextClass(params.textclass);
138 packages << "\\usepackage{array}\n";
142 if (params.graphicsDriver == "default")
143 packages << "\\usepackage{color}\n";
145 packages << "\\usepackage["
146 << params.graphicsDriver
152 if (! tclass.provides(LyXTextClass::makeidx)
153 && params.language->babel() != "french") // french provides
155 // French should not be hardcoded. (Lgb)
156 packages << "\\usepackage{makeidx}\n";
157 packages << "\\makeindex\n";
161 if (graphicx && params.graphicsDriver != "none") {
162 if (params.graphicsDriver == "default")
163 packages << "\\usepackage{graphicx}\n";
165 packages << "\\usepackage["
166 << params.graphicsDriver
170 // INSET_GRAPHICS: remove this when InsetFig is thrown.
172 if (graphics && params.graphicsDriver != "none") {
173 if (params.graphicsDriver == "default")
174 packages << "\\usepackage{graphics}\n";
176 packages << "\\usepackage["
177 << params.graphicsDriver
183 packages << "\\usepackage{verbatim}\n";
186 // packages << "\\usepackage{algorithm}\n";
191 packages << "\\usepackage{lyxchess}\n";
195 if ((params.spacing.getSpace() != Spacing::Single
196 && !params.spacing.isDefault())
198 packages << "\\usepackage{setspace}\n";
200 switch (params.spacing.getSpace()) {
201 case Spacing::Default:
202 case Spacing::Single:
203 // we dont use setspace.sty so dont print anything
204 //packages += "\\singlespacing\n";
206 case Spacing::Onehalf:
207 packages << "\\onehalfspacing\n";
209 case Spacing::Double:
210 packages << "\\doublespacing\n";
213 packages << "\\setstretch{"
214 << params.spacing.getValue() << "}\n";
220 packages << "\\usepackage{longtable}\n";
224 packages << "\\usepackage{rotating}\n";
227 if (amssymb || params.use_amsmath)
228 packages << "\\usepackage{amssymb}\n";
232 packages << "\\usepackage{latexsym}\n";
236 packages << "\\usepackage{pifont}\n";
240 packages << "\\usepackage{subfigure}\n";
244 packages << "\\usepackage{floatflt}\n";
247 if (url && ! tclass.provides(LyXTextClass::url))
248 packages << "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
249 " {\\newcommand{\\url}{\\texttt}}\n";
253 packages << "\\usepackage{varioref}\n";
257 packages << "\\usepackage{prettyref}\n";
260 // We only need float.sty if we use non builtin floats. This includes
261 // modified table and figure floats. (Lgb)
262 if (!usedFloats.empty()) {
263 bool use_float = false;
264 UsedFloats::const_iterator beg = usedFloats.begin();
265 UsedFloats::const_iterator end = usedFloats.end();
266 for (; beg != end; ++beg) {
267 Floating const & fl = floatList.getType((*beg));
268 if (!fl.type().empty() && !fl.builtin()) {
274 packages << "\\usepackage{float}\n";
277 packages << externalPreambles;
279 return packages.str().c_str();
283 string const LaTeXFeatures::getMacros() const
285 ostringstream macros;
287 // always include this
289 macros << lyx_def << '\n';
292 macros << lyxline_def << '\n';
295 macros << noun_def << '\n';
299 macros << lyxarrow_def << '\n';
304 macros << quotesinglbase_def << '\n';
306 macros << quotedblbase_def << '\n';
308 macros << guilsinglleft_def << '\n';
310 macros << guilsinglright_def << '\n';
312 macros << guillemotleft_def << '\n';
314 macros << guillemotright_def << '\n';
317 if (boldsymbol && !amsstyle)
318 macros << boldsymbol_def << '\n';
319 if (binom && !amsstyle)
320 macros << binom_def << '\n';
323 if (NeedLyXMinipageIndent)
324 macros << minipageindent_def;
325 if (LyXParagraphIndent)
326 macros << paragraphindent_def;
327 if (NeedLyXFootnoteCode)
328 macros << floatingfootnote_def;
331 getFloatDefinitions(macros);
333 for (LanguageList::const_iterator cit = UsedLanguages.begin();
334 cit != UsedLanguages.end(); ++cit)
335 if (!(*cit)->latex_options().empty())
336 macros << (*cit)->latex_options() << '\n';
337 if (!params.language->latex_options().empty())
338 macros << params.language->latex_options() << '\n';
340 return macros.str().c_str();
344 string const LaTeXFeatures::getTClassPreamble() const
346 // the text class specific preamble
347 LyXTextClass const & tclass =
348 textclasslist.TextClass(params.textclass);
349 ostringstream tcpreamble;
351 tcpreamble << tclass.preamble();
353 for (unsigned int i = 0; i < tclass.numLayouts(); ++i) {
355 tcpreamble << tclass[i].preamble();
359 return tcpreamble.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 // outputted so that the same flotastyle will not be
447 // used several times. when the same style is still in