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