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