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"
25 #include "support/lassert.h"
26 #include "support/convert.h"
27 #include "support/FileName.h"
28 #include "support/filetools.h"
29 #include "support/lstrings.h"
38 using namespace lyx::support;
43 void parse_text_in_inset(Parser & p, ostream & os, unsigned flags, bool outer,
44 Context const & context, InsetLayout const * layout)
46 bool const forcePlainLayout =
47 layout ? layout->forcePlainLayout() : false;
48 Context newcontext(true, context.textclass);
50 newcontext.layout = &context.textclass.plainLayout();
52 newcontext.font = context.font;
53 parse_text(p, os, flags, outer, newcontext);
54 newcontext.check_end_layout(os);
60 void parse_text_in_inset(Parser & p, ostream & os, unsigned flags, bool outer,
61 Context const & context, string const & name)
63 InsetLayout const * layout = 0;
64 DocumentClass::InsetLayouts::const_iterator it =
65 context.textclass.insetLayouts().find(from_ascii(name));
66 if (it != context.textclass.insetLayouts().end())
67 layout = &(it->second);
68 parse_text_in_inset(p, os, flags, outer, context, layout);
71 /// parses a paragraph snippet, useful for example for \\emph{...}
72 void parse_text_snippet(Parser & p, ostream & os, unsigned flags, bool outer,
75 Context newcontext(context);
76 // Don't inherit the paragraph-level extra stuff
77 newcontext.par_extra_stuff.clear();
78 parse_text(p, os, flags, outer, newcontext);
79 // Make sure that we don't create invalid .lyx files
80 context.need_layout = newcontext.need_layout;
81 context.need_end_layout = newcontext.need_end_layout;
86 * Thin wrapper around parse_text_snippet() using a string.
88 * We completely ignore \c context.need_layout and \c context.need_end_layout,
89 * because our return value is not used directly (otherwise the stream version
90 * of parse_text_snippet() could be used). That means that the caller needs
91 * to do layout management manually.
92 * This is intended to parse text that does not create any layout changes.
94 string parse_text_snippet(Parser & p, unsigned flags, const bool outer,
97 Context newcontext(context);
98 newcontext.need_layout = false;
99 newcontext.need_end_layout = false;
100 newcontext.new_layout_allowed = false;
101 // Avoid warning by Context::~Context()
102 newcontext.par_extra_stuff.clear();
104 parse_text_snippet(p, os, flags, outer, newcontext);
109 char const * const known_ref_commands[] = { "ref", "pageref", "vref",
110 "vpageref", "prettyref", "eqref", 0 };
114 * The starred forms are also known except for "citefullauthor",
115 * "citeyear" and "citeyearpar".
117 char const * const known_natbib_commands[] = { "cite", "citet", "citep",
118 "citealt", "citealp", "citeauthor", "citeyear", "citeyearpar",
119 "citefullauthor", "Citet", "Citep", "Citealt", "Citealp", "Citeauthor", 0 };
123 * No starred form other than "cite*" known.
125 char const * const known_jurabib_commands[] = { "cite", "citet", "citep",
126 "citealt", "citealp", "citeauthor", "citeyear", "citeyearpar",
127 // jurabib commands not (yet) supported by LyX:
129 // "footcite", "footcitet", "footcitep", "footcitealt", "footcitealp",
130 // "footciteauthor", "footciteyear", "footciteyearpar",
131 "citefield", "citetitle", 0 };
133 /// LaTeX names for quotes
134 char const * const known_quotes[] = { "dq", "guillemotleft", "flqq", "og",
135 "guillemotright", "frqq", "fg", "glq", "glqq", "textquoteleft", "grq", "grqq",
136 "quotedblbase", "textquotedblleft", "quotesinglbase", "textquoteright", "flq",
137 "guilsinglleft", "frq", "guilsinglright", 0};
139 /// the same as known_quotes with .lyx names
140 char const * const known_coded_quotes[] = { "prd", "ard", "ard", "ard",
141 "ald", "ald", "ald", "gls", "gld", "els", "els", "grd",
142 "gld", "grd", "gls", "ers", "fls",
143 "fls", "frs", "frs", 0};
145 /// LaTeX names for font sizes
146 char const * const known_sizes[] = { "tiny", "scriptsize", "footnotesize",
147 "small", "normalsize", "large", "Large", "LARGE", "huge", "Huge", 0};
149 /// the same as known_sizes with .lyx names
150 char const * const known_coded_sizes[] = { "tiny", "scriptsize", "footnotesize",
151 "small", "normal", "large", "larger", "largest", "huge", "giant", 0};
153 /// LaTeX 2.09 names for font families
154 char const * const known_old_font_families[] = { "rm", "sf", "tt", 0};
156 /// LaTeX names for font families
157 char const * const known_font_families[] = { "rmfamily", "sffamily",
160 /// the same as known_old_font_families and known_font_families with .lyx names
161 char const * const known_coded_font_families[] = { "roman", "sans",
164 /// LaTeX 2.09 names for font series
165 char const * const known_old_font_series[] = { "bf", 0};
167 /// LaTeX names for font series
168 char const * const known_font_series[] = { "bfseries", "mdseries", 0};
170 /// the same as known_old_font_series and known_font_series with .lyx names
171 char const * const known_coded_font_series[] = { "bold", "medium", 0};
173 /// LaTeX 2.09 names for font shapes
174 char const * const known_old_font_shapes[] = { "it", "sl", "sc", 0};
176 /// LaTeX names for font shapes
177 char const * const known_font_shapes[] = { "itshape", "slshape", "scshape",
180 /// the same as known_old_font_shapes and known_font_shapes with .lyx names
181 char const * const known_coded_font_shapes[] = { "italic", "slanted",
182 "smallcaps", "up", 0};
185 * Graphics file extensions known by the dvips driver of the graphics package.
186 * These extensions are used to complete the filename of an included
187 * graphics file if it does not contain an extension.
188 * The order must be the same that latex uses to find a file, because we
189 * will use the first extension that matches.
190 * This is only an approximation for the common cases. If we would want to
191 * do it right in all cases, we would need to know which graphics driver is
192 * used and know the extensions of every driver of the graphics package.
194 char const * const known_dvips_graphics_formats[] = {"eps", "ps", "eps.gz",
195 "ps.gz", "eps.Z", "ps.Z", 0};
198 * Graphics file extensions known by the pdftex driver of the graphics package.
199 * \sa known_dvips_graphics_formats
201 char const * const known_pdftex_graphics_formats[] = {"png", "pdf", "jpg",
205 * Known file extensions for TeX files as used by \\include.
207 char const * const known_tex_extensions[] = {"tex", 0};
209 /// spaces known by InsetSpace
210 char const * const known_spaces[] = { " ", "space", ",", "thinspace", "quad",
211 "qquad", "enspace", "enskip", "negthinspace", "hfill", "dotfill", "hrulefill",
212 "leftarrowfill", "rightarrowfill", "upbracefill", "downbracefill", 0};
214 /// the same as known_spaces with .lyx names
215 char const * const known_coded_spaces[] = { "space{}", "space{}",
216 "thinspace{}", "thinspace{}", "quad{}", "qquad{}", "enspace{}", "enskip{}",
217 "negthinspace{}", "hfill{}", "dotfill{}", "hrulefill{}", "leftarrowfill{}",
218 "rightarrowfill{}", "upbracefill{}", "downbracefill{}", 0};
220 /// These are translated by LyX to commands like "\\LyX{}", so we have to put
221 /// them in ERT. "LaTeXe" must come before "LaTeX"!
222 char const * const known_phrases[] = {"LyX", "TeX", "LaTeXe", "LaTeX", 0};
223 char const * const known_coded_phrases[] = {"LyX", "TeX", "LaTeX2e", "LaTeX", 0};
224 int const known_phrase_lengths[] = {3, 5, 7, 0};
227 /// splits "x=z, y=b" into a map and an ordered keyword vector
228 void split_map(string const & s, map<string, string> & res, vector<string> & keys)
233 keys.resize(v.size());
234 for (size_t i = 0; i < v.size(); ++i) {
235 size_t const pos = v[i].find('=');
236 string const index = trimSpaceAndEol(v[i].substr(0, pos));
237 string const value = trimSpaceAndEol(v[i].substr(pos + 1, string::npos));
245 * Split a LaTeX length into value and unit.
246 * The latter can be a real unit like "pt", or a latex length variable
247 * like "\textwidth". The unit may contain additional stuff like glue
248 * lengths, but we don't care, because such lengths are ERT anyway.
249 * \returns true if \p value and \p unit are valid.
251 bool splitLatexLength(string const & len, string & value, string & unit)
255 const string::size_type i = len.find_first_not_of(" -+0123456789.,");
256 //'4,5' is a valid LaTeX length number. Change it to '4.5'
257 string const length = subst(len, ',', '.');
258 if (i == string::npos)
261 if (len[0] == '\\') {
262 // We had something like \textwidth without a factor
268 value = trimSpaceAndEol(string(length, 0, i));
272 // 'cM' is a valid LaTeX length unit. Change it to 'cm'
273 if (contains(len, '\\'))
274 unit = trimSpaceAndEol(string(len, i));
276 unit = ascii_lowercase(trimSpaceAndEol(string(len, i)));
281 /// A simple function to translate a latex length to something LyX can
282 /// understand. Not perfect, but rather best-effort.
283 bool translate_len(string const & length, string & valstring, string & unit)
285 if (!splitLatexLength(length, valstring, unit))
287 // LyX uses percent values
289 istringstream iss(valstring);
294 string const percentval = oss.str();
296 if (unit.empty() || unit[0] != '\\')
298 string::size_type const i = unit.find(' ');
299 string const endlen = (i == string::npos) ? string() : string(unit, i);
300 if (unit == "\\textwidth") {
301 valstring = percentval;
302 unit = "text%" + endlen;
303 } else if (unit == "\\columnwidth") {
304 valstring = percentval;
305 unit = "col%" + endlen;
306 } else if (unit == "\\paperwidth") {
307 valstring = percentval;
308 unit = "page%" + endlen;
309 } else if (unit == "\\linewidth") {
310 valstring = percentval;
311 unit = "line%" + endlen;
312 } else if (unit == "\\paperheight") {
313 valstring = percentval;
314 unit = "pheight%" + endlen;
315 } else if (unit == "\\textheight") {
316 valstring = percentval;
317 unit = "theight%" + endlen;
325 string translate_len(string const & length)
329 if (translate_len(length, value, unit))
331 // If the input is invalid, return what we have.
339 * Translates a LaTeX length into \p value, \p unit and
340 * \p special parts suitable for a box inset.
341 * The difference from translate_len() is that a box inset knows about
342 * some special "units" that are stored in \p special.
344 void translate_box_len(string const & length, string & value, string & unit, string & special)
346 if (translate_len(length, value, unit)) {
347 if (unit == "\\height" || unit == "\\depth" ||
348 unit == "\\totalheight" || unit == "\\width") {
349 special = unit.substr(1);
350 // The unit is not used, but LyX requires a dummy setting
363 * Find a file with basename \p name in path \p path and an extension
366 string find_file(string const & name, string const & path,
367 char const * const * extensions)
369 for (char const * const * what = extensions; *what; ++what) {
370 string const trial = addExtension(name, *what);
371 if (makeAbsPath(trial, path).exists())
378 void begin_inset(ostream & os, string const & name)
380 os << "\n\\begin_inset " << name;
384 void begin_command_inset(ostream & os, string const & name,
385 string const & latexname)
387 begin_inset(os, "CommandInset ");
388 os << name << "\nLatexCommand " << latexname << '\n';
392 void end_inset(ostream & os)
394 os << "\n\\end_inset\n\n";
398 bool skip_braces(Parser & p)
400 if (p.next_token().cat() != catBegin)
403 if (p.next_token().cat() == catEnd) {
412 /// replace LaTeX commands in \p s from the unicodesymbols file with their
414 docstring convert_unicodesymbols(docstring s)
417 for (size_t i = 0; i < s.size();) {
424 docstring parsed = encodings.fromLaTeXCommand(s, rem,
425 Encodings::TEXT_CMD);
428 if (s.empty() || s[0] != '\\')
437 /// try to convert \p s to a valid InsetCommand argument
438 string convert_command_inset_arg(string s)
441 // since we don't know the input encoding we can't use from_utf8
442 s = to_utf8(convert_unicodesymbols(from_ascii(s)));
443 // LyX cannot handle newlines in a latex command
444 return subst(s, "\n", " ");
448 void handle_backslash(ostream & os, string const & s)
450 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
452 os << "\n\\backslash\n";
459 void handle_ert(ostream & os, string const & s, Context & context)
461 // We must have a valid layout before outputting the ERT inset.
462 context.check_layout(os);
463 Context newcontext(true, context.textclass);
464 begin_inset(os, "ERT");
465 os << "\nstatus collapsed\n";
466 newcontext.check_layout(os);
467 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
469 os << "\n\\backslash\n";
470 else if (*it == '\n') {
471 newcontext.new_paragraph(os);
472 newcontext.check_layout(os);
476 newcontext.check_end_layout(os);
481 void handle_comment(ostream & os, string const & s, Context & context)
483 // TODO: Handle this better
484 Context newcontext(true, context.textclass);
485 begin_inset(os, "ERT");
486 os << "\nstatus collapsed\n";
487 newcontext.check_layout(os);
488 handle_backslash(os, s);
489 // make sure that our comment is the last thing on the line
490 newcontext.new_paragraph(os);
491 newcontext.check_layout(os);
492 newcontext.check_end_layout(os);
497 Layout const * findLayout(TextClass const & textclass, string const & name, bool command)
499 Layout const * layout = findLayoutWithoutModule(textclass, name, command);
502 if (checkModule(name, command))
503 return findLayoutWithoutModule(textclass, name, command);
508 InsetLayout const * findInsetLayout(TextClass const & textclass, string const & name, bool command)
510 InsetLayout const * insetlayout = findInsetLayoutWithoutModule(textclass, name, command);
513 if (checkModule(name, command))
514 return findInsetLayoutWithoutModule(textclass, name, command);
519 void eat_whitespace(Parser &, ostream &, Context &, bool);
523 * Skips whitespace and braces.
524 * This should be called after a command has been parsed that is not put into
525 * ERT, and where LyX adds "{}" if needed.
527 void skip_spaces_braces(Parser & p, bool keepws = false)
529 /* The following four examples produce the same typeset output and
530 should be handled by this function:
538 // Unfortunately we need to skip comments, too.
539 // We can't use eat_whitespace since writing them after the {}
540 // results in different output in some cases.
541 bool const skipped_spaces = p.skip_spaces(true);
542 bool const skipped_braces = skip_braces(p);
543 if (keepws && skipped_spaces && !skipped_braces)
544 // put back the space (it is better handled by check_space)
545 p.unskip_spaces(true);
549 void output_command_layout(ostream & os, Parser & p, bool outer,
550 Context & parent_context,
551 Layout const * newlayout)
553 TeXFont const oldFont = parent_context.font;
554 // save the current font size
555 string const size = oldFont.size;
556 // reset the font size to default, because the font size switches
557 // don't affect section headings and the like
558 parent_context.font.size = Context::normalfont.size;
559 // we only need to write the font change if we have an open layout
560 if (!parent_context.atParagraphStart())
561 output_font_change(os, oldFont, parent_context.font);
562 parent_context.check_end_layout(os);
563 Context context(true, parent_context.textclass, newlayout,
564 parent_context.layout, parent_context.font);
565 if (parent_context.deeper_paragraph) {
566 // We are beginning a nested environment after a
567 // deeper paragraph inside the outer list environment.
568 // Therefore we don't need to output a "begin deeper".
569 context.need_end_deeper = true;
571 context.check_deeper(os);
572 context.check_layout(os);
573 unsigned int optargs = 0;
574 while (optargs < context.layout->optargs) {
575 eat_whitespace(p, os, context, false);
576 if (p.next_token().cat() == catEscape ||
577 p.next_token().character() != '[')
579 p.get_token(); // eat '['
580 begin_inset(os, "Argument\n");
581 os << "status collapsed\n\n";
582 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
584 eat_whitespace(p, os, context, false);
587 unsigned int reqargs = 0;
588 while (reqargs < context.layout->reqargs) {
589 eat_whitespace(p, os, context, false);
590 if (p.next_token().cat() != catBegin)
592 p.get_token(); // eat '{'
593 begin_inset(os, "Argument\n");
594 os << "status collapsed\n\n";
595 parse_text_in_inset(p, os, FLAG_BRACE_LAST, outer, context);
597 eat_whitespace(p, os, context, false);
600 parse_text(p, os, FLAG_ITEM, outer, context);
601 context.check_end_layout(os);
602 if (parent_context.deeper_paragraph) {
603 // We must suppress the "end deeper" because we
604 // suppressed the "begin deeper" above.
605 context.need_end_deeper = false;
607 context.check_end_deeper(os);
608 // We don't need really a new paragraph, but
609 // we must make sure that the next item gets a \begin_layout.
610 parent_context.new_paragraph(os);
611 // Set the font size to the original value. No need to output it here
612 // (Context::begin_layout() will do that if needed)
613 parent_context.font.size = size;
618 * Output a space if necessary.
619 * This function gets called for every whitespace token.
621 * We have three cases here:
622 * 1. A space must be suppressed. Example: The lyxcode case below
623 * 2. A space may be suppressed. Example: Spaces before "\par"
624 * 3. A space must not be suppressed. Example: A space between two words
626 * We currently handle only 1. and 3 and from 2. only the case of
627 * spaces before newlines as a side effect.
629 * 2. could be used to suppress as many spaces as possible. This has two effects:
630 * - Reimporting LyX generated LaTeX files changes almost no whitespace
631 * - Superflous whitespace from non LyX generated LaTeX files is removed.
632 * The drawback is that the logic inside the function becomes
633 * complicated, and that is the reason why it is not implemented.
635 void check_space(Parser & p, ostream & os, Context & context)
637 Token const next = p.next_token();
638 Token const curr = p.curr_token();
639 // A space before a single newline and vice versa must be ignored
640 // LyX emits a newline before \end{lyxcode}.
641 // This newline must be ignored,
642 // otherwise LyX will add an additional protected space.
643 if (next.cat() == catSpace ||
644 next.cat() == catNewline ||
645 (next.cs() == "end" && context.layout->free_spacing && curr.cat() == catNewline)) {
648 context.check_layout(os);
654 * Parse all arguments of \p command
656 void parse_arguments(string const & command,
657 vector<ArgumentType> const & template_arguments,
658 Parser & p, ostream & os, bool outer, Context & context)
660 string ert = command;
661 size_t no_arguments = template_arguments.size();
662 for (size_t i = 0; i < no_arguments; ++i) {
663 switch (template_arguments[i]) {
665 // This argument contains regular LaTeX
666 handle_ert(os, ert + '{', context);
667 eat_whitespace(p, os, context, false);
668 parse_text(p, os, FLAG_ITEM, outer, context);
672 // This argument consists only of a single item.
673 // The presence of '{' or not must be preserved.
675 if (p.next_token().cat() == catBegin)
676 ert += '{' + p.verbatim_item() + '}';
678 ert += p.verbatim_item();
681 // This argument may contain special characters
682 ert += '{' + p.verbatim_item() + '}';
685 // true because we must not eat whitespace
686 // if an optional arg follows we must not strip the
687 // brackets from this one
688 if (i < no_arguments - 1 &&
689 template_arguments[i+1] == optional)
690 ert += p.getFullOpt(true);
692 ert += p.getOpt(true);
696 handle_ert(os, ert, context);
701 * Check whether \p command is a known command. If yes,
702 * handle the command with all arguments.
703 * \return true if the command was parsed, false otherwise.
705 bool parse_command(string const & command, Parser & p, ostream & os,
706 bool outer, Context & context)
708 if (known_commands.find(command) != known_commands.end()) {
709 parse_arguments(command, known_commands[command], p, os,
717 /// Parses a minipage or parbox
718 void parse_box(Parser & p, ostream & os, unsigned outer_flags,
719 unsigned inner_flags, bool outer, Context & parent_context,
720 string const & outer_type, string const & special,
721 string const & inner_type)
725 string hor_pos = "c";
726 // We need to set the height to the LaTeX default of 1\\totalheight
727 // for the case when no height argument is given
728 string height_value = "1";
729 string height_unit = "in";
730 string height_special = "totalheight";
732 if (!inner_type.empty() && p.hasOpt()) {
733 position = p.getArg('[', ']');
734 if (position != "t" && position != "c" && position != "b") {
735 cerr << "invalid position " << position << " for "
736 << inner_type << endl;
740 latex_height = p.getArg('[', ']');
741 translate_box_len(latex_height, height_value, height_unit, height_special);
744 inner_pos = p.getArg('[', ']');
745 if (inner_pos != "c" && inner_pos != "t" &&
746 inner_pos != "b" && inner_pos != "s") {
747 cerr << "invalid inner_pos "
748 << inner_pos << " for "
749 << inner_type << endl;
750 inner_pos = position;
758 if (inner_type.empty()) {
760 latex_width = "\\columnwidth";
763 latex_width = p2.getArg('[', ']');
764 string const opt = p2.getArg('[', ']');
767 if (hor_pos != "l" && hor_pos != "c" &&
769 cerr << "invalid hor_pos " << hor_pos
770 << " for " << outer_type << endl;
776 latex_width = p.verbatim_item();
777 translate_len(latex_width, width_value, width_unit);
778 // LyX can't handle length variables
779 bool use_ert = contains(width_unit, '\\') || contains(height_unit, '\\');
780 if (!use_ert && !outer_type.empty() && !inner_type.empty()) {
781 // Look whether there is some content after the end of the
782 // inner box, but before the end of the outer box.
783 // If yes, we need to output ERT.
785 if (inner_flags & FLAG_END)
786 p.verbatimEnvironment(inner_type);
790 if ((outer_type == "framed" && p.next_token().asInput() != "\\end") ||
791 (outer_type != "framed" && p.next_token().cat() != catEnd)) {
792 // something is between the end of the inner box and
793 // the end of the outer box, so we need to use ERT.
800 if (!outer_type.empty()) {
801 if (outer_flags & FLAG_END)
802 ss << "\\begin{" << outer_type << '}';
804 ss << '\\' << outer_type << '{';
805 if (!special.empty())
809 if (!inner_type.empty()) {
810 if (inner_flags & FLAG_END)
811 ss << "\\begin{" << inner_type << '}';
813 ss << '\\' << inner_type;
814 if (!position.empty())
815 ss << '[' << position << ']';
816 if (!latex_height.empty())
817 ss << '[' << latex_height << ']';
818 if (!inner_pos.empty())
819 ss << '[' << inner_pos << ']';
820 ss << '{' << latex_width << '}';
821 if (!(inner_flags & FLAG_END))
824 handle_ert(os, ss.str(), parent_context);
825 if (!inner_type.empty()) {
826 parse_text(p, os, inner_flags, outer, parent_context);
827 if (inner_flags & FLAG_END)
828 handle_ert(os, "\\end{" + inner_type + '}',
831 handle_ert(os, "}", parent_context);
833 if (!outer_type.empty()) {
834 parse_text(p, os, outer_flags, outer, parent_context);
835 if (outer_flags & FLAG_END)
836 handle_ert(os, "\\end{" + outer_type + '}',
839 handle_ert(os, "}", parent_context);
842 // LyX does not like empty positions, so we have
843 // to set them to the LaTeX default values here.
844 if (position.empty())
846 if (inner_pos.empty())
847 inner_pos = position;
848 parent_context.check_layout(os);
849 begin_inset(os, "Box ");
850 if (outer_type == "framed")
852 else if (outer_type == "framebox")
854 else if (outer_type == "shadowbox")
856 else if (outer_type == "shaded")
858 else if (outer_type == "doublebox")
860 else if (outer_type.empty())
863 os << outer_type << '\n';
864 os << "position \"" << position << "\"\n";
865 os << "hor_pos \"" << hor_pos << "\"\n";
866 os << "has_inner_box " << !inner_type.empty() << "\n";
867 os << "inner_pos \"" << inner_pos << "\"\n";
868 os << "use_parbox " << (inner_type == "parbox") << '\n';
869 os << "use_makebox 0\n";
870 os << "width \"" << width_value << width_unit << "\"\n";
871 os << "special \"none\"\n";
872 os << "height \"" << height_value << height_unit << "\"\n";
873 os << "height_special \"" << height_special << "\"\n";
874 os << "status open\n\n";
875 Context context(true, parent_context.textclass);
876 context.font = parent_context.font;
878 // If we have no inner box the contens will be read with the outer box
879 if (!inner_type.empty())
880 parse_text(p, os, inner_flags, outer, context);
882 // Ensure that the end of the outer box is parsed correctly:
883 // The opening brace has been eaten by parse_outer_box()
884 if (!outer_type.empty() && (outer_flags & FLAG_ITEM)) {
885 outer_flags &= ~FLAG_ITEM;
886 outer_flags |= FLAG_BRACE_LAST;
889 // Find end of outer box, output contents if inner_type is
890 // empty and output possible comments
891 if (!outer_type.empty()) {
892 // This does not output anything but comments if
893 // inner_type is not empty (see use_ert)
894 parse_text(p, os, outer_flags, outer, context);
897 context.check_end_layout(os);
899 #ifdef PRESERVE_LAYOUT
900 // LyX puts a % after the end of the minipage
901 if (p.next_token().cat() == catNewline && p.next_token().cs().size() > 1) {
903 //handle_comment(os, "%dummy", parent_context);
906 parent_context.new_paragraph(os);
908 else if (p.next_token().cat() == catSpace || p.next_token().cat() == catNewline) {
909 //handle_comment(os, "%dummy", parent_context);
912 // We add a protected space if something real follows
913 if (p.good() && p.next_token().cat() != catComment) {
914 begin_inset(os, "space ~\n");
923 void parse_outer_box(Parser & p, ostream & os, unsigned flags, bool outer,
924 Context & parent_context, string const & outer_type,
925 string const & special)
927 eat_whitespace(p, os, parent_context, false);
928 if (flags & FLAG_ITEM) {
930 if (p.next_token().cat() == catBegin)
933 cerr << "Warning: Ignoring missing '{' after \\"
934 << outer_type << '.' << endl;
935 eat_whitespace(p, os, parent_context, false);
938 unsigned int inner_flags = 0;
939 if (outer_type == "shaded") {
940 // These boxes never have an inner box
942 } else if (p.next_token().asInput() == "\\parbox") {
943 inner = p.get_token().cs();
944 inner_flags = FLAG_ITEM;
945 } else if (p.next_token().asInput() == "\\begin") {
946 // Is this a minipage?
949 inner = p.getArg('{', '}');
951 if (inner == "minipage") {
954 eat_whitespace(p, os, parent_context, false);
955 inner_flags = FLAG_END;
959 if (inner_flags == FLAG_END) {
960 active_environments.push_back(inner);
961 parse_box(p, os, flags, FLAG_END, outer, parent_context,
962 outer_type, special, inner);
963 active_environments.pop_back();
965 parse_box(p, os, flags, inner_flags, outer, parent_context,
966 outer_type, special, inner);
971 void parse_listings(Parser & p, ostream & os, Context & parent_context)
973 parent_context.check_layout(os);
974 begin_inset(os, "listings\n");
975 os << "inline false\n"
976 << "status collapsed\n";
977 Context context(true, parent_context.textclass);
978 context.layout = &parent_context.textclass.plainLayout();
979 context.check_layout(os);
980 string const s = p.verbatimEnvironment("lstlisting");
981 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
983 os << "\n\\backslash\n";
984 else if (*it == '\n') {
985 // avoid adding an empty paragraph at the end
987 context.new_paragraph(os);
988 context.check_layout(os);
993 context.check_end_layout(os);
998 /// parse an unknown environment
999 void parse_unknown_environment(Parser & p, string const & name, ostream & os,
1000 unsigned flags, bool outer,
1001 Context & parent_context)
1003 if (name == "tabbing")
1004 // We need to remember that we have to handle '\=' specially
1005 flags |= FLAG_TABBING;
1007 // We need to translate font changes and paragraphs inside the
1008 // environment to ERT if we have a non standard font.
1009 // Otherwise things like
1010 // \large\begin{foo}\huge bar\end{foo}
1012 bool const specialfont =
1013 (parent_context.font != parent_context.normalfont);
1014 bool const new_layout_allowed = parent_context.new_layout_allowed;
1016 parent_context.new_layout_allowed = false;
1017 handle_ert(os, "\\begin{" + name + "}", parent_context);
1018 parse_text_snippet(p, os, flags, outer, parent_context);
1019 handle_ert(os, "\\end{" + name + "}", parent_context);
1021 parent_context.new_layout_allowed = new_layout_allowed;
1025 void parse_environment(Parser & p, ostream & os, bool outer,
1026 string & last_env, Context & parent_context)
1028 Layout const * newlayout;
1029 InsetLayout const * newinsetlayout = 0;
1030 string const name = p.getArg('{', '}');
1031 const bool is_starred = suffixIs(name, '*');
1032 string const unstarred_name = rtrim(name, "*");
1033 active_environments.push_back(name);
1035 if (is_math_env(name)) {
1036 parent_context.check_layout(os);
1037 begin_inset(os, "Formula ");
1038 os << "\\begin{" << name << "}";
1039 parse_math(p, os, FLAG_END, MATH_MODE);
1040 os << "\\end{" << name << "}";
1044 else if (name == "tabular" || name == "longtable") {
1045 eat_whitespace(p, os, parent_context, false);
1046 parent_context.check_layout(os);
1047 begin_inset(os, "Tabular ");
1048 handle_tabular(p, os, name == "longtable", parent_context);
1053 else if (parent_context.textclass.floats().typeExist(unstarred_name)) {
1054 eat_whitespace(p, os, parent_context, false);
1055 parent_context.check_layout(os);
1056 begin_inset(os, "Float " + unstarred_name + "\n");
1058 os << "placement " << p.getArg('[', ']') << '\n';
1059 os << "wide " << convert<string>(is_starred)
1060 << "\nsideways false"
1061 << "\nstatus open\n\n";
1062 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
1064 // We don't need really a new paragraph, but
1065 // we must make sure that the next item gets a \begin_layout.
1066 parent_context.new_paragraph(os);
1070 else if (name == "minipage") {
1071 eat_whitespace(p, os, parent_context, false);
1072 parse_box(p, os, 0, FLAG_END, outer, parent_context, "", "", name);
1076 else if (name == "comment") {
1077 eat_whitespace(p, os, parent_context, false);
1078 parent_context.check_layout(os);
1079 begin_inset(os, "Note Comment\n");
1080 os << "status open\n";
1081 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
1084 skip_braces(p); // eat {} that might by set by LyX behind comments
1087 else if (name == "lyxgreyedout") {
1088 eat_whitespace(p, os, parent_context, false);
1089 parent_context.check_layout(os);
1090 begin_inset(os, "Note Greyedout\n");
1091 os << "status open\n";
1092 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
1097 else if (name == "framed" || name == "shaded") {
1098 eat_whitespace(p, os, parent_context, false);
1099 parse_outer_box(p, os, FLAG_END, outer, parent_context, name, "");
1103 else if (name == "lstlisting") {
1104 eat_whitespace(p, os, parent_context, false);
1105 // FIXME handle listings with parameters
1107 parse_unknown_environment(p, name, os, FLAG_END,
1108 outer, parent_context);
1110 parse_listings(p, os, parent_context);
1114 else if (!parent_context.new_layout_allowed)
1115 parse_unknown_environment(p, name, os, FLAG_END, outer,
1118 // Alignment and spacing settings
1119 // FIXME (bug xxxx): These settings can span multiple paragraphs and
1120 // therefore are totally broken!
1121 // Note that \centering, raggedright, and raggedleft cannot be handled, as
1122 // they are commands not environments. They are furthermore switches that
1123 // can be ended by another switches, but also by commands like \footnote or
1124 // \parbox. So the only safe way is to leave them untouched.
1125 else if (name == "center" || name == "centering" ||
1126 name == "flushleft" || name == "flushright" ||
1127 name == "singlespace" || name == "onehalfspace" ||
1128 name == "doublespace" || name == "spacing") {
1129 eat_whitespace(p, os, parent_context, false);
1130 // We must begin a new paragraph if not already done
1131 if (! parent_context.atParagraphStart()) {
1132 parent_context.check_end_layout(os);
1133 parent_context.new_paragraph(os);
1135 if (name == "flushleft")
1136 parent_context.add_extra_stuff("\\align left\n");
1137 else if (name == "flushright")
1138 parent_context.add_extra_stuff("\\align right\n");
1139 else if (name == "center" || name == "centering")
1140 parent_context.add_extra_stuff("\\align center\n");
1141 else if (name == "singlespace")
1142 parent_context.add_extra_stuff("\\paragraph_spacing single\n");
1143 else if (name == "onehalfspace")
1144 parent_context.add_extra_stuff("\\paragraph_spacing onehalf\n");
1145 else if (name == "doublespace")
1146 parent_context.add_extra_stuff("\\paragraph_spacing double\n");
1147 else if (name == "spacing")
1148 parent_context.add_extra_stuff("\\paragraph_spacing other " + p.verbatim_item() + "\n");
1149 parse_text(p, os, FLAG_END, outer, parent_context);
1150 // Just in case the environment is empty
1151 parent_context.extra_stuff.erase();
1152 // We must begin a new paragraph to reset the alignment
1153 parent_context.new_paragraph(os);
1157 // The single '=' is meant here.
1158 else if ((newlayout = findLayout(parent_context.textclass, name, false))) {
1159 eat_whitespace(p, os, parent_context, false);
1160 Context context(true, parent_context.textclass, newlayout,
1161 parent_context.layout, parent_context.font);
1162 if (parent_context.deeper_paragraph) {
1163 // We are beginning a nested environment after a
1164 // deeper paragraph inside the outer list environment.
1165 // Therefore we don't need to output a "begin deeper".
1166 context.need_end_deeper = true;
1168 parent_context.check_end_layout(os);
1169 if (last_env == name) {
1170 // we need to output a separator since LyX would export
1171 // the two environments as one otherwise (bug 5716)
1172 docstring const sep = from_ascii("--Separator--");
1173 TeX2LyXDocClass const & textclass(parent_context.textclass);
1174 if (textclass.hasLayout(sep)) {
1175 Context newcontext(parent_context);
1176 newcontext.layout = &(textclass[sep]);
1177 newcontext.check_layout(os);
1178 newcontext.check_end_layout(os);
1180 parent_context.check_layout(os);
1181 begin_inset(os, "Note Note\n");
1182 os << "status closed\n";
1183 Context newcontext(true, textclass,
1184 &(textclass.defaultLayout()));
1185 newcontext.check_layout(os);
1186 newcontext.check_end_layout(os);
1188 parent_context.check_end_layout(os);
1191 switch (context.layout->latextype) {
1192 case LATEX_LIST_ENVIRONMENT:
1193 context.add_par_extra_stuff("\\labelwidthstring "
1194 + p.verbatim_item() + '\n');
1197 case LATEX_BIB_ENVIRONMENT:
1198 p.verbatim_item(); // swallow next arg
1204 context.check_deeper(os);
1205 // handle known optional and required arguments
1206 // layouts require all optional arguments before the required ones
1207 // Unfortunately LyX can't handle arguments of list arguments (bug 7468):
1208 // It is impossible to place anything after the environment name,
1209 // but before the first \\item.
1210 if (context.layout->latextype == LATEX_ENVIRONMENT) {
1211 bool need_layout = true;
1212 unsigned int optargs = 0;
1213 while (optargs < context.layout->optargs) {
1214 eat_whitespace(p, os, context, false);
1215 if (p.next_token().cat() == catEscape ||
1216 p.next_token().character() != '[')
1218 p.get_token(); // eat '['
1220 context.check_layout(os);
1221 need_layout = false;
1223 begin_inset(os, "Argument\n");
1224 os << "status collapsed\n\n";
1225 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
1227 eat_whitespace(p, os, context, false);
1230 unsigned int reqargs = 0;
1231 while (reqargs < context.layout->reqargs) {
1232 eat_whitespace(p, os, context, false);
1233 if (p.next_token().cat() != catBegin)
1235 p.get_token(); // eat '{'
1237 context.check_layout(os);
1238 need_layout = false;
1240 begin_inset(os, "Argument\n");
1241 os << "status collapsed\n\n";
1242 parse_text_in_inset(p, os, FLAG_BRACE_LAST, outer, context);
1244 eat_whitespace(p, os, context, false);
1248 parse_text(p, os, FLAG_END, outer, context);
1249 context.check_end_layout(os);
1250 if (parent_context.deeper_paragraph) {
1251 // We must suppress the "end deeper" because we
1252 // suppressed the "begin deeper" above.
1253 context.need_end_deeper = false;
1255 context.check_end_deeper(os);
1256 parent_context.new_paragraph(os);
1260 // The single '=' is meant here.
1261 else if ((newinsetlayout = findInsetLayout(parent_context.textclass, name, false))) {
1262 eat_whitespace(p, os, parent_context, false);
1263 parent_context.check_layout(os);
1264 begin_inset(os, "Flex ");
1265 os << to_utf8(newinsetlayout->name()) << '\n'
1266 << "status collapsed\n";
1267 parse_text_in_inset(p, os, FLAG_END, false, parent_context, newinsetlayout);
1271 else if (name == "appendix") {
1272 // This is no good latex style, but it works and is used in some documents...
1273 eat_whitespace(p, os, parent_context, false);
1274 parent_context.check_end_layout(os);
1275 Context context(true, parent_context.textclass, parent_context.layout,
1276 parent_context.layout, parent_context.font);
1277 context.check_layout(os);
1278 os << "\\start_of_appendix\n";
1279 parse_text(p, os, FLAG_END, outer, context);
1280 context.check_end_layout(os);
1284 else if (known_environments.find(name) != known_environments.end()) {
1285 vector<ArgumentType> arguments = known_environments[name];
1286 // The last "argument" denotes wether we may translate the
1287 // environment contents to LyX
1288 // The default required if no argument is given makes us
1289 // compatible with the reLyXre environment.
1290 ArgumentType contents = arguments.empty() ?
1293 if (!arguments.empty())
1294 arguments.pop_back();
1295 // See comment in parse_unknown_environment()
1296 bool const specialfont =
1297 (parent_context.font != parent_context.normalfont);
1298 bool const new_layout_allowed =
1299 parent_context.new_layout_allowed;
1301 parent_context.new_layout_allowed = false;
1302 parse_arguments("\\begin{" + name + "}", arguments, p, os,
1303 outer, parent_context);
1304 if (contents == verbatim)
1305 handle_ert(os, p.verbatimEnvironment(name),
1308 parse_text_snippet(p, os, FLAG_END, outer,
1310 handle_ert(os, "\\end{" + name + "}", parent_context);
1312 parent_context.new_layout_allowed = new_layout_allowed;
1316 parse_unknown_environment(p, name, os, FLAG_END, outer,
1320 active_environments.pop_back();
1324 /// parses a comment and outputs it to \p os.
1325 void parse_comment(Parser & p, ostream & os, Token const & t, Context & context)
1327 LASSERT(t.cat() == catComment, return);
1328 if (!t.cs().empty()) {
1329 context.check_layout(os);
1330 handle_comment(os, '%' + t.cs(), context);
1331 if (p.next_token().cat() == catNewline) {
1332 // A newline after a comment line starts a new
1334 if (context.new_layout_allowed) {
1335 if(!context.atParagraphStart())
1336 // Only start a new paragraph if not already
1337 // done (we might get called recursively)
1338 context.new_paragraph(os);
1340 handle_ert(os, "\n", context);
1341 eat_whitespace(p, os, context, true);
1344 // "%\n" combination
1351 * Reads spaces and comments until the first non-space, non-comment token.
1352 * New paragraphs (double newlines or \\par) are handled like simple spaces
1353 * if \p eatParagraph is true.
1354 * Spaces are skipped, but comments are written to \p os.
1356 void eat_whitespace(Parser & p, ostream & os, Context & context,
1360 Token const & t = p.get_token();
1361 if (t.cat() == catComment)
1362 parse_comment(p, os, t, context);
1363 else if ((! eatParagraph && p.isParagraph()) ||
1364 (t.cat() != catSpace && t.cat() != catNewline)) {
1373 * Set a font attribute, parse text and reset the font attribute.
1374 * \param attribute Attribute name (e.g. \\family, \\shape etc.)
1375 * \param currentvalue Current value of the attribute. Is set to the new
1376 * value during parsing.
1377 * \param newvalue New value of the attribute
1379 void parse_text_attributes(Parser & p, ostream & os, unsigned flags, bool outer,
1380 Context & context, string const & attribute,
1381 string & currentvalue, string const & newvalue)
1383 context.check_layout(os);
1384 string const oldvalue = currentvalue;
1385 currentvalue = newvalue;
1386 os << '\n' << attribute << ' ' << newvalue << "\n";
1387 parse_text_snippet(p, os, flags, outer, context);
1388 context.check_layout(os);
1389 os << '\n' << attribute << ' ' << oldvalue << "\n";
1390 currentvalue = oldvalue;
1394 /// get the arguments of a natbib or jurabib citation command
1395 void get_cite_arguments(Parser & p, bool natbibOrder,
1396 string & before, string & after)
1398 // We need to distinguish "" and "[]", so we can't use p.getOpt().
1400 // text before the citation
1402 // text after the citation
1403 after = p.getFullOpt();
1405 if (!after.empty()) {
1406 before = p.getFullOpt();
1407 if (natbibOrder && !before.empty())
1408 swap(before, after);
1413 /// Convert filenames with TeX macros and/or quotes to something LyX
1415 string const normalize_filename(string const & name)
1417 Parser p(trim(name, "\""));
1420 Token const & t = p.get_token();
1421 if (t.cat() != catEscape)
1423 else if (t.cs() == "lyxdot") {
1424 // This is used by LyX for simple dots in relative
1428 } else if (t.cs() == "space") {
1438 /// Convert \p name from TeX convention (relative to master file) to LyX
1439 /// convention (relative to .lyx file) if it is relative
1440 void fix_relative_filename(string & name)
1442 if (FileName::isAbsolute(name))
1445 name = to_utf8(makeRelPath(from_utf8(makeAbsPath(name, getMasterFilePath()).absFileName()),
1446 from_utf8(getParentFilePath())));
1450 /// Parse a NoWeb Scrap section. The initial "<<" is already parsed.
1451 void parse_noweb(Parser & p, ostream & os, Context & context)
1453 // assemble the rest of the keyword
1457 Token const & t = p.get_token();
1458 if (t.asInput() == ">" && p.next_token().asInput() == ">") {
1461 scrap = (p.good() && p.next_token().asInput() == "=");
1463 name += p.get_token().asInput();
1466 name += t.asInput();
1469 if (!scrap || !context.new_layout_allowed ||
1470 !context.textclass.hasLayout(from_ascii("Scrap"))) {
1471 cerr << "Warning: Could not interpret '" << name
1472 << "'. Ignoring it." << endl;
1476 // We use new_paragraph instead of check_end_layout because the stuff
1477 // following the noweb chunk needs to start with a \begin_layout.
1478 // This may create a new paragraph even if there was none in the
1479 // noweb file, but the alternative is an invalid LyX file. Since
1480 // noweb code chunks are implemented with a layout style in LyX they
1481 // always must be in an own paragraph.
1482 context.new_paragraph(os);
1483 Context newcontext(true, context.textclass,
1484 &context.textclass[from_ascii("Scrap")]);
1485 newcontext.check_layout(os);
1488 Token const & t = p.get_token();
1489 // We abuse the parser a bit, because this is no TeX syntax
1491 if (t.cat() == catEscape)
1492 os << subst(t.asInput(), "\\", "\n\\backslash\n");
1495 begin_inset(oss, "Newline newline");
1497 os << subst(t.asInput(), "\n", oss.str());
1499 // The scrap chunk is ended by an @ at the beginning of a line.
1500 // After the @ the line may contain a comment and/or
1501 // whitespace, but nothing else.
1502 if (t.asInput() == "@" && p.prev_token().cat() == catNewline &&
1503 (p.next_token().cat() == catSpace ||
1504 p.next_token().cat() == catNewline ||
1505 p.next_token().cat() == catComment)) {
1506 while (p.good() && p.next_token().cat() == catSpace)
1507 os << p.get_token().asInput();
1508 if (p.next_token().cat() == catComment)
1509 // The comment includes a final '\n'
1510 os << p.get_token().asInput();
1512 if (p.next_token().cat() == catNewline)
1519 newcontext.check_end_layout(os);
1523 /// detects \\def, \\long\\def and \\global\\long\\def with ws and comments
1524 bool is_macro(Parser & p)
1526 Token first = p.curr_token();
1527 if (first.cat() != catEscape || !p.good())
1529 if (first.cs() == "def")
1531 if (first.cs() != "global" && first.cs() != "long")
1533 Token second = p.get_token();
1535 while (p.good() && !p.isParagraph() && (second.cat() == catSpace ||
1536 second.cat() == catNewline || second.cat() == catComment)) {
1537 second = p.get_token();
1540 bool secondvalid = second.cat() == catEscape;
1542 bool thirdvalid = false;
1543 if (p.good() && first.cs() == "global" && secondvalid &&
1544 second.cs() == "long") {
1545 third = p.get_token();
1547 while (p.good() && !p.isParagraph() &&
1548 (third.cat() == catSpace ||
1549 third.cat() == catNewline ||
1550 third.cat() == catComment)) {
1551 third = p.get_token();
1554 thirdvalid = third.cat() == catEscape;
1556 for (int i = 0; i < pos; ++i)
1561 return (first.cs() == "global" || first.cs() == "long") &&
1562 second.cs() == "def";
1563 return first.cs() == "global" && second.cs() == "long" &&
1564 third.cs() == "def";
1568 /// Parse a macro definition (assumes that is_macro() returned true)
1569 void parse_macro(Parser & p, ostream & os, Context & context)
1571 context.check_layout(os);
1572 Token first = p.curr_token();
1575 string command = first.asInput();
1576 if (first.cs() != "def") {
1578 eat_whitespace(p, os, context, false);
1579 second = p.curr_token();
1580 command += second.asInput();
1581 if (second.cs() != "def") {
1583 eat_whitespace(p, os, context, false);
1584 third = p.curr_token();
1585 command += third.asInput();
1588 eat_whitespace(p, os, context, false);
1589 string const name = p.get_token().cs();
1590 eat_whitespace(p, os, context, false);
1596 while (p.next_token().cat() != catBegin) {
1597 if (p.next_token().cat() == catParameter) {
1602 // followed by number?
1603 if (p.next_token().cat() == catOther) {
1604 char c = p.getChar();
1606 // number = current arity + 1?
1607 if (c == arity + '0' + 1)
1612 paramtext += p.get_token().cs();
1614 paramtext += p.get_token().cs();
1619 // only output simple (i.e. compatible) macro as FormulaMacros
1620 string ert = '\\' + name + ' ' + paramtext + '{' + p.verbatim_item() + '}';
1622 context.check_layout(os);
1623 begin_inset(os, "FormulaMacro");
1624 os << "\n\\def" << ert;
1627 handle_ert(os, command + ert, context);
1630 } // anonymous namespace
1633 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
1636 Layout const * newlayout = 0;
1637 InsetLayout const * newinsetlayout = 0;
1638 // Store the latest bibliographystyle and nocite{*} option
1639 // (needed for bibtex inset)
1641 string bibliographystyle;
1642 bool const use_natbib = used_packages.find("natbib") != used_packages.end();
1643 bool const use_jurabib = used_packages.find("jurabib") != used_packages.end();
1646 Token const & t = p.get_token();
1649 debugToken(cerr, t, flags);
1652 if (flags & FLAG_ITEM) {
1653 if (t.cat() == catSpace)
1656 flags &= ~FLAG_ITEM;
1657 if (t.cat() == catBegin) {
1658 // skip the brace and collect everything to the next matching
1660 flags |= FLAG_BRACE_LAST;
1664 // handle only this single token, leave the loop if done
1665 flags |= FLAG_LEAVE;
1668 if (t.cat() != catEscape && t.character() == ']' &&
1669 (flags & FLAG_BRACK_LAST))
1671 if (t.cat() == catEnd && (flags & FLAG_BRACE_LAST))
1674 // If there is anything between \end{env} and \begin{env} we
1675 // don't need to output a separator.
1676 if (t.cat() != catSpace && t.cat() != catNewline &&
1677 t.asInput() != "\\begin")
1683 if (t.cat() == catMath) {
1684 // we are inside some text mode thingy, so opening new math is allowed
1685 context.check_layout(os);
1686 begin_inset(os, "Formula ");
1687 Token const & n = p.get_token();
1688 if (n.cat() == catMath && outer) {
1689 // TeX's $$...$$ syntax for displayed math
1691 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
1693 p.get_token(); // skip the second '$' token
1695 // simple $...$ stuff
1698 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
1704 else if (t.cat() == catSuper || t.cat() == catSub)
1705 cerr << "catcode " << t << " illegal in text mode\n";
1707 // Basic support for english quotes. This should be
1708 // extended to other quotes, but is not so easy (a
1709 // left english quote is the same as a right german
1711 else if (t.asInput() == "`" && p.next_token().asInput() == "`") {
1712 context.check_layout(os);
1713 begin_inset(os, "Quotes ");
1719 else if (t.asInput() == "'" && p.next_token().asInput() == "'") {
1720 context.check_layout(os);
1721 begin_inset(os, "Quotes ");
1728 else if (t.asInput() == ">" && p.next_token().asInput() == ">") {
1729 context.check_layout(os);
1730 begin_inset(os, "Quotes ");
1737 else if (t.asInput() == "<" && p.next_token().asInput() == "<") {
1738 context.check_layout(os);
1739 begin_inset(os, "Quotes ");
1746 else if (t.asInput() == "<"
1747 && p.next_token().asInput() == "<" && noweb_mode) {
1749 parse_noweb(p, os, context);
1752 else if (t.cat() == catSpace || (t.cat() == catNewline && ! p.isParagraph()))
1753 check_space(p, os, context);
1755 else if (t.character() == '[' && noweb_mode &&
1756 p.next_token().character() == '[') {
1757 // These can contain underscores
1759 string const s = p.getFullOpt() + ']';
1760 if (p.next_token().character() == ']')
1763 cerr << "Warning: Inserting missing ']' in '"
1764 << s << "'." << endl;
1765 handle_ert(os, s, context);
1768 else if (t.cat() == catLetter) {
1769 context.check_layout(os);
1770 // Workaround for bug 4752.
1771 // FIXME: This whole code block needs to be removed
1772 // when the bug is fixed and tex2lyx produces
1773 // the updated file format.
1774 // The replacement algorithm in LyX is so stupid that
1775 // it even translates a phrase if it is part of a word.
1776 bool handled = false;
1777 for (int const * l = known_phrase_lengths; *l; ++l) {
1778 string phrase = t.cs();
1779 for (int i = 1; i < *l && p.next_token().isAlnumASCII(); ++i)
1780 phrase += p.get_token().cs();
1781 if (is_known(phrase, known_coded_phrases)) {
1782 handle_ert(os, phrase, context);
1786 for (size_t i = 1; i < phrase.length(); ++i)
1794 else if (t.cat() == catOther ||
1795 t.cat() == catAlign ||
1796 t.cat() == catParameter) {
1797 // This translates "&" to "\\&" which may be wrong...
1798 context.check_layout(os);
1802 else if (p.isParagraph()) {
1803 if (context.new_layout_allowed)
1804 context.new_paragraph(os);
1806 handle_ert(os, "\\par ", context);
1807 eat_whitespace(p, os, context, true);
1810 else if (t.cat() == catActive) {
1811 context.check_layout(os);
1812 if (t.character() == '~') {
1813 if (context.layout->free_spacing)
1816 begin_inset(os, "space ~\n");
1823 else if (t.cat() == catBegin &&
1824 p.next_token().cat() == catEnd) {
1826 Token const prev = p.prev_token();
1828 if (p.next_token().character() == '`' ||
1829 (prev.character() == '-' &&
1830 p.next_token().character() == '-'))
1831 ; // ignore it in {}`` or -{}-
1833 handle_ert(os, "{}", context);
1837 else if (t.cat() == catBegin) {
1838 context.check_layout(os);
1839 // special handling of font attribute changes
1840 Token const prev = p.prev_token();
1841 Token const next = p.next_token();
1842 TeXFont const oldFont = context.font;
1843 if (next.character() == '[' ||
1844 next.character() == ']' ||
1845 next.character() == '*') {
1847 if (p.next_token().cat() == catEnd) {
1852 handle_ert(os, "{", context);
1853 parse_text_snippet(p, os,
1856 handle_ert(os, "}", context);
1858 } else if (! context.new_layout_allowed) {
1859 handle_ert(os, "{", context);
1860 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1862 handle_ert(os, "}", context);
1863 } else if (is_known(next.cs(), known_sizes)) {
1864 // next will change the size, so we must
1866 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1868 if (!context.atParagraphStart())
1870 << context.font.size << "\n";
1871 } else if (is_known(next.cs(), known_font_families)) {
1872 // next will change the font family, so we
1873 // must reset it here
1874 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1876 if (!context.atParagraphStart())
1878 << context.font.family << "\n";
1879 } else if (is_known(next.cs(), known_font_series)) {
1880 // next will change the font series, so we
1881 // must reset it here
1882 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1884 if (!context.atParagraphStart())
1886 << context.font.series << "\n";
1887 } else if (is_known(next.cs(), known_font_shapes)) {
1888 // next will change the font shape, so we
1889 // must reset it here
1890 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1892 if (!context.atParagraphStart())
1894 << context.font.shape << "\n";
1895 } else if (is_known(next.cs(), known_old_font_families) ||
1896 is_known(next.cs(), known_old_font_series) ||
1897 is_known(next.cs(), known_old_font_shapes)) {
1898 // next will change the font family, series
1899 // and shape, so we must reset it here
1900 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1902 if (!context.atParagraphStart())
1904 << context.font.family
1906 << context.font.series
1908 << context.font.shape << "\n";
1910 handle_ert(os, "{", context);
1911 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1913 handle_ert(os, "}", context);
1917 else if (t.cat() == catEnd) {
1918 if (flags & FLAG_BRACE_LAST) {
1921 cerr << "stray '}' in text\n";
1922 handle_ert(os, "}", context);
1925 else if (t.cat() == catComment)
1926 parse_comment(p, os, t, context);
1929 // control sequences
1932 else if (t.cs() == "(") {
1933 context.check_layout(os);
1934 begin_inset(os, "Formula");
1936 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
1941 else if (t.cs() == "[") {
1942 context.check_layout(os);
1943 begin_inset(os, "Formula");
1945 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
1950 else if (t.cs() == "begin")
1951 parse_environment(p, os, outer, last_env, context);
1953 else if (t.cs() == "end") {
1954 if (flags & FLAG_END) {
1955 // eat environment name
1956 string const name = p.getArg('{', '}');
1957 if (name != active_environment())
1958 cerr << "\\end{" + name + "} does not match \\begin{"
1959 + active_environment() + "}\n";
1962 p.error("found 'end' unexpectedly");
1965 else if (t.cs() == "item") {
1968 bool optarg = false;
1969 if (p.next_token().cat() != catEscape &&
1970 p.next_token().character() == '[') {
1971 p.get_token(); // eat '['
1972 s = parse_text_snippet(p, FLAG_BRACK_LAST,
1977 context.check_layout(os);
1978 if (context.has_item) {
1979 // An item in an unknown list-like environment
1980 // FIXME: Do this in check_layout()!
1981 context.has_item = false;
1983 handle_ert(os, "\\item", context);
1985 handle_ert(os, "\\item ", context);
1988 if (context.layout->labeltype != LABEL_MANUAL) {
1989 // LyX does not support \item[\mybullet]
1990 // in itemize environments
1991 handle_ert(os, "[", context);
1993 handle_ert(os, "]", context);
1994 } else if (!s.empty()) {
1995 // The space is needed to separate the
1996 // item from the rest of the sentence.
1998 eat_whitespace(p, os, context, false);
2003 else if (t.cs() == "bibitem") {
2005 context.check_layout(os);
2006 string label = convert_command_inset_arg(p.getArg('[', ']'));
2007 string key = convert_command_inset_arg(p.verbatim_item());
2008 if (contains(label, '\\') || contains(key, '\\')) {
2009 // LyX can't handle LaTeX commands in labels or keys
2010 handle_ert(os, t.asInput() + '[' + label +
2011 "]{" + p.verbatim_item() + '}',
2014 begin_command_inset(os, "bibitem", "bibitem");
2015 os << "label \"" << label << "\"\n"
2016 "key \"" << key << "\"\n";
2021 else if (is_macro(p))
2022 parse_macro(p, os, context);
2024 else if (t.cs() == "noindent") {
2026 context.add_par_extra_stuff("\\noindent\n");
2029 else if (t.cs() == "appendix") {
2030 context.add_par_extra_stuff("\\start_of_appendix\n");
2031 // We need to start a new paragraph. Otherwise the
2032 // appendix in 'bla\appendix\chapter{' would start
2034 context.new_paragraph(os);
2035 // We need to make sure that the paragraph is
2036 // generated even if it is empty. Otherwise the
2037 // appendix in '\par\appendix\par\chapter{' would
2039 context.check_layout(os);
2040 // FIXME: This is a hack to prevent paragraph
2041 // deletion if it is empty. Handle this better!
2043 "%dummy comment inserted by tex2lyx to "
2044 "ensure that this paragraph is not empty",
2046 // Both measures above may generate an additional
2047 // empty paragraph, but that does not hurt, because
2048 // whitespace does not matter here.
2049 eat_whitespace(p, os, context, true);
2052 // Starred section headings
2053 // Must attempt to parse "Section*" before "Section".
2054 else if ((p.next_token().asInput() == "*") &&
2055 context.new_layout_allowed &&
2056 (newlayout = findLayout(context.textclass, t.cs() + '*', true))) {
2059 output_command_layout(os, p, outer, context, newlayout);
2063 // Section headings and the like
2064 else if (context.new_layout_allowed &&
2065 (newlayout = findLayout(context.textclass, t.cs(), true))) {
2067 output_command_layout(os, p, outer, context, newlayout);
2071 else if (t.cs() == "caption") {
2073 context.check_layout(os);
2075 begin_inset(os, "Caption\n\n");
2076 Context newcontext(true, context.textclass);
2077 newcontext.font = context.font;
2078 newcontext.check_layout(os);
2079 if (p.next_token().cat() != catEscape &&
2080 p.next_token().character() == '[') {
2081 p.get_token(); // eat '['
2082 begin_inset(os, "Argument\n");
2083 os << "status collapsed\n";
2084 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
2086 eat_whitespace(p, os, context, false);
2088 parse_text(p, os, FLAG_ITEM, outer, context);
2089 context.check_end_layout(os);
2090 // We don't need really a new paragraph, but
2091 // we must make sure that the next item gets a \begin_layout.
2092 context.new_paragraph(os);
2095 newcontext.check_end_layout(os);
2098 else if (t.cs() == "includegraphics") {
2099 bool const clip = p.next_token().asInput() == "*";
2102 string const arg = p.getArg('[', ']');
2103 map<string, string> opts;
2104 vector<string> keys;
2105 split_map(arg, opts, keys);
2107 opts["clip"] = string();
2108 string name = normalize_filename(p.verbatim_item());
2110 string const path = getMasterFilePath();
2111 // We want to preserve relative / absolute filenames,
2112 // therefore path is only used for testing
2113 if (!makeAbsPath(name, path).exists()) {
2114 // The file extension is probably missing.
2115 // Now try to find it out.
2116 string const dvips_name =
2117 find_file(name, path,
2118 known_dvips_graphics_formats);
2119 string const pdftex_name =
2120 find_file(name, path,
2121 known_pdftex_graphics_formats);
2122 if (!dvips_name.empty()) {
2123 if (!pdftex_name.empty()) {
2124 cerr << "This file contains the "
2126 "\"\\includegraphics{"
2128 "However, files\n\""
2129 << dvips_name << "\" and\n\""
2130 << pdftex_name << "\"\n"
2131 "both exist, so I had to make a "
2132 "choice and took the first one.\n"
2133 "Please move the unwanted one "
2134 "someplace else and try again\n"
2135 "if my choice was wrong."
2139 } else if (!pdftex_name.empty()) {
2145 if (makeAbsPath(name, path).exists())
2146 fix_relative_filename(name);
2148 cerr << "Warning: Could not find graphics file '"
2149 << name << "'." << endl;
2151 context.check_layout(os);
2152 begin_inset(os, "Graphics ");
2153 os << "\n\tfilename " << name << '\n';
2154 if (opts.find("width") != opts.end())
2156 << translate_len(opts["width"]) << '\n';
2157 if (opts.find("height") != opts.end())
2159 << translate_len(opts["height"]) << '\n';
2160 if (opts.find("scale") != opts.end()) {
2161 istringstream iss(opts["scale"]);
2165 os << "\tscale " << val << '\n';
2167 if (opts.find("angle") != opts.end()) {
2168 os << "\trotateAngle "
2169 << opts["angle"] << '\n';
2170 vector<string>::const_iterator a =
2171 find(keys.begin(), keys.end(), "angle");
2172 vector<string>::const_iterator s =
2173 find(keys.begin(), keys.end(), "width");
2174 if (s == keys.end())
2175 s = find(keys.begin(), keys.end(), "height");
2176 if (s == keys.end())
2177 s = find(keys.begin(), keys.end(), "scale");
2178 if (s != keys.end() && distance(s, a) > 0)
2179 os << "\tscaleBeforeRotation\n";
2181 if (opts.find("origin") != opts.end()) {
2183 string const opt = opts["origin"];
2184 if (opt.find('l') != string::npos) ss << "left";
2185 if (opt.find('r') != string::npos) ss << "right";
2186 if (opt.find('c') != string::npos) ss << "center";
2187 if (opt.find('t') != string::npos) ss << "Top";
2188 if (opt.find('b') != string::npos) ss << "Bottom";
2189 if (opt.find('B') != string::npos) ss << "Baseline";
2190 if (!ss.str().empty())
2191 os << "\trotateOrigin " << ss.str() << '\n';
2193 cerr << "Warning: Ignoring unknown includegraphics origin argument '" << opt << "'\n";
2195 if (opts.find("keepaspectratio") != opts.end())
2196 os << "\tkeepAspectRatio\n";
2197 if (opts.find("clip") != opts.end())
2199 if (opts.find("draft") != opts.end())
2201 if (opts.find("bb") != opts.end())
2202 os << "\tBoundingBox "
2203 << opts["bb"] << '\n';
2204 int numberOfbbOptions = 0;
2205 if (opts.find("bbllx") != opts.end())
2206 numberOfbbOptions++;
2207 if (opts.find("bblly") != opts.end())
2208 numberOfbbOptions++;
2209 if (opts.find("bburx") != opts.end())
2210 numberOfbbOptions++;
2211 if (opts.find("bbury") != opts.end())
2212 numberOfbbOptions++;
2213 if (numberOfbbOptions == 4)
2214 os << "\tBoundingBox "
2215 << opts["bbllx"] << " " << opts["bblly"] << " "
2216 << opts["bburx"] << " " << opts["bbury"] << '\n';
2217 else if (numberOfbbOptions > 0)
2218 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
2219 numberOfbbOptions = 0;
2220 if (opts.find("natwidth") != opts.end())
2221 numberOfbbOptions++;
2222 if (opts.find("natheight") != opts.end())
2223 numberOfbbOptions++;
2224 if (numberOfbbOptions == 2)
2225 os << "\tBoundingBox 0bp 0bp "
2226 << opts["natwidth"] << " " << opts["natheight"] << '\n';
2227 else if (numberOfbbOptions > 0)
2228 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
2229 ostringstream special;
2230 if (opts.find("hiresbb") != opts.end())
2231 special << "hiresbb,";
2232 if (opts.find("trim") != opts.end())
2234 if (opts.find("viewport") != opts.end())
2235 special << "viewport=" << opts["viewport"] << ',';
2236 if (opts.find("totalheight") != opts.end())
2237 special << "totalheight=" << opts["totalheight"] << ',';
2238 if (opts.find("type") != opts.end())
2239 special << "type=" << opts["type"] << ',';
2240 if (opts.find("ext") != opts.end())
2241 special << "ext=" << opts["ext"] << ',';
2242 if (opts.find("read") != opts.end())
2243 special << "read=" << opts["read"] << ',';
2244 if (opts.find("command") != opts.end())
2245 special << "command=" << opts["command"] << ',';
2246 string s_special = special.str();
2247 if (!s_special.empty()) {
2248 // We had special arguments. Remove the trailing ','.
2249 os << "\tspecial " << s_special.substr(0, s_special.size() - 1) << '\n';
2251 // TODO: Handle the unknown settings better.
2252 // Warn about invalid options.
2253 // Check whether some option was given twice.
2257 else if (t.cs() == "footnote" ||
2258 (t.cs() == "thanks" && context.layout->intitle)) {
2260 context.check_layout(os);
2261 begin_inset(os, "Foot\n");
2262 os << "status collapsed\n\n";
2263 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
2267 else if (t.cs() == "marginpar") {
2269 context.check_layout(os);
2270 begin_inset(os, "Marginal\n");
2271 os << "status collapsed\n\n";
2272 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
2276 else if (t.cs() == "ensuremath") {
2278 context.check_layout(os);
2279 string const s = p.verbatim_item();
2280 //FIXME: this never triggers in UTF8
2281 if (s == "\xb1" || s == "\xb3" || s == "\xb2" || s == "\xb5")
2284 handle_ert(os, "\\ensuremath{" + s + "}",
2288 else if (t.cs() == "makeindex" || t.cs() == "maketitle") {
2289 // FIXME: Somehow prevent title layouts if
2290 // "maketitle" was not found
2292 skip_spaces_braces(p);
2295 else if (t.cs() == "tableofcontents") {
2296 context.check_layout(os);
2297 begin_command_inset(os, "toc", "tableofcontents");
2299 skip_spaces_braces(p);
2302 else if (t.cs() == "listoffigures") {
2303 context.check_layout(os);
2304 begin_inset(os, "FloatList figure\n");
2306 skip_spaces_braces(p);
2309 else if (t.cs() == "listoftables") {
2310 context.check_layout(os);
2311 begin_inset(os, "FloatList table\n");
2313 skip_spaces_braces(p);
2316 else if (t.cs() == "listof") {
2317 p.skip_spaces(true);
2318 string const name = p.get_token().cs();
2319 if (context.textclass.floats().typeExist(name)) {
2320 context.check_layout(os);
2321 begin_inset(os, "FloatList ");
2324 p.get_token(); // swallow second arg
2326 handle_ert(os, "\\listof{" + name + "}", context);
2329 else if (t.cs() == "textrm")
2330 parse_text_attributes(p, os, FLAG_ITEM, outer,
2331 context, "\\family",
2332 context.font.family, "roman");
2334 else if (t.cs() == "textsf")
2335 parse_text_attributes(p, os, FLAG_ITEM, outer,
2336 context, "\\family",
2337 context.font.family, "sans");
2339 else if (t.cs() == "texttt")
2340 parse_text_attributes(p, os, FLAG_ITEM, outer,
2341 context, "\\family",
2342 context.font.family, "typewriter");
2344 else if (t.cs() == "textmd")
2345 parse_text_attributes(p, os, FLAG_ITEM, outer,
2346 context, "\\series",
2347 context.font.series, "medium");
2349 else if (t.cs() == "textbf")
2350 parse_text_attributes(p, os, FLAG_ITEM, outer,
2351 context, "\\series",
2352 context.font.series, "bold");
2354 else if (t.cs() == "textup")
2355 parse_text_attributes(p, os, FLAG_ITEM, outer,
2357 context.font.shape, "up");
2359 else if (t.cs() == "textit")
2360 parse_text_attributes(p, os, FLAG_ITEM, outer,
2362 context.font.shape, "italic");
2364 else if (t.cs() == "textsl")
2365 parse_text_attributes(p, os, FLAG_ITEM, outer,
2367 context.font.shape, "slanted");
2369 else if (t.cs() == "textsc")
2370 parse_text_attributes(p, os, FLAG_ITEM, outer,
2372 context.font.shape, "smallcaps");
2374 else if (t.cs() == "textnormal" || t.cs() == "normalfont") {
2375 context.check_layout(os);
2376 TeXFont oldFont = context.font;
2377 context.font.init();
2378 context.font.size = oldFont.size;
2379 os << "\n\\family " << context.font.family << "\n";
2380 os << "\n\\series " << context.font.series << "\n";
2381 os << "\n\\shape " << context.font.shape << "\n";
2382 if (t.cs() == "textnormal") {
2383 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
2384 output_font_change(os, context.font, oldFont);
2385 context.font = oldFont;
2387 eat_whitespace(p, os, context, false);
2390 else if (t.cs() == "textcolor") {
2391 // scheme is \textcolor{color name}{text}
2392 string const color = p.verbatim_item();
2393 // we only support the predefined colors of the color package
2394 if (color == "black" || color == "blue" || color == "cyan"
2395 || color == "green" || color == "magenta" || color == "red"
2396 || color == "white" || color == "yellow") {
2397 context.check_layout(os);
2398 os << "\n\\color " << color << "\n";
2399 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
2400 context.check_layout(os);
2401 os << "\n\\color inherit\n";
2403 // for custom defined colors
2404 handle_ert(os, t.asInput() + "{" + color + "}", context);
2407 else if (t.cs() == "underbar" || t.cs() == "uline") {
2408 // \underbar is not 100% correct (LyX outputs \uline
2409 // of ulem.sty). The difference is that \ulem allows
2410 // line breaks, and \underbar does not.
2411 // Do NOT handle \underline.
2412 // \underbar cuts through y, g, q, p etc.,
2413 // \underline does not.
2414 context.check_layout(os);
2415 os << "\n\\bar under\n";
2416 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
2417 context.check_layout(os);
2418 os << "\n\\bar default\n";
2421 else if (t.cs() == "emph" || t.cs() == "noun") {
2422 context.check_layout(os);
2423 os << "\n\\" << t.cs() << " on\n";
2424 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
2425 context.check_layout(os);
2426 os << "\n\\" << t.cs() << " default\n";
2429 else if (t.cs() == "lyxline") {
2430 context.check_layout(os);
2434 else if (is_known(t.cs(), known_phrases) ||
2435 (t.cs() == "protect" &&
2436 p.next_token().cat() == catEscape &&
2437 is_known(p.next_token().cs(), known_phrases))) {
2438 // LyX sometimes puts a \protect in front, so we have to ignore it
2439 // FIXME: This needs to be changed when bug 4752 is fixed.
2440 char const * const * where = is_known(
2441 t.cs() == "protect" ? p.get_token().cs() : t.cs(),
2443 context.check_layout(os);
2444 os << known_coded_phrases[where - known_phrases];
2445 skip_spaces_braces(p);
2448 else if (is_known(t.cs(), known_ref_commands)) {
2449 string const opt = p.getOpt();
2451 context.check_layout(os);
2452 begin_command_inset(os, "ref", t.cs());
2453 os << "reference \""
2454 << convert_command_inset_arg(p.verbatim_item())
2458 // LyX does not support optional arguments of ref commands
2459 handle_ert(os, t.asInput() + '[' + opt + "]{" +
2460 p.verbatim_item() + "}", context);
2464 else if (use_natbib &&
2465 is_known(t.cs(), known_natbib_commands) &&
2466 ((t.cs() != "citefullauthor" &&
2467 t.cs() != "citeyear" &&
2468 t.cs() != "citeyearpar") ||
2469 p.next_token().asInput() != "*")) {
2470 context.check_layout(os);
2471 string command = t.cs();
2472 if (p.next_token().asInput() == "*") {
2476 if (command == "citefullauthor")
2477 // alternative name for "\\citeauthor*"
2478 command = "citeauthor*";
2480 // text before the citation
2482 // text after the citation
2484 get_cite_arguments(p, true, before, after);
2486 if (command == "cite") {
2487 // \cite without optional argument means
2488 // \citet, \cite with at least one optional
2489 // argument means \citep.
2490 if (before.empty() && after.empty())
2495 if (before.empty() && after == "[]")
2496 // avoid \citet[]{a}
2498 else if (before == "[]" && after == "[]") {
2499 // avoid \citet[][]{a}
2503 // remove the brackets around after and before
2504 if (!after.empty()) {
2506 after.erase(after.length() - 1, 1);
2507 after = convert_command_inset_arg(after);
2509 if (!before.empty()) {
2511 before.erase(before.length() - 1, 1);
2512 before = convert_command_inset_arg(before);
2514 begin_command_inset(os, "citation", command);
2515 os << "after " << '"' << after << '"' << "\n";
2516 os << "before " << '"' << before << '"' << "\n";
2518 << convert_command_inset_arg(p.verbatim_item())
2523 else if (use_jurabib &&
2524 is_known(t.cs(), known_jurabib_commands) &&
2525 (t.cs() == "cite" || p.next_token().asInput() != "*")) {
2526 context.check_layout(os);
2527 string command = t.cs();
2528 if (p.next_token().asInput() == "*") {
2532 char argumentOrder = '\0';
2533 vector<string> const & options = used_packages["jurabib"];
2534 if (find(options.begin(), options.end(),
2535 "natbiborder") != options.end())
2536 argumentOrder = 'n';
2537 else if (find(options.begin(), options.end(),
2538 "jurabiborder") != options.end())
2539 argumentOrder = 'j';
2541 // text before the citation
2543 // text after the citation
2545 get_cite_arguments(p, argumentOrder != 'j', before, after);
2547 string const citation = p.verbatim_item();
2548 if (!before.empty() && argumentOrder == '\0') {
2549 cerr << "Warning: Assuming argument order "
2550 "of jurabib version 0.6 for\n'"
2551 << command << before << after << '{'
2552 << citation << "}'.\n"
2553 "Add 'jurabiborder' to the jurabib "
2554 "package options if you used an\n"
2555 "earlier jurabib version." << endl;
2557 if (!after.empty()) {
2559 after.erase(after.length() - 1, 1);
2561 if (!before.empty()) {
2563 before.erase(before.length() - 1, 1);
2565 begin_command_inset(os, "citation", command);
2566 os << "after " << '"' << after << '"' << "\n";
2567 os << "before " << '"' << before << '"' << "\n";
2568 os << "key " << '"' << citation << '"' << "\n";
2572 else if (t.cs() == "cite"
2573 || t.cs() == "nocite") {
2574 context.check_layout(os);
2575 string after = convert_command_inset_arg(p.getArg('[', ']'));
2576 string key = convert_command_inset_arg(p.verbatim_item());
2577 // store the case that it is "\nocite{*}" to use it later for
2580 begin_command_inset(os, "citation", t.cs());
2581 os << "after " << '"' << after << '"' << "\n";
2582 os << "key " << '"' << key << '"' << "\n";
2584 } else if (t.cs() == "nocite")
2588 else if (t.cs() == "index") {
2589 context.check_layout(os);
2590 begin_inset(os, "Index idx\n");
2591 os << "status collapsed\n";
2592 parse_text_in_inset(p, os, FLAG_ITEM, false, context, "Index");
2596 else if (t.cs() == "nomenclature") {
2597 context.check_layout(os);
2598 begin_command_inset(os, "nomenclature", "nomenclature");
2599 string prefix = convert_command_inset_arg(p.getArg('[', ']'));
2600 if (!prefix.empty())
2601 os << "prefix " << '"' << prefix << '"' << "\n";
2602 os << "symbol " << '"'
2603 << convert_command_inset_arg(p.verbatim_item());
2604 os << "\"\ndescription \""
2605 << convert_command_inset_arg(p.verbatim_item())
2610 else if (t.cs() == "label") {
2611 context.check_layout(os);
2612 begin_command_inset(os, "label", "label");
2614 << convert_command_inset_arg(p.verbatim_item())
2619 else if (t.cs() == "printindex") {
2620 context.check_layout(os);
2621 begin_command_inset(os, "index_print", "printindex");
2622 os << "type \"idx\"\n";
2624 skip_spaces_braces(p);
2627 else if (t.cs() == "printnomenclature") {
2628 context.check_layout(os);
2629 begin_command_inset(os, "nomencl_print", "printnomenclature");
2631 skip_spaces_braces(p);
2634 else if (LYX_FORMAT >= 408 &&
2635 (t.cs() == "textsuperscript" || t.cs() == "textsubscript")) {
2636 context.check_layout(os);
2637 begin_inset(os, "script ");
2638 os << t.cs().substr(4) << '\n';
2639 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
2643 else if (is_known(t.cs(), known_quotes)) {
2644 char const * const * where = is_known(t.cs(), known_quotes);
2645 context.check_layout(os);
2646 begin_inset(os, "Quotes ");
2647 os << known_coded_quotes[where - known_quotes];
2649 // LyX adds {} after the quote, so we have to eat
2650 // spaces here if there are any before a possible
2652 eat_whitespace(p, os, context, false);
2656 else if (is_known(t.cs(), known_sizes) &&
2657 context.new_layout_allowed) {
2658 char const * const * where = is_known(t.cs(), known_sizes);
2659 context.check_layout(os);
2660 TeXFont const oldFont = context.font;
2661 context.font.size = known_coded_sizes[where - known_sizes];
2662 output_font_change(os, oldFont, context.font);
2663 eat_whitespace(p, os, context, false);
2666 else if (is_known(t.cs(), known_font_families) &&
2667 context.new_layout_allowed) {
2668 char const * const * where =
2669 is_known(t.cs(), known_font_families);
2670 context.check_layout(os);
2671 TeXFont const oldFont = context.font;
2672 context.font.family =
2673 known_coded_font_families[where - known_font_families];
2674 output_font_change(os, oldFont, context.font);
2675 eat_whitespace(p, os, context, false);
2678 else if (is_known(t.cs(), known_font_series) &&
2679 context.new_layout_allowed) {
2680 char const * const * where =
2681 is_known(t.cs(), known_font_series);
2682 context.check_layout(os);
2683 TeXFont const oldFont = context.font;
2684 context.font.series =
2685 known_coded_font_series[where - known_font_series];
2686 output_font_change(os, oldFont, context.font);
2687 eat_whitespace(p, os, context, false);
2690 else if (is_known(t.cs(), known_font_shapes) &&
2691 context.new_layout_allowed) {
2692 char const * const * where =
2693 is_known(t.cs(), known_font_shapes);
2694 context.check_layout(os);
2695 TeXFont const oldFont = context.font;
2696 context.font.shape =
2697 known_coded_font_shapes[where - known_font_shapes];
2698 output_font_change(os, oldFont, context.font);
2699 eat_whitespace(p, os, context, false);
2701 else if (is_known(t.cs(), known_old_font_families) &&
2702 context.new_layout_allowed) {
2703 char const * const * where =
2704 is_known(t.cs(), known_old_font_families);
2705 context.check_layout(os);
2706 TeXFont const oldFont = context.font;
2707 context.font.init();
2708 context.font.size = oldFont.size;
2709 context.font.family =
2710 known_coded_font_families[where - known_old_font_families];
2711 output_font_change(os, oldFont, context.font);
2712 eat_whitespace(p, os, context, false);
2715 else if (is_known(t.cs(), known_old_font_series) &&
2716 context.new_layout_allowed) {
2717 char const * const * where =
2718 is_known(t.cs(), known_old_font_series);
2719 context.check_layout(os);
2720 TeXFont const oldFont = context.font;
2721 context.font.init();
2722 context.font.size = oldFont.size;
2723 context.font.series =
2724 known_coded_font_series[where - known_old_font_series];
2725 output_font_change(os, oldFont, context.font);
2726 eat_whitespace(p, os, context, false);
2729 else if (is_known(t.cs(), known_old_font_shapes) &&
2730 context.new_layout_allowed) {
2731 char const * const * where =
2732 is_known(t.cs(), known_old_font_shapes);
2733 context.check_layout(os);
2734 TeXFont const oldFont = context.font;
2735 context.font.init();
2736 context.font.size = oldFont.size;
2737 context.font.shape =
2738 known_coded_font_shapes[where - known_old_font_shapes];
2739 output_font_change(os, oldFont, context.font);
2740 eat_whitespace(p, os, context, false);
2743 else if (t.cs() == "selectlanguage") {
2744 context.check_layout(os);
2745 // save the language for the case that a
2746 // \foreignlanguage is used
2748 context.font.language = babel2lyx(p.verbatim_item());
2749 os << "\n\\lang " << context.font.language << "\n";
2752 else if (t.cs() == "foreignlanguage") {
2753 string const lang = babel2lyx(p.verbatim_item());
2754 parse_text_attributes(p, os, FLAG_ITEM, outer,
2756 context.font.language, lang);
2759 else if (t.cs() == "inputencoding") {
2760 // nothing to write here
2761 string const enc = subst(p.verbatim_item(), "\n", " ");
2765 else if (t.cs() == "ldots") {
2766 context.check_layout(os);
2767 os << "\\SpecialChar \\ldots{}\n";
2768 skip_spaces_braces(p);
2771 else if (t.cs() == "lyxarrow") {
2772 context.check_layout(os);
2773 os << "\\SpecialChar \\menuseparator\n";
2774 skip_spaces_braces(p);
2777 else if (t.cs() == "textcompwordmark") {
2778 context.check_layout(os);
2779 os << "\\SpecialChar \\textcompwordmark{}\n";
2780 skip_spaces_braces(p);
2783 else if (t.cs() == "slash") {
2784 context.check_layout(os);
2785 os << "\\SpecialChar \\slash{}\n";
2786 skip_spaces_braces(p);
2789 else if (t.cs() == "nobreakdash" && p.next_token().asInput() == "-") {
2790 context.check_layout(os);
2791 os << "\\SpecialChar \\nobreakdash-\n";
2795 else if (t.cs() == "textquotedbl") {
2796 context.check_layout(os);
2801 else if (t.cs() == "@" && p.next_token().asInput() == ".") {
2802 context.check_layout(os);
2803 os << "\\SpecialChar \\@.\n";
2807 else if (t.cs() == "-") {
2808 context.check_layout(os);
2809 os << "\\SpecialChar \\-\n";
2812 else if (t.cs() == "textasciitilde") {
2813 context.check_layout(os);
2815 skip_spaces_braces(p);
2818 else if (t.cs() == "textasciicircum") {
2819 context.check_layout(os);
2821 skip_spaces_braces(p);
2824 else if (t.cs() == "textbackslash") {
2825 context.check_layout(os);
2826 os << "\n\\backslash\n";
2827 skip_spaces_braces(p);
2830 else if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#"
2831 || t.cs() == "$" || t.cs() == "{" || t.cs() == "}"
2833 context.check_layout(os);
2837 else if (t.cs() == "char") {
2838 context.check_layout(os);
2839 if (p.next_token().character() == '`') {
2841 if (p.next_token().cs() == "\"") {
2846 handle_ert(os, "\\char`", context);
2849 handle_ert(os, "\\char", context);
2853 else if (t.cs() == "verb") {
2854 context.check_layout(os);
2855 char const delimiter = p.next_token().character();
2856 string const arg = p.getArg(delimiter, delimiter);
2858 oss << "\\verb" << delimiter << arg << delimiter;
2859 handle_ert(os, oss.str(), context);
2862 // Problem: \= creates a tabstop inside the tabbing environment
2863 // and else an accent. In the latter case we really would want
2864 // \={o} instead of \= o.
2865 else if (t.cs() == "=" && (flags & FLAG_TABBING))
2866 handle_ert(os, t.asInput(), context);
2868 // accents (see Table 6 in Comprehensive LaTeX Symbol List)
2869 else if (t.cs().size() == 1
2870 && contains("\"'.=^`bcdHkrtuv~", t.cs())) {
2871 context.check_layout(os);
2872 // try to see whether the string is in unicodesymbols
2874 string command = t.asInput() + "{"
2875 + trimSpaceAndEol(p.verbatim_item())
2877 docstring s = encodings.fromLaTeXCommand(from_utf8(command), rem);
2880 cerr << "When parsing " << command
2881 << ", result is " << to_utf8(s)
2882 << "+" << to_utf8(rem) << endl;
2885 // we did not find a non-ert version
2886 handle_ert(os, command, context);
2889 else if (t.cs() == "\\") {
2890 context.check_layout(os);
2892 handle_ert(os, "\\\\" + p.getOpt(), context);
2893 else if (p.next_token().asInput() == "*") {
2895 // getOpt() eats the following space if there
2896 // is no optional argument, but that is OK
2897 // here since it has no effect in the output.
2898 handle_ert(os, "\\\\*" + p.getOpt(), context);
2901 begin_inset(os, "Newline newline");
2906 else if (t.cs() == "newline" ||
2907 (t.cs() == "linebreak" && !p.hasOpt())) {
2908 context.check_layout(os);
2909 begin_inset(os, "Newline ");
2912 skip_spaces_braces(p);
2915 else if (t.cs() == "input" || t.cs() == "include"
2916 || t.cs() == "verbatiminput") {
2917 string name = t.cs();
2918 if (t.cs() == "verbatiminput"
2919 && p.next_token().asInput() == "*")
2920 name += p.get_token().asInput();
2921 context.check_layout(os);
2922 string filename(normalize_filename(p.getArg('{', '}')));
2923 string const path = getMasterFilePath();
2924 // We want to preserve relative / absolute filenames,
2925 // therefore path is only used for testing
2926 if ((t.cs() == "include" || t.cs() == "input") &&
2927 !makeAbsPath(filename, path).exists()) {
2928 // The file extension is probably missing.
2929 // Now try to find it out.
2930 string const tex_name =
2931 find_file(filename, path,
2932 known_tex_extensions);
2933 if (!tex_name.empty())
2934 filename = tex_name;
2936 bool external = false;
2938 if (makeAbsPath(filename, path).exists()) {
2939 string const abstexname =
2940 makeAbsPath(filename, path).absFileName();
2941 string const abslyxname =
2942 changeExtension(abstexname, ".lyx");
2943 string const absfigname =
2944 changeExtension(abstexname, ".fig");
2945 fix_relative_filename(filename);
2946 string const lyxname =
2947 changeExtension(filename, ".lyx");
2949 external = FileName(absfigname).exists();
2950 if (t.cs() == "input") {
2951 string const ext = getExtension(abstexname);
2953 // Combined PS/LaTeX:
2954 // x.eps, x.pstex_t (old xfig)
2955 // x.pstex, x.pstex_t (new xfig, e.g. 3.2.5)
2956 FileName const absepsname(
2957 changeExtension(abstexname, ".eps"));
2958 FileName const abspstexname(
2959 changeExtension(abstexname, ".pstex"));
2960 bool const xfigeps =
2961 (absepsname.exists() ||
2962 abspstexname.exists()) &&
2965 // Combined PDF/LaTeX:
2966 // x.pdf, x.pdftex_t (old xfig)
2967 // x.pdf, x.pdf_t (new xfig, e.g. 3.2.5)
2968 FileName const abspdfname(
2969 changeExtension(abstexname, ".pdf"));
2970 bool const xfigpdf =
2971 abspdfname.exists() &&
2972 (ext == "pdftex_t" || ext == "pdf_t");
2976 // Combined PS/PDF/LaTeX:
2977 // x_pspdftex.eps, x_pspdftex.pdf, x.pspdftex
2978 string const absbase2(
2979 removeExtension(abstexname) + "_pspdftex");
2980 FileName const abseps2name(
2981 addExtension(absbase2, ".eps"));
2982 FileName const abspdf2name(
2983 addExtension(absbase2, ".pdf"));
2984 bool const xfigboth =
2985 abspdf2name.exists() &&
2986 abseps2name.exists() && ext == "pspdftex";
2988 xfig = xfigpdf || xfigeps || xfigboth;
2989 external = external && xfig;
2992 outname = changeExtension(filename, ".fig");
2994 // Don't try to convert, the result
2995 // would be full of ERT.
2997 } else if (t.cs() != "verbatiminput" &&
2998 tex2lyx(abstexname, FileName(abslyxname),
3005 cerr << "Warning: Could not find included file '"
3006 << filename << "'." << endl;
3010 begin_inset(os, "External\n");
3011 os << "\ttemplate XFig\n"
3012 << "\tfilename " << outname << '\n';
3014 begin_command_inset(os, "include", name);
3015 os << "preview false\n"
3016 "filename \"" << outname << "\"\n";
3021 else if (t.cs() == "bibliographystyle") {
3022 // store new bibliographystyle
3023 bibliographystyle = p.verbatim_item();
3024 // If any other command than \bibliography and
3025 // \nocite{*} follows, we need to output the style
3026 // (because it might be used by that command).
3027 // Otherwise, it will automatically be output by LyX.
3030 for (Token t2 = p.get_token(); p.good(); t2 = p.get_token()) {
3031 if (t2.cat() == catBegin)
3033 if (t2.cat() != catEscape)
3035 if (t2.cs() == "nocite") {
3036 if (p.getArg('{', '}') == "*")
3038 } else if (t2.cs() == "bibliography")
3045 "\\bibliographystyle{" + bibliographystyle + '}',
3050 else if (t.cs() == "bibliography") {
3051 context.check_layout(os);
3052 begin_command_inset(os, "bibtex", "bibtex");
3053 if (!btprint.empty()) {
3054 os << "btprint " << '"' << "btPrintAll" << '"' << "\n";
3055 // clear the string because the next BibTeX inset can be without the
3056 // \nocite{*} option
3059 os << "bibfiles " << '"' << p.verbatim_item() << '"' << "\n";
3060 // Do we have a bibliographystyle set?
3061 if (!bibliographystyle.empty())
3062 os << "options " << '"' << bibliographystyle << '"' << "\n";
3066 else if (t.cs() == "parbox")
3067 parse_box(p, os, 0, FLAG_ITEM, outer, context, "", "", t.cs());
3069 else if (t.cs() == "ovalbox" || t.cs() == "Ovalbox" ||
3070 t.cs() == "shadowbox" || t.cs() == "doublebox")
3071 parse_outer_box(p, os, FLAG_ITEM, outer, context, t.cs(), "");
3073 else if (t.cs() == "framebox") {
3074 string special = p.getFullOpt();
3075 special += p.getOpt();
3076 parse_outer_box(p, os, FLAG_ITEM, outer, context, t.cs(), special);
3079 //\makebox() is part of the picture environment and different from \makebox{}
3080 //\makebox{} will be parsed by parse_box when bug 2956 is fixed
3081 else if (t.cs() == "makebox") {
3082 string arg = t.asInput();
3083 if (p.next_token().character() == '(')
3084 //the syntax is: \makebox(x,y)[position]{content}
3085 arg += p.getFullParentheseArg();
3087 //the syntax is: \makebox[width][position]{content}
3088 arg += p.getFullOpt();
3089 handle_ert(os, arg + p.getFullOpt(), context);
3092 else if (t.cs() == "smallskip" ||
3093 t.cs() == "medskip" ||
3094 t.cs() == "bigskip" ||
3095 t.cs() == "vfill") {
3096 context.check_layout(os);
3097 begin_inset(os, "VSpace ");
3100 skip_spaces_braces(p);
3103 else if (is_known(t.cs(), known_spaces)) {
3104 char const * const * where = is_known(t.cs(), known_spaces);
3105 context.check_layout(os);
3106 begin_inset(os, "space ");
3107 os << '\\' << known_coded_spaces[where - known_spaces]
3110 // LaTeX swallows whitespace after all spaces except
3111 // "\\,". We have to do that here, too, because LyX
3112 // adds "{}" which would make the spaces significant.
3114 eat_whitespace(p, os, context, false);
3115 // LyX adds "{}" after all spaces except "\\ " and
3116 // "\\,", so we have to remove "{}".
3117 // "\\,{}" is equivalent to "\\," in LaTeX, so we
3118 // remove the braces after "\\,", too.
3123 else if (t.cs() == "newpage" ||
3124 (t.cs() == "pagebreak" && !p.hasOpt()) ||
3125 t.cs() == "clearpage" ||
3126 t.cs() == "cleardoublepage") {
3127 context.check_layout(os);
3128 begin_inset(os, "Newpage ");
3131 skip_spaces_braces(p);
3134 else if (t.cs() == "DeclareRobustCommand" ||
3135 t.cs() == "DeclareRobustCommandx" ||
3136 t.cs() == "newcommand" ||
3137 t.cs() == "newcommandx" ||
3138 t.cs() == "providecommand" ||
3139 t.cs() == "providecommandx" ||
3140 t.cs() == "renewcommand" ||
3141 t.cs() == "renewcommandx") {
3142 // DeclareRobustCommand, DeclareRobustCommandx,
3143 // providecommand and providecommandx could be handled
3144 // by parse_command(), but we need to call
3145 // add_known_command() here.
3146 string name = t.asInput();
3147 if (p.next_token().asInput() == "*") {
3148 // Starred form. Eat '*'
3152 string const command = p.verbatim_item();
3153 string const opt1 = p.getFullOpt();
3154 string const opt2 = p.getFullOpt();
3155 add_known_command(command, opt1, !opt2.empty());
3156 string const ert = name + '{' + command + '}' +
3158 '{' + p.verbatim_item() + '}';
3160 if (t.cs() == "DeclareRobustCommand" ||
3161 t.cs() == "DeclareRobustCommandx" ||
3162 t.cs() == "providecommand" ||
3163 t.cs() == "providecommandx" ||
3164 name[name.length()-1] == '*')
3165 handle_ert(os, ert, context);
3167 context.check_layout(os);
3168 begin_inset(os, "FormulaMacro");
3174 else if (t.cs() == "let" && p.next_token().asInput() != "*") {
3175 // let could be handled by parse_command(),
3176 // but we need to call add_known_command() here.
3177 string ert = t.asInput();
3180 if (p.next_token().cat() == catBegin) {
3181 name = p.verbatim_item();
3182 ert += '{' + name + '}';
3184 name = p.verbatim_item();
3189 if (p.next_token().cat() == catBegin) {
3190 command = p.verbatim_item();
3191 ert += '{' + command + '}';
3193 command = p.verbatim_item();
3196 // If command is known, make name known too, to parse
3197 // its arguments correctly. For this reason we also
3198 // have commands in syntax.default that are hardcoded.
3199 CommandMap::iterator it = known_commands.find(command);
3200 if (it != known_commands.end())
3201 known_commands[t.asInput()] = it->second;
3202 handle_ert(os, ert, context);
3205 else if (t.cs() == "hspace" || t.cs() == "vspace") {
3206 bool starred = false;
3207 if (p.next_token().asInput() == "*") {
3211 string name = t.asInput();
3212 string const length = p.verbatim_item();
3215 bool valid = splitLatexLength(length, valstring, unit);
3216 bool known_hspace = false;
3217 bool known_vspace = false;
3218 bool known_unit = false;
3221 istringstream iss(valstring);
3224 if (t.cs()[0] == 'h') {
3225 if (unit == "\\fill") {
3230 known_hspace = true;
3233 if (unit == "\\smallskipamount") {
3235 known_vspace = true;
3236 } else if (unit == "\\medskipamount") {
3238 known_vspace = true;
3239 } else if (unit == "\\bigskipamount") {
3241 known_vspace = true;
3242 } else if (unit == "\\fill") {
3244 known_vspace = true;
3248 if (!known_hspace && !known_vspace) {
3249 switch (unitFromString(unit)) {
3270 if (t.cs()[0] == 'h' && (known_unit || known_hspace)) {
3271 // Literal horizontal length or known variable
3272 context.check_layout(os);
3273 begin_inset(os, "space ");
3281 if (known_unit && !known_hspace)
3283 << translate_len(length);
3285 } else if (known_unit || known_vspace) {
3286 // Literal vertical length or known variable
3287 context.check_layout(os);
3288 begin_inset(os, "VSpace ");
3296 // LyX can't handle other length variables in Inset VSpace/space
3301 handle_ert(os, name + '{' + unit + '}', context);
3302 else if (value == -1.0)
3303 handle_ert(os, name + "{-" + unit + '}', context);
3305 handle_ert(os, name + '{' + valstring + unit + '}', context);
3307 handle_ert(os, name + '{' + length + '}', context);
3311 // The single '=' is meant here.
3312 else if ((newinsetlayout = findInsetLayout(context.textclass, t.cs(), true))) {
3314 context.check_layout(os);
3315 begin_inset(os, "Flex ");
3316 os << to_utf8(newinsetlayout->name()) << '\n'
3317 << "status collapsed\n";
3318 parse_text_in_inset(p, os, FLAG_ITEM, false, context, newinsetlayout);
3323 // try to see whether the string is in unicodesymbols
3324 // Only use text mode commands, since we are in text mode here,
3325 // and math commands may be invalid (bug 6797)
3327 docstring s = encodings.fromLaTeXCommand(from_utf8(t.asInput()),
3328 rem, Encodings::TEXT_CMD);
3331 cerr << "When parsing " << t.cs()
3332 << ", result is " << to_utf8(s)
3333 << "+" << to_utf8(rem) << endl;
3334 context.check_layout(os);
3336 skip_spaces_braces(p);
3338 //cerr << "#: " << t << " mode: " << mode << endl;
3339 // heuristic: read up to next non-nested space
3341 string s = t.asInput();
3342 string z = p.verbatim_item();
3343 while (p.good() && z != " " && z.size()) {
3344 //cerr << "read: " << z << endl;
3346 z = p.verbatim_item();
3348 cerr << "found ERT: " << s << endl;
3349 handle_ert(os, s + ' ', context);
3352 string name = t.asInput();
3353 if (p.next_token().asInput() == "*") {
3354 // Starred commands like \vspace*{}
3355 p.get_token(); // Eat '*'
3358 if (!parse_command(name, p, os, outer, context))
3359 handle_ert(os, name, context);
3363 if (flags & FLAG_LEAVE) {
3364 flags &= ~FLAG_LEAVE;