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"
20 #include "BufferParams.h"
22 #include "Converter.h"
25 #include "FloatList.h"
30 #include "TextClass.h"
32 #include "insets/InsetLayout.h"
34 #include "support/debug.h"
35 #include "support/docstream.h"
36 #include "support/FileName.h"
37 #include "support/filetools.h"
38 #include "support/gettext.h"
39 #include "support/lstrings.h"
45 using namespace lyx::support;
50 /////////////////////////////////////////////////////////////////////
54 /////////////////////////////////////////////////////////////////////
56 //\NeedsTeXFormat{LaTeX2e}
57 //\ProvidesPackage{lyx}[1996/01/11 LLE v0.2 (LyX LaTeX Extensions)]
58 //\message{LyX LaTeX Extensions (LLE v0.2) of 11-Jan-1996.}
60 static docstring const lyx_def = from_ascii(
61 "\\providecommand{\\LyX}{L\\kern-.1667em\\lower.25em\\hbox{Y}\\kern-.125emX\\@}");
63 static docstring const lyxline_def = from_ascii(
64 "\\newcommand{\\lyxline}[1][1pt]{%\n"
66 " \\rule[.5ex]{\\linewidth}{#1}\\par}");
68 static docstring const noun_def = from_ascii(
69 "\\newcommand{\\noun}[1]{\\textsc{#1}}");
71 static docstring const lyxarrow_def = from_ascii(
72 "\\DeclareRobustCommand*{\\lyxarrow}{%\n"
74 "{\\leavevmode\\,$\\triangleleft$\\,\\allowbreak}\n"
75 "{\\leavevmode\\,$\\triangleright$\\,\\allowbreak}}");
77 // for quotes without babel. This does not give perfect results, but
78 // anybody serious about non-english quotes should use babel (JMarc).
80 static docstring const quotedblbase_def = from_ascii(
81 "\\ProvideTextCommandDefault{\\quotedblbase}{%\n"
82 " \\raisebox{-1.4ex}[1ex][.5ex]{\\textquotedblright}%\n"
83 " \\penalty10000\\hskip0em\\relax%\n"
86 static docstring const quotesinglbase_def = from_ascii(
87 "\\ProvideTextCommandDefault{\\quotesinglbase}{%\n"
88 " \\raisebox{-1.4ex}[1ex][.5ex]{\\textquoteright}%\n"
89 " \\penalty10000\\hskip0em\\relax%\n"
92 static docstring const guillemotleft_def = from_ascii(
93 "\\ProvideTextCommandDefault{\\guillemotleft}{%\n"
94 " {\\usefont{U}{lasy}{m}{n}\\char'50\\kern-.15em\\char'50}%\n"
95 "\\penalty10000\\hskip0pt\\relax%\n"
98 static docstring const guillemotright_def = from_ascii(
99 "\\ProvideTextCommandDefault{\\guillemotright}{%\n"
100 " \\penalty10000\\hskip0pt%\n"
101 " {\\usefont{U}{lasy}{m}{n}\\char'51\\kern-.15em\\char'51}%\n"
104 static docstring const guilsinglleft_def = from_ascii(
105 "\\ProvideTextCommandDefault{\\guilsinglleft}{%\n"
106 " {\\usefont{U}{lasy}{m}{n}\\char'50}%\n"
107 " \\penalty10000\\hskip0pt\\relax%\n"
110 static docstring const guilsinglright_def = from_ascii(
111 "\\ProvideTextCommandDefault{\\guilsinglright}{%\n"
112 " \\penalty10000\\hskip0pt%\n"
113 " {\\usefont{U}{lasy}{m}{n}\\char'51}%\n"
116 static docstring const paragraphleftindent_def = from_ascii(
117 "\\newenvironment{LyXParagraphLeftIndent}[1]%\n"
119 " \\begin{list}{}{%\n"
120 " \\setlength{\\topsep}{0pt}%\n"
121 " \\addtolength{\\leftmargin}{#1}\n"
122 // ho hum, yet more things commented out with no hint as to why they
123 // weren't just removed
124 // "%% \\addtolength{\\leftmargin}{#1\\textwidth}\n"
125 // "%% \\setlength{\\textwidth}{#2\\textwidth}\n"
126 // "%% \\setlength\\listparindent\\parindent%\n"
127 // "%% \\setlength\\itemindent\\parindent%\n"
128 " \\setlength{\\parsep}{0pt plus 1pt}%\n"
134 static docstring const floatingfootnote_def = from_ascii(
135 "%% Special footnote code from the package 'stblftnt.sty'\n"
136 "%% Author: Robin Fairbairns -- Last revised Dec 13 1996\n"
137 "\\let\\SF@@footnote\\footnote\n"
138 "\\def\\footnote{\\ifx\\protect\\@typeset@protect\n"
139 " \\expandafter\\SF@@footnote\n"
141 " \\expandafter\\SF@gobble@opt\n"
144 "\\expandafter\\def\\csname SF@gobble@opt \\endcsname{\\@ifnextchar[%]\n"
145 " \\SF@gobble@twobracket\n"
148 "\\edef\\SF@gobble@opt{\\noexpand\\protect\n"
149 " \\expandafter\\noexpand\\csname SF@gobble@opt \\endcsname}\n"
150 "\\def\\SF@gobble@twobracket[#1]#2{}\n");
152 static docstring const binom_def = from_ascii(
153 "%% Binom macro for standard LaTeX users\n"
154 "\\newcommand{\\binom}[2]{{#1 \\choose #2}}\n");
156 static docstring const mathcircumflex_def = from_ascii(
157 "%% For printing a cirumflex inside a formula\n"
158 "\\newcommand{\\mathcircumflex}[0]{\\mbox{\\^{}}}\n");
160 static docstring const tabularnewline_def = from_ascii(
161 "%% Because html converters don't know tabularnewline\n"
162 "\\providecommand{\\tabularnewline}{\\\\}\n");
164 static docstring const lyxgreyedout_def = from_ascii(
165 "%% The greyedout annotation environment\n"
166 "\\newenvironment{lyxgreyedout}{\\textcolor{note_fontcolor}\\bgroup}{\\egroup}\n");
168 // We want to omit the file extension for includegraphics, but this does not
169 // work when the filename contains other dots.
170 // Idea from http://www.tex.ac.uk/cgi-bin/texfaq2html?label=unkgrfextn
171 static docstring const lyxdot_def = from_ascii(
172 "%% A simple dot to overcome graphicx limitations\n"
173 "\\newcommand{\\lyxdot}{.}\n");
175 static docstring const changetracking_dvipost_def = from_ascii(
176 "%% Change tracking with dvipost\n"
178 "\\dvipost{osstart color push Red}\n"
179 "\\dvipost{osend color pop}\n"
180 "\\dvipost{cbstart color push Blue}\n"
181 "\\dvipost{cbend color pop}\n"
182 "\\newcommand{\\lyxadded}[3]{\\changestart#3\\changeend}\n"
183 "\\newcommand{\\lyxdeleted}[3]{%\n"
184 "\\changestart\\overstrikeon#3\\overstrikeoff\\changeend}\n");
186 static docstring const changetracking_xcolor_ulem_def = from_ascii(
187 "%% Change tracking with ulem\n"
188 "\\newcommand{\\lyxadded}[3]{{\\color{lyxadded}{}#3}}\n"
189 "\\newcommand{\\lyxdeleted}[3]{{\\color{lyxdeleted}\\sout{#3}}}\n");
191 static docstring const changetracking_xcolor_ulem_hyperref_def = from_ascii(
192 "%% Change tracking with ulem\n"
193 "\\newcommand{\\lyxadded}[3]{{\\texorpdfstring{\\color{lyxadded}{}}{}#3}}\n"
194 "\\newcommand{\\lyxdeleted}[3]{{\\texorpdfstring{\\color{lyxdeleted}\\sout{#3}}{}}}\n");
196 static docstring const changetracking_none_def = from_ascii(
197 "\\newcommand{\\lyxadded}[3]{#3}\n"
198 "\\newcommand{\\lyxdeleted}[3]{}\n");
200 static docstring const textgreek_def = from_ascii(
201 "\\DeclareRobustCommand{\\greektext}{%\n"
202 " \\fontencoding{LGR}\\selectfont\\def\\encodingdefault{LGR}}\n"
203 "\\DeclareRobustCommand{\\textgreek}[1]{\\leavevmode{\\greektext #1}}\n"
204 "\\DeclareFontEncoding{LGR}{}{}\n"
205 "\\DeclareTextSymbol{\\~}{LGR}{126}");
207 static docstring const textcyr_def = from_ascii(
208 "\\DeclareRobustCommand{\\cyrtext}{%\n"
209 " \\fontencoding{T2A}\\selectfont\\def\\encodingdefault{T2A}}\n"
210 "\\DeclareRobustCommand{\\textcyr}[1]{\\leavevmode{\\cyrtext #1}}\n"
211 "\\AtBeginDocument{\\DeclareFontEncoding{T2A}{}{}}\n");
213 static docstring const lyxmathsym_def = from_ascii(
214 "\\newcommand{\\lyxmathsym}[1]{\\ifmmode\\begingroup\\def\\b@ld{bold}\n"
215 " \\text{\\ifx\\math@version\\b@ld\\bfseries\\fi#1}\\endgroup\\else#1\\fi}\n");
217 static docstring const papersizedvi_def = from_ascii(
218 "\\special{papersize=\\the\\paperwidth,\\the\\paperheight}\n");
220 static docstring const papersizepdf_def = from_ascii(
221 "\\pdfpageheight\\paperheight\n"
222 "\\pdfpagewidth\\paperwidth\n");
224 static docstring const cedilla_def = from_ascii(
225 "\\newcommand{\\docedilla}[2]{\\underaccent{#1\\mathchar'30}{#2}}\n"
226 "\\newcommand{\\cedilla}[1]{\\mathpalette\\docedilla{#1}}\n");
228 static docstring const subring_def = from_ascii(
229 "\\newcommand{\\dosubring}[2]{\\underaccent{#1\\mathchar'27}{#2}}\n"
230 "\\newcommand{\\subring}[1]{\\mathpalette\\dosubring{#1}}\n");
232 static docstring const subdot_def = from_ascii(
233 "\\newcommand{\\dosubdot}[2]{\\underaccent{#1.}{#2}}\n"
234 "\\newcommand{\\subdot}[1]{\\mathpalette\\dosubdot{#1}}\n");
236 static docstring const subhat_def = from_ascii(
237 "\\newcommand{\\dosubhat}[2]{\\underaccent{#1\\mathchar'136}{#2}}\n"
238 "\\newcommand{\\subhat}[1]{\\mathpalette\\dosubhat{#1}}\n");
240 static docstring const subtilde_def = from_ascii(
241 "\\newcommand{\\dosubtilde}[2]{\\underaccent{#1\\mathchar'176}{#2}}\n"
242 "\\newcommand{\\subtilde}[1]{\\mathpalette\\dosubtilde{#1}}\n");
244 static docstring const dacute_def = from_ascii(
245 "\\DeclareMathAccent{\\dacute}{\\mathalpha}{operators}{'175}\n");
247 static docstring const tipasymb_def = from_ascii(
248 "\\DeclareFontEncoding{T3}{}{}\n"
249 "\\DeclareSymbolFont{tipasymb}{T3}{cmr}{m}{n}\n");
251 static docstring const dgrave_def = from_ascii(
252 "\\DeclareMathAccent{\\dgrave}{\\mathord}{tipasymb}{'15}\n");
254 static docstring const rcap_def = from_ascii(
255 "\\DeclareMathAccent{\\rcap}{\\mathord}{tipasymb}{'20}\n");
257 static docstring const ogonek_def = from_ascii(
258 "\\newcommand{\\doogonek}[2]{\\setbox0=\\hbox{$#1#2$}\\underaccent{#1\\mkern-6mu\n"
259 " \\ifx#2O\\hskip0.5\\wd0\\else\\ifx#2U\\hskip0.5\\wd0\\else\\hskip\\wd0\\fi\\fi\n"
260 " \\ifx#2o\\mkern-2mu\\else\\ifx#2e\\mkern-1mu\\fi\\fi\n"
261 " \\mathchar\"0\\hexnumber@\\symtipasymb0C}{#2}}\n"
262 "\\newcommand{\\ogonek}[1]{\\mathpalette\\doogonek{#1}}\n");
264 /////////////////////////////////////////////////////////////////////
268 /////////////////////////////////////////////////////////////////////
270 LaTeXFeatures::Packages LaTeXFeatures::packages_;
273 LaTeXFeatures::LaTeXFeatures(Buffer const & b, BufferParams const & p,
274 OutputParams const & r)
275 : buffer_(&b), params_(p), runparams_(r), in_float_(false)
279 bool LaTeXFeatures::useBabel() const
281 return lyxrc.language_use_babel ||
282 (bufferParams().language->lang() != lyxrc.default_language &&
283 !bufferParams().language->babel().empty()) ||
284 this->hasLanguages();
288 void LaTeXFeatures::require(string const & name)
290 features_.insert(name);
294 void LaTeXFeatures::require(set<string> const & names)
296 features_.insert(names.begin(), names.end());
300 void LaTeXFeatures::getAvailable()
303 support::FileName const real_file = libFileSearch("", "packages.lst");
305 if (real_file.empty())
308 lex.setFile(real_file);
313 // Make sure that we are clean
316 bool finished = false;
318 while (lex.isOK() && !finished) {
320 case Lexer::LEX_FEOF:
324 packages_.insert(lex.getString());
330 void LaTeXFeatures::useLayout(docstring const & layoutname)
332 // Some code to avoid loops in dependency definition
333 static int level = 0;
334 const int maxlevel = 30;
335 if (level > maxlevel) {
336 lyxerr << "LaTeXFeatures::useLayout: maximum level of "
337 << "recursion attained by layout "
338 << to_utf8(layoutname) << endl;
342 DocumentClass const & tclass = params_.documentClass();
343 if (tclass.hasLayout(layoutname)) {
344 // Is this layout already in usedLayouts?
345 if (find(usedLayouts_.begin(), usedLayouts_.end(), layoutname)
346 != usedLayouts_.end())
349 Layout const & layout = tclass[layoutname];
350 require(layout.requires());
352 if (!layout.depends_on().empty()) {
354 useLayout(layout.depends_on());
357 usedLayouts_.push_back(layoutname);
359 lyxerr << "LaTeXFeatures::useLayout: layout `"
360 << to_utf8(layoutname) << "' does not exist in this class"
368 void LaTeXFeatures::useInsetLayout(InsetLayout const & lay)
370 docstring const & lname = lay.name();
371 DocumentClass const & tclass = params_.documentClass();
373 // this is a default inset layout, nothing useful here
374 if (!tclass.hasInsetLayout(lname))
376 // Is this layout already in usedInsetLayouts?
377 if (find(usedInsetLayouts_.begin(), usedInsetLayouts_.end(), lname)
378 != usedInsetLayouts_.end())
381 require(lay.requires());
382 usedInsetLayouts_.push_back(lname);
386 bool LaTeXFeatures::isRequired(string const & name) const
388 return features_.find(name) != features_.end();
392 bool LaTeXFeatures::mustProvide(string const & name) const
394 return isRequired(name) && !params_.documentClass().provides(name);
398 bool LaTeXFeatures::isAvailable(string const & name)
400 string::size_type const i = name.find("->");
401 if (i != string::npos) {
402 string const from = name.substr(0,i);
403 string const to = name.substr(i+2);
404 //LYXERR0("from=[" << from << "] to=[" << to << "]");
405 return theConverters().isReachable(from, to);
408 if (packages_.empty())
411 if (suffixIs(n, ".sty"))
412 n.erase(name.length() - 4);
413 return packages_.find(n) != packages_.end();
417 void LaTeXFeatures::addPreambleSnippet(string const & preamble)
419 SnippetList::const_iterator begin = preamble_snippets_.begin();
420 SnippetList::const_iterator end = preamble_snippets_.end();
421 if (find(begin, end, preamble) == end)
422 preamble_snippets_.push_back(preamble);
426 void LaTeXFeatures::useFloat(string const & name, bool subfloat)
428 if (!usedFloats_[name])
429 usedFloats_[name] = subfloat;
432 // We only need float.sty if we use non builtin floats, or if we
433 // use the "H" modifier. This includes modified table and
434 // figure floats. (Lgb)
435 Floating const & fl = params_.documentClass().floats().getType(name);
436 if (!fl.floattype().empty() && fl.needsFloatPkg()) {
442 void LaTeXFeatures::useLanguage(Language const * lang)
444 if (!lang->babel().empty())
445 UsedLanguages_.insert(lang);
446 if (lang->lang() == "vietnamese")
447 require("vietnamese");
448 // CJK languages do not have a babel name.
449 // They use the CJK package
450 if (lang->encoding()->package() == Encoding::CJK)
452 // japanese package is special
453 if (lang->encoding()->package() == Encoding::japanese)
458 void LaTeXFeatures::includeFile(docstring const & key, string const & name)
460 IncludedFiles_[key] = name;
464 bool LaTeXFeatures::hasLanguages() const
466 return !UsedLanguages_.empty();
470 string LaTeXFeatures::getLanguages() const
472 ostringstream languages;
474 LanguageList::const_iterator const begin = UsedLanguages_.begin();
475 for (LanguageList::const_iterator cit = begin;
476 cit != UsedLanguages_.end();
480 languages << (*cit)->babel();
482 return languages.str();
486 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding) const
488 // This does only find encodings of languages supported by babel, but
489 // that does not matter since we don't have a language with an
490 // encoding supported by inputenc but without babel support.
491 set<string> encodings;
492 LanguageList::const_iterator it = UsedLanguages_.begin();
493 LanguageList::const_iterator end = UsedLanguages_.end();
494 for (; it != end; ++it)
495 if ((*it)->encoding()->latexName() != doc_encoding &&
496 ((*it)->encoding()->package() == Encoding::inputenc
497 || (*it)->encoding()->package() == Encoding::japanese))
498 encodings.insert((*it)->encoding()->latexName());
504 char const * simplefeatures[] = {
505 // note that the package order here will be the same in the LaTeX-output
512 // subfig is handled in BufferParams.cpp
515 /*For a successful cooperation of the `wrapfig' package with the
516 `float' package you should load the `wrapfig' package *after*
517 the `float' package. See the caption package documentation
546 // listings is handled in BufferParams.cpp
554 int const nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
559 string const LaTeXFeatures::getColorOptions() const
561 ostringstream colors;
563 // Handling the color packages separately is needed to be able to load them
564 // before babel when hyperref is loaded with the colorlinks option
565 // for more info see Bufferparams.cpp
568 if (mustProvide("color") || mustProvide("xcolor")) {
569 string const package =
570 (mustProvide("xcolor") ? "xcolor" : "color");
571 if (params_.graphicsDriver == "default"
572 || params_.graphicsDriver == "none")
573 colors << "\\usepackage{" << package << "}\n";
575 colors << "\\usepackage["
576 << params_.graphicsDriver
577 << "]{" << package << "}\n";
580 // pdfcolmk must be loaded after color
581 if (mustProvide("pdfcolmk"))
582 colors << "\\usepackage{pdfcolmk}\n";
584 // the following 3 color commands must be set after color
585 // is loaded and before pdfpages, therefore add the command
586 // here define the set color
587 if (mustProvide("pagecolor")) {
588 colors << "\\definecolor{page_backgroundcolor}{rgb}{";
589 colors << outputLaTeXColor(params_.backgroundcolor) << "}\n";
590 // set the page color
591 colors << "\\pagecolor{page_backgroundcolor}\n";
594 if (mustProvide("fontcolor")) {
595 colors << "\\definecolor{document_fontcolor}{rgb}{";
596 colors << outputLaTeXColor(params_.fontcolor) << "}\n";
598 colors << "\\color{document_fontcolor}\n";
601 if (mustProvide("lyxgreyedout")) {
602 colors << "\\definecolor{note_fontcolor}{rgb}{";
603 colors << outputLaTeXColor(params_.notefontcolor) << "}\n";
604 // the color will be set together with the definition of
605 // the lyxgreyedout environment (see lyxgreyedout_def)
608 // color for shaded boxes
609 if (isRequired("framed") && mustProvide("color")) {
610 colors << "\\definecolor{shadecolor}{rgb}{";
611 colors << outputLaTeXColor(params_.boxbgcolor) << "}\n";
612 // this color is automatically used by the LaTeX-package "framed"
619 string const LaTeXFeatures::getPackages() const
621 ostringstream packages;
622 DocumentClass const & tclass = params_.documentClass();
624 // FIXME: currently, we can only load packages and macros known
626 // However, with the Require tag of layouts/custom insets,
627 // also inknown packages can be requested. They are silently
628 // swallowed now. We should change this eventually.
631 // These are all the 'simple' includes. i.e
632 // packages which we just \usepackage{package}
634 for (int i = 0; i < nb_simplefeatures; ++i) {
635 if (mustProvide(simplefeatures[i]))
636 packages << "\\usepackage{"
637 << simplefeatures[i] << "}\n";
641 // The rest of these packages are somewhat more complicated
645 // esint is preferred for esintoramsmath
646 if ((mustProvide("amsmath")
647 && params_.use_amsmath != BufferParams::package_off)
648 || (mustProvide("esintoramsmath")
649 && params_.use_esint == BufferParams::package_off
650 && params_.use_amsmath != BufferParams::package_off)) {
651 packages << "\\usepackage{amsmath}\n";
653 // amsbsy and amstext are already provided by amsmath
654 if (mustProvide("amsbsy"))
655 packages << "\\usepackage{amsbsy}\n";
656 if (mustProvide("amstext"))
657 packages << "\\usepackage{amstext}\n";
660 // wasysym is a simple feature, but it must be after amsmath if both
662 // wasysym redefines some integrals (e.g. iint) from amsmath. That
663 // leads to inconsistent integrals. We only load this package if
664 // the document does not contain integrals (then isRequired("esint")
665 // is false) or if esint is used, since esint redefines all relevant
666 // integral symbols from wasysym and amsmath.
667 // See http://www.lyx.org/trac/ticket/1942
668 if (mustProvide("wasysym") &&
669 (params_.use_esint != BufferParams::package_off || !isRequired("esint")))
670 packages << "\\usepackage{wasysym}\n";
672 // accents must be loaded after amsmath
673 if (mustProvide("accents"))
674 packages << "\\usepackage{accents}\n";
676 // mathdots must be loaded after amsmath
677 if (mustProvide("mathdots"))
678 packages << "\\usepackage{mathdots}\n";
680 // yhmath must be loaded after amsmath
681 if (mustProvide("yhmath"))
682 packages << "\\usepackage{yhmath}\n";
684 // [x]color and pdfcolmk are handled in getColorOptions() above
687 if (isRequired("makeidx") || isRequired("splitidx")) {
688 if (!tclass.provides("makeidx") && !isRequired("splitidx"))
689 packages << "\\usepackage{makeidx}\n";
690 if (!tclass.provides("splitidx") && isRequired("splitidx"))
691 packages << "\\usepackage{splitidx}\n";
692 packages << "\\makeindex\n";
696 if (mustProvide("graphicx") && params_.graphicsDriver != "none") {
697 if (params_.graphicsDriver == "default")
698 packages << "\\usepackage{graphicx}\n";
700 packages << "\\usepackage["
701 << params_.graphicsDriver
705 // lyxskak.sty --- newer chess support based on skak.sty
706 if (mustProvide("chess"))
707 packages << "\\usepackage[ps,mover]{lyxskak}\n";
710 if (mustProvide("setspace") && !tclass.provides("SetSpace"))
711 packages << "\\usepackage{setspace}\n";
714 if (mustProvide("amssymb")
715 || params_.use_amsmath == BufferParams::package_on)
716 packages << "\\usepackage{amssymb}\n";
718 // esint must be after amsmath and wasysym, since it will redeclare
719 // inconsistent integral symbols
720 if ((mustProvide("esint") || mustProvide("esintoramsmath")) &&
721 params_.use_esint != BufferParams::package_off)
722 packages << "\\usepackage{esint}\n";
725 // Some classes load natbib themselves, but still allow (or even require)
726 // plain numeric citations (ReVTeX is such a case, see bug 5182).
727 // This special case is indicated by the "natbib-internal" key.
728 if (mustProvide("natbib") && !tclass.provides("natbib-internal")) {
729 packages << "\\usepackage[";
730 if (params_.citeEngine() == ENGINE_NATBIB_NUMERICAL)
731 packages << "numbers";
733 packages << "authoryear";
734 packages << "]{natbib}\n";
737 // jurabib -- we need version 0.6 at least.
738 if (mustProvide("jurabib"))
739 packages << "\\usepackage{jurabib}[2004/01/25]\n";
741 // xargs -- we need version 1.09 at least
742 if (mustProvide("xargs"))
743 packages << "\\usepackage{xargs}[2008/03/08]\n";
745 // bibtopic -- the dot provides the aux file naming which
747 if (mustProvide("bibtopic"))
748 packages << "\\usepackage[dot]{bibtopic}\n";
750 if (mustProvide("xy"))
751 packages << "\\usepackage[all]{xy}\n";
753 if (mustProvide("ulem"))
754 packages << "\\PassOptionsToPackage{normalem}{ulem}\n"
755 "\\usepackage{ulem}\n";
757 if (mustProvide("mhchem") &&
758 params_.use_mhchem != BufferParams::package_off)
759 packages << "\\PassOptionsToPackage{version=3}{mhchem}\n"
760 "\\usepackage{mhchem}\n";
762 if (mustProvide("nomencl")) {
763 // Make it work with the new and old version of the package,
764 // but don't use the compatibility option since it is
765 // incompatible to other packages.
766 packages << "\\usepackage{nomencl}\n"
767 "% the following is useful when we have the old nomencl.sty package\n"
768 "\\providecommand{\\printnomenclature}{\\printglossary}\n"
769 "\\providecommand{\\makenomenclature}{\\makeglossary}\n"
770 "\\makenomenclature\n";
773 return packages.str();
777 string LaTeXFeatures::getPreambleSnippets() const
780 SnippetList::const_iterator pit = preamble_snippets_.begin();
781 SnippetList::const_iterator pend = preamble_snippets_.end();
782 for (; pit != pend; ++pit)
783 snip << *pit << '\n';
788 docstring const LaTeXFeatures::getMacros() const
790 odocstringstream macros;
792 if (!preamble_snippets_.empty()) {
794 macros << from_utf8(getPreambleSnippets());
797 if (mustProvide("papersize")) {
798 if (runparams_.flavor == OutputParams::LATEX)
799 macros << papersizedvi_def << '\n';
801 macros << papersizepdf_def << '\n';
804 if (mustProvide("LyX"))
805 macros << lyx_def << '\n';
807 if (mustProvide("lyxline"))
808 macros << lyxline_def << '\n';
810 if (mustProvide("noun"))
811 macros << noun_def << '\n';
813 if (mustProvide("lyxarrow"))
814 macros << lyxarrow_def << '\n';
816 if (mustProvide("textgreek")) {
817 // Avoid a LaTeX error if times fonts are used and the grtimes
818 // package is installed but actual fonts are not (bug 6469).
819 if (params_.fontsRoman == "times")
820 macros << subst(textgreek_def,
821 from_ascii("\\greektext #1"),
822 from_ascii("%\n \\IfFileExists"
823 "{grtm10.tfm}{}{\\fontfamily"
824 "{cmr}}\\greektext #1"))
827 macros << textgreek_def << '\n';
830 if (mustProvide("textcyr"))
831 macros << textcyr_def << '\n';
833 if (mustProvide("lyxmathsym"))
834 macros << lyxmathsym_def << '\n';
836 if (mustProvide("cedilla"))
837 macros << cedilla_def << '\n';
839 if (mustProvide("subring"))
840 macros << subring_def << '\n';
842 if (mustProvide("subdot"))
843 macros << subdot_def << '\n';
845 if (mustProvide("subhat"))
846 macros << subhat_def << '\n';
848 if (mustProvide("subtilde"))
849 macros << subtilde_def << '\n';
851 if (mustProvide("dacute"))
852 macros << dacute_def << '\n';
854 if (mustProvide("tipasymb"))
855 macros << tipasymb_def << '\n';
857 if (mustProvide("dgrave"))
858 macros << dgrave_def << '\n';
860 if (mustProvide("rcap"))
861 macros << rcap_def << '\n';
863 if (mustProvide("ogonek"))
864 macros << ogonek_def << '\n';
867 if (mustProvide("quotesinglbase"))
868 macros << quotesinglbase_def << '\n';
869 if (mustProvide("quotedblbase"))
870 macros << quotedblbase_def << '\n';
871 if (mustProvide("guilsinglleft"))
872 macros << guilsinglleft_def << '\n';
873 if (mustProvide("guilsinglright"))
874 macros << guilsinglright_def << '\n';
875 if (mustProvide("guillemotleft"))
876 macros << guillemotleft_def << '\n';
877 if (mustProvide("guillemotright"))
878 macros << guillemotright_def << '\n';
881 if (mustProvide("binom") && !isRequired("amsmath"))
882 macros << binom_def << '\n';
883 if (mustProvide("mathcircumflex"))
884 macros << mathcircumflex_def << '\n';
887 if (mustProvide("ParagraphLeftIndent"))
888 macros << paragraphleftindent_def;
889 if (mustProvide("NeedLyXFootnoteCode"))
890 macros << floatingfootnote_def;
892 // some problems with tex->html converters
893 if (mustProvide("NeedTabularnewline"))
894 macros << tabularnewline_def;
896 // greyed-out environment (note inset)
897 // the color is specified in the routine
898 // getColorOptions() to avoid LaTeX-package clashes
899 if (mustProvide("lyxgreyedout"))
900 macros << lyxgreyedout_def;
902 if (mustProvide("lyxdot"))
903 macros << lyxdot_def << '\n';
906 getFloatDefinitions(macros);
909 if (mustProvide("ct-dvipost"))
910 macros << changetracking_dvipost_def;
912 if (mustProvide("ct-xcolor-ulem")) {
913 int const prec = macros.precision(2);
915 RGBColor cadd = rgbFromHexName(lcolor.getX11Name(Color_addedtext));
916 macros << "\\providecolor{lyxadded}{rgb}{"
917 << cadd.r / 255.0 << ',' << cadd.g / 255.0 << ',' << cadd.b / 255.0 << "}\n";
919 RGBColor cdel = rgbFromHexName(lcolor.getX11Name(Color_deletedtext));
920 macros << "\\providecolor{lyxdeleted}{rgb}{"
921 << cdel.r / 255.0 << ',' << cdel.g / 255.0 << ',' << cdel.b / 255.0 << "}\n";
923 macros.precision(prec);
925 if (isRequired("hyperref"))
926 macros << changetracking_xcolor_ulem_hyperref_def;
928 macros << changetracking_xcolor_ulem_def;
931 if (mustProvide("ct-none"))
932 macros << changetracking_none_def;
938 string const LaTeXFeatures::getBabelOptions() const
942 LanguageList::const_iterator it = UsedLanguages_.begin();
943 LanguageList::const_iterator end = UsedLanguages_.end();
944 for (; it != end; ++it)
945 if (!(*it)->latex_options().empty())
946 tmp << (*it)->latex_options() << '\n';
947 if (!params_.language->latex_options().empty())
948 tmp << params_.language->latex_options() << '\n';
954 docstring const LaTeXFeatures::getTClassPreamble() const
956 // the text class specific preamble
957 DocumentClass const & tclass = params_.documentClass();
958 odocstringstream tcpreamble;
960 tcpreamble << tclass.preamble();
962 list<docstring>::const_iterator cit = usedLayouts_.begin();
963 list<docstring>::const_iterator end = usedLayouts_.end();
964 for (; cit != end; ++cit)
965 tcpreamble << tclass[*cit].preamble();
967 cit = usedInsetLayouts_.begin();
968 end = usedInsetLayouts_.end();
969 TextClass::InsetLayouts const & ils = tclass.insetLayouts();
970 for (; cit != end; ++cit) {
971 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
974 tcpreamble << it->second.preamble();
977 return tcpreamble.str();
981 docstring const LaTeXFeatures::getTClassHTMLPreamble() const
983 DocumentClass const & tclass = params_.documentClass();
984 odocstringstream tcpreamble;
986 tcpreamble << tclass.htmlpreamble();
988 list<docstring>::const_iterator cit = usedLayouts_.begin();
989 list<docstring>::const_iterator end = usedLayouts_.end();
990 for (; cit != end; ++cit)
991 tcpreamble << tclass[*cit].htmlpreamble();
993 cit = usedInsetLayouts_.begin();
994 end = usedInsetLayouts_.end();
995 TextClass::InsetLayouts const & ils = tclass.insetLayouts();
996 for (; cit != end; ++cit) {
997 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1000 tcpreamble << it->second.htmlpreamble();
1003 return tcpreamble.str();
1007 docstring const LaTeXFeatures::getTClassHTMLStyles() const {
1008 DocumentClass const & tclass = params_.documentClass();
1009 odocstringstream tcpreamble;
1011 list<docstring>::const_iterator cit = usedLayouts_.begin();
1012 list<docstring>::const_iterator end = usedLayouts_.end();
1013 for (; cit != end; ++cit)
1014 tcpreamble << tclass[*cit].htmlstyle();
1016 cit = usedInsetLayouts_.begin();
1017 end = usedInsetLayouts_.end();
1018 TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1019 for (; cit != end; ++cit) {
1020 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1021 if (it == ils.end())
1023 tcpreamble << it->second.htmlstyle();
1026 return tcpreamble.str();
1031 docstring const getFloatI18nPreamble(docstring const & type, docstring const & name, docstring const & lang)
1033 odocstringstream os;
1034 os << "\\addto\\captions" << lang
1035 << "{\\renewcommand{\\" << type << "name}{" << name << "}}\n";
1041 docstring const LaTeXFeatures::getTClassI18nPreamble(bool use_babel) const
1043 DocumentClass const & tclass = params_.documentClass();
1044 // collect preamble snippets in a set to prevent multiple identical
1045 // commands (would happen if e.g. both theorem and theorem* are used)
1046 set<docstring> snippets;
1047 typedef LanguageList::const_iterator lang_it;
1048 lang_it const lbeg = UsedLanguages_.begin();
1049 lang_it const lend = UsedLanguages_.end();
1050 list<docstring>::const_iterator cit = usedLayouts_.begin();
1051 list<docstring>::const_iterator end = usedLayouts_.end();
1052 for (; cit != end; ++cit) {
1053 // language dependent commands (once per document)
1054 snippets.insert(tclass[*cit].langpreamble(buffer().language()));
1055 // commands for language changing (for multilanguage documents)
1056 if (use_babel && !UsedLanguages_.empty()) {
1057 snippets.insert(tclass[*cit].babelpreamble(buffer().language()));
1058 for (lang_it lit = lbeg; lit != lend; ++lit)
1059 snippets.insert(tclass[*cit].babelpreamble(*lit));
1062 if (use_babel && !UsedLanguages_.empty()) {
1063 FloatList const & floats = params_.documentClass().floats();
1064 UsedFloats::const_iterator fit = usedFloats_.begin();
1065 UsedFloats::const_iterator fend = usedFloats_.end();
1066 for (; fit != fend; ++fit) {
1067 Floating const & fl = floats.getType(fit->first);
1068 docstring const type = from_ascii(fl.floattype());
1069 docstring const flname = from_utf8(fl.name());
1070 docstring name = translateIfPossible(flname,
1071 buffer().language()->code());
1072 snippets.insert(getFloatI18nPreamble(
1074 from_ascii(buffer().language()->babel())));
1075 for (lang_it lit = lbeg; lit != lend; ++lit) {
1076 name = translateIfPossible(flname,
1078 snippets.insert(getFloatI18nPreamble(
1080 from_ascii((*lit)->babel())));
1085 odocstringstream tcpreamble;
1086 set<docstring>::const_iterator const send = snippets.end();
1087 set<docstring>::const_iterator it = snippets.begin();
1088 for (; it != send; ++it)
1090 return tcpreamble.str();
1094 docstring const LaTeXFeatures::getLyXSGMLEntities() const
1096 // Definition of entities used in the document that are LyX related.
1097 odocstringstream entities;
1099 if (mustProvide("lyxarrow")) {
1100 entities << "<!ENTITY lyxarrow \"->\">" << '\n';
1103 return entities.str();
1107 docstring const LaTeXFeatures::getIncludedFiles(string const & fname) const
1109 odocstringstream sgmlpreamble;
1111 docstring const basename(from_utf8(onlyPath(fname)));
1113 FileMap::const_iterator end = IncludedFiles_.end();
1114 for (FileMap::const_iterator fi = IncludedFiles_.begin();
1117 sgmlpreamble << "\n<!ENTITY " << fi->first
1118 << (isSGMLFileName(fi->second) ? " SYSTEM \"" : " \"")
1119 << makeRelPath(from_utf8(fi->second), basename) << "\">";
1121 return sgmlpreamble.str();
1125 void LaTeXFeatures::showStruct() const
1127 lyxerr << "LyX needs the following commands when LaTeXing:"
1128 << "\n***** Packages:" << getPackages()
1129 << "\n***** Macros:" << to_utf8(getMacros())
1130 << "\n***** Textclass stuff:" << to_utf8(getTClassPreamble())
1131 << "\n***** done." << endl;
1135 Buffer const & LaTeXFeatures::buffer() const
1141 void LaTeXFeatures::setBuffer(Buffer const & buffer)
1147 BufferParams const & LaTeXFeatures::bufferParams() const
1153 void LaTeXFeatures::getFloatDefinitions(odocstream & os) const
1155 FloatList const & floats = params_.documentClass().floats();
1157 // Here we will output the code to create the needed float styles.
1158 // We will try to do this as minimal as possible.
1159 // \floatstyle{ruled}
1160 // \newfloat{algorithm}{htbp}{loa}
1161 // \providecommand{\algorithmname}{Algorithm}
1162 // \floatname{algorithm}{\protect\algorithmname}
1163 UsedFloats::const_iterator cit = usedFloats_.begin();
1164 UsedFloats::const_iterator end = usedFloats_.end();
1165 for (; cit != end; ++cit) {
1166 Floating const & fl = floats.getType(cit->first);
1168 // For builtin floats we do nothing.
1169 if (!fl.needsFloatPkg())
1172 // We have to special case "table" and "figure"
1173 if (fl.floattype() == "tabular" || fl.floattype() == "figure") {
1174 // Output code to modify "table" or "figure"
1175 // but only if builtin == false
1176 // and that have to be true at this point in the
1178 docstring const type = from_ascii(fl.floattype());
1179 docstring const placement = from_ascii(fl.placement());
1180 docstring const style = from_ascii(fl.style());
1181 if (!style.empty()) {
1182 os << "\\floatstyle{" << style << "}\n"
1183 << "\\restylefloat{" << type << "}\n";
1185 if (!placement.empty()) {
1186 os << "\\floatplacement{" << type << "}{"
1187 << placement << "}\n";
1190 // The other non builtin floats.
1192 docstring const type = from_ascii(fl.floattype());
1193 docstring const placement = from_ascii(fl.placement());
1194 docstring const ext = from_ascii(fl.ext());
1195 docstring const within = from_ascii(fl.within());
1196 docstring const style = from_ascii(fl.style());
1197 docstring const name = translateIfPossible(
1198 from_utf8(fl.name()),
1199 buffer().language()->code());
1200 os << "\\floatstyle{" << style << "}\n"
1201 << "\\newfloat{" << type << "}{" << placement
1202 << "}{" << ext << '}';
1203 if (!within.empty())
1204 os << '[' << within << ']';
1206 << "\\providecommand{\\" << type << "name}{"
1208 << "\\floatname{" << type << "}{\\protect\\"
1209 << type << "name}\n";
1211 // What missing here is to code to minimalize the code
1212 // output so that the same floatstyle will not be
1213 // used several times, when the same style is still in
1217 os << "\n\\newsubfloat{" << from_ascii(fl.floattype()) << "}\n";