]> git.lyx.org Git - features.git/blob - src/LaTeXFeatures.cpp
3627cc660c9b628b8ed16813a0f1c72e649937aa
[features.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{\\textcyr}[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         // \textgreek is provided by babel globally if a Greek
619         // language/variety is used in the document
620         if (provides_.find(name) != provides_.end())
621                 return true;
622
623         // FIXME: Analoguously, babel provides a command \textcyrillic, but
624         //        for some reason, we roll our own \textcyr definition
625         //        We should use \textcyrillic instead and only define it
626         //        if we do not use a respective language that features it (i.e.,
627         //        add "textcyrillic" to the test above.
628         // FIXME: the "textbaltic" definitions are only needed if the context
629         //        font-encoding where the respective char is is not l7x.
630         //        We cannot check this here as we have no context information.
631
632         if (params_.useNonTeXFonts)
633                 return params_.documentClass().provides(name);
634
635         bool const ot1 = (params_.main_font_encoding() == "default"
636                 || params_.main_font_encoding() == "OT1");
637         bool const complete = (params_.fontsSans() == "default"
638                 && params_.fontsTypewriter() == "default");
639         bool const nomath = (params_.fontsMath() == "default");
640         return params_.documentClass().provides(name)
641                 || theLaTeXFonts().getLaTeXFont(
642                         from_ascii(params_.fontsRoman())).provides(name, ot1,
643                                                                   complete,
644                                                                   nomath)
645                 || theLaTeXFonts().getLaTeXFont(
646                         from_ascii(params_.fontsSans())).provides(name, ot1,
647                                                                  complete,
648                                                                  nomath)
649                 || theLaTeXFonts().getLaTeXFont(
650                         from_ascii(params_.fontsTypewriter())).provides(name, ot1,
651                                                                        complete,
652                                                                        nomath)
653                 || theLaTeXFonts().getLaTeXFont(
654                         from_ascii(params_.fontsMath())).provides(name, ot1,
655                                                                        complete,
656                                                                        nomath);
657 }
658
659
660 bool LaTeXFeatures::mustProvide(string const & name) const
661 {
662         return isRequired(name) && !isProvided(name);
663 }
664
665
666 bool LaTeXFeatures::isAvailable(string const & name)
667 {
668         string::size_type const i = name.find("->");
669         if (i != string::npos) {
670                 string const from = name.substr(0,i);
671                 string const to = name.substr(i+2);
672                 //LYXERR0("from=[" << from << "] to=[" << to << "]");
673                 return theConverters().isReachable(from, to);
674         }
675         return LaTeXPackages::isAvailable(name);
676 }
677
678
679 namespace {
680
681 void addSnippet(std::list<TexString> & list, TexString ts, bool allow_dupes)
682 {
683         if (allow_dupes ||
684             // test the absense of duplicates, i.e. elements with same str
685             none_of(list.begin(), list.end(), [&](TexString const & ts2){
686                             return ts.str == ts2.str;
687                     })
688             )
689                 list.push_back(move(ts));
690 }
691
692
693 TexString getSnippets(std::list<TexString> const & list)
694 {
695         otexstringstream snip;
696         for (TexString const & ts : list)
697                 snip << TexString(ts) << '\n';
698         return snip.release();
699 }
700
701 } // namespace
702
703
704 void LaTeXFeatures::addPreambleSnippet(TexString ts, bool allow_dupes)
705 {
706         addSnippet(preamble_snippets_, move(ts), allow_dupes);
707 }
708
709
710 void LaTeXFeatures::addPreambleSnippet(docstring const & str, bool allow_dupes)
711 {
712         addSnippet(preamble_snippets_, TexString(str), allow_dupes);
713 }
714
715
716 void LaTeXFeatures::addCSSSnippet(std::string const & snippet)
717 {
718         addSnippet(css_snippets_, TexString(from_ascii(snippet)), false);
719 }
720
721
722 TexString LaTeXFeatures::getPreambleSnippets() const
723 {
724         return getSnippets(preamble_snippets_);
725 }
726
727
728 docstring LaTeXFeatures::getCSSSnippets() const
729 {
730         return getSnippets(css_snippets_).str;
731 }
732
733
734
735 void LaTeXFeatures::useFloat(string const & name, bool subfloat)
736 {
737         if (!usedFloats_[name])
738                 usedFloats_[name] = subfloat;
739         if (subfloat)
740                 require("subfig");
741         // We only need float.sty if we use non builtin floats, or if we
742         // use the "H" modifier. This includes modified table and
743         // figure floats. (Lgb)
744         Floating const & fl = params_.documentClass().floats().getType(name);
745         if (!fl.floattype().empty() && fl.usesFloatPkg()) {
746                 require("float");
747         }
748 }
749
750
751 void LaTeXFeatures::useLanguage(Language const * lang)
752 {
753         if (!lang->babel().empty() || !lang->polyglossia().empty())
754                 UsedLanguages_.insert(lang);
755         if (!lang->requires().empty())
756                 require(lang->requires());
757         // currently only supported for Babel
758         if (!lang->provides().empty() && useBabel())
759                 provide(lang->provides());
760         // CJK languages do not have a babel name.
761         // They use the CJK package
762         if (lang->encoding()->package() == Encoding::CJK)
763                 require("CJK");
764         // japanese package is special
765         if (lang->encoding()->package() == Encoding::japanese)
766                 require("japanese");
767 }
768
769
770 void LaTeXFeatures::includeFile(docstring const & key, string const & name)
771 {
772         IncludedFiles_[key] = name;
773 }
774
775
776 bool LaTeXFeatures::hasLanguages() const
777 {
778         return !UsedLanguages_.empty();
779 }
780
781
782 bool LaTeXFeatures::hasOnlyPolyglossiaLanguages() const
783 {
784         // first the main language
785         if (params_.language->polyglossia().empty())
786                 return false;
787         // now the secondary languages
788         LanguageList::const_iterator const begin = UsedLanguages_.begin();
789         for (LanguageList::const_iterator cit = begin;
790              cit != UsedLanguages_.end();
791              ++cit) {
792                 if ((*cit)->polyglossia().empty())
793                         return false;
794         }
795         return true;
796 }
797
798
799 bool LaTeXFeatures::hasPolyglossiaExclusiveLanguages() const
800 {
801         // first the main language
802         if (params_.language->isPolyglossiaExclusive())
803                 return true;
804         // now the secondary languages
805         LanguageList::const_iterator const begin = UsedLanguages_.begin();
806         for (LanguageList::const_iterator cit = begin;
807              cit != UsedLanguages_.end();
808              ++cit) {
809                 if ((*cit)->isPolyglossiaExclusive())
810                         return true;
811         }
812         return false;
813 }
814
815
816 vector<string> LaTeXFeatures::getPolyglossiaExclusiveLanguages() const
817 {
818         vector<string> result;
819         // first the main language
820         if (params_.language->isPolyglossiaExclusive())
821                 result.push_back(params_.language->display());
822         // now the secondary languages
823         LanguageList::const_iterator const begin = UsedLanguages_.begin();
824         for (LanguageList::const_iterator cit = begin;
825              cit != UsedLanguages_.end();
826              ++cit) {
827                 if ((*cit)->isPolyglossiaExclusive())
828                         result.push_back((*cit)->display());
829         }
830         return result;
831 }
832
833
834 vector<string> LaTeXFeatures::getBabelExclusiveLanguages() const
835 {
836         vector<string> result;
837         // first the main language
838         if (params_.language->isBabelExclusive())
839                 result.push_back(params_.language->display());
840         // now the secondary languages
841         LanguageList::const_iterator const begin = UsedLanguages_.begin();
842         for (LanguageList::const_iterator cit = begin;
843              cit != UsedLanguages_.end();
844              ++cit) {
845                 if ((*cit)->isBabelExclusive())
846                         result.push_back((*cit)->display());
847         }
848         return result;
849 }
850
851
852 string LaTeXFeatures::getBabelLanguages() const
853 {
854         ostringstream langs;
855
856         bool first = true;
857         LanguageList::const_iterator const begin = UsedLanguages_.begin();
858         for (LanguageList::const_iterator cit = begin;
859              cit != UsedLanguages_.end();
860              ++cit) {
861                 if ((*cit)->babel().empty())
862                         continue;
863                 if (!first)
864                         langs << ',';
865                 else
866                         first = false;
867                 langs << (*cit)->babel();
868         }
869         return langs.str();
870 }
871
872
873 set<string> LaTeXFeatures::getPolyglossiaLanguages() const
874 {
875         set<string> langs;
876
877         LanguageList::const_iterator const begin = UsedLanguages_.begin();
878         for (LanguageList::const_iterator cit = begin;
879              cit != UsedLanguages_.end();
880              ++cit) {
881                 // We do not need the variants here
882                 langs.insert((*cit)->polyglossia());
883         }
884         return langs;
885 }
886
887
888 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding) const
889 {
890         // This does only find encodings of languages supported by babel, but
891         // that does not matter since we don't have a language with an
892         // encoding supported by inputenc but without babel support.
893         set<string> encs;
894         for (auto const & lang : UsedLanguages_)
895                 if (lang->encoding()->latexName() != doc_encoding &&
896                     (lang->encoding()->package() == Encoding::inputenc
897                      || lang->encoding()->package() == Encoding::japanese))
898                         encs.insert(lang->encoding()->latexName());
899         return encs;
900 }
901
902
903 void LaTeXFeatures::getFontEncodings(vector<string> & encs, bool const onlylangs) const
904 {
905         if (!onlylangs) {
906                 // these must be loaded if glyphs of this script are used
907                 // unless a language providing them is used in the document
908                 if (mustProvide("textgreek")
909                     && find(encs.begin(), encs.end(), "LGR") == encs.end())
910                         encs.insert(encs.begin(), "LGR");
911                 if (mustProvide("textcyr")
912                     && find(encs.begin(), encs.end(), "T2A") == encs.end())
913                         encs.insert(encs.begin(), "T2A");
914         }
915
916         for (auto const & lang : UsedLanguages_)
917                 if (!lang->fontenc().empty()
918                     && ascii_lowercase(lang->fontenc()) != "none") {
919                         vector<string> extraencs = getVectorFromString(lang->fontenc());
920                         for (auto const & extra : extraencs) {
921                                 if (find(encs.begin(), encs.end(), extra) == encs.end())
922                                         encs.insert(encs.begin(), extra);
923                         }
924                 }
925 }
926
927 namespace {
928
929 char const * simplefeatures[] = {
930 // note that the package order here will be the same in the LaTeX-output
931         "array",
932         "verbatim",
933         "cprotect",
934         "longtable",
935         "rotating",
936         "latexsym",
937         "pifont",
938         // subfig is handled in BufferParams.cpp
939         "varioref",
940         "prettyref",
941         "refstyle",
942         /*For a successful cooperation of the `wrapfig' package with the
943           `float' package you should load the `wrapfig' package *after*
944           the `float' package. See the caption package documentation
945           for explanation.*/
946         "float",
947         "rotfloat",
948         "wrapfig",
949         "booktabs",
950         "dvipost",
951         "fancybox",
952         "calc",
953         "units",
954         "framed",
955         "soul",
956         "textcomp",
957         "pmboxdraw",
958         "bbding",
959         "ifsym",
960         "txfonts",
961         "pxfonts",
962         "mathdesign",
963         "mathrsfs",
964         "mathabx",
965         "mathtools",
966         // "cancel",
967         "ascii",
968         "url",
969         "covington",
970         "csquotes",
971         "enumitem",
972         "endnotes",
973         "hhline",
974         "ifthen",
975         // listings is handled in BufferParams.cpp
976         "bm",
977         "pdfpages",
978         "amscd",
979         "slashed",
980         "multicol",
981         "multirow",
982         "tfrupee",
983         "shapepar",
984         "rsphrase",
985         "hpstatement",
986         "algorithm2e",
987         "sectionbox",
988         "tcolorbox",
989         "pdfcomment",
990         "fixme",
991         "todonotes",
992         "forest",
993         "varwidth",
994         "footnote",
995         "tablefootnote",
996         "afterpage",
997 };
998
999 char const * bibliofeatures[] = {
1000         // Known bibliography packages (will be loaded before natbib)
1001         "achicago",
1002         "apacite",
1003         "apalike",
1004         "astron",
1005         "authordate1-4",
1006         "babelbib",
1007         "bibgerm",
1008         "chapterbib",
1009         "chicago",
1010         "chscite",
1011         "harvard",
1012         "mslapa",
1013         "named"
1014 };
1015
1016 int const nb_bibliofeatures = sizeof(bibliofeatures) / sizeof(char const *);
1017
1018 int const nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
1019
1020 } // namespace
1021
1022
1023 string const LaTeXFeatures::getColorOptions() const
1024 {
1025         ostringstream colors;
1026
1027         // Handling the color packages separately is needed to be able to load them
1028         // before babel when hyperref is loaded with the colorlinks option
1029         // for more info see Bufferparams.cpp
1030
1031         // [x]color.sty
1032         if (mustProvide("color") || mustProvide("xcolor")) {
1033                 string const package =
1034                         (mustProvide("xcolor") ? "xcolor" : "color");
1035                 if (params_.graphics_driver == "default"
1036                         || params_.graphics_driver == "none")
1037                         colors << "\\usepackage{" << package << "}\n";
1038                 else
1039                         colors << "\\usepackage["
1040                                  << params_.graphics_driver
1041                                  << "]{" << package << "}\n";
1042         }
1043
1044         // pdfcolmk must be loaded after color
1045         if (mustProvide("pdfcolmk"))
1046                 colors << "\\usepackage{pdfcolmk}\n";
1047
1048         // the following 3 color commands must be set after color
1049         // is loaded and before pdfpages, therefore add the command
1050         // here define the set color
1051         if (mustProvide("pagecolor")) {
1052                 colors << "\\definecolor{page_backgroundcolor}{rgb}{";
1053                 colors << outputLaTeXColor(params_.backgroundcolor) << "}\n";
1054                 // set the page color
1055                 colors << "\\pagecolor{page_backgroundcolor}\n";
1056         }
1057
1058         if (mustProvide("fontcolor")) {
1059                 colors << "\\definecolor{document_fontcolor}{rgb}{";
1060                 colors << outputLaTeXColor(params_.fontcolor) << "}\n";
1061                 // set the color
1062                 colors << "\\color{document_fontcolor}\n";
1063         }
1064
1065         if (mustProvide("lyxgreyedout")) {
1066                 colors << "\\definecolor{note_fontcolor}{rgb}{";
1067                 colors << outputLaTeXColor(params_.notefontcolor) << "}\n";
1068                 // the color will be set together with the definition of
1069                 // the lyxgreyedout environment (see lyxgreyedout_def)
1070         }
1071
1072         // color for shaded boxes
1073         if (isRequired("framed") && mustProvide("color")) {
1074                 colors << "\\definecolor{shadecolor}{rgb}{";
1075                 colors << outputLaTeXColor(params_.boxbgcolor) << "}\n";
1076                 // this color is automatically used by the LaTeX-package "framed"
1077         }
1078
1079         return colors.str();
1080 }
1081
1082
1083 string const LaTeXFeatures::getPackageOptions() const
1084 {
1085         ostringstream packageopts;
1086         // Output all the package option stuff we have been asked to do.
1087         map<string, string>::const_iterator it =
1088                 params_.documentClass().packageOptions().begin();
1089         map<string, string>::const_iterator en =
1090                 params_.documentClass().packageOptions().end();
1091         for (; it != en; ++it)
1092                 if (mustProvide(it->first))
1093                         packageopts << "\\PassOptionsToPackage{" << it->second << "}"
1094                                  << "{" << it->first << "}\n";
1095         return packageopts.str();
1096 }
1097
1098
1099 string const LaTeXFeatures::getPackages() const
1100 {
1101         ostringstream packages;
1102
1103         // FIXME: currently, we can only load packages and macros known
1104         // to LyX.
1105         // However, with the Require tag of layouts/custom insets,
1106         // also unknown packages can be requested. They are silently
1107         // swallowed now. We should change this eventually.
1108
1109         //  These are all the 'simple' includes.  i.e
1110         //  packages which we just \usepackage{package}
1111         for (int i = 0; i < nb_simplefeatures; ++i) {
1112                 if (mustProvide(simplefeatures[i]))
1113                         packages << "\\usepackage{" << simplefeatures[i] << "}\n";
1114         }
1115
1116         // The rest of these packages are somewhat more complicated
1117         // than those above.
1118
1119         // [pdf]lscape is used to rotate longtables
1120         if (mustProvide("lscape")) {
1121                 if (runparams_.flavor == OutputParams::LATEX
1122                     || runparams_.flavor == OutputParams::DVILUATEX)
1123                         packages << "\\usepackage{lscape}\n";
1124                 else
1125                         packages << "\\usepackage{pdflscape}\n";
1126         }
1127
1128         // The tipa package and its extensions (tipx, tone) must not
1129         // be loaded with non-TeX fonts, since fontspec includes the
1130         // respective macros
1131         if (mustProvide("tipa") && !params_.useNonTeXFonts)
1132                 packages << "\\usepackage{tipa}\n";
1133         if (mustProvide("tipx") && !params_.useNonTeXFonts)
1134                 packages << "\\usepackage{tipx}\n";
1135         if (mustProvide("extraipa") && !params_.useNonTeXFonts)
1136                 packages << "\\usepackage{extraipa}\n";
1137         if (mustProvide("tone") && !params_.useNonTeXFonts)
1138                 packages << "\\usepackage{tone}\n";
1139
1140         // if fontspec or newtxmath is used, AMS packages have to be loaded
1141         // before fontspec (in BufferParams)
1142         string const amsPackages = loadAMSPackages();
1143         bool const ot1 = (params_.main_font_encoding() == "default"
1144                           || params_.main_font_encoding() == "OT1");
1145         bool const use_newtxmath =
1146                 theLaTeXFonts().getLaTeXFont(from_ascii(params_.fontsMath())).getUsedPackage(
1147                         ot1, false, false) == "newtxmath";
1148
1149         if (!params_.useNonTeXFonts && !use_newtxmath && !amsPackages.empty())
1150                 packages << amsPackages;
1151
1152         if (mustProvide("cancel") &&
1153             params_.use_package("cancel") != BufferParams::package_off)
1154                 packages << "\\usepackage{cancel}\n";
1155
1156         // marvosym and bbding both define the \Cross macro
1157         if (mustProvide("marvosym")) {
1158             if (mustProvide("bbding"))
1159                 packages << "\\let\\Cross\\relax\n";
1160             packages << "\\usepackage{marvosym}\n";
1161         }
1162
1163         // accents must be loaded after amsmath
1164         if (mustProvide("accents") &&
1165             params_.use_package("accents") != BufferParams::package_off)
1166                 packages << "\\usepackage{accents}\n";
1167
1168         // mathdots must be loaded after amsmath
1169         if (mustProvide("mathdots") &&
1170                 params_.use_package("mathdots") != BufferParams::package_off)
1171                 packages << "\\usepackage{mathdots}\n";
1172
1173         // yhmath must be loaded after amsmath
1174         if (mustProvide("yhmath") &&
1175             params_.use_package("yhmath") != BufferParams::package_off)
1176                 packages << "\\usepackage{yhmath}\n";
1177
1178         // stmaryrd must be loaded after amsmath
1179         if (mustProvide("stmaryrd") &&
1180             params_.use_package("stmaryrd") != BufferParams::package_off)
1181                 packages << "\\usepackage{stmaryrd}\n";
1182
1183         if (mustProvide("stackrel") &&
1184             params_.use_package("stackrel") != BufferParams::package_off)
1185                 packages << "\\usepackage{stackrel}\n";
1186
1187         if (mustProvide("undertilde") &&
1188                 params_.use_package("undertilde") != BufferParams::package_off)
1189                 packages << "\\usepackage{undertilde}\n";
1190
1191         // [x]color and pdfcolmk are handled in getColorOptions() above
1192
1193         // makeidx.sty
1194         if (isRequired("makeidx") || isRequired("splitidx")) {
1195                 if (!isProvided("makeidx") && !isRequired("splitidx"))
1196                         packages << "\\usepackage{makeidx}\n";
1197                 if (mustProvide("splitidx"))
1198                         packages << "\\usepackage{splitidx}\n";
1199                 packages << "\\makeindex\n";
1200         }
1201
1202         // graphicx.sty
1203         if (mustProvide("graphicx") && params_.graphics_driver != "none") {
1204                 if (params_.graphics_driver == "default")
1205                         packages << "\\usepackage{graphicx}\n";
1206                 else
1207                         packages << "\\usepackage["
1208                                  << params_.graphics_driver
1209                                  << "]{graphicx}\n";
1210         }
1211
1212         // lyxskak.sty --- newer chess support based on skak.sty
1213         if (mustProvide("chess"))
1214                 packages << "\\usepackage[ps,mover]{lyxskak}\n";
1215
1216         // setspace.sty
1217         if (mustProvide("setspace") && !isProvided("SetSpace"))
1218                 packages << "\\usepackage{setspace}\n";
1219
1220         // we need to assure that mhchem is loaded before esint and every other
1221         // package that redefines command of amsmath because mhchem loads amlatex
1222         // (this info is from the author of mhchem from June 2013)
1223         if (mustProvide("mhchem") &&
1224             params_.use_package("mhchem") != BufferParams::package_off)
1225                 packages << "\\PassOptionsToPackage{version=3}{mhchem}\n"
1226                             "\\usepackage{mhchem}\n";
1227
1228         // wasysym is a simple feature, but it must be after amsmath if both
1229         // are used
1230         // wasysym redefines some integrals (e.g. iint) from amsmath. That
1231         // leads to inconsistent integrals. We only load this package if
1232         // the document does not contain integrals (then isRequired("esint")
1233         // is false) or if esint is used, since esint redefines all relevant
1234         // integral symbols from wasysym and amsmath.
1235         // See http://www.lyx.org/trac/ticket/1942
1236         if (mustProvide("wasysym") &&
1237             params_.use_package("wasysym") != BufferParams::package_off &&
1238             (params_.use_package("esint") != BufferParams::package_off || !isRequired("esint")))
1239                 packages << "\\usepackage{wasysym}\n";
1240
1241         // esint must be after amsmath (and packages requiring amsmath, like mhchem)
1242         // and wasysym, since it will redeclare inconsistent integral symbols
1243         if (mustProvide("esint") &&
1244             params_.use_package("esint") != BufferParams::package_off)
1245                 packages << "\\usepackage{esint}\n";
1246
1247         // Known bibliography packages (simple \usepackage{package})
1248         for (int i = 0; i < nb_bibliofeatures; ++i) {
1249                 if (mustProvide(bibliofeatures[i]))
1250                         packages << "\\usepackage{"
1251                                  << bibliofeatures[i] << "}\n";
1252         }
1253
1254         // Compatibility between achicago and natbib
1255         if (mustProvide("achicago") && mustProvide("natbib"))
1256                 packages << "\\let\\achicagobib\\thebibliography\n";
1257
1258         // natbib.sty
1259         // Some classes load natbib themselves, but still allow (or even require)
1260         // plain numeric citations (ReVTeX is such a case, see bug 5182).
1261         // This special case is indicated by the "natbib-internal" key.
1262         if (mustProvide("natbib")
1263             && !isProvided("natbib-internal")
1264             && !isProvided("biblatex")
1265             && !isProvided("biblatex-natbib")
1266             && !isProvided("jurabib")) {
1267                 packages << "\\usepackage[";
1268                 if (params_.citeEngineType() == ENGINE_TYPE_NUMERICAL)
1269                         packages << "numbers";
1270                 else
1271                         packages << "authoryear";
1272                 if (!params_.biblio_opts.empty())
1273                         packages << ',' << params_.biblio_opts;
1274                 packages << "]{natbib}\n";
1275         }
1276
1277         // Compatibility between achicago and natbib
1278         if (mustProvide("achicago") && mustProvide("natbib")) {
1279                 packages << "\\let\\thebibliography\\achicagobib\n";
1280                 packages << "\\let\\SCcite\\astroncite\n";
1281                 packages << "\\let\\UnexpandableProtect\\protect\n";
1282         }
1283
1284         // jurabib -- we need version 0.6 at least.
1285         if (mustProvide("jurabib")
1286             && !isProvided("natbib-internal")
1287             && !isProvided("natbib")
1288             && !isProvided("biblatex")
1289             && !isProvided("biblatex-natbib")) {
1290                 packages << "\\usepackage";
1291                 if (!params_.biblio_opts.empty())
1292                         packages << '[' << params_.biblio_opts << ']';
1293                 packages << "{jurabib}[2004/01/25]\n";
1294         }
1295
1296         // opcit -- we pass custombst as we output \bibliographystyle ourselves
1297         if (mustProvide("opcit")) {
1298                 if (isRequired("hyperref"))
1299                         packages << "\\usepackage[custombst,hyperref]{opcit}\n";
1300                 else
1301                         packages << "\\usepackage[custombst]{opcit}\n";
1302         }
1303
1304         // xargs -- we need version 1.09 at least
1305         if (mustProvide("xargs"))
1306                 packages << "\\usepackage{xargs}[2008/03/08]\n";
1307
1308         if (mustProvide("xy"))
1309                 packages << "\\usepackage[all]{xy}\n";
1310
1311         if (mustProvide("feyn"))
1312                 packages << "\\usepackage{feyn}\n"; //Diagram
1313
1314         if (mustProvide("ulem"))
1315                 packages << "\\PassOptionsToPackage{normalem}{ulem}\n"
1316                             "\\usepackage{ulem}\n";
1317
1318         if (mustProvide("nomencl")) {
1319                 // Make it work with the new and old version of the package,
1320                 // but don't use the compatibility option since it is
1321                 // incompatible to other packages.
1322                 packages << "\\usepackage{nomencl}\n"
1323                             "% the following is useful when we have the old nomencl.sty package\n"
1324                             "\\providecommand{\\printnomenclature}{\\printglossary}\n"
1325                             "\\providecommand{\\makenomenclature}{\\makeglossary}\n"
1326                             "\\makenomenclature\n";
1327         }
1328
1329         // fixltx2e provides subscript
1330         if (mustProvide("subscript") && !isRequired("fixltx2e"))
1331                 packages << "\\usepackage{subscript}\n";
1332
1333         // footmisc must be loaded after setspace
1334         // Set options here, load the package after the user preamble to
1335         // avoid problems with manual loaded footmisc.
1336         if (mustProvide("footmisc"))
1337                 packages << "\\PassOptionsToPackage{stable}{footmisc}\n";
1338
1339         if (mustProvide("microtype")){
1340                 packages << "\\usepackage{microtype}\n";
1341         }
1342
1343         return packages.str();
1344 }
1345
1346
1347 TexString LaTeXFeatures::getMacros() const
1348 {
1349         otexstringstream macros;
1350
1351         if (!preamble_snippets_.empty()) {
1352                 macros << '\n';
1353                 macros << getPreambleSnippets();
1354         }
1355
1356         if (mustProvide("xetexdashbreakstate"))
1357                 macros << "\\XeTeXdashbreakstate 0" << '\n';
1358
1359         if (mustProvide("papersize")) {
1360                 if (runparams_.flavor == OutputParams::LATEX
1361                     || runparams_.flavor == OutputParams::DVILUATEX)
1362                         macros << papersizedvi_def << '\n';
1363                 else if  (runparams_.flavor == OutputParams::LUATEX)
1364                         macros << papersizepdflua_def << '\n';
1365                 else
1366                         macros << papersizepdf_def << '\n';
1367         }
1368
1369         if (mustProvide("LyX")) {
1370                 if (isRequired("hyperref"))
1371                         macros << lyx_hyperref_def << '\n';
1372                 else
1373                         macros << lyx_def << '\n';
1374         }
1375
1376         if (mustProvide("noun"))
1377                 macros << noun_def << '\n';
1378
1379         if (mustProvide("lyxarrow"))
1380                 macros << lyxarrow_def << '\n';
1381
1382         if (mustProvide("lyxzerowidthspace"))
1383                 macros << lyxZWSP_def << '\n';
1384
1385         if (!usePolyglossia() && mustProvide("textgreek")) {
1386             // ensure LGR font encoding is defined also if fontenc is not loaded by LyX
1387                 if (params_.main_font_encoding() == "default")
1388                         macros << textgreek_LGR_def;
1389                 macros << textgreek_def << '\n';
1390         }
1391
1392         if (!usePolyglossia() && mustProvide("textcyr")) {
1393                 // ensure T2A font encoding is set up also if fontenc is not loaded by LyX
1394                 if (params_.main_font_encoding() == "default")
1395                         macros << textcyr_T2A_def;
1396                 macros << textcyr_def << '\n';
1397         }
1398
1399         // non-standard text accents:
1400         if (mustProvide("textcommaabove") || mustProvide("textcommaaboveright") ||
1401             mustProvide("textcommabelow") || mustProvide("textbaltic"))
1402                 macros << lyxaccent_def;
1403
1404         if (mustProvide("textcommabelow") || mustProvide("textbaltic"))
1405                 macros << textcommabelow_def << '\n';
1406
1407         if (mustProvide("textcommaabove") || mustProvide("textbaltic"))
1408                 macros << textcommaabove_def << '\n';
1409
1410         if (mustProvide("textcommaaboveright"))
1411                 macros << textcommaaboveright_def << '\n';
1412
1413         if (mustProvide("textbaltic"))
1414                 macros << textbaltic_def << '\n';
1415
1416         // split-level fractions
1417         if (mustProvide("xfrac") || mustProvide("smallLetterFrac"))
1418                 macros << xfrac_def << '\n';
1419
1420         if (mustProvide("smallLetterFrac"))
1421                 macros << smallLetterFrac_def << '\n';
1422
1423         if (mustProvide("lyxmathsym"))
1424                 macros << lyxmathsym_def << '\n';
1425
1426         if (mustProvide("cedilla"))
1427                 macros << cedilla_def << '\n';
1428
1429         if (mustProvide("subring"))
1430                 macros << subring_def << '\n';
1431
1432         if (mustProvide("subdot"))
1433                 macros << subdot_def << '\n';
1434
1435         if (mustProvide("subhat"))
1436                 macros << subhat_def << '\n';
1437
1438         if (mustProvide("subtilde"))
1439                 macros << subtilde_def << '\n';
1440
1441         if (mustProvide("dacute"))
1442                 macros << dacute_def << '\n';
1443
1444         if (mustProvide("tipasymb"))
1445                 macros << tipasymb_def << '\n';
1446
1447         if (mustProvide("dgrave"))
1448                 macros << dgrave_def << '\n';
1449
1450         if (mustProvide("rcap"))
1451                 macros << rcap_def << '\n';
1452
1453         if (mustProvide("ogonek"))
1454                 macros << ogonek_def << '\n';
1455
1456         // quotes.
1457         if (mustProvide("quotesinglbase"))
1458                 macros << quotesinglbase_def << '\n';
1459         if (mustProvide("quotedblbase"))
1460                 macros << quotedblbase_def << '\n';
1461         if (mustProvide("guilsinglleft"))
1462                 macros << guilsinglleft_def << '\n';
1463         if (mustProvide("guilsinglright"))
1464                 macros << guilsinglright_def << '\n';
1465         if (mustProvide("guillemotleft"))
1466                 macros << guillemotleft_def << '\n';
1467         if (mustProvide("guillemotright"))
1468                 macros << guillemotright_def << '\n';
1469         if (mustProvide("textquotedbl"))
1470                 macros << textquotedbl_def << '\n';
1471         if (mustProvide("textquotesinglep")) {
1472                 if (runparams_.flavor == OutputParams::XETEX)
1473                         macros << textquotesinglep_xetex_def << '\n';
1474                 else
1475                         macros << textquotesinglep_luatex_def << '\n';
1476         }
1477         if (mustProvide("textquotedblp")) {
1478                 if (runparams_.flavor == OutputParams::XETEX)
1479                         macros << textquotedblp_xetex_def << '\n';
1480                 else
1481                         macros << textquotedblp_luatex_def << '\n';
1482         }
1483
1484         // Math mode
1485         if (mustProvide("binom") && !isRequired("amsmath"))
1486                 macros << binom_def << '\n';
1487         if (mustProvide("mathcircumflex"))
1488                 macros << mathcircumflex_def << '\n';
1489
1490         // other
1491         if (mustProvide("ParagraphLeftIndent"))
1492                 macros << paragraphleftindent_def;
1493         if (mustProvide("NeedLyXFootnoteCode"))
1494                 macros << floatingfootnote_def;
1495
1496         // some problems with tex->html converters
1497         if (mustProvide("NeedTabularnewline"))
1498                 macros << tabularnewline_def;
1499
1500         // greyed-out environment (note inset)
1501         // the color is specified in the routine
1502         // getColorOptions() to avoid LaTeX-package clashes
1503         if (mustProvide("lyxgreyedout"))
1504                 macros << lyxgreyedout_def;
1505
1506         if (mustProvide("lyxdot"))
1507                 macros << lyxdot_def << '\n';
1508
1509         // floats
1510         getFloatDefinitions(macros);
1511
1512         if (mustProvide("refstyle"))
1513                 macros << lyxref_def << '\n';
1514
1515         // change tracking
1516         if (mustProvide("ct-dvipost"))
1517                 macros << changetracking_dvipost_def;
1518
1519         if (mustProvide("ct-xcolor-ulem")) {
1520                 streamsize const prec = macros.os().precision(2);
1521
1522                 RGBColor cadd = rgbFromHexName(lcolor.getX11Name(Color_addedtext));
1523                 macros << "\\providecolor{lyxadded}{rgb}{"
1524                        << cadd.r / 255.0 << ',' << cadd.g / 255.0 << ',' << cadd.b / 255.0 << "}\n";
1525
1526                 RGBColor cdel = rgbFromHexName(lcolor.getX11Name(Color_deletedtext));
1527                 macros << "\\providecolor{lyxdeleted}{rgb}{"
1528                        << cdel.r / 255.0 << ',' << cdel.g / 255.0 << ',' << cdel.b / 255.0 << "}\n";
1529
1530                 macros.os().precision(prec);
1531
1532                 if (isRequired("hyperref"))
1533                         macros << changetracking_xcolor_ulem_hyperref_def;
1534                 else
1535                         macros << changetracking_xcolor_ulem_def;
1536         }
1537
1538         if (mustProvide("ct-tikz-math-sout"))
1539                         macros << changetracking_tikz_math_sout_def;
1540
1541         if (mustProvide("ct-none"))
1542                 macros << changetracking_none_def;
1543
1544         if (mustProvide("rtloutputdblcol"))
1545                 macros << rtloutputdblcol_def;
1546
1547         if (mustProvide("lyxmintcaption"))
1548                 macros << lyxmintcaption_def;
1549
1550         return macros.release();
1551 }
1552
1553
1554 docstring const LaTeXFeatures::getBabelPresettings() const
1555 {
1556         odocstringstream tmp;
1557
1558         for (Language const * lang : UsedLanguages_)
1559                 if (!lang->babel_presettings().empty())
1560                         tmp << lang->babel_presettings() << '\n';
1561         if (!params_.language->babel_presettings().empty())
1562                 tmp << params_.language->babel_presettings() << '\n';
1563
1564         if (!contains(tmp.str(), '@'))
1565                 return tmp.str();
1566
1567         return "\\makeatletter\n" + tmp.str() + "\\makeatother\n";
1568 }
1569
1570
1571 docstring const LaTeXFeatures::getBabelPostsettings() const
1572 {
1573         odocstringstream tmp;
1574
1575         for (Language const * lang : UsedLanguages_)
1576                 if (!lang->babel_postsettings().empty())
1577                         tmp << lang->babel_postsettings() << '\n';
1578         if (!params_.language->babel_postsettings().empty())
1579                 tmp << params_.language->babel_postsettings() << '\n';
1580
1581         if (!contains(tmp.str(), '@'))
1582                 return tmp.str();
1583
1584         return "\\makeatletter\n" + tmp.str() + "\\makeatother\n";
1585 }
1586
1587
1588 bool LaTeXFeatures::needBabelLangOptions() const
1589 {
1590         if (!lyxrc.language_global_options || params_.language->asBabelOptions())
1591                 return true;
1592
1593         LanguageList::const_iterator it  = UsedLanguages_.begin();
1594         LanguageList::const_iterator end = UsedLanguages_.end();
1595         for (; it != end; ++it)
1596                 if ((*it)->asBabelOptions())
1597                         return true;
1598
1599         return false;
1600 }
1601
1602
1603 string const LaTeXFeatures::loadAMSPackages() const
1604 {
1605         ostringstream tmp;
1606
1607         if (mustProvide("amsmath")
1608             && params_.use_package("amsmath") != BufferParams::package_off) {
1609                 tmp << "\\usepackage{amsmath}\n";
1610         } else {
1611                 // amsbsy and amstext are already provided by amsmath
1612                 if (mustProvide("amsbsy"))
1613                         tmp << "\\usepackage{amsbsy}\n";
1614                 if (mustProvide("amstext"))
1615                         tmp << "\\usepackage{amstext}\n";
1616         }
1617
1618         if (mustProvide("amsthm"))
1619                 tmp << "\\usepackage{amsthm}\n";
1620
1621         if (mustProvide("amssymb")
1622             && params_.use_package("amssymb") != BufferParams::package_off)
1623                 tmp << "\\usepackage{amssymb}\n";
1624
1625         return tmp.str();
1626 }
1627
1628
1629 docstring const LaTeXFeatures::getTClassPreamble() const
1630 {
1631         // the text class specific preamble
1632         DocumentClass const & tclass = params_.documentClass();
1633         odocstringstream tcpreamble;
1634
1635         tcpreamble << tclass.preamble();
1636
1637         list<docstring>::const_iterator cit = usedLayouts_.begin();
1638         list<docstring>::const_iterator end = usedLayouts_.end();
1639         for (; cit != end; ++cit)
1640                 // For InPreamble layouts, we output the preamble stuff earlier
1641                 // (before the layouts). See Paragraph::Private::validate.
1642                 if (!tclass[*cit].inpreamble)
1643                         tcpreamble << tclass[*cit].preamble();
1644
1645         cit = usedInsetLayouts_.begin();
1646         end = usedInsetLayouts_.end();
1647         TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1648         for (; cit != end; ++cit) {
1649                 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1650                 if (it == ils.end())
1651                         continue;
1652                 tcpreamble << it->second.preamble();
1653         }
1654
1655         return tcpreamble.str();
1656 }
1657
1658
1659 docstring const LaTeXFeatures::getTClassHTMLPreamble() const
1660 {
1661         DocumentClass const & tclass = params_.documentClass();
1662         odocstringstream tcpreamble;
1663
1664         tcpreamble << tclass.htmlpreamble();
1665
1666         list<docstring>::const_iterator cit = usedLayouts_.begin();
1667         list<docstring>::const_iterator end = usedLayouts_.end();
1668         for (; cit != end; ++cit)
1669                 tcpreamble << tclass[*cit].htmlpreamble();
1670
1671         cit = usedInsetLayouts_.begin();
1672         end = usedInsetLayouts_.end();
1673         TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1674         for (; cit != end; ++cit) {
1675                 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1676                 if (it == ils.end())
1677                         continue;
1678                 tcpreamble << it->second.htmlpreamble();
1679         }
1680
1681         return tcpreamble.str();
1682 }
1683
1684
1685 docstring const LaTeXFeatures::getTClassHTMLStyles() const
1686 {
1687         DocumentClass const & tclass = params_.documentClass();
1688         odocstringstream tcpreamble;
1689
1690         if (mustProvide("noun"))
1691                 tcpreamble << lyxnoun_style;
1692         // this isn't exact, but it won't hurt that much if it
1693         // wasn't for this.
1694         if (mustProvide("ulem"))
1695                 tcpreamble << lyxstrikeout_style;
1696
1697         tcpreamble << tclass.htmlstyles();
1698
1699         list<docstring>::const_iterator cit = usedLayouts_.begin();
1700         list<docstring>::const_iterator end = usedLayouts_.end();
1701         for (; cit != end; ++cit)
1702                 tcpreamble << tclass[*cit].htmlstyle();
1703
1704         cit = usedInsetLayouts_.begin();
1705         end = usedInsetLayouts_.end();
1706         TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1707         for (; cit != end; ++cit) {
1708                 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1709                 if (it == ils.end())
1710                         continue;
1711                 tcpreamble << it->second.htmlstyle();
1712         }
1713
1714         return tcpreamble.str();
1715 }
1716
1717
1718 namespace {
1719
1720 docstring const getFloatI18nPreamble(docstring const & type,
1721                         docstring const & name, Language const * lang,
1722                         Encoding const & enc, bool const polyglossia)
1723 {
1724         // Check whether name can be encoded in the buffer encoding
1725         bool encodable = true;
1726         for (size_t i = 0; i < name.size(); ++i) {
1727                 if (!enc.encodable(name[i])) {
1728                         encodable = false;
1729                         break;
1730                 }
1731         }
1732
1733         docstring const language = polyglossia ? from_ascii(lang->polyglossia())
1734                                                : from_ascii(lang->babel());
1735         docstring const langenc = from_ascii(lang->encoding()->iconvName());
1736         docstring const texenc = from_ascii(lang->encoding()->latexName());
1737         docstring const bufenc = from_ascii(enc.iconvName());
1738         docstring const s1 = docstring(1, 0xF0000);
1739         docstring const s2 = docstring(1, 0xF0001);
1740         docstring const translated = encodable ? name
1741                 : from_ascii("\\inputencoding{") + texenc + from_ascii("}")
1742                         + s1 + langenc + s2 + name + s1 + bufenc + s2;
1743
1744         odocstringstream os;
1745         os << "\\addto\\captions" << language
1746            << "{\\renewcommand{\\" << type << "name}{" << translated << "}}\n";
1747         return os.str();
1748 }
1749
1750
1751 docstring const i18npreamble(docstring const & templ, Language const * lang,
1752                              Encoding const & enc, bool const polyglossia,
1753                              bool const need_fixedwidth)
1754 {
1755         if (templ.empty())
1756                 return templ;
1757
1758         string preamble = polyglossia ?
1759                 subst(to_utf8(templ), "$$lang", lang->polyglossia()) :
1760                 subst(to_utf8(templ), "$$lang", lang->babel());
1761
1762         string const langenc = lang->encoding()->iconvName();
1763         string const texenc = lang->encoding()->latexName();
1764         string const bufenc = enc.iconvName();
1765         Encoding const * testenc(&enc);
1766         bool lang_fallback = false;
1767         bool ascii_fallback = false;
1768         if (need_fixedwidth && !enc.hasFixedWidth()) {
1769                 if (lang->encoding()->hasFixedWidth()) {
1770                         testenc = lang->encoding();
1771                         lang_fallback = true;
1772                 } else {
1773                         // We need a fixed width encoding, but both the buffer
1774                         // encoding and the language encoding are variable
1775                         // width. As a last fallback, try to convert to pure
1776                         // ASCII using the LaTeX commands defined in unicodesymbols.
1777                         testenc = encodings.fromLyXName("ascii");
1778                         if (!testenc)
1779                                 return docstring();
1780                         ascii_fallback = true;
1781                 }
1782         }
1783         // First and second character of plane 15 (Private Use Area)
1784         string const s1 = "\xf3\xb0\x80\x80"; // U+F0000
1785         string const s2 = "\xf3\xb0\x80\x81"; // U+F0001
1786         // FIXME UNICODE
1787         // lyx::regex is not unicode-safe.
1788         // Should use QRegExp or (boost::u32regex, but that requires ICU)
1789         static regex const reg("_\\(([^\\)]+)\\)");
1790         smatch sub;
1791         while (regex_search(preamble, sub, reg)) {
1792                 string const key = sub.str(1);
1793                 docstring const name = lang->translateLayout(key);
1794                 // Check whether name can be encoded in the buffer encoding
1795                 bool encodable = true;
1796                 for (size_t i = 0; i < name.size() && encodable; ++i)
1797                         if (!testenc->encodable(name[i]))
1798                                 encodable = false;
1799                 string translated;
1800                 if (encodable && !lang_fallback)
1801                         translated = to_utf8(name);
1802                 else if (ascii_fallback)
1803                         translated = to_ascii(testenc->latexString(name).first);
1804                 else
1805                         translated = "\\inputencoding{" + texenc + "}"
1806                                 + s1 + langenc + s2 + to_utf8(name)
1807                                 + s1 + bufenc + s2;
1808                 preamble = subst(preamble, sub.str(), translated);
1809         }
1810         return from_utf8(preamble);
1811 }
1812
1813 } // namespace
1814
1815
1816 docstring const LaTeXFeatures::getTClassI18nPreamble(bool use_babel,
1817                                 bool use_polyglossia, bool use_minted) const
1818 {
1819         DocumentClass const & tclass = params_.documentClass();
1820         // collect preamble snippets in a set to prevent multiple identical
1821         // commands (would happen if e.g. both theorem and theorem* are used)
1822         set<docstring> snippets;
1823         typedef LanguageList::const_iterator lang_it;
1824         lang_it const lbeg = UsedLanguages_.begin();
1825         lang_it const lend =  UsedLanguages_.end();
1826         list<docstring>::const_iterator cit = usedLayouts_.begin();
1827         list<docstring>::const_iterator end = usedLayouts_.end();
1828         for (; cit != end; ++cit) {
1829                 // language dependent commands (once per document)
1830                 snippets.insert(i18npreamble(tclass[*cit].langpreamble(),
1831                                                 buffer().language(),
1832                                                 buffer().params().encoding(),
1833                                                 use_polyglossia, false));
1834                 // commands for language changing (for multilanguage documents)
1835                 if ((use_babel || use_polyglossia) && !UsedLanguages_.empty()) {
1836                         snippets.insert(i18npreamble(
1837                                                 tclass[*cit].babelpreamble(),
1838                                                 buffer().language(),
1839                                                 buffer().params().encoding(),
1840                                                 use_polyglossia, false));
1841                         for (lang_it lit = lbeg; lit != lend; ++lit)
1842                                 snippets.insert(i18npreamble(
1843                                                 tclass[*cit].babelpreamble(),
1844                                                 *lit,
1845                                                 buffer().params().encoding(),
1846                                                 use_polyglossia, false));
1847                 }
1848         }
1849         if ((use_babel || use_polyglossia) && !UsedLanguages_.empty()) {
1850                 FloatList const & floats = params_.documentClass().floats();
1851                 UsedFloats::const_iterator fit = usedFloats_.begin();
1852                 UsedFloats::const_iterator fend = usedFloats_.end();
1853                 for (; fit != fend; ++fit) {
1854                         Floating const & fl = floats.getType(fit->first);
1855                         // we assume builtin floats are translated
1856                         if (fl.isPredefined())
1857                                 continue;
1858                         docstring const type = from_ascii(fl.floattype());
1859                         docstring const flname = from_utf8(fl.name());
1860                         docstring name = buffer().language()->translateLayout(fl.name());
1861                         // only request translation if we have a real translation
1862                         // (that differs from the source)
1863                         if (flname != name)
1864                                 snippets.insert(getFloatI18nPreamble(
1865                                                 type, name, buffer().language(),
1866                                                 buffer().params().encoding(),
1867                                                 use_polyglossia));
1868                         for (lang_it lit = lbeg; lit != lend; ++lit) {
1869                                 string const code = (*lit)->code();
1870                                 name = (*lit)->translateLayout(fl.name());
1871                                 // we assume we have a suitable translation if
1872                                 // either the language is English (we need to
1873                                 // translate into English if English is a secondary
1874                                 // language) or if translateIfPossible returns
1875                                 // something different to the English source.
1876                                 bool const have_translation =
1877                                         (flname != name || contains(code, "en"));
1878                                 if (have_translation)
1879                                         snippets.insert(getFloatI18nPreamble(
1880                                                 type, name, *lit,
1881                                                 buffer().params().encoding(),
1882                                                 use_polyglossia));
1883                         }
1884                 }
1885         }
1886
1887         cit = usedInsetLayouts_.begin();
1888         end = usedInsetLayouts_.end();
1889         TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1890         for (; cit != end; ++cit) {
1891                 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1892                 if (it == ils.end())
1893                         continue;
1894                 // The listings package does not work with variable width
1895                 // encodings, only with fixed width encodings. Therefore we
1896                 // need to force a fixed width encoding for
1897                 // \lstlistlistingname and \lstlistingname (bug 9382).
1898                 // This needs to be consistent with InsetListings::latex().
1899                 bool const need_fixedwidth = !use_minted &&
1900                                         !runparams_.isFullUnicode() &&
1901                                         it->second.fixedwidthpreambleencoding();
1902                 // language dependent commands (once per document)
1903                 snippets.insert(i18npreamble(it->second.langpreamble(),
1904                                                 buffer().language(),
1905                                                 buffer().params().encoding(),
1906                                                 use_polyglossia, need_fixedwidth));
1907                 // commands for language changing (for multilanguage documents)
1908                 if ((use_babel || use_polyglossia) && !UsedLanguages_.empty()) {
1909                         snippets.insert(i18npreamble(
1910                                                 it->second.babelpreamble(),
1911                                                 buffer().language(),
1912                                                 buffer().params().encoding(),
1913                                                 use_polyglossia, need_fixedwidth));
1914                         for (lang_it lit = lbeg; lit != lend; ++lit)
1915                                 snippets.insert(i18npreamble(
1916                                                 it->second.babelpreamble(),
1917                                                 *lit,
1918                                                 buffer().params().encoding(),
1919                                                 use_polyglossia, need_fixedwidth));
1920                 }
1921         }
1922
1923         odocstringstream tcpreamble;
1924         set<docstring>::const_iterator const send = snippets.end();
1925         set<docstring>::const_iterator it = snippets.begin();
1926         for (; it != send; ++it)
1927                 tcpreamble << *it;
1928         return tcpreamble.str();
1929 }
1930
1931
1932 docstring const LaTeXFeatures::getLyXSGMLEntities() const
1933 {
1934         // Definition of entities used in the document that are LyX related.
1935         odocstringstream entities;
1936
1937         if (mustProvide("lyxarrow")) {
1938                 entities << "<!ENTITY lyxarrow \"-&gt;\">" << '\n';
1939         }
1940
1941         return entities.str();
1942 }
1943
1944
1945 docstring const LaTeXFeatures::getIncludedFiles(string const & fname) const
1946 {
1947         odocstringstream sgmlpreamble;
1948         // FIXME UNICODE
1949         docstring const basename(from_utf8(onlyPath(fname)));
1950
1951         FileMap::const_iterator end = IncludedFiles_.end();
1952         for (FileMap::const_iterator fi = IncludedFiles_.begin();
1953              fi != end; ++fi)
1954                 // FIXME UNICODE
1955                 sgmlpreamble << "\n<!ENTITY " << fi->first
1956                              << (isSGMLFileName(fi->second) ? " SYSTEM \"" : " \"")
1957                              << makeRelPath(from_utf8(fi->second), basename) << "\">";
1958
1959         return sgmlpreamble.str();
1960 }
1961
1962
1963 void LaTeXFeatures::showStruct() const
1964 {
1965         lyxerr << "LyX needs the following commands when LaTeXing:"
1966                << "\n***** Packages:" << getPackages()
1967                << "\n***** Macros:" << to_utf8(getMacros().str)
1968                << "\n***** Textclass stuff:" << to_utf8(getTClassPreamble())
1969                << "\n***** done." << endl;
1970 }
1971
1972
1973 Buffer const & LaTeXFeatures::buffer() const
1974 {
1975         return *buffer_;
1976 }
1977
1978
1979 void LaTeXFeatures::setBuffer(Buffer const & buffer)
1980 {
1981         buffer_ = &buffer;
1982 }
1983
1984
1985 BufferParams const & LaTeXFeatures::bufferParams() const
1986 {
1987         return params_;
1988 }
1989
1990
1991 void LaTeXFeatures::getFloatDefinitions(otexstream & os) const
1992 {
1993         FloatList const & floats = params_.documentClass().floats();
1994
1995         // Here we will output the code to create the needed float styles.
1996         // We will try to do this as minimal as possible.
1997         // \floatstyle{ruled}
1998         // \newfloat{algorithm}{htbp}{loa}
1999         // \providecommand{\algorithmname}{Algorithm}
2000         // \floatname{algorithm}{\protect\algorithmname}
2001         UsedFloats::const_iterator cit = usedFloats_.begin();
2002         UsedFloats::const_iterator end = usedFloats_.end();
2003         for (; cit != end; ++cit) {
2004                 Floating const & fl = floats.getType(cit->first);
2005
2006                 // For builtin floats we do nothing.
2007                 if (fl.isPredefined())
2008                         continue;
2009
2010                 // We have to special case "table" and "figure"
2011                 if (fl.floattype() == "tabular" || fl.floattype() == "figure") {
2012                         // Output code to modify "table" or "figure"
2013                         // but only if builtin == false
2014                         // and that have to be true at this point in the
2015                         // function.
2016                         docstring const type = from_ascii(fl.floattype());
2017                         docstring const placement = from_ascii(fl.placement());
2018                         docstring const style = from_ascii(fl.style());
2019                         if (!style.empty()) {
2020                                 os << "\\floatstyle{" << style << "}\n"
2021                                    << "\\restylefloat{" << type << "}\n";
2022                         }
2023                         if (!placement.empty()) {
2024                                 os << "\\floatplacement{" << type << "}{"
2025                                    << placement << "}\n";
2026                         }
2027                 } else {
2028                         // The other non builtin floats.
2029
2030                         docstring const type = from_ascii(fl.floattype());
2031                         docstring const placement = from_ascii(fl.placement());
2032                         docstring const ext = from_ascii(fl.ext());
2033                         docstring const within = from_ascii(fl.within());
2034                         docstring const style = from_ascii(fl.style());
2035                         docstring const name =
2036                                 buffer().language()->translateLayout(fl.name());
2037                         os << "\\floatstyle{" << style << "}\n"
2038                            << "\\newfloat{" << type << "}{" << placement
2039                            << "}{" << ext << '}';
2040                         if (!within.empty())
2041                                 os << '[' << within << ']';
2042                         os << '\n'
2043                            << "\\providecommand{\\" << type << "name}{"
2044                            << name << "}\n"
2045                            << "\\floatname{" << type << "}{\\protect\\"
2046                            << type << "name}\n";
2047
2048                         // What missing here is to code to minimalize the code
2049                         // output so that the same floatstyle will not be
2050                         // used several times, when the same style is still in
2051                         // effect. (Lgb)
2052                 }
2053                 if (cit->second)
2054                         // The subfig package is loaded later
2055                         os << "\n\\AtBeginDocument{\\newsubfloat{" << from_ascii(fl.floattype()) << "}}\n";
2056         }
2057 }
2058
2059
2060 void LaTeXFeatures::resolveAlternatives()
2061 {
2062         for (Features::iterator it = features_.begin(); it != features_.end();) {
2063                 if (contains(*it, '|')) {
2064                         vector<string> const alternatives = getVectorFromString(*it, "|");
2065                         vector<string>::const_iterator const end = alternatives.end();
2066                         vector<string>::const_iterator ita = alternatives.begin();
2067                         // Is any alternative already required? => use that
2068                         for (; ita != end; ++ita) {
2069                                 if (isRequired(*ita))
2070                                         break;
2071                         }
2072                         // Is any alternative available? => use the first one
2073                         // (bug 9498)
2074                         if (ita == end) {
2075                                 for (ita = alternatives.begin(); ita != end; ++ita) {
2076                                         if (isAvailable(*ita)) {
2077                                                 require(*ita);
2078                                                 break;
2079                                         }
2080                                 }
2081                         }
2082                         // This will not work, but not requiring something
2083                         // would be more confusing
2084                         if (ita == end)
2085                                 require(alternatives.front());
2086                         features_.erase(it);
2087                         it = features_.begin();
2088                 } else
2089                         ++it;
2090         }
2091 }
2092
2093
2094 void LaTeXFeatures::expandMultiples()
2095 {
2096         for (Features::iterator it = features_.begin(); it != features_.end();) {
2097                 if (contains(*it, ',')) {
2098                         vector<string> const multiples = getVectorFromString(*it, ",");
2099                         vector<string>::const_iterator const end = multiples.end();
2100                         vector<string>::const_iterator itm = multiples.begin();
2101                         // Do nothing if any multiple is already required
2102                         for (; itm != end; ++itm) {
2103                                 if (!isRequired(*itm))
2104                                         require(*itm);
2105                         }
2106                         features_.erase(it);
2107                         it = features_.begin();
2108                 } else
2109                         ++it;
2110         }
2111 }
2112
2113
2114 } // namespace lyx