2 * \file LaTeXFeatures.cpp
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"
19 #include "BufferParams.h"
24 #include "FloatList.h"
29 #include "support/docstream.h"
30 #include "support/filetools.h"
32 #include "frontends/controllers/frontend_helpers.h"
36 using support::isSGMLFilename;
37 using support::libFileSearch;
38 using support::makeRelPath;
39 using support::onlyPath;
46 using std::ostringstream;
49 /////////////////////////////////////////////////////////////////////
53 /////////////////////////////////////////////////////////////////////
55 //\NeedsTeXFormat{LaTeX2e}
56 //\ProvidesPackage{lyx}[1996/01/11 LLE v0.2 (LyX LaTeX Extensions)]
57 //\message{LyX LaTeX Extensions (LLE v0.2) of 11-Jan-1996.}
59 static string const lyx_def =
60 "\\providecommand{\\LyX}{L\\kern-.1667em\\lower.25em\\hbox{Y}\\kern-.125emX\\@}";
62 static string const lyxline_def =
63 "\\newcommand{\\lyxline}[1][1pt]{%\n"
65 " \\rule[.5ex]{\\linewidth}{#1}\\par}";
67 static string const noun_def = "\\newcommand{\\noun}[1]{\\textsc{#1}}";
69 static string const lyxarrow_def =
70 "\\newcommand{\\lyxarrow}{\\leavevmode\\,$\\triangleright$\\,\\allowbreak}";
72 // for quotes without babel. This does not give perfect results, but
73 // anybody serious about non-english quotes should use babel (JMarc).
75 static string const quotedblbase_def =
76 "\\ProvideTextCommandDefault{\\quotedblbase}{%\n"
77 " \\raisebox{-1.4ex}[1ex][.5ex]{\\textquotedblright}%\n"
78 " \\penalty10000\\hskip0em\\relax%\n"
81 static string const quotesinglbase_def =
82 "\\ProvideTextCommandDefault{\\quotesinglbase}{%\n"
83 " \\raisebox{-1.4ex}[1ex][.5ex]{\\textquoteright}%\n"
84 " \\penalty10000\\hskip0em\\relax%\n"
87 static string const guillemotleft_def =
88 "\\ProvideTextCommandDefault{\\guillemotleft}{%\n"
89 " {\\usefont{U}{lasy}{m}{n}\\char'50\\kern-.15em\\char'50}%\n"
90 "\\penalty10000\\hskip0pt\\relax%\n"
93 static string const guillemotright_def =
94 "\\ProvideTextCommandDefault{\\guillemotright}{%\n"
95 " \\penalty10000\\hskip0pt%\n"
96 " {\\usefont{U}{lasy}{m}{n}\\char'51\\kern-.15em\\char'51}%\n"
99 static string const guilsinglleft_def =
100 "\\ProvideTextCommandDefault{\\guilsinglleft}{%\n"
101 " {\\usefont{U}{lasy}{m}{n}\\char'50}%\n"
102 " \\penalty10000\\hskip0pt\\relax%\n"
105 static string const guilsinglright_def =
106 "\\ProvideTextCommandDefault{\\guilsinglright}{%\n"
107 " \\penalty10000\\hskip0pt%\n"
108 " {\\usefont{U}{lasy}{m}{n}\\char'51}%\n"
111 static string const paragraphleftindent_def =
112 "\\newenvironment{LyXParagraphLeftIndent}[1]%\n"
114 " \\begin{list}{}{%\n"
115 " \\setlength{\\topsep}{0pt}%\n"
116 " \\addtolength{\\leftmargin}{#1}\n"
117 // ho hum, yet more things commented out with no hint as to why they
118 // weren't just removed
119 // "%% \\addtolength{\\leftmargin}{#1\\textwidth}\n"
120 // "%% \\setlength{\\textwidth}{#2\\textwidth}\n"
121 // "%% \\setlength\\listparindent\\parindent%\n"
122 // "%% \\setlength\\itemindent\\parindent%\n"
123 " \\setlength{\\parsep}{0pt plus 1pt}%\n"
129 static string const floatingfootnote_def =
130 "%% Special footnote code from the package 'stblftnt.sty'\n"
131 "%% Author: Robin Fairbairns -- Last revised Dec 13 1996\n"
132 "\\let\\SF@@footnote\\footnote\n"
133 "\\def\\footnote{\\ifx\\protect\\@typeset@protect\n"
134 " \\expandafter\\SF@@footnote\n"
136 " \\expandafter\\SF@gobble@opt\n"
139 "\\expandafter\\def\\csname SF@gobble@opt \\endcsname{\\@ifnextchar[%]\n"
140 " \\SF@gobble@twobracket\n"
143 "\\edef\\SF@gobble@opt{\\noexpand\\protect\n"
144 " \\expandafter\\noexpand\\csname SF@gobble@opt \\endcsname}\n"
145 "\\def\\SF@gobble@twobracket[#1]#2{}\n";
147 static string const boldsymbol_def =
148 "%% Bold symbol macro for standard LaTeX users\n"
149 "\\providecommand{\\boldsymbol}[1]{\\mbox{\\boldmath $#1$}}\n";
151 static string const binom_def =
152 "%% Binom macro for standard LaTeX users\n"
153 "\\newcommand{\\binom}[2]{{#1 \\choose #2}}\n";
155 static string const mathcircumflex_def =
156 "%% For printing a cirumflex inside a formula\n"
157 "\\newcommand{\\mathcircumflex}[0]{\\mbox{\\^{}}}\n";
159 static string const tabularnewline_def =
160 "%% Because html converters don't know tabularnewline\n"
161 "\\providecommand{\\tabularnewline}{\\\\}\n";
163 static string const lyxgreyedout_def =
164 "%% The greyedout annotation environment\n"
165 "\\newenvironment{lyxgreyedout}{\\textcolor[gray]{0.8}\\bgroup}{\\egroup}\n";
167 // We want to omit the file extension for includegraphics, but this does not
168 // work when the filename contains other dots.
169 // Idea from http://www.tex.ac.uk/cgi-bin/texfaq2html?label=unkgrfextn
170 static string const lyxdot_def =
171 "%% A simple dot to overcome graphicx limitations\n"
172 "\\newcommand{\\lyxdot}{.}\n";
174 static string const changetracking_dvipost_def =
175 "%% Change tracking with dvipost\n"
177 "\\dvipost{osstart color push Red}\n"
178 "\\dvipost{osend color pop}\n"
179 "\\dvipost{cbstart color push Blue}\n"
180 "\\dvipost{cbend color pop}\n"
181 "\\newcommand{\\lyxadded}[3]{\\changestart#3\\changeend}\n"
182 "\\newcommand{\\lyxdeleted}[3]{%\n"
183 "\\changestart\\overstrikeon#3\\overstrikeoff\\changeend}\n";
185 static string const changetracking_none_def =
186 "\\newcommand{\\lyxadded}[3]{#3}\n"
187 "\\newcommand{\\lyxdeleted}[3]{}\n";
190 /////////////////////////////////////////////////////////////////////
194 /////////////////////////////////////////////////////////////////////
196 LaTeXFeatures::PackagesList LaTeXFeatures::packages_;
199 LaTeXFeatures::LaTeXFeatures(Buffer const & b, BufferParams const & p,
200 OutputParams const & r)
201 : buffer_(&b), params_(p), runparams_(r)
205 bool LaTeXFeatures::useBabel() const
207 return lyxrc.language_use_babel ||
208 (bufferParams().language->lang() != lyxrc.default_language &&
209 !bufferParams().language->babel().empty()) ||
210 this->hasLanguages();
214 void LaTeXFeatures::require(string const & name)
216 if (isRequired(name))
219 features_.push_back(name);
223 void LaTeXFeatures::getAvailable()
226 support::FileName const real_file = libFileSearch("", "packages.lst");
228 if (real_file.empty())
231 lex.setFile(real_file);
236 // Make sure that we are clean
239 bool finished = false;
241 while (lex.isOK() && !finished) {
243 case Lexer::LEX_FEOF:
247 string const name = lex.getString();
248 PackagesList::const_iterator begin = packages_.begin();
249 PackagesList::const_iterator end = packages_.end();
250 if (find(begin, end, name) == end)
251 packages_.push_back(name);
257 void LaTeXFeatures::useLayout(string const & layoutname)
259 // Some code to avoid loops in dependency definition
260 static int level = 0;
261 const int maxlevel = 30;
262 if (level > maxlevel) {
263 lyxerr << "LaTeXFeatures::useLayout: maximum level of "
264 << "recursion attained by layout "
265 << layoutname << endl;
269 TextClass const & tclass = params_.getTextClass();
270 if (tclass.hasLayout(layoutname)) {
271 // Is this layout already in usedLayouts?
272 list<string>::const_iterator cit = usedLayouts_.begin();
273 list<string>::const_iterator end = usedLayouts_.end();
274 for (; cit != end; ++cit) {
275 if (layoutname == *cit)
279 Layout_ptr const & lyt = tclass[layoutname];
280 if (!lyt->depends_on().empty()) {
282 useLayout(lyt->depends_on());
285 usedLayouts_.push_back(layoutname);
287 lyxerr << "LaTeXFeatures::useLayout: layout `"
288 << layoutname << "' does not exist in this class"
296 bool LaTeXFeatures::isRequired(string const & name) const
298 return find(features_.begin(), features_.end(), name) != features_.end();
302 bool LaTeXFeatures::mustProvide(string const & name) const
304 return isRequired(name) && !params_.getTextClass().provides(name);
308 bool LaTeXFeatures::isAvailable(string const & name)
310 if (packages_.empty())
312 return find(packages_.begin(), packages_.end(), name) != packages_.end();
316 void LaTeXFeatures::addPreambleSnippet(string const & preamble)
318 FeaturesList::const_iterator begin = preamble_snippets_.begin();
319 FeaturesList::const_iterator end = preamble_snippets_.end();
320 if (find(begin, end, preamble) == end)
321 preamble_snippets_.push_back(preamble);
325 void LaTeXFeatures::useFloat(string const & name)
327 usedFloats_.insert(name);
328 // We only need float.sty if we use non builtin floats, or if we
329 // use the "H" modifier. This includes modified table and
330 // figure floats. (Lgb)
331 Floating const & fl = params_.getTextClass().floats().getType(name);
332 if (!fl.type().empty() && !fl.builtin()) {
338 void LaTeXFeatures::useLanguage(Language const * lang)
340 if (!lang->babel().empty())
341 UsedLanguages_.insert(lang);
345 void LaTeXFeatures::includeFile(docstring const & key, string const & name)
347 IncludedFiles_[key] = name;
351 bool LaTeXFeatures::hasLanguages() const
353 return !UsedLanguages_.empty();
357 string LaTeXFeatures::getLanguages() const
359 ostringstream languages;
361 LanguageList::const_iterator const begin = UsedLanguages_.begin();
362 for (LanguageList::const_iterator cit = begin;
363 cit != UsedLanguages_.end();
367 languages << (*cit)->babel();
369 return languages.str();
373 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding) const
375 // This does only find encodings of languages supported by babel, but
376 // that does not matter since we don't have a language with an
377 // encoding supported by inputenc but without babel support.
378 set<string> encodings;
379 LanguageList::const_iterator it = UsedLanguages_.begin();
380 LanguageList::const_iterator end = UsedLanguages_.end();
381 for (; it != end; ++it)
382 if ((*it)->encoding()->latexName() != doc_encoding &&
383 (*it)->encoding()->package() == Encoding::inputenc)
384 encodings.insert((*it)->encoding()->latexName());
390 char const * simplefeatures[] = {
391 // note that the package order here will be the same in the LaTeX-output
424 int const nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
429 string const LaTeXFeatures::getPackages() const
431 ostringstream packages;
432 TextClass const & tclass = params_.getTextClass();
435 // These are all the 'simple' includes. i.e
436 // packages which we just \usepackage{package}
438 for (int i = 0; i < nb_simplefeatures; ++i) {
439 if (mustProvide(simplefeatures[i]))
440 packages << "\\usepackage{"
441 << simplefeatures[i] << "}\n";
445 // The rest of these packages are somewhat more complicated
449 if (mustProvide("amsmath")
450 && params_.use_amsmath != BufferParams::package_off) {
451 packages << "\\usepackage{amsmath}\n";
454 // wasysym is a simple feature, but it must be after amsmath if both
456 // wasysym redefines some integrals (e.g. iint) from amsmath. That
457 // leads to inconsistent integrals. We only load this package if
458 // esint is used, since esint redefines all relevant integral
459 // symbols from wasysym and amsmath.
460 // See http://bugzilla.lyx.org/show_bug.cgi?id=1942
461 if (mustProvide("wasysym") && isRequired("esint") &&
462 params_.use_esint != BufferParams::package_off)
463 packages << "\\usepackage{wasysym}\n";
466 if (mustProvide("color")) {
467 if (params_.graphicsDriver == "default")
468 packages << "\\usepackage{color}\n";
470 packages << "\\usepackage["
471 << params_.graphicsDriver
476 if (isRequired("makeidx")) {
477 if (!tclass.provides("makeidx"))
478 packages << "\\usepackage{makeidx}\n";
479 packages << "\\makeindex\n";
483 if (mustProvide("graphicx") && params_.graphicsDriver != "none") {
484 if (params_.graphicsDriver == "default")
485 packages << "\\usepackage{graphicx}\n";
487 packages << "\\usepackage["
488 << params_.graphicsDriver
491 // shadecolor for shaded
492 if (mustProvide("framed") && mustProvide("color")) {
493 RGBColor c = RGBColor(lcolor.getX11Name(Color::shadedbg));
494 //255.0 to force conversion to double
495 //NOTE As Jürgen Spitzmüller pointed out, an alternative would be
496 //to use the xcolor package instead, and then we can do
497 // \define{shadcolor}{RGB}...
498 //and not do any conversion. We'd then need to require xcolor
499 //in InsetNote::validate().
500 int const stmSize = packages.precision(2);
501 packages << "\\definecolor{shadecolor}{rgb}{"
502 << c.r/255.0 << ',' << c.g/255.0 << ',' << c.b/255.0 << "}\n";
503 packages.precision(stmSize);
506 // lyxskak.sty --- newer chess support based on skak.sty
507 if (mustProvide("chess")) {
508 packages << "\\usepackage[ps,mover]{lyxskak}\n";
512 if ((params_.spacing().getSpace() != Spacing::Single
513 && !params_.spacing().isDefault())
514 || isRequired("setspace")) {
515 packages << "\\usepackage{setspace}\n";
517 switch (params_.spacing().getSpace()) {
518 case Spacing::Default:
519 case Spacing::Single:
520 // we dont use setspace.sty so dont print anything
521 //packages += "\\singlespacing\n";
523 case Spacing::Onehalf:
524 packages << "\\onehalfspacing\n";
526 case Spacing::Double:
527 packages << "\\doublespacing\n";
530 packages << "\\setstretch{"
531 << params_.spacing().getValue() << "}\n";
536 if (mustProvide("amssymb")
537 || params_.use_amsmath == BufferParams::package_on)
538 packages << "\\usepackage{amssymb}\n";
540 // esint must be after amsmath and wasysym, since it will redeclare
541 // inconsistent integral symbols
542 if (mustProvide("esint")
543 && params_.use_esint != BufferParams::package_off)
544 packages << "\\usepackage{esint}\n";
547 if (mustProvide("url"))
548 packages << "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
549 " {\\newcommand{\\url}{\\texttt}}\n";
552 if (mustProvide("natbib")) {
553 packages << "\\usepackage[";
554 if (params_.getEngine() == biblio::ENGINE_NATBIB_NUMERICAL) {
555 packages << "numbers";
557 packages << "authoryear";
559 packages << "]{natbib}\n";
562 // jurabib -- we need version 0.6 at least.
563 if (mustProvide("jurabib")) {
564 packages << "\\usepackage{jurabib}[2004/01/25]\n";
567 // bibtopic -- the dot provides the aux file naming which
569 if (mustProvide("bibtopic")) {
570 packages << "\\usepackage[dot]{bibtopic}\n";
573 if (mustProvide("xy"))
574 packages << "\\usepackage[all]{xy}\n";
576 if (mustProvide("nomencl")) {
577 // Make it work with the new and old version of the package,
578 // but don't use the compatibility option since it is
579 // incompatible to other packages.
580 packages << "\\usepackage{nomencl}\n"
581 "% the following is useful when we have the old nomencl.sty package\n"
582 "\\providecommand{\\printnomenclature}{\\printglossary}\n"
583 "\\providecommand{\\makenomenclature}{\\makeglossary}\n"
584 "\\makenomenclature\n";
587 if (mustProvide("listings"))
588 packages << "\\usepackage{listings}\n";
590 return packages.str();
594 string const LaTeXFeatures::getMacros() const
596 ostringstream macros;
598 if (!preamble_snippets_.empty())
600 FeaturesList::const_iterator pit = preamble_snippets_.begin();
601 FeaturesList::const_iterator pend = preamble_snippets_.end();
602 for (; pit != pend; ++pit) {
603 macros << *pit << '\n';
606 if (mustProvide("LyX"))
607 macros << lyx_def << '\n';
609 if (mustProvide("lyxline"))
610 macros << lyxline_def << '\n';
612 if (mustProvide("noun"))
613 macros << noun_def << '\n';
615 if (mustProvide("lyxarrow"))
616 macros << lyxarrow_def << '\n';
619 if (mustProvide("quotesinglbase"))
620 macros << quotesinglbase_def << '\n';
621 if (mustProvide("quotedblbase"))
622 macros << quotedblbase_def << '\n';
623 if (mustProvide("guilsinglleft"))
624 macros << guilsinglleft_def << '\n';
625 if (mustProvide("guilsinglright"))
626 macros << guilsinglright_def << '\n';
627 if (mustProvide("guillemotleft"))
628 macros << guillemotleft_def << '\n';
629 if (mustProvide("guillemotright"))
630 macros << guillemotright_def << '\n';
633 if (mustProvide("boldsymbol") && !isRequired("amsmath"))
634 macros << boldsymbol_def << '\n';
635 if (mustProvide("binom") && !isRequired("amsmath"))
636 macros << binom_def << '\n';
637 if (mustProvide("mathcircumflex"))
638 macros << mathcircumflex_def << '\n';
641 if (mustProvide("ParagraphLeftIndent"))
642 macros << paragraphleftindent_def;
643 if (mustProvide("NeedLyXFootnoteCode"))
644 macros << floatingfootnote_def;
646 // some problems with tex->html converters
647 if (mustProvide("NeedTabularnewline"))
648 macros << tabularnewline_def;
650 // greyedout environment (note inset)
651 if (mustProvide("lyxgreyedout"))
652 macros << lyxgreyedout_def;
654 if (mustProvide("lyxdot"))
655 macros << lyxdot_def << '\n';
658 getFloatDefinitions(macros);
661 if (mustProvide("ct-dvipost")) {
662 macros << changetracking_dvipost_def;
664 if (mustProvide("ct-xcolor-soul")) {
665 RGBColor cadd = RGBColor(lcolor.getX11Name(Color::addedtext));
666 macros << "\\providecolor{lyxadded}{rgb}{"
667 << cadd.r/255 << ',' << cadd.g/255 << ',' << cadd.b/255 << "}\n";
669 RGBColor cdel = RGBColor(lcolor.getX11Name(Color::deletedtext));
670 macros << "\\providecolor{lyxdeleted}{rgb}{"
671 << cdel.r/255 << ',' << cdel.g/255 << ',' << cdel.b/255 << "}\n";
673 macros << "\\newcommand{\\lyxadded}[3]{{\\color{lyxadded}#3}}\n"
674 << "\\newcommand{\\lyxdeleted}[3]{{\\color{lyxdeleted}\\st{#3}}}\n";
676 if (mustProvide("ct-none")) {
677 macros << changetracking_none_def;
684 string const LaTeXFeatures::getBabelOptions() const
688 LanguageList::const_iterator it = UsedLanguages_.begin();
689 LanguageList::const_iterator end = UsedLanguages_.end();
690 for (; it != end; ++it)
691 if (!(*it)->latex_options().empty())
692 tmp << (*it)->latex_options() << '\n';
693 if (!params_.language->latex_options().empty())
694 tmp << params_.language->latex_options() << '\n';
700 docstring const LaTeXFeatures::getTClassPreamble() const
702 // the text class specific preamble
703 TextClass const & tclass = params_.getTextClass();
704 odocstringstream tcpreamble;
706 tcpreamble << tclass.preamble();
708 list<string>::const_iterator cit = usedLayouts_.begin();
709 list<string>::const_iterator end = usedLayouts_.end();
710 for (; cit != end; ++cit) {
711 tcpreamble << tclass[*cit]->preamble();
714 CharStyles::iterator cs = tclass.charstyles().begin();
715 CharStyles::iterator csend = tclass.charstyles().end();
716 for (; cs != csend; ++cs) {
717 if (isRequired(cs->name))
718 tcpreamble << cs->preamble;
721 return tcpreamble.str();
725 docstring const LaTeXFeatures::getLyXSGMLEntities() const
727 // Definition of entities used in the document that are LyX related.
728 odocstringstream entities;
730 if (mustProvide("lyxarrow")) {
731 entities << "<!ENTITY lyxarrow \"->\">" << '\n';
734 return entities.str();
738 docstring const LaTeXFeatures::getIncludedFiles(string const & fname) const
740 odocstringstream sgmlpreamble;
742 docstring const basename(from_utf8(onlyPath(fname)));
744 FileMap::const_iterator end = IncludedFiles_.end();
745 for (FileMap::const_iterator fi = IncludedFiles_.begin();
748 sgmlpreamble << "\n<!ENTITY " << fi->first
749 << (isSGMLFilename(fi->second) ? " SYSTEM \"" : " \"")
750 << makeRelPath(from_utf8(fi->second), basename) << "\">";
752 return sgmlpreamble.str();
756 void LaTeXFeatures::showStruct() const {
757 lyxerr << "LyX needs the following commands when LaTeXing:"
758 << "\n***** Packages:" << getPackages()
759 << "\n***** Macros:" << getMacros()
760 << "\n***** Textclass stuff:" << to_utf8(getTClassPreamble())
761 << "\n***** done." << endl;
765 Buffer const & LaTeXFeatures::buffer() const
771 void LaTeXFeatures::setBuffer(Buffer const & buffer)
777 BufferParams const & LaTeXFeatures::bufferParams() const
783 void LaTeXFeatures::getFloatDefinitions(ostream & os) const
785 FloatList const & floats = params_.getTextClass().floats();
787 // Here we will output the code to create the needed float styles.
788 // We will try to do this as minimal as possible.
789 // \floatstyle{ruled}
790 // \newfloat{algorithm}{htbp}{loa}
791 // \floatname{algorithm}{Algorithm}
792 UsedFloats::const_iterator cit = usedFloats_.begin();
793 UsedFloats::const_iterator end = usedFloats_.end();
794 // ostringstream floats;
795 for (; cit != end; ++cit) {
796 Floating const & fl = floats.getType((*cit));
798 // For builtin floats we do nothing.
799 if (fl.builtin()) continue;
801 // We have to special case "table" and "figure"
802 if (fl.type() == "tabular" || fl.type() == "figure") {
803 // Output code to modify "table" or "figure"
804 // but only if builtin == false
805 // and that have to be true at this point in the
807 string const type = fl.type();
808 string const placement = fl.placement();
809 string const style = fl.style();
810 if (!style.empty()) {
811 os << "\\floatstyle{" << style << "}\n"
812 << "\\restylefloat{" << type << "}\n";
814 if (!placement.empty()) {
815 os << "\\floatplacement{" << type << "}{"
816 << placement << "}\n";
819 // The other non builtin floats.
821 string const type = fl.type();
822 string const placement = fl.placement();
823 string const ext = fl.ext();
824 string const within = fl.within();
825 string const style = fl.style();
826 string const name = fl.name();
827 os << "\\floatstyle{" << style << "}\n"
828 << "\\newfloat{" << type << "}{" << placement
829 << "}{" << ext << '}';
831 os << '[' << within << ']';
833 << "\\floatname{" << type << "}{"
836 // What missing here is to code to minimalize the code
837 // output so that the same floatstyle will not be
838 // used several times, when the same style is still in