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)
46 Context newcontext(true, context.textclass);
47 newcontext.font = context.font;
48 parse_text(p, os, flags, outer, newcontext);
49 newcontext.check_end_layout(os);
55 /// parses a paragraph snippet, useful for example for \\emph{...}
56 void parse_text_snippet(Parser & p, ostream & os, unsigned flags, bool outer,
59 Context newcontext(context);
60 // Don't inherit the paragraph-level extra stuff
61 newcontext.par_extra_stuff.clear();
62 parse_text(p, os, flags, outer, newcontext);
63 // Make sure that we don't create invalid .lyx files
64 context.need_layout = newcontext.need_layout;
65 context.need_end_layout = newcontext.need_end_layout;
70 * Thin wrapper around parse_text_snippet() using a string.
72 * We completely ignore \c context.need_layout and \c context.need_end_layout,
73 * because our return value is not used directly (otherwise the stream version
74 * of parse_text_snippet() could be used). That means that the caller needs
75 * to do layout management manually.
76 * This is intended to parse text that does not create any layout changes.
78 string parse_text_snippet(Parser & p, unsigned flags, const bool outer,
81 Context newcontext(context);
82 newcontext.need_layout = false;
83 newcontext.need_end_layout = false;
84 newcontext.new_layout_allowed = false;
85 // Avoid warning by Context::~Context()
86 newcontext.par_extra_stuff.clear();
88 parse_text_snippet(p, os, flags, outer, newcontext);
93 char const * const known_latex_commands[] = { "ref", "cite", "label",
94 "index", "printindex", "pageref", "url", "vref", "vpageref", "prettyref",
99 * We can't put these into known_latex_commands because the argument order
100 * is reversed in lyx if there are 2 arguments.
101 * The starred forms are also known.
103 char const * const known_natbib_commands[] = { "cite", "citet", "citep",
104 "citealt", "citealp", "citeauthor", "citeyear", "citeyearpar",
105 "citefullauthor", "Citet", "Citep", "Citealt", "Citealp", "Citeauthor", 0 };
109 * We can't put these into known_latex_commands because the argument order
110 * is reversed in lyx if there are 2 arguments.
111 * No starred form other than "cite*" known.
113 char const * const known_jurabib_commands[] = { "cite", "citet", "citep",
114 "citealt", "citealp", "citeauthor", "citeyear", "citeyearpar",
115 // jurabib commands not (yet) supported by LyX:
117 // "footcite", "footcitet", "footcitep", "footcitealt", "footcitealp",
118 // "footciteauthor", "footciteyear", "footciteyearpar",
119 "citefield", "citetitle", "cite*", 0 };
121 /// LaTeX names for quotes
122 char const * const known_quotes[] = { "dq", "guillemotleft", "flqq", "og",
123 "guillemotright", "frqq", "fg", "glq", "glqq", "textquoteleft", "grq", "grqq",
124 "quotedblbase", "textquotedblleft", "quotesinglbase", "textquoteright", "flq",
125 "guilsinglleft", "frq", "guilsinglright", 0};
127 /// the same as known_quotes with .lyx names
128 char const * const known_coded_quotes[] = { "prd", "ard", "ard", "ard",
129 "ald", "ald", "ald", "gls", "gld", "els", "els", "grd",
130 "gld", "grd", "gls", "ers", "fls",
131 "fls", "frs", "frs", 0};
133 /// LaTeX names for font sizes
134 char const * const known_sizes[] = { "tiny", "scriptsize", "footnotesize",
135 "small", "normalsize", "large", "Large", "LARGE", "huge", "Huge", 0};
137 /// the same as known_sizes with .lyx names plus a default entry
138 char const * const known_coded_sizes[] = { "default", "tiny", "scriptsize", "footnotesize",
139 "small", "normal", "large", "larger", "largest", "huge", "giant", 0};
141 /// LaTeX 2.09 names for font families
142 char const * const known_old_font_families[] = { "rm", "sf", "tt", 0};
144 /// LaTeX names for font families
145 char const * const known_font_families[] = { "rmfamily", "sffamily",
148 /// the same as known_old_font_families and known_font_families with .lyx names
149 char const * const known_coded_font_families[] = { "roman", "sans",
152 /// LaTeX 2.09 names for font series
153 char const * const known_old_font_series[] = { "bf", 0};
155 /// LaTeX names for font series
156 char const * const known_font_series[] = { "bfseries", "mdseries", 0};
158 /// the same as known_old_font_series and known_font_series with .lyx names
159 char const * const known_coded_font_series[] = { "bold", "medium", 0};
161 /// LaTeX 2.09 names for font shapes
162 char const * const known_old_font_shapes[] = { "it", "sl", "sc", 0};
164 /// LaTeX names for font shapes
165 char const * const known_font_shapes[] = { "itshape", "slshape", "scshape",
168 /// the same as known_old_font_shapes and known_font_shapes with .lyx names
169 char const * const known_coded_font_shapes[] = { "italic", "slanted",
170 "smallcaps", "up", 0};
173 * Graphics file extensions known by the dvips driver of the graphics package.
174 * These extensions are used to complete the filename of an included
175 * graphics file if it does not contain an extension.
176 * The order must be the same that latex uses to find a file, because we
177 * will use the first extension that matches.
178 * This is only an approximation for the common cases. If we would want to
179 * do it right in all cases, we would need to know which graphics driver is
180 * used and know the extensions of every driver of the graphics package.
182 char const * const known_dvips_graphics_formats[] = {"eps", "ps", "eps.gz",
183 "ps.gz", "eps.Z", "ps.Z", 0};
186 * Graphics file extensions known by the pdftex driver of the graphics package.
187 * \sa known_dvips_graphics_formats
189 char const * const known_pdftex_graphics_formats[] = {"png", "pdf", "jpg",
193 * Known file extensions for TeX files as used by \\include.
195 char const * const known_tex_extensions[] = {"tex", 0};
197 /// spaces known by InsetSpace
198 char const * const known_spaces[] = { " ", "space", ",", "thinspace", "quad",
199 "qquad", "enspace", "enskip", "negthinspace", 0};
201 /// the same as known_spaces with .lyx names
202 char const * const known_coded_spaces[] = { "space{}", "space{}",
203 "thinspace{}", "thinspace{}", "quad{}", "qquad{}", "enspace{}", "enskip{}",
204 "negthinspace{}", 0};
207 /// splits "x=z, y=b" into a map
208 map<string, string> split_map(string const & s)
210 map<string, string> res;
213 for (size_t i = 0; i < v.size(); ++i) {
214 size_t const pos = v[i].find('=');
215 string const index = v[i].substr(0, pos);
216 string const value = v[i].substr(pos + 1, string::npos);
217 res[trim(index)] = trim(value);
224 * Split a LaTeX length into value and unit.
225 * The latter can be a real unit like "pt", or a latex length variable
226 * like "\textwidth". The unit may contain additional stuff like glue
227 * lengths, but we don't care, because such lengths are ERT anyway.
228 * \returns true if \p value and \p unit are valid.
230 bool splitLatexLength(string const & len, string & value, string & unit)
234 const string::size_type i = len.find_first_not_of(" -+0123456789.,");
235 //'4,5' is a valid LaTeX length number. Change it to '4.5'
236 string const length = subst(len, ',', '.');
237 if (i == string::npos)
240 if (len[0] == '\\') {
241 // We had something like \textwidth without a factor
247 value = trim(string(length, 0, i));
251 // 'cM' is a valid LaTeX length unit. Change it to 'cm'
252 if (contains(len, '\\'))
253 unit = trim(string(len, i));
255 unit = ascii_lowercase(trim(string(len, i)));
260 /// A simple function to translate a latex length to something lyx can
261 /// understand. Not perfect, but rather best-effort.
262 bool translate_len(string const & length, string & valstring, string & unit)
264 if (!splitLatexLength(length, valstring, unit))
266 // LyX uses percent values
268 istringstream iss(valstring);
273 string const percentval = oss.str();
275 if (unit.empty() || unit[0] != '\\')
277 string::size_type const i = unit.find(' ');
278 string const endlen = (i == string::npos) ? string() : string(unit, i);
279 if (unit == "\\textwidth") {
280 valstring = percentval;
281 unit = "text%" + endlen;
282 } else if (unit == "\\columnwidth") {
283 valstring = percentval;
284 unit = "col%" + endlen;
285 } else if (unit == "\\paperwidth") {
286 valstring = percentval;
287 unit = "page%" + endlen;
288 } else if (unit == "\\linewidth") {
289 valstring = percentval;
290 unit = "line%" + endlen;
291 } else if (unit == "\\paperheight") {
292 valstring = percentval;
293 unit = "pheight%" + endlen;
294 } else if (unit == "\\textheight") {
295 valstring = percentval;
296 unit = "theight%" + endlen;
304 string translate_len(string const & length)
308 if (translate_len(length, value, unit))
310 // If the input is invalid, return what we have.
318 * Translates a LaTeX length into \p value, \p unit and
319 * \p special parts suitable for a box inset.
320 * The difference from translate_len() is that a box inset knows about
321 * some special "units" that are stored in \p special.
323 void translate_box_len(string const & length, string & value, string & unit, string & special)
325 if (translate_len(length, value, unit)) {
326 if (unit == "\\height" || unit == "\\depth" ||
327 unit == "\\totalheight" || unit == "\\width") {
328 special = unit.substr(1);
329 // The unit is not used, but LyX requires a dummy setting
342 * Find a file with basename \p name in path \p path and an extension
345 string find_file(string const & name, string const & path,
346 char const * const * extensions)
348 for (char const * const * what = extensions; *what; ++what) {
349 string const trial = addExtension(name, *what);
350 if (makeAbsPath(trial, path).exists())
357 void begin_inset(ostream & os, string const & name)
359 os << "\n\\begin_inset " << name;
363 void end_inset(ostream & os)
365 os << "\n\\end_inset\n\n";
369 void skip_braces(Parser & p)
371 if (p.next_token().cat() != catBegin)
374 if (p.next_token().cat() == catEnd) {
382 void handle_ert(ostream & os, string const & s, Context & context)
384 // We must have a valid layout before outputting the ERT inset.
385 context.check_layout(os);
386 Context newcontext(true, context.textclass);
387 begin_inset(os, "ERT");
388 os << "\nstatus collapsed\n";
389 newcontext.check_layout(os);
390 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
392 os << "\n\\backslash\n";
393 else if (*it == '\n') {
394 newcontext.new_paragraph(os);
395 newcontext.check_layout(os);
399 newcontext.check_end_layout(os);
404 void handle_comment(ostream & os, string const & s, Context & context)
406 // TODO: Handle this better
407 Context newcontext(true, context.textclass);
408 begin_inset(os, "ERT");
409 os << "\nstatus collapsed\n";
410 newcontext.check_layout(os);
411 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
413 os << "\n\\backslash\n";
417 // make sure that our comment is the last thing on the line
418 newcontext.new_paragraph(os);
419 newcontext.check_layout(os);
420 newcontext.check_end_layout(os);
425 Layout const * findLayout(TextClass const & textclass, string const & name)
427 DocumentClass::const_iterator lit = textclass.begin();
428 DocumentClass::const_iterator len = textclass.end();
429 for (; lit != len; ++lit)
430 if (lit->latexname() == name)
436 void eat_whitespace(Parser &, ostream &, Context &, bool);
439 Layout * captionlayout()
441 static Layout * lay = 0;
444 lay->name_ = from_ascii("Caption");
445 lay->latexname_ = "caption";
446 lay->latextype = LATEX_COMMAND;
447 lay->optionalargs = 1;
453 void output_command_layout(ostream & os, Parser & p, bool outer,
454 Context & parent_context,
455 Layout const * newlayout)
457 parent_context.check_end_layout(os);
458 Context context(true, parent_context.textclass, newlayout,
459 parent_context.layout, parent_context.font);
460 if (parent_context.deeper_paragraph) {
461 // We are beginning a nested environment after a
462 // deeper paragraph inside the outer list environment.
463 // Therefore we don't need to output a "begin deeper".
464 context.need_end_deeper = true;
466 context.check_deeper(os);
467 context.check_layout(os);
468 if (context.layout->optionalargs > 0) {
469 eat_whitespace(p, os, context, false);
470 if (p.next_token().character() == '[') {
471 p.get_token(); // eat '['
472 begin_inset(os, "OptArg\n");
473 os << "status collapsed\n\n";
474 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
476 eat_whitespace(p, os, context, false);
479 parse_text(p, os, FLAG_ITEM, outer, context);
480 context.check_end_layout(os);
481 if (parent_context.deeper_paragraph) {
482 // We must suppress the "end deeper" because we
483 // suppressed the "begin deeper" above.
484 context.need_end_deeper = false;
486 context.check_end_deeper(os);
487 // We don't need really a new paragraph, but
488 // we must make sure that the next item gets a \begin_layout.
489 parent_context.new_paragraph(os);
494 * Output a space if necessary.
495 * This function gets called for every whitespace token.
497 * We have three cases here:
498 * 1. A space must be suppressed. Example: The lyxcode case below
499 * 2. A space may be suppressed. Example: Spaces before "\par"
500 * 3. A space must not be suppressed. Example: A space between two words
502 * We currently handle only 1. and 3 and from 2. only the case of
503 * spaces before newlines as a side effect.
505 * 2. could be used to suppress as many spaces as possible. This has two effects:
506 * - Reimporting LyX generated LaTeX files changes almost no whitespace
507 * - Superflous whitespace from non LyX generated LaTeX files is removed.
508 * The drawback is that the logic inside the function becomes
509 * complicated, and that is the reason why it is not implemented.
511 void check_space(Parser & p, ostream & os, Context & context)
513 Token const next = p.next_token();
514 Token const curr = p.curr_token();
515 // A space before a single newline and vice versa must be ignored
516 // LyX emits a newline before \end{lyxcode}.
517 // This newline must be ignored,
518 // otherwise LyX will add an additional protected space.
519 if (next.cat() == catSpace ||
520 next.cat() == catNewline ||
521 (next.cs() == "end" && context.layout->free_spacing && curr.cat() == catNewline)) {
524 context.check_layout(os);
530 * Parse all arguments of \p command
532 void parse_arguments(string const & command,
533 vector<ArgumentType> const & template_arguments,
534 Parser & p, ostream & os, bool outer, Context & context)
536 string ert = command;
537 size_t no_arguments = template_arguments.size();
538 for (size_t i = 0; i < no_arguments; ++i) {
539 switch (template_arguments[i]) {
541 // This argument contains regular LaTeX
542 handle_ert(os, ert + '{', context);
543 eat_whitespace(p, os, context, false);
544 parse_text(p, os, FLAG_ITEM, outer, context);
548 // This argument may contain special characters
549 ert += '{' + p.verbatim_item() + '}';
556 handle_ert(os, ert, context);
561 * Check whether \p command is a known command. If yes,
562 * handle the command with all arguments.
563 * \return true if the command was parsed, false otherwise.
565 bool parse_command(string const & command, Parser & p, ostream & os,
566 bool outer, Context & context)
568 if (known_commands.find(command) != known_commands.end()) {
569 parse_arguments(command, known_commands[command], p, os,
577 /// Parses a minipage or parbox
578 void parse_box(Parser & p, ostream & os, unsigned flags, bool outer,
579 Context & parent_context, bool use_parbox)
583 // We need to set the height to the LaTeX default of 1\\totalheight
584 // for the case when no height argument is given
585 string height_value = "1";
586 string height_unit = "in";
587 string height_special = "totalheight";
589 if (p.next_token().asInput() == "[") {
590 position = p.getArg('[', ']');
591 if (position != "t" && position != "c" && position != "b") {
593 cerr << "invalid position for minipage/parbox" << endl;
595 if (p.next_token().asInput() == "[") {
596 latex_height = p.getArg('[', ']');
597 translate_box_len(latex_height, height_value, height_unit, height_special);
599 if (p.next_token().asInput() == "[") {
600 inner_pos = p.getArg('[', ']');
601 if (inner_pos != "c" && inner_pos != "t" &&
602 inner_pos != "b" && inner_pos != "s") {
603 inner_pos = position;
604 cerr << "invalid inner_pos for minipage/parbox"
612 string const latex_width = p.verbatim_item();
613 translate_len(latex_width, width_value, width_unit);
614 if (contains(width_unit, '\\') || contains(height_unit, '\\')) {
615 // LyX can't handle length variables
620 ss << "\\begin{minipage}";
621 if (!position.empty())
622 ss << '[' << position << ']';
623 if (!latex_height.empty())
624 ss << '[' << latex_height << ']';
625 if (!inner_pos.empty())
626 ss << '[' << inner_pos << ']';
627 ss << "{" << latex_width << "}";
630 handle_ert(os, ss.str(), parent_context);
631 parent_context.new_paragraph(os);
632 parse_text_in_inset(p, os, flags, outer, parent_context);
634 handle_ert(os, "}", parent_context);
636 handle_ert(os, "\\end{minipage}", parent_context);
638 // LyX does not like empty positions, so we have
639 // to set them to the LaTeX default values here.
640 if (position.empty())
642 if (inner_pos.empty())
643 inner_pos = position;
644 parent_context.check_layout(os);
645 begin_inset(os, "Box Frameless\n");
646 os << "position \"" << position << "\"\n";
647 os << "hor_pos \"c\"\n";
648 os << "has_inner_box 1\n";
649 os << "inner_pos \"" << inner_pos << "\"\n";
650 os << "use_parbox " << use_parbox << "\n";
651 os << "width \"" << width_value << width_unit << "\"\n";
652 os << "special \"none\"\n";
653 os << "height \"" << height_value << height_unit << "\"\n";
654 os << "height_special \"" << height_special << "\"\n";
655 os << "status open\n\n";
656 parse_text_in_inset(p, os, flags, outer, parent_context);
658 #ifdef PRESERVE_LAYOUT
659 // lyx puts a % after the end of the minipage
660 if (p.next_token().cat() == catNewline && p.next_token().cs().size() > 1) {
662 //handle_comment(os, "%dummy", parent_context);
665 parent_context.new_paragraph(os);
667 else if (p.next_token().cat() == catSpace || p.next_token().cat() == catNewline) {
668 //handle_comment(os, "%dummy", parent_context);
671 // We add a protected space if something real follows
672 if (p.good() && p.next_token().cat() != catComment) {
673 os << "\\InsetSpace ~\n";
681 /// parse an unknown environment
682 void parse_unknown_environment(Parser & p, string const & name, ostream & os,
683 unsigned flags, bool outer,
684 Context & parent_context)
686 if (name == "tabbing")
687 // We need to remember that we have to handle '\=' specially
688 flags |= FLAG_TABBING;
690 // We need to translate font changes and paragraphs inside the
691 // environment to ERT if we have a non standard font.
692 // Otherwise things like
693 // \large\begin{foo}\huge bar\end{foo}
695 bool const specialfont =
696 (parent_context.font != parent_context.normalfont);
697 bool const new_layout_allowed = parent_context.new_layout_allowed;
699 parent_context.new_layout_allowed = false;
700 handle_ert(os, "\\begin{" + name + "}", parent_context);
701 parse_text_snippet(p, os, flags, outer, parent_context);
702 handle_ert(os, "\\end{" + name + "}", parent_context);
704 parent_context.new_layout_allowed = new_layout_allowed;
708 void parse_environment(Parser & p, ostream & os, bool outer,
709 Context & parent_context)
711 Layout const * newlayout;
712 string const name = p.getArg('{', '}');
713 const bool is_starred = suffixIs(name, '*');
714 string const unstarred_name = rtrim(name, "*");
715 active_environments.push_back(name);
717 if (is_math_env(name)) {
718 parent_context.check_layout(os);
719 begin_inset(os, "Formula ");
720 os << "\\begin{" << name << "}";
721 parse_math(p, os, FLAG_END, MATH_MODE);
722 os << "\\end{" << name << "}";
726 else if (name == "tabular" || name == "longtable") {
727 eat_whitespace(p, os, parent_context, false);
728 parent_context.check_layout(os);
729 begin_inset(os, "Tabular ");
730 handle_tabular(p, os, name == "longtable", parent_context);
735 else if (parent_context.textclass.floats().typeExist(unstarred_name)) {
736 eat_whitespace(p, os, parent_context, false);
737 parent_context.check_layout(os);
738 begin_inset(os, "Float " + unstarred_name + "\n");
739 if (p.next_token().asInput() == "[") {
740 os << "placement " << p.getArg('[', ']') << '\n';
742 os << "wide " << convert<string>(is_starred)
743 << "\nsideways false"
744 << "\nstatus open\n\n";
745 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
747 // We don't need really a new paragraph, but
748 // we must make sure that the next item gets a \begin_layout.
749 parent_context.new_paragraph(os);
753 else if (name == "minipage") {
754 eat_whitespace(p, os, parent_context, false);
755 parse_box(p, os, FLAG_END, outer, parent_context, false);
759 else if (name == "comment") {
760 eat_whitespace(p, os, parent_context, false);
761 parent_context.check_layout(os);
762 begin_inset(os, "Note Comment\n");
763 os << "status open\n";
764 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
767 skip_braces(p); // eat {} that might by set by LyX behind comments
770 else if (name == "lyxgreyedout") {
771 eat_whitespace(p, os, parent_context, false);
772 parent_context.check_layout(os);
773 begin_inset(os, "Note Greyedout\n");
774 os << "status open\n";
775 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
780 else if (name == "framed") {
781 eat_whitespace(p, os, parent_context, false);
782 parent_context.check_layout(os);
783 begin_inset(os, "Note Framed\n");
784 os << "status open\n";
785 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
790 else if (name == "shaded") {
791 eat_whitespace(p, os, parent_context, false);
792 parent_context.check_layout(os);
793 begin_inset(os, "Note Shaded\n");
794 os << "status open\n";
795 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
800 else if (!parent_context.new_layout_allowed)
801 parse_unknown_environment(p, name, os, FLAG_END, outer,
804 // Alignment and spacing settings
805 // FIXME (bug xxxx): These settings can span multiple paragraphs and
806 // therefore are totally broken!
807 // Note that \centering, raggedright, and raggedleft cannot be handled, as
808 // they are commands not environments. They are furthermore switches that
809 // can be ended by another switches, but also by commands like \footnote or
810 // \parbox. So the only safe way is to leave them untouched.
811 else if (name == "center" || name == "centering" ||
812 name == "flushleft" || name == "flushright" ||
813 name == "singlespace" || name == "onehalfspace" ||
814 name == "doublespace" || name == "spacing") {
815 eat_whitespace(p, os, parent_context, false);
816 // We must begin a new paragraph if not already done
817 if (! parent_context.atParagraphStart()) {
818 parent_context.check_end_layout(os);
819 parent_context.new_paragraph(os);
821 if (name == "flushleft")
822 parent_context.add_extra_stuff("\\align left\n");
823 else if (name == "flushright")
824 parent_context.add_extra_stuff("\\align right\n");
825 else if (name == "center" || name == "centering")
826 parent_context.add_extra_stuff("\\align center\n");
827 else if (name == "singlespace")
828 parent_context.add_extra_stuff("\\paragraph_spacing single\n");
829 else if (name == "onehalfspace")
830 parent_context.add_extra_stuff("\\paragraph_spacing onehalf\n");
831 else if (name == "doublespace")
832 parent_context.add_extra_stuff("\\paragraph_spacing double\n");
833 else if (name == "spacing")
834 parent_context.add_extra_stuff("\\paragraph_spacing other " + p.verbatim_item() + "\n");
835 parse_text(p, os, FLAG_END, outer, parent_context);
836 // Just in case the environment is empty
837 parent_context.extra_stuff.erase();
838 // We must begin a new paragraph to reset the alignment
839 parent_context.new_paragraph(os);
843 // The single '=' is meant here.
844 else if ((newlayout = findLayout(parent_context.textclass, name)) &&
845 newlayout->isEnvironment()) {
846 eat_whitespace(p, os, parent_context, false);
847 Context context(true, parent_context.textclass, newlayout,
848 parent_context.layout, parent_context.font);
849 if (parent_context.deeper_paragraph) {
850 // We are beginning a nested environment after a
851 // deeper paragraph inside the outer list environment.
852 // Therefore we don't need to output a "begin deeper".
853 context.need_end_deeper = true;
855 parent_context.check_end_layout(os);
856 switch (context.layout->latextype) {
857 case LATEX_LIST_ENVIRONMENT:
858 context.add_par_extra_stuff("\\labelwidthstring "
859 + p.verbatim_item() + '\n');
862 case LATEX_BIB_ENVIRONMENT:
863 p.verbatim_item(); // swallow next arg
869 context.check_deeper(os);
870 parse_text(p, os, FLAG_END, outer, context);
871 context.check_end_layout(os);
872 if (parent_context.deeper_paragraph) {
873 // We must suppress the "end deeper" because we
874 // suppressed the "begin deeper" above.
875 context.need_end_deeper = false;
877 context.check_end_deeper(os);
878 parent_context.new_paragraph(os);
882 else if (name == "appendix") {
883 // This is no good latex style, but it works and is used in some documents...
884 eat_whitespace(p, os, parent_context, false);
885 parent_context.check_end_layout(os);
886 Context context(true, parent_context.textclass, parent_context.layout,
887 parent_context.layout, parent_context.font);
888 context.check_layout(os);
889 os << "\\start_of_appendix\n";
890 parse_text(p, os, FLAG_END, outer, context);
891 context.check_end_layout(os);
895 else if (known_environments.find(name) != known_environments.end()) {
896 vector<ArgumentType> arguments = known_environments[name];
897 // The last "argument" denotes wether we may translate the
898 // environment contents to LyX
899 // The default required if no argument is given makes us
900 // compatible with the reLyXre environment.
901 ArgumentType contents = arguments.empty() ?
904 if (!arguments.empty())
905 arguments.pop_back();
906 // See comment in parse_unknown_environment()
907 bool const specialfont =
908 (parent_context.font != parent_context.normalfont);
909 bool const new_layout_allowed =
910 parent_context.new_layout_allowed;
912 parent_context.new_layout_allowed = false;
913 parse_arguments("\\begin{" + name + "}", arguments, p, os,
914 outer, parent_context);
915 if (contents == verbatim)
916 handle_ert(os, p.verbatimEnvironment(name),
919 parse_text_snippet(p, os, FLAG_END, outer,
921 handle_ert(os, "\\end{" + name + "}", parent_context);
923 parent_context.new_layout_allowed = new_layout_allowed;
927 parse_unknown_environment(p, name, os, FLAG_END, outer,
930 active_environments.pop_back();
934 /// parses a comment and outputs it to \p os.
935 void parse_comment(Parser & p, ostream & os, Token const & t, Context & context)
937 LASSERT(t.cat() == catComment, return);
938 if (!t.cs().empty()) {
939 context.check_layout(os);
940 handle_comment(os, '%' + t.cs(), context);
941 if (p.next_token().cat() == catNewline) {
942 // A newline after a comment line starts a new
944 if (context.new_layout_allowed) {
945 if(!context.atParagraphStart())
946 // Only start a new paragraph if not already
947 // done (we might get called recursively)
948 context.new_paragraph(os);
950 handle_ert(os, "\n", context);
951 eat_whitespace(p, os, context, true);
961 * Reads spaces and comments until the first non-space, non-comment token.
962 * New paragraphs (double newlines or \\par) are handled like simple spaces
963 * if \p eatParagraph is true.
964 * Spaces are skipped, but comments are written to \p os.
966 void eat_whitespace(Parser & p, ostream & os, Context & context,
970 Token const & t = p.get_token();
971 if (t.cat() == catComment)
972 parse_comment(p, os, t, context);
973 else if ((! eatParagraph && p.isParagraph()) ||
974 (t.cat() != catSpace && t.cat() != catNewline)) {
983 * Set a font attribute, parse text and reset the font attribute.
984 * \param attribute Attribute name (e.g. \\family, \\shape etc.)
985 * \param currentvalue Current value of the attribute. Is set to the new
986 * value during parsing.
987 * \param newvalue New value of the attribute
989 void parse_text_attributes(Parser & p, ostream & os, unsigned flags, bool outer,
990 Context & context, string const & attribute,
991 string & currentvalue, string const & newvalue)
993 context.check_layout(os);
994 string const oldvalue = currentvalue;
995 currentvalue = newvalue;
996 os << '\n' << attribute << ' ' << newvalue << "\n";
997 parse_text_snippet(p, os, flags, outer, context);
998 context.check_layout(os);
999 os << '\n' << attribute << ' ' << oldvalue << "\n";
1000 currentvalue = oldvalue;
1004 /// get the arguments of a natbib or jurabib citation command
1005 void get_cite_arguments(Parser & p, bool natbibOrder,
1006 string & before, string & after)
1008 // We need to distinguish "" and "[]", so we can't use p.getOpt().
1010 // text before the citation
1012 // text after the citation
1013 after = p.getFullOpt();
1015 if (!after.empty()) {
1016 before = p.getFullOpt();
1017 if (natbibOrder && !before.empty())
1018 swap(before, after);
1023 /// Convert filenames with TeX macros and/or quotes to something LyX
1025 string const normalize_filename(string const & name)
1027 Parser p(trim(name, "\""));
1030 Token const & t = p.get_token();
1031 if (t.cat() != catEscape)
1033 else if (t.cs() == "lyxdot") {
1034 // This is used by LyX for simple dots in relative
1038 } else if (t.cs() == "space") {
1048 /// Convert \p name from TeX convention (relative to master file) to LyX
1049 /// convention (relative to .lyx file) if it is relative
1050 void fix_relative_filename(string & name)
1052 FileName fname(name);
1053 if (fname.isAbsolute())
1056 name = to_utf8(makeRelPath(from_utf8(makeAbsPath(name, getMasterFilePath()).absFilename()),
1057 from_utf8(getParentFilePath())));
1061 /// Parse a NoWeb Scrap section. The initial "<<" is already parsed.
1062 void parse_noweb(Parser & p, ostream & os, Context & context)
1064 // assemble the rest of the keyword
1068 Token const & t = p.get_token();
1069 if (t.asInput() == ">" && p.next_token().asInput() == ">") {
1072 scrap = (p.good() && p.next_token().asInput() == "=");
1074 name += p.get_token().asInput();
1077 name += t.asInput();
1080 if (!scrap || !context.new_layout_allowed ||
1081 !context.textclass.hasLayout(from_ascii("Scrap"))) {
1082 cerr << "Warning: Could not interpret '" << name
1083 << "'. Ignoring it." << endl;
1087 // We use new_paragraph instead of check_end_layout because the stuff
1088 // following the noweb chunk needs to start with a \begin_layout.
1089 // This may create a new paragraph even if there was none in the
1090 // noweb file, but the alternative is an invalid LyX file. Since
1091 // noweb code chunks are implemented with a layout style in LyX they
1092 // always must be in an own paragraph.
1093 context.new_paragraph(os);
1094 Context newcontext(true, context.textclass,
1095 &context.textclass[from_ascii("Scrap")]);
1096 newcontext.check_layout(os);
1099 Token const & t = p.get_token();
1100 // We abuse the parser a bit, because this is no TeX syntax
1102 if (t.cat() == catEscape)
1103 os << subst(t.asInput(), "\\", "\n\\backslash\n");
1105 os << subst(t.asInput(), "\n", "\n\\newline\n");
1106 // The scrap chunk is ended by an @ at the beginning of a line.
1107 // After the @ the line may contain a comment and/or
1108 // whitespace, but nothing else.
1109 if (t.asInput() == "@" && p.prev_token().cat() == catNewline &&
1110 (p.next_token().cat() == catSpace ||
1111 p.next_token().cat() == catNewline ||
1112 p.next_token().cat() == catComment)) {
1113 while (p.good() && p.next_token().cat() == catSpace)
1114 os << p.get_token().asInput();
1115 if (p.next_token().cat() == catComment)
1116 // The comment includes a final '\n'
1117 os << p.get_token().asInput();
1119 if (p.next_token().cat() == catNewline)
1126 newcontext.check_end_layout(os);
1129 } // anonymous namespace
1132 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
1135 Layout const * newlayout = 0;
1136 // store the current selectlanguage to be used after \foreignlanguage
1138 // Store the latest bibliographystyle (needed for bibtex inset)
1139 string bibliographystyle;
1140 bool const use_natbib = used_packages.find("natbib") != used_packages.end();
1141 bool const use_jurabib = used_packages.find("jurabib") != used_packages.end();
1143 Token const & t = p.get_token();
1146 cerr << "t: " << t << " flags: " << flags << "\n";
1149 if (flags & FLAG_ITEM) {
1150 if (t.cat() == catSpace)
1153 flags &= ~FLAG_ITEM;
1154 if (t.cat() == catBegin) {
1155 // skip the brace and collect everything to the next matching
1157 flags |= FLAG_BRACE_LAST;
1161 // handle only this single token, leave the loop if done
1162 flags |= FLAG_LEAVE;
1165 if (t.character() == ']' && (flags & FLAG_BRACK_LAST))
1171 if (t.cat() == catMath) {
1172 // we are inside some text mode thingy, so opening new math is allowed
1173 context.check_layout(os);
1174 begin_inset(os, "Formula ");
1175 Token const & n = p.get_token();
1176 if (n.cat() == catMath && outer) {
1177 // TeX's $$...$$ syntax for displayed math
1179 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
1181 p.get_token(); // skip the second '$' token
1183 // simple $...$ stuff
1186 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
1192 else if (t.cat() == catSuper || t.cat() == catSub)
1193 cerr << "catcode " << t << " illegal in text mode\n";
1195 // Basic support for english quotes. This should be
1196 // extended to other quotes, but is not so easy (a
1197 // left english quote is the same as a right german
1199 else if (t.asInput() == "`" && p.next_token().asInput() == "`") {
1200 context.check_layout(os);
1201 begin_inset(os, "Quotes ");
1207 else if (t.asInput() == "'" && p.next_token().asInput() == "'") {
1208 context.check_layout(os);
1209 begin_inset(os, "Quotes ");
1216 else if (t.asInput() == ">" && p.next_token().asInput() == ">") {
1217 context.check_layout(os);
1218 begin_inset(os, "Quotes ");
1225 else if (t.asInput() == "<" && p.next_token().asInput() == "<") {
1226 context.check_layout(os);
1227 begin_inset(os, "Quotes ");
1234 else if (t.asInput() == "<"
1235 && p.next_token().asInput() == "<" && noweb_mode) {
1237 parse_noweb(p, os, context);
1240 else if (t.cat() == catSpace || (t.cat() == catNewline && ! p.isParagraph()))
1241 check_space(p, os, context);
1243 else if (t.character() == '[' && noweb_mode &&
1244 p.next_token().character() == '[') {
1245 // These can contain underscores
1247 string const s = p.getFullOpt() + ']';
1248 if (p.next_token().character() == ']')
1251 cerr << "Warning: Inserting missing ']' in '"
1252 << s << "'." << endl;
1253 handle_ert(os, s, context);
1256 else if (t.cat() == catLetter ||
1257 t.cat() == catOther ||
1258 t.cat() == catAlign ||
1259 t.cat() == catParameter) {
1260 // This translates "&" to "\\&" which may be wrong...
1261 context.check_layout(os);
1265 else if (p.isParagraph()) {
1266 if (context.new_layout_allowed)
1267 context.new_paragraph(os);
1269 handle_ert(os, "\\par ", context);
1270 eat_whitespace(p, os, context, true);
1273 else if (t.cat() == catActive) {
1274 context.check_layout(os);
1275 if (t.character() == '~') {
1276 if (context.layout->free_spacing)
1279 os << "\\InsetSpace ~\n";
1284 else if (t.cat() == catBegin &&
1285 p.next_token().cat() == catEnd) {
1287 Token const prev = p.prev_token();
1289 if (p.next_token().character() == '`' ||
1290 (prev.character() == '-' &&
1291 p.next_token().character() == '-'))
1292 ; // ignore it in {}`` or -{}-
1294 handle_ert(os, "{}", context);
1298 else if (t.cat() == catBegin) {
1299 context.check_layout(os);
1300 // special handling of font attribute changes
1301 Token const prev = p.prev_token();
1302 Token const next = p.next_token();
1303 TeXFont const oldFont = context.font;
1304 if (next.character() == '[' ||
1305 next.character() == ']' ||
1306 next.character() == '*') {
1308 if (p.next_token().cat() == catEnd) {
1313 handle_ert(os, "{", context);
1314 parse_text_snippet(p, os,
1317 handle_ert(os, "}", context);
1319 } else if (! context.new_layout_allowed) {
1320 handle_ert(os, "{", context);
1321 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1323 handle_ert(os, "}", context);
1324 } else if (is_known(next.cs(), known_sizes)) {
1325 // next will change the size, so we must
1327 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1329 if (!context.atParagraphStart())
1331 << context.font.size << "\n";
1332 } else if (is_known(next.cs(), known_font_families)) {
1333 // next will change the font family, so we
1334 // must reset it here
1335 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1337 if (!context.atParagraphStart())
1339 << context.font.family << "\n";
1340 } else if (is_known(next.cs(), known_font_series)) {
1341 // next will change the font series, so we
1342 // must reset it here
1343 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1345 if (!context.atParagraphStart())
1347 << context.font.series << "\n";
1348 } else if (is_known(next.cs(), known_font_shapes)) {
1349 // next will change the font shape, so we
1350 // must reset it here
1351 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1353 if (!context.atParagraphStart())
1355 << context.font.shape << "\n";
1356 } else if (is_known(next.cs(), known_old_font_families) ||
1357 is_known(next.cs(), known_old_font_series) ||
1358 is_known(next.cs(), known_old_font_shapes)) {
1359 // next will change the font family, series
1360 // and shape, so we must reset it here
1361 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1363 if (!context.atParagraphStart())
1365 << context.font.family
1367 << context.font.series
1369 << context.font.shape << "\n";
1371 handle_ert(os, "{", context);
1372 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1374 handle_ert(os, "}", context);
1378 else if (t.cat() == catEnd) {
1379 if (flags & FLAG_BRACE_LAST) {
1382 cerr << "stray '}' in text\n";
1383 handle_ert(os, "}", context);
1386 else if (t.cat() == catComment)
1387 parse_comment(p, os, t, context);
1390 // control sequences
1393 else if (t.cs() == "(") {
1394 context.check_layout(os);
1395 begin_inset(os, "Formula");
1397 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
1402 else if (t.cs() == "[") {
1403 context.check_layout(os);
1404 begin_inset(os, "Formula");
1406 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
1411 else if (t.cs() == "begin")
1412 parse_environment(p, os, outer, context);
1414 else if (t.cs() == "end") {
1415 if (flags & FLAG_END) {
1416 // eat environment name
1417 string const name = p.getArg('{', '}');
1418 if (name != active_environment())
1419 cerr << "\\end{" + name + "} does not match \\begin{"
1420 + active_environment() + "}\n";
1423 p.error("found 'end' unexpectedly");
1426 else if (t.cs() == "item") {
1429 bool optarg = false;
1430 if (p.next_token().character() == '[') {
1431 p.get_token(); // eat '['
1432 s = parse_text_snippet(p, FLAG_BRACK_LAST,
1437 context.check_layout(os);
1438 if (context.has_item) {
1439 // An item in an unknown list-like environment
1440 // FIXME: Do this in check_layout()!
1441 context.has_item = false;
1443 handle_ert(os, "\\item", context);
1445 handle_ert(os, "\\item ", context);
1448 if (context.layout->labeltype != LABEL_MANUAL) {
1449 // lyx does not support \item[\mybullet]
1450 // in itemize environments
1451 handle_ert(os, "[", context);
1453 handle_ert(os, "]", context);
1454 } else if (!s.empty()) {
1455 // The space is needed to separate the
1456 // item from the rest of the sentence.
1458 eat_whitespace(p, os, context, false);
1463 else if (t.cs() == "bibitem") {
1465 context.check_layout(os);
1468 os << '{' << p.verbatim_item() << '}' << "\n";
1471 else if (t.cs() == "def") {
1472 context.check_layout(os);
1473 eat_whitespace(p, os, context, false);
1474 string name = p.get_token().cs();
1475 eat_whitespace(p, os, context, false);
1481 while (p.next_token().cat() != catBegin) {
1482 if (p.next_token().cat() == catParameter) {
1487 // followed by number?
1488 if (p.next_token().cat() == catOther) {
1489 char c = p.getChar();
1491 // number = current arity + 1?
1492 if (c == arity + '0' + 1)
1497 paramtext += p.get_token().asString();
1499 paramtext += p.get_token().asString();
1504 // only output simple (i.e. compatible) macro as FormulaMacros
1505 string ert = "\\def\\" + name + ' ' + paramtext + '{' + p.verbatim_item() + '}';
1507 context.check_layout(os);
1508 begin_inset(os, "FormulaMacro");
1512 handle_ert(os, ert, context);
1515 else if (t.cs() == "noindent") {
1517 context.add_par_extra_stuff("\\noindent\n");
1520 else if (t.cs() == "appendix") {
1521 context.add_par_extra_stuff("\\start_of_appendix\n");
1522 // We need to start a new paragraph. Otherwise the
1523 // appendix in 'bla\appendix\chapter{' would start
1525 context.new_paragraph(os);
1526 // We need to make sure that the paragraph is
1527 // generated even if it is empty. Otherwise the
1528 // appendix in '\par\appendix\par\chapter{' would
1530 context.check_layout(os);
1531 // FIXME: This is a hack to prevent paragraph
1532 // deletion if it is empty. Handle this better!
1534 "%dummy comment inserted by tex2lyx to "
1535 "ensure that this paragraph is not empty",
1537 // Both measures above may generate an additional
1538 // empty paragraph, but that does not hurt, because
1539 // whitespace does not matter here.
1540 eat_whitespace(p, os, context, true);
1543 // Starred section headings
1544 // Must attempt to parse "Section*" before "Section".
1545 else if ((p.next_token().asInput() == "*") &&
1546 context.new_layout_allowed &&
1547 (newlayout = findLayout(context.textclass, t.cs() + '*')) &&
1548 newlayout->isCommand()) {
1549 TeXFont const oldFont = context.font;
1550 // save the current font size
1551 string const size = oldFont.size;
1552 // reset the font size to default, because the
1553 // font size switches don't affect section
1554 // headings and the like
1555 context.font.size = known_coded_sizes[0];
1556 output_font_change(os, oldFont, context.font);
1559 output_command_layout(os, p, outer, context, newlayout);
1560 // set the font size to the original value
1561 context.font.size = size;
1562 output_font_change(os, oldFont, context.font);
1566 // Section headings and the like
1567 else if (context.new_layout_allowed &&
1568 (newlayout = findLayout(context.textclass, t.cs())) &&
1569 newlayout->isCommand()) {
1570 TeXFont const oldFont = context.font;
1571 // save the current font size
1572 string const size = oldFont.size;
1573 // reset the font size to default, because the font size switches don't
1574 // affect section headings and the like
1575 context.font.size = known_coded_sizes[0];
1576 output_font_change(os, oldFont, context.font);
1578 output_command_layout(os, p, outer, context, newlayout);
1579 // set the font size to the original value
1580 context.font.size = size;
1581 output_font_change(os, oldFont, context.font);
1585 // Special handling for \caption
1586 // FIXME: remove this when InsetCaption is supported.
1587 else if (context.new_layout_allowed &&
1588 t.cs() == captionlayout()->latexname()) {
1589 output_command_layout(os, p, outer, context,
1594 else if (t.cs() == "includegraphics") {
1595 bool const clip = p.next_token().asInput() == "*";
1598 map<string, string> opts = split_map(p.getArg('[', ']'));
1600 opts["clip"] = string();
1601 string name = normalize_filename(p.verbatim_item());
1603 string const path = getMasterFilePath();
1604 // We want to preserve relative / absolute filenames,
1605 // therefore path is only used for testing
1606 if (!makeAbsPath(name, path).exists()) {
1607 // The file extension is probably missing.
1608 // Now try to find it out.
1609 string const dvips_name =
1610 find_file(name, path,
1611 known_dvips_graphics_formats);
1612 string const pdftex_name =
1613 find_file(name, path,
1614 known_pdftex_graphics_formats);
1615 if (!dvips_name.empty()) {
1616 if (!pdftex_name.empty()) {
1617 cerr << "This file contains the "
1619 "\"\\includegraphics{"
1621 "However, files\n\""
1622 << dvips_name << "\" and\n\""
1623 << pdftex_name << "\"\n"
1624 "both exist, so I had to make a "
1625 "choice and took the first one.\n"
1626 "Please move the unwanted one "
1627 "someplace else and try again\n"
1628 "if my choice was wrong."
1632 } else if (!pdftex_name.empty())
1636 if (makeAbsPath(name, path).exists())
1637 fix_relative_filename(name);
1639 cerr << "Warning: Could not find graphics file '"
1640 << name << "'." << endl;
1642 context.check_layout(os);
1643 begin_inset(os, "Graphics ");
1644 os << "\n\tfilename " << name << '\n';
1645 if (opts.find("width") != opts.end())
1647 << translate_len(opts["width"]) << '\n';
1648 if (opts.find("height") != opts.end())
1650 << translate_len(opts["height"]) << '\n';
1651 if (opts.find("scale") != opts.end()) {
1652 istringstream iss(opts["scale"]);
1656 os << "\tscale " << val << '\n';
1658 if (opts.find("angle") != opts.end())
1659 os << "\trotateAngle "
1660 << opts["angle"] << '\n';
1661 if (opts.find("origin") != opts.end()) {
1663 string const opt = opts["origin"];
1664 if (opt.find('l') != string::npos) ss << "left";
1665 if (opt.find('r') != string::npos) ss << "right";
1666 if (opt.find('c') != string::npos) ss << "center";
1667 if (opt.find('t') != string::npos) ss << "Top";
1668 if (opt.find('b') != string::npos) ss << "Bottom";
1669 if (opt.find('B') != string::npos) ss << "Baseline";
1670 if (!ss.str().empty())
1671 os << "\trotateOrigin " << ss.str() << '\n';
1673 cerr << "Warning: Ignoring unknown includegraphics origin argument '" << opt << "'\n";
1675 if (opts.find("keepaspectratio") != opts.end())
1676 os << "\tkeepAspectRatio\n";
1677 if (opts.find("clip") != opts.end())
1679 if (opts.find("draft") != opts.end())
1681 if (opts.find("bb") != opts.end())
1682 os << "\tBoundingBox "
1683 << opts["bb"] << '\n';
1684 int numberOfbbOptions = 0;
1685 if (opts.find("bbllx") != opts.end())
1686 numberOfbbOptions++;
1687 if (opts.find("bblly") != opts.end())
1688 numberOfbbOptions++;
1689 if (opts.find("bburx") != opts.end())
1690 numberOfbbOptions++;
1691 if (opts.find("bbury") != opts.end())
1692 numberOfbbOptions++;
1693 if (numberOfbbOptions == 4)
1694 os << "\tBoundingBox "
1695 << opts["bbllx"] << " " << opts["bblly"] << " "
1696 << opts["bburx"] << " " << opts["bbury"] << '\n';
1697 else if (numberOfbbOptions > 0)
1698 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
1699 numberOfbbOptions = 0;
1700 if (opts.find("natwidth") != opts.end())
1701 numberOfbbOptions++;
1702 if (opts.find("natheight") != opts.end())
1703 numberOfbbOptions++;
1704 if (numberOfbbOptions == 2)
1705 os << "\tBoundingBox 0bp 0bp "
1706 << opts["natwidth"] << " " << opts["natheight"] << '\n';
1707 else if (numberOfbbOptions > 0)
1708 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
1709 ostringstream special;
1710 if (opts.find("hiresbb") != opts.end())
1711 special << "hiresbb,";
1712 if (opts.find("trim") != opts.end())
1714 if (opts.find("viewport") != opts.end())
1715 special << "viewport=" << opts["viewport"] << ',';
1716 if (opts.find("totalheight") != opts.end())
1717 special << "totalheight=" << opts["totalheight"] << ',';
1718 if (opts.find("type") != opts.end())
1719 special << "type=" << opts["type"] << ',';
1720 if (opts.find("ext") != opts.end())
1721 special << "ext=" << opts["ext"] << ',';
1722 if (opts.find("read") != opts.end())
1723 special << "read=" << opts["read"] << ',';
1724 if (opts.find("command") != opts.end())
1725 special << "command=" << opts["command"] << ',';
1726 string s_special = special.str();
1727 if (!s_special.empty()) {
1728 // We had special arguments. Remove the trailing ','.
1729 os << "\tspecial " << s_special.substr(0, s_special.size() - 1) << '\n';
1731 // TODO: Handle the unknown settings better.
1732 // Warn about invalid options.
1733 // Check whether some option was given twice.
1737 else if (t.cs() == "footnote" ||
1738 (t.cs() == "thanks" && context.layout->intitle)) {
1740 context.check_layout(os);
1741 begin_inset(os, "Foot\n");
1742 os << "status collapsed\n\n";
1743 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
1747 else if (t.cs() == "marginpar") {
1749 context.check_layout(os);
1750 begin_inset(os, "Marginal\n");
1751 os << "status collapsed\n\n";
1752 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
1756 else if (t.cs() == "ensuremath") {
1758 context.check_layout(os);
1759 string const s = p.verbatim_item();
1760 //FIXME: this never triggers in UTF8
1761 if (s == "\xb1" || s == "\xb3" || s == "\xb2" || s == "\xb5")
1764 handle_ert(os, "\\ensuremath{" + s + "}",
1768 else if (t.cs() == "hfill") {
1769 context.check_layout(os);
1770 os << "\n\\hfill\n";
1775 else if (t.cs() == "makeindex" || t.cs() == "maketitle") {
1776 // FIXME: Somehow prevent title layouts if
1777 // "maketitle" was not found
1779 skip_braces(p); // swallow this
1782 else if (t.cs() == "tableofcontents") {
1784 context.check_layout(os);
1785 begin_inset(os, "LatexCommand \\tableofcontents\n");
1787 skip_braces(p); // swallow this
1790 else if (t.cs() == "listoffigures") {
1792 context.check_layout(os);
1793 begin_inset(os, "FloatList figure\n");
1795 skip_braces(p); // swallow this
1798 else if (t.cs() == "listoftables") {
1800 context.check_layout(os);
1801 begin_inset(os, "FloatList table\n");
1803 skip_braces(p); // swallow this
1806 else if (t.cs() == "listof") {
1807 p.skip_spaces(true);
1808 string const name = p.get_token().asString();
1809 if (context.textclass.floats().typeExist(name)) {
1810 context.check_layout(os);
1811 begin_inset(os, "FloatList ");
1814 p.get_token(); // swallow second arg
1816 handle_ert(os, "\\listof{" + name + "}", context);
1819 else if (t.cs() == "textrm")
1820 parse_text_attributes(p, os, FLAG_ITEM, outer,
1821 context, "\\family",
1822 context.font.family, "roman");
1824 else if (t.cs() == "textsf")
1825 parse_text_attributes(p, os, FLAG_ITEM, outer,
1826 context, "\\family",
1827 context.font.family, "sans");
1829 else if (t.cs() == "texttt")
1830 parse_text_attributes(p, os, FLAG_ITEM, outer,
1831 context, "\\family",
1832 context.font.family, "typewriter");
1834 else if (t.cs() == "textmd")
1835 parse_text_attributes(p, os, FLAG_ITEM, outer,
1836 context, "\\series",
1837 context.font.series, "medium");
1839 else if (t.cs() == "textbf")
1840 parse_text_attributes(p, os, FLAG_ITEM, outer,
1841 context, "\\series",
1842 context.font.series, "bold");
1844 else if (t.cs() == "textup")
1845 parse_text_attributes(p, os, FLAG_ITEM, outer,
1847 context.font.shape, "up");
1849 else if (t.cs() == "textit")
1850 parse_text_attributes(p, os, FLAG_ITEM, outer,
1852 context.font.shape, "italic");
1854 else if (t.cs() == "textsl")
1855 parse_text_attributes(p, os, FLAG_ITEM, outer,
1857 context.font.shape, "slanted");
1859 else if (t.cs() == "textsc")
1860 parse_text_attributes(p, os, FLAG_ITEM, outer,
1862 context.font.shape, "smallcaps");
1864 else if (t.cs() == "textnormal" || t.cs() == "normalfont") {
1865 context.check_layout(os);
1866 TeXFont oldFont = context.font;
1867 context.font.init();
1868 context.font.size = oldFont.size;
1869 os << "\n\\family " << context.font.family << "\n";
1870 os << "\n\\series " << context.font.series << "\n";
1871 os << "\n\\shape " << context.font.shape << "\n";
1872 if (t.cs() == "textnormal") {
1873 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1874 output_font_change(os, context.font, oldFont);
1875 context.font = oldFont;
1877 eat_whitespace(p, os, context, false);
1880 else if (t.cs() == "textcolor") {
1881 // scheme is \textcolor{color name}{text}
1882 string const color = p.verbatim_item();
1883 // we only support the predefined colors of the color package
1884 if (color == "black" || color == "blue" || color == "cyan"
1885 || color == "green" || color == "magenta" || color == "red"
1886 || color == "white" || color == "yellow") {
1887 context.check_layout(os);
1888 os << "\n\\color " << color << "\n";
1889 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1890 context.check_layout(os);
1891 os << "\n\\color inherit\n";
1893 // for custom defined colors
1894 handle_ert(os, t.asInput() + "{" + color + "}", context);
1897 else if (t.cs() == "underbar") {
1898 // Do NOT handle \underline.
1899 // \underbar cuts through y, g, q, p etc.,
1900 // \underline does not.
1901 context.check_layout(os);
1902 os << "\n\\bar under\n";
1903 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1904 context.check_layout(os);
1905 os << "\n\\bar default\n";
1908 else if (t.cs() == "emph" || t.cs() == "noun") {
1909 context.check_layout(os);
1910 os << "\n\\" << t.cs() << " on\n";
1911 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1912 context.check_layout(os);
1913 os << "\n\\" << t.cs() << " default\n";
1916 else if (t.cs() == "lyxline") {
1917 context.check_layout(os);
1921 else if (use_natbib &&
1922 is_known(t.cs(), known_natbib_commands) &&
1923 ((t.cs() != "citefullauthor" &&
1924 t.cs() != "citeyear" &&
1925 t.cs() != "citeyearpar") ||
1926 p.next_token().asInput() != "*")) {
1927 context.check_layout(os);
1929 // \citet[before][after]{a} \citet[after][before]{a}
1930 // \citet[before][]{a} \citet[][before]{a}
1931 // \citet[after]{a} \citet[after]{a}
1932 // \citet{a} \citet{a}
1933 string command = '\\' + t.cs();
1934 if (p.next_token().asInput() == "*") {
1938 if (command == "\\citefullauthor")
1939 // alternative name for "\\citeauthor*"
1940 command = "\\citeauthor*";
1942 // text before the citation
1944 // text after the citation
1946 get_cite_arguments(p, true, before, after);
1948 if (command == "\\cite") {
1949 // \cite without optional argument means
1950 // \citet, \cite with at least one optional
1951 // argument means \citep.
1952 if (before.empty() && after.empty())
1953 command = "\\citet";
1955 command = "\\citep";
1957 if (before.empty() && after == "[]")
1958 // avoid \citet[]{a}
1960 else if (before == "[]" && after == "[]") {
1961 // avoid \citet[][]{a}
1965 begin_inset(os, "LatexCommand ");
1966 os << command << after << before
1967 << '{' << p.verbatim_item() << "}\n";
1971 else if (use_jurabib &&
1972 is_known(t.cs(), known_jurabib_commands)) {
1973 context.check_layout(os);
1974 string const command = '\\' + t.cs();
1975 char argumentOrder = '\0';
1976 vector<string> const & options = used_packages["jurabib"];
1977 if (find(options.begin(), options.end(),
1978 "natbiborder") != options.end())
1979 argumentOrder = 'n';
1980 else if (find(options.begin(), options.end(),
1981 "jurabiborder") != options.end())
1982 argumentOrder = 'j';
1984 // text before the citation
1986 // text after the citation
1988 get_cite_arguments(p, argumentOrder != 'j', before, after);
1990 string const citation = p.verbatim_item();
1991 if (!before.empty() && argumentOrder == '\0') {
1992 cerr << "Warning: Assuming argument order "
1993 "of jurabib version 0.6 for\n'"
1994 << command << before << after << '{'
1995 << citation << "}'.\n"
1996 "Add 'jurabiborder' to the jurabib "
1997 "package options if you used an\n"
1998 "earlier jurabib version." << endl;
2000 begin_inset(os, "LatexCommand ");
2001 os << command << after << before
2002 << '{' << citation << "}\n";
2006 else if (is_known(t.cs(), known_latex_commands)) {
2007 // This needs to be after the check for natbib and
2008 // jurabib commands, because "cite" has different
2009 // arguments with natbib and jurabib.
2010 context.check_layout(os);
2011 begin_inset(os, "LatexCommand ");
2012 os << '\\' << t.cs();
2013 // lyx cannot handle newlines in a latex command
2014 // FIXME: Move the substitution into parser::getOpt()?
2015 os << subst(p.getOpt(), "\n", " ");
2016 os << subst(p.getOpt(), "\n", " ");
2017 os << '{' << subst(p.verbatim_item(), "\n", " ") << "}\n";
2021 else if (is_known(t.cs(), known_quotes)) {
2022 char const * const * where = is_known(t.cs(), known_quotes);
2023 context.check_layout(os);
2024 begin_inset(os, "Quotes ");
2025 os << known_coded_quotes[where - known_quotes];
2027 // LyX adds {} after the quote, so we have to eat
2028 // spaces here if there are any before a possible
2030 eat_whitespace(p, os, context, false);
2034 else if (is_known(t.cs(), known_sizes) &&
2035 context.new_layout_allowed) {
2036 char const * const * where = is_known(t.cs(), known_sizes);
2037 context.check_layout(os);
2038 TeXFont const oldFont = context.font;
2039 // the font size index differs by 1, because the known_coded_sizes
2040 // has additionally a "default" entry
2041 context.font.size = known_coded_sizes[where - known_sizes + 1];
2042 output_font_change(os, oldFont, context.font);
2043 eat_whitespace(p, os, context, false);
2046 else if (is_known(t.cs(), known_font_families) &&
2047 context.new_layout_allowed) {
2048 char const * const * where =
2049 is_known(t.cs(), known_font_families);
2050 context.check_layout(os);
2051 TeXFont const oldFont = context.font;
2052 context.font.family =
2053 known_coded_font_families[where - known_font_families];
2054 output_font_change(os, oldFont, context.font);
2055 eat_whitespace(p, os, context, false);
2058 else if (is_known(t.cs(), known_font_series) &&
2059 context.new_layout_allowed) {
2060 char const * const * where =
2061 is_known(t.cs(), known_font_series);
2062 context.check_layout(os);
2063 TeXFont const oldFont = context.font;
2064 context.font.series =
2065 known_coded_font_series[where - known_font_series];
2066 output_font_change(os, oldFont, context.font);
2067 eat_whitespace(p, os, context, false);
2070 else if (is_known(t.cs(), known_font_shapes) &&
2071 context.new_layout_allowed) {
2072 char const * const * where =
2073 is_known(t.cs(), known_font_shapes);
2074 context.check_layout(os);
2075 TeXFont const oldFont = context.font;
2076 context.font.shape =
2077 known_coded_font_shapes[where - known_font_shapes];
2078 output_font_change(os, oldFont, context.font);
2079 eat_whitespace(p, os, context, false);
2081 else if (is_known(t.cs(), known_old_font_families) &&
2082 context.new_layout_allowed) {
2083 char const * const * where =
2084 is_known(t.cs(), known_old_font_families);
2085 context.check_layout(os);
2086 TeXFont const oldFont = context.font;
2087 context.font.init();
2088 context.font.size = oldFont.size;
2089 context.font.family =
2090 known_coded_font_families[where - known_old_font_families];
2091 output_font_change(os, oldFont, context.font);
2092 eat_whitespace(p, os, context, false);
2095 else if (is_known(t.cs(), known_old_font_series) &&
2096 context.new_layout_allowed) {
2097 char const * const * where =
2098 is_known(t.cs(), known_old_font_series);
2099 context.check_layout(os);
2100 TeXFont const oldFont = context.font;
2101 context.font.init();
2102 context.font.size = oldFont.size;
2103 context.font.series =
2104 known_coded_font_series[where - known_old_font_series];
2105 output_font_change(os, oldFont, context.font);
2106 eat_whitespace(p, os, context, false);
2109 else if (is_known(t.cs(), known_old_font_shapes) &&
2110 context.new_layout_allowed) {
2111 char const * const * where =
2112 is_known(t.cs(), known_old_font_shapes);
2113 context.check_layout(os);
2114 TeXFont const oldFont = context.font;
2115 context.font.init();
2116 context.font.size = oldFont.size;
2117 context.font.shape =
2118 known_coded_font_shapes[where - known_old_font_shapes];
2119 output_font_change(os, oldFont, context.font);
2120 eat_whitespace(p, os, context, false);
2123 else if (t.cs() == "selectlanguage") {
2124 context.check_layout(os);
2125 // save the language for the case that a
2126 // \foreignlanguage is used
2128 //FIXME: this is wrong, the language should
2129 // be saved in the context. (JMarc)
2130 selectlang = subst(p.verbatim_item(), "\n", " ");
2131 os << "\\lang " << selectlang << "\n";
2134 else if (t.cs() == "foreignlanguage") {
2135 context.check_layout(os);
2136 os << "\n\\lang " << subst(p.verbatim_item(), "\n", " ") << "\n";
2137 os << subst(p.verbatim_item(), "\n", " ");
2138 // FIXME: the second argument of selectlanguage
2139 // has to be parsed (like for \textsf, for
2141 // set back to last selectlanguage
2142 os << "\n\\lang " << selectlang << "\n";
2145 else if (t.cs() == "inputencoding") {
2146 // nothing to write here
2147 string const enc = subst(p.verbatim_item(), "\n", " ");
2150 else if (t.cs() == "LyX" || t.cs() == "TeX"
2151 || t.cs() == "LaTeX") {
2152 context.check_layout(os);
2154 skip_braces(p); // eat {}
2157 else if (t.cs() == "LaTeXe") {
2158 context.check_layout(os);
2160 skip_braces(p); // eat {}
2163 else if (t.cs() == "ldots") {
2164 context.check_layout(os);
2166 os << "\\SpecialChar \\ldots{}\n";
2169 else if (t.cs() == "lyxarrow") {
2170 context.check_layout(os);
2171 os << "\\SpecialChar \\menuseparator\n";
2175 else if (t.cs() == "textcompwordmark") {
2176 context.check_layout(os);
2177 os << "\\SpecialChar \\textcompwordmark{}\n";
2181 else if (t.cs() == "@" && p.next_token().asInput() == ".") {
2182 context.check_layout(os);
2183 os << "\\SpecialChar \\@.\n";
2187 else if (t.cs() == "-") {
2188 context.check_layout(os);
2189 os << "\\SpecialChar \\-\n";
2192 else if (t.cs() == "textasciitilde") {
2193 context.check_layout(os);
2198 else if (t.cs() == "textasciicircum") {
2199 context.check_layout(os);
2204 else if (t.cs() == "textbackslash") {
2205 context.check_layout(os);
2206 os << "\n\\backslash\n";
2210 else if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#"
2211 || t.cs() == "$" || t.cs() == "{" || t.cs() == "}"
2213 context.check_layout(os);
2217 else if (t.cs() == "char") {
2218 context.check_layout(os);
2219 if (p.next_token().character() == '`') {
2221 if (p.next_token().cs() == "\"") {
2226 handle_ert(os, "\\char`", context);
2229 handle_ert(os, "\\char", context);
2233 else if (t.cs() == "verb") {
2234 context.check_layout(os);
2235 char const delimiter = p.next_token().character();
2236 string const arg = p.getArg(delimiter, delimiter);
2238 oss << "\\verb" << delimiter << arg << delimiter;
2239 handle_ert(os, oss.str(), context);
2242 // Problem: \= creates a tabstop inside the tabbing environment
2243 // and else an accent. In the latter case we really would want
2244 // \={o} instead of \= o.
2245 else if (t.cs() == "=" && (flags & FLAG_TABBING))
2246 handle_ert(os, t.asInput(), context);
2248 else if (t.cs() == "H" || t.cs() == "c" || t.cs() == "^"
2249 || t.cs() == "'" || t.cs() == "`"
2250 || t.cs() == "~" || t.cs() == "." || t.cs() == "=") {
2251 // we need the trim as the LyX parser chokes on such spaces
2252 context.check_layout(os);
2253 // try to see whether the string is in unicodesymbols
2255 string command = t.asInput() + "{"
2256 + trim(p.verbatim_item())
2258 docstring s = encodings.fromLaTeXCommand(from_utf8(command), rem);
2261 cerr << "When parsing " << command
2262 << ", result is " << to_utf8(s)
2263 << "+" << to_utf8(rem) << endl;
2266 // we did not find a non-ert version
2267 handle_ert(os, command, context);
2270 else if (t.cs() == "\\") {
2271 context.check_layout(os);
2272 string const next = p.next_token().asInput();
2274 handle_ert(os, "\\\\" + p.getOpt(), context);
2275 else if (next == "*") {
2277 handle_ert(os, "\\\\*" + p.getOpt(), context);
2280 os << "\n\\newline\n";
2284 else if (t.cs() == "newline" ||
2285 t.cs() == "linebreak") {
2286 context.check_layout(os);
2287 os << "\n\\" << t.cs() << "\n";
2288 skip_braces(p); // eat {}
2291 else if (t.cs() == "input" || t.cs() == "include"
2292 || t.cs() == "verbatiminput") {
2293 string name = '\\' + t.cs();
2294 if (t.cs() == "verbatiminput"
2295 && p.next_token().asInput() == "*")
2296 name += p.get_token().asInput();
2297 context.check_layout(os);
2298 begin_inset(os, "Include ");
2299 string filename(normalize_filename(p.getArg('{', '}')));
2300 string const path = getMasterFilePath();
2301 // We want to preserve relative / absolute filenames,
2302 // therefore path is only used for testing
2303 if ((t.cs() == "include" || t.cs() == "input") &&
2304 !makeAbsPath(filename, path).exists()) {
2305 // The file extension is probably missing.
2306 // Now try to find it out.
2307 string const tex_name =
2308 find_file(filename, path,
2309 known_tex_extensions);
2310 if (!tex_name.empty())
2311 filename = tex_name;
2313 if (makeAbsPath(filename, path).exists()) {
2314 string const abstexname =
2315 makeAbsPath(filename, path).absFilename();
2316 string const abslyxname =
2317 changeExtension(abstexname, ".lyx");
2318 fix_relative_filename(filename);
2319 string const lyxname =
2320 changeExtension(filename, ".lyx");
2321 if (t.cs() != "verbatiminput" &&
2322 tex2lyx(abstexname, FileName(abslyxname),
2324 os << name << '{' << lyxname << "}\n";
2326 os << name << '{' << filename << "}\n";
2329 cerr << "Warning: Could not find included file '"
2330 << filename << "'." << endl;
2331 os << name << '{' << filename << "}\n";
2333 os << "preview false\n";
2337 else if (t.cs() == "bibliographystyle") {
2338 // store new bibliographystyle
2339 bibliographystyle = p.verbatim_item();
2340 // output new bibliographystyle.
2341 // This is only necessary if used in some other macro than \bibliography.
2342 handle_ert(os, "\\bibliographystyle{" + bibliographystyle + "}", context);
2345 else if (t.cs() == "bibliography") {
2346 context.check_layout(os);
2347 begin_inset(os, "LatexCommand ");
2349 // Do we have a bibliographystyle set?
2350 if (!bibliographystyle.empty()) {
2351 os << '[' << bibliographystyle << ']';
2353 os << '{' << p.verbatim_item() << "}\n";
2357 else if (t.cs() == "parbox")
2358 parse_box(p, os, FLAG_ITEM, outer, context, true);
2360 //\makebox() is part of the picture environment and different from \makebox{}
2361 //\makebox{} will be parsed by parse_box when bug 2956 is fixed
2362 else if (t.cs() == "makebox") {
2363 string arg = t.asInput();
2364 if (p.next_token().character() == '(')
2365 //the syntax is: \makebox(x,y)[position]{content}
2366 arg += p.getFullParentheseArg();
2368 //the syntax is: \makebox[width][position]{content}
2369 arg += p.getFullOpt();
2370 handle_ert(os, arg + p.getFullOpt(), context);
2373 else if (t.cs() == "smallskip" ||
2374 t.cs() == "medskip" ||
2375 t.cs() == "bigskip" ||
2376 t.cs() == "vfill") {
2377 context.check_layout(os);
2378 begin_inset(os, "VSpace ");
2384 else if (is_known(t.cs(), known_spaces)) {
2385 char const * const * where = is_known(t.cs(), known_spaces);
2386 context.check_layout(os);
2387 os << "\\InsetSpace ";
2388 os << '\\' << known_coded_spaces[where - known_spaces]
2390 // LaTeX swallows whitespace after all spaces except
2391 // "\\,". We have to do that here, too, because LyX
2392 // adds "{}" which would make the spaces significant.
2394 eat_whitespace(p, os, context, false);
2395 // LyX adds "{}" after all spaces except "\\ " and
2396 // "\\,", so we have to remove "{}".
2397 // "\\,{}" is equivalent to "\\," in LaTeX, so we
2398 // remove the braces after "\\,", too.
2403 else if (t.cs() == "newpage" ||
2404 t.cs() == "pagebreak" ||
2405 t.cs() == "clearpage" ||
2406 t.cs() == "cleardoublepage") {
2407 context.check_layout(os);
2408 os << "\n\\" << t.cs() << "\n";
2409 skip_braces(p); // eat {}
2412 else if (t.cs() == "newcommand" ||
2413 t.cs() == "providecommand" ||
2414 t.cs() == "renewcommand") {
2415 // these could be handled by parse_command(), but
2416 // we need to call add_known_command() here.
2417 string name = t.asInput();
2418 if (p.next_token().asInput() == "*") {
2419 // Starred form. Eat '*'
2423 string const command = p.verbatim_item();
2424 string const opt1 = p.getOpt();
2425 string const opt2 = p.getFullOpt();
2426 add_known_command(command, opt1, !opt2.empty());
2427 string const ert = name + '{' + command + '}' +
2429 '{' + p.verbatim_item() + '}';
2431 context.check_layout(os);
2432 begin_inset(os, "FormulaMacro");
2437 else if (t.cs() == "vspace") {
2438 bool starred = false;
2439 if (p.next_token().asInput() == "*") {
2443 string const length = p.verbatim_item();
2446 bool valid = splitLatexLength(length, valstring, unit);
2447 bool known_vspace = false;
2448 bool known_unit = false;
2451 istringstream iss(valstring);
2454 if (unit == "\\smallskipamount") {
2456 known_vspace = true;
2457 } else if (unit == "\\medskipamount") {
2459 known_vspace = true;
2460 } else if (unit == "\\bigskipamount") {
2462 known_vspace = true;
2463 } else if (unit == "\\fill") {
2465 known_vspace = true;
2468 if (!known_vspace) {
2469 switch (unitFromString(unit)) {
2490 if (known_unit || known_vspace) {
2491 // Literal length or known variable
2492 context.check_layout(os);
2493 begin_inset(os, "VSpace ");
2501 // LyX can't handle other length variables in Inset VSpace
2502 string name = t.asInput();
2507 handle_ert(os, name + '{' + unit + '}', context);
2508 else if (value == -1.0)
2509 handle_ert(os, name + "{-" + unit + '}', context);
2511 handle_ert(os, name + '{' + valstring + unit + '}', context);
2513 handle_ert(os, name + '{' + length + '}', context);
2518 // try to see whether the string is in unicodesymbols
2520 docstring s = encodings.fromLaTeXCommand(from_utf8(t.asInput()), rem);
2523 cerr << "When parsing " << t.cs()
2524 << ", result is " << to_utf8(s)
2525 << "+" << to_utf8(rem) << endl;
2526 context.check_layout(os);
2529 skip_braces(p); // eat {}
2531 //cerr << "#: " << t << " mode: " << mode << endl;
2532 // heuristic: read up to next non-nested space
2534 string s = t.asInput();
2535 string z = p.verbatim_item();
2536 while (p.good() && z != " " && z.size()) {
2537 //cerr << "read: " << z << endl;
2539 z = p.verbatim_item();
2541 cerr << "found ERT: " << s << endl;
2542 handle_ert(os, s + ' ', context);
2545 string name = t.asInput();
2546 if (p.next_token().asInput() == "*") {
2547 // Starred commands like \vspace*{}
2548 p.get_token(); // Eat '*'
2551 if (!parse_command(name, p, os, outer, context))
2552 handle_ert(os, name, context);
2556 if (flags & FLAG_LEAVE) {
2557 flags &= ~FLAG_LEAVE;