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 "texstream.h"
36 #include "insets/InsetLayout.h"
38 #include "support/debug.h"
39 #include "support/docstream.h"
40 #include "support/FileName.h"
41 #include "support/filetools.h"
42 #include "support/gettext.h"
43 #include "support/lstrings.h"
44 #include "support/regex.h"
50 using namespace lyx::support;
55 /////////////////////////////////////////////////////////////////////
59 /////////////////////////////////////////////////////////////////////
61 //\NeedsTeXFormat{LaTeX2e}
62 //\ProvidesPackage{lyx}[1996/01/11 LLE v0.2 (LyX LaTeX Extensions)]
63 //\message{LyX LaTeX Extensions (LLE v0.2) of 11-Jan-1996.}
65 static docstring const lyx_def = from_ascii(
66 "\\providecommand{\\LyX}{L\\kern-.1667em\\lower.25em\\hbox{Y}\\kern-.125emX\\@}");
68 static docstring const lyx_hyperref_def = from_ascii(
69 "\\providecommand{\\LyX}{\\texorpdfstring%\n"
70 " {L\\kern-.1667em\\lower.25em\\hbox{Y}\\kern-.125emX\\@}\n"
73 static docstring const noun_def = from_ascii(
74 "\\newcommand{\\noun}[1]{\\textsc{#1}}");
76 static docstring const lyxarrow_def = from_ascii(
77 "\\DeclareRobustCommand*{\\lyxarrow}{%\n"
79 "{\\leavevmode\\,$\\triangleleft$\\,\\allowbreak}\n"
80 "{\\leavevmode\\,$\\triangleright$\\,\\allowbreak}}");
82 // ZERO WIDTH SPACE (ZWSP) is actually not a space character
83 // but marks a line break opportunity. Several commands provide a
84 // line break opportunity. They differ in side-effects:
85 // \allowbreak prevents hyphenation after hyphen or dash + ZWSP
86 // \linebreak[<n>] takes an optional argument denoting "urgency".
87 // The \LyXZeroWidthSpace wrapper allows customization in the preamble.
88 static docstring const lyxZWSP_def = from_ascii(
89 "\\newcommand*\\LyXZeroWidthSpace{\\hspace{0pt}}");
91 // for quotes without babel. This does not give perfect results, but
92 // anybody serious about non-english quotes should use babel (JMarc).
94 static docstring const quotedblbase_def = from_ascii(
95 "\\ProvideTextCommandDefault{\\quotedblbase}{%\n"
96 " \\raisebox{-1.4ex}[1ex][.5ex]{\\textquotedblright}%\n"
97 " \\penalty10000\\hskip0em\\relax%\n"
100 static docstring const quotesinglbase_def = from_ascii(
101 "\\ProvideTextCommandDefault{\\quotesinglbase}{%\n"
102 " \\raisebox{-1.4ex}[1ex][.5ex]{\\textquoteright}%\n"
103 " \\penalty10000\\hskip0em\\relax%\n"
106 static docstring const guillemotleft_def = from_ascii(
107 "\\ProvideTextCommandDefault{\\guillemotleft}{%\n"
108 " {\\usefont{U}{lasy}{m}{n}\\char'50\\kern-.15em\\char'50}%\n"
109 "\\penalty10000\\hskip0pt\\relax%\n"
112 static docstring const guillemotright_def = from_ascii(
113 "\\ProvideTextCommandDefault{\\guillemotright}{%\n"
114 " \\penalty10000\\hskip0pt%\n"
115 " {\\usefont{U}{lasy}{m}{n}\\char'51\\kern-.15em\\char'51}%\n"
118 static docstring const guilsinglleft_def = from_ascii(
119 "\\ProvideTextCommandDefault{\\guilsinglleft}{%\n"
120 " {\\usefont{U}{lasy}{m}{n}\\char'50}%\n"
121 " \\penalty10000\\hskip0pt\\relax%\n"
124 static docstring const guilsinglright_def = from_ascii(
125 "\\ProvideTextCommandDefault{\\guilsinglright}{%\n"
126 " \\penalty10000\\hskip0pt%\n"
127 " {\\usefont{U}{lasy}{m}{n}\\char'51}%\n"
130 static docstring const textquotedbl_def = from_ascii(
131 "\\DeclareTextSymbolDefault{\\textquotedbl}{T1}");
133 static docstring const textquotedblp_xetex_def = from_ascii(
134 "\\providecommand\\textquotedblplain{%\n"
135 " \\bgroup\\addfontfeatures{Mapping=}\\char34\\egroup}");
137 static docstring const textquotedblp_luatex_def = from_ascii(
138 "\\providecommand\\textquotedblplain{%\n"
139 " \\bgroup\\addfontfeatures{RawFeature=-tlig}\\char34\\egroup}");
141 static docstring const textquotesinglep_xetex_def = from_ascii(
142 "\\providecommand\\textquotesingleplain{%\n"
143 " \\bgroup\\addfontfeatures{Mapping=}\\char39\\egroup}");
145 static docstring const textquotesinglep_luatex_def = from_ascii(
146 "\\providecommand\\textquotesingleplain{%\n"
147 " \\bgroup\\addfontfeatures{RawFeature=-tlig}\\char39\\egroup}");
149 static docstring const paragraphleftindent_def = from_ascii(
150 "\\newenvironment{LyXParagraphLeftIndent}[1]%\n"
152 " \\begin{list}{}{%\n"
153 " \\setlength{\\topsep}{0pt}%\n"
154 " \\addtolength{\\leftmargin}{#1}\n"
155 // ho hum, yet more things commented out with no hint as to why they
156 // weren't just removed
157 // "%% \\addtolength{\\leftmargin}{#1\\textwidth}\n"
158 // "%% \\setlength{\\textwidth}{#2\\textwidth}\n"
159 // "%% \\setlength\\listparindent\\parindent%\n"
160 // "%% \\setlength\\itemindent\\parindent%\n"
161 " \\setlength{\\parsep}{0pt plus 1pt}%\n"
167 static docstring const floatingfootnote_def = from_ascii(
168 "%% Special footnote code from the package 'stblftnt.sty'\n"
169 "%% Author: Robin Fairbairns -- Last revised Dec 13 1996\n"
170 "\\let\\SF@@footnote\\footnote\n"
171 "\\def\\footnote{\\ifx\\protect\\@typeset@protect\n"
172 " \\expandafter\\SF@@footnote\n"
174 " \\expandafter\\SF@gobble@opt\n"
177 "\\expandafter\\def\\csname SF@gobble@opt \\endcsname{\\@ifnextchar[%]\n"
178 " \\SF@gobble@twobracket\n"
181 "\\edef\\SF@gobble@opt{\\noexpand\\protect\n"
182 " \\expandafter\\noexpand\\csname SF@gobble@opt \\endcsname}\n"
183 "\\def\\SF@gobble@twobracket[#1]#2{}\n");
185 static docstring const binom_def = from_ascii(
186 "%% Binom macro for standard LaTeX users\n"
187 "\\newcommand{\\binom}[2]{{#1 \\choose #2}}\n");
189 static docstring const mathcircumflex_def = from_ascii(
190 "%% For printing a cirumflex inside a formula\n"
191 "\\newcommand{\\mathcircumflex}[0]{\\mbox{\\^{}}}\n");
193 static docstring const tabularnewline_def = from_ascii(
194 "%% Because html converters don't know tabularnewline\n"
195 "\\providecommand{\\tabularnewline}{\\\\}\n");
197 static docstring const lyxgreyedout_def = from_ascii(
198 "%% The greyedout annotation environment\n"
199 "\\newenvironment{lyxgreyedout}\n"
200 " {\\textcolor{note_fontcolor}\\bgroup\\ignorespaces}\n"
201 " {\\ignorespacesafterend\\egroup}\n");
203 // We want to omit the file extension for includegraphics, but this does not
204 // work when the filename contains other dots.
205 // Idea from http://www.tex.ac.uk/cgi-bin/texfaq2html?label=unkgrfextn
206 static docstring const lyxdot_def = from_ascii(
207 "%% A simple dot to overcome graphicx limitations\n"
208 "\\newcommand{\\lyxdot}{.}\n");
210 static docstring const changetracking_dvipost_def = from_ascii(
211 "%% Change tracking with dvipost\n"
213 "\\dvipost{osstart color push Red}\n"
214 "\\dvipost{osend color pop}\n"
215 "\\dvipost{cbstart color push Blue}\n"
216 "\\dvipost{cbend color pop}\n"
217 "\\DeclareRobustCommand{\\lyxadded}[3]{\\changestart#3\\changeend}\n"
218 "\\DeclareRobustCommand{\\lyxdeleted}[3]{%\n"
219 "\\changestart\\overstrikeon#3\\overstrikeoff\\changeend}\n");
221 static docstring const changetracking_xcolor_ulem_def = from_ascii(
222 "%% Change tracking with ulem\n"
223 "\\DeclareRobustCommand{\\lyxadded}[3]{{\\color{lyxadded}{}#3}}\n"
224 "\\DeclareRobustCommand{\\lyxdeleted}[3]{{\\color{lyxdeleted}\\lyxsout{#3}}}\n"
225 "\\DeclareRobustCommand{\\lyxsout}[1]{\\ifx\\\\#1\\else\\sout{#1}\\fi}\n");
227 static docstring const changetracking_xcolor_ulem_hyperref_def = from_ascii(
228 "%% Change tracking with ulem\n"
229 "\\DeclareRobustCommand{\\lyxadded}[3]{{\\texorpdfstring{\\color{lyxadded}{}}{}#3}}\n"
230 "\\DeclareRobustCommand{\\lyxdeleted}[3]{{\\texorpdfstring{\\color{lyxdeleted}\\lyxsout{#3}}{}}}\n"
231 "\\DeclareRobustCommand{\\lyxsout}[1]{\\ifx\\\\#1\\else\\sout{#1}\\fi}\n");
233 static docstring const changetracking_tikz_math_sout_def = from_ascii(
234 "%% Strike out display math with tikz\n"
235 "\\usepackage{tikz}\n"
236 "\\usetikzlibrary{calc}\n"
237 "\\newcommand{\\lyxmathsout}[1]{%\n"
238 " \\tikz[baseline=(math.base)]{\n"
239 " \\node[inner sep=0pt,outer sep=0pt](math){#1};\n"
240 " \\draw($(math.south west)+(2em,.5em)$)--($(math.north east)-(2em,.5em)$);\n"
244 static docstring const changetracking_none_def = from_ascii(
245 "\\newcommand{\\lyxadded}[3]{#3}\n"
246 "\\newcommand{\\lyxdeleted}[3]{}\n");
248 static docstring const textgreek_LGR_def = from_ascii(
249 "\\DeclareFontEncoding{LGR}{}{}\n");
250 static docstring const textgreek_def = from_ascii(
251 "\\DeclareRobustCommand{\\greektext}{%\n"
252 " \\fontencoding{LGR}\\selectfont\\def\\encodingdefault{LGR}}\n"
253 "\\DeclareRobustCommand{\\textgreek}[1]{\\leavevmode{\\greektext #1}}\n"
254 "\\ProvideTextCommand{\\~}{LGR}[1]{\\char126#1}\n");
256 static docstring const textcyr_T2A_def = from_ascii(
257 "\\InputIfFileExists{t2aenc.def}{}{%\n"
258 " \\errmessage{File `t2aenc.def' not found: Cyrillic script not supported}}\n");
259 static docstring const textcyr_def = from_ascii(
260 "\\DeclareRobustCommand{\\cyrtext}{%\n"
261 " \\fontencoding{T2A}\\selectfont\\def\\encodingdefault{T2A}}\n"
262 "\\DeclareRobustCommand{\\textcyr}[1]{\\leavevmode{\\cyrtext #1}}\n");
264 static docstring const lyxmathsym_def = from_ascii(
265 "\\newcommand{\\lyxmathsym}[1]{\\ifmmode\\begingroup\\def\\b@ld{bold}\n"
266 " \\text{\\ifx\\math@version\\b@ld\\bfseries\\fi#1}\\endgroup\\else#1\\fi}\n");
268 static docstring const papersizedvi_def = from_ascii(
269 "\\special{papersize=\\the\\paperwidth,\\the\\paperheight}\n");
271 static docstring const papersizepdf_def = from_ascii(
272 "\\pdfpageheight\\paperheight\n"
273 "\\pdfpagewidth\\paperwidth\n");
275 static docstring const papersizepdflua_def = from_ascii(
276 "% Backwards compatibility for LuaTeX < 0.90\n"
277 "\\@ifundefined{pageheight}{\\let\\pageheight\\pdfpageheight}{}\n"
278 "\\@ifundefined{pagewidth}{\\let\\pagewidth\\pdfpagewidth}{}\n"
279 "\\pageheight\\paperheight\n"
280 "\\pagewidth\\paperwidth\n");
282 static docstring const cedilla_def = from_ascii(
283 "\\newcommand{\\docedilla}[2]{\\underaccent{#1\\mathchar'30}{#2}}\n"
284 "\\newcommand{\\cedilla}[1]{\\mathpalette\\docedilla{#1}}\n");
286 static docstring const subring_def = from_ascii(
287 "\\newcommand{\\dosubring}[2]{\\underaccent{#1\\mathchar'27}{#2}}\n"
288 "\\newcommand{\\subring}[1]{\\mathpalette\\dosubring{#1}}\n");
290 static docstring const subdot_def = from_ascii(
291 "\\newcommand{\\dosubdot}[2]{\\underaccent{#1.}{#2}}\n"
292 "\\newcommand{\\subdot}[1]{\\mathpalette\\dosubdot{#1}}\n");
294 static docstring const subhat_def = from_ascii(
295 "\\newcommand{\\dosubhat}[2]{\\underaccent{#1\\mathchar'136}{#2}}\n"
296 "\\newcommand{\\subhat}[1]{\\mathpalette\\dosubhat{#1}}\n");
298 static docstring const subtilde_def = from_ascii(
299 "\\newcommand{\\dosubtilde}[2]{\\underaccent{#1\\mathchar'176}{#2}}\n"
300 "\\newcommand{\\subtilde}[1]{\\mathpalette\\dosubtilde{#1}}\n");
302 static docstring const dacute_def = from_ascii(
303 "\\DeclareMathAccent{\\dacute}{\\mathalpha}{operators}{'175}\n");
305 static docstring const tipasymb_def = from_ascii(
306 "\\DeclareFontEncoding{T3}{}{}\n"
307 "\\DeclareSymbolFont{tipasymb}{T3}{cmr}{m}{n}\n");
309 static docstring const dgrave_def = from_ascii(
310 "\\DeclareMathAccent{\\dgrave}{\\mathord}{tipasymb}{'15}\n");
312 static docstring const rcap_def = from_ascii(
313 "\\DeclareMathAccent{\\rcap}{\\mathord}{tipasymb}{'20}\n");
315 static docstring const ogonek_def = from_ascii(
316 "\\newcommand{\\doogonek}[2]{\\setbox0=\\hbox{$#1#2$}\\underaccent{#1\\mkern-6mu\n"
317 " \\ifx#2O\\hskip0.5\\wd0\\else\\ifx#2U\\hskip0.5\\wd0\\else\\hskip\\wd0\\fi\\fi\n"
318 " \\ifx#2o\\mkern-2mu\\else\\ifx#2e\\mkern-1mu\\fi\\fi\n"
319 " \\mathchar\"0\\hexnumber@\\symtipasymb0C}{#2}}\n"
320 "\\newcommand{\\ogonek}[1]{\\mathpalette\\doogonek{#1}}\n");
322 static docstring const lyxaccent_def = from_ascii(
323 "%% custom text accent \\LyxTextAccent[<rise value (length)>]{<accent>}{<base>}\n"
324 "\\newcommand*{\\LyxTextAccent}[3][0ex]{%\n"
325 " \\hmode@bgroup\\ooalign{\\null#3\\crcr\\hidewidth\n"
326 " \\raise#1\\hbox{#2}\\hidewidth}\\egroup}\n"
327 "%% select a font size smaller than the current font size:\n"
328 "\\newcommand{\\LyxAccentSize}[1][\\sf@size]{%\n"
329 " \\check@mathfonts\\fontsize#1\\z@\\math@fontsfalse\\selectfont\n"
332 static docstring const textcommabelow_def = from_ascii(
333 "\\ProvideTextCommandDefault{\\textcommabelow}[1]{%%\n"
334 " \\LyxTextAccent[-.31ex]{\\LyxAccentSize,}{#1}}\n");
336 static docstring const textcommaabove_def = from_ascii(
337 "\\ProvideTextCommandDefault{\\textcommaabove}[1]{%%\n"
338 " \\LyxTextAccent[.5ex]{\\LyxAccentSize`}{#1}}\n");
340 static docstring const textcommaaboveright_def = from_ascii(
341 "\\ProvideTextCommandDefault{\\textcommaaboveright}[1]{%%\n"
342 " \\LyxTextAccent[.5ex]{\\LyxAccentSize\\ '}{#1}}\n");
344 // Baltic languages use a comma-accent instead of a cedilla
345 static docstring const textbaltic_def = from_ascii(
346 "%% use comma accent instead of cedilla for these characters:\n"
347 "\\DeclareTextCompositeCommand{\\c}{T1}{g}{\\textcommaabove{g}}\n"
348 "\\DeclareTextCompositeCommand{\\c}{T1}{G}{\\textcommabelow{G}}\n"
349 "\\DeclareTextCompositeCommand{\\c}{T1}{k}{\\textcommabelow{k}}\n"
350 "\\DeclareTextCompositeCommand{\\c}{T1}{K}{\\textcommabelow{K}}\n"
351 "\\DeclareTextCompositeCommand{\\c}{T1}{l}{\\textcommabelow{l}}\n"
352 "\\DeclareTextCompositeCommand{\\c}{T1}{L}{\\textcommabelow{L}}\n"
353 "\\DeclareTextCompositeCommand{\\c}{T1}{n}{\\textcommabelow{n}}\n"
354 "\\DeclareTextCompositeCommand{\\c}{T1}{N}{\\textcommabelow{N}}\n"
355 "\\DeclareTextCompositeCommand{\\c}{T1}{r}{\\textcommabelow{r}}\n"
356 "\\DeclareTextCompositeCommand{\\c}{T1}{R}{\\textcommabelow{R}}\n");
358 // split-level fractions
359 static docstring const xfrac_def = from_ascii(
360 "\\usepackage{xfrac}\n");
361 static docstring const smallLetterFrac_def = from_ascii(
362 "\\DeclareCollectionInstance{smallLetterFrac}{xfrac}{default}{text}\n"
363 " {phantom=c, scale-factor=1.0, slash-left-kern=-.05em}\n"
364 "\\DeclareCollectionInstance{smallLetterFrac}{xfrac}{lmr}{text}\n"
365 " {slash-symbol-font=ptm, phantom=c, scale-factor=1, slash-left-kern=-.05em}\n"
366 "\\DeclareCollectionInstance{smallLetterFrac}{xfrac}{lmss}{text}\n"
367 " {slash-symbol-font=ptm, phantom=c, scale-factor=1, slash-left-kern=-.05em}\n"
368 "\\DeclareCollectionInstance{smallLetterFrac}{xfrac}{cmr}{text}\n"
369 " {slash-symbol-font=ptm, phantom=c, scale-factor=1, slash-left-kern=-.05em}\n"
370 "\\DeclareCollectionInstance{smallLetterFrac}{xfrac}{cmss}{text}\n"
371 " {slash-symbol-font=ptm, phantom=c, scale-factor=1, slash-left-kern=-.05em}\n"
372 "\\newcommand{\\smallLetterFrac}[2]{%\n"
373 " {\\UseCollection{xfrac}{smallLetterFrac}\\sfrac{#1}{#2}}}\n");
375 static docstring const lyxref_def = from_ascii(
376 "\\RS@ifundefined{subsecref}\n"
377 " {\\newref{subsec}{name = \\RSsectxt}}\n"
379 "\\RS@ifundefined{thmref}\n"
380 " {\\def\\RSthmtxt{theorem~}\\newref{thm}{name = \\RSthmtxt}}\n"
382 "\\RS@ifundefined{lemref}\n"
383 " {\\def\\RSlemtxt{lemma~}\\newref{lem}{name = \\RSlemtxt}}\n"
386 // Make sure the columns are also outputed as rtl
387 static docstring const rtloutputdblcol_def = from_ascii(
388 "\\def\\@outputdblcol{%\n"
389 " \\if@firstcolumn\n"
390 " \\global \\@firstcolumnfalse\n"
391 " \\global \\setbox\\@leftcolumn \\box\\@outputbox\n"
393 " \\global \\@firstcolumntrue\n"
394 " \\setbox\\@outputbox \\vbox {%\n"
395 " \\hb@xt@\\textwidth {%\n"
396 " \\kern\\textwidth \\kern-\\columnwidth %**\n"
397 " \\hb@xt@\\columnwidth {%\n"
398 " \\box\\@leftcolumn \\hss}%\n"
399 " \\kern-\\textwidth %**\n"
401 " {\\normalcolor\\vrule \\@width\\columnseprule}%\n"
403 " \\kern-\\textwidth %**\n"
404 " \\hb@xt@\\columnwidth {%\n"
405 " \\box\\@outputbox \\hss}%\n"
406 " \\kern-\\columnwidth \\kern\\textwidth %**\n"
409 " \\@combinedblfloats\n"
412 " \\@dblfloatplacement\n"
413 " \\@startdblcolumn\n"
414 " \\@whilesw\\if@fcolmade \\fi\n"
416 " \\@startdblcolumn}%\n"
420 "\\@mparswitchtrue\n");
422 static docstring const lyxmintcaption_def = from_ascii(
423 "\\long\\def\\lyxmintcaption[#1]#2{%\n"
424 " \\ifx#1t\\vskip\\baselineskip\\fi%\n"
425 " \\refstepcounter{listing}\\noindent%\n"
426 " \\addcontentsline{lol}{listing}%\n"
427 " {\\protect\\numberline{\\thelisting}{\\ignorespaces #2}}%\n"
428 " \\setbox\\@tempboxa\\hbox{\\listingscaption~\\thelisting: #2}%\n"
429 " \\ifdim \\wd\\@tempboxa >\\linewidth%\n"
430 " \\parbox[t]{\\linewidth}{\\unhbox\\@tempboxa}\\else%\n"
431 " \\hbox to\\linewidth{\\hfil\\box\\@tempboxa\\hfil}\\fi%\n"
432 " \\ifx#1b\\vskip\\baselineskip\\fi\n"
436 /////////////////////////////////////////////////////////////////////
440 /////////////////////////////////////////////////////////////////////
442 static docstring const lyxnoun_style = from_ascii(
444 " font-variant: small-caps;\n"
448 // this is how it normally renders, but it might not always do so.
449 static docstring const lyxstrikeout_style = from_ascii(
451 " text-decoration: line-through;\n"
455 /////////////////////////////////////////////////////////////////////
459 /////////////////////////////////////////////////////////////////////
462 LaTeXFeatures::LaTeXFeatures(Buffer const & b, BufferParams const & p,
463 OutputParams const & r)
464 : buffer_(&b), params_(p), runparams_(r), in_float_(false),
465 in_deleted_inset_(false)
469 LaTeXFeatures::LangPackage LaTeXFeatures::langPackage() const
471 string const local_lp = bufferParams().lang_package;
473 // Locally, custom is just stored as a string
474 // in bufferParams().lang_package.
475 if (local_lp != "auto"
476 && local_lp != "babel"
477 && local_lp != "default"
478 && local_lp != "none")
479 return LANG_PACK_CUSTOM;
481 if (local_lp == "none")
482 return LANG_PACK_NONE;
484 /* If "auto" is selected, we load polyglossia with non-TeX fonts,
485 * else we select babel.
486 * If babel is selected (either directly or via the "auto"
487 * mechanism), we really do only require it if we have
488 * a language that needs it.
490 bool const polyglossia_required =
491 params_.useNonTeXFonts
492 && isAvailable("polyglossia")
493 && !isProvided("babel")
494 && this->hasOnlyPolyglossiaLanguages();
495 bool const babel_required =
496 !bufferParams().language->babel().empty()
497 || !this->getBabelLanguages().empty();
499 if (local_lp == "auto") {
500 // polyglossia requirement has priority over babel
501 if (polyglossia_required)
502 return LANG_PACK_POLYGLOSSIA;
503 else if (babel_required)
504 return LANG_PACK_BABEL;
507 if (local_lp == "babel") {
509 return LANG_PACK_BABEL;
512 if (local_lp == "default") {
513 switch (lyxrc.language_package_selection) {
515 // polyglossia requirement has priority over babel
516 if (polyglossia_required)
517 return LANG_PACK_POLYGLOSSIA;
518 else if (babel_required)
519 return LANG_PACK_BABEL;
521 case LyXRC::LP_BABEL:
523 return LANG_PACK_BABEL;
525 case LyXRC::LP_CUSTOM:
526 return LANG_PACK_CUSTOM;
528 return LANG_PACK_NONE;
532 return LANG_PACK_NONE;
536 void LaTeXFeatures::require(string const & name)
538 features_.insert(name);
542 void LaTeXFeatures::require(set<string> const & names)
544 features_.insert(names.begin(), names.end());
548 void LaTeXFeatures::useLayout(docstring const & layoutname)
550 useLayout(layoutname, 0);
554 void LaTeXFeatures::useLayout(docstring const & layoutname, int level)
556 // Some code to avoid loops in dependency definition
557 const int maxlevel = 30;
558 if (level > maxlevel) {
559 lyxerr << "LaTeXFeatures::useLayout: maximum level of "
560 << "recursion attained by layout "
561 << to_utf8(layoutname) << endl;
565 DocumentClass const & tclass = params_.documentClass();
566 if (tclass.hasLayout(layoutname)) {
567 // Is this layout already in usedLayouts?
568 if (find(usedLayouts_.begin(), usedLayouts_.end(), layoutname)
569 != usedLayouts_.end())
572 Layout const & layout = tclass[layoutname];
573 require(layout.requires());
575 if (!layout.depends_on().empty()) {
576 useLayout(layout.depends_on(), level + 1);
578 usedLayouts_.push_back(layoutname);
580 lyxerr << "LaTeXFeatures::useLayout: layout `"
581 << to_utf8(layoutname) << "' does not exist in this class"
587 void LaTeXFeatures::useInsetLayout(InsetLayout const & lay)
589 docstring const & lname = lay.name();
590 DocumentClass const & tclass = params_.documentClass();
592 // this is a default inset layout, nothing useful here
593 if (!tclass.hasInsetLayout(lname))
595 // Is this layout already in usedInsetLayouts?
596 if (find(usedInsetLayouts_.begin(), usedInsetLayouts_.end(), lname)
597 != usedInsetLayouts_.end())
600 require(lay.requires());
601 usedInsetLayouts_.push_back(lname);
605 bool LaTeXFeatures::isRequired(string const & name) const
607 return features_.find(name) != features_.end();
611 bool LaTeXFeatures::isProvided(string const & name) const
613 if (params_.useNonTeXFonts)
614 return params_.documentClass().provides(name);
616 bool const ot1 = (params_.main_font_encoding() == "default"
617 || params_.main_font_encoding() == "OT1");
618 bool const complete = (params_.fontsSans() == "default"
619 && params_.fontsTypewriter() == "default");
620 bool const nomath = (params_.fontsMath() == "default");
621 return params_.documentClass().provides(name)
622 || theLaTeXFonts().getLaTeXFont(
623 from_ascii(params_.fontsRoman())).provides(name, ot1,
626 || theLaTeXFonts().getLaTeXFont(
627 from_ascii(params_.fontsSans())).provides(name, ot1,
630 || theLaTeXFonts().getLaTeXFont(
631 from_ascii(params_.fontsTypewriter())).provides(name, ot1,
634 || theLaTeXFonts().getLaTeXFont(
635 from_ascii(params_.fontsMath())).provides(name, ot1,
638 // TODO: "textbaltic" provided, if the font-encoding is "L7x"
639 // "textgreek" provided, if a language with font-encoding LGR is used in the document
640 // "textcyr" provided, if a language with font-encoding T2A is used in the document
644 bool LaTeXFeatures::mustProvide(string const & name) const
646 return isRequired(name) && !isProvided(name);
650 bool LaTeXFeatures::isAvailable(string const & name)
652 string::size_type const i = name.find("->");
653 if (i != string::npos) {
654 string const from = name.substr(0,i);
655 string const to = name.substr(i+2);
656 //LYXERR0("from=[" << from << "] to=[" << to << "]");
657 return theConverters().isReachable(from, to);
659 return LaTeXPackages::isAvailable(name);
665 void addSnippet(std::list<TexString> & list, TexString ts, bool allow_dupes)
668 // test the absense of duplicates, i.e. elements with same str
669 none_of(list.begin(), list.end(), [&](TexString const & ts2){
670 return ts.str == ts2.str;
673 list.push_back(move(ts));
677 TexString getSnippets(std::list<TexString> const & list)
679 otexstringstream snip;
680 for (TexString const & ts : list)
681 snip << TexString(ts) << '\n';
682 return snip.release();
688 void LaTeXFeatures::addPreambleSnippet(TexString ts, bool allow_dupes)
690 addSnippet(preamble_snippets_, move(ts), allow_dupes);
694 void LaTeXFeatures::addPreambleSnippet(docstring const & str, bool allow_dupes)
696 addSnippet(preamble_snippets_, TexString(str), allow_dupes);
700 void LaTeXFeatures::addCSSSnippet(std::string const & snippet)
702 addSnippet(css_snippets_, TexString(from_ascii(snippet)), false);
706 TexString LaTeXFeatures::getPreambleSnippets() const
708 return getSnippets(preamble_snippets_);
712 docstring LaTeXFeatures::getCSSSnippets() const
714 return getSnippets(css_snippets_).str;
719 void LaTeXFeatures::useFloat(string const & name, bool subfloat)
721 if (!usedFloats_[name])
722 usedFloats_[name] = subfloat;
725 // We only need float.sty if we use non builtin floats, or if we
726 // use the "H" modifier. This includes modified table and
727 // figure floats. (Lgb)
728 Floating const & fl = params_.documentClass().floats().getType(name);
729 if (!fl.floattype().empty() && fl.usesFloatPkg()) {
735 void LaTeXFeatures::useLanguage(Language const * lang)
737 if (!lang->babel().empty() || !lang->polyglossia().empty())
738 UsedLanguages_.insert(lang);
739 if (!lang->requires().empty())
740 require(lang->requires());
741 // CJK languages do not have a babel name.
742 // They use the CJK package
743 if (lang->encoding()->package() == Encoding::CJK)
745 // japanese package is special
746 if (lang->encoding()->package() == Encoding::japanese)
751 void LaTeXFeatures::includeFile(docstring const & key, string const & name)
753 IncludedFiles_[key] = name;
757 bool LaTeXFeatures::hasLanguages() const
759 return !UsedLanguages_.empty();
763 bool LaTeXFeatures::hasOnlyPolyglossiaLanguages() const
765 // first the main language
766 if (params_.language->polyglossia().empty())
768 // now the secondary languages
769 LanguageList::const_iterator const begin = UsedLanguages_.begin();
770 for (LanguageList::const_iterator cit = begin;
771 cit != UsedLanguages_.end();
773 if ((*cit)->polyglossia().empty())
780 bool LaTeXFeatures::hasPolyglossiaExclusiveLanguages() const
782 // first the main language
783 if (params_.language->isPolyglossiaExclusive())
785 // now the secondary languages
786 LanguageList::const_iterator const begin = UsedLanguages_.begin();
787 for (LanguageList::const_iterator cit = begin;
788 cit != UsedLanguages_.end();
790 if ((*cit)->isPolyglossiaExclusive())
797 vector<string> LaTeXFeatures::getPolyglossiaExclusiveLanguages() const
799 vector<string> result;
800 // first the main language
801 if (params_.language->isPolyglossiaExclusive())
802 result.push_back(params_.language->display());
803 // now the secondary languages
804 LanguageList::const_iterator const begin = UsedLanguages_.begin();
805 for (LanguageList::const_iterator cit = begin;
806 cit != UsedLanguages_.end();
808 if ((*cit)->isPolyglossiaExclusive())
809 result.push_back((*cit)->display());
815 vector<string> LaTeXFeatures::getBabelExclusiveLanguages() const
817 vector<string> result;
818 // first the main language
819 if (params_.language->isBabelExclusive())
820 result.push_back(params_.language->display());
821 // now the secondary languages
822 LanguageList::const_iterator const begin = UsedLanguages_.begin();
823 for (LanguageList::const_iterator cit = begin;
824 cit != UsedLanguages_.end();
826 if ((*cit)->isBabelExclusive())
827 result.push_back((*cit)->display());
833 string LaTeXFeatures::getBabelLanguages() const
835 ostringstream languages;
838 LanguageList::const_iterator const begin = UsedLanguages_.begin();
839 for (LanguageList::const_iterator cit = begin;
840 cit != UsedLanguages_.end();
842 if ((*cit)->babel().empty())
848 languages << (*cit)->babel();
850 return languages.str();
854 set<string> LaTeXFeatures::getPolyglossiaLanguages() const
856 set<string> languages;
858 LanguageList::const_iterator const begin = UsedLanguages_.begin();
859 for (LanguageList::const_iterator cit = begin;
860 cit != UsedLanguages_.end();
862 // We do not need the variants here
863 languages.insert((*cit)->polyglossia());
869 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding) const
871 // This does only find encodings of languages supported by babel, but
872 // that does not matter since we don't have a language with an
873 // encoding supported by inputenc but without babel support.
874 set<string> encodings;
875 LanguageList::const_iterator it = UsedLanguages_.begin();
876 LanguageList::const_iterator end = UsedLanguages_.end();
877 for (; it != end; ++it)
878 if ((*it)->encoding()->latexName() != doc_encoding &&
879 ((*it)->encoding()->package() == Encoding::inputenc
880 || (*it)->encoding()->package() == Encoding::japanese))
881 encodings.insert((*it)->encoding()->latexName());
886 void LaTeXFeatures::getFontEncodings(vector<string> & encodings) const
888 // these must be loaded if glyphs of this script are used
889 // unless a language providing them is used in the document
890 if (mustProvide("textgreek")
891 && find(encodings.begin(), encodings.end(), "LGR") == encodings.end())
892 encodings.insert(encodings.begin(), "LGR");
893 if (mustProvide("textcyr")
894 && find(encodings.begin(), encodings.end(), "T2A") == encodings.end())
895 encodings.insert(encodings.begin(), "T2A");
897 LanguageList::const_iterator it = UsedLanguages_.begin();
898 LanguageList::const_iterator end = UsedLanguages_.end();
899 for (; it != end; ++it)
900 if (!(*it)->fontenc().empty()
901 && ascii_lowercase((*it)->fontenc()) != "none") {
902 vector<string> extraencs = getVectorFromString((*it)->fontenc());
903 vector<string>::const_iterator fit = extraencs.begin();
904 for (; fit != extraencs.end(); ++fit) {
905 if (find(encodings.begin(), encodings.end(), *fit) == encodings.end())
906 encodings.insert(encodings.begin(), *fit);
913 char const * simplefeatures[] = {
914 // note that the package order here will be the same in the LaTeX-output
921 // subfig is handled in BufferParams.cpp
925 /*For a successful cooperation of the `wrapfig' package with the
926 `float' package you should load the `wrapfig' package *after*
927 the `float' package. See the caption package documentation
958 // listings is handled in BufferParams.cpp
979 char const * bibliofeatures[] = {
980 // Known bibliography packages (will be loaded before natbib)
996 int const nb_bibliofeatures = sizeof(bibliofeatures) / sizeof(char const *);
998 int const nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
1003 string const LaTeXFeatures::getColorOptions() const
1005 ostringstream colors;
1007 // Handling the color packages separately is needed to be able to load them
1008 // before babel when hyperref is loaded with the colorlinks option
1009 // for more info see Bufferparams.cpp
1012 if (mustProvide("color") || mustProvide("xcolor")) {
1013 string const package =
1014 (mustProvide("xcolor") ? "xcolor" : "color");
1015 if (params_.graphics_driver == "default"
1016 || params_.graphics_driver == "none")
1017 colors << "\\usepackage{" << package << "}\n";
1019 colors << "\\usepackage["
1020 << params_.graphics_driver
1021 << "]{" << package << "}\n";
1024 // pdfcolmk must be loaded after color
1025 if (mustProvide("pdfcolmk"))
1026 colors << "\\usepackage{pdfcolmk}\n";
1028 // the following 3 color commands must be set after color
1029 // is loaded and before pdfpages, therefore add the command
1030 // here define the set color
1031 if (mustProvide("pagecolor")) {
1032 colors << "\\definecolor{page_backgroundcolor}{rgb}{";
1033 colors << outputLaTeXColor(params_.backgroundcolor) << "}\n";
1034 // set the page color
1035 colors << "\\pagecolor{page_backgroundcolor}\n";
1038 if (mustProvide("fontcolor")) {
1039 colors << "\\definecolor{document_fontcolor}{rgb}{";
1040 colors << outputLaTeXColor(params_.fontcolor) << "}\n";
1042 colors << "\\color{document_fontcolor}\n";
1045 if (mustProvide("lyxgreyedout")) {
1046 colors << "\\definecolor{note_fontcolor}{rgb}{";
1047 colors << outputLaTeXColor(params_.notefontcolor) << "}\n";
1048 // the color will be set together with the definition of
1049 // the lyxgreyedout environment (see lyxgreyedout_def)
1052 // color for shaded boxes
1053 if (isRequired("framed") && mustProvide("color")) {
1054 colors << "\\definecolor{shadecolor}{rgb}{";
1055 colors << outputLaTeXColor(params_.boxbgcolor) << "}\n";
1056 // this color is automatically used by the LaTeX-package "framed"
1059 return colors.str();
1063 string const LaTeXFeatures::getPackageOptions() const
1065 ostringstream packageopts;
1066 // Output all the package option stuff we have been asked to do.
1067 map<string, string>::const_iterator it =
1068 params_.documentClass().packageOptions().begin();
1069 map<string, string>::const_iterator en =
1070 params_.documentClass().packageOptions().end();
1071 for (; it != en; ++it)
1072 if (mustProvide(it->first))
1073 packageopts << "\\PassOptionsToPackage{" << it->second << "}"
1074 << "{" << it->first << "}\n";
1075 return packageopts.str();
1079 string const LaTeXFeatures::getPackages() const
1081 ostringstream packages;
1083 // FIXME: currently, we can only load packages and macros known
1085 // However, with the Require tag of layouts/custom insets,
1086 // also unknown packages can be requested. They are silently
1087 // swallowed now. We should change this eventually.
1089 // These are all the 'simple' includes. i.e
1090 // packages which we just \usepackage{package}
1091 for (int i = 0; i < nb_simplefeatures; ++i) {
1092 if (mustProvide(simplefeatures[i]))
1093 packages << "\\usepackage{" << simplefeatures[i] << "}\n";
1096 // The rest of these packages are somewhat more complicated
1097 // than those above.
1099 // The tipa package and its extensions (tipx, tone) must not
1100 // be loaded with non-TeX fonts, since fontspec includes the
1101 // respective macros
1102 if (mustProvide("tipa") && !params_.useNonTeXFonts)
1103 packages << "\\usepackage{tipa}\n";
1104 if (mustProvide("tipx") && !params_.useNonTeXFonts)
1105 packages << "\\usepackage{tipx}\n";
1106 if (mustProvide("extraipa") && !params_.useNonTeXFonts)
1107 packages << "\\usepackage{extraipa}\n";
1108 if (mustProvide("tone") && !params_.useNonTeXFonts)
1109 packages << "\\usepackage{tone}\n";
1111 // if fontspec or newtxmath is used, AMS packages have to be loaded
1112 // before fontspec (in BufferParams)
1113 string const amsPackages = loadAMSPackages();
1114 bool const ot1 = (params_.main_font_encoding() == "default"
1115 || params_.main_font_encoding() == "OT1");
1116 bool const use_newtxmath =
1117 theLaTeXFonts().getLaTeXFont(from_ascii(params_.fontsMath())).getUsedPackage(
1118 ot1, false, false) == "newtxmath";
1120 if (!params_.useNonTeXFonts && !use_newtxmath && !amsPackages.empty())
1121 packages << amsPackages;
1123 if (mustProvide("cancel") &&
1124 params_.use_package("cancel") != BufferParams::package_off)
1125 packages << "\\usepackage{cancel}\n";
1127 // marvosym and bbding both define the \Cross macro
1128 if (mustProvide("marvosym")) {
1129 if (mustProvide("bbding"))
1130 packages << "\\let\\Cross\\relax\n";
1131 packages << "\\usepackage{marvosym}\n";
1134 // accents must be loaded after amsmath
1135 if (mustProvide("accents") &&
1136 params_.use_package("accents") != BufferParams::package_off)
1137 packages << "\\usepackage{accents}\n";
1139 // mathdots must be loaded after amsmath
1140 if (mustProvide("mathdots") &&
1141 params_.use_package("mathdots") != BufferParams::package_off)
1142 packages << "\\usepackage{mathdots}\n";
1144 // yhmath must be loaded after amsmath
1145 if (mustProvide("yhmath") &&
1146 params_.use_package("yhmath") != BufferParams::package_off)
1147 packages << "\\usepackage{yhmath}\n";
1149 // stmaryrd must be loaded after amsmath
1150 if (mustProvide("stmaryrd") &&
1151 params_.use_package("stmaryrd") != BufferParams::package_off)
1152 packages << "\\usepackage{stmaryrd}\n";
1154 if (mustProvide("stackrel") &&
1155 params_.use_package("stackrel") != BufferParams::package_off)
1156 packages << "\\usepackage{stackrel}\n";
1158 if (mustProvide("undertilde") &&
1159 params_.use_package("undertilde") != BufferParams::package_off)
1160 packages << "\\usepackage{undertilde}\n";
1162 // [x]color and pdfcolmk are handled in getColorOptions() above
1165 if (isRequired("makeidx") || isRequired("splitidx")) {
1166 if (!isProvided("makeidx") && !isRequired("splitidx"))
1167 packages << "\\usepackage{makeidx}\n";
1168 if (mustProvide("splitidx"))
1169 packages << "\\usepackage{splitidx}\n";
1170 packages << "\\makeindex\n";
1174 if (mustProvide("graphicx") && params_.graphics_driver != "none") {
1175 if (params_.graphics_driver == "default")
1176 packages << "\\usepackage{graphicx}\n";
1178 packages << "\\usepackage["
1179 << params_.graphics_driver
1183 // lyxskak.sty --- newer chess support based on skak.sty
1184 if (mustProvide("chess"))
1185 packages << "\\usepackage[ps,mover]{lyxskak}\n";
1188 if (mustProvide("setspace") && !isProvided("SetSpace"))
1189 packages << "\\usepackage{setspace}\n";
1191 // we need to assure that mhchem is loaded before esint and every other
1192 // package that redefines command of amsmath because mhchem loads amlatex
1193 // (this info is from the author of mhchem from June 2013)
1194 if (mustProvide("mhchem") &&
1195 params_.use_package("mhchem") != BufferParams::package_off)
1196 packages << "\\PassOptionsToPackage{version=3}{mhchem}\n"
1197 "\\usepackage{mhchem}\n";
1199 // wasysym is a simple feature, but it must be after amsmath if both
1201 // wasysym redefines some integrals (e.g. iint) from amsmath. That
1202 // leads to inconsistent integrals. We only load this package if
1203 // the document does not contain integrals (then isRequired("esint")
1204 // is false) or if esint is used, since esint redefines all relevant
1205 // integral symbols from wasysym and amsmath.
1206 // See http://www.lyx.org/trac/ticket/1942
1207 if (mustProvide("wasysym") &&
1208 params_.use_package("wasysym") != BufferParams::package_off &&
1209 (params_.use_package("esint") != BufferParams::package_off || !isRequired("esint")))
1210 packages << "\\usepackage{wasysym}\n";
1212 // esint must be after amsmath (and packages requiring amsmath, like mhchem)
1213 // and wasysym, since it will redeclare inconsistent integral symbols
1214 if (mustProvide("esint") &&
1215 params_.use_package("esint") != BufferParams::package_off)
1216 packages << "\\usepackage{esint}\n";
1218 // Known bibliography packages (simple \usepackage{package})
1219 for (int i = 0; i < nb_bibliofeatures; ++i) {
1220 if (mustProvide(bibliofeatures[i]))
1221 packages << "\\usepackage{"
1222 << bibliofeatures[i] << "}\n";
1225 // Compatibility between achicago and natbib
1226 if (mustProvide("achicago") && mustProvide("natbib"))
1227 packages << "\\let\\achicagobib\\thebibliography\n";
1230 // Some classes load natbib themselves, but still allow (or even require)
1231 // plain numeric citations (ReVTeX is such a case, see bug 5182).
1232 // This special case is indicated by the "natbib-internal" key.
1233 if (mustProvide("natbib") && !isProvided("natbib-internal")) {
1234 packages << "\\usepackage[";
1235 if (params_.citeEngineType() == ENGINE_TYPE_NUMERICAL)
1236 packages << "numbers";
1238 packages << "authoryear";
1239 if (!params_.biblio_opts.empty())
1240 packages << ',' << params_.biblio_opts;
1241 packages << "]{natbib}\n";
1244 // Compatibility between achicago and natbib
1245 if (mustProvide("achicago") && mustProvide("natbib")) {
1246 packages << "\\let\\thebibliography\\achicagobib\n";
1247 packages << "\\let\\SCcite\\astroncite\n";
1248 packages << "\\let\\UnexpandableProtect\\protect\n";
1251 // jurabib -- we need version 0.6 at least.
1252 if (mustProvide("jurabib")) {
1253 packages << "\\usepackage";
1254 if (!params_.biblio_opts.empty())
1255 packages << '[' << params_.biblio_opts << ']';
1256 packages << "{jurabib}[2004/01/25]\n";
1259 // opcit -- we pass custombst as we output \bibliographystyle ourselves
1260 if (mustProvide("opcit")) {
1261 if (isRequired("hyperref"))
1262 packages << "\\usepackage[custombst,hyperref]{opcit}\n";
1264 packages << "\\usepackage[custombst]{opcit}\n";
1267 // xargs -- we need version 1.09 at least
1268 if (mustProvide("xargs"))
1269 packages << "\\usepackage{xargs}[2008/03/08]\n";
1271 if (mustProvide("xy"))
1272 packages << "\\usepackage[all]{xy}\n";
1274 if (mustProvide("feyn"))
1275 packages << "\\usepackage{feyn}\n"; //Diagram
1277 if (mustProvide("ulem"))
1278 packages << "\\PassOptionsToPackage{normalem}{ulem}\n"
1279 "\\usepackage{ulem}\n";
1281 if (mustProvide("nomencl")) {
1282 // Make it work with the new and old version of the package,
1283 // but don't use the compatibility option since it is
1284 // incompatible to other packages.
1285 packages << "\\usepackage{nomencl}\n"
1286 "% the following is useful when we have the old nomencl.sty package\n"
1287 "\\providecommand{\\printnomenclature}{\\printglossary}\n"
1288 "\\providecommand{\\makenomenclature}{\\makeglossary}\n"
1289 "\\makenomenclature\n";
1292 // fixltx2e provides subscript
1293 if (mustProvide("subscript") && !isRequired("fixltx2e"))
1294 packages << "\\usepackage{subscript}\n";
1296 // footmisc must be loaded after setspace
1297 // Set options here, load the package after the user preamble to
1298 // avoid problems with manual loaded footmisc.
1299 if (mustProvide("footmisc"))
1300 packages << "\\PassOptionsToPackage{stable}{footmisc}\n";
1302 if (mustProvide("microtype")){
1303 packages << "\\usepackage{microtype}\n";
1306 return packages.str();
1310 TexString LaTeXFeatures::getMacros() const
1312 otexstringstream macros;
1314 if (!preamble_snippets_.empty()) {
1316 macros << getPreambleSnippets();
1319 if (mustProvide("papersize")) {
1320 if (runparams_.flavor == OutputParams::LATEX
1321 || runparams_.flavor == OutputParams::DVILUATEX)
1322 macros << papersizedvi_def << '\n';
1323 else if (runparams_.flavor == OutputParams::LUATEX)
1324 macros << papersizepdflua_def << '\n';
1326 macros << papersizepdf_def << '\n';
1329 if (mustProvide("LyX")) {
1330 if (isRequired("hyperref"))
1331 macros << lyx_hyperref_def << '\n';
1333 macros << lyx_def << '\n';
1336 if (mustProvide("noun"))
1337 macros << noun_def << '\n';
1339 if (mustProvide("lyxarrow"))
1340 macros << lyxarrow_def << '\n';
1342 if (mustProvide("lyxzerowidthspace"))
1343 macros << lyxZWSP_def << '\n';
1345 if (!usePolyglossia() && mustProvide("textgreek")) {
1346 // ensure LGR font encoding is defined also if fontenc is not loaded by LyX
1347 if (params_.main_font_encoding() == "default")
1348 macros << textgreek_LGR_def;
1349 macros << textgreek_def << '\n';
1352 if (!usePolyglossia() && mustProvide("textcyr")) {
1353 // ensure T2A font encoding is set up also if fontenc is not loaded by LyX
1354 if (params_.main_font_encoding() == "default")
1355 macros << textcyr_T2A_def;
1356 macros << textcyr_def << '\n';
1359 // non-standard text accents:
1360 if (mustProvide("textcommaabove") || mustProvide("textcommaaboveright") ||
1361 mustProvide("textcommabelow") || mustProvide("textbaltic"))
1362 macros << lyxaccent_def;
1364 if (mustProvide("textcommabelow") || mustProvide("textbaltic"))
1365 macros << textcommabelow_def << '\n';
1367 if (mustProvide("textcommaabove") || mustProvide("textbaltic"))
1368 macros << textcommaabove_def << '\n';
1370 if (mustProvide("textcommaaboveright"))
1371 macros << textcommaaboveright_def << '\n';
1373 if (mustProvide("textbaltic"))
1374 macros << textbaltic_def << '\n';
1376 // split-level fractions
1377 if (mustProvide("xfrac") || mustProvide("smallLetterFrac"))
1378 macros << xfrac_def << '\n';
1380 if (mustProvide("smallLetterFrac"))
1381 macros << smallLetterFrac_def << '\n';
1383 if (mustProvide("lyxmathsym"))
1384 macros << lyxmathsym_def << '\n';
1386 if (mustProvide("cedilla"))
1387 macros << cedilla_def << '\n';
1389 if (mustProvide("subring"))
1390 macros << subring_def << '\n';
1392 if (mustProvide("subdot"))
1393 macros << subdot_def << '\n';
1395 if (mustProvide("subhat"))
1396 macros << subhat_def << '\n';
1398 if (mustProvide("subtilde"))
1399 macros << subtilde_def << '\n';
1401 if (mustProvide("dacute"))
1402 macros << dacute_def << '\n';
1404 if (mustProvide("tipasymb"))
1405 macros << tipasymb_def << '\n';
1407 if (mustProvide("dgrave"))
1408 macros << dgrave_def << '\n';
1410 if (mustProvide("rcap"))
1411 macros << rcap_def << '\n';
1413 if (mustProvide("ogonek"))
1414 macros << ogonek_def << '\n';
1417 if (mustProvide("quotesinglbase"))
1418 macros << quotesinglbase_def << '\n';
1419 if (mustProvide("quotedblbase"))
1420 macros << quotedblbase_def << '\n';
1421 if (mustProvide("guilsinglleft"))
1422 macros << guilsinglleft_def << '\n';
1423 if (mustProvide("guilsinglright"))
1424 macros << guilsinglright_def << '\n';
1425 if (mustProvide("guillemotleft"))
1426 macros << guillemotleft_def << '\n';
1427 if (mustProvide("guillemotright"))
1428 macros << guillemotright_def << '\n';
1429 if (mustProvide("textquotedbl"))
1430 macros << textquotedbl_def << '\n';
1431 if (mustProvide("textquotesinglep")) {
1432 if (runparams_.flavor == OutputParams::XETEX)
1433 macros << textquotesinglep_xetex_def << '\n';
1435 macros << textquotesinglep_luatex_def << '\n';
1437 if (mustProvide("textquotedblp")) {
1438 if (runparams_.flavor == OutputParams::XETEX)
1439 macros << textquotedblp_xetex_def << '\n';
1441 macros << textquotedblp_luatex_def << '\n';
1445 if (mustProvide("binom") && !isRequired("amsmath"))
1446 macros << binom_def << '\n';
1447 if (mustProvide("mathcircumflex"))
1448 macros << mathcircumflex_def << '\n';
1451 if (mustProvide("ParagraphLeftIndent"))
1452 macros << paragraphleftindent_def;
1453 if (mustProvide("NeedLyXFootnoteCode"))
1454 macros << floatingfootnote_def;
1456 // some problems with tex->html converters
1457 if (mustProvide("NeedTabularnewline"))
1458 macros << tabularnewline_def;
1460 // greyed-out environment (note inset)
1461 // the color is specified in the routine
1462 // getColorOptions() to avoid LaTeX-package clashes
1463 if (mustProvide("lyxgreyedout"))
1464 macros << lyxgreyedout_def;
1466 if (mustProvide("lyxdot"))
1467 macros << lyxdot_def << '\n';
1470 getFloatDefinitions(macros);
1472 if (mustProvide("refstyle"))
1473 macros << lyxref_def << '\n';
1476 if (mustProvide("ct-dvipost"))
1477 macros << changetracking_dvipost_def;
1479 if (mustProvide("ct-xcolor-ulem")) {
1480 streamsize const prec = macros.os().precision(2);
1482 RGBColor cadd = rgbFromHexName(lcolor.getX11Name(Color_addedtext));
1483 macros << "\\providecolor{lyxadded}{rgb}{"
1484 << cadd.r / 255.0 << ',' << cadd.g / 255.0 << ',' << cadd.b / 255.0 << "}\n";
1486 RGBColor cdel = rgbFromHexName(lcolor.getX11Name(Color_deletedtext));
1487 macros << "\\providecolor{lyxdeleted}{rgb}{"
1488 << cdel.r / 255.0 << ',' << cdel.g / 255.0 << ',' << cdel.b / 255.0 << "}\n";
1490 macros.os().precision(prec);
1492 if (isRequired("hyperref"))
1493 macros << changetracking_xcolor_ulem_hyperref_def;
1495 macros << changetracking_xcolor_ulem_def;
1498 if (mustProvide("ct-tikz-math-sout"))
1499 macros << changetracking_tikz_math_sout_def;
1501 if (mustProvide("ct-none"))
1502 macros << changetracking_none_def;
1504 if (mustProvide("rtloutputdblcol"))
1505 macros << rtloutputdblcol_def;
1507 if (mustProvide("lyxmintcaption"))
1508 macros << lyxmintcaption_def;
1510 return macros.release();
1514 docstring const LaTeXFeatures::getBabelPresettings() const
1516 odocstringstream tmp;
1518 for (Language const * lang : UsedLanguages_)
1519 if (!lang->babel_presettings().empty())
1520 tmp << lang->babel_presettings() << '\n';
1521 if (!params_.language->babel_presettings().empty())
1522 tmp << params_.language->babel_presettings() << '\n';
1524 if (!contains(tmp.str(), '@'))
1527 return "\\makeatletter\n" + tmp.str() + "\\makeatother\n";
1531 docstring const LaTeXFeatures::getBabelPostsettings() const
1533 odocstringstream tmp;
1535 for (Language const * lang : UsedLanguages_)
1536 if (!lang->babel_postsettings().empty())
1537 tmp << lang->babel_postsettings() << '\n';
1538 if (!params_.language->babel_postsettings().empty())
1539 tmp << params_.language->babel_postsettings() << '\n';
1541 if (!contains(tmp.str(), '@'))
1544 return "\\makeatletter\n" + tmp.str() + "\\makeatother\n";
1548 bool LaTeXFeatures::needBabelLangOptions() const
1550 if (!lyxrc.language_global_options || params_.language->asBabelOptions())
1553 LanguageList::const_iterator it = UsedLanguages_.begin();
1554 LanguageList::const_iterator end = UsedLanguages_.end();
1555 for (; it != end; ++it)
1556 if ((*it)->asBabelOptions())
1563 string const LaTeXFeatures::loadAMSPackages() const
1567 if (mustProvide("amsmath")
1568 && params_.use_package("amsmath") != BufferParams::package_off) {
1569 tmp << "\\usepackage{amsmath}\n";
1571 // amsbsy and amstext are already provided by amsmath
1572 if (mustProvide("amsbsy"))
1573 tmp << "\\usepackage{amsbsy}\n";
1574 if (mustProvide("amstext"))
1575 tmp << "\\usepackage{amstext}\n";
1578 if (mustProvide("amsthm"))
1579 tmp << "\\usepackage{amsthm}\n";
1581 if (mustProvide("amssymb")
1582 && params_.use_package("amssymb") != BufferParams::package_off)
1583 tmp << "\\usepackage{amssymb}\n";
1589 docstring const LaTeXFeatures::getTClassPreamble() const
1591 // the text class specific preamble
1592 DocumentClass const & tclass = params_.documentClass();
1593 odocstringstream tcpreamble;
1595 tcpreamble << tclass.preamble();
1597 list<docstring>::const_iterator cit = usedLayouts_.begin();
1598 list<docstring>::const_iterator end = usedLayouts_.end();
1599 for (; cit != end; ++cit)
1600 // For InPreamble layouts, we output the preamble stuff earlier
1601 // (before the layouts). See Paragraph::Private::validate.
1602 if (!tclass[*cit].inpreamble)
1603 tcpreamble << tclass[*cit].preamble();
1605 cit = usedInsetLayouts_.begin();
1606 end = usedInsetLayouts_.end();
1607 TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1608 for (; cit != end; ++cit) {
1609 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1610 if (it == ils.end())
1612 tcpreamble << it->second.preamble();
1615 return tcpreamble.str();
1619 docstring const LaTeXFeatures::getTClassHTMLPreamble() const
1621 DocumentClass const & tclass = params_.documentClass();
1622 odocstringstream tcpreamble;
1624 tcpreamble << tclass.htmlpreamble();
1626 list<docstring>::const_iterator cit = usedLayouts_.begin();
1627 list<docstring>::const_iterator end = usedLayouts_.end();
1628 for (; cit != end; ++cit)
1629 tcpreamble << tclass[*cit].htmlpreamble();
1631 cit = usedInsetLayouts_.begin();
1632 end = usedInsetLayouts_.end();
1633 TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1634 for (; cit != end; ++cit) {
1635 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1636 if (it == ils.end())
1638 tcpreamble << it->second.htmlpreamble();
1641 return tcpreamble.str();
1645 docstring const LaTeXFeatures::getTClassHTMLStyles() const
1647 DocumentClass const & tclass = params_.documentClass();
1648 odocstringstream tcpreamble;
1650 if (mustProvide("noun"))
1651 tcpreamble << lyxnoun_style;
1652 // this isn't exact, but it won't hurt that much if it
1654 if (mustProvide("ulem"))
1655 tcpreamble << lyxstrikeout_style;
1657 tcpreamble << tclass.htmlstyles();
1659 list<docstring>::const_iterator cit = usedLayouts_.begin();
1660 list<docstring>::const_iterator end = usedLayouts_.end();
1661 for (; cit != end; ++cit)
1662 tcpreamble << tclass[*cit].htmlstyle();
1664 cit = usedInsetLayouts_.begin();
1665 end = usedInsetLayouts_.end();
1666 TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1667 for (; cit != end; ++cit) {
1668 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1669 if (it == ils.end())
1671 tcpreamble << it->second.htmlstyle();
1674 return tcpreamble.str();
1680 docstring const getFloatI18nPreamble(docstring const & type,
1681 docstring const & name, Language const * lang,
1682 Encoding const & enc, bool const polyglossia)
1684 // Check whether name can be encoded in the buffer encoding
1685 bool encodable = true;
1686 for (size_t i = 0; i < name.size(); ++i) {
1687 if (!enc.encodable(name[i])) {
1693 docstring const language = polyglossia ? from_ascii(lang->polyglossia())
1694 : from_ascii(lang->babel());
1695 docstring const langenc = from_ascii(lang->encoding()->iconvName());
1696 docstring const texenc = from_ascii(lang->encoding()->latexName());
1697 docstring const bufenc = from_ascii(enc.iconvName());
1698 docstring const s1 = docstring(1, 0xF0000);
1699 docstring const s2 = docstring(1, 0xF0001);
1700 docstring const translated = encodable ? name
1701 : from_ascii("\\inputencoding{") + texenc + from_ascii("}")
1702 + s1 + langenc + s2 + name + s1 + bufenc + s2;
1704 odocstringstream os;
1705 os << "\\addto\\captions" << language
1706 << "{\\renewcommand{\\" << type << "name}{" << translated << "}}\n";
1711 docstring const i18npreamble(docstring const & templ, Language const * lang,
1712 Encoding const & enc, bool const polyglossia,
1713 bool const need_fixedwidth)
1718 string preamble = polyglossia ?
1719 subst(to_utf8(templ), "$$lang", lang->polyglossia()) :
1720 subst(to_utf8(templ), "$$lang", lang->babel());
1722 string const langenc = lang->encoding()->iconvName();
1723 string const texenc = lang->encoding()->latexName();
1724 string const bufenc = enc.iconvName();
1725 Encoding const * testenc(&enc);
1726 bool lang_fallback = false;
1727 bool ascii_fallback = false;
1728 if (need_fixedwidth && !enc.hasFixedWidth()) {
1729 if (lang->encoding()->hasFixedWidth()) {
1730 testenc = lang->encoding();
1731 lang_fallback = true;
1733 // We need a fixed width encoding, but both the buffer
1734 // encoding and the language encoding are variable
1735 // width. As a last fallback, try to convert to pure
1736 // ASCII using the LaTeX commands defined in unicodesymbols.
1737 testenc = encodings.fromLyXName("ascii");
1740 ascii_fallback = true;
1743 // First and second character of plane 15 (Private Use Area)
1744 string const s1 = "\xf3\xb0\x80\x80"; // U+F0000
1745 string const s2 = "\xf3\xb0\x80\x81"; // U+F0001
1747 // lyx::regex is not unicode-safe.
1748 // Should use QRegExp or (boost::u32regex, but that requires ICU)
1749 static regex const reg("_\\(([^\\)]+)\\)");
1751 while (regex_search(preamble, sub, reg)) {
1752 string const key = sub.str(1);
1753 docstring const name = lang->translateLayout(key);
1754 // Check whether name can be encoded in the buffer encoding
1755 bool encodable = true;
1756 for (size_t i = 0; i < name.size() && encodable; ++i)
1757 if (!testenc->encodable(name[i]))
1760 if (encodable && !lang_fallback)
1761 translated = to_utf8(name);
1762 else if (ascii_fallback)
1763 translated = to_ascii(testenc->latexString(name).first);
1765 translated = "\\inputencoding{" + texenc + "}"
1766 + s1 + langenc + s2 + to_utf8(name)
1768 preamble = subst(preamble, sub.str(), translated);
1770 return from_utf8(preamble);
1776 docstring const LaTeXFeatures::getTClassI18nPreamble(bool use_babel,
1777 bool use_polyglossia, bool use_minted) const
1779 DocumentClass const & tclass = params_.documentClass();
1780 // collect preamble snippets in a set to prevent multiple identical
1781 // commands (would happen if e.g. both theorem and theorem* are used)
1782 set<docstring> snippets;
1783 typedef LanguageList::const_iterator lang_it;
1784 lang_it const lbeg = UsedLanguages_.begin();
1785 lang_it const lend = UsedLanguages_.end();
1786 list<docstring>::const_iterator cit = usedLayouts_.begin();
1787 list<docstring>::const_iterator end = usedLayouts_.end();
1788 for (; cit != end; ++cit) {
1789 // language dependent commands (once per document)
1790 snippets.insert(i18npreamble(tclass[*cit].langpreamble(),
1791 buffer().language(),
1792 buffer().params().encoding(),
1793 use_polyglossia, false));
1794 // commands for language changing (for multilanguage documents)
1795 if ((use_babel || use_polyglossia) && !UsedLanguages_.empty()) {
1796 snippets.insert(i18npreamble(
1797 tclass[*cit].babelpreamble(),
1798 buffer().language(),
1799 buffer().params().encoding(),
1800 use_polyglossia, false));
1801 for (lang_it lit = lbeg; lit != lend; ++lit)
1802 snippets.insert(i18npreamble(
1803 tclass[*cit].babelpreamble(),
1805 buffer().params().encoding(),
1806 use_polyglossia, false));
1809 if ((use_babel || use_polyglossia) && !UsedLanguages_.empty()) {
1810 FloatList const & floats = params_.documentClass().floats();
1811 UsedFloats::const_iterator fit = usedFloats_.begin();
1812 UsedFloats::const_iterator fend = usedFloats_.end();
1813 for (; fit != fend; ++fit) {
1814 Floating const & fl = floats.getType(fit->first);
1815 // we assume builtin floats are translated
1816 if (fl.isPredefined())
1818 docstring const type = from_ascii(fl.floattype());
1819 docstring const flname = from_utf8(fl.name());
1820 docstring name = buffer().language()->translateLayout(fl.name());
1821 // only request translation if we have a real translation
1822 // (that differs from the source)
1824 snippets.insert(getFloatI18nPreamble(
1825 type, name, buffer().language(),
1826 buffer().params().encoding(),
1828 for (lang_it lit = lbeg; lit != lend; ++lit) {
1829 string const code = (*lit)->code();
1830 name = (*lit)->translateLayout(fl.name());
1831 // we assume we have a suitable translation if
1832 // either the language is English (we need to
1833 // translate into English if English is a secondary
1834 // language) or if translateIfPossible returns
1835 // something different to the English source.
1836 bool const have_translation =
1837 (flname != name || contains(code, "en"));
1838 if (have_translation)
1839 snippets.insert(getFloatI18nPreamble(
1841 buffer().params().encoding(),
1847 cit = usedInsetLayouts_.begin();
1848 end = usedInsetLayouts_.end();
1849 TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1850 for (; cit != end; ++cit) {
1851 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1852 if (it == ils.end())
1854 // The listings package does not work with variable width
1855 // encodings, only with fixed width encodings. Therefore we
1856 // need to force a fixed width encoding for
1857 // \lstlistlistingname and \lstlistingname (bug 9382).
1858 // This needs to be consistent with InsetListings::latex().
1859 bool const need_fixedwidth = !use_minted &&
1860 !runparams_.isFullUnicode() &&
1861 it->second.fixedwidthpreambleencoding();
1862 // language dependent commands (once per document)
1863 snippets.insert(i18npreamble(it->second.langpreamble(),
1864 buffer().language(),
1865 buffer().params().encoding(),
1866 use_polyglossia, need_fixedwidth));
1867 // commands for language changing (for multilanguage documents)
1868 if ((use_babel || use_polyglossia) && !UsedLanguages_.empty()) {
1869 snippets.insert(i18npreamble(
1870 it->second.babelpreamble(),
1871 buffer().language(),
1872 buffer().params().encoding(),
1873 use_polyglossia, need_fixedwidth));
1874 for (lang_it lit = lbeg; lit != lend; ++lit)
1875 snippets.insert(i18npreamble(
1876 it->second.babelpreamble(),
1878 buffer().params().encoding(),
1879 use_polyglossia, need_fixedwidth));
1883 odocstringstream tcpreamble;
1884 set<docstring>::const_iterator const send = snippets.end();
1885 set<docstring>::const_iterator it = snippets.begin();
1886 for (; it != send; ++it)
1888 return tcpreamble.str();
1892 docstring const LaTeXFeatures::getLyXSGMLEntities() const
1894 // Definition of entities used in the document that are LyX related.
1895 odocstringstream entities;
1897 if (mustProvide("lyxarrow")) {
1898 entities << "<!ENTITY lyxarrow \"->\">" << '\n';
1901 return entities.str();
1905 docstring const LaTeXFeatures::getIncludedFiles(string const & fname) const
1907 odocstringstream sgmlpreamble;
1909 docstring const basename(from_utf8(onlyPath(fname)));
1911 FileMap::const_iterator end = IncludedFiles_.end();
1912 for (FileMap::const_iterator fi = IncludedFiles_.begin();
1915 sgmlpreamble << "\n<!ENTITY " << fi->first
1916 << (isSGMLFileName(fi->second) ? " SYSTEM \"" : " \"")
1917 << makeRelPath(from_utf8(fi->second), basename) << "\">";
1919 return sgmlpreamble.str();
1923 void LaTeXFeatures::showStruct() const
1925 lyxerr << "LyX needs the following commands when LaTeXing:"
1926 << "\n***** Packages:" << getPackages()
1927 << "\n***** Macros:" << to_utf8(getMacros().str)
1928 << "\n***** Textclass stuff:" << to_utf8(getTClassPreamble())
1929 << "\n***** done." << endl;
1933 Buffer const & LaTeXFeatures::buffer() const
1939 void LaTeXFeatures::setBuffer(Buffer const & buffer)
1945 BufferParams const & LaTeXFeatures::bufferParams() const
1951 void LaTeXFeatures::getFloatDefinitions(otexstream & os) const
1953 FloatList const & floats = params_.documentClass().floats();
1955 // Here we will output the code to create the needed float styles.
1956 // We will try to do this as minimal as possible.
1957 // \floatstyle{ruled}
1958 // \newfloat{algorithm}{htbp}{loa}
1959 // \providecommand{\algorithmname}{Algorithm}
1960 // \floatname{algorithm}{\protect\algorithmname}
1961 UsedFloats::const_iterator cit = usedFloats_.begin();
1962 UsedFloats::const_iterator end = usedFloats_.end();
1963 for (; cit != end; ++cit) {
1964 Floating const & fl = floats.getType(cit->first);
1966 // For builtin floats we do nothing.
1967 if (fl.isPredefined())
1970 // We have to special case "table" and "figure"
1971 if (fl.floattype() == "tabular" || fl.floattype() == "figure") {
1972 // Output code to modify "table" or "figure"
1973 // but only if builtin == false
1974 // and that have to be true at this point in the
1976 docstring const type = from_ascii(fl.floattype());
1977 docstring const placement = from_ascii(fl.placement());
1978 docstring const style = from_ascii(fl.style());
1979 if (!style.empty()) {
1980 os << "\\floatstyle{" << style << "}\n"
1981 << "\\restylefloat{" << type << "}\n";
1983 if (!placement.empty()) {
1984 os << "\\floatplacement{" << type << "}{"
1985 << placement << "}\n";
1988 // The other non builtin floats.
1990 docstring const type = from_ascii(fl.floattype());
1991 docstring const placement = from_ascii(fl.placement());
1992 docstring const ext = from_ascii(fl.ext());
1993 docstring const within = from_ascii(fl.within());
1994 docstring const style = from_ascii(fl.style());
1995 docstring const name =
1996 buffer().language()->translateLayout(fl.name());
1997 os << "\\floatstyle{" << style << "}\n"
1998 << "\\newfloat{" << type << "}{" << placement
1999 << "}{" << ext << '}';
2000 if (!within.empty())
2001 os << '[' << within << ']';
2003 << "\\providecommand{\\" << type << "name}{"
2005 << "\\floatname{" << type << "}{\\protect\\"
2006 << type << "name}\n";
2008 // What missing here is to code to minimalize the code
2009 // output so that the same floatstyle will not be
2010 // used several times, when the same style is still in
2014 // The subfig package is loaded later
2015 os << "\n\\AtBeginDocument{\\newsubfloat{" << from_ascii(fl.floattype()) << "}}\n";
2020 void LaTeXFeatures::resolveAlternatives()
2022 for (Features::iterator it = features_.begin(); it != features_.end();) {
2023 if (contains(*it, '|')) {
2024 vector<string> const alternatives = getVectorFromString(*it, "|");
2025 vector<string>::const_iterator const end = alternatives.end();
2026 vector<string>::const_iterator ita = alternatives.begin();
2027 // Is any alternative already required? => use that
2028 for (; ita != end; ++ita) {
2029 if (isRequired(*ita))
2032 // Is any alternative available? => use the first one
2035 for (ita = alternatives.begin(); ita != end; ++ita) {
2036 if (isAvailable(*ita)) {
2042 // This will not work, but not requiring something
2043 // would be more confusing
2045 require(alternatives.front());
2046 features_.erase(it);
2047 it = features_.begin();
2054 void LaTeXFeatures::expandMultiples()
2056 for (Features::iterator it = features_.begin(); it != features_.end();) {
2057 if (contains(*it, ',')) {
2058 vector<string> const multiples = getVectorFromString(*it, ",");
2059 vector<string>::const_iterator const end = multiples.end();
2060 vector<string>::const_iterator itm = multiples.begin();
2061 // Do nothing if any multiple is already required
2062 for (; itm != end; ++itm) {
2063 if (!isRequired(*itm))
2066 features_.erase(it);
2067 it = features_.begin();