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