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 lyx_hyperref_def = from_ascii(
64 "\\providecommand{\\LyX}{\\texorpdfstring%\n"
65 " {L\\kern-.1667em\\lower.25em\\hbox{Y}\\kern-.125emX\\@}\n"
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}\n"
167 " {\\textcolor{note_fontcolor}\\bgroup\\ignorespaces}\n"
168 " {\\ignorespacesafterend\\egroup}\n");
170 // We want to omit the file extension for includegraphics, but this does not
171 // work when the filename contains other dots.
172 // Idea from http://www.tex.ac.uk/cgi-bin/texfaq2html?label=unkgrfextn
173 static docstring const lyxdot_def = from_ascii(
174 "%% A simple dot to overcome graphicx limitations\n"
175 "\\newcommand{\\lyxdot}{.}\n");
177 static docstring const changetracking_dvipost_def = from_ascii(
178 "%% Change tracking with dvipost\n"
180 "\\dvipost{osstart color push Red}\n"
181 "\\dvipost{osend color pop}\n"
182 "\\dvipost{cbstart color push Blue}\n"
183 "\\dvipost{cbend color pop}\n"
184 "\\newcommand{\\lyxadded}[3]{\\changestart#3\\changeend}\n"
185 "\\newcommand{\\lyxdeleted}[3]{%\n"
186 "\\changestart\\overstrikeon#3\\overstrikeoff\\changeend}\n");
188 static docstring const changetracking_xcolor_ulem_def = from_ascii(
189 "%% Change tracking with ulem\n"
190 "\\newcommand{\\lyxadded}[3]{{\\color{lyxadded}{}#3}}\n"
191 "\\newcommand{\\lyxdeleted}[3]{{\\color{lyxdeleted}\\sout{#3}}}\n");
193 static docstring const changetracking_xcolor_ulem_hyperref_def = from_ascii(
194 "%% Change tracking with ulem\n"
195 "\\newcommand{\\lyxadded}[3]{{\\texorpdfstring{\\color{lyxadded}{}}{}#3}}\n"
196 "\\newcommand{\\lyxdeleted}[3]{{\\texorpdfstring{\\color{lyxdeleted}\\sout{#3}}{}}}\n");
198 static docstring const changetracking_none_def = from_ascii(
199 "\\newcommand{\\lyxadded}[3]{#3}\n"
200 "\\newcommand{\\lyxdeleted}[3]{}\n");
202 static docstring const textgreek_def = from_ascii(
203 "\\DeclareRobustCommand{\\greektext}{%\n"
204 " \\fontencoding{LGR}\\selectfont\\def\\encodingdefault{LGR}}\n"
205 "\\DeclareRobustCommand{\\textgreek}[1]{\\leavevmode{\\greektext #1}}\n"
206 "\\DeclareFontEncoding{LGR}{}{}\n"
207 "\\DeclareTextSymbol{\\~}{LGR}{126}");
209 static docstring const textcyr_def = from_ascii(
210 "\\DeclareRobustCommand{\\cyrtext}{%\n"
211 " \\fontencoding{T2A}\\selectfont\\def\\encodingdefault{T2A}}\n"
212 "\\DeclareRobustCommand{\\textcyr}[1]{\\leavevmode{\\cyrtext #1}}\n"
213 "\\AtBeginDocument{\\DeclareFontEncoding{T2A}{}{}}\n");
215 static docstring const lyxmathsym_def = from_ascii(
216 "\\newcommand{\\lyxmathsym}[1]{\\ifmmode\\begingroup\\def\\b@ld{bold}\n"
217 " \\text{\\ifx\\math@version\\b@ld\\bfseries\\fi#1}\\endgroup\\else#1\\fi}\n");
219 static docstring const papersizedvi_def = from_ascii(
220 "\\special{papersize=\\the\\paperwidth,\\the\\paperheight}\n");
222 static docstring const papersizepdf_def = from_ascii(
223 "\\pdfpageheight\\paperheight\n"
224 "\\pdfpagewidth\\paperwidth\n");
226 static docstring const cedilla_def = from_ascii(
227 "\\newcommand{\\docedilla}[2]{\\underaccent{#1\\mathchar'30}{#2}}\n"
228 "\\newcommand{\\cedilla}[1]{\\mathpalette\\docedilla{#1}}\n");
230 static docstring const subring_def = from_ascii(
231 "\\newcommand{\\dosubring}[2]{\\underaccent{#1\\mathchar'27}{#2}}\n"
232 "\\newcommand{\\subring}[1]{\\mathpalette\\dosubring{#1}}\n");
234 static docstring const subdot_def = from_ascii(
235 "\\newcommand{\\dosubdot}[2]{\\underaccent{#1.}{#2}}\n"
236 "\\newcommand{\\subdot}[1]{\\mathpalette\\dosubdot{#1}}\n");
238 static docstring const subhat_def = from_ascii(
239 "\\newcommand{\\dosubhat}[2]{\\underaccent{#1\\mathchar'136}{#2}}\n"
240 "\\newcommand{\\subhat}[1]{\\mathpalette\\dosubhat{#1}}\n");
242 static docstring const subtilde_def = from_ascii(
243 "\\newcommand{\\dosubtilde}[2]{\\underaccent{#1\\mathchar'176}{#2}}\n"
244 "\\newcommand{\\subtilde}[1]{\\mathpalette\\dosubtilde{#1}}\n");
246 static docstring const dacute_def = from_ascii(
247 "\\DeclareMathAccent{\\dacute}{\\mathalpha}{operators}{'175}\n");
249 static docstring const tipasymb_def = from_ascii(
250 "\\DeclareFontEncoding{T3}{}{}\n"
251 "\\DeclareSymbolFont{tipasymb}{T3}{cmr}{m}{n}\n");
253 static docstring const dgrave_def = from_ascii(
254 "\\DeclareMathAccent{\\dgrave}{\\mathord}{tipasymb}{'15}\n");
256 static docstring const rcap_def = from_ascii(
257 "\\DeclareMathAccent{\\rcap}{\\mathord}{tipasymb}{'20}\n");
259 static docstring const ogonek_def = from_ascii(
260 "\\newcommand{\\doogonek}[2]{\\setbox0=\\hbox{$#1#2$}\\underaccent{#1\\mkern-6mu\n"
261 " \\ifx#2O\\hskip0.5\\wd0\\else\\ifx#2U\\hskip0.5\\wd0\\else\\hskip\\wd0\\fi\\fi\n"
262 " \\ifx#2o\\mkern-2mu\\else\\ifx#2e\\mkern-1mu\\fi\\fi\n"
263 " \\mathchar\"0\\hexnumber@\\symtipasymb0C}{#2}}\n"
264 "\\newcommand{\\ogonek}[1]{\\mathpalette\\doogonek{#1}}\n");
266 static docstring const lyxref_def = from_ascii(
267 "\\RS@ifundefined{subref}\n"
268 " {\\def\\RSsubtxt{section~}\\newref{sub}{name = \\RSsubtxt}}\n"
270 "\\RS@ifundefined{thmref}\n"
271 " {\\def\\RSthmtxt{theorem~}\\newref{thm}{name = \\RSthmtxt}}\n"
273 "\\RS@ifundefined{lemref}\n"
274 " {\\def\\RSlemtxt{lemma~}\\newref{lem}{name = \\RSlemtxt}}\n"
278 /////////////////////////////////////////////////////////////////////
282 /////////////////////////////////////////////////////////////////////
284 LaTeXFeatures::Packages LaTeXFeatures::packages_;
287 LaTeXFeatures::LaTeXFeatures(Buffer const & b, BufferParams const & p,
288 OutputParams const & r)
289 : buffer_(&b), params_(p), runparams_(r), in_float_(false)
293 bool LaTeXFeatures::useBabel() const
295 if (usePolyglossia())
297 if (bufferParams().lang_package == "default")
298 return (lyxrc.language_package_selection != LyXRC::LP_NONE)
299 || (bufferParams().language->lang() != lyxrc.default_language
300 && !bufferParams().language->babel().empty())
301 || this->hasLanguages();
302 return (bufferParams().lang_package != "none")
303 || (bufferParams().language->lang() != lyxrc.default_language
304 && !bufferParams().language->babel().empty())
305 || this->hasLanguages();
309 bool LaTeXFeatures::usePolyglossia() const
311 if (bufferParams().lang_package == "default")
312 return (lyxrc.language_package_selection == LyXRC::LP_AUTO)
313 && isRequired("polyglossia")
314 && isAvailable("polyglossia")
315 && !params_.documentClass().provides("babel")
316 && this->hasPolyglossiaLanguages();
317 return (bufferParams().lang_package == "auto")
318 && isRequired("polyglossia")
319 && isAvailable("polyglossia")
320 && !params_.documentClass().provides("babel")
321 && this->hasPolyglossiaLanguages();
325 void LaTeXFeatures::require(string const & name)
327 features_.insert(name);
331 void LaTeXFeatures::require(set<string> const & names)
333 features_.insert(names.begin(), names.end());
337 void LaTeXFeatures::getAvailable()
340 support::FileName const real_file = libFileSearch("", "packages.lst");
342 if (real_file.empty())
345 lex.setFile(real_file);
350 // Make sure that we are clean
353 bool finished = false;
355 while (lex.isOK() && !finished) {
357 case Lexer::LEX_FEOF:
361 packages_.insert(lex.getString());
367 void LaTeXFeatures::useLayout(docstring const & layoutname)
369 // Some code to avoid loops in dependency definition
370 static int level = 0;
371 const int maxlevel = 30;
372 if (level > maxlevel) {
373 lyxerr << "LaTeXFeatures::useLayout: maximum level of "
374 << "recursion attained by layout "
375 << to_utf8(layoutname) << endl;
379 DocumentClass const & tclass = params_.documentClass();
380 if (tclass.hasLayout(layoutname)) {
381 // Is this layout already in usedLayouts?
382 if (find(usedLayouts_.begin(), usedLayouts_.end(), layoutname)
383 != usedLayouts_.end())
386 Layout const & layout = tclass[layoutname];
387 require(layout.requires());
389 if (!layout.depends_on().empty()) {
391 useLayout(layout.depends_on());
394 usedLayouts_.push_back(layoutname);
396 lyxerr << "LaTeXFeatures::useLayout: layout `"
397 << to_utf8(layoutname) << "' does not exist in this class"
405 void LaTeXFeatures::useInsetLayout(InsetLayout const & lay)
407 docstring const & lname = lay.name();
408 DocumentClass const & tclass = params_.documentClass();
410 // this is a default inset layout, nothing useful here
411 if (!tclass.hasInsetLayout(lname))
413 // Is this layout already in usedInsetLayouts?
414 if (find(usedInsetLayouts_.begin(), usedInsetLayouts_.end(), lname)
415 != usedInsetLayouts_.end())
418 require(lay.requires());
419 usedInsetLayouts_.push_back(lname);
423 bool LaTeXFeatures::isRequired(string const & name) const
425 return features_.find(name) != features_.end();
429 bool LaTeXFeatures::mustProvide(string const & name) const
431 return isRequired(name) && !params_.documentClass().provides(name);
435 bool LaTeXFeatures::isAvailable(string const & name)
437 string::size_type const i = name.find("->");
438 if (i != string::npos) {
439 string const from = name.substr(0,i);
440 string const to = name.substr(i+2);
441 //LYXERR0("from=[" << from << "] to=[" << to << "]");
442 return theConverters().isReachable(from, to);
445 if (packages_.empty())
448 if (suffixIs(n, ".sty"))
449 n.erase(name.length() - 4);
450 return packages_.find(n) != packages_.end();
454 void LaTeXFeatures::addPreambleSnippet(string const & preamble)
456 SnippetList::const_iterator begin = preamble_snippets_.begin();
457 SnippetList::const_iterator end = preamble_snippets_.end();
458 if (find(begin, end, preamble) == end)
459 preamble_snippets_.push_back(preamble);
463 void LaTeXFeatures::useFloat(string const & name, bool subfloat)
465 if (!usedFloats_[name])
466 usedFloats_[name] = subfloat;
469 // We only need float.sty if we use non builtin floats, or if we
470 // use the "H" modifier. This includes modified table and
471 // figure floats. (Lgb)
472 Floating const & fl = params_.documentClass().floats().getType(name);
473 if (!fl.floattype().empty() && fl.usesFloatPkg()) {
479 void LaTeXFeatures::useLanguage(Language const * lang)
481 if (!lang->babel().empty())
482 UsedLanguages_.insert(lang);
483 if (lang->lang() == "vietnamese")
484 require("vietnamese");
485 // CJK languages do not have a babel name.
486 // They use the CJK package
487 if (lang->encoding()->package() == Encoding::CJK)
489 // japanese package is special
490 if (lang->encoding()->package() == Encoding::japanese)
495 void LaTeXFeatures::includeFile(docstring const & key, string const & name)
497 IncludedFiles_[key] = name;
501 bool LaTeXFeatures::hasLanguages() const
503 return !UsedLanguages_.empty();
507 bool LaTeXFeatures::hasPolyglossiaLanguages() const
509 LanguageList::const_iterator const begin = UsedLanguages_.begin();
510 for (LanguageList::const_iterator cit = begin;
511 cit != UsedLanguages_.end();
513 if ((*cit)->polyglossia().empty())
520 string LaTeXFeatures::getLanguages() const
522 ostringstream languages;
524 LanguageList::const_iterator const begin = UsedLanguages_.begin();
525 for (LanguageList::const_iterator cit = begin;
526 cit != UsedLanguages_.end();
530 languages << (*cit)->babel();
532 return languages.str();
536 std::map<std::string, std::string> LaTeXFeatures::getPolyglossiaLanguages() const
538 std::map<std::string, std::string> languages;
540 LanguageList::const_iterator const begin = UsedLanguages_.begin();
541 for (LanguageList::const_iterator cit = begin;
542 cit != UsedLanguages_.end();
544 languages[(*cit)->polyglossia()] = (*cit)->polyglossiaOpts();
550 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding) const
552 // This does only find encodings of languages supported by babel, but
553 // that does not matter since we don't have a language with an
554 // encoding supported by inputenc but without babel support.
555 set<string> encodings;
556 LanguageList::const_iterator it = UsedLanguages_.begin();
557 LanguageList::const_iterator end = UsedLanguages_.end();
558 for (; it != end; ++it)
559 if ((*it)->encoding()->latexName() != doc_encoding &&
560 ((*it)->encoding()->package() == Encoding::inputenc
561 || (*it)->encoding()->package() == Encoding::japanese))
562 encodings.insert((*it)->encoding()->latexName());
568 char const * simplefeatures[] = {
569 // note that the package order here will be the same in the LaTeX-output
576 // subfig is handled in BufferParams.cpp
580 /*For a successful cooperation of the `wrapfig' package with the
581 `float' package you should load the `wrapfig' package *after*
582 the `float' package. See the caption package documentation
614 // listings is handled in BufferParams.cpp
623 int const nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
628 string const LaTeXFeatures::getColorOptions() const
630 ostringstream colors;
632 // Handling the color packages separately is needed to be able to load them
633 // before babel when hyperref is loaded with the colorlinks option
634 // for more info see Bufferparams.cpp
637 if (mustProvide("color") || mustProvide("xcolor")) {
638 string const package =
639 (mustProvide("xcolor") ? "xcolor" : "color");
640 if (params_.graphics_driver == "default"
641 || params_.graphics_driver == "none")
642 colors << "\\usepackage{" << package << "}\n";
644 colors << "\\usepackage["
645 << params_.graphics_driver
646 << "]{" << package << "}\n";
649 // pdfcolmk must be loaded after color
650 if (mustProvide("pdfcolmk"))
651 colors << "\\usepackage{pdfcolmk}\n";
653 // the following 3 color commands must be set after color
654 // is loaded and before pdfpages, therefore add the command
655 // here define the set color
656 if (mustProvide("pagecolor")) {
657 colors << "\\definecolor{page_backgroundcolor}{rgb}{";
658 colors << outputLaTeXColor(params_.backgroundcolor) << "}\n";
659 // set the page color
660 colors << "\\pagecolor{page_backgroundcolor}\n";
663 if (mustProvide("fontcolor")) {
664 colors << "\\definecolor{document_fontcolor}{rgb}{";
665 colors << outputLaTeXColor(params_.fontcolor) << "}\n";
667 colors << "\\color{document_fontcolor}\n";
670 if (mustProvide("lyxgreyedout")) {
671 colors << "\\definecolor{note_fontcolor}{rgb}{";
672 colors << outputLaTeXColor(params_.notefontcolor) << "}\n";
673 // the color will be set together with the definition of
674 // the lyxgreyedout environment (see lyxgreyedout_def)
677 // color for shaded boxes
678 if (isRequired("framed") && mustProvide("color")) {
679 colors << "\\definecolor{shadecolor}{rgb}{";
680 colors << outputLaTeXColor(params_.boxbgcolor) << "}\n";
681 // this color is automatically used by the LaTeX-package "framed"
688 string const LaTeXFeatures::getPackages() const
690 ostringstream packages;
691 DocumentClass const & tclass = params_.documentClass();
693 // FIXME: currently, we can only load packages and macros known
695 // However, with the Require tag of layouts/custom insets,
696 // also unknown packages can be requested. They are silently
697 // swallowed now. We should change this eventually.
700 // These are all the 'simple' includes. i.e
701 // packages which we just \usepackage{package}
703 for (int i = 0; i < nb_simplefeatures; ++i) {
704 if (mustProvide(simplefeatures[i]))
705 packages << "\\usepackage{"
706 << simplefeatures[i] << "}\n";
710 // The rest of these packages are somewhat more complicated
714 // if fontspec is used, AMS packages have to be loaded before
715 // fontspec (in BufferParams)
716 string const amsPackages = loadAMSPackages();
717 if (!params_.useNonTeXFonts && !amsPackages.empty())
718 packages << amsPackages;
720 // fixltx2e must be loaded after amsthm, since amsthm produces an error with
721 // the redefined \[ command (bug 7233). Load is as early as possible, since
722 // other packages might profit from it.
723 if (mustProvide("fixltx2e"))
724 packages << "\\usepackage{fixltx2e}\n";
726 // wasysym is a simple feature, but it must be after amsmath if both
728 // wasysym redefines some integrals (e.g. iint) from amsmath. That
729 // leads to inconsistent integrals. We only load this package if
730 // the document does not contain integrals (then isRequired("esint")
731 // is false) or if esint is used, since esint redefines all relevant
732 // integral symbols from wasysym and amsmath.
733 // See http://www.lyx.org/trac/ticket/1942
734 if (mustProvide("wasysym") &&
735 (params_.use_esint != BufferParams::package_off || !isRequired("esint")))
736 packages << "\\usepackage{wasysym}\n";
738 // accents must be loaded after amsmath
739 if (mustProvide("accents"))
740 packages << "\\usepackage{accents}\n";
742 // mathdots must be loaded after amsmath
743 if (mustProvide("mathdots") &&
744 params_.use_mathdots != BufferParams::package_off)
745 packages << "\\usepackage{mathdots}\n";
747 // yhmath must be loaded after amsmath
748 if (mustProvide("yhmath"))
749 packages << "\\usepackage{yhmath}\n";
751 if (mustProvide("undertilde") &&
752 params_.use_undertilde != BufferParams::package_off)
753 packages << "\\usepackage{undertilde}\n";
755 // [x]color and pdfcolmk are handled in getColorOptions() above
758 if (isRequired("makeidx") || isRequired("splitidx")) {
759 if (!tclass.provides("makeidx") && !isRequired("splitidx"))
760 packages << "\\usepackage{makeidx}\n";
761 if (!tclass.provides("splitidx") && isRequired("splitidx"))
762 packages << "\\usepackage{splitidx}\n";
763 packages << "\\makeindex\n";
767 if (mustProvide("graphicx") && params_.graphics_driver != "none") {
768 if (params_.graphics_driver == "default")
769 packages << "\\usepackage{graphicx}\n";
771 packages << "\\usepackage["
772 << params_.graphics_driver
776 // lyxskak.sty --- newer chess support based on skak.sty
777 if (mustProvide("chess"))
778 packages << "\\usepackage[ps,mover]{lyxskak}\n";
781 if (mustProvide("setspace") && !tclass.provides("SetSpace"))
782 packages << "\\usepackage{setspace}\n";
784 // esint must be after amsmath and wasysym, since it will redeclare
785 // inconsistent integral symbols
786 if (mustProvide("esint") &&
787 params_.use_esint != BufferParams::package_off)
788 packages << "\\usepackage{esint}\n";
791 // Some classes load natbib themselves, but still allow (or even require)
792 // plain numeric citations (ReVTeX is such a case, see bug 5182).
793 // This special case is indicated by the "natbib-internal" key.
794 if (mustProvide("natbib") && !tclass.provides("natbib-internal")) {
795 packages << "\\usepackage[";
796 if (params_.citeEngine() == ENGINE_NATBIB_NUMERICAL)
797 packages << "numbers";
799 packages << "authoryear";
800 packages << "]{natbib}\n";
803 // jurabib -- we need version 0.6 at least.
804 if (mustProvide("jurabib"))
805 packages << "\\usepackage{jurabib}[2004/01/25]\n";
807 // xargs -- we need version 1.09 at least
808 if (mustProvide("xargs"))
809 packages << "\\usepackage{xargs}[2008/03/08]\n";
811 // bibtopic -- the dot provides the aux file naming which
813 if (mustProvide("bibtopic"))
814 packages << "\\usepackage[dot]{bibtopic}\n";
816 if (mustProvide("xy"))
817 packages << "\\usepackage[all]{xy}\n";
819 if (mustProvide("feyn"))
820 packages << "\\usepackage{feyn}\n"; //Diagram
822 if (mustProvide("ulem"))
823 packages << "\\PassOptionsToPackage{normalem}{ulem}\n"
824 "\\usepackage{ulem}\n";
826 if (params_.use_mhchem == BufferParams::package_on ||
827 (mustProvide("mhchem") &&
828 params_.use_mhchem != BufferParams::package_off))
829 packages << "\\PassOptionsToPackage{version=3}{mhchem}\n"
830 "\\usepackage{mhchem}\n";
832 if (mustProvide("nomencl")) {
833 // Make it work with the new and old version of the package,
834 // but don't use the compatibility option since it is
835 // incompatible to other packages.
836 packages << "\\usepackage{nomencl}\n"
837 "% the following is useful when we have the old nomencl.sty package\n"
838 "\\providecommand{\\printnomenclature}{\\printglossary}\n"
839 "\\providecommand{\\makenomenclature}{\\makeglossary}\n"
840 "\\makenomenclature\n";
843 // fixltx2e provides subscript
844 if (mustProvide("subscript") && !isRequired("fixltx2e"))
845 packages << "\\usepackage{subscript}\n";
847 return packages.str();
851 string LaTeXFeatures::getPreambleSnippets() const
854 SnippetList::const_iterator pit = preamble_snippets_.begin();
855 SnippetList::const_iterator pend = preamble_snippets_.end();
856 for (; pit != pend; ++pit)
857 snip << *pit << '\n';
862 docstring const LaTeXFeatures::getMacros() const
864 odocstringstream macros;
866 if (!preamble_snippets_.empty()) {
868 macros << from_utf8(getPreambleSnippets());
871 if (mustProvide("papersize")) {
872 if (runparams_.flavor == OutputParams::LATEX)
873 macros << papersizedvi_def << '\n';
875 macros << papersizepdf_def << '\n';
878 if (mustProvide("LyX")) {
879 if (isRequired("hyperref"))
880 macros << lyx_hyperref_def << '\n';
882 macros << lyx_def << '\n';
885 if (mustProvide("noun"))
886 macros << noun_def << '\n';
888 if (mustProvide("lyxarrow"))
889 macros << lyxarrow_def << '\n';
891 if (!usePolyglossia() && mustProvide("textgreek")) {
892 // Avoid a LaTeX error if times fonts are used and the grtimes
893 // package is installed but actual fonts are not (bug 6469).
894 if (params_.fonts_roman == "times")
895 macros << subst(textgreek_def,
896 from_ascii("\\greektext #1"),
897 from_ascii("%\n \\IfFileExists"
898 "{grtm10.tfm}{}{\\fontfamily"
899 "{cmr}}\\greektext #1"))
902 macros << textgreek_def << '\n';
905 if (!usePolyglossia() && mustProvide("textcyr"))
906 macros << textcyr_def << '\n';
908 if (mustProvide("lyxmathsym"))
909 macros << lyxmathsym_def << '\n';
911 if (mustProvide("cedilla"))
912 macros << cedilla_def << '\n';
914 if (mustProvide("subring"))
915 macros << subring_def << '\n';
917 if (mustProvide("subdot"))
918 macros << subdot_def << '\n';
920 if (mustProvide("subhat"))
921 macros << subhat_def << '\n';
923 if (mustProvide("subtilde"))
924 macros << subtilde_def << '\n';
926 if (mustProvide("dacute"))
927 macros << dacute_def << '\n';
929 if (mustProvide("tipasymb"))
930 macros << tipasymb_def << '\n';
932 if (mustProvide("dgrave"))
933 macros << dgrave_def << '\n';
935 if (mustProvide("rcap"))
936 macros << rcap_def << '\n';
938 if (mustProvide("ogonek"))
939 macros << ogonek_def << '\n';
942 if (mustProvide("quotesinglbase"))
943 macros << quotesinglbase_def << '\n';
944 if (mustProvide("quotedblbase"))
945 macros << quotedblbase_def << '\n';
946 if (mustProvide("guilsinglleft"))
947 macros << guilsinglleft_def << '\n';
948 if (mustProvide("guilsinglright"))
949 macros << guilsinglright_def << '\n';
950 if (mustProvide("guillemotleft"))
951 macros << guillemotleft_def << '\n';
952 if (mustProvide("guillemotright"))
953 macros << guillemotright_def << '\n';
956 if (mustProvide("binom") && !isRequired("amsmath"))
957 macros << binom_def << '\n';
958 if (mustProvide("mathcircumflex"))
959 macros << mathcircumflex_def << '\n';
962 if (mustProvide("ParagraphLeftIndent"))
963 macros << paragraphleftindent_def;
964 if (mustProvide("NeedLyXFootnoteCode"))
965 macros << floatingfootnote_def;
967 // some problems with tex->html converters
968 if (mustProvide("NeedTabularnewline"))
969 macros << tabularnewline_def;
971 // greyed-out environment (note inset)
972 // the color is specified in the routine
973 // getColorOptions() to avoid LaTeX-package clashes
974 if (mustProvide("lyxgreyedout"))
975 macros << lyxgreyedout_def;
977 if (mustProvide("lyxdot"))
978 macros << lyxdot_def << '\n';
981 getFloatDefinitions(macros);
983 if (mustProvide("refstyle"))
984 macros << lyxref_def << '\n';
987 if (mustProvide("ct-dvipost"))
988 macros << changetracking_dvipost_def;
990 if (mustProvide("ct-xcolor-ulem")) {
991 streamsize const prec = macros.precision(2);
993 RGBColor cadd = rgbFromHexName(lcolor.getX11Name(Color_addedtext));
994 macros << "\\providecolor{lyxadded}{rgb}{"
995 << cadd.r / 255.0 << ',' << cadd.g / 255.0 << ',' << cadd.b / 255.0 << "}\n";
997 RGBColor cdel = rgbFromHexName(lcolor.getX11Name(Color_deletedtext));
998 macros << "\\providecolor{lyxdeleted}{rgb}{"
999 << cdel.r / 255.0 << ',' << cdel.g / 255.0 << ',' << cdel.b / 255.0 << "}\n";
1001 macros.precision(prec);
1003 if (isRequired("hyperref"))
1004 macros << changetracking_xcolor_ulem_hyperref_def;
1006 macros << changetracking_xcolor_ulem_def;
1009 if (mustProvide("ct-none"))
1010 macros << changetracking_none_def;
1012 return macros.str();
1016 string const LaTeXFeatures::getBabelPresettings() const
1020 LanguageList::const_iterator it = UsedLanguages_.begin();
1021 LanguageList::const_iterator end = UsedLanguages_.end();
1022 for (; it != end; ++it)
1023 if (!(*it)->babel_presettings().empty())
1024 tmp << (*it)->babel_presettings() << '\n';
1025 if (!params_.language->babel_presettings().empty())
1026 tmp << params_.language->babel_presettings() << '\n';
1032 string const LaTeXFeatures::getBabelPostsettings() const
1036 LanguageList::const_iterator it = UsedLanguages_.begin();
1037 LanguageList::const_iterator end = UsedLanguages_.end();
1038 for (; it != end; ++it)
1039 if (!(*it)->babel_postsettings().empty())
1040 tmp << (*it)->babel_postsettings() << '\n';
1041 if (!params_.language->babel_postsettings().empty())
1042 tmp << params_.language->babel_postsettings() << '\n';
1048 bool LaTeXFeatures::needBabelLangOptions() const
1050 if (!lyxrc.language_global_options || params_.language->asBabelOptions())
1053 LanguageList::const_iterator it = UsedLanguages_.begin();
1054 LanguageList::const_iterator end = UsedLanguages_.end();
1055 for (; it != end; ++it)
1056 if ((*it)->asBabelOptions())
1063 string const LaTeXFeatures::loadAMSPackages() const
1066 if (mustProvide("amsthm"))
1067 tmp << "\\usepackage{amsthm}\n";
1069 if (mustProvide("amsmath")
1070 && params_.use_amsmath != BufferParams::package_off) {
1071 tmp << "\\usepackage{amsmath}\n";
1073 // amsbsy and amstext are already provided by amsmath
1074 if (mustProvide("amsbsy"))
1075 tmp << "\\usepackage{amsbsy}\n";
1076 if (mustProvide("amstext"))
1077 tmp << "\\usepackage{amstext}\n";
1080 if (mustProvide("amssymb")
1081 || params_.use_amsmath == BufferParams::package_on)
1082 tmp << "\\usepackage{amssymb}\n";
1088 docstring const LaTeXFeatures::getTClassPreamble() const
1090 // the text class specific preamble
1091 DocumentClass const & tclass = params_.documentClass();
1092 odocstringstream tcpreamble;
1094 tcpreamble << tclass.preamble();
1096 list<docstring>::const_iterator cit = usedLayouts_.begin();
1097 list<docstring>::const_iterator end = usedLayouts_.end();
1098 for (; cit != end; ++cit)
1099 tcpreamble << tclass[*cit].preamble();
1101 cit = usedInsetLayouts_.begin();
1102 end = usedInsetLayouts_.end();
1103 TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1104 for (; cit != end; ++cit) {
1105 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1106 if (it == ils.end())
1108 tcpreamble << it->second.preamble();
1111 return tcpreamble.str();
1115 docstring const LaTeXFeatures::getTClassHTMLPreamble() const
1117 DocumentClass const & tclass = params_.documentClass();
1118 odocstringstream tcpreamble;
1120 tcpreamble << tclass.htmlpreamble();
1122 list<docstring>::const_iterator cit = usedLayouts_.begin();
1123 list<docstring>::const_iterator end = usedLayouts_.end();
1124 for (; cit != end; ++cit)
1125 tcpreamble << tclass[*cit].htmlpreamble();
1127 cit = usedInsetLayouts_.begin();
1128 end = usedInsetLayouts_.end();
1129 TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1130 for (; cit != end; ++cit) {
1131 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1132 if (it == ils.end())
1134 tcpreamble << it->second.htmlpreamble();
1137 return tcpreamble.str();
1141 docstring const LaTeXFeatures::getTClassHTMLStyles() const {
1142 DocumentClass const & tclass = params_.documentClass();
1143 odocstringstream tcpreamble;
1145 list<docstring>::const_iterator cit = usedLayouts_.begin();
1146 list<docstring>::const_iterator end = usedLayouts_.end();
1147 for (; cit != end; ++cit)
1148 tcpreamble << tclass[*cit].htmlstyle();
1150 cit = usedInsetLayouts_.begin();
1151 end = usedInsetLayouts_.end();
1152 TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1153 for (; cit != end; ++cit) {
1154 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1155 if (it == ils.end())
1157 tcpreamble << it->second.htmlstyle();
1160 return tcpreamble.str();
1165 docstring const getFloatI18nPreamble(docstring const & type,
1166 docstring const & name, Language const * lang,
1167 Encoding const & enc, bool const polyglossia)
1169 docstring const language = polyglossia ? from_ascii(lang->polyglossia())
1170 : from_ascii(lang->babel());
1171 docstring const langenc = from_ascii(lang->encoding()->iconvName());
1172 docstring const texenc = from_ascii(lang->encoding()->latexName());
1173 docstring const bufenc = from_ascii(enc.iconvName());
1174 docstring const s1 = docstring(1, 0xF0000);
1175 docstring const s2 = docstring(1, 0xF0001);
1176 docstring const translated = (langenc == bufenc) ? name
1177 : from_ascii("\\inputencoding{") + texenc + from_ascii("}")
1178 + s1 + langenc + s2 + translated + s1 + bufenc + s2;
1180 odocstringstream os;
1181 os << "\\addto\\captions" << language
1182 << "{\\renewcommand{\\" << type << "name}{" << translated << "}}\n";
1188 docstring const LaTeXFeatures::getTClassI18nPreamble(bool use_babel, bool use_polyglossia) const
1190 DocumentClass const & tclass = params_.documentClass();
1191 // collect preamble snippets in a set to prevent multiple identical
1192 // commands (would happen if e.g. both theorem and theorem* are used)
1193 set<docstring> snippets;
1194 typedef LanguageList::const_iterator lang_it;
1195 lang_it const lbeg = UsedLanguages_.begin();
1196 lang_it const lend = UsedLanguages_.end();
1197 list<docstring>::const_iterator cit = usedLayouts_.begin();
1198 list<docstring>::const_iterator end = usedLayouts_.end();
1199 for (; cit != end; ++cit) {
1200 // language dependent commands (once per document)
1201 snippets.insert(tclass[*cit].langpreamble(buffer().language(),
1202 buffer().params().encoding(),
1204 // commands for language changing (for multilanguage documents)
1205 if ((use_babel || use_polyglossia) && !UsedLanguages_.empty()) {
1206 snippets.insert(tclass[*cit].babelpreamble(
1207 buffer().language(),
1208 buffer().params().encoding(),
1210 for (lang_it lit = lbeg; lit != lend; ++lit)
1211 snippets.insert(tclass[*cit].babelpreamble(
1213 buffer().params().encoding(),
1217 if ((use_babel || use_polyglossia) && !UsedLanguages_.empty()) {
1218 FloatList const & floats = params_.documentClass().floats();
1219 UsedFloats::const_iterator fit = usedFloats_.begin();
1220 UsedFloats::const_iterator fend = usedFloats_.end();
1221 for (; fit != fend; ++fit) {
1222 Floating const & fl = floats.getType(fit->first);
1223 // we assume builtin floats are translated
1224 if (fl.isPredefined())
1226 docstring const type = from_ascii(fl.floattype());
1227 docstring const flname = from_utf8(fl.name());
1228 docstring name = buffer().language()->translateLayout(fl.name());
1229 // only request translation if we have a real translation
1230 // (that differs from the source)
1232 snippets.insert(getFloatI18nPreamble(
1233 type, name, buffer().language(),
1234 buffer().params().encoding(),
1236 for (lang_it lit = lbeg; lit != lend; ++lit) {
1237 string const code = (*lit)->code();
1238 name = (*lit)->translateLayout(fl.name());
1239 // we assume we have a suitable translation if
1240 // either the language is English (we need to
1241 // translate into English if English is a secondary
1242 // language) or if translateIfPossible returns
1243 // something different to the English source.
1244 bool const have_translation =
1245 (flname != name || contains(code, "en"));
1246 if (have_translation)
1247 snippets.insert(getFloatI18nPreamble(
1249 buffer().params().encoding(),
1255 odocstringstream tcpreamble;
1256 set<docstring>::const_iterator const send = snippets.end();
1257 set<docstring>::const_iterator it = snippets.begin();
1258 for (; it != send; ++it)
1260 return tcpreamble.str();
1264 docstring const LaTeXFeatures::getLyXSGMLEntities() const
1266 // Definition of entities used in the document that are LyX related.
1267 odocstringstream entities;
1269 if (mustProvide("lyxarrow")) {
1270 entities << "<!ENTITY lyxarrow \"->\">" << '\n';
1273 return entities.str();
1277 docstring const LaTeXFeatures::getIncludedFiles(string const & fname) const
1279 odocstringstream sgmlpreamble;
1281 docstring const basename(from_utf8(onlyPath(fname)));
1283 FileMap::const_iterator end = IncludedFiles_.end();
1284 for (FileMap::const_iterator fi = IncludedFiles_.begin();
1287 sgmlpreamble << "\n<!ENTITY " << fi->first
1288 << (isSGMLFileName(fi->second) ? " SYSTEM \"" : " \"")
1289 << makeRelPath(from_utf8(fi->second), basename) << "\">";
1291 return sgmlpreamble.str();
1295 void LaTeXFeatures::showStruct() const
1297 lyxerr << "LyX needs the following commands when LaTeXing:"
1298 << "\n***** Packages:" << getPackages()
1299 << "\n***** Macros:" << to_utf8(getMacros())
1300 << "\n***** Textclass stuff:" << to_utf8(getTClassPreamble())
1301 << "\n***** done." << endl;
1305 Buffer const & LaTeXFeatures::buffer() const
1311 void LaTeXFeatures::setBuffer(Buffer const & buffer)
1317 BufferParams const & LaTeXFeatures::bufferParams() const
1323 void LaTeXFeatures::getFloatDefinitions(odocstream & os) const
1325 FloatList const & floats = params_.documentClass().floats();
1327 // Here we will output the code to create the needed float styles.
1328 // We will try to do this as minimal as possible.
1329 // \floatstyle{ruled}
1330 // \newfloat{algorithm}{htbp}{loa}
1331 // \providecommand{\algorithmname}{Algorithm}
1332 // \floatname{algorithm}{\protect\algorithmname}
1333 UsedFloats::const_iterator cit = usedFloats_.begin();
1334 UsedFloats::const_iterator end = usedFloats_.end();
1335 for (; cit != end; ++cit) {
1336 Floating const & fl = floats.getType(cit->first);
1338 // For builtin floats we do nothing.
1339 if (fl.isPredefined())
1342 // We have to special case "table" and "figure"
1343 if (fl.floattype() == "tabular" || fl.floattype() == "figure") {
1344 // Output code to modify "table" or "figure"
1345 // but only if builtin == false
1346 // and that have to be true at this point in the
1348 docstring const type = from_ascii(fl.floattype());
1349 docstring const placement = from_ascii(fl.placement());
1350 docstring const style = from_ascii(fl.style());
1351 if (!style.empty()) {
1352 os << "\\floatstyle{" << style << "}\n"
1353 << "\\restylefloat{" << type << "}\n";
1355 if (!placement.empty()) {
1356 os << "\\floatplacement{" << type << "}{"
1357 << placement << "}\n";
1360 // The other non builtin floats.
1362 docstring const type = from_ascii(fl.floattype());
1363 docstring const placement = from_ascii(fl.placement());
1364 docstring const ext = from_ascii(fl.ext());
1365 docstring const within = from_ascii(fl.within());
1366 docstring const style = from_ascii(fl.style());
1367 docstring const name =
1368 buffer().language()->translateLayout(fl.name());
1369 os << "\\floatstyle{" << style << "}\n"
1370 << "\\newfloat{" << type << "}{" << placement
1371 << "}{" << ext << '}';
1372 if (!within.empty())
1373 os << '[' << within << ']';
1375 << "\\providecommand{\\" << type << "name}{"
1377 << "\\floatname{" << type << "}{\\protect\\"
1378 << type << "name}\n";
1380 // What missing here is to code to minimalize the code
1381 // output so that the same floatstyle will not be
1382 // used several times, when the same style is still in
1386 os << "\n\\newsubfloat{" << from_ascii(fl.floattype()) << "}\n";
1391 void LaTeXFeatures::resolveAlternatives()
1393 for (Features::iterator it = features_.begin(); it != features_.end();) {
1394 if (contains(*it, '|')) {
1395 vector<string> const alternatives = getVectorFromString(*it, "|");
1396 vector<string>::const_iterator const end = alternatives.end();
1397 vector<string>::const_iterator ita = alternatives.begin();
1398 for (; ita != end; ++ita) {
1399 if (isRequired(*ita))
1403 require(alternatives.front());
1404 features_.erase(it);
1405 it = features_.begin();