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"
27 #include "LaTeXPackages.h"
31 #include "TextClass.h"
33 #include "insets/InsetLayout.h"
35 #include "support/debug.h"
36 #include "support/docstream.h"
37 #include "support/FileName.h"
38 #include "support/filetools.h"
39 #include "support/gettext.h"
40 #include "support/lstrings.h"
46 using namespace lyx::support;
51 /////////////////////////////////////////////////////////////////////
55 /////////////////////////////////////////////////////////////////////
57 //\NeedsTeXFormat{LaTeX2e}
58 //\ProvidesPackage{lyx}[1996/01/11 LLE v0.2 (LyX LaTeX Extensions)]
59 //\message{LyX LaTeX Extensions (LLE v0.2) of 11-Jan-1996.}
61 static docstring const lyx_def = from_ascii(
62 "\\providecommand{\\LyX}{L\\kern-.1667em\\lower.25em\\hbox{Y}\\kern-.125emX\\@}");
64 static docstring const lyx_hyperref_def = from_ascii(
65 "\\providecommand{\\LyX}{\\texorpdfstring%\n"
66 " {L\\kern-.1667em\\lower.25em\\hbox{Y}\\kern-.125emX\\@}\n"
69 static docstring const noun_def = from_ascii(
70 "\\newcommand{\\noun}[1]{\\textsc{#1}}");
72 static docstring const lyxarrow_def = from_ascii(
73 "\\DeclareRobustCommand*{\\lyxarrow}{%\n"
75 "{\\leavevmode\\,$\\triangleleft$\\,\\allowbreak}\n"
76 "{\\leavevmode\\,$\\triangleright$\\,\\allowbreak}}");
78 // for quotes without babel. This does not give perfect results, but
79 // anybody serious about non-english quotes should use babel (JMarc).
81 static docstring const quotedblbase_def = from_ascii(
82 "\\ProvideTextCommandDefault{\\quotedblbase}{%\n"
83 " \\raisebox{-1.4ex}[1ex][.5ex]{\\textquotedblright}%\n"
84 " \\penalty10000\\hskip0em\\relax%\n"
87 static docstring const quotesinglbase_def = from_ascii(
88 "\\ProvideTextCommandDefault{\\quotesinglbase}{%\n"
89 " \\raisebox{-1.4ex}[1ex][.5ex]{\\textquoteright}%\n"
90 " \\penalty10000\\hskip0em\\relax%\n"
93 static docstring const guillemotleft_def = from_ascii(
94 "\\ProvideTextCommandDefault{\\guillemotleft}{%\n"
95 " {\\usefont{U}{lasy}{m}{n}\\char'50\\kern-.15em\\char'50}%\n"
96 "\\penalty10000\\hskip0pt\\relax%\n"
99 static docstring const guillemotright_def = from_ascii(
100 "\\ProvideTextCommandDefault{\\guillemotright}{%\n"
101 " \\penalty10000\\hskip0pt%\n"
102 " {\\usefont{U}{lasy}{m}{n}\\char'51\\kern-.15em\\char'51}%\n"
105 static docstring const guilsinglleft_def = from_ascii(
106 "\\ProvideTextCommandDefault{\\guilsinglleft}{%\n"
107 " {\\usefont{U}{lasy}{m}{n}\\char'50}%\n"
108 " \\penalty10000\\hskip0pt\\relax%\n"
111 static docstring const guilsinglright_def = from_ascii(
112 "\\ProvideTextCommandDefault{\\guilsinglright}{%\n"
113 " \\penalty10000\\hskip0pt%\n"
114 " {\\usefont{U}{lasy}{m}{n}\\char'51}%\n"
117 static docstring const paragraphleftindent_def = from_ascii(
118 "\\newenvironment{LyXParagraphLeftIndent}[1]%\n"
120 " \\begin{list}{}{%\n"
121 " \\setlength{\\topsep}{0pt}%\n"
122 " \\addtolength{\\leftmargin}{#1}\n"
123 // ho hum, yet more things commented out with no hint as to why they
124 // weren't just removed
125 // "%% \\addtolength{\\leftmargin}{#1\\textwidth}\n"
126 // "%% \\setlength{\\textwidth}{#2\\textwidth}\n"
127 // "%% \\setlength\\listparindent\\parindent%\n"
128 // "%% \\setlength\\itemindent\\parindent%\n"
129 " \\setlength{\\parsep}{0pt plus 1pt}%\n"
135 static docstring const floatingfootnote_def = from_ascii(
136 "%% Special footnote code from the package 'stblftnt.sty'\n"
137 "%% Author: Robin Fairbairns -- Last revised Dec 13 1996\n"
138 "\\let\\SF@@footnote\\footnote\n"
139 "\\def\\footnote{\\ifx\\protect\\@typeset@protect\n"
140 " \\expandafter\\SF@@footnote\n"
142 " \\expandafter\\SF@gobble@opt\n"
145 "\\expandafter\\def\\csname SF@gobble@opt \\endcsname{\\@ifnextchar[%]\n"
146 " \\SF@gobble@twobracket\n"
149 "\\edef\\SF@gobble@opt{\\noexpand\\protect\n"
150 " \\expandafter\\noexpand\\csname SF@gobble@opt \\endcsname}\n"
151 "\\def\\SF@gobble@twobracket[#1]#2{}\n");
153 static docstring const binom_def = from_ascii(
154 "%% Binom macro for standard LaTeX users\n"
155 "\\newcommand{\\binom}[2]{{#1 \\choose #2}}\n");
157 static docstring const mathcircumflex_def = from_ascii(
158 "%% For printing a cirumflex inside a formula\n"
159 "\\newcommand{\\mathcircumflex}[0]{\\mbox{\\^{}}}\n");
161 static docstring const tabularnewline_def = from_ascii(
162 "%% Because html converters don't know tabularnewline\n"
163 "\\providecommand{\\tabularnewline}{\\\\}\n");
165 static docstring const lyxgreyedout_def = from_ascii(
166 "%% The greyedout annotation environment\n"
167 "\\newenvironment{lyxgreyedout}\n"
168 " {\\textcolor{note_fontcolor}\\bgroup\\ignorespaces}\n"
169 " {\\ignorespacesafterend\\egroup}\n");
171 // We want to omit the file extension for includegraphics, but this does not
172 // work when the filename contains other dots.
173 // Idea from http://www.tex.ac.uk/cgi-bin/texfaq2html?label=unkgrfextn
174 static docstring const lyxdot_def = from_ascii(
175 "%% A simple dot to overcome graphicx limitations\n"
176 "\\newcommand{\\lyxdot}{.}\n");
178 static docstring const changetracking_dvipost_def = from_ascii(
179 "%% Change tracking with dvipost\n"
181 "\\dvipost{osstart color push Red}\n"
182 "\\dvipost{osend color pop}\n"
183 "\\dvipost{cbstart color push Blue}\n"
184 "\\dvipost{cbend color pop}\n"
185 "\\newcommand{\\lyxadded}[3]{\\changestart#3\\changeend}\n"
186 "\\newcommand{\\lyxdeleted}[3]{%\n"
187 "\\changestart\\overstrikeon#3\\overstrikeoff\\changeend}\n");
189 static docstring const changetracking_xcolor_ulem_def = from_ascii(
190 "%% Change tracking with ulem\n"
191 "\\newcommand{\\lyxadded}[3]{{\\color{lyxadded}{}#3}}\n"
192 "\\newcommand{\\lyxdeleted}[3]{{\\color{lyxdeleted}\\sout{#3}}}\n");
194 static docstring const changetracking_xcolor_ulem_hyperref_def = from_ascii(
195 "%% Change tracking with ulem\n"
196 "\\newcommand{\\lyxadded}[3]{{\\texorpdfstring{\\color{lyxadded}{}}{}#3}}\n"
197 "\\newcommand{\\lyxdeleted}[3]{{\\texorpdfstring{\\color{lyxdeleted}\\sout{#3}}{}}}\n");
199 static docstring const changetracking_none_def = from_ascii(
200 "\\newcommand{\\lyxadded}[3]{#3}\n"
201 "\\newcommand{\\lyxdeleted}[3]{}\n");
203 static docstring const textgreek_def = from_ascii(
204 "\\DeclareRobustCommand{\\greektext}{%\n"
205 " \\fontencoding{LGR}\\selectfont\\def\\encodingdefault{LGR}}\n"
206 "\\DeclareRobustCommand{\\textgreek}[1]{\\leavevmode{\\greektext #1}}\n"
207 "\\DeclareFontEncoding{LGR}{}{}\n"
208 "\\DeclareTextSymbol{\\~}{LGR}{126}");
210 static docstring const textcyr_def = from_ascii(
211 "\\DeclareRobustCommand{\\cyrtext}{%\n"
212 " \\fontencoding{T2A}\\selectfont\\def\\encodingdefault{T2A}}\n"
213 "\\DeclareRobustCommand{\\textcyr}[1]{\\leavevmode{\\cyrtext #1}}\n"
214 "\\AtBeginDocument{\\DeclareFontEncoding{T2A}{}{}}\n");
216 static docstring const lyxmathsym_def = from_ascii(
217 "\\newcommand{\\lyxmathsym}[1]{\\ifmmode\\begingroup\\def\\b@ld{bold}\n"
218 " \\text{\\ifx\\math@version\\b@ld\\bfseries\\fi#1}\\endgroup\\else#1\\fi}\n");
220 static docstring const papersizedvi_def = from_ascii(
221 "\\special{papersize=\\the\\paperwidth,\\the\\paperheight}\n");
223 static docstring const papersizepdf_def = from_ascii(
224 "\\pdfpageheight\\paperheight\n"
225 "\\pdfpagewidth\\paperwidth\n");
227 static docstring const cedilla_def = from_ascii(
228 "\\newcommand{\\docedilla}[2]{\\underaccent{#1\\mathchar'30}{#2}}\n"
229 "\\newcommand{\\cedilla}[1]{\\mathpalette\\docedilla{#1}}\n");
231 static docstring const subring_def = from_ascii(
232 "\\newcommand{\\dosubring}[2]{\\underaccent{#1\\mathchar'27}{#2}}\n"
233 "\\newcommand{\\subring}[1]{\\mathpalette\\dosubring{#1}}\n");
235 static docstring const subdot_def = from_ascii(
236 "\\newcommand{\\dosubdot}[2]{\\underaccent{#1.}{#2}}\n"
237 "\\newcommand{\\subdot}[1]{\\mathpalette\\dosubdot{#1}}\n");
239 static docstring const subhat_def = from_ascii(
240 "\\newcommand{\\dosubhat}[2]{\\underaccent{#1\\mathchar'136}{#2}}\n"
241 "\\newcommand{\\subhat}[1]{\\mathpalette\\dosubhat{#1}}\n");
243 static docstring const subtilde_def = from_ascii(
244 "\\newcommand{\\dosubtilde}[2]{\\underaccent{#1\\mathchar'176}{#2}}\n"
245 "\\newcommand{\\subtilde}[1]{\\mathpalette\\dosubtilde{#1}}\n");
247 static docstring const dacute_def = from_ascii(
248 "\\DeclareMathAccent{\\dacute}{\\mathalpha}{operators}{'175}\n");
250 static docstring const tipasymb_def = from_ascii(
251 "\\DeclareFontEncoding{T3}{}{}\n"
252 "\\DeclareSymbolFont{tipasymb}{T3}{cmr}{m}{n}\n");
254 static docstring const dgrave_def = from_ascii(
255 "\\DeclareMathAccent{\\dgrave}{\\mathord}{tipasymb}{'15}\n");
257 static docstring const rcap_def = from_ascii(
258 "\\DeclareMathAccent{\\rcap}{\\mathord}{tipasymb}{'20}\n");
260 static docstring const ogonek_def = from_ascii(
261 "\\newcommand{\\doogonek}[2]{\\setbox0=\\hbox{$#1#2$}\\underaccent{#1\\mkern-6mu\n"
262 " \\ifx#2O\\hskip0.5\\wd0\\else\\ifx#2U\\hskip0.5\\wd0\\else\\hskip\\wd0\\fi\\fi\n"
263 " \\ifx#2o\\mkern-2mu\\else\\ifx#2e\\mkern-1mu\\fi\\fi\n"
264 " \\mathchar\"0\\hexnumber@\\symtipasymb0C}{#2}}\n"
265 "\\newcommand{\\ogonek}[1]{\\mathpalette\\doogonek{#1}}\n");
267 static docstring const lyxref_def = from_ascii(
268 "\\RS@ifundefined{subref}\n"
269 " {\\def\\RSsubtxt{section~}\\newref{sub}{name = \\RSsubtxt}}\n"
271 "\\RS@ifundefined{thmref}\n"
272 " {\\def\\RSthmtxt{theorem~}\\newref{thm}{name = \\RSthmtxt}}\n"
274 "\\RS@ifundefined{lemref}\n"
275 " {\\def\\RSlemtxt{lemma~}\\newref{lem}{name = \\RSlemtxt}}\n"
279 /////////////////////////////////////////////////////////////////////
283 /////////////////////////////////////////////////////////////////////
286 LaTeXFeatures::LaTeXFeatures(Buffer const & b, BufferParams const & p,
287 OutputParams const & r)
288 : buffer_(&b), params_(p), runparams_(r), in_float_(false)
292 bool LaTeXFeatures::useBabel() const
294 if (usePolyglossia())
296 if (bufferParams().lang_package == "default")
297 return (lyxrc.language_package_selection != LyXRC::LP_NONE)
298 || (bufferParams().language->lang() != lyxrc.default_language
299 && !bufferParams().language->babel().empty())
300 || this->hasLanguages();
301 return (bufferParams().lang_package != "none")
302 || (bufferParams().language->lang() != lyxrc.default_language
303 && !bufferParams().language->babel().empty())
304 || this->hasLanguages();
308 bool LaTeXFeatures::usePolyglossia() const
310 if (bufferParams().lang_package == "default")
311 return (lyxrc.language_package_selection == LyXRC::LP_AUTO)
312 && isRequired("polyglossia")
313 && isAvailable("polyglossia")
314 && !params_.documentClass().provides("babel")
315 && this->hasPolyglossiaLanguages();
316 return (bufferParams().lang_package == "auto")
317 && isRequired("polyglossia")
318 && isAvailable("polyglossia")
319 && !params_.documentClass().provides("babel")
320 && this->hasPolyglossiaLanguages();
324 void LaTeXFeatures::require(string const & name)
326 features_.insert(name);
330 void LaTeXFeatures::require(set<string> const & names)
332 features_.insert(names.begin(), names.end());
336 void LaTeXFeatures::useLayout(docstring const & layoutname)
338 // Some code to avoid loops in dependency definition
339 static int level = 0;
340 const int maxlevel = 30;
341 if (level > maxlevel) {
342 lyxerr << "LaTeXFeatures::useLayout: maximum level of "
343 << "recursion attained by layout "
344 << to_utf8(layoutname) << endl;
348 DocumentClass const & tclass = params_.documentClass();
349 if (tclass.hasLayout(layoutname)) {
350 // Is this layout already in usedLayouts?
351 if (find(usedLayouts_.begin(), usedLayouts_.end(), layoutname)
352 != usedLayouts_.end())
355 Layout const & layout = tclass[layoutname];
356 require(layout.requires());
358 if (!layout.depends_on().empty()) {
360 useLayout(layout.depends_on());
363 usedLayouts_.push_back(layoutname);
365 lyxerr << "LaTeXFeatures::useLayout: layout `"
366 << to_utf8(layoutname) << "' does not exist in this class"
374 void LaTeXFeatures::useInsetLayout(InsetLayout const & lay)
376 docstring const & lname = lay.name();
377 DocumentClass const & tclass = params_.documentClass();
379 // this is a default inset layout, nothing useful here
380 if (!tclass.hasInsetLayout(lname))
382 // Is this layout already in usedInsetLayouts?
383 if (find(usedInsetLayouts_.begin(), usedInsetLayouts_.end(), lname)
384 != usedInsetLayouts_.end())
387 require(lay.requires());
388 usedInsetLayouts_.push_back(lname);
392 bool LaTeXFeatures::isRequired(string const & name) const
394 return features_.find(name) != features_.end();
398 bool LaTeXFeatures::mustProvide(string const & name) const
400 return isRequired(name) && !params_.documentClass().provides(name);
404 bool LaTeXFeatures::isAvailable(string const & name)
406 string::size_type const i = name.find("->");
407 if (i != string::npos) {
408 string const from = name.substr(0,i);
409 string const to = name.substr(i+2);
410 //LYXERR0("from=[" << from << "] to=[" << to << "]");
411 return theConverters().isReachable(from, to);
413 return LaTeXPackages::isAvailable(name);
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::addCSSSnippet(std::string const & snippet)
428 SnippetList::const_iterator begin = css_snippets_.begin();
429 SnippetList::const_iterator end = css_snippets_.end();
430 if (find(begin, end, snippet) == end)
431 css_snippets_.push_back(snippet);
435 void LaTeXFeatures::useFloat(string const & name, bool subfloat)
437 if (!usedFloats_[name])
438 usedFloats_[name] = subfloat;
441 // We only need float.sty if we use non builtin floats, or if we
442 // use the "H" modifier. This includes modified table and
443 // figure floats. (Lgb)
444 Floating const & fl = params_.documentClass().floats().getType(name);
445 if (!fl.floattype().empty() && fl.usesFloatPkg()) {
451 void LaTeXFeatures::useLanguage(Language const * lang)
453 if (!lang->babel().empty())
454 UsedLanguages_.insert(lang);
455 if (lang->lang() == "vietnamese")
456 require("vietnamese");
457 // CJK languages do not have a babel name.
458 // They use the CJK package
459 if (lang->encoding()->package() == Encoding::CJK)
461 // japanese package is special
462 if (lang->encoding()->package() == Encoding::japanese)
467 void LaTeXFeatures::includeFile(docstring const & key, string const & name)
469 IncludedFiles_[key] = name;
473 bool LaTeXFeatures::hasLanguages() const
475 return !UsedLanguages_.empty();
479 bool LaTeXFeatures::hasPolyglossiaLanguages() const
481 LanguageList::const_iterator const begin = UsedLanguages_.begin();
482 for (LanguageList::const_iterator cit = begin;
483 cit != UsedLanguages_.end();
485 if ((*cit)->polyglossia().empty())
492 string LaTeXFeatures::getLanguages() const
494 ostringstream languages;
496 LanguageList::const_iterator const begin = UsedLanguages_.begin();
497 for (LanguageList::const_iterator cit = begin;
498 cit != UsedLanguages_.end();
502 languages << (*cit)->babel();
504 return languages.str();
508 std::map<std::string, std::string> LaTeXFeatures::getPolyglossiaLanguages() const
510 std::map<std::string, std::string> languages;
512 LanguageList::const_iterator const begin = UsedLanguages_.begin();
513 for (LanguageList::const_iterator cit = begin;
514 cit != UsedLanguages_.end();
516 languages[(*cit)->polyglossia()] = (*cit)->polyglossiaOpts();
522 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding) const
524 // This does only find encodings of languages supported by babel, but
525 // that does not matter since we don't have a language with an
526 // encoding supported by inputenc but without babel support.
527 set<string> encodings;
528 LanguageList::const_iterator it = UsedLanguages_.begin();
529 LanguageList::const_iterator end = UsedLanguages_.end();
530 for (; it != end; ++it)
531 if ((*it)->encoding()->latexName() != doc_encoding &&
532 ((*it)->encoding()->package() == Encoding::inputenc
533 || (*it)->encoding()->package() == Encoding::japanese))
534 encodings.insert((*it)->encoding()->latexName());
540 char const * simplefeatures[] = {
541 // note that the package order here will be the same in the LaTeX-output
548 // subfig is handled in BufferParams.cpp
552 /*For a successful cooperation of the `wrapfig' package with the
553 `float' package you should load the `wrapfig' package *after*
554 the `float' package. See the caption package documentation
587 // listings is handled in BufferParams.cpp
596 int const nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
601 string const LaTeXFeatures::getColorOptions() const
603 ostringstream colors;
605 // Handling the color packages separately is needed to be able to load them
606 // before babel when hyperref is loaded with the colorlinks option
607 // for more info see Bufferparams.cpp
610 if (mustProvide("color") || mustProvide("xcolor")) {
611 string const package =
612 (mustProvide("xcolor") ? "xcolor" : "color");
613 if (params_.graphics_driver == "default"
614 || params_.graphics_driver == "none")
615 colors << "\\usepackage{" << package << "}\n";
617 colors << "\\usepackage["
618 << params_.graphics_driver
619 << "]{" << package << "}\n";
622 // pdfcolmk must be loaded after color
623 if (mustProvide("pdfcolmk"))
624 colors << "\\usepackage{pdfcolmk}\n";
626 // the following 3 color commands must be set after color
627 // is loaded and before pdfpages, therefore add the command
628 // here define the set color
629 if (mustProvide("pagecolor")) {
630 colors << "\\definecolor{page_backgroundcolor}{rgb}{";
631 colors << outputLaTeXColor(params_.backgroundcolor) << "}\n";
632 // set the page color
633 colors << "\\pagecolor{page_backgroundcolor}\n";
636 if (mustProvide("fontcolor")) {
637 colors << "\\definecolor{document_fontcolor}{rgb}{";
638 colors << outputLaTeXColor(params_.fontcolor) << "}\n";
640 colors << "\\color{document_fontcolor}\n";
643 if (mustProvide("lyxgreyedout")) {
644 colors << "\\definecolor{note_fontcolor}{rgb}{";
645 colors << outputLaTeXColor(params_.notefontcolor) << "}\n";
646 // the color will be set together with the definition of
647 // the lyxgreyedout environment (see lyxgreyedout_def)
650 // color for shaded boxes
651 if (isRequired("framed") && mustProvide("color")) {
652 colors << "\\definecolor{shadecolor}{rgb}{";
653 colors << outputLaTeXColor(params_.boxbgcolor) << "}\n";
654 // this color is automatically used by the LaTeX-package "framed"
661 string const LaTeXFeatures::getPackages() const
663 ostringstream packages;
664 DocumentClass const & tclass = params_.documentClass();
666 // FIXME: currently, we can only load packages and macros known
668 // However, with the Require tag of layouts/custom insets,
669 // also unknown packages can be requested. They are silently
670 // swallowed now. We should change this eventually.
673 // These are all the 'simple' includes. i.e
674 // packages which we just \usepackage{package}
676 for (int i = 0; i < nb_simplefeatures; ++i) {
677 if (mustProvide(simplefeatures[i]))
678 packages << "\\usepackage{"
679 << simplefeatures[i] << "}\n";
683 // The rest of these packages are somewhat more complicated
687 // if fontspec is used, AMS packages have to be loaded before
688 // fontspec (in BufferParams)
689 string const amsPackages = loadAMSPackages();
690 if (!params_.useNonTeXFonts && !amsPackages.empty())
691 packages << amsPackages;
693 // fixltx2e must be loaded after amsthm, since amsthm produces an error with
694 // the redefined \[ command (bug 7233). Load it as early as possible, since
695 // other packages might profit from it.
696 if (mustProvide("fixltx2e"))
697 packages << "\\usepackage{fixltx2e}\n";
699 // wasysym is a simple feature, but it must be after amsmath if both
701 // wasysym redefines some integrals (e.g. iint) from amsmath. That
702 // leads to inconsistent integrals. We only load this package if
703 // the document does not contain integrals (then isRequired("esint")
704 // is false) or if esint is used, since esint redefines all relevant
705 // integral symbols from wasysym and amsmath.
706 // See http://www.lyx.org/trac/ticket/1942
707 if (mustProvide("wasysym") &&
708 params_.use_package("wasysym") != BufferParams::package_off &&
709 (params_.use_package("esint") != BufferParams::package_off || !isRequired("esint")))
710 packages << "\\usepackage{wasysym}\n";
712 // accents must be loaded after amsmath
713 if (mustProvide("accents") &&
714 params_.use_package("accents") != BufferParams::package_off)
715 packages << "\\usepackage{accents}\n";
717 // mathdots must be loaded after amsmath
718 if (mustProvide("mathdots") &&
719 params_.use_package("mathdots") != BufferParams::package_off)
720 packages << "\\usepackage{mathdots}\n";
722 // yhmath must be loaded after amsmath
723 if (mustProvide("yhmath") &&
724 params_.use_package("yhmath") != BufferParams::package_off)
725 packages << "\\usepackage{yhmath}\n";
727 if (mustProvide("undertilde") &&
728 params_.use_package("undertilde") != BufferParams::package_off)
729 packages << "\\usepackage{undertilde}\n";
731 // [x]color and pdfcolmk are handled in getColorOptions() above
734 if (isRequired("makeidx") || isRequired("splitidx")) {
735 if (!tclass.provides("makeidx") && !isRequired("splitidx"))
736 packages << "\\usepackage{makeidx}\n";
737 if (!tclass.provides("splitidx") && isRequired("splitidx"))
738 packages << "\\usepackage{splitidx}\n";
739 packages << "\\makeindex\n";
743 if (mustProvide("graphicx") && params_.graphics_driver != "none") {
744 if (params_.graphics_driver == "default")
745 packages << "\\usepackage{graphicx}\n";
747 packages << "\\usepackage["
748 << params_.graphics_driver
752 // lyxskak.sty --- newer chess support based on skak.sty
753 if (mustProvide("chess"))
754 packages << "\\usepackage[ps,mover]{lyxskak}\n";
757 if (mustProvide("setspace") && !tclass.provides("SetSpace"))
758 packages << "\\usepackage{setspace}\n";
760 // esint must be after amsmath and wasysym, since it will redeclare
761 // inconsistent integral symbols
762 if (mustProvide("esint") &&
763 params_.use_package("esint") != BufferParams::package_off)
764 packages << "\\usepackage{esint}\n";
767 // Some classes load natbib themselves, but still allow (or even require)
768 // plain numeric citations (ReVTeX is such a case, see bug 5182).
769 // This special case is indicated by the "natbib-internal" key.
770 if (mustProvide("natbib") && !tclass.provides("natbib-internal")) {
771 packages << "\\usepackage[";
772 if (params_.citeEngine() == ENGINE_NATBIB_NUMERICAL)
773 packages << "numbers";
775 packages << "authoryear";
776 packages << "]{natbib}\n";
779 // jurabib -- we need version 0.6 at least.
780 if (mustProvide("jurabib"))
781 packages << "\\usepackage{jurabib}[2004/01/25]\n";
783 // xargs -- we need version 1.09 at least
784 if (mustProvide("xargs"))
785 packages << "\\usepackage{xargs}[2008/03/08]\n";
787 // bibtopic -- the dot provides the aux file naming which
789 if (mustProvide("bibtopic"))
790 packages << "\\usepackage[dot]{bibtopic}\n";
792 if (mustProvide("xy"))
793 packages << "\\usepackage[all]{xy}\n";
795 if (mustProvide("feyn"))
796 packages << "\\usepackage{feyn}\n"; //Diagram
798 if (mustProvide("ulem"))
799 packages << "\\PassOptionsToPackage{normalem}{ulem}\n"
800 "\\usepackage{ulem}\n";
802 if (params_.use_package("mhchem") == BufferParams::package_on ||
803 (mustProvide("mhchem") &&
804 params_.use_package("mhchem") != BufferParams::package_off))
805 packages << "\\PassOptionsToPackage{version=3}{mhchem}\n"
806 "\\usepackage{mhchem}\n";
808 if (mustProvide("nomencl")) {
809 // Make it work with the new and old version of the package,
810 // but don't use the compatibility option since it is
811 // incompatible to other packages.
812 packages << "\\usepackage{nomencl}\n"
813 "% the following is useful when we have the old nomencl.sty package\n"
814 "\\providecommand{\\printnomenclature}{\\printglossary}\n"
815 "\\providecommand{\\makenomenclature}{\\makeglossary}\n"
816 "\\makenomenclature\n";
819 // fixltx2e provides subscript
820 if (mustProvide("subscript") && !isRequired("fixltx2e"))
821 packages << "\\usepackage{subscript}\n";
823 return packages.str();
827 string LaTeXFeatures::getPreambleSnippets() const
830 SnippetList::const_iterator pit = preamble_snippets_.begin();
831 SnippetList::const_iterator pend = preamble_snippets_.end();
832 for (; pit != pend; ++pit)
833 snip << *pit << '\n';
838 std::string LaTeXFeatures::getCSSSnippets() const
841 SnippetList::const_iterator pit = css_snippets_.begin();
842 SnippetList::const_iterator pend = css_snippets_.end();
843 for (; pit != pend; ++pit)
844 snip << *pit << '\n';
849 docstring const LaTeXFeatures::getMacros() const
851 odocstringstream macros;
853 if (!preamble_snippets_.empty()) {
855 macros << from_utf8(getPreambleSnippets());
858 if (mustProvide("papersize")) {
859 if (runparams_.flavor == OutputParams::LATEX)
860 macros << papersizedvi_def << '\n';
862 macros << papersizepdf_def << '\n';
865 if (mustProvide("LyX")) {
866 if (isRequired("hyperref"))
867 macros << lyx_hyperref_def << '\n';
869 macros << lyx_def << '\n';
872 if (mustProvide("noun"))
873 macros << noun_def << '\n';
875 if (mustProvide("lyxarrow"))
876 macros << lyxarrow_def << '\n';
878 if (!usePolyglossia() && mustProvide("textgreek")) {
879 // Avoid a LaTeX error if times fonts are used and the grtimes
880 // package is installed but actual fonts are not (bug 6469).
881 if (params_.fonts_roman == "times")
882 macros << subst(textgreek_def,
883 from_ascii("\\greektext #1"),
884 from_ascii("%\n \\IfFileExists"
885 "{grtm10.tfm}{}{\\fontfamily"
886 "{cmr}}\\greektext #1"))
889 macros << textgreek_def << '\n';
892 if (!usePolyglossia() && mustProvide("textcyr"))
893 macros << textcyr_def << '\n';
895 if (mustProvide("lyxmathsym"))
896 macros << lyxmathsym_def << '\n';
898 if (mustProvide("cedilla"))
899 macros << cedilla_def << '\n';
901 if (mustProvide("subring"))
902 macros << subring_def << '\n';
904 if (mustProvide("subdot"))
905 macros << subdot_def << '\n';
907 if (mustProvide("subhat"))
908 macros << subhat_def << '\n';
910 if (mustProvide("subtilde"))
911 macros << subtilde_def << '\n';
913 if (mustProvide("dacute"))
914 macros << dacute_def << '\n';
916 if (mustProvide("tipasymb"))
917 macros << tipasymb_def << '\n';
919 if (mustProvide("dgrave"))
920 macros << dgrave_def << '\n';
922 if (mustProvide("rcap"))
923 macros << rcap_def << '\n';
925 if (mustProvide("ogonek"))
926 macros << ogonek_def << '\n';
929 if (mustProvide("quotesinglbase"))
930 macros << quotesinglbase_def << '\n';
931 if (mustProvide("quotedblbase"))
932 macros << quotedblbase_def << '\n';
933 if (mustProvide("guilsinglleft"))
934 macros << guilsinglleft_def << '\n';
935 if (mustProvide("guilsinglright"))
936 macros << guilsinglright_def << '\n';
937 if (mustProvide("guillemotleft"))
938 macros << guillemotleft_def << '\n';
939 if (mustProvide("guillemotright"))
940 macros << guillemotright_def << '\n';
943 if (mustProvide("binom") && !isRequired("amsmath"))
944 macros << binom_def << '\n';
945 if (mustProvide("mathcircumflex"))
946 macros << mathcircumflex_def << '\n';
949 if (mustProvide("ParagraphLeftIndent"))
950 macros << paragraphleftindent_def;
951 if (mustProvide("NeedLyXFootnoteCode"))
952 macros << floatingfootnote_def;
954 // some problems with tex->html converters
955 if (mustProvide("NeedTabularnewline"))
956 macros << tabularnewline_def;
958 // greyed-out environment (note inset)
959 // the color is specified in the routine
960 // getColorOptions() to avoid LaTeX-package clashes
961 if (mustProvide("lyxgreyedout"))
962 macros << lyxgreyedout_def;
964 if (mustProvide("lyxdot"))
965 macros << lyxdot_def << '\n';
968 getFloatDefinitions(macros);
970 if (mustProvide("refstyle"))
971 macros << lyxref_def << '\n';
974 if (mustProvide("ct-dvipost"))
975 macros << changetracking_dvipost_def;
977 if (mustProvide("ct-xcolor-ulem")) {
978 streamsize const prec = macros.precision(2);
980 RGBColor cadd = rgbFromHexName(lcolor.getX11Name(Color_addedtext));
981 macros << "\\providecolor{lyxadded}{rgb}{"
982 << cadd.r / 255.0 << ',' << cadd.g / 255.0 << ',' << cadd.b / 255.0 << "}\n";
984 RGBColor cdel = rgbFromHexName(lcolor.getX11Name(Color_deletedtext));
985 macros << "\\providecolor{lyxdeleted}{rgb}{"
986 << cdel.r / 255.0 << ',' << cdel.g / 255.0 << ',' << cdel.b / 255.0 << "}\n";
988 macros.precision(prec);
990 if (isRequired("hyperref"))
991 macros << changetracking_xcolor_ulem_hyperref_def;
993 macros << changetracking_xcolor_ulem_def;
996 if (mustProvide("ct-none"))
997 macros << changetracking_none_def;
1003 string const LaTeXFeatures::getBabelPresettings() const
1007 LanguageList::const_iterator it = UsedLanguages_.begin();
1008 LanguageList::const_iterator end = UsedLanguages_.end();
1009 for (; it != end; ++it)
1010 if (!(*it)->babel_presettings().empty())
1011 tmp << (*it)->babel_presettings() << '\n';
1012 if (!params_.language->babel_presettings().empty())
1013 tmp << params_.language->babel_presettings() << '\n';
1019 string const LaTeXFeatures::getBabelPostsettings() const
1023 LanguageList::const_iterator it = UsedLanguages_.begin();
1024 LanguageList::const_iterator end = UsedLanguages_.end();
1025 for (; it != end; ++it)
1026 if (!(*it)->babel_postsettings().empty())
1027 tmp << (*it)->babel_postsettings() << '\n';
1028 if (!params_.language->babel_postsettings().empty())
1029 tmp << params_.language->babel_postsettings() << '\n';
1035 bool LaTeXFeatures::needBabelLangOptions() const
1037 if (!lyxrc.language_global_options || params_.language->asBabelOptions())
1040 LanguageList::const_iterator it = UsedLanguages_.begin();
1041 LanguageList::const_iterator end = UsedLanguages_.end();
1042 for (; it != end; ++it)
1043 if ((*it)->asBabelOptions())
1050 string const LaTeXFeatures::loadAMSPackages() const
1053 if (mustProvide("amsthm"))
1054 tmp << "\\usepackage{amsthm}\n";
1056 if (mustProvide("amsmath")
1057 && params_.use_package("amsmath") != BufferParams::package_off) {
1058 tmp << "\\usepackage{amsmath}\n";
1060 // amsbsy and amstext are already provided by amsmath
1061 if (mustProvide("amsbsy"))
1062 tmp << "\\usepackage{amsbsy}\n";
1063 if (mustProvide("amstext"))
1064 tmp << "\\usepackage{amstext}\n";
1067 if (mustProvide("amssymb")
1068 || params_.use_package("amsmath") == BufferParams::package_on)
1069 tmp << "\\usepackage{amssymb}\n";
1075 docstring const LaTeXFeatures::getTClassPreamble() const
1077 // the text class specific preamble
1078 DocumentClass const & tclass = params_.documentClass();
1079 odocstringstream tcpreamble;
1081 tcpreamble << tclass.preamble();
1083 list<docstring>::const_iterator cit = usedLayouts_.begin();
1084 list<docstring>::const_iterator end = usedLayouts_.end();
1085 for (; cit != end; ++cit)
1086 tcpreamble << tclass[*cit].preamble();
1088 cit = usedInsetLayouts_.begin();
1089 end = usedInsetLayouts_.end();
1090 TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1091 for (; cit != end; ++cit) {
1092 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1093 if (it == ils.end())
1095 tcpreamble << it->second.preamble();
1098 return tcpreamble.str();
1102 docstring const LaTeXFeatures::getTClassHTMLPreamble() const
1104 DocumentClass const & tclass = params_.documentClass();
1105 odocstringstream tcpreamble;
1107 tcpreamble << tclass.htmlpreamble();
1109 list<docstring>::const_iterator cit = usedLayouts_.begin();
1110 list<docstring>::const_iterator end = usedLayouts_.end();
1111 for (; cit != end; ++cit)
1112 tcpreamble << tclass[*cit].htmlpreamble();
1114 cit = usedInsetLayouts_.begin();
1115 end = usedInsetLayouts_.end();
1116 TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1117 for (; cit != end; ++cit) {
1118 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1119 if (it == ils.end())
1121 tcpreamble << it->second.htmlpreamble();
1124 return tcpreamble.str();
1128 docstring const LaTeXFeatures::getTClassHTMLStyles() const
1130 DocumentClass const & tclass = params_.documentClass();
1131 odocstringstream tcpreamble;
1133 tcpreamble << tclass.htmlstyles();
1135 list<docstring>::const_iterator cit = usedLayouts_.begin();
1136 list<docstring>::const_iterator end = usedLayouts_.end();
1137 for (; cit != end; ++cit)
1138 tcpreamble << tclass[*cit].htmlstyle();
1140 cit = usedInsetLayouts_.begin();
1141 end = usedInsetLayouts_.end();
1142 TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1143 for (; cit != end; ++cit) {
1144 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1145 if (it == ils.end())
1147 tcpreamble << it->second.htmlstyle();
1150 return tcpreamble.str();
1155 docstring const getFloatI18nPreamble(docstring const & type,
1156 docstring const & name, Language const * lang,
1157 Encoding const & enc, bool const polyglossia)
1159 docstring const language = polyglossia ? from_ascii(lang->polyglossia())
1160 : from_ascii(lang->babel());
1161 docstring const langenc = from_ascii(lang->encoding()->iconvName());
1162 docstring const texenc = from_ascii(lang->encoding()->latexName());
1163 docstring const bufenc = from_ascii(enc.iconvName());
1164 docstring const s1 = docstring(1, 0xF0000);
1165 docstring const s2 = docstring(1, 0xF0001);
1166 docstring const translated = (langenc == bufenc) ? name
1167 : from_ascii("\\inputencoding{") + texenc + from_ascii("}")
1168 + s1 + langenc + s2 + name + s1 + bufenc + s2;
1170 odocstringstream os;
1171 os << "\\addto\\captions" << language
1172 << "{\\renewcommand{\\" << type << "name}{" << translated << "}}\n";
1178 docstring const LaTeXFeatures::getTClassI18nPreamble(bool use_babel, bool use_polyglossia) const
1180 DocumentClass const & tclass = params_.documentClass();
1181 // collect preamble snippets in a set to prevent multiple identical
1182 // commands (would happen if e.g. both theorem and theorem* are used)
1183 set<docstring> snippets;
1184 typedef LanguageList::const_iterator lang_it;
1185 lang_it const lbeg = UsedLanguages_.begin();
1186 lang_it const lend = UsedLanguages_.end();
1187 list<docstring>::const_iterator cit = usedLayouts_.begin();
1188 list<docstring>::const_iterator end = usedLayouts_.end();
1189 for (; cit != end; ++cit) {
1190 // language dependent commands (once per document)
1191 snippets.insert(tclass[*cit].langpreamble(buffer().language(),
1192 buffer().params().encoding(),
1194 // commands for language changing (for multilanguage documents)
1195 if ((use_babel || use_polyglossia) && !UsedLanguages_.empty()) {
1196 snippets.insert(tclass[*cit].babelpreamble(
1197 buffer().language(),
1198 buffer().params().encoding(),
1200 for (lang_it lit = lbeg; lit != lend; ++lit)
1201 snippets.insert(tclass[*cit].babelpreamble(
1203 buffer().params().encoding(),
1207 if ((use_babel || use_polyglossia) && !UsedLanguages_.empty()) {
1208 FloatList const & floats = params_.documentClass().floats();
1209 UsedFloats::const_iterator fit = usedFloats_.begin();
1210 UsedFloats::const_iterator fend = usedFloats_.end();
1211 for (; fit != fend; ++fit) {
1212 Floating const & fl = floats.getType(fit->first);
1213 // we assume builtin floats are translated
1214 if (fl.isPredefined())
1216 docstring const type = from_ascii(fl.floattype());
1217 docstring const flname = from_utf8(fl.name());
1218 docstring name = buffer().language()->translateLayout(fl.name());
1219 // only request translation if we have a real translation
1220 // (that differs from the source)
1222 snippets.insert(getFloatI18nPreamble(
1223 type, name, buffer().language(),
1224 buffer().params().encoding(),
1226 for (lang_it lit = lbeg; lit != lend; ++lit) {
1227 string const code = (*lit)->code();
1228 name = (*lit)->translateLayout(fl.name());
1229 // we assume we have a suitable translation if
1230 // either the language is English (we need to
1231 // translate into English if English is a secondary
1232 // language) or if translateIfPossible returns
1233 // something different to the English source.
1234 bool const have_translation =
1235 (flname != name || contains(code, "en"));
1236 if (have_translation)
1237 snippets.insert(getFloatI18nPreamble(
1239 buffer().params().encoding(),
1245 odocstringstream tcpreamble;
1246 set<docstring>::const_iterator const send = snippets.end();
1247 set<docstring>::const_iterator it = snippets.begin();
1248 for (; it != send; ++it)
1250 return tcpreamble.str();
1254 docstring const LaTeXFeatures::getLyXSGMLEntities() const
1256 // Definition of entities used in the document that are LyX related.
1257 odocstringstream entities;
1259 if (mustProvide("lyxarrow")) {
1260 entities << "<!ENTITY lyxarrow \"->\">" << '\n';
1263 return entities.str();
1267 docstring const LaTeXFeatures::getIncludedFiles(string const & fname) const
1269 odocstringstream sgmlpreamble;
1271 docstring const basename(from_utf8(onlyPath(fname)));
1273 FileMap::const_iterator end = IncludedFiles_.end();
1274 for (FileMap::const_iterator fi = IncludedFiles_.begin();
1277 sgmlpreamble << "\n<!ENTITY " << fi->first
1278 << (isSGMLFileName(fi->second) ? " SYSTEM \"" : " \"")
1279 << makeRelPath(from_utf8(fi->second), basename) << "\">";
1281 return sgmlpreamble.str();
1285 void LaTeXFeatures::showStruct() const
1287 lyxerr << "LyX needs the following commands when LaTeXing:"
1288 << "\n***** Packages:" << getPackages()
1289 << "\n***** Macros:" << to_utf8(getMacros())
1290 << "\n***** Textclass stuff:" << to_utf8(getTClassPreamble())
1291 << "\n***** done." << endl;
1295 Buffer const & LaTeXFeatures::buffer() const
1301 void LaTeXFeatures::setBuffer(Buffer const & buffer)
1307 BufferParams const & LaTeXFeatures::bufferParams() const
1313 void LaTeXFeatures::getFloatDefinitions(odocstream & os) const
1315 FloatList const & floats = params_.documentClass().floats();
1317 // Here we will output the code to create the needed float styles.
1318 // We will try to do this as minimal as possible.
1319 // \floatstyle{ruled}
1320 // \newfloat{algorithm}{htbp}{loa}
1321 // \providecommand{\algorithmname}{Algorithm}
1322 // \floatname{algorithm}{\protect\algorithmname}
1323 UsedFloats::const_iterator cit = usedFloats_.begin();
1324 UsedFloats::const_iterator end = usedFloats_.end();
1325 for (; cit != end; ++cit) {
1326 Floating const & fl = floats.getType(cit->first);
1328 // For builtin floats we do nothing.
1329 if (fl.isPredefined())
1332 // We have to special case "table" and "figure"
1333 if (fl.floattype() == "tabular" || fl.floattype() == "figure") {
1334 // Output code to modify "table" or "figure"
1335 // but only if builtin == false
1336 // and that have to be true at this point in the
1338 docstring const type = from_ascii(fl.floattype());
1339 docstring const placement = from_ascii(fl.placement());
1340 docstring const style = from_ascii(fl.style());
1341 if (!style.empty()) {
1342 os << "\\floatstyle{" << style << "}\n"
1343 << "\\restylefloat{" << type << "}\n";
1345 if (!placement.empty()) {
1346 os << "\\floatplacement{" << type << "}{"
1347 << placement << "}\n";
1350 // The other non builtin floats.
1352 docstring const type = from_ascii(fl.floattype());
1353 docstring const placement = from_ascii(fl.placement());
1354 docstring const ext = from_ascii(fl.ext());
1355 docstring const within = from_ascii(fl.within());
1356 docstring const style = from_ascii(fl.style());
1357 docstring const name =
1358 buffer().language()->translateLayout(fl.name());
1359 os << "\\floatstyle{" << style << "}\n"
1360 << "\\newfloat{" << type << "}{" << placement
1361 << "}{" << ext << '}';
1362 if (!within.empty())
1363 os << '[' << within << ']';
1365 << "\\providecommand{\\" << type << "name}{"
1367 << "\\floatname{" << type << "}{\\protect\\"
1368 << type << "name}\n";
1370 // What missing here is to code to minimalize the code
1371 // output so that the same floatstyle will not be
1372 // used several times, when the same style is still in
1376 os << "\n\\newsubfloat{" << from_ascii(fl.floattype()) << "}\n";
1381 void LaTeXFeatures::resolveAlternatives()
1383 for (Features::iterator it = features_.begin(); it != features_.end();) {
1384 if (contains(*it, '|')) {
1385 vector<string> const alternatives = getVectorFromString(*it, "|");
1386 vector<string>::const_iterator const end = alternatives.end();
1387 vector<string>::const_iterator ita = alternatives.begin();
1388 for (; ita != end; ++ita) {
1389 if (isRequired(*ita))
1393 require(alternatives.front());
1394 features_.erase(it);
1395 it = features_.begin();