2 * \file LaTeXFeatures.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
10 * \author André Pönitz
12 * Full author contact details are available in file CREDITS.
17 #include "LaTeXFeatures.h"
20 #include "BufferParams.h"
22 #include "Converter.h"
25 #include "FloatList.h"
27 #include "LaTeXFonts.h"
28 #include "LaTeXPackages.h"
32 #include "TextClass.h"
34 #include "texstream.h"
36 #include "insets/InsetLayout.h"
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"
50 using namespace lyx::support;
55 /////////////////////////////////////////////////////////////////////
59 /////////////////////////////////////////////////////////////////////
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.}
65 static docstring const lyx_def = from_ascii(
66 "\\providecommand{\\LyX}{L\\kern-.1667em\\lower.25em\\hbox{Y}\\kern-.125emX\\@}");
68 static docstring const lyx_hyperref_def = from_ascii(
69 "\\providecommand{\\LyX}{\\texorpdfstring%\n"
70 " {L\\kern-.1667em\\lower.25em\\hbox{Y}\\kern-.125emX\\@}\n"
73 static docstring const noun_def = from_ascii(
74 "\\newcommand{\\noun}[1]{\\textsc{#1}}");
76 static docstring const lyxarrow_def = from_ascii(
77 "\\DeclareRobustCommand*{\\lyxarrow}{%\n"
79 "{\\leavevmode\\,$\\triangleleft$\\,\\allowbreak}\n"
80 "{\\leavevmode\\,$\\triangleright$\\,\\allowbreak}}");
82 // ZERO WIDTH SPACE (ZWSP) is actually not a space character
83 // but marks a line break opportunity. Several commands provide a
84 // line break opportunity. They differ in side-effects:
85 // \allowbreak prevents hyphenation after hyphen or dash + ZWSP
86 // \linebreak[<n>] takes an optional argument denoting "urgency".
87 // The \LyXZeroWidthSpace wrapper allows customization in the preamble.
88 static docstring const lyxZWSP_def = from_ascii(
89 "\\newcommand*\\LyXZeroWidthSpace{\\hspace{0pt}}");
91 // for quotes without babel. This does not give perfect results, but
92 // anybody serious about non-english quotes should use babel (JMarc).
94 static docstring const quotedblbase_def = from_ascii(
95 "\\ProvideTextCommandDefault{\\quotedblbase}{%\n"
96 " \\raisebox{-1.4ex}[1ex][.5ex]{\\textquotedblright}%\n"
97 " \\penalty10000\\hskip0em\\relax%\n"
100 static docstring const quotesinglbase_def = from_ascii(
101 "\\ProvideTextCommandDefault{\\quotesinglbase}{%\n"
102 " \\raisebox{-1.4ex}[1ex][.5ex]{\\textquoteright}%\n"
103 " \\penalty10000\\hskip0em\\relax%\n"
106 static docstring const guillemotleft_def = from_ascii(
107 "\\ProvideTextCommandDefault{\\guillemotleft}{%\n"
108 " {\\usefont{U}{lasy}{m}{n}\\char'50\\kern-.15em\\char'50}%\n"
109 "\\penalty10000\\hskip0pt\\relax%\n"
112 static docstring const guillemotright_def = from_ascii(
113 "\\ProvideTextCommandDefault{\\guillemotright}{%\n"
114 " \\penalty10000\\hskip0pt%\n"
115 " {\\usefont{U}{lasy}{m}{n}\\char'51\\kern-.15em\\char'51}%\n"
118 static docstring const guilsinglleft_def = from_ascii(
119 "\\ProvideTextCommandDefault{\\guilsinglleft}{%\n"
120 " {\\usefont{U}{lasy}{m}{n}\\char'50}%\n"
121 " \\penalty10000\\hskip0pt\\relax%\n"
124 static docstring const guilsinglright_def = from_ascii(
125 "\\ProvideTextCommandDefault{\\guilsinglright}{%\n"
126 " \\penalty10000\\hskip0pt%\n"
127 " {\\usefont{U}{lasy}{m}{n}\\char'51}%\n"
130 static docstring const textquotedbl_def = from_ascii(
131 "\\DeclareTextSymbolDefault{\\textquotedbl}{T1}");
133 static docstring const textquotedblp_xetex_def = from_ascii(
134 "\\providecommand\\textquotedblplain{%\n"
135 " \\bgroup\\addfontfeatures{Mapping=}\\char34\\egroup}");
137 static docstring const textquotedblp_luatex_def = from_ascii(
138 "\\providecommand\\textquotedblplain{%\n"
139 " \\bgroup\\addfontfeatures{RawFeature=-tlig}\\char34\\egroup}");
141 static docstring const textquotesinglep_xetex_def = from_ascii(
142 "\\providecommand\\textquotesingleplain{%\n"
143 " \\bgroup\\addfontfeatures{Mapping=}\\char39\\egroup}");
145 static docstring const textquotesinglep_luatex_def = from_ascii(
146 "\\providecommand\\textquotesingleplain{%\n"
147 " \\bgroup\\addfontfeatures{RawFeature=-tlig}\\char39\\egroup}");
149 static docstring const paragraphleftindent_def = from_ascii(
150 "\\newenvironment{LyXParagraphLeftIndent}[1]%\n"
152 " \\begin{list}{}{%\n"
153 " \\setlength{\\topsep}{0pt}%\n"
154 " \\addtolength{\\leftmargin}{#1}\n"
155 // ho hum, yet more things commented out with no hint as to why they
156 // weren't just removed
157 // "%% \\addtolength{\\leftmargin}{#1\\textwidth}\n"
158 // "%% \\setlength{\\textwidth}{#2\\textwidth}\n"
159 // "%% \\setlength\\listparindent\\parindent%\n"
160 // "%% \\setlength\\itemindent\\parindent%\n"
161 " \\setlength{\\parsep}{0pt plus 1pt}%\n"
167 static docstring const floatingfootnote_def = from_ascii(
168 "%% Special footnote code from the package 'stblftnt.sty'\n"
169 "%% Author: Robin Fairbairns -- Last revised Dec 13 1996\n"
170 "\\let\\SF@@footnote\\footnote\n"
171 "\\def\\footnote{\\ifx\\protect\\@typeset@protect\n"
172 " \\expandafter\\SF@@footnote\n"
174 " \\expandafter\\SF@gobble@opt\n"
177 "\\expandafter\\def\\csname SF@gobble@opt \\endcsname{\\@ifnextchar[%]\n"
178 " \\SF@gobble@twobracket\n"
181 "\\edef\\SF@gobble@opt{\\noexpand\\protect\n"
182 " \\expandafter\\noexpand\\csname SF@gobble@opt \\endcsname}\n"
183 "\\def\\SF@gobble@twobracket[#1]#2{}\n");
185 static docstring const binom_def = from_ascii(
186 "%% Binom macro for standard LaTeX users\n"
187 "\\newcommand{\\binom}[2]{{#1 \\choose #2}}\n");
189 static docstring const mathcircumflex_def = from_ascii(
190 "%% For printing a cirumflex inside a formula\n"
191 "\\newcommand{\\mathcircumflex}[0]{\\mbox{\\^{}}}\n");
193 static docstring const tabularnewline_def = from_ascii(
194 "%% Because html converters don't know tabularnewline\n"
195 "\\providecommand{\\tabularnewline}{\\\\}\n");
197 static docstring const lyxgreyedout_def = from_ascii(
198 "%% The greyedout annotation environment\n"
199 "\\newenvironment{lyxgreyedout}\n"
200 " {\\textcolor{note_fontcolor}\\bgroup\\ignorespaces}\n"
201 " {\\ignorespacesafterend\\egroup}\n");
203 // We want to omit the file extension for includegraphics, but this does not
204 // work when the filename contains other dots.
205 // Idea from http://www.tex.ac.uk/cgi-bin/texfaq2html?label=unkgrfextn
206 static docstring const lyxdot_def = from_ascii(
207 "%% A simple dot to overcome graphicx limitations\n"
208 "\\newcommand{\\lyxdot}{.}\n");
210 static docstring const changetracking_dvipost_def = from_ascii(
211 "%% Change tracking with dvipost\n"
213 "\\dvipost{osstart color push Red}\n"
214 "\\dvipost{osend color pop}\n"
215 "\\dvipost{cbstart color push Blue}\n"
216 "\\dvipost{cbend color pop}\n"
217 "\\DeclareRobustCommand{\\lyxadded}[3]{\\changestart#3\\changeend}\n"
218 "\\DeclareRobustCommand{\\lyxdeleted}[3]{%\n"
219 "\\changestart\\overstrikeon#3\\overstrikeoff\\changeend}\n");
221 static docstring const changetracking_xcolor_ulem_def = from_ascii(
222 "%% Change tracking with ulem\n"
223 "\\DeclareRobustCommand{\\lyxadded}[3]{{\\color{lyxadded}{}#3}}\n"
224 "\\DeclareRobustCommand{\\lyxdeleted}[3]{{\\color{lyxdeleted}\\lyxsout{#3}}}\n"
225 "\\DeclareRobustCommand{\\lyxsout}[1]{\\ifx\\\\#1\\else\\sout{#1}\\fi}\n");
227 static docstring const changetracking_xcolor_ulem_hyperref_def = from_ascii(
228 "%% Change tracking with ulem\n"
229 "\\DeclareRobustCommand{\\lyxadded}[3]{{\\texorpdfstring{\\color{lyxadded}{}}{}#3}}\n"
230 "\\DeclareRobustCommand{\\lyxdeleted}[3]{{\\texorpdfstring{\\color{lyxdeleted}\\lyxsout{#3}}{}}}\n"
231 "\\DeclareRobustCommand{\\lyxsout}[1]{\\ifx\\\\#1\\else\\sout{#1}\\fi}\n");
233 static docstring const changetracking_tikz_math_sout_def = from_ascii(
234 "%% Strike out display math with tikz\n"
235 "\\usepackage{tikz}\n"
236 "\\usetikzlibrary{calc}\n"
237 "\\newcommand{\\lyxmathsout}[1]{%\n"
238 " \\tikz[baseline=(math.base)]{\n"
239 " \\node[inner sep=0pt,outer sep=0pt](math){#1};\n"
240 " \\draw($(math.south west)+(2em,.5em)$)--($(math.north east)-(2em,.5em)$);\n"
244 static docstring const changetracking_none_def = from_ascii(
245 "\\newcommand{\\lyxadded}[3]{#3}\n"
246 "\\newcommand{\\lyxdeleted}[3]{}\n");
248 static docstring const textgreek_LGR_def = from_ascii(
249 "\\DeclareFontEncoding{LGR}{}{}\n");
250 static docstring const textgreek_def = from_ascii(
251 "\\DeclareRobustCommand{\\greektext}{%\n"
252 " \\fontencoding{LGR}\\selectfont\\def\\encodingdefault{LGR}}\n"
253 "\\DeclareRobustCommand{\\textgreek}[1]{\\leavevmode{\\greektext #1}}\n");
255 static docstring const textcyr_T2A_def = from_ascii(
256 "\\InputIfFileExists{t2aenc.def}{}{%\n"
257 " \\errmessage{File `t2aenc.def' not found: Cyrillic script not supported}}\n");
258 static docstring const textcyr_def = from_ascii(
259 "\\DeclareRobustCommand{\\cyrtext}{%\n"
260 " \\fontencoding{T2A}\\selectfont\\def\\encodingdefault{T2A}}\n"
261 "\\DeclareRobustCommand{\\textcyr}[1]{\\leavevmode{\\cyrtext #1}}\n");
263 static docstring const lyxmathsym_def = from_ascii(
264 "\\newcommand{\\lyxmathsym}[1]{\\ifmmode\\begingroup\\def\\b@ld{bold}\n"
265 " \\text{\\ifx\\math@version\\b@ld\\bfseries\\fi#1}\\endgroup\\else#1\\fi}\n");
267 static docstring const papersizedvi_def = from_ascii(
268 "\\special{papersize=\\the\\paperwidth,\\the\\paperheight}\n");
270 static docstring const papersizepdf_def = from_ascii(
271 "\\pdfpageheight\\paperheight\n"
272 "\\pdfpagewidth\\paperwidth\n");
274 static docstring const papersizepdflua_def = from_ascii(
275 "% Backwards compatibility for LuaTeX < 0.90\n"
276 "\\@ifundefined{pageheight}{\\let\\pageheight\\pdfpageheight}{}\n"
277 "\\@ifundefined{pagewidth}{\\let\\pagewidth\\pdfpagewidth}{}\n"
278 "\\pageheight\\paperheight\n"
279 "\\pagewidth\\paperwidth\n");
281 static docstring const cedilla_def = from_ascii(
282 "\\newcommand{\\docedilla}[2]{\\underaccent{#1\\mathchar'30}{#2}}\n"
283 "\\newcommand{\\cedilla}[1]{\\mathpalette\\docedilla{#1}}\n");
285 static docstring const subring_def = from_ascii(
286 "\\newcommand{\\dosubring}[2]{\\underaccent{#1\\mathchar'27}{#2}}\n"
287 "\\newcommand{\\subring}[1]{\\mathpalette\\dosubring{#1}}\n");
289 static docstring const subdot_def = from_ascii(
290 "\\newcommand{\\dosubdot}[2]{\\underaccent{#1.}{#2}}\n"
291 "\\newcommand{\\subdot}[1]{\\mathpalette\\dosubdot{#1}}\n");
293 static docstring const subhat_def = from_ascii(
294 "\\newcommand{\\dosubhat}[2]{\\underaccent{#1\\mathchar'136}{#2}}\n"
295 "\\newcommand{\\subhat}[1]{\\mathpalette\\dosubhat{#1}}\n");
297 static docstring const subtilde_def = from_ascii(
298 "\\newcommand{\\dosubtilde}[2]{\\underaccent{#1\\mathchar'176}{#2}}\n"
299 "\\newcommand{\\subtilde}[1]{\\mathpalette\\dosubtilde{#1}}\n");
301 static docstring const dacute_def = from_ascii(
302 "\\DeclareMathAccent{\\dacute}{\\mathalpha}{operators}{'175}\n");
304 static docstring const tipasymb_def = from_ascii(
305 "\\DeclareFontEncoding{T3}{}{}\n"
306 "\\DeclareSymbolFont{tipasymb}{T3}{cmr}{m}{n}\n");
308 static docstring const dgrave_def = from_ascii(
309 "\\DeclareMathAccent{\\dgrave}{\\mathord}{tipasymb}{'15}\n");
311 static docstring const rcap_def = from_ascii(
312 "\\DeclareMathAccent{\\rcap}{\\mathord}{tipasymb}{'20}\n");
314 static docstring const ogonek_def = from_ascii(
315 "\\newcommand{\\doogonek}[2]{\\setbox0=\\hbox{$#1#2$}\\underaccent{#1\\mkern-6mu\n"
316 " \\ifx#2O\\hskip0.5\\wd0\\else\\ifx#2U\\hskip0.5\\wd0\\else\\hskip\\wd0\\fi\\fi\n"
317 " \\ifx#2o\\mkern-2mu\\else\\ifx#2e\\mkern-1mu\\fi\\fi\n"
318 " \\mathchar\"0\\hexnumber@\\symtipasymb0C}{#2}}\n"
319 "\\newcommand{\\ogonek}[1]{\\mathpalette\\doogonek{#1}}\n");
321 static docstring const lyxaccent_def = from_ascii(
322 "%% custom text accent \\LyxTextAccent[<rise value (length)>]{<accent>}{<base>}\n"
323 "\\newcommand*{\\LyxTextAccent}[3][0ex]{%\n"
324 " \\hmode@bgroup\\ooalign{\\null#3\\crcr\\hidewidth\n"
325 " \\raise#1\\hbox{#2}\\hidewidth}\\egroup}\n"
326 "%% select a font size smaller than the current font size:\n"
327 "\\newcommand{\\LyxAccentSize}[1][\\sf@size]{%\n"
328 " \\check@mathfonts\\fontsize#1\\z@\\math@fontsfalse\\selectfont\n"
331 static docstring const textcommabelow_def = from_ascii(
332 "\\ProvideTextCommandDefault{\\textcommabelow}[1]{%%\n"
333 " \\LyxTextAccent[-.31ex]{\\LyxAccentSize,}{#1}}\n");
335 static docstring const textcommaabove_def = from_ascii(
336 "\\ProvideTextCommandDefault{\\textcommaabove}[1]{%%\n"
337 " \\LyxTextAccent[.5ex]{\\LyxAccentSize`}{#1}}\n");
339 static docstring const textcommaaboveright_def = from_ascii(
340 "\\ProvideTextCommandDefault{\\textcommaaboveright}[1]{%%\n"
341 " \\LyxTextAccent[.5ex]{\\LyxAccentSize\\ '}{#1}}\n");
343 // Baltic languages use a comma-accent instead of a cedilla
344 static docstring const textbaltic_def = from_ascii(
345 "%% use comma accent instead of cedilla for these characters:\n"
346 "\\DeclareTextCompositeCommand{\\c}{T1}{g}{\\textcommaabove{g}}\n"
347 "\\DeclareTextCompositeCommand{\\c}{T1}{G}{\\textcommabelow{G}}\n"
348 "\\DeclareTextCompositeCommand{\\c}{T1}{k}{\\textcommabelow{k}}\n"
349 "\\DeclareTextCompositeCommand{\\c}{T1}{K}{\\textcommabelow{K}}\n"
350 "\\DeclareTextCompositeCommand{\\c}{T1}{l}{\\textcommabelow{l}}\n"
351 "\\DeclareTextCompositeCommand{\\c}{T1}{L}{\\textcommabelow{L}}\n"
352 "\\DeclareTextCompositeCommand{\\c}{T1}{n}{\\textcommabelow{n}}\n"
353 "\\DeclareTextCompositeCommand{\\c}{T1}{N}{\\textcommabelow{N}}\n"
354 "\\DeclareTextCompositeCommand{\\c}{T1}{r}{\\textcommabelow{r}}\n"
355 "\\DeclareTextCompositeCommand{\\c}{T1}{R}{\\textcommabelow{R}}\n");
357 // split-level fractions
358 static docstring const xfrac_def = from_ascii(
359 "\\usepackage{xfrac}\n");
360 static docstring const smallLetterFrac_def = from_ascii(
361 "\\DeclareCollectionInstance{smallLetterFrac}{xfrac}{default}{text}\n"
362 " {phantom=c, scale-factor=1.0, slash-left-kern=-.05em}\n"
363 "\\DeclareCollectionInstance{smallLetterFrac}{xfrac}{lmr}{text}\n"
364 " {slash-symbol-font=ptm, phantom=c, scale-factor=1, slash-left-kern=-.05em}\n"
365 "\\DeclareCollectionInstance{smallLetterFrac}{xfrac}{lmss}{text}\n"
366 " {slash-symbol-font=ptm, phantom=c, scale-factor=1, slash-left-kern=-.05em}\n"
367 "\\DeclareCollectionInstance{smallLetterFrac}{xfrac}{cmr}{text}\n"
368 " {slash-symbol-font=ptm, phantom=c, scale-factor=1, slash-left-kern=-.05em}\n"
369 "\\DeclareCollectionInstance{smallLetterFrac}{xfrac}{cmss}{text}\n"
370 " {slash-symbol-font=ptm, phantom=c, scale-factor=1, slash-left-kern=-.05em}\n"
371 "\\newcommand{\\smallLetterFrac}[2]{%\n"
372 " {\\UseCollection{xfrac}{smallLetterFrac}\\sfrac{#1}{#2}}}\n");
374 static docstring const lyxref_def = from_ascii(
375 "\\RS@ifundefined{subsecref}\n"
376 " {\\newref{subsec}{name = \\RSsectxt}}\n"
378 "\\RS@ifundefined{thmref}\n"
379 " {\\def\\RSthmtxt{theorem~}\\newref{thm}{name = \\RSthmtxt}}\n"
381 "\\RS@ifundefined{lemref}\n"
382 " {\\def\\RSlemtxt{lemma~}\\newref{lem}{name = \\RSlemtxt}}\n"
385 // Make sure the columns are also outputed as rtl
386 static docstring const rtloutputdblcol_def = from_ascii(
387 "\\def\\@outputdblcol{%\n"
388 " \\if@firstcolumn\n"
389 " \\global \\@firstcolumnfalse\n"
390 " \\global \\setbox\\@leftcolumn \\box\\@outputbox\n"
392 " \\global \\@firstcolumntrue\n"
393 " \\setbox\\@outputbox \\vbox {%\n"
394 " \\hb@xt@\\textwidth {%\n"
395 " \\kern\\textwidth \\kern-\\columnwidth %**\n"
396 " \\hb@xt@\\columnwidth {%\n"
397 " \\box\\@leftcolumn \\hss}%\n"
398 " \\kern-\\textwidth %**\n"
400 " {\\normalcolor\\vrule \\@width\\columnseprule}%\n"
402 " \\kern-\\textwidth %**\n"
403 " \\hb@xt@\\columnwidth {%\n"
404 " \\box\\@outputbox \\hss}%\n"
405 " \\kern-\\columnwidth \\kern\\textwidth %**\n"
408 " \\@combinedblfloats\n"
411 " \\@dblfloatplacement\n"
412 " \\@startdblcolumn\n"
413 " \\@whilesw\\if@fcolmade \\fi\n"
415 " \\@startdblcolumn}%\n"
419 "\\@mparswitchtrue\n");
421 static docstring const lyxmintcaption_def = from_ascii(
422 "\\long\\def\\lyxmintcaption[#1]#2{%\n"
423 " \\ifx#1t\\vskip\\baselineskip\\fi%\n"
424 " \\refstepcounter{listing}\\noindent%\n"
425 " \\addcontentsline{lol}{listing}%\n"
426 " {\\protect\\numberline{\\thelisting}{\\ignorespaces #2}}%\n"
427 " \\setbox\\@tempboxa\\hbox{\\listingscaption~\\thelisting: #2}%\n"
428 " \\ifdim \\wd\\@tempboxa >\\linewidth%\n"
429 " \\parbox[t]{\\linewidth}{\\unhbox\\@tempboxa}\\else%\n"
430 " \\hbox to\\linewidth{\\hfil\\box\\@tempboxa\\hfil}\\fi%\n"
431 " \\ifx#1b\\vskip\\baselineskip\\fi\n"
435 /////////////////////////////////////////////////////////////////////
439 /////////////////////////////////////////////////////////////////////
441 static docstring const lyxnoun_style = from_ascii(
443 " font-variant: small-caps;\n"
447 // this is how it normally renders, but it might not always do so.
448 static docstring const lyxstrikeout_style = from_ascii(
450 " text-decoration: line-through;\n"
454 /////////////////////////////////////////////////////////////////////
458 /////////////////////////////////////////////////////////////////////
461 LaTeXFeatures::LaTeXFeatures(Buffer const & b, BufferParams const & p,
462 OutputParams const & r)
463 : buffer_(&b), params_(p), runparams_(r), in_float_(false),
464 in_deleted_inset_(false)
468 LaTeXFeatures::LangPackage LaTeXFeatures::langPackage() const
470 string const local_lp = bufferParams().lang_package;
472 // Locally, custom is just stored as a string
473 // in bufferParams().lang_package.
474 if (local_lp != "auto"
475 && local_lp != "babel"
476 && local_lp != "default"
477 && local_lp != "none")
478 return LANG_PACK_CUSTOM;
480 if (local_lp == "none")
481 return LANG_PACK_NONE;
483 /* If "auto" is selected, we load polyglossia with non-TeX fonts,
484 * else we select babel.
485 * If babel is selected (either directly or via the "auto"
486 * mechanism), we really do only require it if we have
487 * a language that needs it.
489 bool const polyglossia_required =
490 params_.useNonTeXFonts
491 && isAvailable("polyglossia")
492 && !isProvided("babel")
493 && this->hasOnlyPolyglossiaLanguages();
494 bool const babel_required =
495 !bufferParams().language->babel().empty()
496 || !this->getBabelLanguages().empty();
498 if (local_lp == "auto") {
499 // polyglossia requirement has priority over babel
500 if (polyglossia_required)
501 return LANG_PACK_POLYGLOSSIA;
502 else if (babel_required)
503 return LANG_PACK_BABEL;
506 if (local_lp == "babel") {
508 return LANG_PACK_BABEL;
511 if (local_lp == "default") {
512 switch (lyxrc.language_package_selection) {
514 // polyglossia requirement has priority over babel
515 if (polyglossia_required)
516 return LANG_PACK_POLYGLOSSIA;
517 else if (babel_required)
518 return LANG_PACK_BABEL;
520 case LyXRC::LP_BABEL:
522 return LANG_PACK_BABEL;
524 case LyXRC::LP_CUSTOM:
525 return LANG_PACK_CUSTOM;
527 return LANG_PACK_NONE;
531 return LANG_PACK_NONE;
535 void LaTeXFeatures::require(string const & name)
537 features_.insert(name);
541 void LaTeXFeatures::require(set<string> const & names)
543 features_.insert(names.begin(), names.end());
547 void LaTeXFeatures::useLayout(docstring const & layoutname)
549 useLayout(layoutname, 0);
553 void LaTeXFeatures::useLayout(docstring const & layoutname, int level)
555 // Some code to avoid loops in dependency definition
556 const int maxlevel = 30;
557 if (level > maxlevel) {
558 lyxerr << "LaTeXFeatures::useLayout: maximum level of "
559 << "recursion attained by layout "
560 << to_utf8(layoutname) << endl;
564 DocumentClass const & tclass = params_.documentClass();
565 if (tclass.hasLayout(layoutname)) {
566 // Is this layout already in usedLayouts?
567 if (find(usedLayouts_.begin(), usedLayouts_.end(), layoutname)
568 != usedLayouts_.end())
571 Layout const & layout = tclass[layoutname];
572 require(layout.requires());
574 if (!layout.depends_on().empty()) {
575 useLayout(layout.depends_on(), level + 1);
577 usedLayouts_.push_back(layoutname);
579 lyxerr << "LaTeXFeatures::useLayout: layout `"
580 << to_utf8(layoutname) << "' does not exist in this class"
586 void LaTeXFeatures::useInsetLayout(InsetLayout const & lay)
588 docstring const & lname = lay.name();
589 DocumentClass const & tclass = params_.documentClass();
591 // this is a default inset layout, nothing useful here
592 if (!tclass.hasInsetLayout(lname))
594 // Is this layout already in usedInsetLayouts?
595 if (find(usedInsetLayouts_.begin(), usedInsetLayouts_.end(), lname)
596 != usedInsetLayouts_.end())
599 require(lay.requires());
600 usedInsetLayouts_.push_back(lname);
604 bool LaTeXFeatures::isRequired(string const & name) const
606 return features_.find(name) != features_.end();
610 bool LaTeXFeatures::isProvided(string const & name) const
612 if (params_.useNonTeXFonts)
613 return params_.documentClass().provides(name);
615 bool const ot1 = (params_.main_font_encoding() == "default"
616 || params_.main_font_encoding() == "OT1");
617 bool const complete = (params_.fontsSans() == "default"
618 && params_.fontsTypewriter() == "default");
619 bool const nomath = (params_.fontsMath() == "default");
620 return params_.documentClass().provides(name)
621 || theLaTeXFonts().getLaTeXFont(
622 from_ascii(params_.fontsRoman())).provides(name, ot1,
625 || theLaTeXFonts().getLaTeXFont(
626 from_ascii(params_.fontsSans())).provides(name, ot1,
629 || theLaTeXFonts().getLaTeXFont(
630 from_ascii(params_.fontsTypewriter())).provides(name, ot1,
633 || theLaTeXFonts().getLaTeXFont(
634 from_ascii(params_.fontsMath())).provides(name, ot1,
637 // TODO: "textbaltic" provided, if the font-encoding is "L7x"
638 // "textgreek" provided, if a language with font-encoding LGR is used in the document
639 // "textcyr" provided, if a language with font-encoding T2A is used in the document
643 bool LaTeXFeatures::mustProvide(string const & name) const
645 return isRequired(name) && !isProvided(name);
649 bool LaTeXFeatures::isAvailable(string const & name)
651 string::size_type const i = name.find("->");
652 if (i != string::npos) {
653 string const from = name.substr(0,i);
654 string const to = name.substr(i+2);
655 //LYXERR0("from=[" << from << "] to=[" << to << "]");
656 return theConverters().isReachable(from, to);
658 return LaTeXPackages::isAvailable(name);
664 void addSnippet(std::list<TexString> & list, TexString ts, bool allow_dupes)
667 // test the absense of duplicates, i.e. elements with same str
668 none_of(list.begin(), list.end(), [&](TexString const & ts2){
669 return ts.str == ts2.str;
672 list.push_back(move(ts));
676 TexString getSnippets(std::list<TexString> const & list)
678 otexstringstream snip;
679 for (TexString const & ts : list)
680 snip << TexString(ts) << '\n';
681 return snip.release();
687 void LaTeXFeatures::addPreambleSnippet(TexString ts, bool allow_dupes)
689 addSnippet(preamble_snippets_, move(ts), allow_dupes);
693 void LaTeXFeatures::addPreambleSnippet(docstring const & str, bool allow_dupes)
695 addSnippet(preamble_snippets_, TexString(str), allow_dupes);
699 void LaTeXFeatures::addCSSSnippet(std::string const & snippet)
701 addSnippet(css_snippets_, TexString(from_ascii(snippet)), false);
705 TexString LaTeXFeatures::getPreambleSnippets() const
707 return getSnippets(preamble_snippets_);
711 docstring LaTeXFeatures::getCSSSnippets() const
713 return getSnippets(css_snippets_).str;
718 void LaTeXFeatures::useFloat(string const & name, bool subfloat)
720 if (!usedFloats_[name])
721 usedFloats_[name] = subfloat;
724 // We only need float.sty if we use non builtin floats, or if we
725 // use the "H" modifier. This includes modified table and
726 // figure floats. (Lgb)
727 Floating const & fl = params_.documentClass().floats().getType(name);
728 if (!fl.floattype().empty() && fl.usesFloatPkg()) {
734 void LaTeXFeatures::useLanguage(Language const * lang)
736 if (!lang->babel().empty() || !lang->polyglossia().empty())
737 UsedLanguages_.insert(lang);
738 if (!lang->requires().empty())
739 require(lang->requires());
740 // CJK languages do not have a babel name.
741 // They use the CJK package
742 if (lang->encoding()->package() == Encoding::CJK)
744 // japanese package is special
745 if (lang->encoding()->package() == Encoding::japanese)
750 void LaTeXFeatures::includeFile(docstring const & key, string const & name)
752 IncludedFiles_[key] = name;
756 bool LaTeXFeatures::hasLanguages() const
758 return !UsedLanguages_.empty();
762 bool LaTeXFeatures::hasOnlyPolyglossiaLanguages() const
764 // first the main language
765 if (params_.language->polyglossia().empty())
767 // now the secondary languages
768 LanguageList::const_iterator const begin = UsedLanguages_.begin();
769 for (LanguageList::const_iterator cit = begin;
770 cit != UsedLanguages_.end();
772 if ((*cit)->polyglossia().empty())
779 bool LaTeXFeatures::hasPolyglossiaExclusiveLanguages() const
781 // first the main language
782 if (params_.language->isPolyglossiaExclusive())
784 // now the secondary languages
785 LanguageList::const_iterator const begin = UsedLanguages_.begin();
786 for (LanguageList::const_iterator cit = begin;
787 cit != UsedLanguages_.end();
789 if ((*cit)->isPolyglossiaExclusive())
796 vector<string> LaTeXFeatures::getPolyglossiaExclusiveLanguages() const
798 vector<string> result;
799 // first the main language
800 if (params_.language->isPolyglossiaExclusive())
801 result.push_back(params_.language->display());
802 // now the secondary languages
803 LanguageList::const_iterator const begin = UsedLanguages_.begin();
804 for (LanguageList::const_iterator cit = begin;
805 cit != UsedLanguages_.end();
807 if ((*cit)->isPolyglossiaExclusive())
808 result.push_back((*cit)->display());
814 vector<string> LaTeXFeatures::getBabelExclusiveLanguages() const
816 vector<string> result;
817 // first the main language
818 if (params_.language->isBabelExclusive())
819 result.push_back(params_.language->display());
820 // now the secondary languages
821 LanguageList::const_iterator const begin = UsedLanguages_.begin();
822 for (LanguageList::const_iterator cit = begin;
823 cit != UsedLanguages_.end();
825 if ((*cit)->isBabelExclusive())
826 result.push_back((*cit)->display());
832 string LaTeXFeatures::getBabelLanguages() const
837 LanguageList::const_iterator const begin = UsedLanguages_.begin();
838 for (LanguageList::const_iterator cit = begin;
839 cit != UsedLanguages_.end();
841 if ((*cit)->babel().empty())
847 langs << (*cit)->babel();
853 set<string> LaTeXFeatures::getPolyglossiaLanguages() const
857 LanguageList::const_iterator const begin = UsedLanguages_.begin();
858 for (LanguageList::const_iterator cit = begin;
859 cit != UsedLanguages_.end();
861 // We do not need the variants here
862 langs.insert((*cit)->polyglossia());
868 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding) const
870 // This does only find encodings of languages supported by babel, but
871 // that does not matter since we don't have a language with an
872 // encoding supported by inputenc but without babel support.
874 for (auto const & lang : UsedLanguages_)
875 if (lang->encoding()->latexName() != doc_encoding &&
876 (lang->encoding()->package() == Encoding::inputenc
877 || lang->encoding()->package() == Encoding::japanese))
878 encs.insert(lang->encoding()->latexName());
883 void LaTeXFeatures::getFontEncodings(vector<string> & encs) const
885 // these must be loaded if glyphs of this script are used
886 // unless a language providing them is used in the document
887 if (mustProvide("textgreek")
888 && find(encs.begin(), encs.end(), "LGR") == encs.end())
889 encs.insert(encs.begin(), "LGR");
890 if (mustProvide("textcyr")
891 && find(encs.begin(), encs.end(), "T2A") == encs.end())
892 encs.insert(encs.begin(), "T2A");
894 for (auto const & lang : UsedLanguages_)
895 if (!lang->fontenc().empty()
896 && ascii_lowercase(lang->fontenc()) != "none") {
897 vector<string> extraencs = getVectorFromString(lang->fontenc());
898 for (auto const & extra : extraencs) {
899 if (find(encs.begin(), encs.end(), extra) == encs.end())
900 encs.insert(encs.begin(), extra);
907 char const * simplefeatures[] = {
908 // note that the package order here will be the same in the LaTeX-output
916 // subfig is handled in BufferParams.cpp
920 /*For a successful cooperation of the `wrapfig' package with the
921 `float' package you should load the `wrapfig' package *after*
922 the `float' package. See the caption package documentation
953 // listings is handled in BufferParams.cpp
976 char const * bibliofeatures[] = {
977 // Known bibliography packages (will be loaded before natbib)
993 int const nb_bibliofeatures = sizeof(bibliofeatures) / sizeof(char const *);
995 int const nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *);
1000 string const LaTeXFeatures::getColorOptions() const
1002 ostringstream colors;
1004 // Handling the color packages separately is needed to be able to load them
1005 // before babel when hyperref is loaded with the colorlinks option
1006 // for more info see Bufferparams.cpp
1009 if (mustProvide("color") || mustProvide("xcolor")) {
1010 string const package =
1011 (mustProvide("xcolor") ? "xcolor" : "color");
1012 if (params_.graphics_driver == "default"
1013 || params_.graphics_driver == "none")
1014 colors << "\\usepackage{" << package << "}\n";
1016 colors << "\\usepackage["
1017 << params_.graphics_driver
1018 << "]{" << package << "}\n";
1021 // pdfcolmk must be loaded after color
1022 if (mustProvide("pdfcolmk"))
1023 colors << "\\usepackage{pdfcolmk}\n";
1025 // the following 3 color commands must be set after color
1026 // is loaded and before pdfpages, therefore add the command
1027 // here define the set color
1028 if (mustProvide("pagecolor")) {
1029 colors << "\\definecolor{page_backgroundcolor}{rgb}{";
1030 colors << outputLaTeXColor(params_.backgroundcolor) << "}\n";
1031 // set the page color
1032 colors << "\\pagecolor{page_backgroundcolor}\n";
1035 if (mustProvide("fontcolor")) {
1036 colors << "\\definecolor{document_fontcolor}{rgb}{";
1037 colors << outputLaTeXColor(params_.fontcolor) << "}\n";
1039 colors << "\\color{document_fontcolor}\n";
1042 if (mustProvide("lyxgreyedout")) {
1043 colors << "\\definecolor{note_fontcolor}{rgb}{";
1044 colors << outputLaTeXColor(params_.notefontcolor) << "}\n";
1045 // the color will be set together with the definition of
1046 // the lyxgreyedout environment (see lyxgreyedout_def)
1049 // color for shaded boxes
1050 if (isRequired("framed") && mustProvide("color")) {
1051 colors << "\\definecolor{shadecolor}{rgb}{";
1052 colors << outputLaTeXColor(params_.boxbgcolor) << "}\n";
1053 // this color is automatically used by the LaTeX-package "framed"
1056 return colors.str();
1060 string const LaTeXFeatures::getPackageOptions() const
1062 ostringstream packageopts;
1063 // Output all the package option stuff we have been asked to do.
1064 map<string, string>::const_iterator it =
1065 params_.documentClass().packageOptions().begin();
1066 map<string, string>::const_iterator en =
1067 params_.documentClass().packageOptions().end();
1068 for (; it != en; ++it)
1069 if (mustProvide(it->first))
1070 packageopts << "\\PassOptionsToPackage{" << it->second << "}"
1071 << "{" << it->first << "}\n";
1072 return packageopts.str();
1076 string const LaTeXFeatures::getPackages() const
1078 ostringstream packages;
1080 // FIXME: currently, we can only load packages and macros known
1082 // However, with the Require tag of layouts/custom insets,
1083 // also unknown packages can be requested. They are silently
1084 // swallowed now. We should change this eventually.
1086 // These are all the 'simple' includes. i.e
1087 // packages which we just \usepackage{package}
1088 for (int i = 0; i < nb_simplefeatures; ++i) {
1089 if (mustProvide(simplefeatures[i]))
1090 packages << "\\usepackage{" << simplefeatures[i] << "}\n";
1093 // The rest of these packages are somewhat more complicated
1094 // than those above.
1096 // The tipa package and its extensions (tipx, tone) must not
1097 // be loaded with non-TeX fonts, since fontspec includes the
1098 // respective macros
1099 if (mustProvide("tipa") && !params_.useNonTeXFonts)
1100 packages << "\\usepackage{tipa}\n";
1101 if (mustProvide("tipx") && !params_.useNonTeXFonts)
1102 packages << "\\usepackage{tipx}\n";
1103 if (mustProvide("extraipa") && !params_.useNonTeXFonts)
1104 packages << "\\usepackage{extraipa}\n";
1105 if (mustProvide("tone") && !params_.useNonTeXFonts)
1106 packages << "\\usepackage{tone}\n";
1108 // if fontspec or newtxmath is used, AMS packages have to be loaded
1109 // before fontspec (in BufferParams)
1110 string const amsPackages = loadAMSPackages();
1111 bool const ot1 = (params_.main_font_encoding() == "default"
1112 || params_.main_font_encoding() == "OT1");
1113 bool const use_newtxmath =
1114 theLaTeXFonts().getLaTeXFont(from_ascii(params_.fontsMath())).getUsedPackage(
1115 ot1, false, false) == "newtxmath";
1117 if (!params_.useNonTeXFonts && !use_newtxmath && !amsPackages.empty())
1118 packages << amsPackages;
1120 if (mustProvide("cancel") &&
1121 params_.use_package("cancel") != BufferParams::package_off)
1122 packages << "\\usepackage{cancel}\n";
1124 // marvosym and bbding both define the \Cross macro
1125 if (mustProvide("marvosym")) {
1126 if (mustProvide("bbding"))
1127 packages << "\\let\\Cross\\relax\n";
1128 packages << "\\usepackage{marvosym}\n";
1131 // accents must be loaded after amsmath
1132 if (mustProvide("accents") &&
1133 params_.use_package("accents") != BufferParams::package_off)
1134 packages << "\\usepackage{accents}\n";
1136 // mathdots must be loaded after amsmath
1137 if (mustProvide("mathdots") &&
1138 params_.use_package("mathdots") != BufferParams::package_off)
1139 packages << "\\usepackage{mathdots}\n";
1141 // yhmath must be loaded after amsmath
1142 if (mustProvide("yhmath") &&
1143 params_.use_package("yhmath") != BufferParams::package_off)
1144 packages << "\\usepackage{yhmath}\n";
1146 // stmaryrd must be loaded after amsmath
1147 if (mustProvide("stmaryrd") &&
1148 params_.use_package("stmaryrd") != BufferParams::package_off)
1149 packages << "\\usepackage{stmaryrd}\n";
1151 if (mustProvide("stackrel") &&
1152 params_.use_package("stackrel") != BufferParams::package_off)
1153 packages << "\\usepackage{stackrel}\n";
1155 if (mustProvide("undertilde") &&
1156 params_.use_package("undertilde") != BufferParams::package_off)
1157 packages << "\\usepackage{undertilde}\n";
1159 // [x]color and pdfcolmk are handled in getColorOptions() above
1162 if (isRequired("makeidx") || isRequired("splitidx")) {
1163 if (!isProvided("makeidx") && !isRequired("splitidx"))
1164 packages << "\\usepackage{makeidx}\n";
1165 if (mustProvide("splitidx"))
1166 packages << "\\usepackage{splitidx}\n";
1167 packages << "\\makeindex\n";
1171 if (mustProvide("graphicx") && params_.graphics_driver != "none") {
1172 if (params_.graphics_driver == "default")
1173 packages << "\\usepackage{graphicx}\n";
1175 packages << "\\usepackage["
1176 << params_.graphics_driver
1180 // lyxskak.sty --- newer chess support based on skak.sty
1181 if (mustProvide("chess"))
1182 packages << "\\usepackage[ps,mover]{lyxskak}\n";
1185 if (mustProvide("setspace") && !isProvided("SetSpace"))
1186 packages << "\\usepackage{setspace}\n";
1188 // we need to assure that mhchem is loaded before esint and every other
1189 // package that redefines command of amsmath because mhchem loads amlatex
1190 // (this info is from the author of mhchem from June 2013)
1191 if (mustProvide("mhchem") &&
1192 params_.use_package("mhchem") != BufferParams::package_off)
1193 packages << "\\PassOptionsToPackage{version=3}{mhchem}\n"
1194 "\\usepackage{mhchem}\n";
1196 // wasysym is a simple feature, but it must be after amsmath if both
1198 // wasysym redefines some integrals (e.g. iint) from amsmath. That
1199 // leads to inconsistent integrals. We only load this package if
1200 // the document does not contain integrals (then isRequired("esint")
1201 // is false) or if esint is used, since esint redefines all relevant
1202 // integral symbols from wasysym and amsmath.
1203 // See http://www.lyx.org/trac/ticket/1942
1204 if (mustProvide("wasysym") &&
1205 params_.use_package("wasysym") != BufferParams::package_off &&
1206 (params_.use_package("esint") != BufferParams::package_off || !isRequired("esint")))
1207 packages << "\\usepackage{wasysym}\n";
1209 // esint must be after amsmath (and packages requiring amsmath, like mhchem)
1210 // and wasysym, since it will redeclare inconsistent integral symbols
1211 if (mustProvide("esint") &&
1212 params_.use_package("esint") != BufferParams::package_off)
1213 packages << "\\usepackage{esint}\n";
1215 // Known bibliography packages (simple \usepackage{package})
1216 for (int i = 0; i < nb_bibliofeatures; ++i) {
1217 if (mustProvide(bibliofeatures[i]))
1218 packages << "\\usepackage{"
1219 << bibliofeatures[i] << "}\n";
1222 // Compatibility between achicago and natbib
1223 if (mustProvide("achicago") && mustProvide("natbib"))
1224 packages << "\\let\\achicagobib\\thebibliography\n";
1227 // Some classes load natbib themselves, but still allow (or even require)
1228 // plain numeric citations (ReVTeX is such a case, see bug 5182).
1229 // This special case is indicated by the "natbib-internal" key.
1230 if (mustProvide("natbib")
1231 && !isProvided("natbib-internal")
1232 && !isProvided("biblatex")
1233 && !isProvided("biblatex-natbib")
1234 && !isProvided("jurabib")) {
1235 packages << "\\usepackage[";
1236 if (params_.citeEngineType() == ENGINE_TYPE_NUMERICAL)
1237 packages << "numbers";
1239 packages << "authoryear";
1240 if (!params_.biblio_opts.empty())
1241 packages << ',' << params_.biblio_opts;
1242 packages << "]{natbib}\n";
1245 // Compatibility between achicago and natbib
1246 if (mustProvide("achicago") && mustProvide("natbib")) {
1247 packages << "\\let\\thebibliography\\achicagobib\n";
1248 packages << "\\let\\SCcite\\astroncite\n";
1249 packages << "\\let\\UnexpandableProtect\\protect\n";
1252 // jurabib -- we need version 0.6 at least.
1253 if (mustProvide("jurabib")
1254 && !isProvided("natbib-internal")
1255 && !isProvided("natbib")
1256 && !isProvided("biblatex")
1257 && !isProvided("biblatex-natbib")) {
1258 packages << "\\usepackage";
1259 if (!params_.biblio_opts.empty())
1260 packages << '[' << params_.biblio_opts << ']';
1261 packages << "{jurabib}[2004/01/25]\n";
1264 // opcit -- we pass custombst as we output \bibliographystyle ourselves
1265 if (mustProvide("opcit")) {
1266 if (isRequired("hyperref"))
1267 packages << "\\usepackage[custombst,hyperref]{opcit}\n";
1269 packages << "\\usepackage[custombst]{opcit}\n";
1272 // xargs -- we need version 1.09 at least
1273 if (mustProvide("xargs"))
1274 packages << "\\usepackage{xargs}[2008/03/08]\n";
1276 if (mustProvide("xy"))
1277 packages << "\\usepackage[all]{xy}\n";
1279 if (mustProvide("feyn"))
1280 packages << "\\usepackage{feyn}\n"; //Diagram
1282 if (mustProvide("ulem"))
1283 packages << "\\PassOptionsToPackage{normalem}{ulem}\n"
1284 "\\usepackage{ulem}\n";
1286 if (mustProvide("nomencl")) {
1287 // Make it work with the new and old version of the package,
1288 // but don't use the compatibility option since it is
1289 // incompatible to other packages.
1290 packages << "\\usepackage{nomencl}\n"
1291 "% the following is useful when we have the old nomencl.sty package\n"
1292 "\\providecommand{\\printnomenclature}{\\printglossary}\n"
1293 "\\providecommand{\\makenomenclature}{\\makeglossary}\n"
1294 "\\makenomenclature\n";
1297 // fixltx2e provides subscript
1298 if (mustProvide("subscript") && !isRequired("fixltx2e"))
1299 packages << "\\usepackage{subscript}\n";
1301 // footmisc must be loaded after setspace
1302 // Set options here, load the package after the user preamble to
1303 // avoid problems with manual loaded footmisc.
1304 if (mustProvide("footmisc"))
1305 packages << "\\PassOptionsToPackage{stable}{footmisc}\n";
1307 if (mustProvide("microtype")){
1308 packages << "\\usepackage{microtype}\n";
1311 return packages.str();
1315 TexString LaTeXFeatures::getMacros() const
1317 otexstringstream macros;
1319 if (!preamble_snippets_.empty()) {
1321 macros << getPreambleSnippets();
1324 if (mustProvide("xetexdashbreakstate"))
1325 macros << "\\XeTeXdashbreakstate 0" << '\n';
1327 if (mustProvide("papersize")) {
1328 if (runparams_.flavor == OutputParams::LATEX
1329 || runparams_.flavor == OutputParams::DVILUATEX)
1330 macros << papersizedvi_def << '\n';
1331 else if (runparams_.flavor == OutputParams::LUATEX)
1332 macros << papersizepdflua_def << '\n';
1334 macros << papersizepdf_def << '\n';
1337 if (mustProvide("LyX")) {
1338 if (isRequired("hyperref"))
1339 macros << lyx_hyperref_def << '\n';
1341 macros << lyx_def << '\n';
1344 if (mustProvide("noun"))
1345 macros << noun_def << '\n';
1347 if (mustProvide("lyxarrow"))
1348 macros << lyxarrow_def << '\n';
1350 if (mustProvide("lyxzerowidthspace"))
1351 macros << lyxZWSP_def << '\n';
1353 if (!usePolyglossia() && mustProvide("textgreek")) {
1354 // ensure LGR font encoding is defined also if fontenc is not loaded by LyX
1355 if (params_.main_font_encoding() == "default")
1356 macros << textgreek_LGR_def;
1357 macros << textgreek_def << '\n';
1360 if (!usePolyglossia() && mustProvide("textcyr")) {
1361 // ensure T2A font encoding is set up also if fontenc is not loaded by LyX
1362 if (params_.main_font_encoding() == "default")
1363 macros << textcyr_T2A_def;
1364 macros << textcyr_def << '\n';
1367 // non-standard text accents:
1368 if (mustProvide("textcommaabove") || mustProvide("textcommaaboveright") ||
1369 mustProvide("textcommabelow") || mustProvide("textbaltic"))
1370 macros << lyxaccent_def;
1372 if (mustProvide("textcommabelow") || mustProvide("textbaltic"))
1373 macros << textcommabelow_def << '\n';
1375 if (mustProvide("textcommaabove") || mustProvide("textbaltic"))
1376 macros << textcommaabove_def << '\n';
1378 if (mustProvide("textcommaaboveright"))
1379 macros << textcommaaboveright_def << '\n';
1381 if (mustProvide("textbaltic"))
1382 macros << textbaltic_def << '\n';
1384 // split-level fractions
1385 if (mustProvide("xfrac") || mustProvide("smallLetterFrac"))
1386 macros << xfrac_def << '\n';
1388 if (mustProvide("smallLetterFrac"))
1389 macros << smallLetterFrac_def << '\n';
1391 if (mustProvide("lyxmathsym"))
1392 macros << lyxmathsym_def << '\n';
1394 if (mustProvide("cedilla"))
1395 macros << cedilla_def << '\n';
1397 if (mustProvide("subring"))
1398 macros << subring_def << '\n';
1400 if (mustProvide("subdot"))
1401 macros << subdot_def << '\n';
1403 if (mustProvide("subhat"))
1404 macros << subhat_def << '\n';
1406 if (mustProvide("subtilde"))
1407 macros << subtilde_def << '\n';
1409 if (mustProvide("dacute"))
1410 macros << dacute_def << '\n';
1412 if (mustProvide("tipasymb"))
1413 macros << tipasymb_def << '\n';
1415 if (mustProvide("dgrave"))
1416 macros << dgrave_def << '\n';
1418 if (mustProvide("rcap"))
1419 macros << rcap_def << '\n';
1421 if (mustProvide("ogonek"))
1422 macros << ogonek_def << '\n';
1425 if (mustProvide("quotesinglbase"))
1426 macros << quotesinglbase_def << '\n';
1427 if (mustProvide("quotedblbase"))
1428 macros << quotedblbase_def << '\n';
1429 if (mustProvide("guilsinglleft"))
1430 macros << guilsinglleft_def << '\n';
1431 if (mustProvide("guilsinglright"))
1432 macros << guilsinglright_def << '\n';
1433 if (mustProvide("guillemotleft"))
1434 macros << guillemotleft_def << '\n';
1435 if (mustProvide("guillemotright"))
1436 macros << guillemotright_def << '\n';
1437 if (mustProvide("textquotedbl"))
1438 macros << textquotedbl_def << '\n';
1439 if (mustProvide("textquotesinglep")) {
1440 if (runparams_.flavor == OutputParams::XETEX)
1441 macros << textquotesinglep_xetex_def << '\n';
1443 macros << textquotesinglep_luatex_def << '\n';
1445 if (mustProvide("textquotedblp")) {
1446 if (runparams_.flavor == OutputParams::XETEX)
1447 macros << textquotedblp_xetex_def << '\n';
1449 macros << textquotedblp_luatex_def << '\n';
1453 if (mustProvide("binom") && !isRequired("amsmath"))
1454 macros << binom_def << '\n';
1455 if (mustProvide("mathcircumflex"))
1456 macros << mathcircumflex_def << '\n';
1459 if (mustProvide("ParagraphLeftIndent"))
1460 macros << paragraphleftindent_def;
1461 if (mustProvide("NeedLyXFootnoteCode"))
1462 macros << floatingfootnote_def;
1464 // some problems with tex->html converters
1465 if (mustProvide("NeedTabularnewline"))
1466 macros << tabularnewline_def;
1468 // greyed-out environment (note inset)
1469 // the color is specified in the routine
1470 // getColorOptions() to avoid LaTeX-package clashes
1471 if (mustProvide("lyxgreyedout"))
1472 macros << lyxgreyedout_def;
1474 if (mustProvide("lyxdot"))
1475 macros << lyxdot_def << '\n';
1478 getFloatDefinitions(macros);
1480 if (mustProvide("refstyle"))
1481 macros << lyxref_def << '\n';
1484 if (mustProvide("ct-dvipost"))
1485 macros << changetracking_dvipost_def;
1487 if (mustProvide("ct-xcolor-ulem")) {
1488 streamsize const prec = macros.os().precision(2);
1490 RGBColor cadd = rgbFromHexName(lcolor.getX11Name(Color_addedtext));
1491 macros << "\\providecolor{lyxadded}{rgb}{"
1492 << cadd.r / 255.0 << ',' << cadd.g / 255.0 << ',' << cadd.b / 255.0 << "}\n";
1494 RGBColor cdel = rgbFromHexName(lcolor.getX11Name(Color_deletedtext));
1495 macros << "\\providecolor{lyxdeleted}{rgb}{"
1496 << cdel.r / 255.0 << ',' << cdel.g / 255.0 << ',' << cdel.b / 255.0 << "}\n";
1498 macros.os().precision(prec);
1500 if (isRequired("hyperref"))
1501 macros << changetracking_xcolor_ulem_hyperref_def;
1503 macros << changetracking_xcolor_ulem_def;
1506 if (mustProvide("ct-tikz-math-sout"))
1507 macros << changetracking_tikz_math_sout_def;
1509 if (mustProvide("ct-none"))
1510 macros << changetracking_none_def;
1512 if (mustProvide("rtloutputdblcol"))
1513 macros << rtloutputdblcol_def;
1515 if (mustProvide("lyxmintcaption"))
1516 macros << lyxmintcaption_def;
1518 return macros.release();
1522 docstring const LaTeXFeatures::getBabelPresettings() const
1524 odocstringstream tmp;
1526 for (Language const * lang : UsedLanguages_)
1527 if (!lang->babel_presettings().empty())
1528 tmp << lang->babel_presettings() << '\n';
1529 if (!params_.language->babel_presettings().empty())
1530 tmp << params_.language->babel_presettings() << '\n';
1532 if (!contains(tmp.str(), '@'))
1535 return "\\makeatletter\n" + tmp.str() + "\\makeatother\n";
1539 docstring const LaTeXFeatures::getBabelPostsettings() const
1541 odocstringstream tmp;
1543 for (Language const * lang : UsedLanguages_)
1544 if (!lang->babel_postsettings().empty())
1545 tmp << lang->babel_postsettings() << '\n';
1546 if (!params_.language->babel_postsettings().empty())
1547 tmp << params_.language->babel_postsettings() << '\n';
1549 if (!contains(tmp.str(), '@'))
1552 return "\\makeatletter\n" + tmp.str() + "\\makeatother\n";
1556 bool LaTeXFeatures::needBabelLangOptions() const
1558 if (!lyxrc.language_global_options || params_.language->asBabelOptions())
1561 LanguageList::const_iterator it = UsedLanguages_.begin();
1562 LanguageList::const_iterator end = UsedLanguages_.end();
1563 for (; it != end; ++it)
1564 if ((*it)->asBabelOptions())
1571 string const LaTeXFeatures::loadAMSPackages() const
1575 if (mustProvide("amsmath")
1576 && params_.use_package("amsmath") != BufferParams::package_off) {
1577 tmp << "\\usepackage{amsmath}\n";
1579 // amsbsy and amstext are already provided by amsmath
1580 if (mustProvide("amsbsy"))
1581 tmp << "\\usepackage{amsbsy}\n";
1582 if (mustProvide("amstext"))
1583 tmp << "\\usepackage{amstext}\n";
1586 if (mustProvide("amsthm"))
1587 tmp << "\\usepackage{amsthm}\n";
1589 if (mustProvide("amssymb")
1590 && params_.use_package("amssymb") != BufferParams::package_off)
1591 tmp << "\\usepackage{amssymb}\n";
1597 docstring const LaTeXFeatures::getTClassPreamble() const
1599 // the text class specific preamble
1600 DocumentClass const & tclass = params_.documentClass();
1601 odocstringstream tcpreamble;
1603 tcpreamble << tclass.preamble();
1605 list<docstring>::const_iterator cit = usedLayouts_.begin();
1606 list<docstring>::const_iterator end = usedLayouts_.end();
1607 for (; cit != end; ++cit)
1608 // For InPreamble layouts, we output the preamble stuff earlier
1609 // (before the layouts). See Paragraph::Private::validate.
1610 if (!tclass[*cit].inpreamble)
1611 tcpreamble << tclass[*cit].preamble();
1613 cit = usedInsetLayouts_.begin();
1614 end = usedInsetLayouts_.end();
1615 TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1616 for (; cit != end; ++cit) {
1617 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1618 if (it == ils.end())
1620 tcpreamble << it->second.preamble();
1623 return tcpreamble.str();
1627 docstring const LaTeXFeatures::getTClassHTMLPreamble() const
1629 DocumentClass const & tclass = params_.documentClass();
1630 odocstringstream tcpreamble;
1632 tcpreamble << tclass.htmlpreamble();
1634 list<docstring>::const_iterator cit = usedLayouts_.begin();
1635 list<docstring>::const_iterator end = usedLayouts_.end();
1636 for (; cit != end; ++cit)
1637 tcpreamble << tclass[*cit].htmlpreamble();
1639 cit = usedInsetLayouts_.begin();
1640 end = usedInsetLayouts_.end();
1641 TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1642 for (; cit != end; ++cit) {
1643 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1644 if (it == ils.end())
1646 tcpreamble << it->second.htmlpreamble();
1649 return tcpreamble.str();
1653 docstring const LaTeXFeatures::getTClassHTMLStyles() const
1655 DocumentClass const & tclass = params_.documentClass();
1656 odocstringstream tcpreamble;
1658 if (mustProvide("noun"))
1659 tcpreamble << lyxnoun_style;
1660 // this isn't exact, but it won't hurt that much if it
1662 if (mustProvide("ulem"))
1663 tcpreamble << lyxstrikeout_style;
1665 tcpreamble << tclass.htmlstyles();
1667 list<docstring>::const_iterator cit = usedLayouts_.begin();
1668 list<docstring>::const_iterator end = usedLayouts_.end();
1669 for (; cit != end; ++cit)
1670 tcpreamble << tclass[*cit].htmlstyle();
1672 cit = usedInsetLayouts_.begin();
1673 end = usedInsetLayouts_.end();
1674 TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1675 for (; cit != end; ++cit) {
1676 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1677 if (it == ils.end())
1679 tcpreamble << it->second.htmlstyle();
1682 return tcpreamble.str();
1688 docstring const getFloatI18nPreamble(docstring const & type,
1689 docstring const & name, Language const * lang,
1690 Encoding const & enc, bool const polyglossia)
1692 // Check whether name can be encoded in the buffer encoding
1693 bool encodable = true;
1694 for (size_t i = 0; i < name.size(); ++i) {
1695 if (!enc.encodable(name[i])) {
1701 docstring const language = polyglossia ? from_ascii(lang->polyglossia())
1702 : from_ascii(lang->babel());
1703 docstring const langenc = from_ascii(lang->encoding()->iconvName());
1704 docstring const texenc = from_ascii(lang->encoding()->latexName());
1705 docstring const bufenc = from_ascii(enc.iconvName());
1706 docstring const s1 = docstring(1, 0xF0000);
1707 docstring const s2 = docstring(1, 0xF0001);
1708 docstring const translated = encodable ? name
1709 : from_ascii("\\inputencoding{") + texenc + from_ascii("}")
1710 + s1 + langenc + s2 + name + s1 + bufenc + s2;
1712 odocstringstream os;
1713 os << "\\addto\\captions" << language
1714 << "{\\renewcommand{\\" << type << "name}{" << translated << "}}\n";
1719 docstring const i18npreamble(docstring const & templ, Language const * lang,
1720 Encoding const & enc, bool const polyglossia,
1721 bool const need_fixedwidth)
1726 string preamble = polyglossia ?
1727 subst(to_utf8(templ), "$$lang", lang->polyglossia()) :
1728 subst(to_utf8(templ), "$$lang", lang->babel());
1730 string const langenc = lang->encoding()->iconvName();
1731 string const texenc = lang->encoding()->latexName();
1732 string const bufenc = enc.iconvName();
1733 Encoding const * testenc(&enc);
1734 bool lang_fallback = false;
1735 bool ascii_fallback = false;
1736 if (need_fixedwidth && !enc.hasFixedWidth()) {
1737 if (lang->encoding()->hasFixedWidth()) {
1738 testenc = lang->encoding();
1739 lang_fallback = true;
1741 // We need a fixed width encoding, but both the buffer
1742 // encoding and the language encoding are variable
1743 // width. As a last fallback, try to convert to pure
1744 // ASCII using the LaTeX commands defined in unicodesymbols.
1745 testenc = encodings.fromLyXName("ascii");
1748 ascii_fallback = true;
1751 // First and second character of plane 15 (Private Use Area)
1752 string const s1 = "\xf3\xb0\x80\x80"; // U+F0000
1753 string const s2 = "\xf3\xb0\x80\x81"; // U+F0001
1755 // lyx::regex is not unicode-safe.
1756 // Should use QRegExp or (boost::u32regex, but that requires ICU)
1757 static regex const reg("_\\(([^\\)]+)\\)");
1759 while (regex_search(preamble, sub, reg)) {
1760 string const key = sub.str(1);
1761 docstring const name = lang->translateLayout(key);
1762 // Check whether name can be encoded in the buffer encoding
1763 bool encodable = true;
1764 for (size_t i = 0; i < name.size() && encodable; ++i)
1765 if (!testenc->encodable(name[i]))
1768 if (encodable && !lang_fallback)
1769 translated = to_utf8(name);
1770 else if (ascii_fallback)
1771 translated = to_ascii(testenc->latexString(name).first);
1773 translated = "\\inputencoding{" + texenc + "}"
1774 + s1 + langenc + s2 + to_utf8(name)
1776 preamble = subst(preamble, sub.str(), translated);
1778 return from_utf8(preamble);
1784 docstring const LaTeXFeatures::getTClassI18nPreamble(bool use_babel,
1785 bool use_polyglossia, bool use_minted) const
1787 DocumentClass const & tclass = params_.documentClass();
1788 // collect preamble snippets in a set to prevent multiple identical
1789 // commands (would happen if e.g. both theorem and theorem* are used)
1790 set<docstring> snippets;
1791 typedef LanguageList::const_iterator lang_it;
1792 lang_it const lbeg = UsedLanguages_.begin();
1793 lang_it const lend = UsedLanguages_.end();
1794 list<docstring>::const_iterator cit = usedLayouts_.begin();
1795 list<docstring>::const_iterator end = usedLayouts_.end();
1796 for (; cit != end; ++cit) {
1797 // language dependent commands (once per document)
1798 snippets.insert(i18npreamble(tclass[*cit].langpreamble(),
1799 buffer().language(),
1800 buffer().params().encoding(),
1801 use_polyglossia, false));
1802 // commands for language changing (for multilanguage documents)
1803 if ((use_babel || use_polyglossia) && !UsedLanguages_.empty()) {
1804 snippets.insert(i18npreamble(
1805 tclass[*cit].babelpreamble(),
1806 buffer().language(),
1807 buffer().params().encoding(),
1808 use_polyglossia, false));
1809 for (lang_it lit = lbeg; lit != lend; ++lit)
1810 snippets.insert(i18npreamble(
1811 tclass[*cit].babelpreamble(),
1813 buffer().params().encoding(),
1814 use_polyglossia, false));
1817 if ((use_babel || use_polyglossia) && !UsedLanguages_.empty()) {
1818 FloatList const & floats = params_.documentClass().floats();
1819 UsedFloats::const_iterator fit = usedFloats_.begin();
1820 UsedFloats::const_iterator fend = usedFloats_.end();
1821 for (; fit != fend; ++fit) {
1822 Floating const & fl = floats.getType(fit->first);
1823 // we assume builtin floats are translated
1824 if (fl.isPredefined())
1826 docstring const type = from_ascii(fl.floattype());
1827 docstring const flname = from_utf8(fl.name());
1828 docstring name = buffer().language()->translateLayout(fl.name());
1829 // only request translation if we have a real translation
1830 // (that differs from the source)
1832 snippets.insert(getFloatI18nPreamble(
1833 type, name, buffer().language(),
1834 buffer().params().encoding(),
1836 for (lang_it lit = lbeg; lit != lend; ++lit) {
1837 string const code = (*lit)->code();
1838 name = (*lit)->translateLayout(fl.name());
1839 // we assume we have a suitable translation if
1840 // either the language is English (we need to
1841 // translate into English if English is a secondary
1842 // language) or if translateIfPossible returns
1843 // something different to the English source.
1844 bool const have_translation =
1845 (flname != name || contains(code, "en"));
1846 if (have_translation)
1847 snippets.insert(getFloatI18nPreamble(
1849 buffer().params().encoding(),
1855 cit = usedInsetLayouts_.begin();
1856 end = usedInsetLayouts_.end();
1857 TextClass::InsetLayouts const & ils = tclass.insetLayouts();
1858 for (; cit != end; ++cit) {
1859 TextClass::InsetLayouts::const_iterator it = ils.find(*cit);
1860 if (it == ils.end())
1862 // The listings package does not work with variable width
1863 // encodings, only with fixed width encodings. Therefore we
1864 // need to force a fixed width encoding for
1865 // \lstlistlistingname and \lstlistingname (bug 9382).
1866 // This needs to be consistent with InsetListings::latex().
1867 bool const need_fixedwidth = !use_minted &&
1868 !runparams_.isFullUnicode() &&
1869 it->second.fixedwidthpreambleencoding();
1870 // language dependent commands (once per document)
1871 snippets.insert(i18npreamble(it->second.langpreamble(),
1872 buffer().language(),
1873 buffer().params().encoding(),
1874 use_polyglossia, need_fixedwidth));
1875 // commands for language changing (for multilanguage documents)
1876 if ((use_babel || use_polyglossia) && !UsedLanguages_.empty()) {
1877 snippets.insert(i18npreamble(
1878 it->second.babelpreamble(),
1879 buffer().language(),
1880 buffer().params().encoding(),
1881 use_polyglossia, need_fixedwidth));
1882 for (lang_it lit = lbeg; lit != lend; ++lit)
1883 snippets.insert(i18npreamble(
1884 it->second.babelpreamble(),
1886 buffer().params().encoding(),
1887 use_polyglossia, need_fixedwidth));
1891 odocstringstream tcpreamble;
1892 set<docstring>::const_iterator const send = snippets.end();
1893 set<docstring>::const_iterator it = snippets.begin();
1894 for (; it != send; ++it)
1896 return tcpreamble.str();
1900 docstring const LaTeXFeatures::getLyXSGMLEntities() const
1902 // Definition of entities used in the document that are LyX related.
1903 odocstringstream entities;
1905 if (mustProvide("lyxarrow")) {
1906 entities << "<!ENTITY lyxarrow \"->\">" << '\n';
1909 return entities.str();
1913 docstring const LaTeXFeatures::getIncludedFiles(string const & fname) const
1915 odocstringstream sgmlpreamble;
1917 docstring const basename(from_utf8(onlyPath(fname)));
1919 FileMap::const_iterator end = IncludedFiles_.end();
1920 for (FileMap::const_iterator fi = IncludedFiles_.begin();
1923 sgmlpreamble << "\n<!ENTITY " << fi->first
1924 << (isSGMLFileName(fi->second) ? " SYSTEM \"" : " \"")
1925 << makeRelPath(from_utf8(fi->second), basename) << "\">";
1927 return sgmlpreamble.str();
1931 void LaTeXFeatures::showStruct() const
1933 lyxerr << "LyX needs the following commands when LaTeXing:"
1934 << "\n***** Packages:" << getPackages()
1935 << "\n***** Macros:" << to_utf8(getMacros().str)
1936 << "\n***** Textclass stuff:" << to_utf8(getTClassPreamble())
1937 << "\n***** done." << endl;
1941 Buffer const & LaTeXFeatures::buffer() const
1947 void LaTeXFeatures::setBuffer(Buffer const & buffer)
1953 BufferParams const & LaTeXFeatures::bufferParams() const
1959 void LaTeXFeatures::getFloatDefinitions(otexstream & os) const
1961 FloatList const & floats = params_.documentClass().floats();
1963 // Here we will output the code to create the needed float styles.
1964 // We will try to do this as minimal as possible.
1965 // \floatstyle{ruled}
1966 // \newfloat{algorithm}{htbp}{loa}
1967 // \providecommand{\algorithmname}{Algorithm}
1968 // \floatname{algorithm}{\protect\algorithmname}
1969 UsedFloats::const_iterator cit = usedFloats_.begin();
1970 UsedFloats::const_iterator end = usedFloats_.end();
1971 for (; cit != end; ++cit) {
1972 Floating const & fl = floats.getType(cit->first);
1974 // For builtin floats we do nothing.
1975 if (fl.isPredefined())
1978 // We have to special case "table" and "figure"
1979 if (fl.floattype() == "tabular" || fl.floattype() == "figure") {
1980 // Output code to modify "table" or "figure"
1981 // but only if builtin == false
1982 // and that have to be true at this point in the
1984 docstring const type = from_ascii(fl.floattype());
1985 docstring const placement = from_ascii(fl.placement());
1986 docstring const style = from_ascii(fl.style());
1987 if (!style.empty()) {
1988 os << "\\floatstyle{" << style << "}\n"
1989 << "\\restylefloat{" << type << "}\n";
1991 if (!placement.empty()) {
1992 os << "\\floatplacement{" << type << "}{"
1993 << placement << "}\n";
1996 // The other non builtin floats.
1998 docstring const type = from_ascii(fl.floattype());
1999 docstring const placement = from_ascii(fl.placement());
2000 docstring const ext = from_ascii(fl.ext());
2001 docstring const within = from_ascii(fl.within());
2002 docstring const style = from_ascii(fl.style());
2003 docstring const name =
2004 buffer().language()->translateLayout(fl.name());
2005 os << "\\floatstyle{" << style << "}\n"
2006 << "\\newfloat{" << type << "}{" << placement
2007 << "}{" << ext << '}';
2008 if (!within.empty())
2009 os << '[' << within << ']';
2011 << "\\providecommand{\\" << type << "name}{"
2013 << "\\floatname{" << type << "}{\\protect\\"
2014 << type << "name}\n";
2016 // What missing here is to code to minimalize the code
2017 // output so that the same floatstyle will not be
2018 // used several times, when the same style is still in
2022 // The subfig package is loaded later
2023 os << "\n\\AtBeginDocument{\\newsubfloat{" << from_ascii(fl.floattype()) << "}}\n";
2028 void LaTeXFeatures::resolveAlternatives()
2030 for (Features::iterator it = features_.begin(); it != features_.end();) {
2031 if (contains(*it, '|')) {
2032 vector<string> const alternatives = getVectorFromString(*it, "|");
2033 vector<string>::const_iterator const end = alternatives.end();
2034 vector<string>::const_iterator ita = alternatives.begin();
2035 // Is any alternative already required? => use that
2036 for (; ita != end; ++ita) {
2037 if (isRequired(*ita))
2040 // Is any alternative available? => use the first one
2043 for (ita = alternatives.begin(); ita != end; ++ita) {
2044 if (isAvailable(*ita)) {
2050 // This will not work, but not requiring something
2051 // would be more confusing
2053 require(alternatives.front());
2054 features_.erase(it);
2055 it = features_.begin();
2062 void LaTeXFeatures::expandMultiples()
2064 for (Features::iterator it = features_.begin(); it != features_.end();) {
2065 if (contains(*it, ',')) {
2066 vector<string> const multiples = getVectorFromString(*it, ",");
2067 vector<string>::const_iterator const end = multiples.end();
2068 vector<string>::const_iterator itm = multiples.begin();
2069 // Do nothing if any multiple is already required
2070 for (; itm != end; ++itm) {
2071 if (!isRequired(*itm))
2074 features_.erase(it);
2075 it = features_.begin();