2 * \file tex2lyx/text.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Jean-Marc Lasgouttes
10 * Full author contact details are available in file CREDITS.
21 #include "FloatList.h"
22 #include "LaTeXPackages.h"
27 #include "insets/ExternalTemplate.h"
29 #include "support/lassert.h"
30 #include "support/convert.h"
31 #include "support/FileName.h"
32 #include "support/filetools.h"
33 #include "support/lstrings.h"
34 #include "support/lyxtime.h"
43 using namespace lyx::support;
50 void output_arguments(ostream &, Parser &, bool, bool, string, Context &,
51 Layout::LaTeXArgMap const &);
56 void parse_text_in_inset(Parser & p, ostream & os, unsigned flags, bool outer,
57 Context const & context, InsetLayout const * layout,
60 bool const forcePlainLayout =
61 layout ? layout->forcePlainLayout() : false;
62 Context newcontext(true, context.textclass);
64 newcontext.layout = &context.textclass.plainLayout();
66 newcontext.font = context.font;
68 output_arguments(os, p, outer, false, string(), newcontext,
70 // If we have a latex param, we eat it here.
71 if (!context.latexparam.empty()) {
73 Context dummy(true, context.textclass);
74 parse_text(p, oss, FLAG_RDELIM, outer, dummy,
75 string(1, context.latexparam.back()));
77 parse_text(p, os, flags, outer, newcontext, rdelim);
79 output_arguments(os, p, outer, false, "post", newcontext,
80 layout->postcommandargs());
81 newcontext.check_end_layout(os);
87 void parse_text_in_inset(Parser & p, ostream & os, unsigned flags, bool outer,
88 Context const & context, string const & name,
89 string const rdelim = string())
91 InsetLayout const * layout = 0;
92 DocumentClass::InsetLayouts::const_iterator it =
93 context.textclass.insetLayouts().find(from_ascii(name));
94 if (it != context.textclass.insetLayouts().end())
95 layout = &(it->second);
96 parse_text_in_inset(p, os, flags, outer, context, layout, rdelim);
99 /// parses a paragraph snippet, useful for example for \\emph{...}
100 void parse_text_snippet(Parser & p, ostream & os, unsigned flags, bool outer,
103 Context newcontext(context);
104 // Don't inherit the paragraph-level extra stuff
105 newcontext.par_extra_stuff.clear();
106 parse_text(p, os, flags, outer, newcontext);
107 // Make sure that we don't create invalid .lyx files
108 context.need_layout = newcontext.need_layout;
109 context.need_end_layout = newcontext.need_end_layout;
114 * Thin wrapper around parse_text_snippet() using a string.
116 * We completely ignore \c context.need_layout and \c context.need_end_layout,
117 * because our return value is not used directly (otherwise the stream version
118 * of parse_text_snippet() could be used). That means that the caller needs
119 * to do layout management manually.
120 * This is intended to parse text that does not create any layout changes.
122 string parse_text_snippet(Parser & p, unsigned flags, const bool outer,
125 Context newcontext(context);
126 newcontext.need_layout = false;
127 newcontext.need_end_layout = false;
128 newcontext.new_layout_allowed = false;
129 // Avoid warning by Context::~Context()
130 newcontext.par_extra_stuff.clear();
132 parse_text_snippet(p, os, flags, outer, newcontext);
136 string fboxrule = "";
138 string shadow_size = "";
140 char const * const known_ref_commands[] = { "ref", "pageref", "vref",
141 "vpageref", "prettyref", "nameref", "eqref", 0 };
143 char const * const known_coded_ref_commands[] = { "ref", "pageref", "vref",
144 "vpageref", "formatted", "nameref", "eqref", 0 };
146 char const * const known_refstyle_commands[] = { "algref", "chapref", "corref",
147 "eqref", "enuref", "figref", "fnref", "lemref", "parref", "partref", "propref",
148 "secref", "subsecref", "tabref", "thmref", 0 };
150 char const * const known_refstyle_prefixes[] = { "alg", "chap", "cor",
151 "eq", "enu", "fig", "fn", "lem", "par", "part", "prop",
152 "sec", "subsec", "tab", "thm", 0 };
156 * supported CJK encodings
157 * JIS does not work with LyX's encoding conversion
159 const char * const supported_CJK_encodings[] = {
160 "EUC-JP", "KS", "GB", "UTF8",
161 "Bg5", /*"JIS",*/ "SJIS", 0};
164 * the same as supported_CJK_encodings with their corresponding LyX language name
165 * FIXME: The mapping "UTF8" => "chinese-traditional" is only correct for files
167 * NOTE: "Bg5", "JIS" and "SJIS" are not supported by LyX, on re-export the
168 * encodings "UTF8", "EUC-JP" and "EUC-JP" will be used.
169 * please keep this in sync with supported_CJK_encodings line by line!
171 const char * const supported_CJK_languages[] = {
172 "japanese-cjk", "korean", "chinese-simplified", "chinese-traditional",
173 "chinese-traditional", /*"japanese-cjk",*/ "japanese-cjk", 0};
177 * The starred forms are also known except for "citefullauthor",
178 * "citeyear" and "citeyearpar".
180 char const * const known_natbib_commands[] = { "cite", "citet", "citep",
181 "citealt", "citealp", "citeauthor", "citeyear", "citeyearpar",
182 "citefullauthor", "Citet", "Citep", "Citealt", "Citealp", "Citeauthor", 0 };
186 * No starred form other than "cite*" known.
188 char const * const known_jurabib_commands[] = { "cite", "citet", "citep",
189 "citealt", "citealp", "citeauthor", "citeyear", "citeyearpar",
190 // jurabib commands not (yet) supported by LyX:
192 // "footcite", "footcitet", "footcitep", "footcitealt", "footcitealp",
193 // "footciteauthor", "footciteyear", "footciteyearpar",
194 "citefield", "citetitle", 0 };
198 * Known starred forms: \cite*, \citeauthor*, \Citeauthor*, \parencite*, \citetitle*.
200 char const * const known_biblatex_commands[] = { "cite", "Cite", "textcite", "Textcite",
201 "parencite", "Parencite", "citeauthor", "Citeauthor", "citeyear", "smartcite", "Smartcite",
202 "footcite", "Footcite", "autocite", "Autocite", "citetitle", "fullcite", "footfullcite",
203 "supercite", "cites", "Cites", "textcites", "Textcites", "parencites", "Parencites",
204 "smartcites", "Smartcites", "autocites", "Autocites", 0 };
206 // Whether we need to insert a bibtex inset in a comment
207 bool need_commentbib = false;
209 /// LaTeX names for quotes
210 char const * const known_quotes[] = { "dq", "guillemotleft", "flqq", "og",
211 "guillemotright", "frqq", "fg", "glq", "glqq", "textquoteleft", "grq", "grqq",
212 "quotedblbase", "textquotedblleft", "quotesinglbase", "textquoteright", "flq",
213 "guilsinglleft", "frq", "guilsinglright", "textquotedblright", "textquotesingle",
216 /// the same as known_quotes with .lyx names
217 char const * const known_coded_quotes[] = { "qrd", "ard", "ard", "ard",
218 "ald", "ald", "ald", "gls", "gld", "els", "els", "eld",
219 "gld", "eld", "gls", "ers", "ars",
220 "ars", "als", "als", "erd", "qrs", "qrd", 0};
222 /// LaTeX names for font sizes
223 char const * const known_sizes[] = { "tiny", "scriptsize", "footnotesize",
224 "small", "normalsize", "large", "Large", "LARGE", "huge", "Huge", 0};
226 /// the same as known_sizes with .lyx names
227 char const * const known_coded_sizes[] = { "tiny", "scriptsize", "footnotesize",
228 "small", "normal", "large", "larger", "largest", "huge", "giant", 0};
230 /// LaTeX 2.09 names for font families
231 char const * const known_old_font_families[] = { "rm", "sf", "tt", 0};
233 /// LaTeX names for font families
234 char const * const known_font_families[] = { "rmfamily", "sffamily",
237 /// LaTeX names for font family changing commands
238 char const * const known_text_font_families[] = { "textrm", "textsf",
241 /// The same as known_old_font_families, known_font_families and
242 /// known_text_font_families with .lyx names
243 char const * const known_coded_font_families[] = { "roman", "sans",
246 /// LaTeX 2.09 names for font series
247 char const * const known_old_font_series[] = { "bf", 0};
249 /// LaTeX names for font series
250 char const * const known_font_series[] = { "bfseries", "mdseries", 0};
252 /// LaTeX names for font series changing commands
253 char const * const known_text_font_series[] = { "textbf", "textmd", 0};
255 /// The same as known_old_font_series, known_font_series and
256 /// known_text_font_series with .lyx names
257 char const * const known_coded_font_series[] = { "bold", "medium", 0};
259 /// LaTeX 2.09 names for font shapes
260 char const * const known_old_font_shapes[] = { "it", "sl", "sc", 0};
262 /// LaTeX names for font shapes
263 char const * const known_font_shapes[] = { "itshape", "slshape", "scshape",
266 /// LaTeX names for font shape changing commands
267 char const * const known_text_font_shapes[] = { "textit", "textsl", "textsc",
270 /// The same as known_old_font_shapes, known_font_shapes and
271 /// known_text_font_shapes with .lyx names
272 char const * const known_coded_font_shapes[] = { "italic", "slanted",
273 "smallcaps", "up", 0};
275 /// Known special characters which need skip_spaces_braces() afterwards
276 char const * const known_special_chars[] = {"ldots",
277 "lyxarrow", "textcompwordmark",
278 "slash", "textasciitilde", "textasciicircum", "textbackslash",
279 "LyX", "TeX", "LaTeXe",
282 /// special characters from known_special_chars which may have a \\protect before
283 char const * const known_special_protect_chars[] = {"LyX", "TeX",
284 "LaTeXe", "LaTeX", 0};
286 /// the same as known_special_chars with .lyx names
287 char const * const known_coded_special_chars[] = {"\\SpecialChar ldots\n",
288 "\\SpecialChar menuseparator\n", "\\SpecialChar ligaturebreak\n",
289 "\\SpecialChar breakableslash\n", "~", "^", "\n\\backslash\n",
290 "\\SpecialChar LyX\n", "\\SpecialChar TeX\n", "\\SpecialChar LaTeX2e\n",
291 "\\SpecialChar LaTeX\n", 0};
294 * Graphics file extensions known by the dvips driver of the graphics package.
295 * These extensions are used to complete the filename of an included
296 * graphics file if it does not contain an extension.
297 * The order must be the same that latex uses to find a file, because we
298 * will use the first extension that matches.
299 * This is only an approximation for the common cases. If we would want to
300 * do it right in all cases, we would need to know which graphics driver is
301 * used and know the extensions of every driver of the graphics package.
303 char const * const known_dvips_graphics_formats[] = {"eps", "ps", "eps.gz",
304 "ps.gz", "eps.Z", "ps.Z", 0};
307 * Graphics file extensions known by the pdftex driver of the graphics package.
308 * \sa known_dvips_graphics_formats
310 char const * const known_pdftex_graphics_formats[] = {"png", "pdf", "jpg",
314 * Known file extensions for TeX files as used by \\include.
316 char const * const known_tex_extensions[] = {"tex", 0};
318 /// spaces known by InsetSpace
319 char const * const known_spaces[] = { " ", "space", ",",
320 "thinspace", "quad", "qquad", "enspace", "enskip",
321 "negthinspace", "negmedspace", "negthickspace", "textvisiblespace",
322 "hfill", "dotfill", "hrulefill", "leftarrowfill", "rightarrowfill",
323 "upbracefill", "downbracefill", 0};
325 /// the same as known_spaces with .lyx names
326 char const * const known_coded_spaces[] = { "space{}", "space{}",
327 "thinspace{}", "thinspace{}", "quad{}", "qquad{}", "enspace{}", "enskip{}",
328 "negthinspace{}", "negmedspace{}", "negthickspace{}", "textvisiblespace{}",
329 "hfill{}", "dotfill{}", "hrulefill{}", "leftarrowfill{}", "rightarrowfill{}",
330 "upbracefill{}", "downbracefill{}", 0};
332 /// known TIPA combining diacritical marks
333 char const * const known_tipa_marks[] = {"textsubwedge", "textsubumlaut",
334 "textsubtilde", "textseagull", "textsubbridge", "textinvsubbridge",
335 "textsubsquare", "textsubrhalfring", "textsublhalfring", "textsubplus",
336 "textovercross", "textsubarch", "textsuperimposetilde", "textraising",
337 "textlowering", "textadvancing", "textretracting", "textdoublegrave",
338 "texthighrise", "textlowrise", "textrisefall", "textsyllabic",
339 "textsubring", "textsubbar", 0};
341 /// TIPA tones that need special handling
342 char const * const known_tones[] = {"15", "51", "45", "12", "454", 0};
344 // string to store the float type to be able to determine the type of subfloats
345 string float_type = "";
347 // string to store the float status of minted listings
348 string minted_float = "";
350 // whether a caption has been parsed for a floating minted listing
351 bool minted_float_has_caption = false;
353 // The caption for non-floating minted listings
354 string minted_nonfloat_caption = "";
356 // Characters that have to be escaped by \\ in LaTeX
357 char const * const known_escaped_chars[] = {
358 "&", "_", "$", "%", "#", "^", "{", "}", 0};
361 /// splits "x=z, y=b" into a map and an ordered keyword vector
362 void split_map(string const & s, map<string, string> & res, vector<string> & keys)
367 keys.resize(v.size());
368 for (size_t i = 0; i < v.size(); ++i) {
369 size_t const pos = v[i].find('=');
370 string const index = trimSpaceAndEol(v[i].substr(0, pos));
371 string const value = trimSpaceAndEol(v[i].substr(pos + 1, string::npos));
379 * Split a LaTeX length into value and unit.
380 * The latter can be a real unit like "pt", or a latex length variable
381 * like "\textwidth". The unit may contain additional stuff like glue
382 * lengths, but we don't care, because such lengths are ERT anyway.
383 * \returns true if \p value and \p unit are valid.
385 bool splitLatexLength(string const & len, string & value, string & unit)
389 const string::size_type i = len.find_first_not_of(" -+0123456789.,");
390 //'4,5' is a valid LaTeX length number. Change it to '4.5'
391 string const length = subst(len, ',', '.');
392 if (i == string::npos)
395 if (len[0] == '\\') {
396 // We had something like \textwidth without a factor
402 value = trimSpaceAndEol(string(length, 0, i));
406 // 'cM' is a valid LaTeX length unit. Change it to 'cm'
407 if (contains(len, '\\'))
408 unit = trimSpaceAndEol(string(len, i));
410 unit = ascii_lowercase(trimSpaceAndEol(string(len, i)));
415 /// A simple function to translate a latex length to something LyX can
416 /// understand. Not perfect, but rather best-effort.
417 bool translate_len(string const & length, string & valstring, string & unit)
419 if (!splitLatexLength(length, valstring, unit))
421 // LyX uses percent values
423 istringstream iss(valstring);
428 string const percentval = oss.str();
430 if (unit.empty() || unit[0] != '\\')
432 string::size_type const i = unit.find(' ');
433 string const endlen = (i == string::npos) ? string() : string(unit, i);
434 if (unit == "\\textwidth") {
435 valstring = percentval;
436 unit = "text%" + endlen;
437 } else if (unit == "\\columnwidth") {
438 valstring = percentval;
439 unit = "col%" + endlen;
440 } else if (unit == "\\paperwidth") {
441 valstring = percentval;
442 unit = "page%" + endlen;
443 } else if (unit == "\\linewidth") {
444 valstring = percentval;
445 unit = "line%" + endlen;
446 } else if (unit == "\\paperheight") {
447 valstring = percentval;
448 unit = "pheight%" + endlen;
449 } else if (unit == "\\textheight") {
450 valstring = percentval;
451 unit = "theight%" + endlen;
452 } else if (unit == "\\baselineskip") {
453 valstring = percentval;
454 unit = "baselineskip%" + endlen;
460 /// If we have ambiguous quotation marks, make a smart guess
461 /// based on main quote style
462 string guessQuoteStyle(string in, bool const opening)
465 if (prefixIs(in, "qr")) {// straight quote
467 res = subst(res, "r", "l");
468 } else if (in == "eld") {// ``
469 if (preamble.quotesStyle() == "german")
471 else if (preamble.quotesStyle() == "british")
473 else if (preamble.quotesStyle() == "french")
475 else if (preamble.quotesStyle() == "russian")
477 } else if (in == "erd") {// ''
478 if (preamble.quotesStyle() == "polish")
480 else if (preamble.quotesStyle() == "british")
482 else if (preamble.quotesStyle() == "french")
484 else if (preamble.quotesStyle() == "swedish")
485 res = opening ? "sld" : "srd";
486 } else if (in == "els") {// `
487 if (preamble.quotesStyle() == "german")
489 else if (preamble.quotesStyle() == "british")
491 } else if (in == "ers") {// '
492 if (preamble.quotesStyle() == "polish")
494 else if (preamble.quotesStyle() == "british")
496 else if (preamble.quotesStyle() == "swedish")
497 res = opening ? "sls" : "srs";
498 } else if (in == "ard") {// >>
499 if (preamble.quotesStyle() == "swiss")
501 else if (preamble.quotesStyle() == "french")
503 else if (preamble.quotesStyle() == "russian")
505 } else if (in == "ald") {// <<
506 if (preamble.quotesStyle() == "swiss")
508 else if (preamble.quotesStyle() == "french")
510 else if (preamble.quotesStyle() == "russian")
512 } else if (in == "ars") {// >
513 if (preamble.quotesStyle() == "swiss")
515 } else if (in == "als") {// <
516 if (preamble.quotesStyle() == "swiss")
518 } else if (in == "gld") {// ,,
519 if (preamble.quotesStyle() == "polish")
521 else if (preamble.quotesStyle() == "russian")
523 } else if (in == "gls") {// ,
524 if (preamble.quotesStyle() == "polish")
531 string const fromPolyglossiaEnvironment(string const s)
533 // Since \arabic is taken by the LaTeX kernel,
534 // the Arabic polyglossia environment is upcased
542 string uncapitalize(string const s)
544 docstring in = from_ascii(s);
545 char_type t = lowercase(s[0]);
551 bool isCapitalized(string const s)
553 docstring in = from_ascii(s);
554 char_type t = uppercase(s[0]);
556 return to_ascii(in) == s;
563 string translate_len(string const & length)
567 if (translate_len(length, value, unit))
569 // If the input is invalid, return what we have.
577 * Translates a LaTeX length into \p value, \p unit and
578 * \p special parts suitable for a box inset.
579 * The difference from translate_len() is that a box inset knows about
580 * some special "units" that are stored in \p special.
582 void translate_box_len(string const & length, string & value, string & unit, string & special)
584 if (translate_len(length, value, unit)) {
585 if (unit == "\\height" || unit == "\\depth" ||
586 unit == "\\totalheight" || unit == "\\width") {
587 special = unit.substr(1);
588 // The unit is not used, but LyX requires a dummy setting
600 void begin_inset(ostream & os, string const & name)
602 os << "\n\\begin_inset " << name;
606 void begin_command_inset(ostream & os, string const & name,
607 string const & latexname)
609 begin_inset(os, "CommandInset ");
610 os << name << "\nLatexCommand " << latexname << '\n';
614 void end_inset(ostream & os)
616 os << "\n\\end_inset\n\n";
620 bool skip_braces(Parser & p)
622 if (p.next_token().cat() != catBegin)
625 if (p.next_token().cat() == catEnd) {
634 /// replace LaTeX commands in \p s from the unicodesymbols file with their
636 pair<bool, docstring> convert_unicodesymbols(docstring s)
640 for (size_t i = 0; i < s.size();) {
649 docstring parsed = normalize_c(encodings.fromLaTeXCommand(s,
650 Encodings::TEXT_CMD, termination, rem, &req));
651 set<string>::const_iterator it = req.begin();
652 set<string>::const_iterator en = req.end();
653 for (; it != en; ++it)
654 preamble.registerAutomaticallyLoadedPackage(*it);
657 if (s.empty() || s[0] != '\\')
661 for (auto const & c : known_escaped_chars)
662 if (c != 0 && prefixIs(s, from_ascii("\\") + c))
667 return make_pair(res, os.str());
671 /// try to convert \p s to a valid InsetCommand argument
672 /// return whether this succeeded. If not, these command insets
673 /// get the "literate" flag.
674 pair<bool, string> convert_latexed_command_inset_arg(string s)
676 bool success = false;
678 // since we don't know the input encoding we can't use from_utf8
679 pair<bool, docstring> res = convert_unicodesymbols(from_ascii(s));
681 s = to_utf8(res.second);
683 // LyX cannot handle newlines in a latex command
684 return make_pair(success, subst(s, "\n", " "));
687 /// try to convert \p s to a valid InsetCommand argument
688 /// without trying to recode macros.
689 string convert_literate_command_inset_arg(string s)
691 // LyX cannot handle newlines in a latex command
692 return subst(s, "\n", " ");
695 void output_ert(ostream & os, string const & s, Context & context)
697 context.check_layout(os);
698 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
700 os << "\n\\backslash\n";
701 else if (*it == '\n') {
702 context.new_paragraph(os);
703 context.check_layout(os);
707 context.check_end_layout(os);
711 void output_ert_inset(ostream & os, string const & s, Context & context)
713 // We must have a valid layout before outputting the ERT inset.
714 context.check_layout(os);
715 Context newcontext(true, context.textclass);
716 InsetLayout const & layout = context.textclass.insetLayout(from_ascii("ERT"));
717 if (layout.forcePlainLayout())
718 newcontext.layout = &context.textclass.plainLayout();
719 begin_inset(os, "ERT");
720 os << "\nstatus collapsed\n";
721 output_ert(os, s, newcontext);
726 void output_comment(Parser & p, ostream & os, string const & s,
729 if (p.next_token().cat() == catNewline)
730 output_ert_inset(os, '%' + s, context);
732 output_ert_inset(os, '%' + s + '\n', context);
736 Layout const * findLayout(TextClass const & textclass, string const & name, bool command,
737 string const & latexparam = string())
739 Layout const * layout = findLayoutWithoutModule(textclass, name, command, latexparam);
742 if (checkModule(name, command))
743 return findLayoutWithoutModule(textclass, name, command, latexparam);
748 InsetLayout const * findInsetLayout(TextClass const & textclass, string const & name, bool command,
749 string const & latexparam = string())
751 InsetLayout const * insetlayout =
752 findInsetLayoutWithoutModule(textclass, name, command, latexparam);
755 if (checkModule(name, command))
756 return findInsetLayoutWithoutModule(textclass, name, command, latexparam);
761 void eat_whitespace(Parser &, ostream &, Context &, bool);
765 * Skips whitespace and braces.
766 * This should be called after a command has been parsed that is not put into
767 * ERT, and where LyX adds "{}" if needed.
769 void skip_spaces_braces(Parser & p, bool keepws = false)
771 /* The following four examples produce the same typeset output and
772 should be handled by this function:
780 // Unfortunately we need to skip comments, too.
781 // We can't use eat_whitespace since writing them after the {}
782 // results in different output in some cases.
783 bool const skipped_spaces = p.skip_spaces(true);
784 bool const skipped_braces = skip_braces(p);
785 if (keepws && skipped_spaces && !skipped_braces)
786 // put back the space (it is better handled by check_space)
787 p.unskip_spaces(true);
791 void output_arguments(ostream & os, Parser & p, bool outer, bool need_layout, string const prefix,
792 Context & context, Layout::LaTeXArgMap const & latexargs)
794 if (context.layout->latextype != LATEX_ITEM_ENVIRONMENT || !prefix.empty()) {
796 context.check_layout(os);
802 Layout::LaTeXArgMap::const_iterator lait = latexargs.begin();
803 Layout::LaTeXArgMap::const_iterator const laend = latexargs.end();
804 for (; lait != laend; ++lait) {
806 eat_whitespace(p, os, context, false);
807 if (lait->second.mandatory) {
808 if (p.next_token().cat() != catBegin)
810 string ldelim = to_utf8(lait->second.ldelim);
811 string rdelim = to_utf8(lait->second.rdelim);
816 p.get_token(); // eat ldelim
817 if (ldelim.size() > 1)
818 p.get_token(); // eat ldelim
820 context.check_layout(os);
823 begin_inset(os, "Argument ");
826 os << i << "\nstatus collapsed\n\n";
827 parse_text_in_inset(p, os, FLAG_RDELIM, outer, context, 0, rdelim);
830 string ldelim = to_utf8(lait->second.ldelim);
831 string rdelim = to_utf8(lait->second.rdelim);
836 string tok = p.next_token().asInput();
837 // we only support delimiters with max 2 chars for now.
838 if (ldelim.size() > 1)
839 tok += p.next_next_token().asInput();
840 if (p.next_token().cat() == catEscape || tok != ldelim)
842 p.get_token(); // eat ldelim
843 if (ldelim.size() > 1)
844 p.get_token(); // eat ldelim
846 context.check_layout(os);
849 begin_inset(os, "Argument ");
852 os << i << "\nstatus collapsed\n\n";
853 parse_text_in_inset(p, os, FLAG_RDELIM, outer, context, 0, rdelim);
856 eat_whitespace(p, os, context, false);
861 void output_command_layout(ostream & os, Parser & p, bool outer,
862 Context & parent_context,
863 Layout const * newlayout)
865 TeXFont const oldFont = parent_context.font;
866 // save the current font size
867 string const size = oldFont.size;
868 // reset the font size to default, because the font size switches
869 // don't affect section headings and the like
870 parent_context.font.size = Context::normalfont.size;
871 // we only need to write the font change if we have an open layout
872 if (!parent_context.atParagraphStart())
873 output_font_change(os, oldFont, parent_context.font);
874 parent_context.check_end_layout(os);
875 Context context(true, parent_context.textclass, newlayout,
876 parent_context.layout, parent_context.font);
877 if (parent_context.deeper_paragraph) {
878 // We are beginning a nested environment after a
879 // deeper paragraph inside the outer list environment.
880 // Therefore we don't need to output a "begin deeper".
881 context.need_end_deeper = true;
883 context.check_deeper(os);
884 output_arguments(os, p, outer, true, string(), context,
885 context.layout->latexargs());
886 // If we have a latex param, we eat it here.
887 if (!parent_context.latexparam.empty()) {
889 Context dummy(true, parent_context.textclass);
890 parse_text(p, oss, FLAG_RDELIM, outer, dummy,
891 string(1, parent_context.latexparam.back()));
893 parse_text(p, os, FLAG_ITEM, outer, context);
894 output_arguments(os, p, outer, false, "post", context,
895 context.layout->postcommandargs());
896 context.check_end_layout(os);
897 if (parent_context.deeper_paragraph) {
898 // We must suppress the "end deeper" because we
899 // suppressed the "begin deeper" above.
900 context.need_end_deeper = false;
902 context.check_end_deeper(os);
903 // We don't need really a new paragraph, but
904 // we must make sure that the next item gets a \begin_layout.
905 parent_context.new_paragraph(os);
906 // Set the font size to the original value. No need to output it here
907 // (Context::begin_layout() will do that if needed)
908 parent_context.font.size = size;
913 * Output a space if necessary.
914 * This function gets called for every whitespace token.
916 * We have three cases here:
917 * 1. A space must be suppressed. Example: The lyxcode case below
918 * 2. A space may be suppressed. Example: Spaces before "\par"
919 * 3. A space must not be suppressed. Example: A space between two words
921 * We currently handle only 1. and 3 and from 2. only the case of
922 * spaces before newlines as a side effect.
924 * 2. could be used to suppress as many spaces as possible. This has two effects:
925 * - Reimporting LyX generated LaTeX files changes almost no whitespace
926 * - Superflous whitespace from non LyX generated LaTeX files is removed.
927 * The drawback is that the logic inside the function becomes
928 * complicated, and that is the reason why it is not implemented.
930 void check_space(Parser & p, ostream & os, Context & context)
932 Token const next = p.next_token();
933 Token const curr = p.curr_token();
934 // A space before a single newline and vice versa must be ignored
935 // LyX emits a newline before \end{lyxcode}.
936 // This newline must be ignored,
937 // otherwise LyX will add an additional protected space.
938 if (next.cat() == catSpace ||
939 next.cat() == catNewline ||
940 (next.cs() == "end" && context.layout->free_spacing && curr.cat() == catNewline)) {
943 context.check_layout(os);
949 * Parse all arguments of \p command
951 void parse_arguments(string const & command,
952 vector<ArgumentType> const & template_arguments,
953 Parser & p, ostream & os, bool outer, Context & context)
955 string ert = command;
956 size_t no_arguments = template_arguments.size();
957 for (size_t i = 0; i < no_arguments; ++i) {
958 switch (template_arguments[i]) {
961 // This argument contains regular LaTeX
962 output_ert_inset(os, ert + '{', context);
963 eat_whitespace(p, os, context, false);
964 if (template_arguments[i] == required)
965 parse_text(p, os, FLAG_ITEM, outer, context);
967 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
971 // This argument consists only of a single item.
972 // The presence of '{' or not must be preserved.
974 if (p.next_token().cat() == catBegin)
975 ert += '{' + p.verbatim_item() + '}';
977 ert += p.verbatim_item();
981 // This argument may contain special characters
982 ert += '{' + p.verbatim_item() + '}';
986 // true because we must not eat whitespace
987 // if an optional arg follows we must not strip the
988 // brackets from this one
989 if (i < no_arguments - 1 &&
990 template_arguments[i+1] == optional)
991 ert += p.getFullOpt(true);
993 ert += p.getOpt(true);
997 output_ert_inset(os, ert, context);
1002 * Check whether \p command is a known command. If yes,
1003 * handle the command with all arguments.
1004 * \return true if the command was parsed, false otherwise.
1006 bool parse_command(string const & command, Parser & p, ostream & os,
1007 bool outer, Context & context)
1009 if (known_commands.find(command) != known_commands.end()) {
1010 parse_arguments(command, known_commands[command], p, os,
1018 /// Parses a minipage or parbox
1019 void parse_box(Parser & p, ostream & os, unsigned outer_flags,
1020 unsigned inner_flags, bool outer, Context & parent_context,
1021 string const & outer_type, string const & special,
1022 string inner_type, string const & frame_color,
1023 string const & background_color)
1027 string hor_pos = "l";
1028 // We need to set the height to the LaTeX default of 1\\totalheight
1029 // for the case when no height argument is given
1030 string height_value = "1";
1031 string height_unit = "in";
1032 string height_special = "totalheight";
1033 string latex_height;
1037 string width_special = "none";
1038 string thickness = "0.4pt";
1039 if (!fboxrule.empty())
1040 thickness = fboxrule;
1042 thickness = "0.4pt";
1044 if (!fboxsep.empty())
1045 separation = fboxsep;
1049 if (!shadow_size.empty())
1050 shadowsize = shadow_size;
1053 string framecolor = "black";
1054 string backgroundcolor = "none";
1055 if (!frame_color.empty())
1056 framecolor = frame_color;
1057 if (!background_color.empty())
1058 backgroundcolor = background_color;
1059 // if there is a color box around the \begin statements have not yet been parsed
1061 if (!frame_color.empty() || !background_color.empty()) {
1062 eat_whitespace(p, os, parent_context, false);
1063 p.get_token().asInput(); // the '{'
1065 if (p.next_token().asInput() == "\\begin") {
1066 p.get_token().asInput();
1068 inner_type = "minipage";
1069 inner_flags = FLAG_END;
1070 active_environments.push_back("minipage");
1073 else if (p.next_token().asInput() == "\\parbox") {
1074 p.get_token().asInput();
1075 inner_type = "parbox";
1076 inner_flags = FLAG_ITEM;
1079 else if (p.next_token().asInput() == "\\makebox") {
1080 p.get_token().asInput();
1081 inner_type = "makebox";
1082 inner_flags = FLAG_ITEM;
1084 // in case there is just \colorbox{color}{text}
1087 inner_type = "makebox";
1088 inner_flags = FLAG_BRACE_LAST;
1093 if (!p.hasOpt() && (inner_type == "makebox" || outer_type == "mbox"))
1095 if (!inner_type.empty() && p.hasOpt()) {
1096 if (inner_type != "makebox")
1097 position = p.getArg('[', ']');
1099 latex_width = p.getArg('[', ']');
1100 translate_box_len(latex_width, width_value, width_unit, width_special);
1103 if (position != "t" && position != "c" && position != "b") {
1104 cerr << "invalid position " << position << " for "
1105 << inner_type << endl;
1109 if (inner_type != "makebox") {
1110 latex_height = p.getArg('[', ']');
1111 translate_box_len(latex_height, height_value, height_unit, height_special);
1113 string const opt = p.getArg('[', ']');
1116 if (hor_pos != "l" && hor_pos != "c" &&
1117 hor_pos != "r" && hor_pos != "s") {
1118 cerr << "invalid hor_pos " << hor_pos
1119 << " for " << inner_type << endl;
1126 inner_pos = p.getArg('[', ']');
1127 if (inner_pos != "c" && inner_pos != "t" &&
1128 inner_pos != "b" && inner_pos != "s") {
1129 cerr << "invalid inner_pos "
1130 << inner_pos << " for "
1131 << inner_type << endl;
1132 inner_pos = position;
1136 if (inner_type == "makebox")
1140 if (inner_type.empty()) {
1141 if (special.empty() && outer_type != "framebox")
1142 latex_width = "1\\columnwidth";
1145 latex_width = p2.getArg('[', ']');
1146 string const opt = p2.getArg('[', ']');
1149 if (hor_pos != "l" && hor_pos != "c" &&
1150 hor_pos != "r" && hor_pos != "s") {
1151 cerr << "invalid hor_pos " << hor_pos
1152 << " for " << outer_type << endl;
1156 if (outer_type == "framebox")
1160 } else if (inner_type != "makebox")
1161 latex_width = p.verbatim_item();
1162 // if e.g. only \ovalbox{content} was used, set the width to 1\columnwidth
1163 // as this is LyX's standard for such cases (except for makebox)
1164 // \framebox is more special and handled below
1165 if (latex_width.empty() && inner_type != "makebox"
1166 && outer_type != "framebox")
1167 latex_width = "1\\columnwidth";
1169 translate_len(latex_width, width_value, width_unit);
1171 bool shadedparbox = false;
1172 if (inner_type == "shaded") {
1173 eat_whitespace(p, os, parent_context, false);
1174 if (outer_type == "parbox") {
1176 if (p.next_token().cat() == catBegin)
1178 eat_whitespace(p, os, parent_context, false);
1179 shadedparbox = true;
1184 // If we already read the inner box we have to push the inner env
1185 if (!outer_type.empty() && !inner_type.empty() &&
1186 (inner_flags & FLAG_END))
1187 active_environments.push_back(inner_type);
1188 bool use_ert = false;
1189 if (!outer_type.empty() && !inner_type.empty()) {
1190 // Look whether there is some content after the end of the
1191 // inner box, but before the end of the outer box.
1192 // If yes, we need to output ERT.
1194 if (inner_flags & FLAG_END)
1195 p.ertEnvironment(inner_type);
1198 p.skip_spaces(true);
1199 bool const outer_env(outer_type == "framed" || outer_type == "minipage");
1200 if ((outer_env && p.next_token().asInput() != "\\end") ||
1201 (!outer_env && p.next_token().cat() != catEnd)) {
1202 // something is between the end of the inner box and
1203 // the end of the outer box, so we need to use ERT.
1211 if (!outer_type.empty()) {
1212 if (outer_flags & FLAG_END)
1213 ss << "\\begin{" << outer_type << '}';
1215 ss << '\\' << outer_type << '{';
1216 if (!special.empty())
1220 if (!inner_type.empty()) {
1221 if (inner_type != "shaded") {
1222 if (inner_flags & FLAG_END)
1223 ss << "\\begin{" << inner_type << '}';
1225 ss << '\\' << inner_type;
1227 if (!position.empty())
1228 ss << '[' << position << ']';
1229 if (!latex_height.empty())
1230 ss << '[' << latex_height << ']';
1231 if (!inner_pos.empty())
1232 ss << '[' << inner_pos << ']';
1233 ss << '{' << latex_width << '}';
1234 if (!(inner_flags & FLAG_END))
1237 if (inner_type == "shaded")
1238 ss << "\\begin{shaded}";
1239 output_ert_inset(os, ss.str(), parent_context);
1240 if (!inner_type.empty()) {
1241 parse_text(p, os, inner_flags, outer, parent_context);
1242 if (inner_flags & FLAG_END)
1243 output_ert_inset(os, "\\end{" + inner_type + '}',
1246 output_ert_inset(os, "}", parent_context);
1248 if (!outer_type.empty()) {
1249 // If we already read the inner box we have to pop
1251 if (!inner_type.empty() && (inner_flags & FLAG_END))
1252 active_environments.pop_back();
1254 // Ensure that the end of the outer box is parsed correctly:
1255 // The opening brace has been eaten by parse_outer_box()
1256 if (!outer_type.empty() && (outer_flags & FLAG_ITEM)) {
1257 outer_flags &= ~FLAG_ITEM;
1258 outer_flags |= FLAG_BRACE_LAST;
1260 parse_text(p, os, outer_flags, outer, parent_context);
1261 if (outer_flags & FLAG_END)
1262 output_ert_inset(os, "\\end{" + outer_type + '}',
1265 output_ert_inset(os, "}", parent_context);
1268 // LyX does not like empty positions, so we have
1269 // to set them to the LaTeX default values here.
1270 if (position.empty())
1272 if (inner_pos.empty())
1273 inner_pos = position;
1274 parent_context.check_layout(os);
1275 begin_inset(os, "Box ");
1276 if (outer_type == "framed")
1278 else if (outer_type == "framebox" || outer_type == "fbox" || !frame_color.empty())
1280 else if (outer_type == "shadowbox")
1281 os << "Shadowbox\n";
1282 else if ((outer_type == "shaded" && inner_type.empty()) ||
1283 (outer_type == "minipage" && inner_type == "shaded") ||
1284 (outer_type == "parbox" && inner_type == "shaded")) {
1286 preamble.registerAutomaticallyLoadedPackage("color");
1287 } else if (outer_type == "doublebox")
1288 os << "Doublebox\n";
1289 else if (outer_type.empty() || outer_type == "mbox")
1290 os << "Frameless\n";
1292 os << outer_type << '\n';
1293 os << "position \"" << position << "\"\n";
1294 os << "hor_pos \"" << hor_pos << "\"\n";
1295 if (outer_type == "mbox")
1296 os << "has_inner_box 1\n";
1297 else if (!frame_color.empty() && inner_type == "makebox")
1298 os << "has_inner_box 0\n";
1300 os << "has_inner_box " << !inner_type.empty() << "\n";
1301 os << "inner_pos \"" << inner_pos << "\"\n";
1302 os << "use_parbox " << (inner_type == "parbox" || shadedparbox)
1304 if (outer_type == "mbox")
1305 os << "use_makebox 1\n";
1306 else if (!frame_color.empty())
1307 os << "use_makebox 0\n";
1309 os << "use_makebox " << (inner_type == "makebox") << '\n';
1310 if (outer_type == "mbox" || (outer_type == "fbox" && inner_type.empty()))
1311 os << "width \"\"\n";
1312 // for values like "1.5\width" LyX uses "1.5in" as width ad sets "width" as sepecial
1313 else if (contains(width_unit, '\\'))
1314 os << "width \"" << width_value << "in" << "\"\n";
1316 os << "width \"" << width_value << width_unit << "\"\n";
1317 if (contains(width_unit, '\\')) {
1318 width_unit.erase (0,1); // remove the leading '\'
1319 os << "special \"" << width_unit << "\"\n";
1321 os << "special \"" << width_special << "\"\n";
1322 if (contains(height_unit, '\\'))
1323 os << "height \"" << height_value << "in" << "\"\n";
1325 os << "height \"" << height_value << height_unit << "\"\n";
1326 os << "height_special \"" << height_special << "\"\n";
1327 os << "thickness \"" << thickness << "\"\n";
1328 os << "separation \"" << separation << "\"\n";
1329 os << "shadowsize \"" << shadowsize << "\"\n";
1330 os << "framecolor \"" << framecolor << "\"\n";
1331 os << "backgroundcolor \"" << backgroundcolor << "\"\n";
1332 os << "status open\n\n";
1334 // Unfortunately we can't use parse_text_in_inset:
1335 // InsetBox::forcePlainLayout() is hard coded and does not
1336 // use the inset layout. Apart from that do we call parse_text
1337 // up to two times, but need only one check_end_layout.
1338 bool const forcePlainLayout =
1339 (!inner_type.empty() || inner_type == "makebox") &&
1340 outer_type != "shaded" && outer_type != "framed";
1341 Context context(true, parent_context.textclass);
1342 if (forcePlainLayout)
1343 context.layout = &context.textclass.plainLayout();
1345 context.font = parent_context.font;
1347 // If we have no inner box the contents will be read with the outer box
1348 if (!inner_type.empty())
1349 parse_text(p, os, inner_flags, outer, context);
1351 // Ensure that the end of the outer box is parsed correctly:
1352 // The opening brace has been eaten by parse_outer_box()
1353 if (!outer_type.empty() && (outer_flags & FLAG_ITEM)) {
1354 outer_flags &= ~FLAG_ITEM;
1355 outer_flags |= FLAG_BRACE_LAST;
1358 // Find end of outer box, output contents if inner_type is
1359 // empty and output possible comments
1360 if (!outer_type.empty()) {
1361 // If we already read the inner box we have to pop
1363 if (!inner_type.empty() && (inner_flags & FLAG_END))
1364 active_environments.pop_back();
1365 // This does not output anything but comments if
1366 // inner_type is not empty (see use_ert)
1367 parse_text(p, os, outer_flags, outer, context);
1370 context.check_end_layout(os);
1372 #ifdef PRESERVE_LAYOUT
1373 // LyX puts a % after the end of the minipage
1374 if (p.next_token().cat() == catNewline && p.next_token().cs().size() > 1) {
1376 //output_comment(p, os, "dummy", parent_context);
1379 parent_context.new_paragraph(os);
1381 else if (p.next_token().cat() == catSpace || p.next_token().cat() == catNewline) {
1382 //output_comment(p, os, "dummy", parent_context);
1385 // We add a protected space if something real follows
1386 if (p.good() && p.next_token().cat() != catComment) {
1387 begin_inset(os, "space ~\n");
1393 if (inner_type == "minipage" && (!frame_color.empty() || !background_color.empty()))
1394 active_environments.pop_back();
1395 if (inner_flags != FLAG_BRACE_LAST && (!frame_color.empty() || !background_color.empty())) {
1396 // in this case we have to eat the the closing brace of the color box
1397 p.get_token().asInput(); // the '}'
1399 if (p.next_token().asInput() == "}") {
1400 // in this case we assume that the closing brace is from the box settings
1401 // therefore reset these values for the next box
1407 // all boxes except of Frameless and Shaded require calc
1408 if (!(outer_type.empty() || outer_type == "mbox") &&
1409 !((outer_type == "shaded" && inner_type.empty()) ||
1410 (outer_type == "minipage" && inner_type == "shaded") ||
1411 (outer_type == "parbox" && inner_type == "shaded")))
1412 preamble.registerAutomaticallyLoadedPackage("calc");
1416 void parse_outer_box(Parser & p, ostream & os, unsigned flags, bool outer,
1417 Context & parent_context, string const & outer_type,
1418 string const & special)
1420 eat_whitespace(p, os, parent_context, false);
1421 if (flags & FLAG_ITEM) {
1423 if (p.next_token().cat() == catBegin)
1426 cerr << "Warning: Ignoring missing '{' after \\"
1427 << outer_type << '.' << endl;
1428 eat_whitespace(p, os, parent_context, false);
1431 unsigned int inner_flags = 0;
1433 if (outer_type == "minipage" || outer_type == "parbox") {
1434 p.skip_spaces(true);
1435 while (p.hasOpt()) {
1437 p.skip_spaces(true);
1440 p.skip_spaces(true);
1441 if (outer_type == "parbox") {
1443 if (p.next_token().cat() == catBegin)
1445 p.skip_spaces(true);
1448 if (outer_type == "shaded" || outer_type == "mbox") {
1449 // These boxes never have an inner box
1451 } else if (p.next_token().asInput() == "\\parbox") {
1452 inner = p.get_token().cs();
1453 inner_flags = FLAG_ITEM;
1454 } else if (p.next_token().asInput() == "\\begin") {
1455 // Is this a minipage or shaded box?
1458 inner = p.getArg('{', '}');
1460 if (inner == "minipage" || inner == "shaded")
1461 inner_flags = FLAG_END;
1466 if (inner_flags == FLAG_END) {
1467 if (inner != "shaded")
1471 eat_whitespace(p, os, parent_context, false);
1473 parse_box(p, os, flags, FLAG_END, outer, parent_context,
1474 outer_type, special, inner, "", "");
1476 if (inner_flags == FLAG_ITEM) {
1478 eat_whitespace(p, os, parent_context, false);
1480 parse_box(p, os, flags, inner_flags, outer, parent_context,
1481 outer_type, special, inner, "", "");
1486 void parse_listings(Parser & p, ostream & os, Context & parent_context,
1487 bool in_line, bool use_minted)
1489 parent_context.check_layout(os);
1490 begin_inset(os, "listings\n");
1491 string arg = p.hasOpt() ? subst(p.verbatimOption(), "\n", "") : string();
1493 while ((i = arg.find(", ")) != string::npos
1494 || (i = arg.find(",\t")) != string::npos)
1495 arg.erase(i + 1, 1);
1498 string const language = p.getArg('{', '}');
1499 p.skip_spaces(true);
1500 arg += string(arg.empty() ? "" : ",") + "language=" + language;
1501 if (!minted_float.empty()) {
1502 arg += string(arg.empty() ? "" : ",") + minted_float;
1503 minted_nonfloat_caption.clear();
1507 os << "lstparams " << '"' << arg << '"' << '\n';
1508 if (arg.find("\\color") != string::npos)
1509 preamble.registerAutomaticallyLoadedPackage("color");
1512 os << "inline true\n";
1514 os << "inline false\n";
1515 os << "status open\n";
1516 Context context(true, parent_context.textclass);
1517 context.layout = &parent_context.textclass.plainLayout();
1518 if (use_minted && prefixIs(minted_nonfloat_caption, "[t]")) {
1519 minted_nonfloat_caption.erase(0,3);
1520 os << "\n\\begin_layout Plain Layout\n";
1521 begin_inset(os, "Caption Standard\n");
1522 Context newcontext(true, context.textclass,
1523 context.layout, 0, context.font);
1524 newcontext.check_layout(os);
1525 os << minted_nonfloat_caption << "\n";
1526 newcontext.check_end_layout(os);
1528 os << "\n\\end_layout\n";
1529 minted_nonfloat_caption.clear();
1533 // set catcodes to verbatim early, just in case.
1534 p.setCatcodes(VERBATIM_CATCODES);
1535 string delim = p.get_token().asInput();
1536 //FIXME: handler error condition
1537 s = p.verbatimStuff(delim).second;
1538 // context.new_paragraph(os);
1539 } else if (use_minted) {
1540 s = p.verbatimEnvironment("minted");
1542 s = p.verbatimEnvironment("lstlisting");
1544 output_ert(os, s, context);
1545 if (use_minted && prefixIs(minted_nonfloat_caption, "[b]")) {
1546 minted_nonfloat_caption.erase(0,3);
1547 os << "\n\\begin_layout Plain Layout\n";
1548 begin_inset(os, "Caption Standard\n");
1549 Context newcontext(true, context.textclass,
1550 context.layout, 0, context.font);
1551 newcontext.check_layout(os);
1552 os << minted_nonfloat_caption << "\n";
1553 newcontext.check_end_layout(os);
1555 os << "\n\\end_layout\n";
1556 minted_nonfloat_caption.clear();
1558 // Don't close the inset here for floating minted listings.
1559 // It will be closed at the end of the listing environment.
1560 if (!use_minted || minted_float.empty())
1563 eat_whitespace(p, os, parent_context, true);
1564 Token t = p.get_token();
1565 if (t.asInput() != "\\end") {
1566 // If anything follows, collect it into a caption.
1567 minted_float_has_caption = true;
1568 os << "\n\\begin_layout Plain Layout\n"; // outer layout
1569 begin_inset(os, "Caption Standard\n");
1570 os << "\n\\begin_layout Plain Layout\n"; // inner layout
1577 /// parse an unknown environment
1578 void parse_unknown_environment(Parser & p, string const & name, ostream & os,
1579 unsigned flags, bool outer,
1580 Context & parent_context)
1582 if (name == "tabbing")
1583 // We need to remember that we have to handle '\=' specially
1584 flags |= FLAG_TABBING;
1586 // We need to translate font changes and paragraphs inside the
1587 // environment to ERT if we have a non standard font.
1588 // Otherwise things like
1589 // \large\begin{foo}\huge bar\end{foo}
1591 bool const specialfont =
1592 (parent_context.font != parent_context.normalfont);
1593 bool const new_layout_allowed = parent_context.new_layout_allowed;
1595 parent_context.new_layout_allowed = false;
1596 output_ert_inset(os, "\\begin{" + name + "}", parent_context);
1597 // Try to handle options: Look if we have an optional arguments,
1598 // and if so, put the brackets in ERT.
1599 while (p.hasOpt()) {
1600 p.get_token(); // eat '['
1601 output_ert_inset(os, "[", parent_context);
1602 os << parse_text_snippet(p, FLAG_BRACK_LAST, outer, parent_context);
1603 output_ert_inset(os, "]", parent_context);
1605 parse_text_snippet(p, os, flags, outer, parent_context);
1606 output_ert_inset(os, "\\end{" + name + "}", parent_context);
1608 parent_context.new_layout_allowed = new_layout_allowed;
1612 void parse_environment(Parser & p, ostream & os, bool outer,
1613 string & last_env, Context & parent_context)
1615 Layout const * newlayout;
1616 InsetLayout const * newinsetlayout = 0;
1617 string const name = p.getArg('{', '}');
1618 const bool is_starred = suffixIs(name, '*');
1619 string const unstarred_name = rtrim(name, "*");
1620 active_environments.push_back(name);
1622 if (is_math_env(name)) {
1623 parent_context.check_layout(os);
1624 begin_inset(os, "Formula ");
1625 os << "\\begin{" << name << "}";
1626 parse_math(p, os, FLAG_END, MATH_MODE);
1627 os << "\\end{" << name << "}";
1629 if (is_display_math_env(name)) {
1630 // Prevent the conversion of a line break to a space
1631 // (bug 7668). This does not change the output, but
1632 // looks ugly in LyX.
1633 eat_whitespace(p, os, parent_context, false);
1637 // We need to use fromPolyglossiaEnvironment die to Arabic > arabic
1638 else if (is_known(fromPolyglossiaEnvironment(name), preamble.polyglossia_languages)) {
1639 // We must begin a new paragraph if not already done
1640 if (! parent_context.atParagraphStart()) {
1641 parent_context.check_end_layout(os);
1642 parent_context.new_paragraph(os);
1644 // save the language in the context so that it is
1645 // handled by parse_text
1646 parent_context.font.language =
1647 preamble.polyglossia2lyx(fromPolyglossiaEnvironment(name));
1648 parse_text(p, os, FLAG_END, outer, parent_context);
1649 // Just in case the environment is empty
1650 parent_context.extra_stuff.erase();
1651 // We must begin a new paragraph to reset the language
1652 parent_context.new_paragraph(os);
1656 else if (unstarred_name == "tabular" || name == "longtable") {
1657 eat_whitespace(p, os, parent_context, false);
1658 string width = "0pt";
1660 if (name == "longtable" && p.hasOpt()) {
1661 string const opt = p.getArg('[', ']');
1664 else if (opt == "l")
1666 else if (opt == "r")
1669 if (name == "tabular*") {
1670 width = lyx::translate_len(p.getArg('{', '}'));
1671 eat_whitespace(p, os, parent_context, false);
1673 parent_context.check_layout(os);
1674 begin_inset(os, "Tabular ");
1675 handle_tabular(p, os, name, width, halign, parent_context);
1680 else if (parent_context.textclass.floats().typeExist(unstarred_name)) {
1681 eat_whitespace(p, os, parent_context, false);
1682 string const opt = p.hasOpt() ? p.getArg('[', ']') : string();
1683 eat_whitespace(p, os, parent_context, false);
1684 parent_context.check_layout(os);
1685 begin_inset(os, "Float " + unstarred_name + "\n");
1686 // store the float type for subfloats
1687 // subfloats only work with figures and tables
1688 if (unstarred_name == "figure")
1689 float_type = unstarred_name;
1690 else if (unstarred_name == "table")
1691 float_type = unstarred_name;
1695 os << "placement " << opt << '\n';
1696 if (contains(opt, "H"))
1697 preamble.registerAutomaticallyLoadedPackage("float");
1699 Floating const & fl = parent_context.textclass.floats()
1700 .getType(unstarred_name);
1701 if (!fl.floattype().empty() && fl.usesFloatPkg())
1702 preamble.registerAutomaticallyLoadedPackage("float");
1705 os << "wide " << convert<string>(is_starred)
1706 << "\nsideways false"
1707 << "\nstatus open\n\n";
1708 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
1710 // We don't need really a new paragraph, but
1711 // we must make sure that the next item gets a \begin_layout.
1712 parent_context.new_paragraph(os);
1714 // the float is parsed thus delete the type
1718 else if (unstarred_name == "sidewaysfigure"
1719 || unstarred_name == "sidewaystable"
1720 || unstarred_name == "sidewaysalgorithm") {
1721 string const opt = p.hasOpt() ? p.getArg('[', ']') : string();
1722 eat_whitespace(p, os, parent_context, false);
1723 parent_context.check_layout(os);
1724 if (unstarred_name == "sidewaysfigure")
1725 begin_inset(os, "Float figure\n");
1726 else if (unstarred_name == "sidewaystable")
1727 begin_inset(os, "Float table\n");
1728 else if (unstarred_name == "sidewaysalgorithm")
1729 begin_inset(os, "Float algorithm\n");
1731 os << "placement " << opt << '\n';
1732 if (contains(opt, "H"))
1733 preamble.registerAutomaticallyLoadedPackage("float");
1734 os << "wide " << convert<string>(is_starred)
1735 << "\nsideways true"
1736 << "\nstatus open\n\n";
1737 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
1739 // We don't need really a new paragraph, but
1740 // we must make sure that the next item gets a \begin_layout.
1741 parent_context.new_paragraph(os);
1743 preamble.registerAutomaticallyLoadedPackage("rotfloat");
1746 else if (name == "wrapfigure" || name == "wraptable") {
1747 // syntax is \begin{wrapfigure}[lines]{placement}[overhang]{width}
1748 eat_whitespace(p, os, parent_context, false);
1749 parent_context.check_layout(os);
1752 string overhang = "0col%";
1755 lines = p.getArg('[', ']');
1756 string const placement = p.getArg('{', '}');
1758 overhang = p.getArg('[', ']');
1759 string const width = p.getArg('{', '}');
1761 if (name == "wrapfigure")
1762 begin_inset(os, "Wrap figure\n");
1764 begin_inset(os, "Wrap table\n");
1765 os << "lines " << lines
1766 << "\nplacement " << placement
1767 << "\noverhang " << lyx::translate_len(overhang)
1768 << "\nwidth " << lyx::translate_len(width)
1769 << "\nstatus open\n\n";
1770 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
1772 // We don't need really a new paragraph, but
1773 // we must make sure that the next item gets a \begin_layout.
1774 parent_context.new_paragraph(os);
1776 preamble.registerAutomaticallyLoadedPackage("wrapfig");
1779 else if (name == "minipage") {
1780 eat_whitespace(p, os, parent_context, false);
1781 // Test whether this is an outer box of a shaded box
1783 // swallow arguments
1784 while (p.hasOpt()) {
1786 p.skip_spaces(true);
1789 p.skip_spaces(true);
1790 Token t = p.get_token();
1791 bool shaded = false;
1792 if (t.asInput() == "\\begin") {
1793 p.skip_spaces(true);
1794 if (p.getArg('{', '}') == "shaded")
1799 parse_outer_box(p, os, FLAG_END, outer,
1800 parent_context, name, "shaded");
1802 parse_box(p, os, 0, FLAG_END, outer, parent_context,
1803 "", "", name, "", "");
1807 else if (name == "comment") {
1808 eat_whitespace(p, os, parent_context, false);
1809 parent_context.check_layout(os);
1810 begin_inset(os, "Note Comment\n");
1811 os << "status open\n";
1812 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
1815 skip_braces(p); // eat {} that might by set by LyX behind comments
1816 preamble.registerAutomaticallyLoadedPackage("verbatim");
1819 else if (unstarred_name == "verbatim") {
1820 // FIXME: this should go in the generic code that
1821 // handles environments defined in layout file that
1822 // have "PassThru 1". However, the code over there is
1823 // already too complicated for my taste.
1824 string const ascii_name =
1825 (name == "verbatim*") ? "Verbatim*" : "Verbatim";
1826 parent_context.new_paragraph(os);
1827 Context context(true, parent_context.textclass,
1828 &parent_context.textclass[from_ascii(ascii_name)]);
1829 string s = p.verbatimEnvironment(name);
1830 output_ert(os, s, context);
1834 else if (name == "IPA") {
1835 eat_whitespace(p, os, parent_context, false);
1836 parent_context.check_layout(os);
1837 begin_inset(os, "IPA\n");
1838 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
1841 preamble.registerAutomaticallyLoadedPackage("tipa");
1842 preamble.registerAutomaticallyLoadedPackage("tipx");
1845 else if (name == parent_context.textclass.titlename()
1846 && parent_context.textclass.titletype() == TITLE_ENVIRONMENT) {
1847 parse_text(p, os, FLAG_END, outer, parent_context);
1848 // Just in case the environment is empty
1849 parent_context.extra_stuff.erase();
1850 // We must begin a new paragraph
1851 parent_context.new_paragraph(os);
1855 else if (name == "CJK") {
1856 // the scheme is \begin{CJK}{encoding}{mapping}text\end{CJK}
1857 // It is impossible to decide if a CJK environment was in its own paragraph or within
1858 // a line. We therefore always assume a paragraph since the latter is a rare case.
1859 eat_whitespace(p, os, parent_context, false);
1860 parent_context.check_end_layout(os);
1861 // store the encoding to be able to reset it
1862 string const encoding_old = p.getEncoding();
1863 string const encoding = p.getArg('{', '}');
1864 // FIXME: For some reason JIS does not work. Although the text
1865 // in tests/CJK.tex is identical with the SJIS version if you
1866 // convert both snippets using the recode command line utility,
1867 // the resulting .lyx file contains some extra characters if
1868 // you set buggy_encoding to false for JIS.
1869 bool const buggy_encoding = encoding == "JIS";
1870 if (!buggy_encoding)
1871 p.setEncoding(encoding, Encoding::CJK);
1873 // FIXME: This will read garbage, since the data is not encoded in utf8.
1874 p.setEncoding("UTF-8");
1876 // LyX only supports the same mapping for all CJK
1877 // environments, so we might need to output everything as ERT
1878 string const mapping = trim(p.getArg('{', '}'));
1879 char const * const * const where =
1880 is_known(encoding, supported_CJK_encodings);
1881 if (!buggy_encoding && !preamble.fontCJKSet())
1882 preamble.fontCJK(mapping);
1883 bool knownMapping = mapping == preamble.fontCJK();
1884 if (buggy_encoding || !knownMapping || !where) {
1885 parent_context.check_layout(os);
1886 output_ert_inset(os, "\\begin{" + name + "}{" + encoding + "}{" + mapping + "}",
1888 // we must parse the content as verbatim because e.g. JIS can contain
1889 // normally invalid characters
1890 // FIXME: This works only for the most simple cases.
1891 // Since TeX control characters are not parsed,
1892 // things like comments are completely wrong.
1893 string const s = p.plainEnvironment("CJK");
1894 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
1897 if (snip == "\\" || is_known(snip, known_escaped_chars))
1898 output_ert_inset(os, snip, parent_context);
1899 else if (*it == '\n' && it + 1 != et && s.begin() + 1 != it)
1904 output_ert_inset(os, "\\end{" + name + "}",
1908 supported_CJK_languages[where - supported_CJK_encodings];
1909 // store the language because we must reset it at the end
1910 string const lang_old = parent_context.font.language;
1911 parent_context.font.language = lang;
1912 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
1913 parent_context.font.language = lang_old;
1914 parent_context.new_paragraph(os);
1916 p.setEncoding(encoding_old);
1920 else if (name == "lyxgreyedout") {
1921 eat_whitespace(p, os, parent_context, false);
1922 parent_context.check_layout(os);
1923 begin_inset(os, "Note Greyedout\n");
1924 os << "status open\n";
1925 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
1928 if (!preamble.notefontcolor().empty())
1929 preamble.registerAutomaticallyLoadedPackage("color");
1932 else if (name == "btSect") {
1933 eat_whitespace(p, os, parent_context, false);
1934 parent_context.check_layout(os);
1935 begin_command_inset(os, "bibtex", "bibtex");
1936 string bibstyle = "plain";
1938 bibstyle = p.getArg('[', ']');
1939 p.skip_spaces(true);
1941 string const bibfile = p.getArg('{', '}');
1942 eat_whitespace(p, os, parent_context, false);
1943 Token t = p.get_token();
1944 if (t.asInput() == "\\btPrintCited") {
1945 p.skip_spaces(true);
1946 os << "btprint " << '"' << "btPrintCited" << '"' << "\n";
1948 if (t.asInput() == "\\btPrintNotCited") {
1949 p.skip_spaces(true);
1950 os << "btprint " << '"' << "btPrintNotCited" << '"' << "\n";
1952 if (t.asInput() == "\\btPrintAll") {
1953 p.skip_spaces(true);
1954 os << "btprint " << '"' << "btPrintAll" << '"' << "\n";
1956 os << "bibfiles " << '"' << bibfile << "\"\n"
1957 << "options " << '"' << bibstyle << "\"\n";
1958 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
1963 else if (name == "btUnit") {
1964 string const nt = p.next_next_token().cs();
1965 // Do not attempt to overwrite a former diverging multibib.
1966 // Those are output as ERT instead.
1967 if ((nt == "part" || nt == "chapter"
1968 || nt == "section" || nt == "subsection")
1969 && (preamble.multibib().empty() || preamble.multibib() == nt)) {
1970 parse_text(p, os, FLAG_END, outer, parent_context);
1971 preamble.multibib(nt);
1973 parse_unknown_environment(p, name, os, FLAG_END, outer,
1977 else if (name == "framed" || name == "shaded") {
1978 eat_whitespace(p, os, parent_context, false);
1979 parse_outer_box(p, os, FLAG_END, outer, parent_context, name, "");
1981 preamble.registerAutomaticallyLoadedPackage("framed");
1984 else if (name == "listing") {
1985 minted_float = "float";
1986 eat_whitespace(p, os, parent_context, false);
1987 string const opt = p.hasOpt() ? p.getArg('[', ']') : string();
1989 minted_float += "=" + opt;
1990 // If something precedes \begin{minted}, we output it at the end
1991 // as a caption, in order to keep it inside the listings inset.
1992 eat_whitespace(p, os, parent_context, true);
1994 Token const & t = p.get_token();
1995 p.skip_spaces(true);
1996 string const envname = p.next_token().cat() == catBegin
1997 ? p.getArg('{', '}') : string();
1998 bool prologue = t.asInput() != "\\begin" || envname != "minted";
2000 minted_float_has_caption = false;
2001 string content = parse_text_snippet(p, FLAG_END, outer,
2003 size_t i = content.find("\\begin_inset listings");
2004 bool minted_env = i != string::npos;
2007 caption = content.substr(0, i);
2008 content.erase(0, i);
2010 parent_context.check_layout(os);
2011 if (minted_env && minted_float_has_caption) {
2012 eat_whitespace(p, os, parent_context, true);
2013 os << content << "\n";
2014 if (!caption.empty())
2015 os << caption << "\n";
2016 os << "\n\\end_layout\n"; // close inner layout
2017 end_inset(os); // close caption inset
2018 os << "\n\\end_layout\n"; // close outer layout
2019 } else if (!caption.empty()) {
2021 begin_inset(os, "listings\n");
2022 os << "lstparams " << '"' << minted_float << '"' << '\n';
2023 os << "inline false\n";
2024 os << "status collapsed\n";
2026 os << "\n\\begin_layout Plain Layout\n";
2027 begin_inset(os, "Caption Standard\n");
2028 Context newcontext(true, parent_context.textclass,
2029 0, 0, parent_context.font);
2030 newcontext.check_layout(os);
2031 os << caption << "\n";
2032 newcontext.check_end_layout(os);
2034 os << "\n\\end_layout\n";
2035 } else if (content.empty()) {
2036 begin_inset(os, "listings\n");
2037 os << "lstparams " << '"' << minted_float << '"' << '\n';
2038 os << "inline false\n";
2039 os << "status collapsed\n";
2041 os << content << "\n";
2043 end_inset(os); // close listings inset
2044 parent_context.check_end_layout(os);
2045 parent_context.new_paragraph(os);
2047 minted_float.clear();
2048 minted_float_has_caption = false;
2051 else if (name == "lstlisting" || name == "minted") {
2052 bool use_minted = name == "minted";
2053 eat_whitespace(p, os, parent_context, false);
2054 if (use_minted && minted_float.empty()) {
2055 // look ahead for a bottom caption
2057 bool found_end_minted = false;
2058 while (!found_end_minted && p.good()) {
2059 Token const & t = p.get_token();
2061 string const envname =
2062 p.next_token().cat() == catBegin
2063 ? p.getArg('{', '}') : string();
2064 found_end_minted = t.asInput() == "\\end"
2065 && envname == "minted";
2067 eat_whitespace(p, os, parent_context, true);
2068 Token const & t = p.get_token();
2069 p.skip_spaces(true);
2070 if (t.asInput() == "\\lyxmintcaption") {
2071 string const pos = p.getArg('[', ']');
2073 string const caption =
2074 parse_text_snippet(p, FLAG_ITEM,
2075 false, parent_context);
2076 minted_nonfloat_caption = "[b]" + caption;
2077 eat_whitespace(p, os, parent_context, true);
2082 parse_listings(p, os, parent_context, false, use_minted);
2086 else if (!parent_context.new_layout_allowed)
2087 parse_unknown_environment(p, name, os, FLAG_END, outer,
2090 // Alignment and spacing settings
2091 // FIXME (bug xxxx): These settings can span multiple paragraphs and
2092 // therefore are totally broken!
2093 // Note that \centering, \raggedright, and \raggedleft cannot be handled, as
2094 // they are commands not environments. They are furthermore switches that
2095 // can be ended by another switches, but also by commands like \footnote or
2096 // \parbox. So the only safe way is to leave them untouched.
2097 // However, we support the pseudo-environments
2098 // \begin{centering} ... \end{centering}
2099 // \begin{raggedright} ... \end{raggedright}
2100 // \begin{raggedleft} ... \end{raggedleft}
2101 // since they are used by LyX in floats (for spacing reasons)
2102 else if (name == "center" || name == "centering" ||
2103 name == "flushleft" || name == "raggedright" ||
2104 name == "flushright" || name == "raggedleft" ||
2105 name == "singlespace" || name == "onehalfspace" ||
2106 name == "doublespace" || name == "spacing") {
2107 eat_whitespace(p, os, parent_context, false);
2108 // We must begin a new paragraph if not already done
2109 if (! parent_context.atParagraphStart()) {
2110 parent_context.check_end_layout(os);
2111 parent_context.new_paragraph(os);
2113 if (name == "flushleft" || name == "raggedright")
2114 parent_context.add_extra_stuff("\\align left\n");
2115 else if (name == "flushright" || name == "raggedleft")
2116 parent_context.add_extra_stuff("\\align right\n");
2117 else if (name == "center" || name == "centering")
2118 parent_context.add_extra_stuff("\\align center\n");
2119 else if (name == "singlespace")
2120 parent_context.add_extra_stuff("\\paragraph_spacing single\n");
2121 else if (name == "onehalfspace") {
2122 parent_context.add_extra_stuff("\\paragraph_spacing onehalf\n");
2123 preamble.registerAutomaticallyLoadedPackage("setspace");
2124 } else if (name == "doublespace") {
2125 parent_context.add_extra_stuff("\\paragraph_spacing double\n");
2126 preamble.registerAutomaticallyLoadedPackage("setspace");
2127 } else if (name == "spacing") {
2128 parent_context.add_extra_stuff("\\paragraph_spacing other " + p.verbatim_item() + "\n");
2129 preamble.registerAutomaticallyLoadedPackage("setspace");
2131 parse_text(p, os, FLAG_END, outer, parent_context);
2132 // Just in case the environment is empty
2133 parent_context.extra_stuff.erase();
2134 // We must begin a new paragraph to reset the alignment
2135 parent_context.new_paragraph(os);
2139 // The single '=' is meant here.
2140 else if ((newlayout = findLayout(parent_context.textclass, name, false))) {
2141 eat_whitespace(p, os, parent_context, false);
2142 Context context(true, parent_context.textclass, newlayout,
2143 parent_context.layout, parent_context.font);
2144 if (parent_context.deeper_paragraph) {
2145 // We are beginning a nested environment after a
2146 // deeper paragraph inside the outer list environment.
2147 // Therefore we don't need to output a "begin deeper".
2148 context.need_end_deeper = true;
2150 parent_context.check_end_layout(os);
2151 if (last_env == name) {
2152 // we need to output a separator since LyX would export
2153 // the two environments as one otherwise (bug 5716)
2154 TeX2LyXDocClass const & textclass(parent_context.textclass);
2155 Context newcontext(true, textclass,
2156 &(textclass.defaultLayout()));
2157 newcontext.check_layout(os);
2158 begin_inset(os, "Separator plain\n");
2160 newcontext.check_end_layout(os);
2162 switch (context.layout->latextype) {
2163 case LATEX_LIST_ENVIRONMENT:
2164 context.add_par_extra_stuff("\\labelwidthstring "
2165 + p.verbatim_item() + '\n');
2168 case LATEX_BIB_ENVIRONMENT:
2169 p.verbatim_item(); // swallow next arg
2175 context.check_deeper(os);
2176 if (newlayout->keepempty) {
2177 // We need to start a new paragraph
2178 // even if it is empty.
2179 context.new_paragraph(os);
2180 context.check_layout(os);
2182 // handle known optional and required arguments
2183 if (context.layout->latextype == LATEX_ENVIRONMENT)
2184 output_arguments(os, p, outer, false, string(), context,
2185 context.layout->latexargs());
2186 else if (context.layout->latextype == LATEX_ITEM_ENVIRONMENT) {
2188 output_arguments(oss, p, outer, false, string(), context,
2189 context.layout->latexargs());
2190 context.list_extra_stuff = oss.str();
2192 parse_text(p, os, FLAG_END, outer, context);
2193 if (context.layout->latextype == LATEX_ENVIRONMENT)
2194 output_arguments(os, p, outer, false, "post", context,
2195 context.layout->postcommandargs());
2196 context.check_end_layout(os);
2197 if (parent_context.deeper_paragraph) {
2198 // We must suppress the "end deeper" because we
2199 // suppressed the "begin deeper" above.
2200 context.need_end_deeper = false;
2202 context.check_end_deeper(os);
2203 parent_context.new_paragraph(os);
2205 if (!preamble.titleLayoutFound())
2206 preamble.titleLayoutFound(newlayout->intitle);
2207 set<string> const & req = newlayout->requires();
2208 set<string>::const_iterator it = req.begin();
2209 set<string>::const_iterator en = req.end();
2210 for (; it != en; ++it)
2211 preamble.registerAutomaticallyLoadedPackage(*it);
2214 // The single '=' is meant here.
2215 else if ((newinsetlayout = findInsetLayout(parent_context.textclass, name, false))) {
2216 eat_whitespace(p, os, parent_context, false);
2217 parent_context.check_layout(os);
2218 begin_inset(os, "Flex ");
2219 os << to_utf8(newinsetlayout->name()) << '\n'
2220 << "status collapsed\n";
2221 if (newinsetlayout->isPassThru()) {
2222 string const arg = p.verbatimEnvironment(name);
2223 Context context(true, parent_context.textclass,
2224 &parent_context.textclass.plainLayout(),
2225 parent_context.layout);
2226 output_ert(os, arg, parent_context);
2228 parse_text_in_inset(p, os, FLAG_END, false, parent_context, newinsetlayout);
2232 else if (name == "appendix") {
2233 // This is no good latex style, but it works and is used in some documents...
2234 eat_whitespace(p, os, parent_context, false);
2235 parent_context.check_end_layout(os);
2236 Context context(true, parent_context.textclass, parent_context.layout,
2237 parent_context.layout, parent_context.font);
2238 context.check_layout(os);
2239 os << "\\start_of_appendix\n";
2240 parse_text(p, os, FLAG_END, outer, context);
2241 context.check_end_layout(os);
2245 else if (known_environments.find(name) != known_environments.end()) {
2246 vector<ArgumentType> arguments = known_environments[name];
2247 // The last "argument" denotes wether we may translate the
2248 // environment contents to LyX
2249 // The default required if no argument is given makes us
2250 // compatible with the reLyXre environment.
2251 ArgumentType contents = arguments.empty() ?
2254 if (!arguments.empty())
2255 arguments.pop_back();
2256 // See comment in parse_unknown_environment()
2257 bool const specialfont =
2258 (parent_context.font != parent_context.normalfont);
2259 bool const new_layout_allowed =
2260 parent_context.new_layout_allowed;
2262 parent_context.new_layout_allowed = false;
2263 parse_arguments("\\begin{" + name + "}", arguments, p, os,
2264 outer, parent_context);
2265 if (contents == verbatim)
2266 output_ert_inset(os, p.ertEnvironment(name),
2269 parse_text_snippet(p, os, FLAG_END, outer,
2271 output_ert_inset(os, "\\end{" + name + "}", parent_context);
2273 parent_context.new_layout_allowed = new_layout_allowed;
2277 parse_unknown_environment(p, name, os, FLAG_END, outer,
2281 active_environments.pop_back();
2285 /// parses a comment and outputs it to \p os.
2286 void parse_comment(Parser & p, ostream & os, Token const & t, Context & context)
2288 LASSERT(t.cat() == catComment, return);
2289 if (!t.cs().empty()) {
2290 context.check_layout(os);
2291 output_comment(p, os, t.cs(), context);
2292 if (p.next_token().cat() == catNewline) {
2293 // A newline after a comment line starts a new
2295 if (context.new_layout_allowed) {
2296 if(!context.atParagraphStart())
2297 // Only start a new paragraph if not already
2298 // done (we might get called recursively)
2299 context.new_paragraph(os);
2301 output_ert_inset(os, "\n", context);
2302 eat_whitespace(p, os, context, true);
2305 // "%\n" combination
2312 * Reads spaces and comments until the first non-space, non-comment token.
2313 * New paragraphs (double newlines or \\par) are handled like simple spaces
2314 * if \p eatParagraph is true.
2315 * Spaces are skipped, but comments are written to \p os.
2317 void eat_whitespace(Parser & p, ostream & os, Context & context,
2321 Token const & t = p.get_token();
2322 if (t.cat() == catComment)
2323 parse_comment(p, os, t, context);
2324 else if ((! eatParagraph && p.isParagraph()) ||
2325 (t.cat() != catSpace && t.cat() != catNewline)) {
2334 * Set a font attribute, parse text and reset the font attribute.
2335 * \param attribute Attribute name (e.g. \\family, \\shape etc.)
2336 * \param currentvalue Current value of the attribute. Is set to the new
2337 * value during parsing.
2338 * \param newvalue New value of the attribute
2340 void parse_text_attributes(Parser & p, ostream & os, unsigned flags, bool outer,
2341 Context & context, string const & attribute,
2342 string & currentvalue, string const & newvalue)
2344 context.check_layout(os);
2345 string const oldvalue = currentvalue;
2346 currentvalue = newvalue;
2347 os << '\n' << attribute << ' ' << newvalue << "\n";
2348 parse_text_snippet(p, os, flags, outer, context);
2349 context.check_layout(os);
2350 os << '\n' << attribute << ' ' << oldvalue << "\n";
2351 currentvalue = oldvalue;
2355 /// get the arguments of a natbib or jurabib citation command
2356 void get_cite_arguments(Parser & p, bool natbibOrder,
2357 string & before, string & after, bool const qualified = false)
2359 // We need to distinguish "" and "[]", so we can't use p.getOpt().
2361 // text before the citation
2363 // text after the citation
2364 after = qualified ? p.getFullOpt(false, '(', ')') : p.getFullOpt();
2366 if (!after.empty()) {
2367 before = qualified ? p.getFullOpt(false, '(', ')') : p.getFullOpt();
2368 if (natbibOrder && !before.empty())
2369 swap(before, after);
2374 void copy_file(FileName const & src, string dstname)
2378 string const absParent = getParentFilePath(false);
2380 if (FileName::isAbsolute(dstname))
2381 dst = FileName(dstname);
2383 dst = makeAbsPath(dstname, absParent);
2384 FileName const srcpath = src.onlyPath();
2385 FileName const dstpath = dst.onlyPath();
2386 if (equivalent(srcpath, dstpath))
2388 if (!dstpath.isDirectory()) {
2389 if (!dstpath.createPath()) {
2390 cerr << "Warning: Could not create directory for file `"
2391 << dst.absFileName() << "´." << endl;
2395 if (dst.isReadableFile()) {
2396 if (overwriteFiles())
2397 cerr << "Warning: Overwriting existing file `"
2398 << dst.absFileName() << "´." << endl;
2400 cerr << "Warning: Not overwriting existing file `"
2401 << dst.absFileName() << "´." << endl;
2405 if (!src.copyTo(dst))
2406 cerr << "Warning: Could not copy file `" << src.absFileName()
2407 << "´ to `" << dst.absFileName() << "´." << endl;
2411 /// Parse a literate Chunk section. The initial "<<" is already parsed.
2412 bool parse_chunk(Parser & p, ostream & os, Context & context)
2414 // check whether a chunk is possible here.
2415 if (!context.textclass.hasInsetLayout(from_ascii("Flex:Chunk"))) {
2421 // read the parameters
2422 Parser::Arg const params = p.verbatimStuff(">>=\n", false);
2423 if (!params.first) {
2428 Parser::Arg const code = p.verbatimStuff("\n@");
2433 string const post_chunk = p.verbatimStuff("\n").second + '\n';
2434 if (post_chunk[0] != ' ' && post_chunk[0] != '\n') {
2438 // The last newline read is important for paragraph handling
2442 //cerr << "params=[" << params.second << "], code=[" << code.second << "]" <<endl;
2443 // We must have a valid layout before outputting the Chunk inset.
2444 context.check_layout(os);
2445 Context chunkcontext(true, context.textclass);
2446 chunkcontext.layout = &context.textclass.plainLayout();
2447 begin_inset(os, "Flex Chunk");
2448 os << "\nstatus open\n";
2449 if (!params.second.empty()) {
2450 chunkcontext.check_layout(os);
2451 Context paramscontext(true, context.textclass);
2452 paramscontext.layout = &context.textclass.plainLayout();
2453 begin_inset(os, "Argument 1");
2454 os << "\nstatus open\n";
2455 output_ert(os, params.second, paramscontext);
2458 output_ert(os, code.second, chunkcontext);
2466 /// detects \\def, \\long\\def and \\global\\long\\def with ws and comments
2467 bool is_macro(Parser & p)
2469 Token first = p.curr_token();
2470 if (first.cat() != catEscape || !p.good())
2472 if (first.cs() == "def")
2474 if (first.cs() != "global" && first.cs() != "long")
2476 Token second = p.get_token();
2478 while (p.good() && !p.isParagraph() && (second.cat() == catSpace ||
2479 second.cat() == catNewline || second.cat() == catComment)) {
2480 second = p.get_token();
2483 bool secondvalid = second.cat() == catEscape;
2485 bool thirdvalid = false;
2486 if (p.good() && first.cs() == "global" && secondvalid &&
2487 second.cs() == "long") {
2488 third = p.get_token();
2490 while (p.good() && !p.isParagraph() &&
2491 (third.cat() == catSpace ||
2492 third.cat() == catNewline ||
2493 third.cat() == catComment)) {
2494 third = p.get_token();
2497 thirdvalid = third.cat() == catEscape;
2499 for (int i = 0; i < pos; ++i)
2504 return (first.cs() == "global" || first.cs() == "long") &&
2505 second.cs() == "def";
2506 return first.cs() == "global" && second.cs() == "long" &&
2507 third.cs() == "def";
2511 /// Parse a macro definition (assumes that is_macro() returned true)
2512 void parse_macro(Parser & p, ostream & os, Context & context)
2514 context.check_layout(os);
2515 Token first = p.curr_token();
2518 string command = first.asInput();
2519 if (first.cs() != "def") {
2521 eat_whitespace(p, os, context, false);
2522 second = p.curr_token();
2523 command += second.asInput();
2524 if (second.cs() != "def") {
2526 eat_whitespace(p, os, context, false);
2527 third = p.curr_token();
2528 command += third.asInput();
2531 eat_whitespace(p, os, context, false);
2532 string const name = p.get_token().cs();
2533 eat_whitespace(p, os, context, false);
2539 while (p.next_token().cat() != catBegin) {
2540 if (p.next_token().cat() == catParameter) {
2545 // followed by number?
2546 if (p.next_token().cat() == catOther) {
2547 string s = p.get_token().asInput();
2549 // number = current arity + 1?
2550 if (s.size() == 1 && s[0] == arity + '0' + 1)
2555 paramtext += p.get_token().cs();
2557 paramtext += p.get_token().cs();
2562 // only output simple (i.e. compatible) macro as FormulaMacros
2563 string ert = '\\' + name + ' ' + paramtext + '{' + p.verbatim_item() + '}';
2565 context.check_layout(os);
2566 begin_inset(os, "FormulaMacro");
2567 os << "\n\\def" << ert;
2570 output_ert_inset(os, command + ert, context);
2574 void registerExternalTemplatePackages(string const & name)
2576 external::TemplateManager const & etm = external::TemplateManager::get();
2577 external::Template const * const et = etm.getTemplateByName(name);
2580 external::Template::Formats::const_iterator cit = et->formats.end();
2582 cit = et->formats.find("PDFLaTeX");
2583 if (cit == et->formats.end())
2584 // If the template has not specified a PDFLaTeX output,
2585 // we try the LaTeX format.
2586 cit = et->formats.find("LaTeX");
2587 if (cit == et->formats.end())
2589 vector<string>::const_iterator qit = cit->second.requirements.begin();
2590 vector<string>::const_iterator qend = cit->second.requirements.end();
2591 for (; qit != qend; ++qit)
2592 preamble.registerAutomaticallyLoadedPackage(*qit);
2595 } // anonymous namespace
2599 * Find a file with basename \p name in path \p path and an extension
2602 string find_file(string const & name, string const & path,
2603 char const * const * extensions)
2605 for (char const * const * what = extensions; *what; ++what) {
2606 string const trial = addExtension(name, *what);
2607 if (makeAbsPath(trial, path).exists())
2614 /// Convert filenames with TeX macros and/or quotes to something LyX
2616 string const normalize_filename(string const & name)
2621 Token const & t = p.get_token();
2622 if (t.cat() != catEscape)
2624 else if (t.cs() == "lyxdot") {
2625 // This is used by LyX for simple dots in relative
2629 } else if (t.cs() == "space") {
2632 } else if (t.cs() == "string") {
2633 // Convert \string" to " and \string~ to ~
2634 Token const & n = p.next_token();
2635 if (n.asInput() != "\"" && n.asInput() != "~")
2640 // Strip quotes. This is a bit complicated (see latex_path()).
2641 string full = os.str();
2642 if (!full.empty() && full[0] == '"') {
2643 string base = removeExtension(full);
2644 string ext = getExtension(full);
2645 if (!base.empty() && base[base.length()-1] == '"')
2648 return addExtension(trim(base, "\""), ext);
2649 if (full[full.length()-1] == '"')
2652 return trim(full, "\"");
2658 /// Convert \p name from TeX convention (relative to master file) to LyX
2659 /// convention (relative to .lyx file) if it is relative
2660 void fix_child_filename(string & name)
2662 string const absMasterTeX = getMasterFilePath(true);
2663 bool const isabs = FileName::isAbsolute(name);
2664 // convert from "relative to .tex master" to absolute original path
2666 name = makeAbsPath(name, absMasterTeX).absFileName();
2667 bool copyfile = copyFiles();
2668 string const absParentLyX = getParentFilePath(false);
2671 // convert from absolute original path to "relative to master file"
2672 string const rel = to_utf8(makeRelPath(from_utf8(name),
2673 from_utf8(absMasterTeX)));
2674 // re-interpret "relative to .tex file" as "relative to .lyx file"
2675 // (is different if the master .lyx file resides in a
2676 // different path than the master .tex file)
2677 string const absMasterLyX = getMasterFilePath(false);
2678 abs = makeAbsPath(rel, absMasterLyX).absFileName();
2679 // Do not copy if the new path is impossible to create. Example:
2680 // absMasterTeX = "/foo/bar/"
2681 // absMasterLyX = "/bar/"
2682 // name = "/baz.eps" => new absolute name would be "/../baz.eps"
2683 if (contains(name, "/../"))
2690 // convert from absolute original path to
2691 // "relative to .lyx file"
2692 name = to_utf8(makeRelPath(from_utf8(abs),
2693 from_utf8(absParentLyX)));
2697 // convert from absolute original path to "relative to .lyx file"
2698 name = to_utf8(makeRelPath(from_utf8(name),
2699 from_utf8(absParentLyX)));
2704 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
2705 Context & context, string const rdelim)
2707 Layout const * newlayout = 0;
2708 InsetLayout const * newinsetlayout = 0;
2709 char const * const * where = 0;
2710 // Store the latest bibliographystyle, addcontentslineContent and
2711 // nocite{*} option (needed for bibtex inset)
2713 string contentslineContent;
2714 // Some classes provide a \bibliographystyle, so do not output
2715 // any if none is explicitly set.
2716 string bibliographystyle;
2717 bool const use_natbib = isProvided("natbib");
2718 bool const use_jurabib = isProvided("jurabib");
2719 bool const use_biblatex = isProvided("biblatex")
2720 && preamble.citeEngine() != "biblatex-natbib";
2721 bool const use_biblatex_natbib = isProvided("biblatex-natbib")
2722 || (isProvided("biblatex") && preamble.citeEngine() == "biblatex-natbib");
2723 need_commentbib = use_biblatex || use_biblatex_natbib;
2726 // it is impossible to determine the correct encoding for non-CJK Japanese.
2727 // Therefore write a note at the beginning of the document
2728 if (is_nonCJKJapanese) {
2729 context.check_layout(os);
2730 begin_inset(os, "Note Note\n");
2731 os << "status open\n\\begin_layout Plain Layout\n"
2732 << "\\series bold\n"
2733 << "Important information:\n"
2734 << "\\end_layout\n\n"
2735 << "\\begin_layout Plain Layout\n"
2736 << "The original LaTeX source for this document is in Japanese (pLaTeX).\n"
2737 << " It was therefore impossible for tex2lyx to determine the correct encoding.\n"
2738 << " The iconv encoding " << p.getEncoding() << " was used.\n"
2739 << " If this is incorrect, you must run the tex2lyx program on the command line\n"
2740 << " and specify the encoding using the -e command-line switch.\n"
2741 << " In addition, you might want to double check that the desired output encoding\n"
2742 << " is correctly selected in Document > Settings > Language.\n"
2743 << "\\end_layout\n";
2745 is_nonCJKJapanese = false;
2748 bool have_cycled = false;
2750 // Leave here only after at least one cycle
2751 if (have_cycled && flags & FLAG_LEAVE) {
2752 flags &= ~FLAG_LEAVE;
2756 Token const & t = p.get_token();
2758 debugToken(cerr, t, flags);
2761 if (flags & FLAG_ITEM) {
2762 if (t.cat() == catSpace)
2765 flags &= ~FLAG_ITEM;
2766 if (t.cat() == catBegin) {
2767 // skip the brace and collect everything to the next matching
2769 flags |= FLAG_BRACE_LAST;
2773 // handle only this single token, leave the loop if done
2774 flags |= FLAG_LEAVE;
2777 if (t.cat() != catEscape && t.character() == ']' &&
2778 (flags & FLAG_BRACK_LAST))
2780 if (t.cat() == catEnd && (flags & FLAG_BRACE_LAST))
2782 string tok = t.asInput();
2783 // we only support delimiters with max 2 chars for now.
2784 if (rdelim.size() > 1)
2785 tok += p.next_token().asInput();
2786 if (t.cat() != catEscape && !rdelim.empty()
2787 && tok == rdelim && (flags & FLAG_RDELIM)) {
2788 if (rdelim.size() > 1)
2789 p.get_token(); // eat rdelim
2793 // If there is anything between \end{env} and \begin{env} we
2794 // don't need to output a separator.
2795 if (t.cat() != catSpace && t.cat() != catNewline &&
2796 t.asInput() != "\\begin")
2803 bool const starred = p.next_token().asInput() == "*";
2804 string const starredname(starred ? (t.cs() + '*') : t.cs());
2805 if (t.cat() == catMath) {
2806 // we are inside some text mode thingy, so opening new math is allowed
2807 context.check_layout(os);
2808 begin_inset(os, "Formula ");
2809 Token const & n = p.get_token();
2810 bool const display(n.cat() == catMath && outer);
2812 // TeX's $$...$$ syntax for displayed math
2814 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
2816 p.get_token(); // skip the second '$' token
2818 // simple $...$ stuff
2821 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
2826 // Prevent the conversion of a line break to a
2827 // space (bug 7668). This does not change the
2828 // output, but looks ugly in LyX.
2829 eat_whitespace(p, os, context, false);
2834 if (t.cat() == catSuper || t.cat() == catSub) {
2835 cerr << "catcode " << t << " illegal in text mode\n";
2839 // Basic support for quotes. We try to disambiguate
2840 // quotes from the context (e.g., a left english quote is
2841 // the same as a right german quote...).
2842 // Try to make a smart guess about the side
2843 Token const prev = p.prev_token();
2844 bool const opening = (prev.cat() != catSpace && prev.character() != 0
2845 && prev.character() != '\n' && prev.character() != '~');
2846 if (t.asInput() == "`" && p.next_token().asInput() == "`") {
2847 context.check_layout(os);
2848 begin_inset(os, "Quotes ");
2849 os << guessQuoteStyle("eld", opening);
2855 if (t.asInput() == "'" && p.next_token().asInput() == "'") {
2856 context.check_layout(os);
2857 begin_inset(os, "Quotes ");
2858 os << guessQuoteStyle("erd", opening);
2865 if (t.asInput() == ">" && p.next_token().asInput() == ">") {
2866 context.check_layout(os);
2867 begin_inset(os, "Quotes ");
2868 os << guessQuoteStyle("ald", opening);
2875 if (t.asInput() == "<"
2876 && p.next_token().asInput() == "<") {
2877 bool has_chunk = false;
2881 has_chunk = parse_chunk(p, os, context);
2887 context.check_layout(os);
2888 begin_inset(os, "Quotes ");
2889 os << guessQuoteStyle("ard", opening);
2897 if (t.cat() == catSpace || (t.cat() == catNewline && ! p.isParagraph())) {
2898 check_space(p, os, context);
2902 // babel shorthands (also used by polyglossia)
2903 // Since these can have different meanings for different languages
2904 // we import them as ERT (but they must be put in ERT to get output
2906 if (t.asInput() == "\"") {
2908 // These are known pairs. We put them together in
2909 // one ERT inset. In other cases (such as "a), only
2910 // the quotation mark is ERTed.
2911 if (p.next_token().asInput() == "\""
2912 || p.next_token().asInput() == "|"
2913 || p.next_token().asInput() == "-"
2914 || p.next_token().asInput() == "~"
2915 || p.next_token().asInput() == "="
2916 || p.next_token().asInput() == "/"
2917 || p.next_token().asInput() == "~"
2918 || p.next_token().asInput() == "'"
2919 || p.next_token().asInput() == "`"
2920 || p.next_token().asInput() == "<"
2921 || p.next_token().asInput() == ">") {
2922 s += p.next_token().asInput();
2925 output_ert_inset(os, s, context);
2929 if (t.character() == '[' && noweb_mode &&
2930 p.next_token().character() == '[') {
2931 // These can contain underscores
2933 string const s = p.getFullOpt() + ']';
2934 if (p.next_token().character() == ']')
2937 cerr << "Warning: Inserting missing ']' in '"
2938 << s << "'." << endl;
2939 output_ert_inset(os, s, context);
2943 if (t.cat() == catLetter) {
2944 context.check_layout(os);
2949 if (t.cat() == catOther ||
2950 t.cat() == catAlign ||
2951 t.cat() == catParameter) {
2952 context.check_layout(os);
2953 if (t.asInput() == "-" && p.next_token().asInput() == "-" &&
2954 context.merging_hyphens_allowed &&
2955 context.font.family != "ttfamily" &&
2956 !context.layout->pass_thru) {
2957 if (p.next_next_token().asInput() == "-") {
2959 os << to_utf8(docstring(1, 0x2014));
2963 os << to_utf8(docstring(1, 0x2013));
2966 // This translates "&" to "\\&" which may be wrong...
2971 if (p.isParagraph()) {
2972 // In minted floating listings we will collect
2973 // everything into the caption, where multiple
2974 // paragraphs are forbidden.
2975 if (minted_float.empty()) {
2976 if (context.new_layout_allowed)
2977 context.new_paragraph(os);
2979 output_ert_inset(os, "\\par ", context);
2982 eat_whitespace(p, os, context, true);
2986 if (t.cat() == catActive) {
2987 context.check_layout(os);
2988 if (t.character() == '~') {
2989 if (context.layout->free_spacing)
2992 begin_inset(os, "space ~\n");
3000 if (t.cat() == catBegin) {
3001 Token const next = p.next_token();
3002 Token const end = p.next_next_token();
3003 if (next.cat() == catEnd) {
3005 Token const prev = p.prev_token();
3007 if (p.next_token().character() == '`')
3008 ; // ignore it in {}``
3010 output_ert_inset(os, "{}", context);
3011 } else if (next.cat() == catEscape &&
3012 is_known(next.cs(), known_quotes) &&
3013 end.cat() == catEnd) {
3014 // Something like {\textquoteright} (e.g.
3015 // from writer2latex). We may skip the
3016 // braces here for better readability.
3017 parse_text_snippet(p, os, FLAG_BRACE_LAST,
3019 } else if (p.next_token().asInput() == "\\ascii") {
3020 // handle the \ascii characters
3021 // (the case without braces is handled later)
3022 // the code is "{\ascii\xxx}"
3023 p.get_token(); // eat \ascii
3024 string name2 = p.get_token().asInput();
3025 p.get_token(); // eat the final '}'
3026 string const name = "{\\ascii" + name2 + "}";
3030 // get the character from unicodesymbols
3031 docstring s = encodings.fromLaTeXCommand(from_utf8(name),
3032 Encodings::TEXT_CMD, termination, rem, &req);
3034 context.check_layout(os);
3037 output_ert_inset(os,
3038 to_utf8(rem), context);
3039 for (set<string>::const_iterator it = req.begin();
3040 it != req.end(); ++it)
3041 preamble.registerAutomaticallyLoadedPackage(*it);
3043 // we did not find a non-ert version
3044 output_ert_inset(os, name, context);
3046 context.check_layout(os);
3047 // special handling of font attribute changes
3048 Token const prev = p.prev_token();
3049 TeXFont const oldFont = context.font;
3050 if (next.character() == '[' ||
3051 next.character() == ']' ||
3052 next.character() == '*') {
3054 if (p.next_token().cat() == catEnd) {
3059 output_ert_inset(os, "{", context);
3060 parse_text_snippet(p, os,
3063 output_ert_inset(os, "}", context);
3065 } else if (! context.new_layout_allowed) {
3066 output_ert_inset(os, "{", context);
3067 parse_text_snippet(p, os, FLAG_BRACE_LAST,
3069 output_ert_inset(os, "}", context);
3070 } else if (is_known(next.cs(), known_sizes)) {
3071 // next will change the size, so we must
3073 parse_text_snippet(p, os, FLAG_BRACE_LAST,
3075 if (!context.atParagraphStart())
3077 << context.font.size << "\n";
3078 } else if (is_known(next.cs(), known_font_families)) {
3079 // next will change the font family, so we
3080 // must reset it here
3081 parse_text_snippet(p, os, FLAG_BRACE_LAST,
3083 if (!context.atParagraphStart())
3085 << context.font.family << "\n";
3086 } else if (is_known(next.cs(), known_font_series)) {
3087 // next will change the font series, so we
3088 // must reset it here
3089 parse_text_snippet(p, os, FLAG_BRACE_LAST,
3091 if (!context.atParagraphStart())
3093 << context.font.series << "\n";
3094 } else if (is_known(next.cs(), known_font_shapes)) {
3095 // next will change the font shape, so we
3096 // must reset it here
3097 parse_text_snippet(p, os, FLAG_BRACE_LAST,
3099 if (!context.atParagraphStart())
3101 << context.font.shape << "\n";
3102 } else if (is_known(next.cs(), known_old_font_families) ||
3103 is_known(next.cs(), known_old_font_series) ||
3104 is_known(next.cs(), known_old_font_shapes)) {
3105 // next will change the font family, series
3106 // and shape, so we must reset it here
3107 parse_text_snippet(p, os, FLAG_BRACE_LAST,
3109 if (!context.atParagraphStart())
3111 << context.font.family
3113 << context.font.series
3115 << context.font.shape << "\n";
3117 output_ert_inset(os, "{", context);
3118 parse_text_snippet(p, os, FLAG_BRACE_LAST,
3120 output_ert_inset(os, "}", context);
3126 if (t.cat() == catEnd) {
3127 if (flags & FLAG_BRACE_LAST) {
3130 cerr << "stray '}' in text\n";
3131 output_ert_inset(os, "}", context);
3135 if (t.cat() == catComment) {
3136 parse_comment(p, os, t, context);
3141 // control sequences
3144 if (t.cs() == "(" || t.cs() == "[") {
3145 bool const simple = t.cs() == "(";
3146 context.check_layout(os);
3147 begin_inset(os, "Formula");
3148 os << " \\" << t.cs();
3149 parse_math(p, os, simple ? FLAG_SIMPLE2 : FLAG_EQUATION, MATH_MODE);
3150 os << '\\' << (simple ? ')' : ']');
3153 // Prevent the conversion of a line break to a
3154 // space (bug 7668). This does not change the
3155 // output, but looks ugly in LyX.
3156 eat_whitespace(p, os, context, false);
3161 if (t.cs() == "begin") {
3162 parse_environment(p, os, outer, last_env,
3167 if (t.cs() == "end") {
3168 if (flags & FLAG_END) {
3169 // eat environment name
3170 string const name = p.getArg('{', '}');
3171 if (name != active_environment())
3172 cerr << "\\end{" + name + "} does not match \\begin{"
3173 + active_environment() + "}\n";
3176 p.error("found 'end' unexpectedly");
3180 // "item" by default, but could be something else
3181 if (t.cs() == context.layout->itemcommand()) {
3183 if (context.layout->labeltype == LABEL_MANUAL) {
3184 // FIXME: This swallows comments, but we cannot use
3185 // eat_whitespace() since we must not output
3186 // anything before the item.
3187 p.skip_spaces(true);
3188 s = p.verbatimOption();
3190 p.skip_spaces(false);
3192 context.check_layout(os);
3193 if (context.has_item) {
3194 // An item in an unknown list-like environment
3195 // FIXME: Do this in check_layout()!
3196 context.has_item = false;
3197 string item = "\\" + context.layout->itemcommand();
3200 output_ert_inset(os, item, context);
3202 if (context.layout->labeltype != LABEL_MANUAL)
3203 output_arguments(os, p, outer, false, "item", context,
3204 context.layout->itemargs());
3205 if (!context.list_extra_stuff.empty()) {
3206 os << context.list_extra_stuff;
3207 context.list_extra_stuff.clear();
3209 else if (!s.empty()) {
3210 // LyX adds braces around the argument,
3211 // so we need to remove them here.
3212 if (s.size() > 2 && s[0] == '{' &&
3213 s[s.size()-1] == '}')
3214 s = s.substr(1, s.size()-2);
3215 // If the argument contains a space we
3216 // must put it into ERT: Otherwise LyX
3217 // would misinterpret the space as
3218 // item delimiter (bug 7663)
3219 if (contains(s, ' ')) {
3220 output_ert_inset(os, s, context);
3223 os << parse_text_snippet(p2,
3224 FLAG_BRACK_LAST, outer, context);
3226 // The space is needed to separate the
3227 // item from the rest of the sentence.
3229 eat_whitespace(p, os, context, false);
3234 if (t.cs() == "bibitem") {
3236 context.check_layout(os);
3237 eat_whitespace(p, os, context, false);
3238 string label = p.verbatimOption();
3239 pair<bool, string> lbl = convert_latexed_command_inset_arg(label);
3240 bool const literal = !lbl.first;
3241 label = literal ? subst(label, "\n", " ") : lbl.second;
3242 string lit = literal ? "\"true\"" : "\"false\"";
3243 string key = convert_literate_command_inset_arg(p.verbatim_item());
3244 begin_command_inset(os, "bibitem", "bibitem");
3245 os << "label \"" << label << "\"\n"
3246 << "key \"" << key << "\"\n"
3247 << "literal " << lit << "\n";
3253 // catch the case of \def\inputGnumericTable
3255 if (t.cs() == "def") {
3256 Token second = p.next_token();
3257 if (second.cs() == "inputGnumericTable") {
3261 Token third = p.get_token();
3263 if (third.cs() == "input") {
3267 string name = normalize_filename(p.verbatim_item());
3268 string const path = getMasterFilePath(true);
3269 // We want to preserve relative / absolute filenames,
3270 // therefore path is only used for testing
3271 // The file extension is in every case ".tex".
3272 // So we need to remove this extension and check for
3273 // the original one.
3274 name = removeExtension(name);
3275 if (!makeAbsPath(name, path).exists()) {
3276 char const * const Gnumeric_formats[] = {"gnumeric",
3278 string const Gnumeric_name =
3279 find_file(name, path, Gnumeric_formats);
3280 if (!Gnumeric_name.empty())
3281 name = Gnumeric_name;
3283 FileName const absname = makeAbsPath(name, path);
3284 if (absname.exists()) {
3285 fix_child_filename(name);
3286 copy_file(absname, name);
3288 cerr << "Warning: Could not find file '"
3289 << name << "'." << endl;
3290 context.check_layout(os);
3291 begin_inset(os, "External\n\ttemplate ");
3292 os << "GnumericSpreadsheet\n\tfilename "
3295 context.check_layout(os);
3297 // register the packages that are automatically loaded
3298 // by the Gnumeric template
3299 registerExternalTemplatePackages("GnumericSpreadsheet");
3304 parse_macro(p, os, context);
3308 if (t.cs() == "noindent") {
3310 context.add_par_extra_stuff("\\noindent\n");
3314 if (t.cs() == "appendix") {
3315 context.add_par_extra_stuff("\\start_of_appendix\n");
3316 // We need to start a new paragraph. Otherwise the
3317 // appendix in 'bla\appendix\chapter{' would start
3319 context.new_paragraph(os);
3320 // We need to make sure that the paragraph is
3321 // generated even if it is empty. Otherwise the
3322 // appendix in '\par\appendix\par\chapter{' would
3324 context.check_layout(os);
3325 // FIXME: This is a hack to prevent paragraph
3326 // deletion if it is empty. Handle this better!
3327 output_comment(p, os,
3328 "dummy comment inserted by tex2lyx to "
3329 "ensure that this paragraph is not empty",
3331 // Both measures above may generate an additional
3332 // empty paragraph, but that does not hurt, because
3333 // whitespace does not matter here.
3334 eat_whitespace(p, os, context, true);
3338 // Must catch empty dates before findLayout is called below
3339 if (t.cs() == "date") {
3340 eat_whitespace(p, os, context, false);
3342 string const date = p.verbatim_item();
3345 preamble.suppressDate(true);
3348 preamble.suppressDate(false);
3349 if (context.new_layout_allowed &&
3350 (newlayout = findLayout(context.textclass,
3353 output_command_layout(os, p, outer,
3354 context, newlayout);
3355 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
3356 if (!preamble.titleLayoutFound())
3357 preamble.titleLayoutFound(newlayout->intitle);
3358 set<string> const & req = newlayout->requires();
3359 set<string>::const_iterator it = req.begin();
3360 set<string>::const_iterator en = req.end();
3361 for (; it != en; ++it)
3362 preamble.registerAutomaticallyLoadedPackage(*it);
3364 output_ert_inset(os,
3365 "\\date{" + p.verbatim_item() + '}',
3371 // Before we look for the layout name with star and alone below, we check the layouts including
3372 // the LateXParam, which might be one or several options or a star.
3373 // The single '=' is meant here.
3374 if (context.new_layout_allowed &&
3375 (newlayout = findLayout(context.textclass, t.cs(), true, p.getCommandLatexParam()))) {
3376 // store the latexparam here. This is eaten in output_command_layout
3377 context.latexparam = newlayout->latexparam();
3379 output_command_layout(os, p, outer, context, newlayout);
3380 context.latexparam.clear();
3382 if (!preamble.titleLayoutFound())
3383 preamble.titleLayoutFound(newlayout->intitle);
3384 set<string> const & req = newlayout->requires();
3385 for (set<string>::const_iterator it = req.begin(); it != req.end(); ++it)
3386 preamble.registerAutomaticallyLoadedPackage(*it);
3390 // Starred section headings
3391 // Must attempt to parse "Section*" before "Section".
3392 if ((p.next_token().asInput() == "*") &&
3393 context.new_layout_allowed &&
3394 (newlayout = findLayout(context.textclass, t.cs() + '*', true))) {
3397 output_command_layout(os, p, outer, context, newlayout);
3399 if (!preamble.titleLayoutFound())
3400 preamble.titleLayoutFound(newlayout->intitle);
3401 set<string> const & req = newlayout->requires();
3402 for (set<string>::const_iterator it = req.begin(); it != req.end(); ++it)
3403 preamble.registerAutomaticallyLoadedPackage(*it);
3407 // Section headings and the like
3408 if (context.new_layout_allowed &&
3409 (newlayout = findLayout(context.textclass, t.cs(), true))) {
3411 output_command_layout(os, p, outer, context, newlayout);
3413 if (!preamble.titleLayoutFound())
3414 preamble.titleLayoutFound(newlayout->intitle);
3415 set<string> const & req = newlayout->requires();
3416 for (set<string>::const_iterator it = req.begin(); it != req.end(); ++it)
3417 preamble.registerAutomaticallyLoadedPackage(*it);
3421 if (t.cs() == "subfloat") {
3422 // the syntax is \subfloat[list entry][sub caption]{content}
3423 // if it is a table of figure depends on the surrounding float
3425 // do nothing if there is no outer float
3426 if (!float_type.empty()) {
3427 context.check_layout(os);
3429 begin_inset(os, "Float " + float_type + "\n");
3431 << "\nsideways false"
3432 << "\nstatus collapsed\n\n";
3435 bool has_caption = false;
3436 if (p.next_token().cat() != catEscape &&
3437 p.next_token().character() == '[') {
3438 p.get_token(); // eat '['
3439 caption = parse_text_snippet(p, FLAG_BRACK_LAST, outer, context);
3442 // In case we have two optional args, the second is the caption.
3443 if (p.next_token().cat() != catEscape &&
3444 p.next_token().character() == '[') {
3445 p.get_token(); // eat '['
3446 caption = parse_text_snippet(p, FLAG_BRACK_LAST, outer, context);
3449 parse_text_in_inset(p, os, FLAG_ITEM, outer, context);
3450 // the caption comes always as the last
3452 // we must make sure that the caption gets a \begin_layout
3453 os << "\n\\begin_layout Plain Layout";
3455 begin_inset(os, "Caption Standard\n");
3456 Context newcontext(true, context.textclass,
3457 0, 0, context.font);
3458 newcontext.check_layout(os);
3459 os << caption << "\n";
3460 newcontext.check_end_layout(os);
3463 // close the layout we opened
3464 os << "\n\\end_layout";
3469 // if the float type is not supported or there is no surrounding float
3474 opt_arg1 = convert_literate_command_inset_arg(p.getFullOpt());
3476 opt_arg2 = convert_literate_command_inset_arg(p.getFullOpt());
3478 output_ert_inset(os, t.asInput() + opt_arg1 + opt_arg2
3479 + "{" + p.verbatim_item() + '}', context);
3484 if (t.cs() == "xymatrix") {
3485 // we must open a new math because LyX's xy support is in math
3486 context.check_layout(os);
3487 begin_inset(os, "Formula ");
3489 os << "\\" << t.cs() << '{';
3490 parse_math(p, os, FLAG_ITEM, MATH_MODE);
3493 preamble.registerAutomaticallyLoadedPackage("xy");
3497 if (t.cs() == "includegraphics") {
3498 bool const clip = p.next_token().asInput() == "*";
3501 string const arg = p.getArg('[', ']');
3502 map<string, string> opts;
3503 vector<string> keys;
3504 split_map(arg, opts, keys);
3506 opts["clip"] = string();
3507 string name = normalize_filename(p.verbatim_item());
3509 string const path = getMasterFilePath(true);
3510 // We want to preserve relative / absolute filenames,
3511 // therefore path is only used for testing
3512 if (!makeAbsPath(name, path).exists()) {
3513 // The file extension is probably missing.
3514 // Now try to find it out.
3515 string const dvips_name =
3516 find_file(name, path,
3517 known_dvips_graphics_formats);
3518 string const pdftex_name =
3519 find_file(name, path,
3520 known_pdftex_graphics_formats);
3521 if (!dvips_name.empty()) {
3522 if (!pdftex_name.empty()) {
3523 cerr << "This file contains the "
3525 "\"\\includegraphics{"
3527 "However, files\n\""
3528 << dvips_name << "\" and\n\""
3529 << pdftex_name << "\"\n"
3530 "both exist, so I had to make a "
3531 "choice and took the first one.\n"
3532 "Please move the unwanted one "
3533 "someplace else and try again\n"
3534 "if my choice was wrong."
3538 } else if (!pdftex_name.empty()) {
3544 FileName const absname = makeAbsPath(name, path);
3545 if (absname.exists()) {
3546 fix_child_filename(name);
3547 copy_file(absname, name);
3549 cerr << "Warning: Could not find graphics file '"
3550 << name << "'." << endl;
3552 context.check_layout(os);
3553 begin_inset(os, "Graphics ");
3554 os << "\n\tfilename " << name << '\n';
3555 if (opts.find("width") != opts.end())
3557 << translate_len(opts["width"]) << '\n';
3558 if (opts.find("height") != opts.end())
3560 << translate_len(opts["height"]) << '\n';
3561 if (opts.find("scale") != opts.end()) {
3562 istringstream iss(opts["scale"]);
3566 os << "\tscale " << val << '\n';
3568 if (opts.find("angle") != opts.end()) {
3569 os << "\trotateAngle "
3570 << opts["angle"] << '\n';
3571 vector<string>::const_iterator a =
3572 find(keys.begin(), keys.end(), "angle");
3573 vector<string>::const_iterator s =
3574 find(keys.begin(), keys.end(), "width");
3575 if (s == keys.end())
3576 s = find(keys.begin(), keys.end(), "height");
3577 if (s == keys.end())
3578 s = find(keys.begin(), keys.end(), "scale");
3579 if (s != keys.end() && distance(s, a) > 0)
3580 os << "\tscaleBeforeRotation\n";
3582 if (opts.find("origin") != opts.end()) {
3584 string const opt = opts["origin"];
3585 if (opt.find('l') != string::npos) ss << "left";
3586 if (opt.find('r') != string::npos) ss << "right";
3587 if (opt.find('c') != string::npos) ss << "center";
3588 if (opt.find('t') != string::npos) ss << "Top";
3589 if (opt.find('b') != string::npos) ss << "Bottom";
3590 if (opt.find('B') != string::npos) ss << "Baseline";
3591 if (!ss.str().empty())
3592 os << "\trotateOrigin " << ss.str() << '\n';
3594 cerr << "Warning: Ignoring unknown includegraphics origin argument '" << opt << "'\n";
3596 if (opts.find("keepaspectratio") != opts.end())
3597 os << "\tkeepAspectRatio\n";
3598 if (opts.find("clip") != opts.end())
3600 if (opts.find("draft") != opts.end())
3602 if (opts.find("bb") != opts.end())
3603 os << "\tBoundingBox "
3604 << opts["bb"] << '\n';
3605 int numberOfbbOptions = 0;
3606 if (opts.find("bbllx") != opts.end())
3607 numberOfbbOptions++;
3608 if (opts.find("bblly") != opts.end())
3609 numberOfbbOptions++;
3610 if (opts.find("bburx") != opts.end())
3611 numberOfbbOptions++;
3612 if (opts.find("bbury") != opts.end())
3613 numberOfbbOptions++;
3614 if (numberOfbbOptions == 4)
3615 os << "\tBoundingBox "
3616 << opts["bbllx"] << " " << opts["bblly"] << " "
3617 << opts["bburx"] << " " << opts["bbury"] << '\n';
3618 else if (numberOfbbOptions > 0)
3619 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
3620 numberOfbbOptions = 0;
3621 if (opts.find("natwidth") != opts.end())
3622 numberOfbbOptions++;
3623 if (opts.find("natheight") != opts.end())
3624 numberOfbbOptions++;
3625 if (numberOfbbOptions == 2)
3626 os << "\tBoundingBox 0bp 0bp "
3627 << opts["natwidth"] << " " << opts["natheight"] << '\n';
3628 else if (numberOfbbOptions > 0)
3629 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
3630 ostringstream special;
3631 if (opts.find("hiresbb") != opts.end())
3632 special << "hiresbb,";
3633 if (opts.find("trim") != opts.end())
3635 if (opts.find("viewport") != opts.end())
3636 special << "viewport=" << opts["viewport"] << ',';
3637 if (opts.find("totalheight") != opts.end())
3638 special << "totalheight=" << opts["totalheight"] << ',';
3639 if (opts.find("type") != opts.end())
3640 special << "type=" << opts["type"] << ',';
3641 if (opts.find("ext") != opts.end())
3642 special << "ext=" << opts["ext"] << ',';
3643 if (opts.find("read") != opts.end())
3644 special << "read=" << opts["read"] << ',';
3645 if (opts.find("command") != opts.end())
3646 special << "command=" << opts["command"] << ',';
3647 string s_special = special.str();
3648 if (!s_special.empty()) {
3649 // We had special arguments. Remove the trailing ','.
3650 os << "\tspecial " << s_special.substr(0, s_special.size() - 1) << '\n';
3652 // TODO: Handle the unknown settings better.
3653 // Warn about invalid options.
3654 // Check whether some option was given twice.
3656 preamble.registerAutomaticallyLoadedPackage("graphicx");
3660 if (t.cs() == "footnote" ||
3661 (t.cs() == "thanks" && context.layout->intitle)) {
3663 context.check_layout(os);
3664 begin_inset(os, "Foot\n");
3665 os << "status collapsed\n\n";
3666 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
3671 if (t.cs() == "marginpar") {
3673 context.check_layout(os);
3674 begin_inset(os, "Marginal\n");
3675 os << "status collapsed\n\n";
3676 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
3681 if (t.cs() == "lstinline" || t.cs() == "mintinline") {
3682 bool const use_minted = t.cs() == "mintinline";
3684 parse_listings(p, os, context, true, use_minted);
3688 if (t.cs() == "ensuremath") {
3690 context.check_layout(os);
3691 string const s = p.verbatim_item();
3692 //FIXME: this never triggers in UTF8
3693 if (s == "\xb1" || s == "\xb3" || s == "\xb2" || s == "\xb5")
3696 output_ert_inset(os, "\\ensuremath{" + s + "}",
3701 else if (t.cs() == "makeindex"
3702 || (t.cs() == context.textclass.titlename()
3703 && context.textclass.titletype() == TITLE_COMMAND_AFTER)) {
3704 if (preamble.titleLayoutFound()) {
3706 skip_spaces_braces(p);
3708 output_ert_inset(os, t.asInput(), context);
3712 if (t.cs() == "tableofcontents"
3713 || t.cs() == "lstlistoflistings"
3714 || t.cs() == "listoflistings") {
3715 string name = t.cs();
3716 if (preamble.minted() && name == "listoflistings")
3717 name.insert(0, "lst");
3718 context.check_layout(os);
3719 begin_command_inset(os, "toc", name);
3721 skip_spaces_braces(p);
3722 if (name == "lstlistoflistings") {
3723 if (preamble.minted())
3724 preamble.registerAutomaticallyLoadedPackage("minted");
3726 preamble.registerAutomaticallyLoadedPackage("listings");
3731 if (t.cs() == "listoffigures" || t.cs() == "listoftables") {
3732 context.check_layout(os);
3733 if (t.cs() == "listoffigures")
3734 begin_inset(os, "FloatList figure\n");
3736 begin_inset(os, "FloatList table\n");
3738 skip_spaces_braces(p);
3742 if (t.cs() == "listof") {
3743 p.skip_spaces(true);
3744 string const name = p.get_token().cs();
3745 if (context.textclass.floats().typeExist(name)) {
3746 context.check_layout(os);
3747 begin_inset(os, "FloatList ");
3750 p.get_token(); // swallow second arg
3752 output_ert_inset(os, "\\listof{" + name + "}", context);
3756 if ((where = is_known(t.cs(), known_text_font_families))) {
3757 parse_text_attributes(p, os, FLAG_ITEM, outer,
3758 context, "\\family", context.font.family,
3759 known_coded_font_families[where - known_text_font_families]);
3763 // beamer has a \textbf<overlay>{} inset
3764 if (!p.hasOpt("<") && (where = is_known(t.cs(), known_text_font_series))) {
3765 parse_text_attributes(p, os, FLAG_ITEM, outer,
3766 context, "\\series", context.font.series,
3767 known_coded_font_series[where - known_text_font_series]);
3771 // beamer has a \textit<overlay>{} inset
3772 if (!p.hasOpt("<") && (where = is_known(t.cs(), known_text_font_shapes))) {
3773 parse_text_attributes(p, os, FLAG_ITEM, outer,
3774 context, "\\shape", context.font.shape,
3775 known_coded_font_shapes[where - known_text_font_shapes]);
3779 if (t.cs() == "textnormal" || t.cs() == "normalfont") {
3780 context.check_layout(os);
3781 TeXFont oldFont = context.font;
3782 context.font.init();
3783 context.font.size = oldFont.size;
3784 os << "\n\\family " << context.font.family << "\n";
3785 os << "\n\\series " << context.font.series << "\n";
3786 os << "\n\\shape " << context.font.shape << "\n";
3787 if (t.cs() == "textnormal") {
3788 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
3789 output_font_change(os, context.font, oldFont);
3790 context.font = oldFont;
3792 eat_whitespace(p, os, context, false);
3796 if (t.cs() == "textcolor") {
3797 // scheme is \textcolor{color name}{text}
3798 string const color = p.verbatim_item();
3799 // we support the predefined colors of the color and the xcolor package
3800 if (color == "black" || color == "blue" || color == "cyan"
3801 || color == "green" || color == "magenta" || color == "red"
3802 || color == "white" || color == "yellow") {
3803 context.check_layout(os);
3804 os << "\n\\color " << color << "\n";
3805 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
3806 context.check_layout(os);
3807 os << "\n\\color inherit\n";
3808 preamble.registerAutomaticallyLoadedPackage("color");
3809 } else if (color == "brown" || color == "darkgray" || color == "gray"
3810 || color == "lightgray" || color == "lime" || color == "olive"
3811 || color == "orange" || color == "pink" || color == "purple"
3812 || color == "teal" || color == "violet") {
3813 context.check_layout(os);
3814 os << "\n\\color " << color << "\n";
3815 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
3816 context.check_layout(os);
3817 os << "\n\\color inherit\n";
3818 preamble.registerAutomaticallyLoadedPackage("xcolor");
3820 // for custom defined colors
3821 output_ert_inset(os, t.asInput() + "{" + color + "}", context);
3825 if (t.cs() == "underbar" || t.cs() == "uline") {
3826 // \underbar is not 100% correct (LyX outputs \uline
3827 // of ulem.sty). The difference is that \ulem allows
3828 // line breaks, and \underbar does not.
3829 // Do NOT handle \underline.
3830 // \underbar cuts through y, g, q, p etc.,
3831 // \underline does not.
3832 context.check_layout(os);
3833 os << "\n\\bar under\n";
3834 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
3835 context.check_layout(os);
3836 os << "\n\\bar default\n";
3837 preamble.registerAutomaticallyLoadedPackage("ulem");
3841 if (t.cs() == "sout") {
3842 context.check_layout(os);
3843 os << "\n\\strikeout on\n";
3844 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
3845 context.check_layout(os);
3846 os << "\n\\strikeout default\n";
3847 preamble.registerAutomaticallyLoadedPackage("ulem");
3851 // beamer has an \emph<overlay>{} inset
3852 if ((t.cs() == "uuline" || t.cs() == "uwave"
3853 || t.cs() == "emph" || t.cs() == "noun"
3854 || t.cs() == "xout") && !p.hasOpt("<")) {
3855 context.check_layout(os);
3856 os << "\n\\" << t.cs() << " on\n";
3857 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
3858 context.check_layout(os);
3859 os << "\n\\" << t.cs() << " default\n";
3860 if (t.cs() == "uuline" || t.cs() == "uwave" || t.cs() == "xout")
3861 preamble.registerAutomaticallyLoadedPackage("ulem");
3865 if (t.cs() == "lyxadded" || t.cs() == "lyxdeleted") {
3866 context.check_layout(os);
3867 string name = p.getArg('{', '}');
3868 string localtime = p.getArg('{', '}');
3869 preamble.registerAuthor(name);
3870 Author const & author = preamble.getAuthor(name);
3871 // from_asctime_utc() will fail if LyX decides to output the
3872 // time in the text language.
3873 time_t ptime = from_asctime_utc(localtime);
3874 if (ptime == static_cast<time_t>(-1)) {
3875 cerr << "Warning: Could not parse time `" << localtime
3876 << "´ for change tracking, using current time instead.\n";
3877 ptime = current_time();
3879 if (t.cs() == "lyxadded")
3880 os << "\n\\change_inserted ";
3882 os << "\n\\change_deleted ";
3883 os << author.bufferId() << ' ' << ptime << '\n';
3884 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
3885 bool dvipost = LaTeXPackages::isAvailable("dvipost");
3886 bool xcolorulem = LaTeXPackages::isAvailable("ulem") &&
3887 LaTeXPackages::isAvailable("xcolor");
3888 // No need to test for luatex, since luatex comes in
3889 // two flavours (dvi and pdf), like latex, and those
3890 // are detected by pdflatex.
3891 if (pdflatex || xetex) {
3893 preamble.registerAutomaticallyLoadedPackage("ulem");
3894 preamble.registerAutomaticallyLoadedPackage("xcolor");
3895 preamble.registerAutomaticallyLoadedPackage("pdfcolmk");
3899 preamble.registerAutomaticallyLoadedPackage("dvipost");
3900 } else if (xcolorulem) {
3901 preamble.registerAutomaticallyLoadedPackage("ulem");
3902 preamble.registerAutomaticallyLoadedPackage("xcolor");
3908 if (t.cs() == "textipa") {
3909 context.check_layout(os);
3910 begin_inset(os, "IPA\n");
3911 bool merging_hyphens_allowed = context.merging_hyphens_allowed;
3912 context.merging_hyphens_allowed = false;
3913 parse_text_in_inset(p, os, FLAG_ITEM, outer, context);
3914 context.merging_hyphens_allowed = merging_hyphens_allowed;
3916 preamble.registerAutomaticallyLoadedPackage("tipa");
3917 preamble.registerAutomaticallyLoadedPackage("tipx");
3921 if ((preamble.isPackageUsed("tipa") && t.cs() == "t" && p.next_token().asInput() == "*")
3922 || t.cs() == "texttoptiebar" || t.cs() == "textbottomtiebar") {
3923 context.check_layout(os);
3927 string const type = (t.cs() == "t") ? "bottomtiebar" : t.cs().substr(4);
3928 begin_inset(os, "IPADeco " + type + "\n");
3929 os << "status open\n";
3930 parse_text_in_inset(p, os, FLAG_ITEM, outer, context);
3936 if (t.cs() == "textvertline") {
3937 // FIXME: This is not correct, \textvertline is higher than |
3943 if (t.cs() == "tone" ) {
3944 context.check_layout(os);
3945 // register the tone package
3946 preamble.registerAutomaticallyLoadedPackage("tone");
3947 string content = trimSpaceAndEol(p.verbatim_item());
3948 string command = t.asInput() + "{" + content + "}";
3949 // some tones can be detected by unicodesymbols, some need special code
3950 if (is_known(content, known_tones)) {
3951 os << "\\IPAChar " << command << "\n";
3954 // try to see whether the string is in unicodesymbols
3958 docstring s = encodings.fromLaTeXCommand(from_utf8(command),
3959 Encodings::TEXT_CMD | Encodings::MATH_CMD,
3960 termination, rem, &req);
3964 output_ert_inset(os, to_utf8(rem), context);
3965 for (set<string>::const_iterator it = req.begin();
3966 it != req.end(); ++it)
3967 preamble.registerAutomaticallyLoadedPackage(*it);
3969 // we did not find a non-ert version
3970 output_ert_inset(os, command, context);
3974 if (t.cs() == "phantom" || t.cs() == "hphantom" ||
3975 t.cs() == "vphantom") {
3976 context.check_layout(os);
3977 if (t.cs() == "phantom")
3978 begin_inset(os, "Phantom Phantom\n");
3979 if (t.cs() == "hphantom")
3980 begin_inset(os, "Phantom HPhantom\n");
3981 if (t.cs() == "vphantom")
3982 begin_inset(os, "Phantom VPhantom\n");
3983 os << "status open\n";
3984 parse_text_in_inset(p, os, FLAG_ITEM, outer, context,
3990 if (t.cs() == "href") {
3991 context.check_layout(os);
3992 string target = convert_literate_command_inset_arg(p.verbatim_item());
3993 string name = p.verbatim_item();
3994 pair<bool, string> nm = convert_latexed_command_inset_arg(name);
3995 bool const literal = !nm.first;
3996 name = literal ? subst(name, "\n", " ") : nm.second;
3997 string lit = literal ? "\"true\"" : "\"false\"";
3999 size_t i = target.find(':');
4000 if (i != string::npos) {
4001 type = target.substr(0, i + 1);
4002 if (type == "mailto:" || type == "file:")
4003 target = target.substr(i + 1);
4004 // handle the case that name is equal to target, except of "http(s)://"
4005 else if (target.substr(i + 3) == name && (type == "http:" || type == "https:"))
4008 begin_command_inset(os, "href", "href");
4010 os << "name \"" << name << "\"\n";
4011 os << "target \"" << target << "\"\n";
4012 if (type == "mailto:" || type == "file:")
4013 os << "type \"" << type << "\"\n";
4014 os << "literal " << lit << "\n";
4016 skip_spaces_braces(p);
4020 if (t.cs() == "lyxline") {
4021 // swallow size argument (it is not used anyway)
4023 if (!context.atParagraphStart()) {
4024 // so our line is in the middle of a paragraph
4025 // we need to add a new line, lest this line
4026 // follow the other content on that line and
4027 // run off the side of the page
4028 // FIXME: This may create an empty paragraph,
4029 // but without that it would not be
4030 // possible to set noindent below.
4031 // Fortunately LaTeX does not care
4032 // about the empty paragraph.
4033 context.new_paragraph(os);
4035 if (preamble.indentParagraphs()) {
4036 // we need to unindent, lest the line be too long
4037 context.add_par_extra_stuff("\\noindent\n");
4039 context.check_layout(os);
4040 begin_command_inset(os, "line", "rule");
4041 os << "offset \"0.5ex\"\n"
4042 "width \"100line%\"\n"
4048 if (t.cs() == "rule") {
4049 string const offset = (p.hasOpt() ? p.getArg('[', ']') : string());
4050 string const width = p.getArg('{', '}');
4051 string const thickness = p.getArg('{', '}');
4052 context.check_layout(os);
4053 begin_command_inset(os, "line", "rule");
4054 if (!offset.empty())
4055 os << "offset \"" << translate_len(offset) << "\"\n";
4056 os << "width \"" << translate_len(width) << "\"\n"
4057 "height \"" << translate_len(thickness) << "\"\n";
4062 // Handle refstyle first in order to to catch \eqref, because this
4063 // can also occur without refstyle. Only recognize these commands if
4064 // refstyle.sty was found in the preamble (otherwise \eqref
4065 // and user defined ref commands could be misdetected).
4066 // We uncapitalize the input in order to catch capitalized commands
4068 if ((where = is_known(uncapitalize(t.cs()), known_refstyle_commands))
4069 && preamble.refstyle()) {
4070 string const cap = isCapitalized(t.cs()) ? "true" : "false";
4071 string plural = "false";
4072 // Catch the plural option [s]
4074 string const opt = p.getOpt();
4078 // LyX does not yet support other optional arguments of ref commands
4079 output_ert_inset(os, t.asInput() + opt + "{" +
4080 p.verbatim_item() + '}', context);
4084 context.check_layout(os);
4085 begin_command_inset(os, "ref", "formatted");
4086 os << "reference \"";
4087 os << known_refstyle_prefixes[where - known_refstyle_commands]
4089 os << convert_literate_command_inset_arg(p.getArg('{', '}'))
4091 os << "plural \"" << plural << "\"\n";
4092 os << "caps \"" << cap << "\"\n";
4093 os << "noprefix \"false\"\n";
4095 preamble.registerAutomaticallyLoadedPackage("refstyle");
4099 // if refstyle is used, we must not convert \prettyref to a
4100 // formatted reference, since that would result in a refstyle command.
4101 if ((where = is_known(t.cs(), known_ref_commands)) &&
4102 (t.cs() != "prettyref" || !preamble.refstyle())) {
4103 string const opt = p.getOpt();
4105 context.check_layout(os);
4106 begin_command_inset(os, "ref",
4107 known_coded_ref_commands[where - known_ref_commands]);
4108 os << "reference \""
4109 << convert_literate_command_inset_arg(p.verbatim_item())
4111 os << "plural \"false\"\n";
4112 os << "caps \"false\"\n";
4113 os << "noprefix \"false\"\n";
4115 if (t.cs() == "vref" || t.cs() == "vpageref")
4116 preamble.registerAutomaticallyLoadedPackage("varioref");
4117 else if (t.cs() == "prettyref")
4118 preamble.registerAutomaticallyLoadedPackage("prettyref");
4120 // LyX does not yet support optional arguments of ref commands
4121 output_ert_inset(os, t.asInput() + opt + "{" +
4122 p.verbatim_item() + '}', context);
4128 is_known(t.cs(), known_natbib_commands) &&
4129 ((t.cs() != "citefullauthor" &&
4130 t.cs() != "citeyear" &&
4131 t.cs() != "citeyearpar") ||
4132 p.next_token().asInput() != "*")) {
4133 context.check_layout(os);
4134 string command = t.cs();
4135 if (p.next_token().asInput() == "*") {
4139 if (command == "citefullauthor")
4140 // alternative name for "\\citeauthor*"
4141 command = "citeauthor*";
4143 // text before the citation
4145 // text after the citation
4147 get_cite_arguments(p, true, before, after);
4149 if (command == "cite") {
4150 // \cite without optional argument means
4151 // \citet, \cite with at least one optional
4152 // argument means \citep.
4153 if (before.empty() && after.empty())
4158 if (before.empty() && after == "[]")
4159 // avoid \citet[]{a}
4161 else if (before == "[]" && after == "[]") {
4162 // avoid \citet[][]{a}
4166 bool literal = false;
4167 pair<bool, string> aft;
4168 pair<bool, string> bef;
4169 // remove the brackets around after and before
4170 if (!after.empty()) {
4172 after.erase(after.length() - 1, 1);
4173 aft = convert_latexed_command_inset_arg(after);
4174 literal = !aft.first;
4175 after = literal ? subst(after, "\n", " ") : aft.second;
4177 if (!before.empty()) {
4179 before.erase(before.length() - 1, 1);
4180 bef = convert_latexed_command_inset_arg(before);
4181 literal |= !bef.first;
4182 before = literal ? subst(before, "\n", " ") : bef.second;
4183 if (literal && !after.empty())
4184 after = subst(after, "\n", " ");
4186 string lit = literal ? "\"true\"" : "\"false\"";
4187 begin_command_inset(os, "citation", command);
4188 os << "after " << '"' << after << '"' << "\n";
4189 os << "before " << '"' << before << '"' << "\n";
4191 << convert_literate_command_inset_arg(p.verbatim_item())
4193 << "literal " << lit << "\n";
4195 // Need to set the cite engine if natbib is loaded by
4196 // the document class directly
4197 if (preamble.citeEngine() == "basic")
4198 preamble.citeEngine("natbib");
4203 && is_known(t.cs(), known_biblatex_commands)
4204 && ((t.cs() == "cite"
4205 || t.cs() == "citeauthor"
4206 || t.cs() == "Citeauthor"
4207 || t.cs() == "parencite"
4208 || t.cs() == "citetitle")
4209 || p.next_token().asInput() != "*"))
4210 || (use_biblatex_natbib
4211 && (is_known(t.cs(), known_biblatex_commands)
4212 || is_known(t.cs(), known_natbib_commands))
4213 && ((t.cs() == "cite" || t.cs() == "citet" || t.cs() == "Citet"
4214 || t.cs() == "citep" || t.cs() == "Citep" || t.cs() == "citealt"
4215 || t.cs() == "Citealt" || t.cs() == "citealp" || t.cs() == "Citealp"
4216 || t.cs() == "citeauthor" || t.cs() == "Citeauthor"
4217 || t.cs() == "parencite" || t.cs() == "citetitle")
4218 || p.next_token().asInput() != "*"))){
4219 context.check_layout(os);
4220 string command = t.cs();
4221 if (p.next_token().asInput() == "*") {
4226 bool const qualified = suffixIs(command, "s");
4228 command = rtrim(command, "s");
4230 // text before the citation
4232 // text after the citation
4234 get_cite_arguments(p, true, before, after, qualified);
4236 // These use natbib cmd names in LyX
4237 // for inter-citeengine compativility
4238 if (command == "citeyear")
4239 command = "citebyear";
4240 else if (command == "cite*")
4241 command = "citeyear";
4242 else if (command == "textcite")
4244 else if (command == "Textcite")
4246 else if (command == "parencite")
4248 else if (command == "Parencite")
4250 else if (command == "parencite*")
4251 command = "citeyearpar";
4252 else if (command == "smartcite")
4253 command = "footcite";
4254 else if (command == "Smartcite")
4255 command = "Footcite";
4257 string const emptyarg = qualified ? "()" : "[]";
4258 if (before.empty() && after == emptyarg)
4261 else if (before == emptyarg && after == emptyarg) {
4262 // avoid \cite[][]{a}
4266 bool literal = false;
4267 pair<bool, string> aft;
4268 pair<bool, string> bef;
4269 // remove the brackets around after and before
4270 if (!after.empty()) {
4272 after.erase(after.length() - 1, 1);
4273 aft = convert_latexed_command_inset_arg(after);
4274 literal = !aft.first;
4275 after = literal ? subst(after, "\n", " ") : aft.second;
4277 if (!before.empty()) {
4279 before.erase(before.length() - 1, 1);
4280 bef = convert_latexed_command_inset_arg(before);
4281 literal |= !bef.first;
4282 before = literal ? subst(before, "\n", " ") : bef.second;
4284 string keys, pretextlist, posttextlist;
4286 map<string, string> pres, posts, preslit, postslit;
4287 vector<string> lkeys;
4288 // text before the citation
4289 string lbefore, lbeforelit;
4290 // text after the citation
4291 string lafter, lafterlit;
4293 pair<bool, string> laft, lbef;
4295 get_cite_arguments(p, true, lbefore, lafter);
4296 // remove the brackets around after and before
4297 if (!lafter.empty()) {
4299 lafter.erase(lafter.length() - 1, 1);
4300 laft = convert_latexed_command_inset_arg(lafter);
4301 literal |= !laft.first;
4302 lafter = laft.second;
4303 lafterlit = subst(lbefore, "\n", " ");
4305 if (!lbefore.empty()) {
4306 lbefore.erase(0, 1);
4307 lbefore.erase(lbefore.length() - 1, 1);
4308 lbef = convert_latexed_command_inset_arg(lbefore);
4309 literal |= !lbef.first;
4310 lbefore = lbef.second;
4311 lbeforelit = subst(lbefore, "\n", " ");
4313 if (lbefore.empty() && lafter == "[]") {
4318 else if (lbefore == "[]" && lafter == "[]") {
4319 // avoid \cite[][]{a}
4325 lkey = p.getArg('{', '}');
4328 if (!lbefore.empty()) {
4329 pres.insert(make_pair(lkey, lbefore));
4330 preslit.insert(make_pair(lkey, lbeforelit));
4332 if (!lafter.empty()) {
4333 posts.insert(make_pair(lkey, lafter));
4334 postslit.insert(make_pair(lkey, lafterlit));
4336 lkeys.push_back(lkey);
4338 keys = convert_literate_command_inset_arg(getStringFromVector(lkeys));
4343 for (auto const & ptl : pres) {
4344 if (!pretextlist.empty())
4345 pretextlist += '\t';
4346 pretextlist += ptl.first + " " + ptl.second;
4348 for (auto const & potl : posts) {
4349 if (!posttextlist.empty())
4350 posttextlist += '\t';
4351 posttextlist += potl.first + " " + potl.second;
4354 keys = convert_literate_command_inset_arg(p.verbatim_item());
4357 after = subst(after, "\n", " ");
4358 if (!before.empty())
4359 before = subst(after, "\n", " ");
4361 string lit = literal ? "\"true\"" : "\"false\"";
4362 begin_command_inset(os, "citation", command);
4363 os << "after " << '"' << after << '"' << "\n";
4364 os << "before " << '"' << before << '"' << "\n";
4368 if (!pretextlist.empty())
4369 os << "pretextlist " << '"' << pretextlist << '"' << "\n";
4370 if (!posttextlist.empty())
4371 os << "posttextlist " << '"' << posttextlist << '"' << "\n";
4372 os << "literal " << lit << "\n";
4374 // Need to set the cite engine if biblatex is loaded by
4375 // the document class directly
4376 if (preamble.citeEngine() == "basic")
4377 use_biblatex_natbib ?
4378 preamble.citeEngine("biblatex-natbib")
4379 : preamble.citeEngine("biblatex");
4384 is_known(t.cs(), known_jurabib_commands) &&
4385 (t.cs() == "cite" || p.next_token().asInput() != "*")) {
4386 context.check_layout(os);
4387 string command = t.cs();
4388 if (p.next_token().asInput() == "*") {
4392 char argumentOrder = '\0';
4393 vector<string> const options =
4394 preamble.getPackageOptions("jurabib");
4395 if (find(options.begin(), options.end(),
4396 "natbiborder") != options.end())
4397 argumentOrder = 'n';
4398 else if (find(options.begin(), options.end(),
4399 "jurabiborder") != options.end())
4400 argumentOrder = 'j';
4402 // text before the citation
4404 // text after the citation
4406 get_cite_arguments(p, argumentOrder != 'j', before, after);
4408 string const citation = p.verbatim_item();
4409 if (!before.empty() && argumentOrder == '\0') {
4410 cerr << "Warning: Assuming argument order "
4411 "of jurabib version 0.6 for\n'"
4412 << command << before << after << '{'
4413 << citation << "}'.\n"
4414 "Add 'jurabiborder' to the jurabib "
4415 "package options if you used an\n"
4416 "earlier jurabib version." << endl;
4418 bool literal = false;
4419 pair<bool, string> aft;
4420 pair<bool, string> bef;
4421 // remove the brackets around after and before
4422 if (!after.empty()) {
4424 after.erase(after.length() - 1, 1);
4425 aft = convert_latexed_command_inset_arg(after);
4426 literal = !aft.first;
4427 after = literal ? subst(after, "\n", " ") : aft.second;
4429 if (!before.empty()) {
4431 before.erase(before.length() - 1, 1);
4432 bef = convert_latexed_command_inset_arg(before);
4433 literal |= !bef.first;
4434 before = literal ? subst(before, "\n", " ") : bef.second;
4435 if (literal && !after.empty())
4436 after = subst(after, "\n", " ");
4438 string lit = literal ? "\"true\"" : "\"false\"";
4439 begin_command_inset(os, "citation", command);
4440 os << "after " << '"' << after << "\"\n"
4441 << "before " << '"' << before << "\"\n"
4442 << "key " << '"' << citation << "\"\n"
4443 << "literal " << lit << "\n";
4445 // Need to set the cite engine if jurabib is loaded by
4446 // the document class directly
4447 if (preamble.citeEngine() == "basic")
4448 preamble.citeEngine("jurabib");
4452 if (t.cs() == "cite"
4453 || t.cs() == "nocite") {
4454 context.check_layout(os);
4455 string after = p.getArg('[', ']');
4456 pair<bool, string> aft = convert_latexed_command_inset_arg(after);
4457 bool const literal = !aft.first;
4458 after = literal ? subst(after, "\n", " ") : aft.second;
4459 string lit = literal ? "\"true\"" : "\"false\"";
4460 string key = convert_literate_command_inset_arg(p.verbatim_item());
4461 // store the case that it is "\nocite{*}" to use it later for
4464 begin_command_inset(os, "citation", t.cs());
4465 os << "after " << '"' << after << "\"\n"
4466 << "key " << '"' << key << "\"\n"
4467 << "literal " << lit << "\n";
4469 } else if (t.cs() == "nocite")
4474 if (t.cs() == "index" ||
4475 (t.cs() == "sindex" && preamble.use_indices() == "true")) {
4476 context.check_layout(os);
4477 string const arg = (t.cs() == "sindex" && p.hasOpt()) ?
4478 p.getArg('[', ']') : "";
4479 string const kind = arg.empty() ? "idx" : arg;
4480 begin_inset(os, "Index ");
4481 os << kind << "\nstatus collapsed\n";
4482 parse_text_in_inset(p, os, FLAG_ITEM, false, context, "Index");
4485 preamble.registerAutomaticallyLoadedPackage("splitidx");
4489 if (t.cs() == "nomenclature") {
4490 context.check_layout(os);
4491 begin_command_inset(os, "nomenclature", "nomenclature");
4492 string prefix = convert_literate_command_inset_arg(p.getArg('[', ']'));
4493 if (!prefix.empty())
4494 os << "prefix " << '"' << prefix << '"' << "\n";
4495 string symbol = p.verbatim_item();
4496 pair<bool, string> sym = convert_latexed_command_inset_arg(symbol);
4497 bool literal = !sym.first;
4498 string description = p.verbatim_item();
4499 pair<bool, string> desc = convert_latexed_command_inset_arg(description);
4500 literal |= !desc.first;
4502 symbol = subst(symbol, "\n", " ");
4503 description = subst(description, "\n", " ");
4505 symbol = sym.second;
4506 description = desc.second;
4508 string lit = literal ? "\"true\"" : "\"false\"";
4509 os << "symbol " << '"' << symbol;
4510 os << "\"\ndescription \""
4511 << description << "\"\n"
4512 << "literal " << lit << "\n";
4514 preamble.registerAutomaticallyLoadedPackage("nomencl");
4518 if (t.cs() == "label") {
4519 context.check_layout(os);
4520 begin_command_inset(os, "label", "label");
4522 << convert_literate_command_inset_arg(p.verbatim_item())
4528 if (t.cs() == "lyxmintcaption") {
4529 string const pos = p.getArg('[', ']');
4531 string const caption =
4532 parse_text_snippet(p, FLAG_ITEM, false,
4534 minted_nonfloat_caption = "[t]" + caption;
4536 // We already got the caption at the bottom,
4537 // so simply skip it.
4538 parse_text_snippet(p, FLAG_ITEM, false, context);
4540 eat_whitespace(p, os, context, true);
4544 if (t.cs() == "printindex" || t.cs() == "printsubindex") {
4545 context.check_layout(os);
4546 string commandname = t.cs();
4548 if (p.next_token().asInput() == "*") {
4553 begin_command_inset(os, "index_print", commandname);
4554 string const indexname = p.getArg('[', ']');
4556 if (indexname.empty())
4557 os << "type \"idx\"\n";
4559 os << "type \"" << indexname << "\"\n";
4560 os << "literal \"true\"\n";
4563 skip_spaces_braces(p);
4564 preamble.registerAutomaticallyLoadedPackage("makeidx");
4565 if (preamble.use_indices() == "true")
4566 preamble.registerAutomaticallyLoadedPackage("splitidx");
4570 if (t.cs() == "printnomenclature") {
4572 string width_type = "";
4573 context.check_layout(os);
4574 begin_command_inset(os, "nomencl_print", "printnomenclature");
4575 // case of a custom width
4577 width = p.getArg('[', ']');
4578 width = translate_len(width);
4579 width_type = "custom";
4581 // case of no custom width
4582 // the case of no custom width but the width set
4583 // via \settowidth{\nomlabelwidth}{***} cannot be supported
4584 // because the user could have set anything, not only the width
4585 // of the longest label (which would be width_type = "auto")
4586 string label = convert_literate_command_inset_arg(p.getArg('{', '}'));
4587 if (label.empty() && width_type.empty())
4588 width_type = "none";
4589 os << "set_width \"" << width_type << "\"\n";
4590 if (width_type == "custom")
4591 os << "width \"" << width << '\"';
4593 skip_spaces_braces(p);
4594 preamble.registerAutomaticallyLoadedPackage("nomencl");
4598 if ((t.cs() == "textsuperscript" || t.cs() == "textsubscript")) {
4599 context.check_layout(os);
4600 begin_inset(os, "script ");
4601 os << t.cs().substr(4) << '\n';
4602 newinsetlayout = findInsetLayout(context.textclass, t.cs(), true);
4603 parse_text_in_inset(p, os, FLAG_ITEM, false, context, newinsetlayout);
4605 if (t.cs() == "textsubscript")
4606 preamble.registerAutomaticallyLoadedPackage("subscript");
4610 if ((where = is_known(t.cs(), known_quotes))) {
4611 context.check_layout(os);
4612 begin_inset(os, "Quotes ");
4613 string quotetype = known_coded_quotes[where - known_quotes];
4614 // try to make a smart guess about the side
4615 Token const prev = p.prev_token();
4616 bool const opening = (prev.cat() != catSpace && prev.character() != 0
4617 && prev.character() != '\n' && prev.character() != '~');
4618 quotetype = guessQuoteStyle(quotetype, opening);
4621 // LyX adds {} after the quote, so we have to eat
4622 // spaces here if there are any before a possible
4624 eat_whitespace(p, os, context, false);
4629 if ((where = is_known(t.cs(), known_sizes)) &&
4630 context.new_layout_allowed) {
4631 context.check_layout(os);
4632 TeXFont const oldFont = context.font;
4633 context.font.size = known_coded_sizes[where - known_sizes];
4634 output_font_change(os, oldFont, context.font);
4635 eat_whitespace(p, os, context, false);
4639 if ((where = is_known(t.cs(), known_font_families)) &&
4640 context.new_layout_allowed) {
4641 context.check_layout(os);
4642 TeXFont const oldFont = context.font;
4643 context.font.family =
4644 known_coded_font_families[where - known_font_families];
4645 output_font_change(os, oldFont, context.font);
4646 eat_whitespace(p, os, context, false);
4650 if ((where = is_known(t.cs(), known_font_series)) &&
4651 context.new_layout_allowed) {
4652 context.check_layout(os);
4653 TeXFont const oldFont = context.font;
4654 context.font.series =
4655 known_coded_font_series[where - known_font_series];
4656 output_font_change(os, oldFont, context.font);
4657 eat_whitespace(p, os, context, false);
4661 if ((where = is_known(t.cs(), known_font_shapes)) &&
4662 context.new_layout_allowed) {
4663 context.check_layout(os);
4664 TeXFont const oldFont = context.font;
4665 context.font.shape =
4666 known_coded_font_shapes[where - known_font_shapes];
4667 output_font_change(os, oldFont, context.font);
4668 eat_whitespace(p, os, context, false);
4671 if ((where = is_known(t.cs(), known_old_font_families)) &&
4672 context.new_layout_allowed) {
4673 context.check_layout(os);
4674 TeXFont const oldFont = context.font;
4675 context.font.init();
4676 context.font.size = oldFont.size;
4677 context.font.family =
4678 known_coded_font_families[where - known_old_font_families];
4679 output_font_change(os, oldFont, context.font);
4680 eat_whitespace(p, os, context, false);
4684 if ((where = is_known(t.cs(), known_old_font_series)) &&
4685 context.new_layout_allowed) {
4686 context.check_layout(os);
4687 TeXFont const oldFont = context.font;
4688 context.font.init();
4689 context.font.size = oldFont.size;
4690 context.font.series =
4691 known_coded_font_series[where - known_old_font_series];
4692 output_font_change(os, oldFont, context.font);
4693 eat_whitespace(p, os, context, false);
4697 if ((where = is_known(t.cs(), known_old_font_shapes)) &&
4698 context.new_layout_allowed) {
4699 context.check_layout(os);
4700 TeXFont const oldFont = context.font;
4701 context.font.init();
4702 context.font.size = oldFont.size;
4703 context.font.shape =
4704 known_coded_font_shapes[where - known_old_font_shapes];
4705 output_font_change(os, oldFont, context.font);
4706 eat_whitespace(p, os, context, false);
4710 if (t.cs() == "selectlanguage") {
4711 context.check_layout(os);
4712 // save the language for the case that a
4713 // \foreignlanguage is used
4714 context.font.language = babel2lyx(p.verbatim_item());
4715 os << "\n\\lang " << context.font.language << "\n";
4719 if (t.cs() == "foreignlanguage") {
4720 string const lang = babel2lyx(p.verbatim_item());
4721 parse_text_attributes(p, os, FLAG_ITEM, outer,
4723 context.font.language, lang);
4727 if (prefixIs(t.cs(), "text") && preamble.usePolyglossia()
4728 && is_known(t.cs().substr(4), preamble.polyglossia_languages)) {
4729 // scheme is \textLANGUAGE{text} where LANGUAGE is in polyglossia_languages[]
4731 // We have to output the whole command if it has an option
4732 // because LyX doesn't support this yet, see bug #8214,
4733 // only if there is a single option specifying a variant, we can handle it.
4735 string langopts = p.getOpt();
4736 // check if the option contains a variant, if yes, extract it
4737 string::size_type pos_var = langopts.find("variant");
4738 string::size_type i = langopts.find(',');
4739 string::size_type k = langopts.find('=', pos_var);
4740 if (pos_var != string::npos && i == string::npos) {
4742 variant = langopts.substr(k + 1, langopts.length() - k - 2);
4743 lang = preamble.polyglossia2lyx(variant);
4744 parse_text_attributes(p, os, FLAG_ITEM, outer,
4746 context.font.language, lang);
4748 output_ert_inset(os, t.asInput() + langopts, context);
4750 lang = preamble.polyglossia2lyx(t.cs().substr(4, string::npos));
4751 parse_text_attributes(p, os, FLAG_ITEM, outer,
4753 context.font.language, lang);
4758 if (t.cs() == "inputencoding") {
4759 // nothing to write here
4760 string const enc = subst(p.verbatim_item(), "\n", " ");
4761 p.setEncoding(enc, Encoding::inputenc);
4765 if (is_known(t.cs(), known_special_chars) ||
4766 (t.cs() == "protect" &&
4767 p.next_token().cat() == catEscape &&
4768 is_known(p.next_token().cs(), known_special_protect_chars))) {
4769 // LyX sometimes puts a \protect in front, so we have to ignore it
4771 t.cs() == "protect" ? p.get_token().cs() : t.cs(),
4772 known_special_chars);
4773 context.check_layout(os);
4774 os << known_coded_special_chars[where - known_special_chars];
4775 skip_spaces_braces(p);
4779 if ((t.cs() == "nobreakdash" && p.next_token().asInput() == "-") ||
4780 (t.cs() == "protect" && p.next_token().asInput() == "\\nobreakdash" &&
4781 p.next_next_token().asInput() == "-") ||
4782 (t.cs() == "@" && p.next_token().asInput() == ".")) {
4783 // LyX sometimes puts a \protect in front, so we have to ignore it
4784 if (t.cs() == "protect")
4786 context.check_layout(os);
4787 if (t.cs() == "nobreakdash")
4788 os << "\\SpecialChar nobreakdash\n";
4790 os << "\\SpecialChar endofsentence\n";
4795 if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#"
4796 || t.cs() == "$" || t.cs() == "{" || t.cs() == "}"
4797 || t.cs() == "%" || t.cs() == "-") {
4798 context.check_layout(os);
4800 os << "\\SpecialChar softhyphen\n";
4806 if (t.cs() == "char") {
4807 context.check_layout(os);
4808 if (p.next_token().character() == '`') {
4810 if (p.next_token().cs() == "\"") {
4815 output_ert_inset(os, "\\char`", context);
4818 output_ert_inset(os, "\\char", context);
4823 if (t.cs() == "verb") {
4824 context.check_layout(os);
4825 // set catcodes to verbatim early, just in case.
4826 p.setCatcodes(VERBATIM_CATCODES);
4827 string delim = p.get_token().asInput();
4828 Parser::Arg arg = p.verbatimStuff(delim);
4830 output_ert_inset(os, "\\verb" + delim
4831 + arg.second + delim, context);
4833 cerr << "invalid \\verb command. Skipping" << endl;
4837 // Problem: \= creates a tabstop inside the tabbing environment
4838 // and else an accent. In the latter case we really would want
4839 // \={o} instead of \= o.
4840 if (t.cs() == "=" && (flags & FLAG_TABBING)) {
4841 output_ert_inset(os, t.asInput(), context);
4845 if (t.cs() == "\\") {
4846 context.check_layout(os);
4848 output_ert_inset(os, "\\\\" + p.getOpt(), context);
4849 else if (p.next_token().asInput() == "*") {
4851 // getOpt() eats the following space if there
4852 // is no optional argument, but that is OK
4853 // here since it has no effect in the output.
4854 output_ert_inset(os, "\\\\*" + p.getOpt(), context);
4857 begin_inset(os, "Newline newline");
4863 if (t.cs() == "newline" ||
4864 (t.cs() == "linebreak" && !p.hasOpt())) {
4865 context.check_layout(os);
4866 begin_inset(os, "Newline ");
4869 skip_spaces_braces(p);
4873 if (t.cs() == "input" || t.cs() == "include"
4874 || t.cs() == "verbatiminput"
4875 || t.cs() == "lstinputlisting"
4876 || t.cs() == "inputminted") {
4877 string name = t.cs();
4878 if (name == "verbatiminput"
4879 && p.next_token().asInput() == "*")
4880 name += p.get_token().asInput();
4881 context.check_layout(os);
4883 if (name == "lstinputlisting" && p.hasOpt()) {
4884 lstparams = p.getArg('[', ']');
4885 lstparams = subst(lstparams, "\n", " ");
4886 } else if (name == "inputminted") {
4887 name = "lstinputlisting";
4888 string const lang = p.getArg('{', '}');
4889 if (lang != "tex") {
4890 string cmd = "\\inputminted{" + lang + "}{";
4891 cmd += p.getArg('{', '}') + "}";
4892 output_ert_inset(os, cmd, context);
4895 if (prefixIs(minted_nonfloat_caption, "[t]")) {
4896 minted_nonfloat_caption.erase(0,3);
4897 // extract label and caption from the already produced LyX code
4898 vector<string> nfc = getVectorFromString(minted_nonfloat_caption, "\n");
4899 string const caption = nfc.front();
4901 vector<string>::iterator it =
4902 find(nfc.begin(), nfc.end(), "LatexCommand label");
4903 if (it != nfc.end()) {
4905 if (it != nfc.end())
4907 label = support::split(label, '"');
4910 minted_nonfloat_caption.clear();
4911 lstparams = "caption=" + caption;
4913 lstparams += ",label=" + label;
4914 lstparams = subst(lstparams, "\n", " ");
4917 string filename(normalize_filename(p.getArg('{', '}')));
4918 string const path = getMasterFilePath(true);
4919 // We want to preserve relative / absolute filenames,
4920 // therefore path is only used for testing
4921 if ((t.cs() == "include" || t.cs() == "input") &&
4922 !makeAbsPath(filename, path).exists()) {
4923 // The file extension is probably missing.
4924 // Now try to find it out.
4925 string const tex_name =
4926 find_file(filename, path,
4927 known_tex_extensions);
4928 if (!tex_name.empty())
4929 filename = tex_name;
4931 bool external = false;
4933 if (makeAbsPath(filename, path).exists()) {
4934 string const abstexname =
4935 makeAbsPath(filename, path).absFileName();
4936 string const absfigname =
4937 changeExtension(abstexname, ".fig");
4938 fix_child_filename(filename);
4939 string const lyxname = changeExtension(filename,
4940 roundtripMode() ? ".lyx.lyx" : ".lyx");
4941 string const abslyxname = makeAbsPath(
4942 lyxname, getParentFilePath(false)).absFileName();
4944 if (!skipChildren())
4945 external = FileName(absfigname).exists();
4946 if (t.cs() == "input" && !skipChildren()) {
4947 string const ext = getExtension(abstexname);
4949 // Combined PS/LaTeX:
4950 // x.eps, x.pstex_t (old xfig)
4951 // x.pstex, x.pstex_t (new xfig, e.g. 3.2.5)
4952 FileName const absepsname(
4953 changeExtension(abstexname, ".eps"));
4954 FileName const abspstexname(
4955 changeExtension(abstexname, ".pstex"));
4956 bool const xfigeps =
4957 (absepsname.exists() ||
4958 abspstexname.exists()) &&
4961 // Combined PDF/LaTeX:
4962 // x.pdf, x.pdftex_t (old xfig)
4963 // x.pdf, x.pdf_t (new xfig, e.g. 3.2.5)
4964 FileName const abspdfname(
4965 changeExtension(abstexname, ".pdf"));
4966 bool const xfigpdf =
4967 abspdfname.exists() &&
4968 (ext == "pdftex_t" || ext == "pdf_t");
4972 // Combined PS/PDF/LaTeX:
4973 // x_pspdftex.eps, x_pspdftex.pdf, x.pspdftex
4974 string const absbase2(
4975 removeExtension(abstexname) + "_pspdftex");
4976 FileName const abseps2name(
4977 addExtension(absbase2, ".eps"));
4978 FileName const abspdf2name(
4979 addExtension(absbase2, ".pdf"));
4980 bool const xfigboth =
4981 abspdf2name.exists() &&
4982 abseps2name.exists() && ext == "pspdftex";
4984 xfig = xfigpdf || xfigeps || xfigboth;
4985 external = external && xfig;
4988 outname = changeExtension(filename, ".fig");
4989 FileName abssrc(changeExtension(abstexname, ".fig"));
4990 copy_file(abssrc, outname);
4992 // Don't try to convert, the result
4993 // would be full of ERT.
4995 FileName abssrc(abstexname);
4996 copy_file(abssrc, outname);
4997 } else if (t.cs() != "verbatiminput" &&
4999 tex2lyx(abstexname, FileName(abslyxname),
5002 // no need to call copy_file
5003 // tex2lyx creates the file
5006 FileName abssrc(abstexname);
5007 copy_file(abssrc, outname);
5010 cerr << "Warning: Could not find included file '"
5011 << filename << "'." << endl;
5015 begin_inset(os, "External\n");
5016 os << "\ttemplate XFig\n"
5017 << "\tfilename " << outname << '\n';
5018 registerExternalTemplatePackages("XFig");
5020 begin_command_inset(os, "include", name);
5021 outname = subst(outname, "\"", "\\\"");
5022 os << "preview false\n"
5023 "filename \"" << outname << "\"\n";
5024 if (!lstparams.empty())
5025 os << "lstparams \"" << lstparams << "\"\n";
5026 if (t.cs() == "verbatiminput")
5027 preamble.registerAutomaticallyLoadedPackage("verbatim");
5033 if (t.cs() == "bibliographystyle") {
5034 // store new bibliographystyle
5035 bibliographystyle = p.verbatim_item();
5036 // If any other command than \bibliography, \addcontentsline
5037 // and \nocite{*} follows, we need to output the style
5038 // (because it might be used by that command).
5039 // Otherwise, it will automatically be output by LyX.
5042 for (Token t2 = p.get_token(); p.good(); t2 = p.get_token()) {
5043 if (t2.cat() == catBegin)
5045 if (t2.cat() != catEscape)
5047 if (t2.cs() == "nocite") {
5048 if (p.getArg('{', '}') == "*")
5050 } else if (t2.cs() == "bibliography")
5052 else if (t2.cs() == "phantomsection") {
5056 else if (t2.cs() == "addcontentsline") {
5057 // get the 3 arguments of \addcontentsline
5060 contentslineContent = p.getArg('{', '}');
5061 // if the last argument is not \refname we must output
5062 if (contentslineContent == "\\refname")
5069 output_ert_inset(os,
5070 "\\bibliographystyle{" + bibliographystyle + '}',
5076 if (t.cs() == "phantomsection") {
5077 // we only support this if it occurs between
5078 // \bibliographystyle and \bibliography
5079 if (bibliographystyle.empty())
5080 output_ert_inset(os, "\\phantomsection", context);
5084 if (t.cs() == "addcontentsline") {
5085 context.check_layout(os);
5086 // get the 3 arguments of \addcontentsline
5087 string const one = p.getArg('{', '}');
5088 string const two = p.getArg('{', '}');
5089 string const three = p.getArg('{', '}');
5090 // only if it is a \refname, we support if for the bibtex inset
5091 if (contentslineContent != "\\refname") {
5092 output_ert_inset(os,
5093 "\\addcontentsline{" + one + "}{" + two + "}{"+ three + '}',
5099 else if (t.cs() == "bibliography") {
5100 context.check_layout(os);
5102 begin_command_inset(os, "bibtex", "bibtex");
5103 if (!btprint.empty()) {
5104 os << "btprint " << '"' << "btPrintAll" << '"' << "\n";
5105 // clear the string because the next BibTeX inset can be without the
5106 // \nocite{*} option
5109 os << "bibfiles " << '"' << normalize_filename(p.verbatim_item()) << '"' << "\n";
5110 // Do we have addcontentsline?
5111 if (contentslineContent == "\\refname") {
5112 BibOpts = "bibtotoc";
5113 // clear string because next BibTeX inset can be without addcontentsline
5114 contentslineContent.clear();
5116 // Do we have a bibliographystyle set?
5117 if (!bibliographystyle.empty()) {
5118 if (BibOpts.empty())
5119 BibOpts = normalize_filename(bibliographystyle);
5121 BibOpts = BibOpts + ',' + normalize_filename(bibliographystyle);
5122 // clear it because each bibtex entry has its style
5123 // and we need an empty string to handle \phantomsection
5124 bibliographystyle.clear();
5126 os << "options " << '"' << BibOpts << '"' << "\n";
5131 if (t.cs() == "printbibliography") {
5132 context.check_layout(os);
5134 string bbloptions = p.hasOpt() ? p.getArg('[', ']') : string();
5135 vector<string> opts = getVectorFromString(bbloptions);
5136 vector<string>::iterator it =
5137 find(opts.begin(), opts.end(), "heading=bibintoc");
5138 if (it != opts.end()) {
5140 BibOpts = "bibtotoc";
5142 bbloptions = getStringFromVector(opts);
5143 begin_command_inset(os, "bibtex", "bibtex");
5144 if (!btprint.empty()) {
5145 os << "btprint " << '"' << "btPrintAll" << '"' << "\n";
5146 // clear the string because the next BibTeX inset can be without the
5147 // \nocite{*} option
5151 for (auto const & bf : preamble.biblatex_bibliographies) {
5152 if (!bibfiles.empty())
5154 bibfiles += normalize_filename(bf);
5156 if (!bibfiles.empty())
5157 os << "bibfiles " << '"' << bibfiles << '"' << "\n";
5158 // Do we have addcontentsline?
5159 if (contentslineContent == "\\refname") {
5160 BibOpts = "bibtotoc";
5161 // clear string because next BibTeX inset can be without addcontentsline
5162 contentslineContent.clear();
5164 os << "options " << '"' << BibOpts << '"' << "\n";
5165 if (!bbloptions.empty())
5166 os << "biblatexopts " << '"' << bbloptions << '"' << "\n";
5168 need_commentbib = false;
5172 if (t.cs() == "bibbysection") {
5173 context.check_layout(os);
5175 string bbloptions = p.hasOpt() ? p.getArg('[', ']') : string();
5176 vector<string> opts = getVectorFromString(bbloptions);
5177 vector<string>::iterator it =
5178 find(opts.begin(), opts.end(), "heading=bibintoc");
5179 if (it != opts.end()) {
5181 BibOpts = "bibtotoc";
5183 bbloptions = getStringFromVector(opts);
5184 begin_command_inset(os, "bibtex", "bibtex");
5185 os << "btprint " << '"' << "bibbysection" << '"' << "\n";
5187 for (auto const & bf : preamble.biblatex_bibliographies) {
5188 if (!bibfiles.empty())
5190 bibfiles += normalize_filename(bf);
5192 if (!bibfiles.empty())
5193 os << "bibfiles " << '"' << bibfiles << '"' << "\n";
5194 os << "options " << '"' << BibOpts << '"' << "\n";
5195 if (!bbloptions.empty())
5196 os << "biblatexopts " << '"' << bbloptions << '"' << "\n";
5198 need_commentbib = false;
5202 if (t.cs() == "parbox") {
5203 // Test whether this is an outer box of a shaded box
5205 // swallow arguments
5206 while (p.hasOpt()) {
5208 p.skip_spaces(true);
5211 p.skip_spaces(true);
5213 if (p.next_token().cat() == catBegin)
5215 p.skip_spaces(true);
5216 Token to = p.get_token();
5217 bool shaded = false;
5218 if (to.asInput() == "\\begin") {
5219 p.skip_spaces(true);
5220 if (p.getArg('{', '}') == "shaded")
5225 parse_outer_box(p, os, FLAG_ITEM, outer,
5226 context, "parbox", "shaded");
5228 parse_box(p, os, 0, FLAG_ITEM, outer, context,
5229 "", "", t.cs(), "", "");
5233 if (t.cs() == "fbox" || t.cs() == "mbox" ||
5234 t.cs() == "ovalbox" || t.cs() == "Ovalbox" ||
5235 t.cs() == "shadowbox" || t.cs() == "doublebox") {
5236 parse_outer_box(p, os, FLAG_ITEM, outer, context, t.cs(), "");
5240 if (t.cs() == "fcolorbox" || t.cs() == "colorbox") {
5241 string backgroundcolor;
5242 preamble.registerAutomaticallyLoadedPackage("xcolor");
5243 if (t.cs() == "fcolorbox") {
5244 string const framecolor = p.getArg('{', '}');
5245 backgroundcolor = p.getArg('{', '}');
5246 parse_box(p, os, 0, 0, outer, context, "", "", "", framecolor, backgroundcolor);
5248 backgroundcolor = p.getArg('{', '}');
5249 parse_box(p, os, 0, 0, outer, context, "", "", "", "", backgroundcolor);
5254 // FIXME: due to the compiler limit of "if" nestings
5255 // the code for the alignment was put here
5256 // put them in their own if if this is fixed
5257 if (t.cs() == "fboxrule" || t.cs() == "fboxsep"
5258 || t.cs() == "shadowsize"
5259 || t.cs() == "raggedleft" || t.cs() == "centering"
5260 || t.cs() == "raggedright") {
5261 if (t.cs() == "fboxrule")
5263 if (t.cs() == "fboxsep")
5265 if (t.cs() == "shadowsize")
5267 if (t.cs() != "raggedleft" && t.cs() != "centering"
5268 && t.cs() != "raggedright") {
5269 p.skip_spaces(true);
5270 while (p.good() && p.next_token().cat() != catSpace
5271 && p.next_token().cat() != catNewline
5272 && p.next_token().cat() != catEscape) {
5273 if (t.cs() == "fboxrule")
5274 fboxrule = fboxrule + p.get_token().asInput();
5275 if (t.cs() == "fboxsep")
5276 fboxsep = fboxsep + p.get_token().asInput();
5277 if (t.cs() == "shadowsize")
5278 shadow_size = shadow_size + p.get_token().asInput();
5281 output_ert_inset(os, t.asInput(), context);
5286 //\framebox() is part of the picture environment and different from \framebox{}
5287 //\framebox{} will be parsed by parse_outer_box
5288 if (t.cs() == "framebox") {
5289 if (p.next_token().character() == '(') {
5290 //the syntax is: \framebox(x,y)[position]{content}
5291 string arg = t.asInput();
5292 arg += p.getFullParentheseArg();
5293 arg += p.getFullOpt();
5294 eat_whitespace(p, os, context, false);
5295 output_ert_inset(os, arg + '{', context);
5296 parse_text(p, os, FLAG_ITEM, outer, context);
5297 output_ert_inset(os, "}", context);
5299 //the syntax is: \framebox[width][position]{content}
5300 string special = p.getFullOpt();
5301 special += p.getOpt();
5302 parse_outer_box(p, os, FLAG_ITEM, outer,
5303 context, t.cs(), special);
5308 //\makebox() is part of the picture environment and different from \makebox{}
5309 //\makebox{} will be parsed by parse_box
5310 if (t.cs() == "makebox") {
5311 if (p.next_token().character() == '(') {
5312 //the syntax is: \makebox(x,y)[position]{content}
5313 string arg = t.asInput();
5314 arg += p.getFullParentheseArg();
5315 arg += p.getFullOpt();
5316 eat_whitespace(p, os, context, false);
5317 output_ert_inset(os, arg + '{', context);
5318 parse_text(p, os, FLAG_ITEM, outer, context);
5319 output_ert_inset(os, "}", context);
5321 //the syntax is: \makebox[width][position]{content}
5322 parse_box(p, os, 0, FLAG_ITEM, outer, context,
5323 "", "", t.cs(), "", "");
5327 if (t.cs() == "smallskip" ||
5328 t.cs() == "medskip" ||
5329 t.cs() == "bigskip" ||
5330 t.cs() == "vfill") {
5331 context.check_layout(os);
5332 begin_inset(os, "VSpace ");
5335 skip_spaces_braces(p);
5339 if ((where = is_known(t.cs(), known_spaces))) {
5340 context.check_layout(os);
5341 begin_inset(os, "space ");
5342 os << '\\' << known_coded_spaces[where - known_spaces]
5345 // LaTeX swallows whitespace after all spaces except
5346 // "\\,". We have to do that here, too, because LyX
5347 // adds "{}" which would make the spaces significant.
5349 eat_whitespace(p, os, context, false);
5350 // LyX adds "{}" after all spaces except "\\ " and
5351 // "\\,", so we have to remove "{}".
5352 // "\\,{}" is equivalent to "\\," in LaTeX, so we
5353 // remove the braces after "\\,", too.
5359 if (t.cs() == "newpage" ||
5360 (t.cs() == "pagebreak" && !p.hasOpt()) ||
5361 t.cs() == "clearpage" ||
5362 t.cs() == "cleardoublepage") {
5363 context.check_layout(os);
5364 begin_inset(os, "Newpage ");
5367 skip_spaces_braces(p);
5371 if (t.cs() == "DeclareRobustCommand" ||
5372 t.cs() == "DeclareRobustCommandx" ||
5373 t.cs() == "newcommand" ||
5374 t.cs() == "newcommandx" ||
5375 t.cs() == "providecommand" ||
5376 t.cs() == "providecommandx" ||
5377 t.cs() == "renewcommand" ||
5378 t.cs() == "renewcommandx") {
5379 // DeclareRobustCommand, DeclareRobustCommandx,
5380 // providecommand and providecommandx could be handled
5381 // by parse_command(), but we need to call
5382 // add_known_command() here.
5383 string name = t.asInput();
5384 if (p.next_token().asInput() == "*") {
5385 // Starred form. Eat '*'
5389 string const command = p.verbatim_item();
5390 string const opt1 = p.getFullOpt();
5391 string const opt2 = p.getFullOpt();
5392 add_known_command(command, opt1, !opt2.empty());
5393 string const ert = name + '{' + command + '}' +
5395 '{' + p.verbatim_item() + '}';
5397 if (t.cs() == "DeclareRobustCommand" ||
5398 t.cs() == "DeclareRobustCommandx" ||
5399 t.cs() == "providecommand" ||
5400 t.cs() == "providecommandx" ||
5401 name[name.length()-1] == '*')
5402 output_ert_inset(os, ert, context);
5404 context.check_layout(os);
5405 begin_inset(os, "FormulaMacro");
5412 if (t.cs() == "let" && p.next_token().asInput() != "*") {
5413 // let could be handled by parse_command(),
5414 // but we need to call add_known_command() here.
5415 string ert = t.asInput();
5418 if (p.next_token().cat() == catBegin) {
5419 name = p.verbatim_item();
5420 ert += '{' + name + '}';
5422 name = p.verbatim_item();
5427 if (p.next_token().cat() == catBegin) {
5428 command = p.verbatim_item();
5429 ert += '{' + command + '}';
5431 command = p.verbatim_item();
5434 // If command is known, make name known too, to parse
5435 // its arguments correctly. For this reason we also
5436 // have commands in syntax.default that are hardcoded.
5437 CommandMap::iterator it = known_commands.find(command);
5438 if (it != known_commands.end())
5439 known_commands[t.asInput()] = it->second;
5440 output_ert_inset(os, ert, context);
5444 if (t.cs() == "hspace" || t.cs() == "vspace") {
5447 string name = t.asInput();
5448 string const length = p.verbatim_item();
5451 bool valid = splitLatexLength(length, valstring, unit);
5452 bool known_hspace = false;
5453 bool known_vspace = false;
5454 bool known_unit = false;
5457 istringstream iss(valstring);
5460 if (t.cs()[0] == 'h') {
5461 if (unit == "\\fill") {
5466 known_hspace = true;
5469 if (unit == "\\smallskipamount") {
5471 known_vspace = true;
5472 } else if (unit == "\\medskipamount") {
5474 known_vspace = true;
5475 } else if (unit == "\\bigskipamount") {
5477 known_vspace = true;
5478 } else if (unit == "\\fill") {
5480 known_vspace = true;
5484 if (!known_hspace && !known_vspace) {
5485 switch (unitFromString(unit)) {
5501 //unitFromString(unit) fails for relative units like Length::PCW
5502 // therefore handle them separately
5503 if (unit == "\\paperwidth" || unit == "\\columnwidth"
5504 || unit == "\\textwidth" || unit == "\\linewidth"
5505 || unit == "\\textheight" || unit == "\\paperheight"
5506 || unit == "\\baselineskip")
5514 // check for glue lengths
5515 bool is_gluelength = false;
5516 string gluelength = length;
5517 string::size_type i = length.find(" minus");
5518 if (i == string::npos) {
5519 i = length.find(" plus");
5520 if (i != string::npos)
5521 is_gluelength = true;
5523 is_gluelength = true;
5524 // if yes transform "9xx minus 8yy plus 7zz"
5526 if (is_gluelength) {
5527 i = gluelength.find(" minus");
5528 if (i != string::npos)
5529 gluelength.replace(i, 7, "-");
5530 i = gluelength.find(" plus");
5531 if (i != string::npos)
5532 gluelength.replace(i, 6, "+");
5535 if (t.cs()[0] == 'h' && (known_unit || known_hspace || is_gluelength)) {
5536 // Literal horizontal length or known variable
5537 context.check_layout(os);
5538 begin_inset(os, "space ");
5546 if (known_unit && !known_hspace)
5547 os << "\n\\length " << translate_len(length);
5549 os << "\n\\length " << gluelength;
5551 } else if (known_unit || known_vspace || is_gluelength) {
5552 // Literal vertical length or known variable
5553 context.check_layout(os);
5554 begin_inset(os, "VSpace ");
5557 if (known_unit && !known_vspace)
5558 os << translate_len(length);
5565 // LyX can't handle other length variables in Inset VSpace/space
5570 output_ert_inset(os, name + '{' + unit + '}', context);
5571 else if (value == -1.0)
5572 output_ert_inset(os, name + "{-" + unit + '}', context);
5574 output_ert_inset(os, name + '{' + valstring + unit + '}', context);
5576 output_ert_inset(os, name + '{' + length + '}', context);
5581 // Before we look for the layout name alone below, we check the layouts including the LateXParam, which
5582 // might be one or several options or a star.
5583 // The single '=' is meant here.
5584 if ((newinsetlayout = findInsetLayout(context.textclass, starredname, true, p.getCommandLatexParam()))) {
5588 context.check_layout(os);
5589 // store the latexparam here. This is eaten in parse_text_in_inset
5590 context.latexparam = newinsetlayout->latexparam();
5591 docstring name = newinsetlayout->name();
5592 bool const caption = name.find(from_ascii("Caption:")) == 0;
5594 // Already done for floating minted listings.
5595 if (minted_float.empty()) {
5596 begin_inset(os, "Caption ");
5597 os << to_utf8(name.substr(8)) << '\n';
5600 // FIXME: what do we do if the prefix is not Flex: ?
5601 if (prefixIs(name, from_ascii("Flex:")))
5603 begin_inset(os, "Flex ");
5604 os << to_utf8(name) << '\n'
5605 << "status collapsed\n";
5607 if (!minted_float.empty()) {
5608 parse_text_snippet(p, os, FLAG_ITEM, false, context);
5609 } else if (newinsetlayout->isPassThru()) {
5610 // set catcodes to verbatim early, just in case.
5611 p.setCatcodes(VERBATIM_CATCODES);
5612 string delim = p.get_token().asInput();
5614 cerr << "Warning: bad delimiter for command " << t.asInput() << endl;
5615 //FIXME: handle error condition
5616 string const arg = p.verbatimStuff("}").second;
5617 Context newcontext(true, context.textclass);
5618 if (newinsetlayout->forcePlainLayout())
5619 newcontext.layout = &context.textclass.plainLayout();
5620 output_ert(os, arg, newcontext);
5622 parse_text_in_inset(p, os, FLAG_ITEM, false, context, newinsetlayout);
5623 context.latexparam.clear();
5626 // Minted caption insets are not closed here because
5627 // we collect everything into the caption.
5628 if (minted_float.empty())
5633 // The single '=' is meant here.
5634 if ((newinsetlayout = findInsetLayout(context.textclass, starredname, true))) {
5638 context.check_layout(os);
5639 docstring const name = newinsetlayout->name();
5640 bool const caption = name.find(from_ascii("Caption:")) == 0;
5642 // Already done for floating minted listings.
5643 if (minted_float.empty()) {
5644 begin_inset(os, "Caption ");
5645 os << to_utf8(name.substr(8)) << '\n';
5648 begin_inset(os, "Flex ");
5649 os << to_utf8(name) << '\n'
5650 << "status collapsed\n";
5652 if (!minted_float.empty()) {
5653 parse_text_snippet(p, os, FLAG_ITEM, false, context);
5654 } else if (newinsetlayout->isPassThru()) {
5655 // set catcodes to verbatim early, just in case.
5656 p.setCatcodes(VERBATIM_CATCODES);
5657 string delim = p.get_token().asInput();
5659 cerr << "Warning: bad delimiter for command " << t.asInput() << endl;
5660 //FIXME: handle error condition
5661 string const arg = p.verbatimStuff("}").second;
5662 Context newcontext(true, context.textclass);
5663 if (newinsetlayout->forcePlainLayout())
5664 newcontext.layout = &context.textclass.plainLayout();
5665 output_ert(os, arg, newcontext);
5667 parse_text_in_inset(p, os, FLAG_ITEM, false, context, newinsetlayout);
5670 // Minted caption insets are not closed here because
5671 // we collect everything into the caption.
5672 if (minted_float.empty())
5677 if (t.cs() == "includepdf") {
5679 string const arg = p.getArg('[', ']');
5680 map<string, string> opts;
5681 vector<string> keys;
5682 split_map(arg, opts, keys);
5683 string name = normalize_filename(p.verbatim_item());
5684 string const path = getMasterFilePath(true);
5685 // We want to preserve relative / absolute filenames,
5686 // therefore path is only used for testing
5687 if (!makeAbsPath(name, path).exists()) {
5688 // The file extension is probably missing.
5689 // Now try to find it out.
5690 char const * const pdfpages_format[] = {"pdf", 0};
5691 string const pdftex_name =
5692 find_file(name, path, pdfpages_format);
5693 if (!pdftex_name.empty()) {
5698 FileName const absname = makeAbsPath(name, path);
5699 if (absname.exists())
5701 fix_child_filename(name);
5702 copy_file(absname, name);
5704 cerr << "Warning: Could not find file '"
5705 << name << "'." << endl;
5707 context.check_layout(os);
5708 begin_inset(os, "External\n\ttemplate ");
5709 os << "PDFPages\n\tfilename "
5711 // parse the options
5712 if (opts.find("pages") != opts.end())
5713 os << "\textra LaTeX \"pages="
5714 << opts["pages"] << "\"\n";
5715 if (opts.find("angle") != opts.end())
5716 os << "\trotateAngle "
5717 << opts["angle"] << '\n';
5718 if (opts.find("origin") != opts.end()) {
5720 string const opt = opts["origin"];
5721 if (opt == "tl") ss << "topleft";
5722 if (opt == "bl") ss << "bottomleft";
5723 if (opt == "Bl") ss << "baselineleft";
5724 if (opt == "c") ss << "center";
5725 if (opt == "tc") ss << "topcenter";
5726 if (opt == "bc") ss << "bottomcenter";
5727 if (opt == "Bc") ss << "baselinecenter";
5728 if (opt == "tr") ss << "topright";
5729 if (opt == "br") ss << "bottomright";
5730 if (opt == "Br") ss << "baselineright";
5731 if (!ss.str().empty())
5732 os << "\trotateOrigin " << ss.str() << '\n';
5734 cerr << "Warning: Ignoring unknown includegraphics origin argument '" << opt << "'\n";
5736 if (opts.find("width") != opts.end())
5738 << translate_len(opts["width"]) << '\n';
5739 if (opts.find("height") != opts.end())
5741 << translate_len(opts["height"]) << '\n';
5742 if (opts.find("keepaspectratio") != opts.end())
5743 os << "\tkeepAspectRatio\n";
5745 context.check_layout(os);
5746 registerExternalTemplatePackages("PDFPages");
5750 if (t.cs() == "loadgame") {
5752 string name = normalize_filename(p.verbatim_item());
5753 string const path = getMasterFilePath(true);
5754 // We want to preserve relative / absolute filenames,
5755 // therefore path is only used for testing
5756 if (!makeAbsPath(name, path).exists()) {
5757 // The file extension is probably missing.
5758 // Now try to find it out.
5759 char const * const lyxskak_format[] = {"fen", 0};
5760 string const lyxskak_name =
5761 find_file(name, path, lyxskak_format);
5762 if (!lyxskak_name.empty())
5763 name = lyxskak_name;
5765 FileName const absname = makeAbsPath(name, path);
5766 if (absname.exists())
5768 fix_child_filename(name);
5769 copy_file(absname, name);
5771 cerr << "Warning: Could not find file '"
5772 << name << "'." << endl;
5773 context.check_layout(os);
5774 begin_inset(os, "External\n\ttemplate ");
5775 os << "ChessDiagram\n\tfilename "
5778 context.check_layout(os);
5779 // after a \loadgame follows a \showboard
5780 if (p.get_token().asInput() == "showboard")
5782 registerExternalTemplatePackages("ChessDiagram");
5786 // try to see whether the string is in unicodesymbols
5787 // Only use text mode commands, since we are in text mode here,
5788 // and math commands may be invalid (bug 6797)
5789 string name = t.asInput();
5790 // handle the dingbats, cyrillic and greek
5791 if (name == "\\ding" || name == "\\textcyr" ||
5792 (name == "\\textgreek" && !preamble.usePolyglossia()))
5793 name = name + '{' + p.getArg('{', '}') + '}';
5794 // handle the ifsym characters
5795 else if (name == "\\textifsymbol") {
5796 string const optif = p.getFullOpt();
5797 string const argif = p.getArg('{', '}');
5798 name = name + optif + '{' + argif + '}';
5800 // handle the \ascii characters
5801 // the case of \ascii within braces, as LyX outputs it, is already
5802 // handled for t.cat() == catBegin
5803 else if (name == "\\ascii") {
5804 // the code is "\asci\xxx"
5805 name = "{" + name + p.get_token().asInput() + "}";
5808 // handle some TIPA special characters
5809 else if (preamble.isPackageUsed("tipa")) {
5810 if (name == "\\s") {
5811 // fromLaTeXCommand() does not yet
5812 // recognize tipa short cuts
5813 name = "\\textsyllabic";
5814 } else if (name == "\\=" &&
5815 p.next_token().asInput() == "*") {
5816 // fromLaTeXCommand() does not yet
5817 // recognize tipa short cuts
5819 name = "\\textsubbar";
5820 } else if (name == "\\textdoublevertline") {
5821 // FIXME: This is not correct,
5822 // \textvertline is higher than \textbardbl
5823 name = "\\textbardbl";
5825 } else if (name == "\\!" ) {
5826 if (p.next_token().asInput() == "b") {
5827 p.get_token(); // eat 'b'
5830 } else if (p.next_token().asInput() == "d") {
5834 } else if (p.next_token().asInput() == "g") {
5838 } else if (p.next_token().asInput() == "G") {
5840 name = "\\texthtscg";
5842 } else if (p.next_token().asInput() == "j") {
5844 name = "\\texthtbardotlessj";
5846 } else if (p.next_token().asInput() == "o") {
5848 name = "\\textbullseye";
5851 } else if (name == "\\*" ) {
5852 if (p.next_token().asInput() == "k") {
5854 name = "\\textturnk";
5856 } else if (p.next_token().asInput() == "r") {
5857 p.get_token(); // eat 'b'
5858 name = "\\textturnr";
5860 } else if (p.next_token().asInput() == "t") {
5862 name = "\\textturnt";
5864 } else if (p.next_token().asInput() == "w") {
5866 name = "\\textturnw";
5871 if ((name.size() == 2 &&
5872 contains("\"'.=^`bcdHkrtuv~", name[1]) &&
5873 p.next_token().asInput() != "*") ||
5874 is_known(name.substr(1), known_tipa_marks)) {
5875 // name is a command that corresponds to a
5876 // combining character in unicodesymbols.
5877 // Append the argument, fromLaTeXCommand()
5878 // will either convert it to a single
5879 // character or a combining sequence.
5880 name += '{' + p.verbatim_item() + '}';
5882 // now get the character from unicodesymbols
5886 docstring s = normalize_c(encodings.fromLaTeXCommand(from_utf8(name),
5887 Encodings::TEXT_CMD, termination, rem, &req));
5889 context.check_layout(os);
5892 output_ert_inset(os, to_utf8(rem), context);
5894 skip_spaces_braces(p);
5895 for (set<string>::const_iterator it = req.begin(); it != req.end(); ++it)
5896 preamble.registerAutomaticallyLoadedPackage(*it);
5898 //cerr << "#: " << t << " mode: " << mode << endl;
5899 // heuristic: read up to next non-nested space
5901 string s = t.asInput();
5902 string z = p.verbatim_item();
5903 while (p.good() && z != " " && !z.empty()) {
5904 //cerr << "read: " << z << endl;
5906 z = p.verbatim_item();
5908 cerr << "found ERT: " << s << endl;
5909 output_ert_inset(os, s + ' ', context);
5912 if (t.asInput() == name &&
5913 p.next_token().asInput() == "*") {
5914 // Starred commands like \vspace*{}
5915 p.get_token(); // Eat '*'
5918 if (!parse_command(name, p, os, outer, context)) {
5919 output_ert_inset(os, name, context);
5920 // Try to handle options of unknown commands:
5921 // Look if we have an optional arguments,
5922 // and if so, put the brackets in ERT.
5923 while (p.hasOpt()) {
5924 p.get_token(); // eat '['
5925 output_ert_inset(os, "[", context);
5926 os << parse_text_snippet(p, FLAG_BRACK_LAST, outer, context);
5927 output_ert_inset(os, "]", context);
5935 string guessLanguage(Parser & p, string const & lang)
5937 typedef std::map<std::string, size_t> LangMap;
5938 // map from language names to number of characters
5941 for (char const * const * i = supported_CJK_languages; *i; i++)
5942 used[string(*i)] = 0;
5945 Token const t = p.get_token();
5946 // comments are not counted for any language
5947 if (t.cat() == catComment)
5949 // commands are not counted as well, but we need to detect
5950 // \begin{CJK} and switch encoding if needed
5951 if (t.cat() == catEscape) {
5952 if (t.cs() == "inputencoding") {
5953 string const enc = subst(p.verbatim_item(), "\n", " ");
5954 p.setEncoding(enc, Encoding::inputenc);
5957 if (t.cs() != "begin")
5960 // Non-CJK content is counted for lang.
5961 // We do not care about the real language here:
5962 // If we have more non-CJK contents than CJK contents,
5963 // we simply use the language that was specified as
5964 // babel main language.
5965 used[lang] += t.asInput().length();
5968 // Now we are starting an environment
5970 string const name = p.getArg('{', '}');
5971 if (name != "CJK") {
5975 // It is a CJK environment
5977 /* name = */ p.getArg('{', '}');
5978 string const encoding = p.getArg('{', '}');
5979 /* mapping = */ p.getArg('{', '}');
5980 string const encoding_old = p.getEncoding();
5981 char const * const * const where =
5982 is_known(encoding, supported_CJK_encodings);
5984 p.setEncoding(encoding, Encoding::CJK);
5986 p.setEncoding("UTF-8");
5987 string const text = p.ertEnvironment("CJK");
5988 p.setEncoding(encoding_old);
5991 // ignore contents in unknown CJK encoding
5994 // the language of the text
5996 supported_CJK_languages[where - supported_CJK_encodings];
5997 used[cjk] += text.length();
5999 LangMap::const_iterator use = used.begin();
6000 for (LangMap::const_iterator it = used.begin(); it != used.end(); ++it) {
6001 if (it->second > use->second)
6008 void check_comment_bib(ostream & os, Context & context)
6010 if (!need_commentbib)
6012 // We have a bibliography database, but no bibliography with biblatex
6013 // which is completely valid. Insert a bibtex inset in a note.
6014 context.check_layout(os);
6015 begin_inset(os, "Note Note\n");
6016 os << "status open\n";
6017 os << "\\begin_layout Plain Layout\n";
6018 begin_command_inset(os, "bibtex", "bibtex");
6020 for (auto const & bf : preamble.biblatex_bibliographies) {
6021 if (!bibfiles.empty())
6023 bibfiles += normalize_filename(bf);
6025 if (!bibfiles.empty())
6026 os << "bibfiles " << '"' << bibfiles << '"' << "\n";
6027 end_inset(os);// Bibtex
6028 os << "\\end_layout\n";
6029 end_inset(os);// Note