]> git.lyx.org Git - lyx.git/blob - src/LaTeXFeatures.cpp
bf8cf24f0744fdfec7856af72a829f7cb057863f
[lyx.git] / src / LaTeXFeatures.cpp
1 /**
2  * \file LaTeXFeatures.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author José Matos
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author Jürgen Vigna
10  * \author André Pönitz
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16
17 #include "LaTeXFeatures.h"
18
19 #include "Buffer.h"
20 #include "BufferParams.h"
21 #include "ColorSet.h"
22 #include "Converter.h"
23 #include "Encoding.h"
24 #include "Floating.h"
25 #include "FloatList.h"
26 #include "Language.h"
27 #include "LaTeXFonts.h"
28 #include "LaTeXPackages.h"
29 #include "Layout.h"
30 #include "Lexer.h"
31 #include "LyXRC.h"
32 #include "TextClass.h"
33 #include "TexRow.h"
34 #include "texstream.h"
35
36 #include "insets/InsetLayout.h"
37
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"
45
46 #include <algorithm>
47
48
49 using namespace std;
50 using namespace lyx::support;
51
52
53 namespace lyx {
54
55 /////////////////////////////////////////////////////////////////////
56 //
57 // Strings
58 //
59 /////////////////////////////////////////////////////////////////////
60
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.}
64
65 static docstring const lyx_def = from_ascii(
66         "\\providecommand{\\LyX}{L\\kern-.1667em\\lower.25em\\hbox{Y}\\kern-.125emX\\@}");
67
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"
71         "  {LyX}}");
72
73 static docstring const noun_def = from_ascii(
74         "\\newcommand{\\noun}[1]{\\textsc{#1}}");
75
76 static docstring const lyxarrow_def = from_ascii(
77         "\\DeclareRobustCommand*{\\lyxarrow}{%\n"
78         "\\@ifstar\n"
79         "{\\leavevmode\\,$\\triangleleft$\\,\\allowbreak}\n"
80         "{\\leavevmode\\,$\\triangleright$\\,\\allowbreak}}");
81
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}}");
90
91 // for quotes without babel. This does not give perfect results, but
92 // anybody serious about non-english quotes should use babel (JMarc).
93
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"
98         "}");
99
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"
104         "}");
105
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"
110         "}");
111
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"
116         "}");
117
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"
122         "}");
123
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"
128         "}");
129
130 static docstring const textquotedbl_def = from_ascii(
131         "\\DeclareTextSymbolDefault{\\textquotedbl}{T1}");
132
133 static docstring const textquotedblp_xetex_def = from_ascii(
134         "\\providecommand\\textquotedblplain{%\n"
135         "  \\bgroup\\addfontfeatures{Mapping=}\\char34\\egroup}");
136
137 static docstring const textquotedblp_luatex_def = from_ascii(
138         "\\providecommand\\textquotedblplain{%\n"
139         "  \\bgroup\\addfontfeatures{RawFeature=-tlig}\\char34\\egroup}");
140
141 static docstring const textquotesinglep_xetex_def = from_ascii(
142         "\\providecommand\\textquotesingleplain{%\n"
143         "  \\bgroup\\addfontfeatures{Mapping=}\\char39\\egroup}");
144
145 static docstring const textquotesinglep_luatex_def = from_ascii(
146         "\\providecommand\\textquotesingleplain{%\n"
147         "  \\bgroup\\addfontfeatures{RawFeature=-tlig}\\char39\\egroup}");
148
149 static docstring const paragraphleftindent_def = from_ascii(
150         "\\newenvironment{LyXParagraphLeftIndent}[1]%\n"
151         "{\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"
162         "  }\n"
163         "  \\item[]\n"
164         "}\n"
165         "{\\end{list}}\n");
166
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"
173         "  \\else\n"
174         "    \\expandafter\\SF@gobble@opt\n"
175         "  \\fi\n"
176         "}\n"
177         "\\expandafter\\def\\csname SF@gobble@opt \\endcsname{\\@ifnextchar[%]\n"
178         "  \\SF@gobble@twobracket\n"
179         "  \\@gobble\n"
180         "}\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");
184
185 static docstring const binom_def = from_ascii(
186         "%% Binom macro for standard LaTeX users\n"
187         "\\newcommand{\\binom}[2]{{#1 \\choose #2}}\n");
188
189 static docstring const mathcircumflex_def = from_ascii(
190         "%% For printing a cirumflex inside a formula\n"
191         "\\newcommand{\\mathcircumflex}[0]{\\mbox{\\^{}}}\n");
192
193 static docstring const tabularnewline_def = from_ascii(
194         "%% Because html converters don't know tabularnewline\n"
195         "\\providecommand{\\tabularnewline}{\\\\}\n");
196
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");
202
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");
209
210 static docstring const changetracking_dvipost_def = from_ascii(
211         "%% Change tracking with dvipost\n"
212         "\\dvipostlayout\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");
220
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");
226
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");
232
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"
241         "  }\n"
242         "}\n");
243
244 static docstring const changetracking_none_def = from_ascii(
245         "\\newcommand{\\lyxadded}[3]{#3}\n"
246         "\\newcommand{\\lyxdeleted}[3]{}\n");
247
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");
255
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");
263
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");
267
268 static docstring const papersizedvi_def = from_ascii(
269         "\\special{papersize=\\the\\paperwidth,\\the\\paperheight}\n");
270
271 static docstring const papersizepdf_def = from_ascii(
272         "\\pdfpageheight\\paperheight\n"
273         "\\pdfpagewidth\\paperwidth\n");
274
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");
281
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");
285
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");
289
290 static docstring const subdot_def = from_ascii(
291         "\\newcommand{\\dosubdot}[2]{\\underaccent{#1.}{#2}}\n"
292         "\\newcommand{\\subdot}[1]{\\mathpalette\\dosubdot{#1}}\n");
293
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");
297
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");
301
302 static docstring const dacute_def = from_ascii(
303         "\\DeclareMathAccent{\\dacute}{\\mathalpha}{operators}{'175}\n");
304
305 static docstring const tipasymb_def = from_ascii(
306         "\\DeclareFontEncoding{T3}{}{}\n"
307         "\\DeclareSymbolFont{tipasymb}{T3}{cmr}{m}{n}\n");
308
309 static docstring const dgrave_def = from_ascii(
310         "\\DeclareMathAccent{\\dgrave}{\\mathord}{tipasymb}{'15}\n");
311
312 static docstring const rcap_def = from_ascii(
313         "\\DeclareMathAccent{\\rcap}{\\mathord}{tipasymb}{'20}\n");
314
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");
321
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"
330         "}\n");
331
332 static docstring const textcommabelow_def = from_ascii(
333         "\\ProvideTextCommandDefault{\\textcommabelow}[1]{%%\n"
334         "  \\LyxTextAccent[-.31ex]{\\LyxAccentSize,}{#1}}\n");
335
336 static docstring const textcommaabove_def = from_ascii(
337         "\\ProvideTextCommandDefault{\\textcommaabove}[1]{%%\n"
338         "  \\LyxTextAccent[.5ex]{\\LyxAccentSize`}{#1}}\n");
339
340 static docstring const textcommaaboveright_def = from_ascii(
341         "\\ProvideTextCommandDefault{\\textcommaaboveright}[1]{%%\n"
342         "  \\LyxTextAccent[.5ex]{\\LyxAccentSize\\ '}{#1}}\n");
343
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");
357
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");
374
375 static docstring const lyxref_def = from_ascii(
376                 "\\RS@ifundefined{subsecref}\n"
377                 "  {\\newref{subsec}{name = \\RSsectxt}}\n"
378                 "  {}\n"
379                 "\\RS@ifundefined{thmref}\n"
380                 "  {\\def\\RSthmtxt{theorem~}\\newref{thm}{name = \\RSthmtxt}}\n"
381                 "  {}\n"
382                 "\\RS@ifundefined{lemref}\n"
383                 "  {\\def\\RSlemtxt{lemma~}\\newref{lem}{name = \\RSlemtxt}}\n"
384                 "  {}\n");
385
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"
392         "  \\else\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"
400         "      \\hfil\n"
401         "      {\\normalcolor\\vrule \\@width\\columnseprule}%\n"
402         "      \\hfil\n"
403         "      \\kern-\\textwidth  %**\n"
404         "      \\hb@xt@\\columnwidth {%\n"
405         "         \\box\\@outputbox \\hss}%\n"
406         "      \\kern-\\columnwidth \\kern\\textwidth %**\n"
407         "    }%\n"
408         "  }%\n"
409         "  \\@combinedblfloats\n"
410         "  \\@outputpage\n"
411         "  \\begingroup\n"
412         "  \\@dblfloatplacement\n"
413         "  \\@startdblcolumn\n"
414         "  \\@whilesw\\if@fcolmade \\fi\n"
415         "  {\\@outputpage\n"
416         "    \\@startdblcolumn}%\n"
417         "  \\endgroup\n"
418         "  \\fi\n"
419         "}\n"
420         "\\@mparswitchtrue\n");
421
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         "  \\setbox\\@tempboxa\\hbox{\\listingscaption~\\thelisting: #2}%\n"
427         "  \\ifdim \\wd\\@tempboxa >\\linewidth%\n"
428         "  \\parbox[t]{\\linewidth}{\\unhbox\\@tempboxa}\\else%\n"
429         "  \\hbox to\\linewidth{\\hfil\\box\\@tempboxa\\hfil}\\fi%\n"
430         "  \\ifx#1b\\vskip\\baselineskip\\fi\n"
431         "}\n");
432
433
434 /////////////////////////////////////////////////////////////////////
435 //
436 // LyXHTML strings
437 //
438 /////////////////////////////////////////////////////////////////////
439
440 static docstring const lyxnoun_style = from_ascii(
441         "dfn.lyxnoun {\n"
442         "  font-variant: small-caps;\n"
443         "}\n");
444
445
446 // this is how it normally renders, but it might not always do so.
447 static docstring const lyxstrikeout_style = from_ascii(
448         "del.strikeout {\n"
449         "  text-decoration: line-through;\n"
450         "}\n");
451
452
453 /////////////////////////////////////////////////////////////////////
454 //
455 // LaTeXFeatures
456 //
457 /////////////////////////////////////////////////////////////////////
458
459
460 LaTeXFeatures::LaTeXFeatures(Buffer const & b, BufferParams const & p,
461                              OutputParams const & r)
462         : buffer_(&b), params_(p), runparams_(r), in_float_(false),
463           in_deleted_inset_(false)
464 {}
465
466
467 LaTeXFeatures::LangPackage LaTeXFeatures::langPackage() const
468 {
469         string const local_lp = bufferParams().lang_package;
470
471         // Locally, custom is just stored as a string
472         // in bufferParams().lang_package.
473         if (local_lp != "auto"
474             && local_lp != "babel"
475             && local_lp != "default"
476             && local_lp != "none")
477                  return LANG_PACK_CUSTOM;
478
479         if (local_lp == "none")
480                 return LANG_PACK_NONE;
481
482         /* If "auto" is selected, we load polyglossia with non-TeX fonts,
483          * else we select babel.
484          * If babel is selected (either directly or via the "auto"
485          * mechanism), we really do only require it if we have
486          * a language that needs it.
487          */
488         bool const polyglossia_required =
489                 params_.useNonTeXFonts
490                 && isAvailable("polyglossia")
491                 && !isProvided("babel")
492                 && this->hasOnlyPolyglossiaLanguages();
493         bool const babel_required =
494                 !bufferParams().language->babel().empty()
495                 || !this->getBabelLanguages().empty();
496
497         if (local_lp == "auto") {
498                 // polyglossia requirement has priority over babel
499                 if (polyglossia_required)
500                         return LANG_PACK_POLYGLOSSIA;
501                 else if (babel_required)
502                         return LANG_PACK_BABEL;
503         }
504
505         if (local_lp == "babel") {
506                 if (babel_required)
507                         return LANG_PACK_BABEL;
508         }
509
510         if (local_lp == "default") {
511                 switch (lyxrc.language_package_selection) {
512                 case LyXRC::LP_AUTO:
513                         // polyglossia requirement has priority over babel
514                         if (polyglossia_required)
515                                 return LANG_PACK_POLYGLOSSIA;
516                         else if (babel_required)
517                                 return LANG_PACK_BABEL;
518                         break;
519                 case LyXRC::LP_BABEL:
520                         if (babel_required)
521                                 return LANG_PACK_BABEL;
522                         break;
523                 case LyXRC::LP_CUSTOM:
524                         return LANG_PACK_CUSTOM;
525                 case LyXRC::LP_NONE:
526                         return LANG_PACK_NONE;
527                 }
528         }
529
530         return LANG_PACK_NONE;
531 }
532
533
534 void LaTeXFeatures::require(string const & name)
535 {
536         features_.insert(name);
537 }
538
539
540 void LaTeXFeatures::require(set<string> const & names)
541 {
542         features_.insert(names.begin(), names.end());
543 }
544
545
546 void LaTeXFeatures::useLayout(docstring const & layoutname)
547 {
548         useLayout(layoutname, 0);
549 }
550
551
552 void LaTeXFeatures::useLayout(docstring const & layoutname, int level)
553 {
554         // Some code to avoid loops in dependency definition
555         const int maxlevel = 30;
556         if (level > maxlevel) {
557                 lyxerr << "LaTeXFeatures::useLayout: maximum level of "
558                        << "recursion attained by layout "
559                        << to_utf8(layoutname) << endl;
560                 return;
561         }
562
563         DocumentClass const & tclass = params_.documentClass();
564         if (tclass.hasLayout(layoutname)) {
565                 // Is this layout already in usedLayouts?
566                 if (find(usedLayouts_.begin(), usedLayouts_.end(), layoutname)
567                     != usedLayouts_.end())
568                         return;
569
570                 Layout const & layout = tclass[layoutname];
571                 require(layout.requires());
572
573                 if (!layout.depends_on().empty()) {
574                         useLayout(layout.depends_on(), level + 1);
575                 }
576                 usedLayouts_.push_back(layoutname);
577         } else {
578                 lyxerr << "LaTeXFeatures::useLayout: layout `"
579                        << to_utf8(layoutname) << "' does not exist in this class"
580                        << endl;
581         }
582 }
583
584
585 void LaTeXFeatures::useInsetLayout(InsetLayout const & lay)
586 {
587         docstring const & lname = lay.name();
588         DocumentClass const & tclass = params_.documentClass();
589
590         // this is a default inset layout, nothing useful here
591         if (!tclass.hasInsetLayout(lname))
592                 return;
593         // Is this layout already in usedInsetLayouts?
594         if (find(usedInsetLayouts_.begin(), usedInsetLayouts_.end(), lname)
595                         != usedInsetLayouts_.end())
596                 return;
597
598         require(lay.requires());
599         usedInsetLayouts_.push_back(lname);
600 }
601
602
603 bool LaTeXFeatures::isRequired(string const & name) const
604 {
605         return features_.find(name) != features_.end();
606 }
607
608
609 bool LaTeXFeatures::isProvided(string const & name) const
610 {
611         if (params_.useNonTeXFonts)
612                 return params_.documentClass().provides(name);
613
614         bool const ot1 = (params_.main_font_encoding() == "default"
615                 || params_.main_font_encoding() == "OT1");
616         bool const complete = (params_.fontsSans() == "default"
617                 && params_.fontsTypewriter() == "default");
618         bool const nomath = (params_.fontsMath() == "default");
619         return params_.documentClass().provides(name)
620                 || theLaTeXFonts().getLaTeXFont(
621                         from_ascii(params_.fontsRoman())).provides(name, ot1,
622                                                                   complete,
623                                                                   nomath)
624                 || theLaTeXFonts().getLaTeXFont(
625                         from_ascii(params_.fontsSans())).provides(name, ot1,
626                                                                  complete,
627                                                                  nomath)
628                 || theLaTeXFonts().getLaTeXFont(
629                         from_ascii(params_.fontsTypewriter())).provides(name, ot1,
630                                                                        complete,
631                                                                        nomath)
632                 || theLaTeXFonts().getLaTeXFont(
633                         from_ascii(params_.fontsMath())).provides(name, ot1,
634                                                                        complete,
635                                                                        nomath);
636         // TODO: "textbaltic" provided, if the font-encoding is "L7x"
637         //       "textgreek" provided, if a language with font-encoding LGR is used in the document
638         //       "textcyr" provided, if a language with font-encoding T2A is used in the document
639 }
640
641
642 bool LaTeXFeatures::mustProvide(string const & name) const
643 {
644         return isRequired(name) && !isProvided(name);
645 }
646
647
648 bool LaTeXFeatures::isAvailable(string const & name)
649 {
650         string::size_type const i = name.find("->");
651         if (i != string::npos) {
652                 string const from = name.substr(0,i);
653                 string const to = name.substr(i+2);
654                 //LYXERR0("from=[" << from << "] to=[" << to << "]");
655                 return theConverters().isReachable(from, to);
656         }
657         return LaTeXPackages::isAvailable(name);
658 }
659
660
661 namespace {
662
663 void addSnippet(std::list<TexString> & list, TexString ts, bool allow_dupes)
664 {
665         if (allow_dupes ||
666             // test the absense of duplicates, i.e. elements with same str
667             none_of(list.begin(), list.end(), [&](TexString const & ts2){
668                             return ts.str == ts2.str;
669                     })
670             )
671                 list.push_back(move(ts));
672 }
673
674
675 TexString getSnippets(std::list<TexString> const & list)
676 {
677         otexstringstream snip;
678         for (TexString const & ts : list)
679                 snip << TexString(ts) << '\n';
680         return snip.release();
681 }
682
683 } //anon namespace
684
685
686 void LaTeXFeatures::addPreambleSnippet(TexString ts, bool allow_dupes)
687 {
688         addSnippet(preamble_snippets_, move(ts), allow_dupes);
689 }
690
691
692 void LaTeXFeatures::addPreambleSnippet(docstring const & str, bool allow_dupes)
693 {
694         addSnippet(preamble_snippets_, TexString(str), allow_dupes);
695 }
696
697
698 void LaTeXFeatures::addCSSSnippet(std::string const & snippet)
699 {
700         addSnippet(css_snippets_, TexString(from_ascii(snippet)), false);
701 }
702
703
704 TexString LaTeXFeatures::getPreambleSnippets() const
705 {
706         return getSnippets(preamble_snippets_);
707 }
708
709
710 docstring LaTeXFeatures::getCSSSnippets() const
711 {
712         return getSnippets(css_snippets_).str;
713 }
714
715
716
717 void LaTeXFeatures::useFloat(string const & name, bool subfloat)
718 {
719         if (!usedFloats_[name])
720                 usedFloats_[name] = subfloat;
721         if (subfloat)
722                 require("subfig");
723         // We only need float.sty if we use non builtin floats, or if we
724         // use the "H" modifier. This includes modified table and
725         // figure floats. (Lgb)
726         Floating const & fl = params_.documentClass().floats().getType(name);
727         if (!fl.floattype().empty() && fl.usesFloatPkg()) {
728                 require("float");
729         }
730 }
731
732
733 void LaTeXFeatures::useLanguage(Language const * lang)
734 {
735         if (!lang->babel().empty() || !lang->polyglossia().empty())
736                 UsedLanguages_.insert(lang);
737         if (!lang->requires().empty())
738                 require(lang->requires());
739         // CJK languages do not have a babel name.
740         // They use the CJK package
741         if (lang->encoding()->package() == Encoding::CJK)
742                 require("CJK");
743         // japanese package is special
744         if (lang->encoding()->package() == Encoding::japanese)
745                 require("japanese");
746 }
747
748
749 void LaTeXFeatures::includeFile(docstring const & key, string const & name)
750 {
751         IncludedFiles_[key] = name;
752 }
753
754
755 bool LaTeXFeatures::hasLanguages() const
756 {
757         return !UsedLanguages_.empty();
758 }
759
760
761 bool LaTeXFeatures::hasOnlyPolyglossiaLanguages() const
762 {
763         // first the main language
764         if (params_.language->polyglossia().empty())
765                 return false;
766         // now the secondary languages
767         LanguageList::const_iterator const begin = UsedLanguages_.begin();
768         for (LanguageList::const_iterator cit = begin;
769              cit != UsedLanguages_.end();
770              ++cit) {
771                 if ((*cit)->polyglossia().empty())
772                         return false;
773         }
774         return true;
775 }
776
777
778 bool LaTeXFeatures::hasPolyglossiaExclusiveLanguages() const
779 {
780         // first the main language
781         if (params_.language->isPolyglossiaExclusive())
782                 return true;
783         // now the secondary languages
784         LanguageList::const_iterator const begin = UsedLanguages_.begin();
785         for (LanguageList::const_iterator cit = begin;
786              cit != UsedLanguages_.end();
787              ++cit) {
788                 if ((*cit)->isPolyglossiaExclusive())
789                         return true;
790         }
791         return false;
792 }
793
794
795 vector<string> LaTeXFeatures::getPolyglossiaExclusiveLanguages() const
796 {
797         vector<string> result;
798         // first the main language
799         if (params_.language->isPolyglossiaExclusive())
800                 result.push_back(params_.language->display());
801         // now the secondary languages
802         LanguageList::const_iterator const begin = UsedLanguages_.begin();
803         for (LanguageList::const_iterator cit = begin;
804              cit != UsedLanguages_.end();
805              ++cit) {
806                 if ((*cit)->isPolyglossiaExclusive())
807                         result.push_back((*cit)->display());
808         }
809         return result;
810 }
811
812
813 vector<string> LaTeXFeatures::getBabelExclusiveLanguages() const
814 {
815         vector<string> result;
816         // first the main language
817         if (params_.language->isBabelExclusive())
818                 result.push_back(params_.language->display());
819         // now the secondary languages
820         LanguageList::const_iterator const begin = UsedLanguages_.begin();
821         for (LanguageList::const_iterator cit = begin;
822              cit != UsedLanguages_.end();
823              ++cit) {
824                 if ((*cit)->isBabelExclusive())
825                         result.push_back((*cit)->display());
826         }
827         return result;
828 }
829
830
831 string LaTeXFeatures::getBabelLanguages() const
832 {
833         ostringstream languages;
834
835         bool first = true;
836         LanguageList::const_iterator const begin = UsedLanguages_.begin();
837         for (LanguageList::const_iterator cit = begin;
838              cit != UsedLanguages_.end();
839              ++cit) {
840                 if ((*cit)->babel().empty())
841                         continue;
842                 if (!first)
843                         languages << ',';
844                 else
845                         first = false;
846                 languages << (*cit)->babel();
847         }
848         return languages.str();
849 }
850
851
852 set<string> LaTeXFeatures::getPolyglossiaLanguages() const
853 {
854         set<string> languages;
855
856         LanguageList::const_iterator const begin = UsedLanguages_.begin();
857         for (LanguageList::const_iterator cit = begin;
858              cit != UsedLanguages_.end();
859              ++cit) {
860                 // We do not need the variants here
861                 languages.insert((*cit)->polyglossia());
862         }
863         return languages;
864 }
865
866
867 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding) const
868 {
869         // This does only find encodings of languages supported by babel, but
870         // that does not matter since we don't have a language with an
871         // encoding supported by inputenc but without babel support.
872         set<string> encodings;
873         LanguageList::const_iterator it  = UsedLanguages_.begin();
874         LanguageList::const_iterator end = UsedLanguages_.end();
875         for (; it != end; ++it)
876                 if ((*it)->encoding()->latexName() != doc_encoding &&
877                     ((*it)->encoding()->package() == Encoding::inputenc
878                      || (*it)->encoding()->package() == Encoding::japanese))
879                         encodings.insert((*it)->encoding()->latexName());
880         return encodings;
881 }
882
883
884 void LaTeXFeatures::getFontEncodings(vector<string> & encodings) const
885 {
886         // these must be loaded if glyphs of this script are used
887         // unless a language providing them is used in the document
888         if (mustProvide("textgreek")
889             && find(encodings.begin(), encodings.end(), "LGR") == encodings.end())
890                 encodings.insert(encodings.begin(), "LGR");
891         if (mustProvide("textcyr")
892             && find(encodings.begin(), encodings.end(), "T2A") == encodings.end())
893                 encodings.insert(encodings.begin(), "T2A");
894
895         LanguageList::const_iterator it  = UsedLanguages_.begin();
896         LanguageList::const_iterator end = UsedLanguages_.end();
897         for (; it != end; ++it)
898                 if (!(*it)->fontenc().empty()
899                     && ascii_lowercase((*it)->fontenc()) != "none") {
900                         vector<string> extraencs = getVectorFromString((*it)->fontenc());
901                         vector<string>::const_iterator fit = extraencs.begin();
902                         for (; fit != extraencs.end(); ++fit) {
903                                 if (find(encodings.begin(), encodings.end(), *fit) == encodings.end())
904                                         encodings.insert(encodings.begin(), *fit);
905                         }
906                 }
907 }
908
909 namespace {
910
911 char const * simplefeatures[] = {
912 // note that the package order here will be the same in the LaTeX-output
913         "array",
914         "verbatim",
915         "longtable",
916         "rotating",
917         "latexsym",
918         "pifont",
919         // subfig is handled in BufferParams.cpp
920         "varioref",
921         "prettyref",
922         "refstyle",
923         /*For a successful cooperation of the `wrapfig' package with the
924           `float' package you should load the `wrapfig' package *after*
925           the `float' package. See the caption package documentation
926           for explanation.*/
927         "float",
928         "rotfloat",
929         "wrapfig",
930         "booktabs",
931         "dvipost",
932         "fancybox",
933         "calc",
934         "units",
935         "framed",
936         "soul",
937         "textcomp",
938         "pmboxdraw",
939         "bbding",
940         "ifsym",
941         "txfonts",
942         "pxfonts",
943         "mathdesign",
944         "mathrsfs",
945         "mathabx",
946         "mathtools",
947         // "cancel",
948         "ascii",
949         "url",
950         "covington",
951         "csquotes",
952         "enumitem",
953         "endnotes",
954         "hhline",
955         "ifthen",
956         // listings is handled in BufferParams.cpp
957         "bm",
958         "pdfpages",
959         "amscd",
960         "slashed",
961         "multicol",
962         "multirow",
963         "tfrupee",
964         "shapepar",
965         "rsphrase",
966         "hpstatement",
967         "algorithm2e",
968         "sectionbox",
969         "tcolorbox",
970         "pdfcomment",
971         "fixme",
972         "todonotes",
973         "forest",
974         "varwidth",
975 };
976
977 char const * bibliofeatures[] = {
978         // Known bibliography packages (will be loaded before natbib)
979         "achicago",
980         "apacite",
981         "apalike",
982         "astron",
983         "authordate1-4",
984         "babelbib",
985         "bibgerm",
986         "chapterbib",
987         "chicago",
988         "chscite",
989         "harvard",
990         "mslapa",
991         "named"
992 };
993
994 int const nb_bibliofeatures = sizeof(bibliofeatures) / sizeof(char const *);
995
996 int const nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
997
998 }
999
1000
1001 string const LaTeXFeatures::getColorOptions() const
1002 {
1003         ostringstream colors;
1004
1005         // Handling the color packages separately is needed to be able to load them
1006         // before babel when hyperref is loaded with the colorlinks option
1007         // for more info see Bufferparams.cpp
1008
1009         // [x]color.sty
1010         if (mustProvide("color") || mustProvide("xcolor")) {
1011                 string const package =
1012                         (mustProvide("xcolor") ? "xcolor" : "color");
1013                 if (params_.graphics_driver == "default"
1014                         || params_.graphics_driver == "none")
1015                         colors << "\\usepackage{" << package << "}\n";
1016                 else
1017                         colors << "\\usepackage["
1018                                  << params_.graphics_driver
1019                                  << "]{" << package << "}\n";
1020         }
1021
1022         // pdfcolmk must be loaded after color
1023         if (mustProvide("pdfcolmk"))
1024                 colors << "\\usepackage{pdfcolmk}\n";
1025
1026         // the following 3 color commands must be set after color
1027         // is loaded and before pdfpages, therefore add the command
1028         // here define the set color
1029         if (mustProvide("pagecolor")) {
1030                 colors << "\\definecolor{page_backgroundcolor}{rgb}{";
1031                 colors << outputLaTeXColor(params_.backgroundcolor) << "}\n";
1032                 // set the page color
1033                 colors << "\\pagecolor{page_backgroundcolor}\n";
1034         }
1035
1036         if (mustProvide("fontcolor")) {
1037                 colors << "\\definecolor{document_fontcolor}{rgb}{";
1038                 colors << outputLaTeXColor(params_.fontcolor) << "}\n";
1039                 // set the color
1040                 colors << "\\color{document_fontcolor}\n";
1041         }
1042
1043         if (mustProvide("lyxgreyedout")) {
1044                 colors << "\\definecolor{note_fontcolor}{rgb}{";
1045                 colors << outputLaTeXColor(params_.notefontcolor) << "}\n";
1046                 // the color will be set together with the definition of
1047                 // the lyxgreyedout environment (see lyxgreyedout_def)
1048         }
1049
1050         // color for shaded boxes
1051         if (isRequired("framed") && mustProvide("color")) {
1052                 colors << "\\definecolor{shadecolor}{rgb}{";
1053                 colors << outputLaTeXColor(params_.boxbgcolor) << "}\n";
1054                 // this color is automatically used by the LaTeX-package "framed"
1055         }
1056
1057         return colors.str();
1058 }
1059
1060
1061 string const LaTeXFeatures::getPackageOptions() const
1062 {
1063         ostringstream packageopts;
1064         // Output all the package option stuff we have been asked to do.
1065         map<string, string>::const_iterator it =
1066                 params_.documentClass().packageOptions().begin();
1067         map<string, string>::const_iterator en =
1068                 params_.documentClass().packageOptions().end();
1069         for (; it != en; ++it)
1070                 if (mustProvide(it->first))
1071                         packageopts << "\\PassOptionsToPackage{" << it->second << "}"
1072                                  << "{" << it->first << "}\n";
1073         return packageopts.str();
1074 }
1075
1076
1077 string const LaTeXFeatures::getPackages() const
1078 {
1079         ostringstream packages;
1080
1081         // FIXME: currently, we can only load packages and macros known
1082         // to LyX.
1083         // However, with the Require tag of layouts/custom insets,
1084         // also unknown packages can be requested. They are silently
1085         // swallowed now. We should change this eventually.
1086
1087         //  These are all the 'simple' includes.  i.e
1088         //  packages which we just \usepackage{package}
1089         for (int i = 0; i < nb_simplefeatures; ++i) {
1090                 if (mustProvide(simplefeatures[i]))
1091                         packages << "\\usepackage{" << simplefeatures[i] << "}\n";
1092         }
1093
1094         // The rest of these packages are somewhat more complicated
1095         // than those above.
1096
1097         // The tipa package and its extensions (tipx, tone) must not
1098         // be loaded with non-TeX fonts, since fontspec includes the
1099         // respective macros
1100         if (mustProvide("tipa") && !params_.useNonTeXFonts)
1101                 packages << "\\usepackage{tipa}\n";
1102         if (mustProvide("tipx") && !params_.useNonTeXFonts)
1103                 packages << "\\usepackage{tipx}\n";
1104         if (mustProvide("extraipa") && !params_.useNonTeXFonts)
1105                 packages << "\\usepackage{extraipa}\n";
1106         if (mustProvide("tone") && !params_.useNonTeXFonts)
1107                 packages << "\\usepackage{tone}\n";
1108
1109         // if fontspec or newtxmath is used, AMS packages have to be loaded
1110         // before fontspec (in BufferParams)
1111         string const amsPackages = loadAMSPackages();
1112         bool const ot1 = (params_.main_font_encoding() == "default"
1113                           || params_.main_font_encoding() == "OT1");
1114         bool const use_newtxmath =
1115                 theLaTeXFonts().getLaTeXFont(from_ascii(params_.fontsMath())).getUsedPackage(
1116                         ot1, false, false) == "newtxmath";
1117
1118         if (!params_.useNonTeXFonts && !use_newtxmath && !amsPackages.empty())
1119                 packages << amsPackages;
1120
1121         if (mustProvide("cancel") &&
1122             params_.use_package("cancel") != BufferParams::package_off)
1123                 packages << "\\usepackage{cancel}\n";
1124
1125         // marvosym and bbding both define the \Cross macro
1126         if (mustProvide("marvosym")) {
1127             if (mustProvide("bbding"))
1128                 packages << "\\let\\Cross\\relax\n";
1129             packages << "\\usepackage{marvosym}\n";
1130         }
1131
1132         // accents must be loaded after amsmath
1133         if (mustProvide("accents") &&
1134             params_.use_package("accents") != BufferParams::package_off)
1135                 packages << "\\usepackage{accents}\n";
1136
1137         // mathdots must be loaded after amsmath
1138         if (mustProvide("mathdots") &&
1139                 params_.use_package("mathdots") != BufferParams::package_off)
1140                 packages << "\\usepackage{mathdots}\n";
1141
1142         // yhmath must be loaded after amsmath
1143         if (mustProvide("yhmath") &&
1144             params_.use_package("yhmath") != BufferParams::package_off)
1145                 packages << "\\usepackage{yhmath}\n";
1146
1147         // stmaryrd must be loaded after amsmath
1148         if (mustProvide("stmaryrd") &&
1149             params_.use_package("stmaryrd") != BufferParams::package_off)
1150                 packages << "\\usepackage{stmaryrd}\n";
1151
1152         if (mustProvide("stackrel") &&
1153             params_.use_package("stackrel") != BufferParams::package_off)
1154                 packages << "\\usepackage{stackrel}\n";
1155
1156         if (mustProvide("undertilde") &&
1157                 params_.use_package("undertilde") != BufferParams::package_off)
1158                 packages << "\\usepackage{undertilde}\n";
1159
1160         // [x]color and pdfcolmk are handled in getColorOptions() above
1161
1162         // makeidx.sty
1163         if (isRequired("makeidx") || isRequired("splitidx")) {
1164                 if (!isProvided("makeidx") && !isRequired("splitidx"))
1165                         packages << "\\usepackage{makeidx}\n";
1166                 if (mustProvide("splitidx"))
1167                         packages << "\\usepackage{splitidx}\n";
1168                 packages << "\\makeindex\n";
1169         }
1170
1171         // graphicx.sty
1172         if (mustProvide("graphicx") && params_.graphics_driver != "none") {
1173                 if (params_.graphics_driver == "default")
1174                         packages << "\\usepackage{graphicx}\n";
1175                 else
1176                         packages << "\\usepackage["
1177                                  << params_.graphics_driver
1178                                  << "]{graphicx}\n";
1179         }
1180
1181         // lyxskak.sty --- newer chess support based on skak.sty
1182         if (mustProvide("chess"))
1183                 packages << "\\usepackage[ps,mover]{lyxskak}\n";
1184
1185         // setspace.sty
1186         if (mustProvide("setspace") && !isProvided("SetSpace"))
1187                 packages << "\\usepackage{setspace}\n";
1188
1189         // we need to assure that mhchem is loaded before esint and every other
1190         // package that redefines command of amsmath because mhchem loads amlatex
1191         // (this info is from the author of mhchem from June 2013)
1192         if (mustProvide("mhchem") &&
1193             params_.use_package("mhchem") != BufferParams::package_off)
1194                 packages << "\\PassOptionsToPackage{version=3}{mhchem}\n"
1195                             "\\usepackage{mhchem}\n";
1196
1197         // wasysym is a simple feature, but it must be after amsmath if both
1198         // are used
1199         // wasysym redefines some integrals (e.g. iint) from amsmath. That
1200         // leads to inconsistent integrals. We only load this package if
1201         // the document does not contain integrals (then isRequired("esint")
1202         // is false) or if esint is used, since esint redefines all relevant
1203         // integral symbols from wasysym and amsmath.
1204         // See http://www.lyx.org/trac/ticket/1942
1205         if (mustProvide("wasysym") &&
1206             params_.use_package("wasysym") != BufferParams::package_off &&
1207             (params_.use_package("esint") != BufferParams::package_off || !isRequired("esint")))
1208                 packages << "\\usepackage{wasysym}\n";
1209
1210         // esint must be after amsmath (and packages requiring amsmath, like mhchem)
1211         // and wasysym, since it will redeclare inconsistent integral symbols
1212         if (mustProvide("esint") &&
1213             params_.use_package("esint") != BufferParams::package_off)
1214                 packages << "\\usepackage{esint}\n";
1215
1216         // Known bibliography packages (simple \usepackage{package})
1217         for (int i = 0; i < nb_bibliofeatures; ++i) {
1218                 if (mustProvide(bibliofeatures[i]))
1219                         packages << "\\usepackage{"
1220                                  << bibliofeatures[i] << "}\n";
1221         }
1222
1223         // Compatibility between achicago and natbib
1224         if (mustProvide("achicago") && mustProvide("natbib"))
1225                 packages << "\\let\\achicagobib\\thebibliography\n";
1226
1227         // natbib.sty
1228         // Some classes load natbib themselves, but still allow (or even require)
1229         // plain numeric citations (ReVTeX is such a case, see bug 5182).
1230         // This special case is indicated by the "natbib-internal" key.
1231         if (mustProvide("natbib") && !isProvided("natbib-internal")) {
1232                 packages << "\\usepackage[";
1233                 if (params_.citeEngineType() == ENGINE_TYPE_NUMERICAL)
1234                         packages << "numbers";
1235                 else
1236                         packages << "authoryear";
1237                 if (!params_.biblio_opts.empty())
1238                         packages << ',' << params_.biblio_opts;
1239                 packages << "]{natbib}\n";
1240         }
1241
1242         // Compatibility between achicago and natbib
1243         if (mustProvide("achicago") && mustProvide("natbib")) {
1244                 packages << "\\let\\thebibliography\\achicagobib\n";
1245                 packages << "\\let\\SCcite\\astroncite\n";
1246                 packages << "\\let\\UnexpandableProtect\\protect\n";
1247         }
1248
1249         // jurabib -- we need version 0.6 at least.
1250         if (mustProvide("jurabib")) {
1251                 packages << "\\usepackage";
1252                 if (!params_.biblio_opts.empty())
1253                         packages << '[' << params_.biblio_opts << ']';
1254                 packages << "{jurabib}[2004/01/25]\n";
1255         }
1256
1257         // opcit -- we pass custombst as we output \bibliographystyle ourselves
1258         if (mustProvide("opcit")) {
1259                 if (isRequired("hyperref"))
1260                         packages << "\\usepackage[custombst,hyperref]{opcit}\n";
1261                 else
1262                         packages << "\\usepackage[custombst]{opcit}\n";
1263         }
1264
1265         // xargs -- we need version 1.09 at least
1266         if (mustProvide("xargs"))
1267                 packages << "\\usepackage{xargs}[2008/03/08]\n";
1268
1269         if (mustProvide("xy"))
1270                 packages << "\\usepackage[all]{xy}\n";
1271
1272         if (mustProvide("feyn"))
1273                 packages << "\\usepackage{feyn}\n"; //Diagram
1274
1275         if (mustProvide("ulem"))
1276                 packages << "\\PassOptionsToPackage{normalem}{ulem}\n"
1277                             "\\usepackage{ulem}\n";
1278
1279         if (mustProvide("nomencl")) {
1280                 // Make it work with the new and old version of the package,
1281                 // but don't use the compatibility option since it is
1282                 // incompatible to other packages.
1283                 packages << "\\usepackage{nomencl}\n"
1284                             "% the following is useful when we have the old nomencl.sty package\n"
1285                             "\\providecommand{\\printnomenclature}{\\printglossary}\n"
1286                             "\\providecommand{\\makenomenclature}{\\makeglossary}\n"
1287                             "\\makenomenclature\n";
1288         }
1289
1290         // fixltx2e provides subscript
1291         if (mustProvide("subscript") && !isRequired("fixltx2e"))
1292                 packages << "\\usepackage{subscript}\n";
1293
1294         // footmisc must be loaded after setspace
1295         // Set options here, load the package after the user preamble to
1296         // avoid problems with manual loaded footmisc.
1297         if (mustProvide("footmisc"))
1298                 packages << "\\PassOptionsToPackage{stable}{footmisc}\n";
1299
1300         if (mustProvide("microtype")){
1301                 packages << "\\usepackage{microtype}\n";
1302         }
1303
1304         return packages.str();
1305 }
1306
1307
1308 TexString LaTeXFeatures::getMacros() const
1309 {
1310         otexstringstream macros;
1311
1312         if (!preamble_snippets_.empty()) {
1313                 macros << '\n';
1314                 macros << getPreambleSnippets();
1315         }
1316
1317         if (mustProvide("papersize")) {
1318                 if (runparams_.flavor == OutputParams::LATEX
1319                     || runparams_.flavor == OutputParams::DVILUATEX)
1320                         macros << papersizedvi_def << '\n';
1321                 else if  (runparams_.flavor == OutputParams::LUATEX)
1322                         macros << papersizepdflua_def << '\n';
1323                 else
1324                         macros << papersizepdf_def << '\n';
1325         }
1326
1327         if (mustProvide("LyX")) {
1328                 if (isRequired("hyperref"))
1329                         macros << lyx_hyperref_def << '\n';
1330                 else
1331                         macros << lyx_def << '\n';
1332         }
1333
1334         if (mustProvide("noun"))
1335                 macros << noun_def << '\n';
1336
1337         if (mustProvide("lyxarrow"))
1338                 macros << lyxarrow_def << '\n';
1339
1340         if (mustProvide("lyxzerowidthspace"))
1341                 macros << lyxZWSP_def << '\n';
1342
1343         if (!usePolyglossia() && mustProvide("textgreek")) {
1344             // ensure LGR font encoding is defined also if fontenc is not loaded by LyX
1345                 if (params_.main_font_encoding() == "default")
1346                         macros << textgreek_LGR_def;
1347                 macros << textgreek_def << '\n';
1348         }
1349
1350         if (!usePolyglossia() && mustProvide("textcyr")) {
1351                 // ensure T2A font encoding is set up also if fontenc is not loaded by LyX
1352                 if (params_.main_font_encoding() == "default")
1353                         macros << textcyr_T2A_def;
1354                 macros << textcyr_def << '\n';
1355         }
1356
1357         // non-standard text accents:
1358         if (mustProvide("textcommaabove") || mustProvide("textcommaaboveright") ||
1359             mustProvide("textcommabelow") || mustProvide("textbaltic"))
1360                 macros << lyxaccent_def;
1361
1362         if (mustProvide("textcommabelow") || mustProvide("textbaltic"))
1363                 macros << textcommabelow_def << '\n';
1364
1365         if (mustProvide("textcommaabove") || mustProvide("textbaltic"))
1366                 macros << textcommaabove_def << '\n';
1367
1368         if (mustProvide("textcommaaboveright"))
1369                 macros << textcommaaboveright_def << '\n';
1370
1371         if (mustProvide("textbaltic"))
1372                 macros << textbaltic_def << '\n';
1373
1374         // split-level fractions
1375         if (mustProvide("xfrac") || mustProvide("smallLetterFrac"))
1376                 macros << xfrac_def << '\n';
1377
1378         if (mustProvide("smallLetterFrac"))
1379                 macros << smallLetterFrac_def << '\n';
1380
1381         if (mustProvide("lyxmathsym"))
1382                 macros << lyxmathsym_def << '\n';
1383
1384         if (mustProvide("cedilla"))
1385                 macros << cedilla_def << '\n';
1386
1387         if (mustProvide("subring"))
1388                 macros << subring_def << '\n';
1389
1390         if (mustProvide("subdot"))
1391                 macros << subdot_def << '\n';
1392
1393         if (mustProvide("subhat"))
1394                 macros << subhat_def << '\n';
1395
1396         if (mustProvide("subtilde"))
1397                 macros << subtilde_def << '\n';
1398
1399         if (mustProvide("dacute"))
1400                 macros << dacute_def << '\n';
1401
1402         if (mustProvide("tipasymb"))
1403                 macros << tipasymb_def << '\n';
1404
1405         if (mustProvide("dgrave"))
1406                 macros << dgrave_def << '\n';
1407
1408         if (mustProvide("rcap"))
1409                 macros << rcap_def << '\n';
1410
1411         if (mustProvide("ogonek"))
1412                 macros << ogonek_def << '\n';
1413
1414         // quotes.
1415         if (mustProvide("quotesinglbase"))
1416                 macros << quotesinglbase_def << '\n';
1417         if (mustProvide("quotedblbase"))
1418                 macros << quotedblbase_def << '\n';
1419         if (mustProvide("guilsinglleft"))
1420                 macros << guilsinglleft_def << '\n';
1421         if (mustProvide("guilsinglright"))
1422                 macros << guilsinglright_def << '\n';
1423         if (mustProvide("guillemotleft"))
1424                 macros << guillemotleft_def << '\n';
1425         if (mustProvide("guillemotright"))
1426                 macros << guillemotright_def << '\n';
1427         if (mustProvide("textquotedbl"))
1428                 macros << textquotedbl_def << '\n';
1429         if (mustProvide("textquotesinglep")) {
1430                 if (runparams_.flavor == OutputParams::XETEX)
1431                         macros << textquotesinglep_xetex_def << '\n';
1432                 else
1433                         macros << textquotesinglep_luatex_def << '\n';
1434         }
1435         if (mustProvide("textquotedblp")) {
1436                 if (runparams_.flavor == OutputParams::XETEX)
1437                         macros << textquotedblp_xetex_def << '\n';
1438                 else
1439                         macros << textquotedblp_luatex_def << '\n';
1440         }
1441
1442         // Math mode
1443         if (mustProvide("binom") && !isRequired("amsmath"))
1444                 macros << binom_def << '\n';
1445         if (mustProvide("mathcircumflex"))
1446                 macros << mathcircumflex_def << '\n';
1447
1448         // other
1449         if (mustProvide("ParagraphLeftIndent"))
1450                 macros << paragraphleftindent_def;
1451         if (mustProvide("NeedLyXFootnoteCode"))
1452                 macros << floatingfootnote_def;
1453
1454         // some problems with tex->html converters
1455         if (mustProvide("NeedTabularnewline"))
1456                 macros << tabularnewline_def;
1457
1458         // greyed-out environment (note inset)
1459         // the color is specified in the routine
1460         // getColorOptions() to avoid LaTeX-package clashes
1461         if (mustProvide("lyxgreyedout"))
1462                 macros << lyxgreyedout_def;
1463
1464         if (mustProvide("lyxdot"))
1465                 macros << lyxdot_def << '\n';
1466
1467         // floats
1468         getFloatDefinitions(macros);
1469
1470         if (mustProvide("refstyle"))
1471                 macros << lyxref_def << '\n';
1472
1473         // change tracking
1474         if (mustProvide("ct-dvipost"))
1475                 macros << changetracking_dvipost_def;
1476
1477         if (mustProvide("ct-xcolor-ulem")) {
1478                 streamsize const prec = macros.os().precision(2);
1479
1480                 RGBColor cadd = rgbFromHexName(lcolor.getX11Name(Color_addedtext));
1481                 macros << "\\providecolor{lyxadded}{rgb}{"
1482                        << cadd.r / 255.0 << ',' << cadd.g / 255.0 << ',' << cadd.b / 255.0 << "}\n";
1483
1484                 RGBColor cdel = rgbFromHexName(lcolor.getX11Name(Color_deletedtext));
1485                 macros << "\\providecolor{lyxdeleted}{rgb}{"
1486                        << cdel.r / 255.0 << ',' << cdel.g / 255.0 << ',' << cdel.b / 255.0 << "}\n";
1487
1488                 macros.os().precision(prec);
1489
1490                 if (isRequired("hyperref"))
1491                         macros << changetracking_xcolor_ulem_hyperref_def;
1492                 else
1493                         macros << changetracking_xcolor_ulem_def;
1494         }
1495
1496         if (mustProvide("ct-tikz-math-sout"))
1497                         macros << changetracking_tikz_math_sout_def;
1498
1499         if (mustProvide("ct-none"))
1500                 macros << changetracking_none_def;
1501
1502         if (mustProvide("rtloutputdblcol"))
1503                 macros << rtloutputdblcol_def;
1504
1505         if (mustProvide("lyxmintcaption"))
1506                 macros << lyxmintcaption_def;
1507
1508         return macros.release();
1509 }
1510
1511
1512 docstring const LaTeXFeatures::getBabelPresettings() const
1513 {
1514         odocstringstream tmp;
1515
1516         for (Language const * lang : UsedLanguages_)
1517                 if (!lang->babel_presettings().empty())
1518                         tmp << lang->babel_presettings() << '\n';
1519         if (!params_.language->babel_presettings().empty())
1520                 tmp << params_.language->babel_presettings() << '\n';
1521
1522         if (!contains(tmp.str(), '@'))
1523                 return tmp.str();
1524
1525         return "\\makeatletter\n" + tmp.str() + "\\makeatother\n";
1526 }
1527
1528
1529 docstring const LaTeXFeatures::getBabelPostsettings() const
1530 {
1531         odocstringstream tmp;
1532
1533         for (Language const * lang : UsedLanguages_)
1534                 if (!lang->babel_postsettings().empty())
1535                         tmp << lang->babel_postsettings() << '\n';
1536         if (!params_.language->babel_postsettings().empty())
1537                 tmp << params_.language->babel_postsettings() << '\n';
1538
1539         if (!contains(tmp.str(), '@'))
1540                 return tmp.str();
1541
1542         return "\\makeatletter\n" + tmp.str() + "\\makeatother\n";
1543 }
1544
1545
1546 bool LaTeXFeatures::needBabelLangOptions() const
1547 {
1548         if (!lyxrc.language_global_options || params_.language->asBabelOptions())
1549                 return true;
1550
1551         LanguageList::const_iterator it  = UsedLanguages_.begin();
1552         LanguageList::const_iterator end = UsedLanguages_.end();
1553         for (; it != end; ++it)
1554                 if ((*it)->asBabelOptions())
1555                         return true;
1556
1557         return false;
1558 }
1559
1560
1561 string const LaTeXFeatures::loadAMSPackages() const
1562 {
1563         ostringstream tmp;
1564
1565         if (mustProvide("amsmath")
1566             && params_.use_package("amsmath") != BufferParams::package_off) {
1567                 tmp << "\\usepackage{amsmath}\n";
1568         } else {
1569                 // amsbsy and amstext are already provided by amsmath
1570                 if (mustProvide("amsbsy"))
1571                         tmp << "\\usepackage{amsbsy}\n";
1572                 if (mustProvide("amstext"))
1573                         tmp << "\\usepackage{amstext}\n";
1574         }
1575
1576         if (mustProvide("amsthm"))
1577                 tmp << "\\usepackage{amsthm}\n";
1578
1579         if (mustProvide("amssymb")
1580             && params_.use_package("amssymb") != BufferParams::package_off)
1581                 tmp << "\\usepackage{amssymb}\n";
1582
1583         return tmp.str();
1584 }
1585
1586
1587 docstring const LaTeXFeatures::getTClassPreamble() const
1588 {
1589         // the text class specific preamble
1590         DocumentClass const & tclass = params_.documentClass();
1591         odocstringstream tcpreamble;
1592
1593         tcpreamble << tclass.preamble();
1594
1595         list<docstring>::const_iterator cit = usedLayouts_.begin();
1596         list<docstring>::const_iterator end = usedLayouts_.end();
1597         for (; cit != end; ++cit)
1598                 // For InPreamble layouts, we output the preamble stuff earlier
1599                 // (before the layouts). See Paragraph::Private::validate.
1600                 if (!tclass[*cit].inpreamble)
1601                         tcpreamble << tclass[*cit].preamble();
1602
1603         cit = usedInsetLayouts_.begin();
1604         end = usedInsetLayouts_.end();
1605         TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1606         for (; cit != end; ++cit) {
1607                 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1608                 if (it == ils.end())
1609                         continue;
1610                 tcpreamble << it->second.preamble();
1611         }
1612
1613         return tcpreamble.str();
1614 }
1615
1616
1617 docstring const LaTeXFeatures::getTClassHTMLPreamble() const
1618 {
1619         DocumentClass const & tclass = params_.documentClass();
1620         odocstringstream tcpreamble;
1621
1622         tcpreamble << tclass.htmlpreamble();
1623
1624         list<docstring>::const_iterator cit = usedLayouts_.begin();
1625         list<docstring>::const_iterator end = usedLayouts_.end();
1626         for (; cit != end; ++cit)
1627                 tcpreamble << tclass[*cit].htmlpreamble();
1628
1629         cit = usedInsetLayouts_.begin();
1630         end = usedInsetLayouts_.end();
1631         TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1632         for (; cit != end; ++cit) {
1633                 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1634                 if (it == ils.end())
1635                         continue;
1636                 tcpreamble << it->second.htmlpreamble();
1637         }
1638
1639         return tcpreamble.str();
1640 }
1641
1642
1643 docstring const LaTeXFeatures::getTClassHTMLStyles() const
1644 {
1645         DocumentClass const & tclass = params_.documentClass();
1646         odocstringstream tcpreamble;
1647
1648         if (mustProvide("noun"))
1649                 tcpreamble << lyxnoun_style;
1650         // this isn't exact, but it won't hurt that much if it
1651         // wasn't for this.
1652         if (mustProvide("ulem"))
1653                 tcpreamble << lyxstrikeout_style;
1654
1655         tcpreamble << tclass.htmlstyles();
1656
1657         list<docstring>::const_iterator cit = usedLayouts_.begin();
1658         list<docstring>::const_iterator end = usedLayouts_.end();
1659         for (; cit != end; ++cit)
1660                 tcpreamble << tclass[*cit].htmlstyle();
1661
1662         cit = usedInsetLayouts_.begin();
1663         end = usedInsetLayouts_.end();
1664         TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1665         for (; cit != end; ++cit) {
1666                 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1667                 if (it == ils.end())
1668                         continue;
1669                 tcpreamble << it->second.htmlstyle();
1670         }
1671
1672         return tcpreamble.str();
1673 }
1674
1675
1676 namespace {
1677
1678 docstring const getFloatI18nPreamble(docstring const & type,
1679                         docstring const & name, Language const * lang,
1680                         Encoding const & enc, bool const polyglossia)
1681 {
1682         // Check whether name can be encoded in the buffer encoding
1683         bool encodable = true;
1684         for (size_t i = 0; i < name.size(); ++i) {
1685                 if (!enc.encodable(name[i])) {
1686                         encodable = false;
1687                         break;
1688                 }
1689         }
1690
1691         docstring const language = polyglossia ? from_ascii(lang->polyglossia())
1692                                                : from_ascii(lang->babel());
1693         docstring const langenc = from_ascii(lang->encoding()->iconvName());
1694         docstring const texenc = from_ascii(lang->encoding()->latexName());
1695         docstring const bufenc = from_ascii(enc.iconvName());
1696         docstring const s1 = docstring(1, 0xF0000);
1697         docstring const s2 = docstring(1, 0xF0001);
1698         docstring const translated = encodable ? name
1699                 : from_ascii("\\inputencoding{") + texenc + from_ascii("}")
1700                         + s1 + langenc + s2 + name + s1 + bufenc + s2;
1701
1702         odocstringstream os;
1703         os << "\\addto\\captions" << language
1704            << "{\\renewcommand{\\" << type << "name}{" << translated << "}}\n";
1705         return os.str();
1706 }
1707
1708
1709 docstring const i18npreamble(docstring const & templ, Language const * lang,
1710                              Encoding const & enc, bool const polyglossia,
1711                              bool const need_fixedwidth)
1712 {
1713         if (templ.empty())
1714                 return templ;
1715
1716         string preamble = polyglossia ?
1717                 subst(to_utf8(templ), "$$lang", lang->polyglossia()) :
1718                 subst(to_utf8(templ), "$$lang", lang->babel());
1719
1720         string const langenc = lang->encoding()->iconvName();
1721         string const texenc = lang->encoding()->latexName();
1722         string const bufenc = enc.iconvName();
1723         Encoding const * testenc(&enc);
1724         bool lang_fallback = false;
1725         bool ascii_fallback = false;
1726         if (need_fixedwidth && !enc.hasFixedWidth()) {
1727                 if (lang->encoding()->hasFixedWidth()) {
1728                         testenc = lang->encoding();
1729                         lang_fallback = true;
1730                 } else {
1731                         // We need a fixed width encoding, but both the buffer
1732                         // encoding and the language encoding are variable
1733                         // width. As a last fallback, try to convert to pure
1734                         // ASCII using the LaTeX commands defined in unicodesymbols.
1735                         testenc = encodings.fromLyXName("ascii");
1736                         if (!testenc)
1737                                 return docstring();
1738                         ascii_fallback = true;
1739                 }
1740         }
1741         // First and second character of plane 15 (Private Use Area)
1742         string const s1 = "\xf3\xb0\x80\x80"; // U+F0000
1743         string const s2 = "\xf3\xb0\x80\x81"; // U+F0001
1744         // FIXME UNICODE
1745         // lyx::regex is not unicode-safe.
1746         // Should use QRegExp or (boost::u32regex, but that requires ICU)
1747         static regex const reg("_\\(([^\\)]+)\\)");
1748         smatch sub;
1749         while (regex_search(preamble, sub, reg)) {
1750                 string const key = sub.str(1);
1751                 docstring const name = lang->translateLayout(key);
1752                 // Check whether name can be encoded in the buffer encoding
1753                 bool encodable = true;
1754                 for (size_t i = 0; i < name.size() && encodable; ++i)
1755                         if (!testenc->encodable(name[i]))
1756                                 encodable = false;
1757                 string translated;
1758                 if (encodable && !lang_fallback)
1759                         translated = to_utf8(name);
1760                 else if (ascii_fallback)
1761                         translated = to_ascii(testenc->latexString(name).first);
1762                 else
1763                         translated = "\\inputencoding{" + texenc + "}"
1764                                 + s1 + langenc + s2 + to_utf8(name)
1765                                 + s1 + bufenc + s2;
1766                 preamble = subst(preamble, sub.str(), translated);
1767         }
1768         return from_utf8(preamble);
1769 }
1770
1771 }
1772
1773
1774 docstring const LaTeXFeatures::getTClassI18nPreamble(bool use_babel,
1775                                 bool use_polyglossia, bool use_minted) const
1776 {
1777         DocumentClass const & tclass = params_.documentClass();
1778         // collect preamble snippets in a set to prevent multiple identical
1779         // commands (would happen if e.g. both theorem and theorem* are used)
1780         set<docstring> snippets;
1781         typedef LanguageList::const_iterator lang_it;
1782         lang_it const lbeg = UsedLanguages_.begin();
1783         lang_it const lend =  UsedLanguages_.end();
1784         list<docstring>::const_iterator cit = usedLayouts_.begin();
1785         list<docstring>::const_iterator end = usedLayouts_.end();
1786         for (; cit != end; ++cit) {
1787                 // language dependent commands (once per document)
1788                 snippets.insert(i18npreamble(tclass[*cit].langpreamble(),
1789                                                 buffer().language(),
1790                                                 buffer().params().encoding(),
1791                                                 use_polyglossia, false));
1792                 // commands for language changing (for multilanguage documents)
1793                 if ((use_babel || use_polyglossia) && !UsedLanguages_.empty()) {
1794                         snippets.insert(i18npreamble(
1795                                                 tclass[*cit].babelpreamble(),
1796                                                 buffer().language(),
1797                                                 buffer().params().encoding(),
1798                                                 use_polyglossia, false));
1799                         for (lang_it lit = lbeg; lit != lend; ++lit)
1800                                 snippets.insert(i18npreamble(
1801                                                 tclass[*cit].babelpreamble(),
1802                                                 *lit,
1803                                                 buffer().params().encoding(),
1804                                                 use_polyglossia, false));
1805                 }
1806         }
1807         if ((use_babel || use_polyglossia) && !UsedLanguages_.empty()) {
1808                 FloatList const & floats = params_.documentClass().floats();
1809                 UsedFloats::const_iterator fit = usedFloats_.begin();
1810                 UsedFloats::const_iterator fend = usedFloats_.end();
1811                 for (; fit != fend; ++fit) {
1812                         Floating const & fl = floats.getType(fit->first);
1813                         // we assume builtin floats are translated
1814                         if (fl.isPredefined())
1815                                 continue;
1816                         docstring const type = from_ascii(fl.floattype());
1817                         docstring const flname = from_utf8(fl.name());
1818                         docstring name = buffer().language()->translateLayout(fl.name());
1819                         // only request translation if we have a real translation
1820                         // (that differs from the source)
1821                         if (flname != name)
1822                                 snippets.insert(getFloatI18nPreamble(
1823                                                 type, name, buffer().language(),
1824                                                 buffer().params().encoding(),
1825                                                 use_polyglossia));
1826                         for (lang_it lit = lbeg; lit != lend; ++lit) {
1827                                 string const code = (*lit)->code();
1828                                 name = (*lit)->translateLayout(fl.name());
1829                                 // we assume we have a suitable translation if
1830                                 // either the language is English (we need to
1831                                 // translate into English if English is a secondary
1832                                 // language) or if translateIfPossible returns
1833                                 // something different to the English source.
1834                                 bool const have_translation =
1835                                         (flname != name || contains(code, "en"));
1836                                 if (have_translation)
1837                                         snippets.insert(getFloatI18nPreamble(
1838                                                 type, name, *lit,
1839                                                 buffer().params().encoding(),
1840                                                 use_polyglossia));
1841                         }
1842                 }
1843         }
1844
1845         cit = usedInsetLayouts_.begin();
1846         end = usedInsetLayouts_.end();
1847         TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1848         for (; cit != end; ++cit) {
1849                 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1850                 if (it == ils.end())
1851                         continue;
1852                 // The listings package does not work with variable width
1853                 // encodings, only with fixed width encodings. Therefore we
1854                 // need to force a fixed width encoding for
1855                 // \lstlistlistingname and \lstlistingname (bug 9382).
1856                 // This needs to be consistent with InsetListings::latex().
1857                 bool const need_fixedwidth = !use_minted &&
1858                                         !runparams_.isFullUnicode() &&
1859                                         it->second.fixedwidthpreambleencoding();
1860                 // language dependent commands (once per document)
1861                 snippets.insert(i18npreamble(it->second.langpreamble(),
1862                                                 buffer().language(),
1863                                                 buffer().params().encoding(),
1864                                                 use_polyglossia, need_fixedwidth));
1865                 // commands for language changing (for multilanguage documents)
1866                 if ((use_babel || use_polyglossia) && !UsedLanguages_.empty()) {
1867                         snippets.insert(i18npreamble(
1868                                                 it->second.babelpreamble(),
1869                                                 buffer().language(),
1870                                                 buffer().params().encoding(),
1871                                                 use_polyglossia, need_fixedwidth));
1872                         for (lang_it lit = lbeg; lit != lend; ++lit)
1873                                 snippets.insert(i18npreamble(
1874                                                 it->second.babelpreamble(),
1875                                                 *lit,
1876                                                 buffer().params().encoding(),
1877                                                 use_polyglossia, need_fixedwidth));
1878                 }
1879         }
1880
1881         odocstringstream tcpreamble;
1882         set<docstring>::const_iterator const send = snippets.end();
1883         set<docstring>::const_iterator it = snippets.begin();
1884         for (; it != send; ++it)
1885                 tcpreamble << *it;
1886         return tcpreamble.str();
1887 }
1888
1889
1890 docstring const LaTeXFeatures::getLyXSGMLEntities() const
1891 {
1892         // Definition of entities used in the document that are LyX related.
1893         odocstringstream entities;
1894
1895         if (mustProvide("lyxarrow")) {
1896                 entities << "<!ENTITY lyxarrow \"-&gt;\">" << '\n';
1897         }
1898
1899         return entities.str();
1900 }
1901
1902
1903 docstring const LaTeXFeatures::getIncludedFiles(string const & fname) const
1904 {
1905         odocstringstream sgmlpreamble;
1906         // FIXME UNICODE
1907         docstring const basename(from_utf8(onlyPath(fname)));
1908
1909         FileMap::const_iterator end = IncludedFiles_.end();
1910         for (FileMap::const_iterator fi = IncludedFiles_.begin();
1911              fi != end; ++fi)
1912                 // FIXME UNICODE
1913                 sgmlpreamble << "\n<!ENTITY " << fi->first
1914                              << (isSGMLFileName(fi->second) ? " SYSTEM \"" : " \"")
1915                              << makeRelPath(from_utf8(fi->second), basename) << "\">";
1916
1917         return sgmlpreamble.str();
1918 }
1919
1920
1921 void LaTeXFeatures::showStruct() const
1922 {
1923         lyxerr << "LyX needs the following commands when LaTeXing:"
1924                << "\n***** Packages:" << getPackages()
1925                << "\n***** Macros:" << to_utf8(getMacros().str)
1926                << "\n***** Textclass stuff:" << to_utf8(getTClassPreamble())
1927                << "\n***** done." << endl;
1928 }
1929
1930
1931 Buffer const & LaTeXFeatures::buffer() const
1932 {
1933         return *buffer_;
1934 }
1935
1936
1937 void LaTeXFeatures::setBuffer(Buffer const & buffer)
1938 {
1939         buffer_ = &buffer;
1940 }
1941
1942
1943 BufferParams const & LaTeXFeatures::bufferParams() const
1944 {
1945         return params_;
1946 }
1947
1948
1949 void LaTeXFeatures::getFloatDefinitions(otexstream & os) const
1950 {
1951         FloatList const & floats = params_.documentClass().floats();
1952
1953         // Here we will output the code to create the needed float styles.
1954         // We will try to do this as minimal as possible.
1955         // \floatstyle{ruled}
1956         // \newfloat{algorithm}{htbp}{loa}
1957         // \providecommand{\algorithmname}{Algorithm}
1958         // \floatname{algorithm}{\protect\algorithmname}
1959         UsedFloats::const_iterator cit = usedFloats_.begin();
1960         UsedFloats::const_iterator end = usedFloats_.end();
1961         for (; cit != end; ++cit) {
1962                 Floating const & fl = floats.getType(cit->first);
1963
1964                 // For builtin floats we do nothing.
1965                 if (fl.isPredefined())
1966                         continue;
1967
1968                 // We have to special case "table" and "figure"
1969                 if (fl.floattype() == "tabular" || fl.floattype() == "figure") {
1970                         // Output code to modify "table" or "figure"
1971                         // but only if builtin == false
1972                         // and that have to be true at this point in the
1973                         // function.
1974                         docstring const type = from_ascii(fl.floattype());
1975                         docstring const placement = from_ascii(fl.placement());
1976                         docstring const style = from_ascii(fl.style());
1977                         if (!style.empty()) {
1978                                 os << "\\floatstyle{" << style << "}\n"
1979                                    << "\\restylefloat{" << type << "}\n";
1980                         }
1981                         if (!placement.empty()) {
1982                                 os << "\\floatplacement{" << type << "}{"
1983                                    << placement << "}\n";
1984                         }
1985                 } else {
1986                         // The other non builtin floats.
1987
1988                         docstring const type = from_ascii(fl.floattype());
1989                         docstring const placement = from_ascii(fl.placement());
1990                         docstring const ext = from_ascii(fl.ext());
1991                         docstring const within = from_ascii(fl.within());
1992                         docstring const style = from_ascii(fl.style());
1993                         docstring const name =
1994                                 buffer().language()->translateLayout(fl.name());
1995                         os << "\\floatstyle{" << style << "}\n"
1996                            << "\\newfloat{" << type << "}{" << placement
1997                            << "}{" << ext << '}';
1998                         if (!within.empty())
1999                                 os << '[' << within << ']';
2000                         os << '\n'
2001                            << "\\providecommand{\\" << type << "name}{"
2002                            << name << "}\n"
2003                            << "\\floatname{" << type << "}{\\protect\\"
2004                            << type << "name}\n";
2005
2006                         // What missing here is to code to minimalize the code
2007                         // output so that the same floatstyle will not be
2008                         // used several times, when the same style is still in
2009                         // effect. (Lgb)
2010                 }
2011                 if (cit->second)
2012                         // The subfig package is loaded later
2013                         os << "\n\\AtBeginDocument{\\newsubfloat{" << from_ascii(fl.floattype()) << "}}\n";
2014         }
2015 }
2016
2017
2018 void LaTeXFeatures::resolveAlternatives()
2019 {
2020         for (Features::iterator it = features_.begin(); it != features_.end();) {
2021                 if (contains(*it, '|')) {
2022                         vector<string> const alternatives = getVectorFromString(*it, "|");
2023                         vector<string>::const_iterator const end = alternatives.end();
2024                         vector<string>::const_iterator ita = alternatives.begin();
2025                         // Is any alternative already required? => use that
2026                         for (; ita != end; ++ita) {
2027                                 if (isRequired(*ita))
2028                                         break;
2029                         }
2030                         // Is any alternative available? => use the first one
2031                         // (bug 9498)
2032                         if (ita == end) {
2033                                 for (ita = alternatives.begin(); ita != end; ++ita) {
2034                                         if (isAvailable(*ita)) {
2035                                                 require(*ita);
2036                                                 break;
2037                                         }
2038                                 }
2039                         }
2040                         // This will not work, but not requiring something
2041                         // would be more confusing
2042                         if (ita == end)
2043                                 require(alternatives.front());
2044                         features_.erase(it);
2045                         it = features_.begin();
2046                 } else
2047                         ++it;
2048         }
2049 }
2050
2051
2052 void LaTeXFeatures::expandMultiples()
2053 {
2054         for (Features::iterator it = features_.begin(); it != features_.end();) {
2055                 if (contains(*it, ',')) {
2056                         vector<string> const multiples = getVectorFromString(*it, ",");
2057                         vector<string>::const_iterator const end = multiples.end();
2058                         vector<string>::const_iterator itm = multiples.begin();
2059                         // Do nothing if any multiple is already required
2060                         for (; itm != end; ++itm) {
2061                                 if (!isRequired(*itm))
2062                                         require(*itm);
2063                         }
2064                         features_.erase(it);
2065                         it = features_.begin();
2066                 } else
2067                         ++it;
2068         }
2069 }
2070
2071
2072 } // namespace lyx