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 "LaTeXFonts.h"
28 #include "LaTeXPackages.h"
32 #include "TextClass.h"
34 #include "insets/InsetLayout.h"
36 #include "support/debug.h"
37 #include "support/docstream.h"
38 #include "support/FileName.h"
39 #include "support/filetools.h"
40 #include "support/gettext.h"
41 #include "support/lstrings.h"
42 #include "support/regex.h"
48 using namespace lyx::support;
53 /////////////////////////////////////////////////////////////////////
57 /////////////////////////////////////////////////////////////////////
59 //\NeedsTeXFormat{LaTeX2e}
60 //\ProvidesPackage{lyx}[1996/01/11 LLE v0.2 (LyX LaTeX Extensions)]
61 //\message{LyX LaTeX Extensions (LLE v0.2) of 11-Jan-1996.}
63 static docstring const lyx_def = from_ascii(
64 "\\providecommand{\\LyX}{L\\kern-.1667em\\lower.25em\\hbox{Y}\\kern-.125emX\\@}");
66 static docstring const lyx_hyperref_def = from_ascii(
67 "\\providecommand{\\LyX}{\\texorpdfstring%\n"
68 " {L\\kern-.1667em\\lower.25em\\hbox{Y}\\kern-.125emX\\@}\n"
71 static docstring const noun_def = from_ascii(
72 "\\newcommand{\\noun}[1]{\\textsc{#1}}");
74 static docstring const lyxarrow_def = from_ascii(
75 "\\DeclareRobustCommand*{\\lyxarrow}{%\n"
77 "{\\leavevmode\\,$\\triangleleft$\\,\\allowbreak}\n"
78 "{\\leavevmode\\,$\\triangleright$\\,\\allowbreak}}");
80 // for quotes without babel. This does not give perfect results, but
81 // anybody serious about non-english quotes should use babel (JMarc).
83 static docstring const quotedblbase_def = from_ascii(
84 "\\ProvideTextCommandDefault{\\quotedblbase}{%\n"
85 " \\raisebox{-1.4ex}[1ex][.5ex]{\\textquotedblright}%\n"
86 " \\penalty10000\\hskip0em\\relax%\n"
89 static docstring const quotesinglbase_def = from_ascii(
90 "\\ProvideTextCommandDefault{\\quotesinglbase}{%\n"
91 " \\raisebox{-1.4ex}[1ex][.5ex]{\\textquoteright}%\n"
92 " \\penalty10000\\hskip0em\\relax%\n"
95 static docstring const guillemotleft_def = from_ascii(
96 "\\ProvideTextCommandDefault{\\guillemotleft}{%\n"
97 " {\\usefont{U}{lasy}{m}{n}\\char'50\\kern-.15em\\char'50}%\n"
98 "\\penalty10000\\hskip0pt\\relax%\n"
101 static docstring const guillemotright_def = from_ascii(
102 "\\ProvideTextCommandDefault{\\guillemotright}{%\n"
103 " \\penalty10000\\hskip0pt%\n"
104 " {\\usefont{U}{lasy}{m}{n}\\char'51\\kern-.15em\\char'51}%\n"
107 static docstring const guilsinglleft_def = from_ascii(
108 "\\ProvideTextCommandDefault{\\guilsinglleft}{%\n"
109 " {\\usefont{U}{lasy}{m}{n}\\char'50}%\n"
110 " \\penalty10000\\hskip0pt\\relax%\n"
113 static docstring const guilsinglright_def = from_ascii(
114 "\\ProvideTextCommandDefault{\\guilsinglright}{%\n"
115 " \\penalty10000\\hskip0pt%\n"
116 " {\\usefont{U}{lasy}{m}{n}\\char'51}%\n"
119 static docstring const paragraphleftindent_def = from_ascii(
120 "\\newenvironment{LyXParagraphLeftIndent}[1]%\n"
122 " \\begin{list}{}{%\n"
123 " \\setlength{\\topsep}{0pt}%\n"
124 " \\addtolength{\\leftmargin}{#1}\n"
125 // ho hum, yet more things commented out with no hint as to why they
126 // weren't just removed
127 // "%% \\addtolength{\\leftmargin}{#1\\textwidth}\n"
128 // "%% \\setlength{\\textwidth}{#2\\textwidth}\n"
129 // "%% \\setlength\\listparindent\\parindent%\n"
130 // "%% \\setlength\\itemindent\\parindent%\n"
131 " \\setlength{\\parsep}{0pt plus 1pt}%\n"
137 static docstring const floatingfootnote_def = from_ascii(
138 "%% Special footnote code from the package 'stblftnt.sty'\n"
139 "%% Author: Robin Fairbairns -- Last revised Dec 13 1996\n"
140 "\\let\\SF@@footnote\\footnote\n"
141 "\\def\\footnote{\\ifx\\protect\\@typeset@protect\n"
142 " \\expandafter\\SF@@footnote\n"
144 " \\expandafter\\SF@gobble@opt\n"
147 "\\expandafter\\def\\csname SF@gobble@opt \\endcsname{\\@ifnextchar[%]\n"
148 " \\SF@gobble@twobracket\n"
151 "\\edef\\SF@gobble@opt{\\noexpand\\protect\n"
152 " \\expandafter\\noexpand\\csname SF@gobble@opt \\endcsname}\n"
153 "\\def\\SF@gobble@twobracket[#1]#2{}\n");
155 static docstring const binom_def = from_ascii(
156 "%% Binom macro for standard LaTeX users\n"
157 "\\newcommand{\\binom}[2]{{#1 \\choose #2}}\n");
159 static docstring const mathcircumflex_def = from_ascii(
160 "%% For printing a cirumflex inside a formula\n"
161 "\\newcommand{\\mathcircumflex}[0]{\\mbox{\\^{}}}\n");
163 static docstring const tabularnewline_def = from_ascii(
164 "%% Because html converters don't know tabularnewline\n"
165 "\\providecommand{\\tabularnewline}{\\\\}\n");
167 static docstring const lyxgreyedout_def = from_ascii(
168 "%% The greyedout annotation environment\n"
169 "\\newenvironment{lyxgreyedout}\n"
170 " {\\textcolor{note_fontcolor}\\bgroup\\ignorespaces}\n"
171 " {\\ignorespacesafterend\\egroup}\n");
173 // We want to omit the file extension for includegraphics, but this does not
174 // work when the filename contains other dots.
175 // Idea from http://www.tex.ac.uk/cgi-bin/texfaq2html?label=unkgrfextn
176 static docstring const lyxdot_def = from_ascii(
177 "%% A simple dot to overcome graphicx limitations\n"
178 "\\newcommand{\\lyxdot}{.}\n");
180 static docstring const changetracking_dvipost_def = from_ascii(
181 "%% Change tracking with dvipost\n"
183 "\\dvipost{osstart color push Red}\n"
184 "\\dvipost{osend color pop}\n"
185 "\\dvipost{cbstart color push Blue}\n"
186 "\\dvipost{cbend color pop}\n"
187 "\\DeclareRobustCommand{\\lyxadded}[3]{\\changestart#3\\changeend}\n"
188 "\\DeclareRobustCommand{\\lyxdeleted}[3]{%\n"
189 "\\changestart\\overstrikeon#3\\overstrikeoff\\changeend}\n");
191 static docstring const changetracking_xcolor_ulem_def = from_ascii(
192 "%% Change tracking with ulem\n"
193 "\\DeclareRobustCommand{\\lyxadded}[3]{{\\color{lyxadded}{}#3}}\n"
194 "\\DeclareRobustCommand{\\lyxdeleted}[3]{{\\color{lyxdeleted}\\sout{#3}}}\n");
196 static docstring const changetracking_xcolor_ulem_hyperref_def = from_ascii(
197 "%% Change tracking with ulem\n"
198 "\\DeclareRobustCommand{\\lyxadded}[3]{{\\texorpdfstring{\\color{lyxadded}{}}{}#3}}\n"
199 "\\DeclareRobustCommand{\\lyxdeleted}[3]{{\\texorpdfstring{\\color{lyxdeleted}\\sout{#3}}{}}}\n");
201 static docstring const changetracking_none_def = from_ascii(
202 "\\newcommand{\\lyxadded}[3]{#3}\n"
203 "\\newcommand{\\lyxdeleted}[3]{}\n");
205 static docstring const textgreek_LGR_def = from_ascii(
206 "\\DeclareFontEncoding{LGR}{}{}\n");
207 static docstring const textgreek_def = from_ascii(
208 "\\DeclareRobustCommand{\\greektext}{%\n"
209 " \\fontencoding{LGR}\\selectfont\\def\\encodingdefault{LGR}}\n"
210 "\\DeclareRobustCommand{\\textgreek}[1]{\\leavevmode{\\greektext #1}}\n"
211 "\\ProvideTextCommand{\\~}{LGR}[1]{\\char126#1}\n");
213 static docstring const textcyr_T2A_def = from_ascii(
214 "\\InputIfFileExists{t2aenc.def}{}{%\n"
215 " \\errmessage{File `t2aenc.def' not found: Cyrillic script not supported}}\n");
216 static docstring const textcyr_def = from_ascii(
217 "\\DeclareRobustCommand{\\cyrtext}{%\n"
218 " \\fontencoding{T2A}\\selectfont\\def\\encodingdefault{T2A}}\n"
219 "\\DeclareRobustCommand{\\textcyr}[1]{\\leavevmode{\\cyrtext #1}}\n");
221 static docstring const lyxmathsym_def = from_ascii(
222 "\\newcommand{\\lyxmathsym}[1]{\\ifmmode\\begingroup\\def\\b@ld{bold}\n"
223 " \\text{\\ifx\\math@version\\b@ld\\bfseries\\fi#1}\\endgroup\\else#1\\fi}\n");
225 static docstring const papersizedvi_def = from_ascii(
226 "\\special{papersize=\\the\\paperwidth,\\the\\paperheight}\n");
228 static docstring const papersizepdf_def = from_ascii(
229 "\\pdfpageheight\\paperheight\n"
230 "\\pdfpagewidth\\paperwidth\n");
232 static docstring const papersizepdflua_def = from_ascii(
233 "% Backwards compatibility for LuaTeX < 0.90\n"
234 "\\@ifundefined{pageheight}{\\let\\pageheight\\pdfpageheight}{}\n"
235 "\\@ifundefined{pagewidth}{\\let\\pagewidth\\pdfpagewidth}{}\n"
236 "\\pageheight\\paperheight\n"
237 "\\pagewidth\\paperwidth\n");
239 static docstring const cedilla_def = from_ascii(
240 "\\newcommand{\\docedilla}[2]{\\underaccent{#1\\mathchar'30}{#2}}\n"
241 "\\newcommand{\\cedilla}[1]{\\mathpalette\\docedilla{#1}}\n");
243 static docstring const subring_def = from_ascii(
244 "\\newcommand{\\dosubring}[2]{\\underaccent{#1\\mathchar'27}{#2}}\n"
245 "\\newcommand{\\subring}[1]{\\mathpalette\\dosubring{#1}}\n");
247 static docstring const subdot_def = from_ascii(
248 "\\newcommand{\\dosubdot}[2]{\\underaccent{#1.}{#2}}\n"
249 "\\newcommand{\\subdot}[1]{\\mathpalette\\dosubdot{#1}}\n");
251 static docstring const subhat_def = from_ascii(
252 "\\newcommand{\\dosubhat}[2]{\\underaccent{#1\\mathchar'136}{#2}}\n"
253 "\\newcommand{\\subhat}[1]{\\mathpalette\\dosubhat{#1}}\n");
255 static docstring const subtilde_def = from_ascii(
256 "\\newcommand{\\dosubtilde}[2]{\\underaccent{#1\\mathchar'176}{#2}}\n"
257 "\\newcommand{\\subtilde}[1]{\\mathpalette\\dosubtilde{#1}}\n");
259 static docstring const dacute_def = from_ascii(
260 "\\DeclareMathAccent{\\dacute}{\\mathalpha}{operators}{'175}\n");
262 static docstring const tipasymb_def = from_ascii(
263 "\\DeclareFontEncoding{T3}{}{}\n"
264 "\\DeclareSymbolFont{tipasymb}{T3}{cmr}{m}{n}\n");
266 static docstring const dgrave_def = from_ascii(
267 "\\DeclareMathAccent{\\dgrave}{\\mathord}{tipasymb}{'15}\n");
269 static docstring const rcap_def = from_ascii(
270 "\\DeclareMathAccent{\\rcap}{\\mathord}{tipasymb}{'20}\n");
272 static docstring const ogonek_def = from_ascii(
273 "\\newcommand{\\doogonek}[2]{\\setbox0=\\hbox{$#1#2$}\\underaccent{#1\\mkern-6mu\n"
274 " \\ifx#2O\\hskip0.5\\wd0\\else\\ifx#2U\\hskip0.5\\wd0\\else\\hskip\\wd0\\fi\\fi\n"
275 " \\ifx#2o\\mkern-2mu\\else\\ifx#2e\\mkern-1mu\\fi\\fi\n"
276 " \\mathchar\"0\\hexnumber@\\symtipasymb0C}{#2}}\n"
277 "\\newcommand{\\ogonek}[1]{\\mathpalette\\doogonek{#1}}\n");
279 static docstring const lyxaccent_def = from_ascii(
280 "%% custom text accent \\LyxTextAccent[<rise value (length)>]{<accent>}{<base>}\n"
281 "\\newcommand*{\\LyxTextAccent}[3][0ex]{%\n"
282 " \\hmode@bgroup\\ooalign{\\null#3\\crcr\\hidewidth\n"
283 " \\raise#1\\hbox{#2}\\hidewidth}\\egroup}\n"
284 "%% select a font size smaller than the current font size:\n"
285 "\\newcommand{\\LyxAccentSize}[1][\\sf@size]{%\n"
286 " \\check@mathfonts\\fontsize#1\\z@\\math@fontsfalse\\selectfont\n"
289 static docstring const textcommabelow_def = from_ascii(
290 "\\ProvideTextCommandDefault{\\textcommabelow}[1]{%%\n"
291 " \\LyxTextAccent[-.31ex]{\\LyxAccentSize,}{#1}}\n");
293 static docstring const textcommaabove_def = from_ascii(
294 "\\ProvideTextCommandDefault{\\textcommaabove}[1]{%%\n"
295 " \\LyxTextAccent[.5ex]{\\LyxAccentSize`}{#1}}\n");
297 static docstring const textcommaaboveright_def = from_ascii(
298 "\\ProvideTextCommandDefault{\\textcommaaboveright}[1]{%%\n"
299 " \\LyxTextAccent[.5ex]{\\LyxAccentSize\\ '}{#1}}\n");
301 // Baltic languages use a comma-accent instead of a cedilla
302 static docstring const textbaltic_def = from_ascii(
303 "%% use comma accent instead of cedilla for these characters:\n"
304 "\\DeclareTextCompositeCommand{\\c}{T1}{g}{\\textcommaabove{g}}\n"
305 "\\DeclareTextCompositeCommand{\\c}{T1}{G}{\\textcommabelow{G}}\n"
306 "\\DeclareTextCompositeCommand{\\c}{T1}{k}{\\textcommabelow{k}}\n"
307 "\\DeclareTextCompositeCommand{\\c}{T1}{K}{\\textcommabelow{K}}\n"
308 "\\DeclareTextCompositeCommand{\\c}{T1}{l}{\\textcommabelow{l}}\n"
309 "\\DeclareTextCompositeCommand{\\c}{T1}{L}{\\textcommabelow{L}}\n"
310 "\\DeclareTextCompositeCommand{\\c}{T1}{n}{\\textcommabelow{n}}\n"
311 "\\DeclareTextCompositeCommand{\\c}{T1}{N}{\\textcommabelow{N}}\n"
312 "\\DeclareTextCompositeCommand{\\c}{T1}{r}{\\textcommabelow{r}}\n"
313 "\\DeclareTextCompositeCommand{\\c}{T1}{R}{\\textcommabelow{R}}\n");
315 static docstring const lyxref_def = from_ascii(
316 "\\RS@ifundefined{subsecref}\n"
317 " {\\newref{subsec}{name = \\RSsectxt}}\n"
319 "\\RS@ifundefined{thmref}\n"
320 " {\\def\\RSthmtxt{theorem~}\\newref{thm}{name = \\RSthmtxt}}\n"
322 "\\RS@ifundefined{lemref}\n"
323 " {\\def\\RSlemtxt{lemma~}\\newref{lem}{name = \\RSlemtxt}}\n"
326 // Make sure the columns are also outputed as rtl
327 static docstring const rtloutputdblcol_def = from_ascii(
328 "\\def\\@outputdblcol{%\n"
329 " \\if@firstcolumn\n"
330 " \\global \\@firstcolumnfalse\n"
331 " \\global \\setbox\\@leftcolumn \\box\\@outputbox\n"
333 " \\global \\@firstcolumntrue\n"
334 " \\setbox\\@outputbox \\vbox {%\n"
335 " \\hb@xt@\\textwidth {%\n"
336 " \\kern\\textwidth \\kern-\\columnwidth %**\n"
337 " \\hb@xt@\\columnwidth {%\n"
338 " \\box\\@leftcolumn \\hss}%\n"
339 " \\kern-\\textwidth %**\n"
341 " {\\normalcolor\\vrule \\@width\\columnseprule}%\n"
343 " \\kern-\\textwidth %**\n"
344 " \\hb@xt@\\columnwidth {%\n"
345 " \\box\\@outputbox \\hss}%\n"
346 " \\kern-\\columnwidth \\kern\\textwidth %**\n"
349 " \\@combinedblfloats\n"
352 " \\@dblfloatplacement\n"
353 " \\@startdblcolumn\n"
354 " \\@whilesw\\if@fcolmade \\fi\n"
356 " \\@startdblcolumn}%\n"
360 "\\@mparswitchtrue\n");
363 /////////////////////////////////////////////////////////////////////
367 /////////////////////////////////////////////////////////////////////
369 static docstring const lyxnoun_style = from_ascii(
371 " font-variant: small-caps;\n"
375 // this is how it normally renders, but it might not always do so.
376 static docstring const lyxstrikeout_style = from_ascii(
378 " text-decoration: line-through;\n"
382 /////////////////////////////////////////////////////////////////////
386 /////////////////////////////////////////////////////////////////////
389 LaTeXFeatures::LaTeXFeatures(Buffer const & b, BufferParams const & p,
390 OutputParams const & r)
391 : buffer_(&b), params_(p), runparams_(r), in_float_(false)
395 LaTeXFeatures::LangPackage LaTeXFeatures::langPackage() const
397 string const local_lp = bufferParams().lang_package;
399 // Locally, custom is just stored as a string
400 // in bufferParams().lang_package.
401 if (local_lp != "auto"
402 && local_lp != "babel"
403 && local_lp != "default"
404 && local_lp != "none")
405 return LANG_PACK_CUSTOM;
407 if (local_lp == "none")
408 return LANG_PACK_NONE;
410 /* If "auto" is selected, we load polyglossia with non-TeX fonts,
411 * else we select babel.
412 * If babel is selected (either directly or via the "auto"
413 * mechanism), we really do only require it if we have
414 * a language that needs it.
416 bool const polyglossia_required =
417 params_.useNonTeXFonts
418 && isAvailable("polyglossia")
419 && !isProvided("babel")
420 && this->hasOnlyPolyglossiaLanguages();
421 bool const babel_required =
422 !bufferParams().language->babel().empty()
423 || !this->getBabelLanguages().empty();
425 if (local_lp == "auto") {
426 // polyglossia requirement has priority over babel
427 if (polyglossia_required)
428 return LANG_PACK_POLYGLOSSIA;
429 else if (babel_required)
430 return LANG_PACK_BABEL;
433 if (local_lp == "babel") {
435 return LANG_PACK_BABEL;
438 if (local_lp == "default") {
439 switch (lyxrc.language_package_selection) {
441 // polyglossia requirement has priority over babel
442 if (polyglossia_required)
443 return LANG_PACK_POLYGLOSSIA;
444 else if (babel_required)
445 return LANG_PACK_BABEL;
447 case LyXRC::LP_BABEL:
449 return LANG_PACK_BABEL;
451 case LyXRC::LP_CUSTOM:
452 return LANG_PACK_CUSTOM;
454 return LANG_PACK_NONE;
458 return LANG_PACK_NONE;
462 void LaTeXFeatures::require(string const & name)
464 features_.insert(name);
468 void LaTeXFeatures::require(set<string> const & names)
470 features_.insert(names.begin(), names.end());
474 void LaTeXFeatures::useLayout(docstring const & layoutname)
476 useLayout(layoutname, 0);
480 void LaTeXFeatures::useLayout(docstring const & layoutname, int level)
482 // Some code to avoid loops in dependency definition
483 const int maxlevel = 30;
484 if (level > maxlevel) {
485 lyxerr << "LaTeXFeatures::useLayout: maximum level of "
486 << "recursion attained by layout "
487 << to_utf8(layoutname) << endl;
491 DocumentClass const & tclass = params_.documentClass();
492 if (tclass.hasLayout(layoutname)) {
493 // Is this layout already in usedLayouts?
494 if (find(usedLayouts_.begin(), usedLayouts_.end(), layoutname)
495 != usedLayouts_.end())
498 Layout const & layout = tclass[layoutname];
499 require(layout.requires());
501 if (!layout.depends_on().empty()) {
502 useLayout(layout.depends_on(), level + 1);
504 usedLayouts_.push_back(layoutname);
506 lyxerr << "LaTeXFeatures::useLayout: layout `"
507 << to_utf8(layoutname) << "' does not exist in this class"
513 void LaTeXFeatures::useInsetLayout(InsetLayout const & lay)
515 docstring const & lname = lay.name();
516 DocumentClass const & tclass = params_.documentClass();
518 // this is a default inset layout, nothing useful here
519 if (!tclass.hasInsetLayout(lname))
521 // Is this layout already in usedInsetLayouts?
522 if (find(usedInsetLayouts_.begin(), usedInsetLayouts_.end(), lname)
523 != usedInsetLayouts_.end())
526 require(lay.requires());
527 usedInsetLayouts_.push_back(lname);
531 bool LaTeXFeatures::isRequired(string const & name) const
533 return features_.find(name) != features_.end();
537 bool LaTeXFeatures::isProvided(string const & name) const
539 if (params_.useNonTeXFonts)
540 return params_.documentClass().provides(name);
542 bool const ot1 = (params_.font_encoding() == "default"
543 || params_.font_encoding() == "OT1");
544 bool const complete = (params_.fontsSans() == "default"
545 && params_.fontsTypewriter() == "default");
546 bool const nomath = (params_.fontsMath() == "default");
547 return params_.documentClass().provides(name)
548 || theLaTeXFonts().getLaTeXFont(
549 from_ascii(params_.fontsRoman())).provides(name, ot1,
552 || theLaTeXFonts().getLaTeXFont(
553 from_ascii(params_.fontsSans())).provides(name, ot1,
556 || theLaTeXFonts().getLaTeXFont(
557 from_ascii(params_.fontsTypewriter())).provides(name, ot1,
560 || theLaTeXFonts().getLaTeXFont(
561 from_ascii(params_.fontsMath())).provides(name, ot1,
564 // TODO: "textbaltic" provided, if the font-encoding is "L7x"
565 // "textgreek" provided, if a language with font-encoding LGR is used in the document
566 // "textcyr" provided, if a language with font-encoding T2A is used in the document
570 bool LaTeXFeatures::mustProvide(string const & name) const
572 return isRequired(name) && !isProvided(name);
576 bool LaTeXFeatures::isAvailable(string const & name)
578 string::size_type const i = name.find("->");
579 if (i != string::npos) {
580 string const from = name.substr(0,i);
581 string const to = name.substr(i+2);
582 //LYXERR0("from=[" << from << "] to=[" << to << "]");
583 return theConverters().isReachable(from, to);
585 return LaTeXPackages::isAvailable(name);
589 void LaTeXFeatures::addPreambleSnippet(docstring const & preamble,
592 SnippetList::const_iterator begin = preamble_snippets_.begin();
593 SnippetList::const_iterator end = preamble_snippets_.end();
594 if (allowdupes || find(begin, end, preamble) == end)
595 preamble_snippets_.push_back(preamble);
599 void LaTeXFeatures::addCSSSnippet(std::string const & snippet)
601 docstring const u_snippet = from_ascii(snippet);
602 SnippetList::const_iterator begin = css_snippets_.begin();
603 SnippetList::const_iterator end = css_snippets_.end();
604 if (find(begin, end, u_snippet) == end)
605 css_snippets_.push_back(u_snippet);
609 void LaTeXFeatures::useFloat(string const & name, bool subfloat)
611 if (!usedFloats_[name])
612 usedFloats_[name] = subfloat;
615 // We only need float.sty if we use non builtin floats, or if we
616 // use the "H" modifier. This includes modified table and
617 // figure floats. (Lgb)
618 Floating const & fl = params_.documentClass().floats().getType(name);
619 if (!fl.floattype().empty() && fl.usesFloatPkg()) {
625 void LaTeXFeatures::useLanguage(Language const * lang)
627 if (!lang->babel().empty() || !lang->polyglossia().empty())
628 UsedLanguages_.insert(lang);
629 if (!lang->requires().empty())
630 require(lang->requires());
631 // CJK languages do not have a babel name.
632 // They use the CJK package
633 if (lang->encoding()->package() == Encoding::CJK)
635 // japanese package is special
636 if (lang->encoding()->package() == Encoding::japanese)
641 void LaTeXFeatures::includeFile(docstring const & key, string const & name)
643 IncludedFiles_[key] = name;
647 bool LaTeXFeatures::hasLanguages() const
649 return !UsedLanguages_.empty();
653 bool LaTeXFeatures::hasOnlyPolyglossiaLanguages() const
655 // first the main language
656 if (params_.language->polyglossia().empty())
658 // now the secondary languages
659 LanguageList::const_iterator const begin = UsedLanguages_.begin();
660 for (LanguageList::const_iterator cit = begin;
661 cit != UsedLanguages_.end();
663 if ((*cit)->polyglossia().empty())
670 bool LaTeXFeatures::hasPolyglossiaExclusiveLanguages() const
672 // first the main language
673 if (params_.language->isPolyglossiaExclusive())
675 // now the secondary languages
676 LanguageList::const_iterator const begin = UsedLanguages_.begin();
677 for (LanguageList::const_iterator cit = begin;
678 cit != UsedLanguages_.end();
680 if ((*cit)->isPolyglossiaExclusive())
687 string LaTeXFeatures::getBabelLanguages() const
689 ostringstream languages;
692 LanguageList::const_iterator const begin = UsedLanguages_.begin();
693 for (LanguageList::const_iterator cit = begin;
694 cit != UsedLanguages_.end();
696 if ((*cit)->babel().empty())
702 languages << (*cit)->babel();
704 return languages.str();
708 std::map<std::string, std::string> LaTeXFeatures::getPolyglossiaLanguages() const
710 std::map<std::string, std::string> languages;
712 LanguageList::const_iterator const begin = UsedLanguages_.begin();
713 for (LanguageList::const_iterator cit = begin;
714 cit != UsedLanguages_.end();
716 languages[(*cit)->polyglossia()] = (*cit)->polyglossiaOpts();
722 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding) const
724 // This does only find encodings of languages supported by babel, but
725 // that does not matter since we don't have a language with an
726 // encoding supported by inputenc but without babel support.
727 set<string> encodings;
728 LanguageList::const_iterator it = UsedLanguages_.begin();
729 LanguageList::const_iterator end = UsedLanguages_.end();
730 for (; it != end; ++it)
731 if ((*it)->encoding()->latexName() != doc_encoding &&
732 ((*it)->encoding()->package() == Encoding::inputenc
733 || (*it)->encoding()->package() == Encoding::japanese))
734 encodings.insert((*it)->encoding()->latexName());
739 void LaTeXFeatures::getFontEncodings(vector<string> & encodings) const
741 // these must be loaded if glyphs of this script are used
742 // unless a language providing them is used in the document
743 // FIXME: currently the option is written twice in this case
744 if (mustProvide("textgreek"))
745 encodings.insert(encodings.begin(), "LGR");
746 if (mustProvide("textcyr"))
747 encodings.insert(encodings.begin(), "T2A");
749 LanguageList::const_iterator it = UsedLanguages_.begin();
750 LanguageList::const_iterator end = UsedLanguages_.end();
751 for (; it != end; ++it)
752 if (!(*it)->fontenc().empty()
753 && ascii_lowercase((*it)->fontenc()) != "none") {
754 vector<string> extraencs = getVectorFromString((*it)->fontenc());
755 vector<string>::const_iterator fit = extraencs.begin();
756 for (; fit != extraencs.end(); ++fit) {
757 if (find(encodings.begin(), encodings.end(), *fit) == encodings.end())
758 encodings.insert(encodings.begin(), *fit);
765 char const * simplefeatures[] = {
766 // note that the package order here will be the same in the LaTeX-output
773 // subfig is handled in BufferParams.cpp
777 /*For a successful cooperation of the `wrapfig' package with the
778 `float' package you should load the `wrapfig' package *after*
779 the `float' package. See the caption package documentation
810 // listings is handled in BufferParams.cpp
831 char const * bibliofeatures[] = {
832 // Known bibliography packages (will be loaded before natbib)
847 int const nb_bibliofeatures = sizeof(bibliofeatures) / sizeof(char const *);
849 int const nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
854 string const LaTeXFeatures::getColorOptions() const
856 ostringstream colors;
858 // Handling the color packages separately is needed to be able to load them
859 // before babel when hyperref is loaded with the colorlinks option
860 // for more info see Bufferparams.cpp
863 if (mustProvide("color") || mustProvide("xcolor")) {
864 string const package =
865 (mustProvide("xcolor") ? "xcolor" : "color");
866 if (params_.graphics_driver == "default"
867 || params_.graphics_driver == "none")
868 colors << "\\usepackage{" << package << "}\n";
870 colors << "\\usepackage["
871 << params_.graphics_driver
872 << "]{" << package << "}\n";
875 // pdfcolmk must be loaded after color
876 if (mustProvide("pdfcolmk"))
877 colors << "\\usepackage{pdfcolmk}\n";
879 // the following 3 color commands must be set after color
880 // is loaded and before pdfpages, therefore add the command
881 // here define the set color
882 if (mustProvide("pagecolor")) {
883 colors << "\\definecolor{page_backgroundcolor}{rgb}{";
884 colors << outputLaTeXColor(params_.backgroundcolor) << "}\n";
885 // set the page color
886 colors << "\\pagecolor{page_backgroundcolor}\n";
889 if (mustProvide("fontcolor")) {
890 colors << "\\definecolor{document_fontcolor}{rgb}{";
891 colors << outputLaTeXColor(params_.fontcolor) << "}\n";
893 colors << "\\color{document_fontcolor}\n";
896 if (mustProvide("lyxgreyedout")) {
897 colors << "\\definecolor{note_fontcolor}{rgb}{";
898 colors << outputLaTeXColor(params_.notefontcolor) << "}\n";
899 // the color will be set together with the definition of
900 // the lyxgreyedout environment (see lyxgreyedout_def)
903 // color for shaded boxes
904 if (isRequired("framed") && mustProvide("color")) {
905 colors << "\\definecolor{shadecolor}{rgb}{";
906 colors << outputLaTeXColor(params_.boxbgcolor) << "}\n";
907 // this color is automatically used by the LaTeX-package "framed"
914 string const LaTeXFeatures::getPackageOptions() const
916 ostringstream packageopts;
917 // Output all the package option stuff we have been asked to do.
918 map<string, string>::const_iterator it =
919 params_.documentClass().packageOptions().begin();
920 map<string, string>::const_iterator en =
921 params_.documentClass().packageOptions().end();
922 for (; it != en; ++it)
923 if (mustProvide(it->first))
924 packageopts << "\\PassOptionsToPackage{" << it->second << "}"
925 << "{" << it->first << "}\n";
926 return packageopts.str();
930 string const LaTeXFeatures::getPackages() const
932 ostringstream packages;
934 // FIXME: currently, we can only load packages and macros known
936 // However, with the Require tag of layouts/custom insets,
937 // also unknown packages can be requested. They are silently
938 // swallowed now. We should change this eventually.
940 // These are all the 'simple' includes. i.e
941 // packages which we just \usepackage{package}
942 for (int i = 0; i < nb_simplefeatures; ++i) {
943 if (mustProvide(simplefeatures[i]))
944 packages << "\\usepackage{" << simplefeatures[i] << "}\n";
947 // The rest of these packages are somewhat more complicated
950 // The tipa package and its extensions (tipx, tone) must not
951 // be loaded with non-TeX fonts, since fontspec includes the
953 if (mustProvide("tipa") && !params_.useNonTeXFonts)
954 packages << "\\usepackage{tipa}\n";
955 if (mustProvide("tipx") && !params_.useNonTeXFonts)
956 packages << "\\usepackage{tipx}\n";
957 if (mustProvide("extraipa") && !params_.useNonTeXFonts)
958 packages << "\\usepackage{extraipa}\n";
959 if (mustProvide("tone") && !params_.useNonTeXFonts)
960 packages << "\\usepackage{tone}\n";
962 // if fontspec or newtxmath is used, AMS packages have to be loaded
963 // before fontspec (in BufferParams)
964 string const amsPackages = loadAMSPackages();
965 bool const ot1 = (params_.font_encoding() == "default" || params_.font_encoding() == "OT1");
966 bool const use_newtxmath =
967 theLaTeXFonts().getLaTeXFont(from_ascii(params_.fontsMath())).getUsedPackage(
968 ot1, false, false) == "newtxmath";
970 if (!params_.useNonTeXFonts && !use_newtxmath && !amsPackages.empty())
971 packages << amsPackages;
973 if (mustProvide("cancel") &&
974 params_.use_package("cancel") != BufferParams::package_off)
975 packages << "\\usepackage{cancel}\n";
977 // marvosym and bbding both define the \Cross macro
978 if (mustProvide("marvosym")) {
979 if (mustProvide("bbding"))
980 packages << "\\let\\Cross\\relax\n";
981 packages << "\\usepackage{marvosym}\n";
984 // accents must be loaded after amsmath
985 if (mustProvide("accents") &&
986 params_.use_package("accents") != BufferParams::package_off)
987 packages << "\\usepackage{accents}\n";
989 // mathdots must be loaded after amsmath
990 if (mustProvide("mathdots") &&
991 params_.use_package("mathdots") != BufferParams::package_off)
992 packages << "\\usepackage{mathdots}\n";
994 // yhmath must be loaded after amsmath
995 if (mustProvide("yhmath") &&
996 params_.use_package("yhmath") != BufferParams::package_off)
997 packages << "\\usepackage{yhmath}\n";
999 // stmaryrd must be loaded after amsmath
1000 if (mustProvide("stmaryrd") &&
1001 params_.use_package("stmaryrd") != BufferParams::package_off)
1002 packages << "\\usepackage{stmaryrd}\n";
1004 if (mustProvide("stackrel") &&
1005 params_.use_package("stackrel") != BufferParams::package_off)
1006 packages << "\\usepackage{stackrel}\n";
1008 if (mustProvide("undertilde") &&
1009 params_.use_package("undertilde") != BufferParams::package_off)
1010 packages << "\\usepackage{undertilde}\n";
1012 // [x]color and pdfcolmk are handled in getColorOptions() above
1015 if (isRequired("makeidx") || isRequired("splitidx")) {
1016 if (!isProvided("makeidx") && !isRequired("splitidx"))
1017 packages << "\\usepackage{makeidx}\n";
1018 if (mustProvide("splitidx"))
1019 packages << "\\usepackage{splitidx}\n";
1020 packages << "\\makeindex\n";
1024 if (mustProvide("graphicx") && params_.graphics_driver != "none") {
1025 if (params_.graphics_driver == "default")
1026 packages << "\\usepackage{graphicx}\n";
1028 packages << "\\usepackage["
1029 << params_.graphics_driver
1033 // lyxskak.sty --- newer chess support based on skak.sty
1034 if (mustProvide("chess"))
1035 packages << "\\usepackage[ps,mover]{lyxskak}\n";
1038 if (mustProvide("setspace") && !isProvided("SetSpace"))
1039 packages << "\\usepackage{setspace}\n";
1041 // we need to assure that mhchem is loaded before esint and every other
1042 // package that redefines command of amsmath because mhchem loads amlatex
1043 // (this info is from the author of mhchem from June 2013)
1044 if (mustProvide("mhchem") &&
1045 params_.use_package("mhchem") != BufferParams::package_off)
1046 packages << "\\PassOptionsToPackage{version=3}{mhchem}\n"
1047 "\\usepackage{mhchem}\n";
1049 // wasysym is a simple feature, but it must be after amsmath if both
1051 // wasysym redefines some integrals (e.g. iint) from amsmath. That
1052 // leads to inconsistent integrals. We only load this package if
1053 // the document does not contain integrals (then isRequired("esint")
1054 // is false) or if esint is used, since esint redefines all relevant
1055 // integral symbols from wasysym and amsmath.
1056 // See http://www.lyx.org/trac/ticket/1942
1057 if (mustProvide("wasysym") &&
1058 params_.use_package("wasysym") != BufferParams::package_off &&
1059 (params_.use_package("esint") != BufferParams::package_off || !isRequired("esint")))
1060 packages << "\\usepackage{wasysym}\n";
1062 // esint must be after amsmath (and packages requiring amsmath, like mhchem)
1063 // and wasysym, since it will redeclare inconsistent integral symbols
1064 if (mustProvide("esint") &&
1065 params_.use_package("esint") != BufferParams::package_off)
1066 packages << "\\usepackage{esint}\n";
1068 // Known bibliography packages (simple \usepackage{package})
1069 for (int i = 0; i < nb_bibliofeatures; ++i) {
1070 if (mustProvide(bibliofeatures[i]))
1071 packages << "\\usepackage{"
1072 << bibliofeatures[i] << "}\n";
1075 // Compatibility between achicago and natbib
1076 if (mustProvide("achicago") && mustProvide("natbib"))
1077 packages << "\\let\\achicagobib\\thebibliography\n";
1080 // Some classes load natbib themselves, but still allow (or even require)
1081 // plain numeric citations (ReVTeX is such a case, see bug 5182).
1082 // This special case is indicated by the "natbib-internal" key.
1083 if (mustProvide("natbib") && !isProvided("natbib-internal")) {
1084 packages << "\\usepackage[";
1085 if (params_.citeEngineType() == ENGINE_TYPE_NUMERICAL)
1086 packages << "numbers";
1088 packages << "authoryear";
1089 packages << "]{natbib}\n";
1092 // Compatibility between achicago and natbib
1093 if (mustProvide("achicago") && mustProvide("natbib")) {
1094 packages << "\\let\\thebibliography\\achicagobib\n";
1095 packages << "\\let\\SCcite\\astroncite\n";
1096 packages << "\\let\\UnexpandableProtect\\protect\n";
1099 // jurabib -- we need version 0.6 at least.
1100 if (mustProvide("jurabib"))
1101 packages << "\\usepackage{jurabib}[2004/01/25]\n";
1103 // opcit -- we pass custombst as we output \bibliographystyle ourselves
1104 if (mustProvide("opcit")) {
1105 if (isRequired("hyperref"))
1106 packages << "\\usepackage[custombst,hyperref]{opcit}\n";
1108 packages << "\\usepackage[custombst]{opcit}\n";
1111 // xargs -- we need version 1.09 at least
1112 if (mustProvide("xargs"))
1113 packages << "\\usepackage{xargs}[2008/03/08]\n";
1115 if (mustProvide("xy"))
1116 packages << "\\usepackage[all]{xy}\n";
1118 if (mustProvide("feyn"))
1119 packages << "\\usepackage{feyn}\n"; //Diagram
1121 if (mustProvide("ulem"))
1122 packages << "\\PassOptionsToPackage{normalem}{ulem}\n"
1123 "\\usepackage{ulem}\n";
1125 if (mustProvide("nomencl")) {
1126 // Make it work with the new and old version of the package,
1127 // but don't use the compatibility option since it is
1128 // incompatible to other packages.
1129 packages << "\\usepackage{nomencl}\n"
1130 "% the following is useful when we have the old nomencl.sty package\n"
1131 "\\providecommand{\\printnomenclature}{\\printglossary}\n"
1132 "\\providecommand{\\makenomenclature}{\\makeglossary}\n"
1133 "\\makenomenclature\n";
1136 // fixltx2e provides subscript
1137 if (mustProvide("subscript") && !isRequired("fixltx2e"))
1138 packages << "\\usepackage{subscript}\n";
1140 // footmisc must be loaded after setspace
1141 // Set options here, load the package after the user preamble to
1142 // avoid problems with manual loaded footmisc.
1143 if (mustProvide("footmisc"))
1144 packages << "\\PassOptionsToPackage{stable}{footmisc}\n";
1146 if (mustProvide("microtype")){
1147 packages << "\\usepackage{microtype}\n";
1150 return packages.str();
1154 docstring LaTeXFeatures::getPreambleSnippets() const
1156 odocstringstream snip;
1157 SnippetList::const_iterator pit = preamble_snippets_.begin();
1158 SnippetList::const_iterator pend = preamble_snippets_.end();
1159 for (; pit != pend; ++pit)
1160 snip << *pit << '\n';
1165 docstring LaTeXFeatures::getCSSSnippets() const
1167 odocstringstream snip;
1168 SnippetList::const_iterator pit = css_snippets_.begin();
1169 SnippetList::const_iterator pend = css_snippets_.end();
1170 for (; pit != pend; ++pit)
1171 snip << *pit << '\n';
1176 docstring const LaTeXFeatures::getMacros() const
1178 odocstringstream macros;
1180 if (!preamble_snippets_.empty()) {
1182 macros << getPreambleSnippets();
1185 if (mustProvide("papersize")) {
1186 if (runparams_.flavor == OutputParams::LATEX
1187 || runparams_.flavor == OutputParams::DVILUATEX)
1188 macros << papersizedvi_def << '\n';
1189 else if (runparams_.flavor == OutputParams::LUATEX)
1190 macros << papersizepdflua_def << '\n';
1192 macros << papersizepdf_def << '\n';
1195 if (mustProvide("LyX")) {
1196 if (isRequired("hyperref"))
1197 macros << lyx_hyperref_def << '\n';
1199 macros << lyx_def << '\n';
1202 if (mustProvide("noun"))
1203 macros << noun_def << '\n';
1205 if (mustProvide("lyxarrow"))
1206 macros << lyxarrow_def << '\n';
1208 if (!usePolyglossia() && mustProvide("textgreek")) {
1209 // ensure LGR font encoding is defined also if fontenc is not loaded by LyX
1210 if (params_.font_encoding() == "default")
1211 macros << textgreek_LGR_def;
1212 macros << textgreek_def << '\n';
1215 if (!usePolyglossia() && mustProvide("textcyr")) {
1216 // ensure T2A font encoding is set up also if fontenc is not loaded by LyX
1217 if (params_.font_encoding() == "default")
1218 macros << textcyr_T2A_def;
1219 macros << textcyr_def << '\n';
1222 // non-standard text accents:
1223 if (mustProvide("textcommaabove") || mustProvide("textcommaaboveright") ||
1224 mustProvide("textcommabelow") || mustProvide("textbaltic"))
1225 macros << lyxaccent_def;
1227 if (mustProvide("textcommabelow") || mustProvide("textbaltic"))
1228 macros << textcommabelow_def << '\n';
1230 if (mustProvide("textcommaabove") || mustProvide("textbaltic"))
1231 macros << textcommaabove_def << '\n';
1233 if (mustProvide("textcommaaboveright"))
1234 macros << textcommaaboveright_def << '\n';
1236 if (mustProvide("textbaltic"))
1237 macros << textbaltic_def << '\n';
1239 if (mustProvide("lyxmathsym"))
1240 macros << lyxmathsym_def << '\n';
1242 if (mustProvide("cedilla"))
1243 macros << cedilla_def << '\n';
1245 if (mustProvide("subring"))
1246 macros << subring_def << '\n';
1248 if (mustProvide("subdot"))
1249 macros << subdot_def << '\n';
1251 if (mustProvide("subhat"))
1252 macros << subhat_def << '\n';
1254 if (mustProvide("subtilde"))
1255 macros << subtilde_def << '\n';
1257 if (mustProvide("dacute"))
1258 macros << dacute_def << '\n';
1260 if (mustProvide("tipasymb"))
1261 macros << tipasymb_def << '\n';
1263 if (mustProvide("dgrave"))
1264 macros << dgrave_def << '\n';
1266 if (mustProvide("rcap"))
1267 macros << rcap_def << '\n';
1269 if (mustProvide("ogonek"))
1270 macros << ogonek_def << '\n';
1273 if (mustProvide("quotesinglbase"))
1274 macros << quotesinglbase_def << '\n';
1275 if (mustProvide("quotedblbase"))
1276 macros << quotedblbase_def << '\n';
1277 if (mustProvide("guilsinglleft"))
1278 macros << guilsinglleft_def << '\n';
1279 if (mustProvide("guilsinglright"))
1280 macros << guilsinglright_def << '\n';
1281 if (mustProvide("guillemotleft"))
1282 macros << guillemotleft_def << '\n';
1283 if (mustProvide("guillemotright"))
1284 macros << guillemotright_def << '\n';
1287 if (mustProvide("binom") && !isRequired("amsmath"))
1288 macros << binom_def << '\n';
1289 if (mustProvide("mathcircumflex"))
1290 macros << mathcircumflex_def << '\n';
1293 if (mustProvide("ParagraphLeftIndent"))
1294 macros << paragraphleftindent_def;
1295 if (mustProvide("NeedLyXFootnoteCode"))
1296 macros << floatingfootnote_def;
1298 // some problems with tex->html converters
1299 if (mustProvide("NeedTabularnewline"))
1300 macros << tabularnewline_def;
1302 // greyed-out environment (note inset)
1303 // the color is specified in the routine
1304 // getColorOptions() to avoid LaTeX-package clashes
1305 if (mustProvide("lyxgreyedout"))
1306 macros << lyxgreyedout_def;
1308 if (mustProvide("lyxdot"))
1309 macros << lyxdot_def << '\n';
1312 getFloatDefinitions(macros);
1314 if (mustProvide("refstyle"))
1315 macros << lyxref_def << '\n';
1318 if (mustProvide("ct-dvipost"))
1319 macros << changetracking_dvipost_def;
1321 if (mustProvide("ct-xcolor-ulem")) {
1322 streamsize const prec = macros.precision(2);
1324 RGBColor cadd = rgbFromHexName(lcolor.getX11Name(Color_addedtext));
1325 macros << "\\providecolor{lyxadded}{rgb}{"
1326 << cadd.r / 255.0 << ',' << cadd.g / 255.0 << ',' << cadd.b / 255.0 << "}\n";
1328 RGBColor cdel = rgbFromHexName(lcolor.getX11Name(Color_deletedtext));
1329 macros << "\\providecolor{lyxdeleted}{rgb}{"
1330 << cdel.r / 255.0 << ',' << cdel.g / 255.0 << ',' << cdel.b / 255.0 << "}\n";
1332 macros.precision(prec);
1334 if (isRequired("hyperref"))
1335 macros << changetracking_xcolor_ulem_hyperref_def;
1337 macros << changetracking_xcolor_ulem_def;
1340 if (mustProvide("ct-none"))
1341 macros << changetracking_none_def;
1343 if (mustProvide("rtloutputdblcol"))
1344 macros << rtloutputdblcol_def;
1346 return macros.str();
1350 docstring const LaTeXFeatures::getBabelPresettings() const
1352 odocstringstream tmp;
1354 for (Language const * lang : UsedLanguages_)
1355 if (!lang->babel_presettings().empty())
1356 tmp << lang->babel_presettings() << '\n';
1357 if (!params_.language->babel_presettings().empty())
1358 tmp << params_.language->babel_presettings() << '\n';
1360 if (!contains(tmp.str(), '@'))
1363 return "\\makeatletter\n" + tmp.str() + "\\makeatother\n";
1367 docstring const LaTeXFeatures::getBabelPostsettings() const
1369 odocstringstream tmp;
1371 for (Language const * lang : UsedLanguages_)
1372 if (!lang->babel_postsettings().empty())
1373 tmp << lang->babel_postsettings() << '\n';
1374 if (!params_.language->babel_postsettings().empty())
1375 tmp << params_.language->babel_postsettings() << '\n';
1377 if (!contains(tmp.str(), '@'))
1380 return "\\makeatletter\n" + tmp.str() + "\\makeatother\n";
1384 bool LaTeXFeatures::needBabelLangOptions() const
1386 if (!lyxrc.language_global_options || params_.language->asBabelOptions())
1389 LanguageList::const_iterator it = UsedLanguages_.begin();
1390 LanguageList::const_iterator end = UsedLanguages_.end();
1391 for (; it != end; ++it)
1392 if ((*it)->asBabelOptions())
1399 string const LaTeXFeatures::loadAMSPackages() const
1403 if (mustProvide("amsmath")
1404 && params_.use_package("amsmath") != BufferParams::package_off) {
1405 tmp << "\\usepackage{amsmath}\n";
1407 // amsbsy and amstext are already provided by amsmath
1408 if (mustProvide("amsbsy"))
1409 tmp << "\\usepackage{amsbsy}\n";
1410 if (mustProvide("amstext"))
1411 tmp << "\\usepackage{amstext}\n";
1414 if (mustProvide("amsthm"))
1415 tmp << "\\usepackage{amsthm}\n";
1417 if (mustProvide("amssymb")
1418 && params_.use_package("amssymb") != BufferParams::package_off)
1419 tmp << "\\usepackage{amssymb}\n";
1425 docstring const LaTeXFeatures::getTClassPreamble() const
1427 // the text class specific preamble
1428 DocumentClass const & tclass = params_.documentClass();
1429 odocstringstream tcpreamble;
1431 tcpreamble << tclass.preamble();
1433 list<docstring>::const_iterator cit = usedLayouts_.begin();
1434 list<docstring>::const_iterator end = usedLayouts_.end();
1435 for (; cit != end; ++cit)
1436 tcpreamble << tclass[*cit].preamble();
1438 cit = usedInsetLayouts_.begin();
1439 end = usedInsetLayouts_.end();
1440 TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1441 for (; cit != end; ++cit) {
1442 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1443 if (it == ils.end())
1445 tcpreamble << it->second.preamble();
1448 return tcpreamble.str();
1452 docstring const LaTeXFeatures::getTClassHTMLPreamble() const
1454 DocumentClass const & tclass = params_.documentClass();
1455 odocstringstream tcpreamble;
1457 tcpreamble << tclass.htmlpreamble();
1459 list<docstring>::const_iterator cit = usedLayouts_.begin();
1460 list<docstring>::const_iterator end = usedLayouts_.end();
1461 for (; cit != end; ++cit)
1462 tcpreamble << tclass[*cit].htmlpreamble();
1464 cit = usedInsetLayouts_.begin();
1465 end = usedInsetLayouts_.end();
1466 TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1467 for (; cit != end; ++cit) {
1468 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1469 if (it == ils.end())
1471 tcpreamble << it->second.htmlpreamble();
1474 return tcpreamble.str();
1478 docstring const LaTeXFeatures::getTClassHTMLStyles() const
1480 DocumentClass const & tclass = params_.documentClass();
1481 odocstringstream tcpreamble;
1483 if (mustProvide("noun"))
1484 tcpreamble << lyxnoun_style;
1485 // this isn't exact, but it won't hurt that much if it
1487 if (mustProvide("ulem"))
1488 tcpreamble << lyxstrikeout_style;
1490 tcpreamble << tclass.htmlstyles();
1492 list<docstring>::const_iterator cit = usedLayouts_.begin();
1493 list<docstring>::const_iterator end = usedLayouts_.end();
1494 for (; cit != end; ++cit)
1495 tcpreamble << tclass[*cit].htmlstyle();
1497 cit = usedInsetLayouts_.begin();
1498 end = usedInsetLayouts_.end();
1499 TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1500 for (; cit != end; ++cit) {
1501 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1502 if (it == ils.end())
1504 tcpreamble << it->second.htmlstyle();
1507 return tcpreamble.str();
1513 docstring const getFloatI18nPreamble(docstring const & type,
1514 docstring const & name, Language const * lang,
1515 Encoding const & enc, bool const polyglossia)
1517 // Check whether name can be encoded in the buffer encoding
1518 bool encodable = true;
1519 for (size_t i = 0; i < name.size(); ++i) {
1520 if (!enc.encodable(name[i])) {
1526 docstring const language = polyglossia ? from_ascii(lang->polyglossia())
1527 : from_ascii(lang->babel());
1528 docstring const langenc = from_ascii(lang->encoding()->iconvName());
1529 docstring const texenc = from_ascii(lang->encoding()->latexName());
1530 docstring const bufenc = from_ascii(enc.iconvName());
1531 docstring const s1 = docstring(1, 0xF0000);
1532 docstring const s2 = docstring(1, 0xF0001);
1533 docstring const translated = encodable ? name
1534 : from_ascii("\\inputencoding{") + texenc + from_ascii("}")
1535 + s1 + langenc + s2 + name + s1 + bufenc + s2;
1537 odocstringstream os;
1538 os << "\\addto\\captions" << language
1539 << "{\\renewcommand{\\" << type << "name}{" << translated << "}}\n";
1544 docstring const i18npreamble(docstring const & templ, Language const * lang,
1545 Encoding const & enc, bool const polyglossia,
1546 bool const need_fixedwidth)
1551 string preamble = polyglossia ?
1552 subst(to_utf8(templ), "$$lang", lang->polyglossia()) :
1553 subst(to_utf8(templ), "$$lang", lang->babel());
1555 string const langenc = lang->encoding()->iconvName();
1556 string const texenc = lang->encoding()->latexName();
1557 string const bufenc = enc.iconvName();
1558 Encoding const * testenc(&enc);
1559 bool lang_fallback = false;
1560 bool ascii_fallback = false;
1561 if (need_fixedwidth && !enc.hasFixedWidth()) {
1562 if (lang->encoding()->hasFixedWidth()) {
1563 testenc = lang->encoding();
1564 lang_fallback = true;
1566 // We need a fixed width encoding, but both the buffer
1567 // encoding and the language encoding are variable
1568 // width. As a last fallback, try to convert to pure
1569 // ASCII using the LaTeX commands defined in unicodesymbols.
1570 testenc = encodings.fromLyXName("ascii");
1573 ascii_fallback = true;
1576 // First and second character of plane 15 (Private Use Area)
1577 string const s1 = "\xf3\xb0\x80\x80"; // U+F0000
1578 string const s2 = "\xf3\xb0\x80\x81"; // U+F0001
1580 // lyx::regex is not unicode-safe.
1581 // Should use QRegExp or (boost::u32regex, but that requires ICU)
1582 static regex const reg("_\\(([^\\)]+)\\)");
1584 while (regex_search(preamble, sub, reg)) {
1585 string const key = sub.str(1);
1586 docstring const name = lang->translateLayout(key);
1587 // Check whether name can be encoded in the buffer encoding
1588 bool encodable = true;
1589 for (size_t i = 0; i < name.size() && encodable; ++i)
1590 if (!testenc->encodable(name[i]))
1593 if (encodable && !lang_fallback)
1594 translated = to_utf8(name);
1595 else if (ascii_fallback)
1596 translated = to_ascii(testenc->latexString(name).first);
1598 translated = "\\inputencoding{" + texenc + "}"
1599 + s1 + langenc + s2 + to_utf8(name)
1601 preamble = subst(preamble, sub.str(), translated);
1603 return from_utf8(preamble);
1609 docstring const LaTeXFeatures::getTClassI18nPreamble(bool use_babel, bool use_polyglossia) const
1611 DocumentClass const & tclass = params_.documentClass();
1612 // collect preamble snippets in a set to prevent multiple identical
1613 // commands (would happen if e.g. both theorem and theorem* are used)
1614 set<docstring> snippets;
1615 typedef LanguageList::const_iterator lang_it;
1616 lang_it const lbeg = UsedLanguages_.begin();
1617 lang_it const lend = UsedLanguages_.end();
1618 list<docstring>::const_iterator cit = usedLayouts_.begin();
1619 list<docstring>::const_iterator end = usedLayouts_.end();
1620 for (; cit != end; ++cit) {
1621 // language dependent commands (once per document)
1622 snippets.insert(i18npreamble(tclass[*cit].langpreamble(),
1623 buffer().language(),
1624 buffer().params().encoding(),
1625 use_polyglossia, false));
1626 // commands for language changing (for multilanguage documents)
1627 if ((use_babel || use_polyglossia) && !UsedLanguages_.empty()) {
1628 snippets.insert(i18npreamble(
1629 tclass[*cit].babelpreamble(),
1630 buffer().language(),
1631 buffer().params().encoding(),
1632 use_polyglossia, false));
1633 for (lang_it lit = lbeg; lit != lend; ++lit)
1634 snippets.insert(i18npreamble(
1635 tclass[*cit].babelpreamble(),
1637 buffer().params().encoding(),
1638 use_polyglossia, false));
1641 if ((use_babel || use_polyglossia) && !UsedLanguages_.empty()) {
1642 FloatList const & floats = params_.documentClass().floats();
1643 UsedFloats::const_iterator fit = usedFloats_.begin();
1644 UsedFloats::const_iterator fend = usedFloats_.end();
1645 for (; fit != fend; ++fit) {
1646 Floating const & fl = floats.getType(fit->first);
1647 // we assume builtin floats are translated
1648 if (fl.isPredefined())
1650 docstring const type = from_ascii(fl.floattype());
1651 docstring const flname = from_utf8(fl.name());
1652 docstring name = buffer().language()->translateLayout(fl.name());
1653 // only request translation if we have a real translation
1654 // (that differs from the source)
1656 snippets.insert(getFloatI18nPreamble(
1657 type, name, buffer().language(),
1658 buffer().params().encoding(),
1660 for (lang_it lit = lbeg; lit != lend; ++lit) {
1661 string const code = (*lit)->code();
1662 name = (*lit)->translateLayout(fl.name());
1663 // we assume we have a suitable translation if
1664 // either the language is English (we need to
1665 // translate into English if English is a secondary
1666 // language) or if translateIfPossible returns
1667 // something different to the English source.
1668 bool const have_translation =
1669 (flname != name || contains(code, "en"));
1670 if (have_translation)
1671 snippets.insert(getFloatI18nPreamble(
1673 buffer().params().encoding(),
1679 cit = usedInsetLayouts_.begin();
1680 end = usedInsetLayouts_.end();
1681 TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1682 for (; cit != end; ++cit) {
1683 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1684 if (it == ils.end())
1686 // The listings package does not work with variable width
1687 // encodings, only with fixed width encodings. Therefore we
1688 // need to force a fixed width encoding for
1689 // \lstlistlistingname and \lstlistingname (bug 9382).
1690 // This needs to be consistent with InsetListings::latex().
1691 bool const need_fixedwidth = !runparams_.isFullUnicode() &&
1692 it->second.fixedwidthpreambleencoding();
1693 // language dependent commands (once per document)
1694 snippets.insert(i18npreamble(it->second.langpreamble(),
1695 buffer().language(),
1696 buffer().params().encoding(),
1697 use_polyglossia, need_fixedwidth));
1698 // commands for language changing (for multilanguage documents)
1699 if ((use_babel || use_polyglossia) && !UsedLanguages_.empty()) {
1700 snippets.insert(i18npreamble(
1701 it->second.babelpreamble(),
1702 buffer().language(),
1703 buffer().params().encoding(),
1704 use_polyglossia, need_fixedwidth));
1705 for (lang_it lit = lbeg; lit != lend; ++lit)
1706 snippets.insert(i18npreamble(
1707 it->second.babelpreamble(),
1709 buffer().params().encoding(),
1710 use_polyglossia, need_fixedwidth));
1714 odocstringstream tcpreamble;
1715 set<docstring>::const_iterator const send = snippets.end();
1716 set<docstring>::const_iterator it = snippets.begin();
1717 for (; it != send; ++it)
1719 return tcpreamble.str();
1723 docstring const LaTeXFeatures::getLyXSGMLEntities() const
1725 // Definition of entities used in the document that are LyX related.
1726 odocstringstream entities;
1728 if (mustProvide("lyxarrow")) {
1729 entities << "<!ENTITY lyxarrow \"->\">" << '\n';
1732 return entities.str();
1736 docstring const LaTeXFeatures::getIncludedFiles(string const & fname) const
1738 odocstringstream sgmlpreamble;
1740 docstring const basename(from_utf8(onlyPath(fname)));
1742 FileMap::const_iterator end = IncludedFiles_.end();
1743 for (FileMap::const_iterator fi = IncludedFiles_.begin();
1746 sgmlpreamble << "\n<!ENTITY " << fi->first
1747 << (isSGMLFileName(fi->second) ? " SYSTEM \"" : " \"")
1748 << makeRelPath(from_utf8(fi->second), basename) << "\">";
1750 return sgmlpreamble.str();
1754 void LaTeXFeatures::showStruct() const
1756 lyxerr << "LyX needs the following commands when LaTeXing:"
1757 << "\n***** Packages:" << getPackages()
1758 << "\n***** Macros:" << to_utf8(getMacros())
1759 << "\n***** Textclass stuff:" << to_utf8(getTClassPreamble())
1760 << "\n***** done." << endl;
1764 Buffer const & LaTeXFeatures::buffer() const
1770 void LaTeXFeatures::setBuffer(Buffer const & buffer)
1776 BufferParams const & LaTeXFeatures::bufferParams() const
1782 void LaTeXFeatures::getFloatDefinitions(odocstream & os) const
1784 FloatList const & floats = params_.documentClass().floats();
1786 // Here we will output the code to create the needed float styles.
1787 // We will try to do this as minimal as possible.
1788 // \floatstyle{ruled}
1789 // \newfloat{algorithm}{htbp}{loa}
1790 // \providecommand{\algorithmname}{Algorithm}
1791 // \floatname{algorithm}{\protect\algorithmname}
1792 UsedFloats::const_iterator cit = usedFloats_.begin();
1793 UsedFloats::const_iterator end = usedFloats_.end();
1794 for (; cit != end; ++cit) {
1795 Floating const & fl = floats.getType(cit->first);
1797 // For builtin floats we do nothing.
1798 if (fl.isPredefined())
1801 // We have to special case "table" and "figure"
1802 if (fl.floattype() == "tabular" || fl.floattype() == "figure") {
1803 // Output code to modify "table" or "figure"
1804 // but only if builtin == false
1805 // and that have to be true at this point in the
1807 docstring const type = from_ascii(fl.floattype());
1808 docstring const placement = from_ascii(fl.placement());
1809 docstring const style = from_ascii(fl.style());
1810 if (!style.empty()) {
1811 os << "\\floatstyle{" << style << "}\n"
1812 << "\\restylefloat{" << type << "}\n";
1814 if (!placement.empty()) {
1815 os << "\\floatplacement{" << type << "}{"
1816 << placement << "}\n";
1819 // The other non builtin floats.
1821 docstring const type = from_ascii(fl.floattype());
1822 docstring const placement = from_ascii(fl.placement());
1823 docstring const ext = from_ascii(fl.ext());
1824 docstring const within = from_ascii(fl.within());
1825 docstring const style = from_ascii(fl.style());
1826 docstring const name =
1827 buffer().language()->translateLayout(fl.name());
1828 os << "\\floatstyle{" << style << "}\n"
1829 << "\\newfloat{" << type << "}{" << placement
1830 << "}{" << ext << '}';
1831 if (!within.empty())
1832 os << '[' << within << ']';
1834 << "\\providecommand{\\" << type << "name}{"
1836 << "\\floatname{" << type << "}{\\protect\\"
1837 << type << "name}\n";
1839 // What missing here is to code to minimalize the code
1840 // output so that the same floatstyle will not be
1841 // used several times, when the same style is still in
1845 // The subfig package is loaded later
1846 os << "\n\\AtBeginDocument{\\newsubfloat{" << from_ascii(fl.floattype()) << "}}\n";
1851 void LaTeXFeatures::resolveAlternatives()
1853 for (Features::iterator it = features_.begin(); it != features_.end();) {
1854 if (contains(*it, '|')) {
1855 vector<string> const alternatives = getVectorFromString(*it, "|");
1856 vector<string>::const_iterator const end = alternatives.end();
1857 vector<string>::const_iterator ita = alternatives.begin();
1858 // Is any alternative already required? => use that
1859 for (; ita != end; ++ita) {
1860 if (isRequired(*ita))
1863 // Is any alternative available? => use the first one
1866 for (ita = alternatives.begin(); ita != end; ++ita) {
1867 if (isAvailable(*ita)) {
1873 // This will not work, but not requiring something
1874 // would be more confusing
1876 require(alternatives.front());
1877 features_.erase(it);
1878 it = features_.begin();
1885 void LaTeXFeatures::expandMultiples()
1887 for (Features::iterator it = features_.begin(); it != features_.end();) {
1888 if (contains(*it, ',')) {
1889 vector<string> const multiples = getVectorFromString(*it, ",");
1890 vector<string>::const_iterator const end = multiples.end();
1891 vector<string>::const_iterator itm = multiples.begin();
1892 // Do nothing if any multiple is already required
1893 for (; itm != end; ++itm) {
1894 if (!isRequired(*itm))
1897 features_.erase(it);
1898 it = features_.begin();