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 noun_def = from_ascii(
64 "\\newcommand{\\noun}[1]{\\textsc{#1}}");
66 static docstring const lyxarrow_def = from_ascii(
67 "\\DeclareRobustCommand*{\\lyxarrow}{%\n"
69 "{\\leavevmode\\,$\\triangleleft$\\,\\allowbreak}\n"
70 "{\\leavevmode\\,$\\triangleright$\\,\\allowbreak}}");
72 // for quotes without babel. This does not give perfect results, but
73 // anybody serious about non-english quotes should use babel (JMarc).
75 static docstring const quotedblbase_def = from_ascii(
76 "\\ProvideTextCommandDefault{\\quotedblbase}{%\n"
77 " \\raisebox{-1.4ex}[1ex][.5ex]{\\textquotedblright}%\n"
78 " \\penalty10000\\hskip0em\\relax%\n"
81 static docstring const quotesinglbase_def = from_ascii(
82 "\\ProvideTextCommandDefault{\\quotesinglbase}{%\n"
83 " \\raisebox{-1.4ex}[1ex][.5ex]{\\textquoteright}%\n"
84 " \\penalty10000\\hskip0em\\relax%\n"
87 static docstring const guillemotleft_def = from_ascii(
88 "\\ProvideTextCommandDefault{\\guillemotleft}{%\n"
89 " {\\usefont{U}{lasy}{m}{n}\\char'50\\kern-.15em\\char'50}%\n"
90 "\\penalty10000\\hskip0pt\\relax%\n"
93 static docstring const guillemotright_def = from_ascii(
94 "\\ProvideTextCommandDefault{\\guillemotright}{%\n"
95 " \\penalty10000\\hskip0pt%\n"
96 " {\\usefont{U}{lasy}{m}{n}\\char'51\\kern-.15em\\char'51}%\n"
99 static docstring const guilsinglleft_def = from_ascii(
100 "\\ProvideTextCommandDefault{\\guilsinglleft}{%\n"
101 " {\\usefont{U}{lasy}{m}{n}\\char'50}%\n"
102 " \\penalty10000\\hskip0pt\\relax%\n"
105 static docstring const guilsinglright_def = from_ascii(
106 "\\ProvideTextCommandDefault{\\guilsinglright}{%\n"
107 " \\penalty10000\\hskip0pt%\n"
108 " {\\usefont{U}{lasy}{m}{n}\\char'51}%\n"
111 static docstring const paragraphleftindent_def = from_ascii(
112 "\\newenvironment{LyXParagraphLeftIndent}[1]%\n"
114 " \\begin{list}{}{%\n"
115 " \\setlength{\\topsep}{0pt}%\n"
116 " \\addtolength{\\leftmargin}{#1}\n"
117 // ho hum, yet more things commented out with no hint as to why they
118 // weren't just removed
119 // "%% \\addtolength{\\leftmargin}{#1\\textwidth}\n"
120 // "%% \\setlength{\\textwidth}{#2\\textwidth}\n"
121 // "%% \\setlength\\listparindent\\parindent%\n"
122 // "%% \\setlength\\itemindent\\parindent%\n"
123 " \\setlength{\\parsep}{0pt plus 1pt}%\n"
129 static docstring const floatingfootnote_def = from_ascii(
130 "%% Special footnote code from the package 'stblftnt.sty'\n"
131 "%% Author: Robin Fairbairns -- Last revised Dec 13 1996\n"
132 "\\let\\SF@@footnote\\footnote\n"
133 "\\def\\footnote{\\ifx\\protect\\@typeset@protect\n"
134 " \\expandafter\\SF@@footnote\n"
136 " \\expandafter\\SF@gobble@opt\n"
139 "\\expandafter\\def\\csname SF@gobble@opt \\endcsname{\\@ifnextchar[%]\n"
140 " \\SF@gobble@twobracket\n"
143 "\\edef\\SF@gobble@opt{\\noexpand\\protect\n"
144 " \\expandafter\\noexpand\\csname SF@gobble@opt \\endcsname}\n"
145 "\\def\\SF@gobble@twobracket[#1]#2{}\n");
147 static docstring const binom_def = from_ascii(
148 "%% Binom macro for standard LaTeX users\n"
149 "\\newcommand{\\binom}[2]{{#1 \\choose #2}}\n");
151 static docstring const mathcircumflex_def = from_ascii(
152 "%% For printing a cirumflex inside a formula\n"
153 "\\newcommand{\\mathcircumflex}[0]{\\mbox{\\^{}}}\n");
155 static docstring const tabularnewline_def = from_ascii(
156 "%% Because html converters don't know tabularnewline\n"
157 "\\providecommand{\\tabularnewline}{\\\\}\n");
159 static docstring const lyxgreyedout_def = from_ascii(
160 "%% The greyedout annotation environment\n"
161 "\\newenvironment{lyxgreyedout}{\\textcolor{note_fontcolor}\\bgroup}{\\egroup}\n");
163 // We want to omit the file extension for includegraphics, but this does not
164 // work when the filename contains other dots.
165 // Idea from http://www.tex.ac.uk/cgi-bin/texfaq2html?label=unkgrfextn
166 static docstring const lyxdot_def = from_ascii(
167 "%% A simple dot to overcome graphicx limitations\n"
168 "\\newcommand{\\lyxdot}{.}\n");
170 static docstring const changetracking_dvipost_def = from_ascii(
171 "%% Change tracking with dvipost\n"
173 "\\dvipost{osstart color push Red}\n"
174 "\\dvipost{osend color pop}\n"
175 "\\dvipost{cbstart color push Blue}\n"
176 "\\dvipost{cbend color pop}\n"
177 "\\newcommand{\\lyxadded}[3]{\\changestart#3\\changeend}\n"
178 "\\newcommand{\\lyxdeleted}[3]{%\n"
179 "\\changestart\\overstrikeon#3\\overstrikeoff\\changeend}\n");
181 static docstring const changetracking_xcolor_ulem_def = from_ascii(
182 "%% Change tracking with ulem\n"
183 "\\newcommand{\\lyxadded}[3]{{\\color{lyxadded}{}#3}}\n"
184 "\\newcommand{\\lyxdeleted}[3]{{\\color{lyxdeleted}\\sout{#3}}}\n");
186 static docstring const changetracking_xcolor_ulem_hyperref_def = from_ascii(
187 "%% Change tracking with ulem\n"
188 "\\newcommand{\\lyxadded}[3]{{\\texorpdfstring{\\color{lyxadded}{}}{}#3}}\n"
189 "\\newcommand{\\lyxdeleted}[3]{{\\texorpdfstring{\\color{lyxdeleted}\\sout{#3}}{}}}\n");
191 static docstring const changetracking_none_def = from_ascii(
192 "\\newcommand{\\lyxadded}[3]{#3}\n"
193 "\\newcommand{\\lyxdeleted}[3]{}\n");
195 static docstring const textgreek_def = from_ascii(
196 "\\DeclareRobustCommand{\\greektext}{%\n"
197 " \\fontencoding{LGR}\\selectfont\\def\\encodingdefault{LGR}}\n"
198 "\\DeclareRobustCommand{\\textgreek}[1]{\\leavevmode{\\greektext #1}}\n"
199 "\\DeclareFontEncoding{LGR}{}{}\n"
200 "\\DeclareTextSymbol{\\~}{LGR}{126}");
202 static docstring const textcyr_def = from_ascii(
203 "\\DeclareRobustCommand{\\cyrtext}{%\n"
204 " \\fontencoding{T2A}\\selectfont\\def\\encodingdefault{T2A}}\n"
205 "\\DeclareRobustCommand{\\textcyr}[1]{\\leavevmode{\\cyrtext #1}}\n"
206 "\\AtBeginDocument{\\DeclareFontEncoding{T2A}{}{}}\n");
208 static docstring const lyxmathsym_def = from_ascii(
209 "\\newcommand{\\lyxmathsym}[1]{\\ifmmode\\begingroup\\def\\b@ld{bold}\n"
210 " \\text{\\ifx\\math@version\\b@ld\\bfseries\\fi#1}\\endgroup\\else#1\\fi}\n");
212 static docstring const papersizedvi_def = from_ascii(
213 "\\special{papersize=\\the\\paperwidth,\\the\\paperheight}\n");
215 static docstring const papersizepdf_def = from_ascii(
216 "\\pdfpageheight\\paperheight\n"
217 "\\pdfpagewidth\\paperwidth\n");
219 static docstring const cedilla_def = from_ascii(
220 "\\newcommand{\\docedilla}[2]{\\underaccent{#1\\mathchar'30}{#2}}\n"
221 "\\newcommand{\\cedilla}[1]{\\mathpalette\\docedilla{#1}}\n");
223 static docstring const subring_def = from_ascii(
224 "\\newcommand{\\dosubring}[2]{\\underaccent{#1\\mathchar'27}{#2}}\n"
225 "\\newcommand{\\subring}[1]{\\mathpalette\\dosubring{#1}}\n");
227 static docstring const subdot_def = from_ascii(
228 "\\newcommand{\\dosubdot}[2]{\\underaccent{#1.}{#2}}\n"
229 "\\newcommand{\\subdot}[1]{\\mathpalette\\dosubdot{#1}}\n");
231 static docstring const subhat_def = from_ascii(
232 "\\newcommand{\\dosubhat}[2]{\\underaccent{#1\\mathchar'136}{#2}}\n"
233 "\\newcommand{\\subhat}[1]{\\mathpalette\\dosubhat{#1}}\n");
235 static docstring const subtilde_def = from_ascii(
236 "\\newcommand{\\dosubtilde}[2]{\\underaccent{#1\\mathchar'176}{#2}}\n"
237 "\\newcommand{\\subtilde}[1]{\\mathpalette\\dosubtilde{#1}}\n");
239 static docstring const dacute_def = from_ascii(
240 "\\DeclareMathAccent{\\dacute}{\\mathalpha}{operators}{'175}\n");
242 static docstring const tipasymb_def = from_ascii(
243 "\\DeclareFontEncoding{T3}{}{}\n"
244 "\\DeclareSymbolFont{tipasymb}{T3}{cmr}{m}{n}\n");
246 static docstring const dgrave_def = from_ascii(
247 "\\DeclareMathAccent{\\dgrave}{\\mathord}{tipasymb}{'15}\n");
249 static docstring const rcap_def = from_ascii(
250 "\\DeclareMathAccent{\\rcap}{\\mathord}{tipasymb}{'20}\n");
252 static docstring const ogonek_def = from_ascii(
253 "\\newcommand{\\doogonek}[2]{\\setbox0=\\hbox{$#1#2$}\\underaccent{#1\\mkern-6mu\n"
254 " \\ifx#2O\\hskip0.5\\wd0\\else\\ifx#2U\\hskip0.5\\wd0\\else\\hskip\\wd0\\fi\\fi\n"
255 " \\ifx#2o\\mkern-2mu\\else\\ifx#2e\\mkern-1mu\\fi\\fi\n"
256 " \\mathchar\"0\\hexnumber@\\symtipasymb0C}{#2}}\n"
257 "\\newcommand{\\ogonek}[1]{\\mathpalette\\doogonek{#1}}\n");
259 static docstring const lyxref_def = from_ascii(
260 "\\RS@ifundefined{subref}\n"
261 " {\\def\\RSsubtxt{section~}\\newref{sub}{name = \\RSsubtxt}}\n"
263 "\\RS@ifundefined{thmref}\n"
264 " {\\def\\RSthmtxt{theorem~}\\newref{thm}{name = \\RSthmtxt}}\n"
266 "\\RS@ifundefined{lemref}\n"
267 " {\\def\\RSlemtxt{lemma~}\\newref{lem}{name = \\RSlemtxt}}\n"
271 /////////////////////////////////////////////////////////////////////
275 /////////////////////////////////////////////////////////////////////
277 LaTeXFeatures::Packages LaTeXFeatures::packages_;
280 LaTeXFeatures::LaTeXFeatures(Buffer const & b, BufferParams const & p,
281 OutputParams const & r)
282 : buffer_(&b), params_(p), runparams_(r), in_float_(false)
286 bool LaTeXFeatures::useBabel() const
288 return lyxrc.language_use_babel ||
289 (bufferParams().language->lang() != lyxrc.default_language &&
290 !bufferParams().language->babel().empty()) ||
291 this->hasLanguages();
295 void LaTeXFeatures::require(string const & name)
297 features_.insert(name);
301 void LaTeXFeatures::require(set<string> const & names)
303 features_.insert(names.begin(), names.end());
307 void LaTeXFeatures::getAvailable()
310 support::FileName const real_file = libFileSearch("", "packages.lst");
312 if (real_file.empty())
315 lex.setFile(real_file);
320 // Make sure that we are clean
323 bool finished = false;
325 while (lex.isOK() && !finished) {
327 case Lexer::LEX_FEOF:
331 packages_.insert(lex.getString());
337 void LaTeXFeatures::useLayout(docstring const & layoutname)
339 // Some code to avoid loops in dependency definition
340 static int level = 0;
341 const int maxlevel = 30;
342 if (level > maxlevel) {
343 lyxerr << "LaTeXFeatures::useLayout: maximum level of "
344 << "recursion attained by layout "
345 << to_utf8(layoutname) << endl;
349 DocumentClass const & tclass = params_.documentClass();
350 if (tclass.hasLayout(layoutname)) {
351 // Is this layout already in usedLayouts?
352 if (find(usedLayouts_.begin(), usedLayouts_.end(), layoutname)
353 != usedLayouts_.end())
356 Layout const & layout = tclass[layoutname];
357 require(layout.requires());
359 if (!layout.depends_on().empty()) {
361 useLayout(layout.depends_on());
364 usedLayouts_.push_back(layoutname);
366 lyxerr << "LaTeXFeatures::useLayout: layout `"
367 << to_utf8(layoutname) << "' does not exist in this class"
375 void LaTeXFeatures::useInsetLayout(InsetLayout const & lay)
377 docstring const & lname = lay.name();
378 DocumentClass const & tclass = params_.documentClass();
380 // this is a default inset layout, nothing useful here
381 if (!tclass.hasInsetLayout(lname))
383 // Is this layout already in usedInsetLayouts?
384 if (find(usedInsetLayouts_.begin(), usedInsetLayouts_.end(), lname)
385 != usedInsetLayouts_.end())
388 require(lay.requires());
389 usedInsetLayouts_.push_back(lname);
393 bool LaTeXFeatures::isRequired(string const & name) const
395 return features_.find(name) != features_.end();
399 bool LaTeXFeatures::mustProvide(string const & name) const
401 return isRequired(name) && !params_.documentClass().provides(name);
405 bool LaTeXFeatures::isAvailable(string const & name)
407 string::size_type const i = name.find("->");
408 if (i != string::npos) {
409 string const from = name.substr(0,i);
410 string const to = name.substr(i+2);
411 //LYXERR0("from=[" << from << "] to=[" << to << "]");
412 return theConverters().isReachable(from, to);
415 if (packages_.empty())
418 if (suffixIs(n, ".sty"))
419 n.erase(name.length() - 4);
420 return packages_.find(n) != packages_.end();
424 void LaTeXFeatures::addPreambleSnippet(string const & preamble)
426 SnippetList::const_iterator begin = preamble_snippets_.begin();
427 SnippetList::const_iterator end = preamble_snippets_.end();
428 if (find(begin, end, preamble) == end)
429 preamble_snippets_.push_back(preamble);
433 void LaTeXFeatures::useFloat(string const & name, bool subfloat)
435 if (!usedFloats_[name])
436 usedFloats_[name] = subfloat;
439 // We only need float.sty if we use non builtin floats, or if we
440 // use the "H" modifier. This includes modified table and
441 // figure floats. (Lgb)
442 Floating const & fl = params_.documentClass().floats().getType(name);
443 if (!fl.floattype().empty() && fl.needsFloatPkg()) {
449 void LaTeXFeatures::useLanguage(Language const * lang)
451 if (!lang->babel().empty())
452 UsedLanguages_.insert(lang);
453 if (lang->lang() == "vietnamese")
454 require("vietnamese");
455 // CJK languages do not have a babel name.
456 // They use the CJK package
457 if (lang->encoding()->package() == Encoding::CJK)
459 // japanese package is special
460 if (lang->encoding()->package() == Encoding::japanese)
465 void LaTeXFeatures::includeFile(docstring const & key, string const & name)
467 IncludedFiles_[key] = name;
471 bool LaTeXFeatures::hasLanguages() const
473 return !UsedLanguages_.empty();
477 string LaTeXFeatures::getLanguages() const
479 ostringstream languages;
481 LanguageList::const_iterator const begin = UsedLanguages_.begin();
482 for (LanguageList::const_iterator cit = begin;
483 cit != UsedLanguages_.end();
487 languages << (*cit)->babel();
489 return languages.str();
493 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding) const
495 // This does only find encodings of languages supported by babel, but
496 // that does not matter since we don't have a language with an
497 // encoding supported by inputenc but without babel support.
498 set<string> encodings;
499 LanguageList::const_iterator it = UsedLanguages_.begin();
500 LanguageList::const_iterator end = UsedLanguages_.end();
501 for (; it != end; ++it)
502 if ((*it)->encoding()->latexName() != doc_encoding &&
503 ((*it)->encoding()->package() == Encoding::inputenc
504 || (*it)->encoding()->package() == Encoding::japanese))
505 encodings.insert((*it)->encoding()->latexName());
511 char const * simplefeatures[] = {
512 // note that the package order here will be the same in the LaTeX-output
519 // subfig is handled in BufferParams.cpp
523 /*For a successful cooperation of the `wrapfig' package with the
524 `float' package you should load the `wrapfig' package *after*
525 the `float' package. See the caption package documentation
554 // listings is handled in BufferParams.cpp
562 int const nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
567 string const LaTeXFeatures::getColorOptions() const
569 ostringstream colors;
571 // Handling the color packages separately is needed to be able to load them
572 // before babel when hyperref is loaded with the colorlinks option
573 // for more info see Bufferparams.cpp
576 if (mustProvide("color") || mustProvide("xcolor")) {
577 string const package =
578 (mustProvide("xcolor") ? "xcolor" : "color");
579 if (params_.graphicsDriver == "default"
580 || params_.graphicsDriver == "none")
581 colors << "\\usepackage{" << package << "}\n";
583 colors << "\\usepackage["
584 << params_.graphicsDriver
585 << "]{" << package << "}\n";
588 // pdfcolmk must be loaded after color
589 if (mustProvide("pdfcolmk"))
590 colors << "\\usepackage{pdfcolmk}\n";
592 // the following 3 color commands must be set after color
593 // is loaded and before pdfpages, therefore add the command
594 // here define the set color
595 if (mustProvide("pagecolor")) {
596 colors << "\\definecolor{page_backgroundcolor}{rgb}{";
597 colors << outputLaTeXColor(params_.backgroundcolor) << "}\n";
598 // set the page color
599 colors << "\\pagecolor{page_backgroundcolor}\n";
602 if (mustProvide("fontcolor")) {
603 colors << "\\definecolor{document_fontcolor}{rgb}{";
604 colors << outputLaTeXColor(params_.fontcolor) << "}\n";
606 colors << "\\color{document_fontcolor}\n";
609 if (mustProvide("lyxgreyedout")) {
610 colors << "\\definecolor{note_fontcolor}{rgb}{";
611 colors << outputLaTeXColor(params_.notefontcolor) << "}\n";
612 // the color will be set together with the definition of
613 // the lyxgreyedout environment (see lyxgreyedout_def)
616 // color for shaded boxes
617 if (isRequired("framed") && mustProvide("color")) {
618 colors << "\\definecolor{shadecolor}{rgb}{";
619 colors << outputLaTeXColor(params_.boxbgcolor) << "}\n";
620 // this color is automatically used by the LaTeX-package "framed"
627 string const LaTeXFeatures::getPackages() const
629 ostringstream packages;
630 DocumentClass const & tclass = params_.documentClass();
632 // FIXME: currently, we can only load packages and macros known
634 // However, with the Require tag of layouts/custom insets,
635 // also inknown packages can be requested. They are silently
636 // swallowed now. We should change this eventually.
639 // These are all the 'simple' includes. i.e
640 // packages which we just \usepackage{package}
642 for (int i = 0; i < nb_simplefeatures; ++i) {
643 if (mustProvide(simplefeatures[i]))
644 packages << "\\usepackage{"
645 << simplefeatures[i] << "}\n";
649 // The rest of these packages are somewhat more complicated
653 // esint is preferred for esintoramsmath
654 if ((mustProvide("amsmath")
655 && params_.use_amsmath != BufferParams::package_off)
656 || (mustProvide("esintoramsmath")
657 && params_.use_esint == BufferParams::package_off
658 && params_.use_amsmath != BufferParams::package_off)) {
659 packages << "\\usepackage{amsmath}\n";
661 // amsbsy and amstext are already provided by amsmath
662 if (mustProvide("amsbsy"))
663 packages << "\\usepackage{amsbsy}\n";
664 if (mustProvide("amstext"))
665 packages << "\\usepackage{amstext}\n";
668 // wasysym is a simple feature, but it must be after amsmath if both
670 // wasysym redefines some integrals (e.g. iint) from amsmath. That
671 // leads to inconsistent integrals. We only load this package if
672 // the document does not contain integrals (then isRequired("esint")
673 // is false) or if esint is used, since esint redefines all relevant
674 // integral symbols from wasysym and amsmath.
675 // See http://www.lyx.org/trac/ticket/1942
676 if (mustProvide("wasysym") &&
677 (params_.use_esint != BufferParams::package_off || !isRequired("esint")))
678 packages << "\\usepackage{wasysym}\n";
680 // accents must be loaded after amsmath
681 if (mustProvide("accents"))
682 packages << "\\usepackage{accents}\n";
684 // mathdots must be loaded after amsmath
685 if (mustProvide("mathdots") &&
686 params_.use_mathdots != BufferParams::package_off)
687 packages << "\\usepackage{mathdots}\n";
689 // yhmath must be loaded after amsmath
690 if (mustProvide("yhmath"))
691 packages << "\\usepackage{yhmath}\n";
693 // [x]color and pdfcolmk are handled in getColorOptions() above
696 if (isRequired("makeidx") || isRequired("splitidx")) {
697 if (!tclass.provides("makeidx") && !isRequired("splitidx"))
698 packages << "\\usepackage{makeidx}\n";
699 if (!tclass.provides("splitidx") && isRequired("splitidx"))
700 packages << "\\usepackage{splitidx}\n";
701 packages << "\\makeindex\n";
705 if (mustProvide("graphicx") && params_.graphicsDriver != "none") {
706 if (params_.graphicsDriver == "default")
707 packages << "\\usepackage{graphicx}\n";
709 packages << "\\usepackage["
710 << params_.graphicsDriver
714 // lyxskak.sty --- newer chess support based on skak.sty
715 if (mustProvide("chess"))
716 packages << "\\usepackage[ps,mover]{lyxskak}\n";
719 if (mustProvide("setspace") && !tclass.provides("SetSpace"))
720 packages << "\\usepackage{setspace}\n";
723 if (mustProvide("amssymb")
724 || params_.use_amsmath == BufferParams::package_on)
725 packages << "\\usepackage{amssymb}\n";
727 // esint must be after amsmath and wasysym, since it will redeclare
728 // inconsistent integral symbols
729 if ((mustProvide("esint") || mustProvide("esintoramsmath")) &&
730 params_.use_esint != BufferParams::package_off)
731 packages << "\\usepackage{esint}\n";
734 // Some classes load natbib themselves, but still allow (or even require)
735 // plain numeric citations (ReVTeX is such a case, see bug 5182).
736 // This special case is indicated by the "natbib-internal" key.
737 if (mustProvide("natbib") && !tclass.provides("natbib-internal")) {
738 packages << "\\usepackage[";
739 if (params_.citeEngine() == ENGINE_NATBIB_NUMERICAL)
740 packages << "numbers";
742 packages << "authoryear";
743 packages << "]{natbib}\n";
746 // jurabib -- we need version 0.6 at least.
747 if (mustProvide("jurabib"))
748 packages << "\\usepackage{jurabib}[2004/01/25]\n";
750 // xargs -- we need version 1.09 at least
751 if (mustProvide("xargs"))
752 packages << "\\usepackage{xargs}[2008/03/08]\n";
754 // bibtopic -- the dot provides the aux file naming which
756 if (mustProvide("bibtopic"))
757 packages << "\\usepackage[dot]{bibtopic}\n";
759 if (mustProvide("xy"))
760 packages << "\\usepackage[all]{xy}\n";
762 if (mustProvide("feyn"))
763 packages << "\\usepackage{feyn}\n"; //Diagram
765 if (mustProvide("ulem"))
766 packages << "\\PassOptionsToPackage{normalem}{ulem}\n"
767 "\\usepackage{ulem}\n";
769 if (mustProvide("mhchem") &&
770 params_.use_mhchem != BufferParams::package_off)
771 packages << "\\PassOptionsToPackage{version=3}{mhchem}\n"
772 "\\usepackage{mhchem}\n";
774 if (mustProvide("nomencl")) {
775 // Make it work with the new and old version of the package,
776 // but don't use the compatibility option since it is
777 // incompatible to other packages.
778 packages << "\\usepackage{nomencl}\n"
779 "% the following is useful when we have the old nomencl.sty package\n"
780 "\\providecommand{\\printnomenclature}{\\printglossary}\n"
781 "\\providecommand{\\makenomenclature}{\\makeglossary}\n"
782 "\\makenomenclature\n";
785 return packages.str();
789 string LaTeXFeatures::getPreambleSnippets() const
792 SnippetList::const_iterator pit = preamble_snippets_.begin();
793 SnippetList::const_iterator pend = preamble_snippets_.end();
794 for (; pit != pend; ++pit)
795 snip << *pit << '\n';
800 docstring const LaTeXFeatures::getMacros() const
802 odocstringstream macros;
804 if (!preamble_snippets_.empty()) {
806 macros << from_utf8(getPreambleSnippets());
809 if (mustProvide("papersize")) {
810 if (runparams_.flavor == OutputParams::LATEX)
811 macros << papersizedvi_def << '\n';
813 macros << papersizepdf_def << '\n';
816 if (mustProvide("LyX"))
817 macros << lyx_def << '\n';
819 if (mustProvide("noun"))
820 macros << noun_def << '\n';
822 if (mustProvide("lyxarrow"))
823 macros << lyxarrow_def << '\n';
825 if (mustProvide("textgreek")) {
826 // Avoid a LaTeX error if times fonts are used and the grtimes
827 // package is installed but actual fonts are not (bug 6469).
828 if (params_.fontsRoman == "times")
829 macros << subst(textgreek_def,
830 from_ascii("\\greektext #1"),
831 from_ascii("%\n \\IfFileExists"
832 "{grtm10.tfm}{}{\\fontfamily"
833 "{cmr}}\\greektext #1"))
836 macros << textgreek_def << '\n';
839 if (mustProvide("textcyr"))
840 macros << textcyr_def << '\n';
842 if (mustProvide("lyxmathsym"))
843 macros << lyxmathsym_def << '\n';
845 if (mustProvide("cedilla"))
846 macros << cedilla_def << '\n';
848 if (mustProvide("subring"))
849 macros << subring_def << '\n';
851 if (mustProvide("subdot"))
852 macros << subdot_def << '\n';
854 if (mustProvide("subhat"))
855 macros << subhat_def << '\n';
857 if (mustProvide("subtilde"))
858 macros << subtilde_def << '\n';
860 if (mustProvide("dacute"))
861 macros << dacute_def << '\n';
863 if (mustProvide("tipasymb"))
864 macros << tipasymb_def << '\n';
866 if (mustProvide("dgrave"))
867 macros << dgrave_def << '\n';
869 if (mustProvide("rcap"))
870 macros << rcap_def << '\n';
872 if (mustProvide("ogonek"))
873 macros << ogonek_def << '\n';
876 if (mustProvide("quotesinglbase"))
877 macros << quotesinglbase_def << '\n';
878 if (mustProvide("quotedblbase"))
879 macros << quotedblbase_def << '\n';
880 if (mustProvide("guilsinglleft"))
881 macros << guilsinglleft_def << '\n';
882 if (mustProvide("guilsinglright"))
883 macros << guilsinglright_def << '\n';
884 if (mustProvide("guillemotleft"))
885 macros << guillemotleft_def << '\n';
886 if (mustProvide("guillemotright"))
887 macros << guillemotright_def << '\n';
890 if (mustProvide("binom") && !isRequired("amsmath"))
891 macros << binom_def << '\n';
892 if (mustProvide("mathcircumflex"))
893 macros << mathcircumflex_def << '\n';
896 if (mustProvide("ParagraphLeftIndent"))
897 macros << paragraphleftindent_def;
898 if (mustProvide("NeedLyXFootnoteCode"))
899 macros << floatingfootnote_def;
901 // some problems with tex->html converters
902 if (mustProvide("NeedTabularnewline"))
903 macros << tabularnewline_def;
905 // greyed-out environment (note inset)
906 // the color is specified in the routine
907 // getColorOptions() to avoid LaTeX-package clashes
908 if (mustProvide("lyxgreyedout"))
909 macros << lyxgreyedout_def;
911 if (mustProvide("lyxdot"))
912 macros << lyxdot_def << '\n';
915 getFloatDefinitions(macros);
917 if (mustProvide("refstyle"))
918 macros << lyxref_def << '\n';
921 if (mustProvide("ct-dvipost"))
922 macros << changetracking_dvipost_def;
924 if (mustProvide("ct-xcolor-ulem")) {
925 streamsize const prec = macros.precision(2);
927 RGBColor cadd = rgbFromHexName(lcolor.getX11Name(Color_addedtext));
928 macros << "\\providecolor{lyxadded}{rgb}{"
929 << cadd.r / 255.0 << ',' << cadd.g / 255.0 << ',' << cadd.b / 255.0 << "}\n";
931 RGBColor cdel = rgbFromHexName(lcolor.getX11Name(Color_deletedtext));
932 macros << "\\providecolor{lyxdeleted}{rgb}{"
933 << cdel.r / 255.0 << ',' << cdel.g / 255.0 << ',' << cdel.b / 255.0 << "}\n";
935 macros.precision(prec);
937 if (isRequired("hyperref"))
938 macros << changetracking_xcolor_ulem_hyperref_def;
940 macros << changetracking_xcolor_ulem_def;
943 if (mustProvide("ct-none"))
944 macros << changetracking_none_def;
950 string const LaTeXFeatures::getBabelPresettings() const
954 LanguageList::const_iterator it = UsedLanguages_.begin();
955 LanguageList::const_iterator end = UsedLanguages_.end();
956 for (; it != end; ++it)
957 if (!(*it)->babel_presettings().empty())
958 tmp << (*it)->babel_presettings() << '\n';
959 if (!params_.language->babel_presettings().empty())
960 tmp << params_.language->babel_presettings() << '\n';
966 string const LaTeXFeatures::getBabelPostsettings() const
970 LanguageList::const_iterator it = UsedLanguages_.begin();
971 LanguageList::const_iterator end = UsedLanguages_.end();
972 for (; it != end; ++it)
973 if (!(*it)->babel_postsettings().empty())
974 tmp << (*it)->babel_postsettings() << '\n';
975 if (!params_.language->babel_postsettings().empty())
976 tmp << params_.language->babel_postsettings() << '\n';
982 bool LaTeXFeatures::needBabelLangOptions() const
984 if (!lyxrc.language_global_options || params_.language->asBabelOptions())
987 LanguageList::const_iterator it = UsedLanguages_.begin();
988 LanguageList::const_iterator end = UsedLanguages_.end();
989 for (; it != end; ++it)
990 if ((*it)->asBabelOptions())
997 docstring const LaTeXFeatures::getTClassPreamble() const
999 // the text class specific preamble
1000 DocumentClass const & tclass = params_.documentClass();
1001 odocstringstream tcpreamble;
1003 tcpreamble << tclass.preamble();
1005 list<docstring>::const_iterator cit = usedLayouts_.begin();
1006 list<docstring>::const_iterator end = usedLayouts_.end();
1007 for (; cit != end; ++cit)
1008 tcpreamble << tclass[*cit].preamble();
1010 cit = usedInsetLayouts_.begin();
1011 end = usedInsetLayouts_.end();
1012 TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1013 for (; cit != end; ++cit) {
1014 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1015 if (it == ils.end())
1017 tcpreamble << it->second.preamble();
1020 return tcpreamble.str();
1024 docstring const LaTeXFeatures::getTClassHTMLPreamble() const
1026 DocumentClass const & tclass = params_.documentClass();
1027 odocstringstream tcpreamble;
1029 tcpreamble << tclass.htmlpreamble();
1031 list<docstring>::const_iterator cit = usedLayouts_.begin();
1032 list<docstring>::const_iterator end = usedLayouts_.end();
1033 for (; cit != end; ++cit)
1034 tcpreamble << tclass[*cit].htmlpreamble();
1036 cit = usedInsetLayouts_.begin();
1037 end = usedInsetLayouts_.end();
1038 TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1039 for (; cit != end; ++cit) {
1040 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1041 if (it == ils.end())
1043 tcpreamble << it->second.htmlpreamble();
1046 return tcpreamble.str();
1050 docstring const LaTeXFeatures::getTClassHTMLStyles() const {
1051 DocumentClass const & tclass = params_.documentClass();
1052 odocstringstream tcpreamble;
1054 list<docstring>::const_iterator cit = usedLayouts_.begin();
1055 list<docstring>::const_iterator end = usedLayouts_.end();
1056 for (; cit != end; ++cit)
1057 tcpreamble << tclass[*cit].htmlstyle();
1059 cit = usedInsetLayouts_.begin();
1060 end = usedInsetLayouts_.end();
1061 TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1062 for (; cit != end; ++cit) {
1063 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1064 if (it == ils.end())
1066 tcpreamble << it->second.htmlstyle();
1069 return tcpreamble.str();
1074 docstring const getFloatI18nPreamble(docstring const & type, docstring const & name, docstring const & lang)
1076 odocstringstream os;
1077 os << "\\addto\\captions" << lang
1078 << "{\\renewcommand{\\" << type << "name}{" << name << "}}\n";
1084 docstring const LaTeXFeatures::getTClassI18nPreamble(bool use_babel) const
1086 DocumentClass const & tclass = params_.documentClass();
1087 // collect preamble snippets in a set to prevent multiple identical
1088 // commands (would happen if e.g. both theorem and theorem* are used)
1089 set<docstring> snippets;
1090 typedef LanguageList::const_iterator lang_it;
1091 lang_it const lbeg = UsedLanguages_.begin();
1092 lang_it const lend = UsedLanguages_.end();
1093 list<docstring>::const_iterator cit = usedLayouts_.begin();
1094 list<docstring>::const_iterator end = usedLayouts_.end();
1095 for (; cit != end; ++cit) {
1096 // language dependent commands (once per document)
1097 snippets.insert(tclass[*cit].langpreamble(buffer().language()));
1098 // commands for language changing (for multilanguage documents)
1099 if (use_babel && !UsedLanguages_.empty()) {
1100 snippets.insert(tclass[*cit].babelpreamble(buffer().language()));
1101 for (lang_it lit = lbeg; lit != lend; ++lit)
1102 snippets.insert(tclass[*cit].babelpreamble(*lit));
1105 if (use_babel && !UsedLanguages_.empty()) {
1106 FloatList const & floats = params_.documentClass().floats();
1107 UsedFloats::const_iterator fit = usedFloats_.begin();
1108 UsedFloats::const_iterator fend = usedFloats_.end();
1109 for (; fit != fend; ++fit) {
1110 Floating const & fl = floats.getType(fit->first);
1111 docstring const type = from_ascii(fl.floattype());
1112 docstring const flname = from_utf8(fl.name());
1113 docstring name = translateIfPossible(flname,
1114 buffer().language()->code());
1115 snippets.insert(getFloatI18nPreamble(
1117 from_ascii(buffer().language()->babel())));
1118 for (lang_it lit = lbeg; lit != lend; ++lit) {
1119 name = translateIfPossible(flname,
1121 snippets.insert(getFloatI18nPreamble(
1123 from_ascii((*lit)->babel())));
1128 odocstringstream tcpreamble;
1129 set<docstring>::const_iterator const send = snippets.end();
1130 set<docstring>::const_iterator it = snippets.begin();
1131 for (; it != send; ++it)
1133 return tcpreamble.str();
1137 docstring const LaTeXFeatures::getLyXSGMLEntities() const
1139 // Definition of entities used in the document that are LyX related.
1140 odocstringstream entities;
1142 if (mustProvide("lyxarrow")) {
1143 entities << "<!ENTITY lyxarrow \"->\">" << '\n';
1146 return entities.str();
1150 docstring const LaTeXFeatures::getIncludedFiles(string const & fname) const
1152 odocstringstream sgmlpreamble;
1154 docstring const basename(from_utf8(onlyPath(fname)));
1156 FileMap::const_iterator end = IncludedFiles_.end();
1157 for (FileMap::const_iterator fi = IncludedFiles_.begin();
1160 sgmlpreamble << "\n<!ENTITY " << fi->first
1161 << (isSGMLFileName(fi->second) ? " SYSTEM \"" : " \"")
1162 << makeRelPath(from_utf8(fi->second), basename) << "\">";
1164 return sgmlpreamble.str();
1168 void LaTeXFeatures::showStruct() const
1170 lyxerr << "LyX needs the following commands when LaTeXing:"
1171 << "\n***** Packages:" << getPackages()
1172 << "\n***** Macros:" << to_utf8(getMacros())
1173 << "\n***** Textclass stuff:" << to_utf8(getTClassPreamble())
1174 << "\n***** done." << endl;
1178 Buffer const & LaTeXFeatures::buffer() const
1184 void LaTeXFeatures::setBuffer(Buffer const & buffer)
1190 BufferParams const & LaTeXFeatures::bufferParams() const
1196 void LaTeXFeatures::getFloatDefinitions(odocstream & os) const
1198 FloatList const & floats = params_.documentClass().floats();
1200 // Here we will output the code to create the needed float styles.
1201 // We will try to do this as minimal as possible.
1202 // \floatstyle{ruled}
1203 // \newfloat{algorithm}{htbp}{loa}
1204 // \providecommand{\algorithmname}{Algorithm}
1205 // \floatname{algorithm}{\protect\algorithmname}
1206 UsedFloats::const_iterator cit = usedFloats_.begin();
1207 UsedFloats::const_iterator end = usedFloats_.end();
1208 for (; cit != end; ++cit) {
1209 Floating const & fl = floats.getType(cit->first);
1211 // For builtin floats we do nothing.
1212 if (!fl.needsFloatPkg())
1215 // We have to special case "table" and "figure"
1216 if (fl.floattype() == "tabular" || fl.floattype() == "figure") {
1217 // Output code to modify "table" or "figure"
1218 // but only if builtin == false
1219 // and that have to be true at this point in the
1221 docstring const type = from_ascii(fl.floattype());
1222 docstring const placement = from_ascii(fl.placement());
1223 docstring const style = from_ascii(fl.style());
1224 if (!style.empty()) {
1225 os << "\\floatstyle{" << style << "}\n"
1226 << "\\restylefloat{" << type << "}\n";
1228 if (!placement.empty()) {
1229 os << "\\floatplacement{" << type << "}{"
1230 << placement << "}\n";
1233 // The other non builtin floats.
1235 docstring const type = from_ascii(fl.floattype());
1236 docstring const placement = from_ascii(fl.placement());
1237 docstring const ext = from_ascii(fl.ext());
1238 docstring const within = from_ascii(fl.within());
1239 docstring const style = from_ascii(fl.style());
1240 docstring const name = translateIfPossible(
1241 from_utf8(fl.name()),
1242 buffer().language()->code());
1243 os << "\\floatstyle{" << style << "}\n"
1244 << "\\newfloat{" << type << "}{" << placement
1245 << "}{" << ext << '}';
1246 if (!within.empty())
1247 os << '[' << within << ']';
1249 << "\\providecommand{\\" << type << "name}{"
1251 << "\\floatname{" << type << "}{\\protect\\"
1252 << type << "name}\n";
1254 // What missing here is to code to minimalize the code
1255 // output so that the same floatstyle will not be
1256 // used several times, when the same style is still in
1260 os << "\n\\newsubfloat{" << from_ascii(fl.floattype()) << "}\n";