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