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, "OptArg\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 (LYX_FORMAT >= 392 && 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, "OptArg\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 << "width \"" << width_value << width_unit << "\"\n";
870 os << "special \"none\"\n";
871 os << "height \"" << height_value << height_unit << "\"\n";
872 os << "height_special \"" << height_special << "\"\n";
873 os << "status open\n\n";
874 Context context(true, parent_context.textclass);
875 context.font = parent_context.font;
877 // If we have no inner box the contens will be read with the outer box
878 if (!inner_type.empty())
879 parse_text(p, os, inner_flags, outer, context);
881 // Ensure that the end of the outer box is parsed correctly:
882 // The opening brace has been eaten by parse_outer_box()
883 if (!outer_type.empty() && (outer_flags & FLAG_ITEM)) {
884 outer_flags &= ~FLAG_ITEM;
885 outer_flags |= FLAG_BRACE_LAST;
888 // Find end of outer box, output contents if inner_type is
889 // empty and output possible comments
890 if (!outer_type.empty()) {
891 // This does not output anything but comments if
892 // inner_type is not empty (see use_ert)
893 parse_text(p, os, outer_flags, outer, context);
896 context.check_end_layout(os);
898 #ifdef PRESERVE_LAYOUT
899 // LyX puts a % after the end of the minipage
900 if (p.next_token().cat() == catNewline && p.next_token().cs().size() > 1) {
902 //handle_comment(os, "%dummy", parent_context);
905 parent_context.new_paragraph(os);
907 else if (p.next_token().cat() == catSpace || p.next_token().cat() == catNewline) {
908 //handle_comment(os, "%dummy", parent_context);
911 // We add a protected space if something real follows
912 if (p.good() && p.next_token().cat() != catComment) {
913 begin_inset(os, "space ~\n");
922 void parse_outer_box(Parser & p, ostream & os, unsigned flags, bool outer,
923 Context & parent_context, string const & outer_type,
924 string const & special)
926 eat_whitespace(p, os, parent_context, false);
927 if (flags & FLAG_ITEM) {
929 if (p.next_token().cat() == catBegin)
932 cerr << "Warning: Ignoring missing '{' after \\"
933 << outer_type << '.' << endl;
934 eat_whitespace(p, os, parent_context, false);
937 unsigned int inner_flags = 0;
938 if (outer_type == "shaded") {
939 // These boxes never have an inner box
941 } else if (p.next_token().asInput() == "\\parbox") {
942 inner = p.get_token().cs();
943 inner_flags = FLAG_ITEM;
944 } else if (p.next_token().asInput() == "\\begin") {
945 // Is this a minipage?
948 inner = p.getArg('{', '}');
950 if (inner == "minipage") {
953 eat_whitespace(p, os, parent_context, false);
954 inner_flags = FLAG_END;
958 if (inner_flags == FLAG_END) {
959 active_environments.push_back(inner);
960 parse_box(p, os, flags, FLAG_END, outer, parent_context,
961 outer_type, special, inner);
962 active_environments.pop_back();
964 parse_box(p, os, flags, inner_flags, outer, parent_context,
965 outer_type, special, inner);
970 void parse_listings(Parser & p, ostream & os, Context & parent_context)
972 parent_context.check_layout(os);
973 begin_inset(os, "listings\n");
974 os << "inline false\n"
975 << "status collapsed\n";
976 Context context(true, parent_context.textclass);
977 context.layout = &parent_context.textclass.plainLayout();
978 context.check_layout(os);
979 string const s = p.verbatimEnvironment("lstlisting");
980 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
982 os << "\n\\backslash\n";
983 else if (*it == '\n') {
984 // avoid adding an empty paragraph at the end
986 context.new_paragraph(os);
987 context.check_layout(os);
992 context.check_end_layout(os);
997 /// parse an unknown environment
998 void parse_unknown_environment(Parser & p, string const & name, ostream & os,
999 unsigned flags, bool outer,
1000 Context & parent_context)
1002 if (name == "tabbing")
1003 // We need to remember that we have to handle '\=' specially
1004 flags |= FLAG_TABBING;
1006 // We need to translate font changes and paragraphs inside the
1007 // environment to ERT if we have a non standard font.
1008 // Otherwise things like
1009 // \large\begin{foo}\huge bar\end{foo}
1011 bool const specialfont =
1012 (parent_context.font != parent_context.normalfont);
1013 bool const new_layout_allowed = parent_context.new_layout_allowed;
1015 parent_context.new_layout_allowed = false;
1016 handle_ert(os, "\\begin{" + name + "}", parent_context);
1017 parse_text_snippet(p, os, flags, outer, parent_context);
1018 handle_ert(os, "\\end{" + name + "}", parent_context);
1020 parent_context.new_layout_allowed = new_layout_allowed;
1024 void parse_environment(Parser & p, ostream & os, bool outer,
1025 string & last_env, Context & parent_context)
1027 Layout const * newlayout;
1028 InsetLayout const * newinsetlayout = 0;
1029 string const name = p.getArg('{', '}');
1030 const bool is_starred = suffixIs(name, '*');
1031 string const unstarred_name = rtrim(name, "*");
1032 active_environments.push_back(name);
1034 if (is_math_env(name)) {
1035 parent_context.check_layout(os);
1036 begin_inset(os, "Formula ");
1037 os << "\\begin{" << name << "}";
1038 parse_math(p, os, FLAG_END, MATH_MODE);
1039 os << "\\end{" << name << "}";
1043 else if (name == "tabular" || name == "longtable") {
1044 eat_whitespace(p, os, parent_context, false);
1045 parent_context.check_layout(os);
1046 begin_inset(os, "Tabular ");
1047 handle_tabular(p, os, name == "longtable", parent_context);
1052 else if (parent_context.textclass.floats().typeExist(unstarred_name)) {
1053 eat_whitespace(p, os, parent_context, false);
1054 parent_context.check_layout(os);
1055 begin_inset(os, "Float " + unstarred_name + "\n");
1057 os << "placement " << p.getArg('[', ']') << '\n';
1058 os << "wide " << convert<string>(is_starred)
1059 << "\nsideways false"
1060 << "\nstatus open\n\n";
1061 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
1063 // We don't need really a new paragraph, but
1064 // we must make sure that the next item gets a \begin_layout.
1065 parent_context.new_paragraph(os);
1069 else if (name == "minipage") {
1070 eat_whitespace(p, os, parent_context, false);
1071 parse_box(p, os, 0, FLAG_END, outer, parent_context, "", "", name);
1075 else if (name == "comment") {
1076 eat_whitespace(p, os, parent_context, false);
1077 parent_context.check_layout(os);
1078 begin_inset(os, "Note Comment\n");
1079 os << "status open\n";
1080 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
1083 skip_braces(p); // eat {} that might by set by LyX behind comments
1086 else if (name == "lyxgreyedout") {
1087 eat_whitespace(p, os, parent_context, false);
1088 parent_context.check_layout(os);
1089 begin_inset(os, "Note Greyedout\n");
1090 os << "status open\n";
1091 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
1096 else if (name == "framed" || name == "shaded") {
1097 eat_whitespace(p, os, parent_context, false);
1098 parse_outer_box(p, os, FLAG_END, outer, parent_context, name, "");
1102 else if (name == "lstlisting") {
1103 eat_whitespace(p, os, parent_context, false);
1104 // FIXME handle listings with parameters
1106 parse_unknown_environment(p, name, os, FLAG_END,
1107 outer, parent_context);
1109 parse_listings(p, os, parent_context);
1113 else if (!parent_context.new_layout_allowed)
1114 parse_unknown_environment(p, name, os, FLAG_END, outer,
1117 // Alignment and spacing settings
1118 // FIXME (bug xxxx): These settings can span multiple paragraphs and
1119 // therefore are totally broken!
1120 // Note that \centering, raggedright, and raggedleft cannot be handled, as
1121 // they are commands not environments. They are furthermore switches that
1122 // can be ended by another switches, but also by commands like \footnote or
1123 // \parbox. So the only safe way is to leave them untouched.
1124 else if (name == "center" || name == "centering" ||
1125 name == "flushleft" || name == "flushright" ||
1126 name == "singlespace" || name == "onehalfspace" ||
1127 name == "doublespace" || name == "spacing") {
1128 eat_whitespace(p, os, parent_context, false);
1129 // We must begin a new paragraph if not already done
1130 if (! parent_context.atParagraphStart()) {
1131 parent_context.check_end_layout(os);
1132 parent_context.new_paragraph(os);
1134 if (name == "flushleft")
1135 parent_context.add_extra_stuff("\\align left\n");
1136 else if (name == "flushright")
1137 parent_context.add_extra_stuff("\\align right\n");
1138 else if (name == "center" || name == "centering")
1139 parent_context.add_extra_stuff("\\align center\n");
1140 else if (name == "singlespace")
1141 parent_context.add_extra_stuff("\\paragraph_spacing single\n");
1142 else if (name == "onehalfspace")
1143 parent_context.add_extra_stuff("\\paragraph_spacing onehalf\n");
1144 else if (name == "doublespace")
1145 parent_context.add_extra_stuff("\\paragraph_spacing double\n");
1146 else if (name == "spacing")
1147 parent_context.add_extra_stuff("\\paragraph_spacing other " + p.verbatim_item() + "\n");
1148 parse_text(p, os, FLAG_END, outer, parent_context);
1149 // Just in case the environment is empty
1150 parent_context.extra_stuff.erase();
1151 // We must begin a new paragraph to reset the alignment
1152 parent_context.new_paragraph(os);
1156 // The single '=' is meant here.
1157 else if ((newlayout = findLayout(parent_context.textclass, name, false))) {
1158 eat_whitespace(p, os, parent_context, false);
1159 Context context(true, parent_context.textclass, newlayout,
1160 parent_context.layout, parent_context.font);
1161 if (parent_context.deeper_paragraph) {
1162 // We are beginning a nested environment after a
1163 // deeper paragraph inside the outer list environment.
1164 // Therefore we don't need to output a "begin deeper".
1165 context.need_end_deeper = true;
1167 parent_context.check_end_layout(os);
1168 if (last_env == name) {
1169 // we need to output a separator since LyX would export
1170 // the two environments as one otherwise (bug 5716)
1171 docstring const sep = from_ascii("--Separator--");
1172 TeX2LyXDocClass const & textclass(parent_context.textclass);
1173 if (textclass.hasLayout(sep)) {
1174 Context newcontext(parent_context);
1175 newcontext.layout = &(textclass[sep]);
1176 newcontext.check_layout(os);
1177 newcontext.check_end_layout(os);
1179 parent_context.check_layout(os);
1180 begin_inset(os, "Note Note\n");
1181 os << "status closed\n";
1182 Context newcontext(true, textclass,
1183 &(textclass.defaultLayout()));
1184 newcontext.check_layout(os);
1185 newcontext.check_end_layout(os);
1187 parent_context.check_end_layout(os);
1190 switch (context.layout->latextype) {
1191 case LATEX_LIST_ENVIRONMENT:
1192 context.add_par_extra_stuff("\\labelwidthstring "
1193 + p.verbatim_item() + '\n');
1196 case LATEX_BIB_ENVIRONMENT:
1197 p.verbatim_item(); // swallow next arg
1203 context.check_deeper(os);
1204 // handle known optional and required arguments
1205 // layouts require all optional arguments before the required ones
1206 // Unfortunately LyX can't handle arguments of list arguments (bug 7468):
1207 // It is impossible to place anything after the environment name,
1208 // but before the first \\item.
1209 if (context.layout->latextype == LATEX_ENVIRONMENT) {
1210 bool need_layout = true;
1211 unsigned int optargs = 0;
1212 while (optargs < context.layout->optargs) {
1213 eat_whitespace(p, os, context, false);
1214 if (p.next_token().cat() == catEscape ||
1215 p.next_token().character() != '[')
1217 p.get_token(); // eat '['
1219 context.check_layout(os);
1220 need_layout = false;
1222 begin_inset(os, "OptArg\n");
1223 os << "status collapsed\n\n";
1224 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
1226 eat_whitespace(p, os, context, false);
1229 unsigned int reqargs = 0;
1230 while (LYX_FORMAT >= 392 && reqargs < context.layout->reqargs) {
1231 eat_whitespace(p, os, context, false);
1232 if (p.next_token().cat() != catBegin)
1234 p.get_token(); // eat '{'
1236 context.check_layout(os);
1237 need_layout = false;
1239 begin_inset(os, "OptArg\n");
1240 os << "status collapsed\n\n";
1241 parse_text_in_inset(p, os, FLAG_BRACE_LAST, outer, context);
1243 eat_whitespace(p, os, context, false);
1247 parse_text(p, os, FLAG_END, outer, context);
1248 context.check_end_layout(os);
1249 if (parent_context.deeper_paragraph) {
1250 // We must suppress the "end deeper" because we
1251 // suppressed the "begin deeper" above.
1252 context.need_end_deeper = false;
1254 context.check_end_deeper(os);
1255 parent_context.new_paragraph(os);
1259 // The single '=' is meant here.
1260 else if ((newinsetlayout = findInsetLayout(parent_context.textclass, name, false))) {
1261 eat_whitespace(p, os, parent_context, false);
1262 parent_context.check_layout(os);
1263 begin_inset(os, "Flex ");
1264 os << to_utf8(newinsetlayout->name()) << '\n'
1265 << "status collapsed\n";
1266 parse_text_in_inset(p, os, FLAG_END, false, parent_context, newinsetlayout);
1270 else if (name == "appendix") {
1271 // This is no good latex style, but it works and is used in some documents...
1272 eat_whitespace(p, os, parent_context, false);
1273 parent_context.check_end_layout(os);
1274 Context context(true, parent_context.textclass, parent_context.layout,
1275 parent_context.layout, parent_context.font);
1276 context.check_layout(os);
1277 os << "\\start_of_appendix\n";
1278 parse_text(p, os, FLAG_END, outer, context);
1279 context.check_end_layout(os);
1283 else if (known_environments.find(name) != known_environments.end()) {
1284 vector<ArgumentType> arguments = known_environments[name];
1285 // The last "argument" denotes wether we may translate the
1286 // environment contents to LyX
1287 // The default required if no argument is given makes us
1288 // compatible with the reLyXre environment.
1289 ArgumentType contents = arguments.empty() ?
1292 if (!arguments.empty())
1293 arguments.pop_back();
1294 // See comment in parse_unknown_environment()
1295 bool const specialfont =
1296 (parent_context.font != parent_context.normalfont);
1297 bool const new_layout_allowed =
1298 parent_context.new_layout_allowed;
1300 parent_context.new_layout_allowed = false;
1301 parse_arguments("\\begin{" + name + "}", arguments, p, os,
1302 outer, parent_context);
1303 if (contents == verbatim)
1304 handle_ert(os, p.verbatimEnvironment(name),
1307 parse_text_snippet(p, os, FLAG_END, outer,
1309 handle_ert(os, "\\end{" + name + "}", parent_context);
1311 parent_context.new_layout_allowed = new_layout_allowed;
1315 parse_unknown_environment(p, name, os, FLAG_END, outer,
1319 active_environments.pop_back();
1323 /// parses a comment and outputs it to \p os.
1324 void parse_comment(Parser & p, ostream & os, Token const & t, Context & context)
1326 LASSERT(t.cat() == catComment, return);
1327 if (!t.cs().empty()) {
1328 context.check_layout(os);
1329 handle_comment(os, '%' + t.cs(), context);
1330 if (p.next_token().cat() == catNewline) {
1331 // A newline after a comment line starts a new
1333 if (context.new_layout_allowed) {
1334 if(!context.atParagraphStart())
1335 // Only start a new paragraph if not already
1336 // done (we might get called recursively)
1337 context.new_paragraph(os);
1339 handle_ert(os, "\n", context);
1340 eat_whitespace(p, os, context, true);
1343 // "%\n" combination
1350 * Reads spaces and comments until the first non-space, non-comment token.
1351 * New paragraphs (double newlines or \\par) are handled like simple spaces
1352 * if \p eatParagraph is true.
1353 * Spaces are skipped, but comments are written to \p os.
1355 void eat_whitespace(Parser & p, ostream & os, Context & context,
1359 Token const & t = p.get_token();
1360 if (t.cat() == catComment)
1361 parse_comment(p, os, t, context);
1362 else if ((! eatParagraph && p.isParagraph()) ||
1363 (t.cat() != catSpace && t.cat() != catNewline)) {
1372 * Set a font attribute, parse text and reset the font attribute.
1373 * \param attribute Attribute name (e.g. \\family, \\shape etc.)
1374 * \param currentvalue Current value of the attribute. Is set to the new
1375 * value during parsing.
1376 * \param newvalue New value of the attribute
1378 void parse_text_attributes(Parser & p, ostream & os, unsigned flags, bool outer,
1379 Context & context, string const & attribute,
1380 string & currentvalue, string const & newvalue)
1382 context.check_layout(os);
1383 string const oldvalue = currentvalue;
1384 currentvalue = newvalue;
1385 os << '\n' << attribute << ' ' << newvalue << "\n";
1386 parse_text_snippet(p, os, flags, outer, context);
1387 context.check_layout(os);
1388 os << '\n' << attribute << ' ' << oldvalue << "\n";
1389 currentvalue = oldvalue;
1393 /// get the arguments of a natbib or jurabib citation command
1394 void get_cite_arguments(Parser & p, bool natbibOrder,
1395 string & before, string & after)
1397 // We need to distinguish "" and "[]", so we can't use p.getOpt().
1399 // text before the citation
1401 // text after the citation
1402 after = p.getFullOpt();
1404 if (!after.empty()) {
1405 before = p.getFullOpt();
1406 if (natbibOrder && !before.empty())
1407 swap(before, after);
1412 /// Convert filenames with TeX macros and/or quotes to something LyX
1414 string const normalize_filename(string const & name)
1416 Parser p(trim(name, "\""));
1419 Token const & t = p.get_token();
1420 if (t.cat() != catEscape)
1422 else if (t.cs() == "lyxdot") {
1423 // This is used by LyX for simple dots in relative
1427 } else if (t.cs() == "space") {
1437 /// Convert \p name from TeX convention (relative to master file) to LyX
1438 /// convention (relative to .lyx file) if it is relative
1439 void fix_relative_filename(string & name)
1441 if (FileName::isAbsolute(name))
1444 name = to_utf8(makeRelPath(from_utf8(makeAbsPath(name, getMasterFilePath()).absFileName()),
1445 from_utf8(getParentFilePath())));
1449 /// Parse a NoWeb Scrap section. The initial "<<" is already parsed.
1450 void parse_noweb(Parser & p, ostream & os, Context & context)
1452 // assemble the rest of the keyword
1456 Token const & t = p.get_token();
1457 if (t.asInput() == ">" && p.next_token().asInput() == ">") {
1460 scrap = (p.good() && p.next_token().asInput() == "=");
1462 name += p.get_token().asInput();
1465 name += t.asInput();
1468 if (!scrap || !context.new_layout_allowed ||
1469 !context.textclass.hasLayout(from_ascii("Scrap"))) {
1470 cerr << "Warning: Could not interpret '" << name
1471 << "'. Ignoring it." << endl;
1475 // We use new_paragraph instead of check_end_layout because the stuff
1476 // following the noweb chunk needs to start with a \begin_layout.
1477 // This may create a new paragraph even if there was none in the
1478 // noweb file, but the alternative is an invalid LyX file. Since
1479 // noweb code chunks are implemented with a layout style in LyX they
1480 // always must be in an own paragraph.
1481 context.new_paragraph(os);
1482 Context newcontext(true, context.textclass,
1483 &context.textclass[from_ascii("Scrap")]);
1484 newcontext.check_layout(os);
1487 Token const & t = p.get_token();
1488 // We abuse the parser a bit, because this is no TeX syntax
1490 if (t.cat() == catEscape)
1491 os << subst(t.asInput(), "\\", "\n\\backslash\n");
1494 begin_inset(oss, "Newline newline");
1496 os << subst(t.asInput(), "\n", oss.str());
1498 // The scrap chunk is ended by an @ at the beginning of a line.
1499 // After the @ the line may contain a comment and/or
1500 // whitespace, but nothing else.
1501 if (t.asInput() == "@" && p.prev_token().cat() == catNewline &&
1502 (p.next_token().cat() == catSpace ||
1503 p.next_token().cat() == catNewline ||
1504 p.next_token().cat() == catComment)) {
1505 while (p.good() && p.next_token().cat() == catSpace)
1506 os << p.get_token().asInput();
1507 if (p.next_token().cat() == catComment)
1508 // The comment includes a final '\n'
1509 os << p.get_token().asInput();
1511 if (p.next_token().cat() == catNewline)
1518 newcontext.check_end_layout(os);
1522 /// detects \\def, \\long\\def and \\global\\long\\def with ws and comments
1523 bool is_macro(Parser & p)
1525 Token first = p.curr_token();
1526 if (first.cat() != catEscape || !p.good())
1528 if (first.cs() == "def")
1530 if (first.cs() != "global" && first.cs() != "long")
1532 Token second = p.get_token();
1534 while (p.good() && !p.isParagraph() && (second.cat() == catSpace ||
1535 second.cat() == catNewline || second.cat() == catComment)) {
1536 second = p.get_token();
1539 bool secondvalid = second.cat() == catEscape;
1541 bool thirdvalid = false;
1542 if (p.good() && first.cs() == "global" && secondvalid &&
1543 second.cs() == "long") {
1544 third = p.get_token();
1546 while (p.good() && !p.isParagraph() &&
1547 (third.cat() == catSpace ||
1548 third.cat() == catNewline ||
1549 third.cat() == catComment)) {
1550 third = p.get_token();
1553 thirdvalid = third.cat() == catEscape;
1555 for (int i = 0; i < pos; ++i)
1560 return (first.cs() == "global" || first.cs() == "long") &&
1561 second.cs() == "def";
1562 return first.cs() == "global" && second.cs() == "long" &&
1563 third.cs() == "def";
1567 /// Parse a macro definition (assumes that is_macro() returned true)
1568 void parse_macro(Parser & p, ostream & os, Context & context)
1570 context.check_layout(os);
1571 Token first = p.curr_token();
1574 string command = first.asInput();
1575 if (first.cs() != "def") {
1577 eat_whitespace(p, os, context, false);
1578 second = p.curr_token();
1579 command += second.asInput();
1580 if (second.cs() != "def") {
1582 eat_whitespace(p, os, context, false);
1583 third = p.curr_token();
1584 command += third.asInput();
1587 eat_whitespace(p, os, context, false);
1588 string const name = p.get_token().cs();
1589 eat_whitespace(p, os, context, false);
1595 while (p.next_token().cat() != catBegin) {
1596 if (p.next_token().cat() == catParameter) {
1601 // followed by number?
1602 if (p.next_token().cat() == catOther) {
1603 char c = p.getChar();
1605 // number = current arity + 1?
1606 if (c == arity + '0' + 1)
1611 paramtext += p.get_token().cs();
1613 paramtext += p.get_token().cs();
1618 // only output simple (i.e. compatible) macro as FormulaMacros
1619 string ert = '\\' + name + ' ' + paramtext + '{' + p.verbatim_item() + '}';
1621 context.check_layout(os);
1622 begin_inset(os, "FormulaMacro");
1623 os << "\n\\def" << ert;
1626 handle_ert(os, command + ert, context);
1629 } // anonymous namespace
1632 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
1635 Layout const * newlayout = 0;
1636 InsetLayout const * newinsetlayout = 0;
1637 // Store the latest bibliographystyle and nocite{*} option
1638 // (needed for bibtex inset)
1640 string bibliographystyle;
1641 bool const use_natbib = used_packages.find("natbib") != used_packages.end();
1642 bool const use_jurabib = used_packages.find("jurabib") != used_packages.end();
1645 Token const & t = p.get_token();
1648 debugToken(cerr, t, flags);
1651 if (flags & FLAG_ITEM) {
1652 if (t.cat() == catSpace)
1655 flags &= ~FLAG_ITEM;
1656 if (t.cat() == catBegin) {
1657 // skip the brace and collect everything to the next matching
1659 flags |= FLAG_BRACE_LAST;
1663 // handle only this single token, leave the loop if done
1664 flags |= FLAG_LEAVE;
1667 if (t.cat() != catEscape && t.character() == ']' &&
1668 (flags & FLAG_BRACK_LAST))
1670 if (t.cat() == catEnd && (flags & FLAG_BRACE_LAST))
1673 // If there is anything between \end{env} and \begin{env} we
1674 // don't need to output a separator.
1675 if (t.cat() != catSpace && t.cat() != catNewline &&
1676 t.asInput() != "\\begin")
1682 if (t.cat() == catMath) {
1683 // we are inside some text mode thingy, so opening new math is allowed
1684 context.check_layout(os);
1685 begin_inset(os, "Formula ");
1686 Token const & n = p.get_token();
1687 if (n.cat() == catMath && outer) {
1688 // TeX's $$...$$ syntax for displayed math
1690 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
1692 p.get_token(); // skip the second '$' token
1694 // simple $...$ stuff
1697 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
1703 else if (t.cat() == catSuper || t.cat() == catSub)
1704 cerr << "catcode " << t << " illegal in text mode\n";
1706 // Basic support for english quotes. This should be
1707 // extended to other quotes, but is not so easy (a
1708 // left english quote is the same as a right german
1710 else if (t.asInput() == "`" && p.next_token().asInput() == "`") {
1711 context.check_layout(os);
1712 begin_inset(os, "Quotes ");
1718 else if (t.asInput() == "'" && p.next_token().asInput() == "'") {
1719 context.check_layout(os);
1720 begin_inset(os, "Quotes ");
1727 else if (t.asInput() == ">" && p.next_token().asInput() == ">") {
1728 context.check_layout(os);
1729 begin_inset(os, "Quotes ");
1736 else if (t.asInput() == "<" && p.next_token().asInput() == "<") {
1737 context.check_layout(os);
1738 begin_inset(os, "Quotes ");
1745 else if (t.asInput() == "<"
1746 && p.next_token().asInput() == "<" && noweb_mode) {
1748 parse_noweb(p, os, context);
1751 else if (t.cat() == catSpace || (t.cat() == catNewline && ! p.isParagraph()))
1752 check_space(p, os, context);
1754 else if (t.character() == '[' && noweb_mode &&
1755 p.next_token().character() == '[') {
1756 // These can contain underscores
1758 string const s = p.getFullOpt() + ']';
1759 if (p.next_token().character() == ']')
1762 cerr << "Warning: Inserting missing ']' in '"
1763 << s << "'." << endl;
1764 handle_ert(os, s, context);
1767 else if (t.cat() == catLetter) {
1768 context.check_layout(os);
1769 // Workaround for bug 4752.
1770 // FIXME: This whole code block needs to be removed
1771 // when the bug is fixed and tex2lyx produces
1772 // the updated file format.
1773 // The replacement algorithm in LyX is so stupid that
1774 // it even translates a phrase if it is part of a word.
1775 bool handled = false;
1776 for (int const * l = known_phrase_lengths; *l; ++l) {
1777 string phrase = t.cs();
1778 for (int i = 1; i < *l && p.next_token().isAlnumASCII(); ++i)
1779 phrase += p.get_token().cs();
1780 if (is_known(phrase, known_coded_phrases)) {
1781 handle_ert(os, phrase, context);
1785 for (size_t i = 1; i < phrase.length(); ++i)
1793 else if (t.cat() == catOther ||
1794 t.cat() == catAlign ||
1795 t.cat() == catParameter) {
1796 // This translates "&" to "\\&" which may be wrong...
1797 context.check_layout(os);
1801 else if (p.isParagraph()) {
1802 if (context.new_layout_allowed)
1803 context.new_paragraph(os);
1805 handle_ert(os, "\\par ", context);
1806 eat_whitespace(p, os, context, true);
1809 else if (t.cat() == catActive) {
1810 context.check_layout(os);
1811 if (t.character() == '~') {
1812 if (context.layout->free_spacing)
1815 begin_inset(os, "space ~\n");
1822 else if (t.cat() == catBegin &&
1823 p.next_token().cat() == catEnd) {
1825 Token const prev = p.prev_token();
1827 if (p.next_token().character() == '`' ||
1828 (prev.character() == '-' &&
1829 p.next_token().character() == '-'))
1830 ; // ignore it in {}`` or -{}-
1832 handle_ert(os, "{}", context);
1836 else if (t.cat() == catBegin) {
1837 context.check_layout(os);
1838 // special handling of font attribute changes
1839 Token const prev = p.prev_token();
1840 Token const next = p.next_token();
1841 TeXFont const oldFont = context.font;
1842 if (next.character() == '[' ||
1843 next.character() == ']' ||
1844 next.character() == '*') {
1846 if (p.next_token().cat() == catEnd) {
1851 handle_ert(os, "{", context);
1852 parse_text_snippet(p, os,
1855 handle_ert(os, "}", context);
1857 } else if (! context.new_layout_allowed) {
1858 handle_ert(os, "{", context);
1859 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1861 handle_ert(os, "}", context);
1862 } else if (is_known(next.cs(), known_sizes)) {
1863 // next will change the size, so we must
1865 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1867 if (!context.atParagraphStart())
1869 << context.font.size << "\n";
1870 } else if (is_known(next.cs(), known_font_families)) {
1871 // next will change the font family, so we
1872 // must reset it here
1873 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1875 if (!context.atParagraphStart())
1877 << context.font.family << "\n";
1878 } else if (is_known(next.cs(), known_font_series)) {
1879 // next will change the font series, so we
1880 // must reset it here
1881 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1883 if (!context.atParagraphStart())
1885 << context.font.series << "\n";
1886 } else if (is_known(next.cs(), known_font_shapes)) {
1887 // next will change the font shape, so we
1888 // must reset it here
1889 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1891 if (!context.atParagraphStart())
1893 << context.font.shape << "\n";
1894 } else if (is_known(next.cs(), known_old_font_families) ||
1895 is_known(next.cs(), known_old_font_series) ||
1896 is_known(next.cs(), known_old_font_shapes)) {
1897 // next will change the font family, series
1898 // and shape, so we must reset it here
1899 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1901 if (!context.atParagraphStart())
1903 << context.font.family
1905 << context.font.series
1907 << context.font.shape << "\n";
1909 handle_ert(os, "{", context);
1910 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1912 handle_ert(os, "}", context);
1916 else if (t.cat() == catEnd) {
1917 if (flags & FLAG_BRACE_LAST) {
1920 cerr << "stray '}' in text\n";
1921 handle_ert(os, "}", context);
1924 else if (t.cat() == catComment)
1925 parse_comment(p, os, t, context);
1928 // control sequences
1931 else if (t.cs() == "(") {
1932 context.check_layout(os);
1933 begin_inset(os, "Formula");
1935 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
1940 else if (t.cs() == "[") {
1941 context.check_layout(os);
1942 begin_inset(os, "Formula");
1944 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
1949 else if (t.cs() == "begin")
1950 parse_environment(p, os, outer, last_env, context);
1952 else if (t.cs() == "end") {
1953 if (flags & FLAG_END) {
1954 // eat environment name
1955 string const name = p.getArg('{', '}');
1956 if (name != active_environment())
1957 cerr << "\\end{" + name + "} does not match \\begin{"
1958 + active_environment() + "}\n";
1961 p.error("found 'end' unexpectedly");
1964 else if (t.cs() == "item") {
1967 bool optarg = false;
1968 if (p.next_token().cat() != catEscape &&
1969 p.next_token().character() == '[') {
1970 p.get_token(); // eat '['
1971 s = parse_text_snippet(p, FLAG_BRACK_LAST,
1976 context.check_layout(os);
1977 if (context.has_item) {
1978 // An item in an unknown list-like environment
1979 // FIXME: Do this in check_layout()!
1980 context.has_item = false;
1982 handle_ert(os, "\\item", context);
1984 handle_ert(os, "\\item ", context);
1987 if (context.layout->labeltype != LABEL_MANUAL) {
1988 // LyX does not support \item[\mybullet]
1989 // in itemize environments
1990 handle_ert(os, "[", context);
1992 handle_ert(os, "]", context);
1993 } else if (!s.empty()) {
1994 // The space is needed to separate the
1995 // item from the rest of the sentence.
1997 eat_whitespace(p, os, context, false);
2002 else if (t.cs() == "bibitem") {
2004 context.check_layout(os);
2005 string label = convert_command_inset_arg(p.getArg('[', ']'));
2006 string key = convert_command_inset_arg(p.verbatim_item());
2007 if (contains(label, '\\') || contains(key, '\\')) {
2008 // LyX can't handle LaTeX commands in labels or keys
2009 handle_ert(os, t.asInput() + '[' + label +
2010 "]{" + p.verbatim_item() + '}',
2013 begin_command_inset(os, "bibitem", "bibitem");
2014 os << "label \"" << label << "\"\n"
2015 "key \"" << key << "\"\n";
2020 else if (is_macro(p))
2021 parse_macro(p, os, context);
2023 else if (t.cs() == "noindent") {
2025 context.add_par_extra_stuff("\\noindent\n");
2028 else if (t.cs() == "appendix") {
2029 context.add_par_extra_stuff("\\start_of_appendix\n");
2030 // We need to start a new paragraph. Otherwise the
2031 // appendix in 'bla\appendix\chapter{' would start
2033 context.new_paragraph(os);
2034 // We need to make sure that the paragraph is
2035 // generated even if it is empty. Otherwise the
2036 // appendix in '\par\appendix\par\chapter{' would
2038 context.check_layout(os);
2039 // FIXME: This is a hack to prevent paragraph
2040 // deletion if it is empty. Handle this better!
2042 "%dummy comment inserted by tex2lyx to "
2043 "ensure that this paragraph is not empty",
2045 // Both measures above may generate an additional
2046 // empty paragraph, but that does not hurt, because
2047 // whitespace does not matter here.
2048 eat_whitespace(p, os, context, true);
2051 // Starred section headings
2052 // Must attempt to parse "Section*" before "Section".
2053 else if ((p.next_token().asInput() == "*") &&
2054 context.new_layout_allowed &&
2055 (newlayout = findLayout(context.textclass, t.cs() + '*', true))) {
2058 output_command_layout(os, p, outer, context, newlayout);
2062 // Section headings and the like
2063 else if (context.new_layout_allowed &&
2064 (newlayout = findLayout(context.textclass, t.cs(), true))) {
2066 output_command_layout(os, p, outer, context, newlayout);
2070 else if (t.cs() == "caption") {
2072 context.check_layout(os);
2074 begin_inset(os, "Caption\n\n");
2075 Context newcontext(true, context.textclass);
2076 newcontext.font = context.font;
2077 newcontext.check_layout(os);
2078 if (p.next_token().cat() != catEscape &&
2079 p.next_token().character() == '[') {
2080 p.get_token(); // eat '['
2081 begin_inset(os, "OptArg\n");
2082 os << "status collapsed\n";
2083 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
2085 eat_whitespace(p, os, context, false);
2087 parse_text(p, os, FLAG_ITEM, outer, context);
2088 context.check_end_layout(os);
2089 // We don't need really a new paragraph, but
2090 // we must make sure that the next item gets a \begin_layout.
2091 context.new_paragraph(os);
2094 newcontext.check_end_layout(os);
2097 else if (t.cs() == "includegraphics") {
2098 bool const clip = p.next_token().asInput() == "*";
2101 string const arg = p.getArg('[', ']');
2102 map<string, string> opts;
2103 vector<string> keys;
2104 split_map(arg, opts, keys);
2106 opts["clip"] = string();
2107 string name = normalize_filename(p.verbatim_item());
2109 string const path = getMasterFilePath();
2110 // We want to preserve relative / absolute filenames,
2111 // therefore path is only used for testing
2112 if (!makeAbsPath(name, path).exists()) {
2113 // The file extension is probably missing.
2114 // Now try to find it out.
2115 string const dvips_name =
2116 find_file(name, path,
2117 known_dvips_graphics_formats);
2118 string const pdftex_name =
2119 find_file(name, path,
2120 known_pdftex_graphics_formats);
2121 if (!dvips_name.empty()) {
2122 if (!pdftex_name.empty()) {
2123 cerr << "This file contains the "
2125 "\"\\includegraphics{"
2127 "However, files\n\""
2128 << dvips_name << "\" and\n\""
2129 << pdftex_name << "\"\n"
2130 "both exist, so I had to make a "
2131 "choice and took the first one.\n"
2132 "Please move the unwanted one "
2133 "someplace else and try again\n"
2134 "if my choice was wrong."
2138 } else if (!pdftex_name.empty()) {
2144 if (makeAbsPath(name, path).exists())
2145 fix_relative_filename(name);
2147 cerr << "Warning: Could not find graphics file '"
2148 << name << "'." << endl;
2150 context.check_layout(os);
2151 begin_inset(os, "Graphics ");
2152 os << "\n\tfilename " << name << '\n';
2153 if (opts.find("width") != opts.end())
2155 << translate_len(opts["width"]) << '\n';
2156 if (opts.find("height") != opts.end())
2158 << translate_len(opts["height"]) << '\n';
2159 if (opts.find("scale") != opts.end()) {
2160 istringstream iss(opts["scale"]);
2164 os << "\tscale " << val << '\n';
2166 if (opts.find("angle") != opts.end()) {
2167 os << "\trotateAngle "
2168 << opts["angle"] << '\n';
2169 vector<string>::const_iterator a =
2170 find(keys.begin(), keys.end(), "angle");
2171 vector<string>::const_iterator s =
2172 find(keys.begin(), keys.end(), "width");
2173 if (s == keys.end())
2174 s = find(keys.begin(), keys.end(), "height");
2175 if (s == keys.end())
2176 s = find(keys.begin(), keys.end(), "scale");
2177 if (s != keys.end() && distance(s, a) > 0)
2178 os << "\tscaleBeforeRotation\n";
2180 if (opts.find("origin") != opts.end()) {
2182 string const opt = opts["origin"];
2183 if (opt.find('l') != string::npos) ss << "left";
2184 if (opt.find('r') != string::npos) ss << "right";
2185 if (opt.find('c') != string::npos) ss << "center";
2186 if (opt.find('t') != string::npos) ss << "Top";
2187 if (opt.find('b') != string::npos) ss << "Bottom";
2188 if (opt.find('B') != string::npos) ss << "Baseline";
2189 if (!ss.str().empty())
2190 os << "\trotateOrigin " << ss.str() << '\n';
2192 cerr << "Warning: Ignoring unknown includegraphics origin argument '" << opt << "'\n";
2194 if (opts.find("keepaspectratio") != opts.end())
2195 os << "\tkeepAspectRatio\n";
2196 if (opts.find("clip") != opts.end())
2198 if (opts.find("draft") != opts.end())
2200 if (opts.find("bb") != opts.end())
2201 os << "\tBoundingBox "
2202 << opts["bb"] << '\n';
2203 int numberOfbbOptions = 0;
2204 if (opts.find("bbllx") != opts.end())
2205 numberOfbbOptions++;
2206 if (opts.find("bblly") != opts.end())
2207 numberOfbbOptions++;
2208 if (opts.find("bburx") != opts.end())
2209 numberOfbbOptions++;
2210 if (opts.find("bbury") != opts.end())
2211 numberOfbbOptions++;
2212 if (numberOfbbOptions == 4)
2213 os << "\tBoundingBox "
2214 << opts["bbllx"] << " " << opts["bblly"] << " "
2215 << opts["bburx"] << " " << opts["bbury"] << '\n';
2216 else if (numberOfbbOptions > 0)
2217 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
2218 numberOfbbOptions = 0;
2219 if (opts.find("natwidth") != opts.end())
2220 numberOfbbOptions++;
2221 if (opts.find("natheight") != opts.end())
2222 numberOfbbOptions++;
2223 if (numberOfbbOptions == 2)
2224 os << "\tBoundingBox 0bp 0bp "
2225 << opts["natwidth"] << " " << opts["natheight"] << '\n';
2226 else if (numberOfbbOptions > 0)
2227 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
2228 ostringstream special;
2229 if (opts.find("hiresbb") != opts.end())
2230 special << "hiresbb,";
2231 if (opts.find("trim") != opts.end())
2233 if (opts.find("viewport") != opts.end())
2234 special << "viewport=" << opts["viewport"] << ',';
2235 if (opts.find("totalheight") != opts.end())
2236 special << "totalheight=" << opts["totalheight"] << ',';
2237 if (opts.find("type") != opts.end())
2238 special << "type=" << opts["type"] << ',';
2239 if (opts.find("ext") != opts.end())
2240 special << "ext=" << opts["ext"] << ',';
2241 if (opts.find("read") != opts.end())
2242 special << "read=" << opts["read"] << ',';
2243 if (opts.find("command") != opts.end())
2244 special << "command=" << opts["command"] << ',';
2245 string s_special = special.str();
2246 if (!s_special.empty()) {
2247 // We had special arguments. Remove the trailing ','.
2248 os << "\tspecial " << s_special.substr(0, s_special.size() - 1) << '\n';
2250 // TODO: Handle the unknown settings better.
2251 // Warn about invalid options.
2252 // Check whether some option was given twice.
2256 else if (t.cs() == "footnote" ||
2257 (t.cs() == "thanks" && context.layout->intitle)) {
2259 context.check_layout(os);
2260 begin_inset(os, "Foot\n");
2261 os << "status collapsed\n\n";
2262 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
2266 else if (t.cs() == "marginpar") {
2268 context.check_layout(os);
2269 begin_inset(os, "Marginal\n");
2270 os << "status collapsed\n\n";
2271 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
2275 else if (t.cs() == "ensuremath") {
2277 context.check_layout(os);
2278 string const s = p.verbatim_item();
2279 //FIXME: this never triggers in UTF8
2280 if (s == "\xb1" || s == "\xb3" || s == "\xb2" || s == "\xb5")
2283 handle_ert(os, "\\ensuremath{" + s + "}",
2287 else if (t.cs() == "makeindex" || t.cs() == "maketitle") {
2288 // FIXME: Somehow prevent title layouts if
2289 // "maketitle" was not found
2291 skip_spaces_braces(p);
2294 else if (t.cs() == "tableofcontents") {
2295 context.check_layout(os);
2296 begin_command_inset(os, "toc", "tableofcontents");
2298 skip_spaces_braces(p);
2301 else if (t.cs() == "listoffigures") {
2302 context.check_layout(os);
2303 begin_inset(os, "FloatList figure\n");
2305 skip_spaces_braces(p);
2308 else if (t.cs() == "listoftables") {
2309 context.check_layout(os);
2310 begin_inset(os, "FloatList table\n");
2312 skip_spaces_braces(p);
2315 else if (t.cs() == "listof") {
2316 p.skip_spaces(true);
2317 string const name = p.get_token().cs();
2318 if (context.textclass.floats().typeExist(name)) {
2319 context.check_layout(os);
2320 begin_inset(os, "FloatList ");
2323 p.get_token(); // swallow second arg
2325 handle_ert(os, "\\listof{" + name + "}", context);
2328 else if (t.cs() == "textrm")
2329 parse_text_attributes(p, os, FLAG_ITEM, outer,
2330 context, "\\family",
2331 context.font.family, "roman");
2333 else if (t.cs() == "textsf")
2334 parse_text_attributes(p, os, FLAG_ITEM, outer,
2335 context, "\\family",
2336 context.font.family, "sans");
2338 else if (t.cs() == "texttt")
2339 parse_text_attributes(p, os, FLAG_ITEM, outer,
2340 context, "\\family",
2341 context.font.family, "typewriter");
2343 else if (t.cs() == "textmd")
2344 parse_text_attributes(p, os, FLAG_ITEM, outer,
2345 context, "\\series",
2346 context.font.series, "medium");
2348 else if (t.cs() == "textbf")
2349 parse_text_attributes(p, os, FLAG_ITEM, outer,
2350 context, "\\series",
2351 context.font.series, "bold");
2353 else if (t.cs() == "textup")
2354 parse_text_attributes(p, os, FLAG_ITEM, outer,
2356 context.font.shape, "up");
2358 else if (t.cs() == "textit")
2359 parse_text_attributes(p, os, FLAG_ITEM, outer,
2361 context.font.shape, "italic");
2363 else if (t.cs() == "textsl")
2364 parse_text_attributes(p, os, FLAG_ITEM, outer,
2366 context.font.shape, "slanted");
2368 else if (t.cs() == "textsc")
2369 parse_text_attributes(p, os, FLAG_ITEM, outer,
2371 context.font.shape, "smallcaps");
2373 else if (t.cs() == "textnormal" || t.cs() == "normalfont") {
2374 context.check_layout(os);
2375 TeXFont oldFont = context.font;
2376 context.font.init();
2377 context.font.size = oldFont.size;
2378 os << "\n\\family " << context.font.family << "\n";
2379 os << "\n\\series " << context.font.series << "\n";
2380 os << "\n\\shape " << context.font.shape << "\n";
2381 if (t.cs() == "textnormal") {
2382 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
2383 output_font_change(os, context.font, oldFont);
2384 context.font = oldFont;
2386 eat_whitespace(p, os, context, false);
2389 else if (t.cs() == "textcolor") {
2390 // scheme is \textcolor{color name}{text}
2391 string const color = p.verbatim_item();
2392 // we only support the predefined colors of the color package
2393 if (color == "black" || color == "blue" || color == "cyan"
2394 || color == "green" || color == "magenta" || color == "red"
2395 || color == "white" || color == "yellow") {
2396 context.check_layout(os);
2397 os << "\n\\color " << color << "\n";
2398 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
2399 context.check_layout(os);
2400 os << "\n\\color inherit\n";
2402 // for custom defined colors
2403 handle_ert(os, t.asInput() + "{" + color + "}", context);
2406 else if (t.cs() == "underbar" || t.cs() == "uline") {
2407 // Do NOT handle \underline.
2408 // \underbar cuts through y, g, q, p etc.,
2409 // \underline does not.
2410 context.check_layout(os);
2411 os << "\n\\bar under\n";
2412 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
2413 context.check_layout(os);
2414 os << "\n\\bar default\n";
2417 else if (t.cs() == "emph" || t.cs() == "noun") {
2418 context.check_layout(os);
2419 os << "\n\\" << t.cs() << " on\n";
2420 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
2421 context.check_layout(os);
2422 os << "\n\\" << t.cs() << " default\n";
2425 else if (t.cs() == "lyxline") {
2426 context.check_layout(os);
2430 else if (is_known(t.cs(), known_phrases) ||
2431 (t.cs() == "protect" &&
2432 p.next_token().cat() == catEscape &&
2433 is_known(p.next_token().cs(), known_phrases))) {
2434 // LyX sometimes puts a \protect in front, so we have to ignore it
2435 // FIXME: This needs to be changed when bug 4752 is fixed.
2436 char const * const * where = is_known(
2437 t.cs() == "protect" ? p.get_token().cs() : t.cs(),
2439 context.check_layout(os);
2440 os << known_coded_phrases[where - known_phrases];
2441 skip_spaces_braces(p);
2444 else if (is_known(t.cs(), known_ref_commands)) {
2445 string const opt = p.getOpt();
2447 context.check_layout(os);
2448 begin_command_inset(os, "ref", t.cs());
2449 os << "reference \""
2450 << convert_command_inset_arg(p.verbatim_item())
2454 // LyX does not support optional arguments of ref commands
2455 handle_ert(os, t.asInput() + '[' + opt + "]{" +
2456 p.verbatim_item() + "}", context);
2460 else if (use_natbib &&
2461 is_known(t.cs(), known_natbib_commands) &&
2462 ((t.cs() != "citefullauthor" &&
2463 t.cs() != "citeyear" &&
2464 t.cs() != "citeyearpar") ||
2465 p.next_token().asInput() != "*")) {
2466 context.check_layout(os);
2467 string command = t.cs();
2468 if (p.next_token().asInput() == "*") {
2472 if (command == "citefullauthor")
2473 // alternative name for "\\citeauthor*"
2474 command = "citeauthor*";
2476 // text before the citation
2478 // text after the citation
2480 get_cite_arguments(p, true, before, after);
2482 if (command == "cite") {
2483 // \cite without optional argument means
2484 // \citet, \cite with at least one optional
2485 // argument means \citep.
2486 if (before.empty() && after.empty())
2491 if (before.empty() && after == "[]")
2492 // avoid \citet[]{a}
2494 else if (before == "[]" && after == "[]") {
2495 // avoid \citet[][]{a}
2499 // remove the brackets around after and before
2500 if (!after.empty()) {
2502 after.erase(after.length() - 1, 1);
2503 after = convert_command_inset_arg(after);
2505 if (!before.empty()) {
2507 before.erase(before.length() - 1, 1);
2508 before = convert_command_inset_arg(before);
2510 begin_command_inset(os, "citation", command);
2511 os << "after " << '"' << after << '"' << "\n";
2512 os << "before " << '"' << before << '"' << "\n";
2514 << convert_command_inset_arg(p.verbatim_item())
2519 else if (use_jurabib &&
2520 is_known(t.cs(), known_jurabib_commands) &&
2521 (t.cs() == "cite" || p.next_token().asInput() != "*")) {
2522 context.check_layout(os);
2523 string command = t.cs();
2524 if (p.next_token().asInput() == "*") {
2528 char argumentOrder = '\0';
2529 vector<string> const & options = used_packages["jurabib"];
2530 if (find(options.begin(), options.end(),
2531 "natbiborder") != options.end())
2532 argumentOrder = 'n';
2533 else if (find(options.begin(), options.end(),
2534 "jurabiborder") != options.end())
2535 argumentOrder = 'j';
2537 // text before the citation
2539 // text after the citation
2541 get_cite_arguments(p, argumentOrder != 'j', before, after);
2543 string const citation = p.verbatim_item();
2544 if (!before.empty() && argumentOrder == '\0') {
2545 cerr << "Warning: Assuming argument order "
2546 "of jurabib version 0.6 for\n'"
2547 << command << before << after << '{'
2548 << citation << "}'.\n"
2549 "Add 'jurabiborder' to the jurabib "
2550 "package options if you used an\n"
2551 "earlier jurabib version." << endl;
2553 if (!after.empty()) {
2555 after.erase(after.length() - 1, 1);
2557 if (!before.empty()) {
2559 before.erase(before.length() - 1, 1);
2561 begin_command_inset(os, "citation", command);
2562 os << "after " << '"' << after << '"' << "\n";
2563 os << "before " << '"' << before << '"' << "\n";
2564 os << "key " << '"' << citation << '"' << "\n";
2568 else if (t.cs() == "cite"
2569 || t.cs() == "nocite") {
2570 context.check_layout(os);
2571 string after = convert_command_inset_arg(p.getArg('[', ']'));
2572 string key = convert_command_inset_arg(p.verbatim_item());
2573 // store the case that it is "\nocite{*}" to use it later for
2576 begin_command_inset(os, "citation", t.cs());
2577 os << "after " << '"' << after << '"' << "\n";
2578 os << "key " << '"' << key << '"' << "\n";
2580 } else if (t.cs() == "nocite")
2584 else if (t.cs() == "index") {
2585 context.check_layout(os);
2586 begin_inset(os, "Index\n");
2587 os << "status collapsed\n";
2588 parse_text_in_inset(p, os, FLAG_ITEM, false, context, "Index");
2592 else if (t.cs() == "nomenclature") {
2593 context.check_layout(os);
2594 begin_command_inset(os, "nomenclature", "nomenclature");
2595 string prefix = convert_command_inset_arg(p.getArg('[', ']'));
2596 if (!prefix.empty())
2597 os << "prefix " << '"' << prefix << '"' << "\n";
2598 os << "symbol " << '"'
2599 << convert_command_inset_arg(p.verbatim_item());
2600 os << "\"\ndescription \""
2601 << convert_command_inset_arg(p.verbatim_item())
2606 else if (t.cs() == "label") {
2607 context.check_layout(os);
2608 begin_command_inset(os, "label", "label");
2610 << convert_command_inset_arg(p.verbatim_item())
2615 else if (t.cs() == "printindex") {
2616 context.check_layout(os);
2617 begin_command_inset(os, "index_print", "printindex");
2619 skip_spaces_braces(p);
2622 else if (t.cs() == "printnomenclature") {
2623 context.check_layout(os);
2624 begin_command_inset(os, "nomencl_print", "printnomenclature");
2626 skip_spaces_braces(p);
2629 else if (LYX_FORMAT >= 408 &&
2630 (t.cs() == "textsuperscript" || t.cs() == "textsubscript")) {
2631 context.check_layout(os);
2632 begin_inset(os, "script ");
2633 os << t.cs().substr(4) << '\n';
2634 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
2638 else if (is_known(t.cs(), known_quotes)) {
2639 char const * const * where = is_known(t.cs(), known_quotes);
2640 context.check_layout(os);
2641 begin_inset(os, "Quotes ");
2642 os << known_coded_quotes[where - known_quotes];
2644 // LyX adds {} after the quote, so we have to eat
2645 // spaces here if there are any before a possible
2647 eat_whitespace(p, os, context, false);
2651 else if (is_known(t.cs(), known_sizes) &&
2652 context.new_layout_allowed) {
2653 char const * const * where = is_known(t.cs(), known_sizes);
2654 context.check_layout(os);
2655 TeXFont const oldFont = context.font;
2656 context.font.size = known_coded_sizes[where - known_sizes];
2657 output_font_change(os, oldFont, context.font);
2658 eat_whitespace(p, os, context, false);
2661 else if (is_known(t.cs(), known_font_families) &&
2662 context.new_layout_allowed) {
2663 char const * const * where =
2664 is_known(t.cs(), known_font_families);
2665 context.check_layout(os);
2666 TeXFont const oldFont = context.font;
2667 context.font.family =
2668 known_coded_font_families[where - known_font_families];
2669 output_font_change(os, oldFont, context.font);
2670 eat_whitespace(p, os, context, false);
2673 else if (is_known(t.cs(), known_font_series) &&
2674 context.new_layout_allowed) {
2675 char const * const * where =
2676 is_known(t.cs(), known_font_series);
2677 context.check_layout(os);
2678 TeXFont const oldFont = context.font;
2679 context.font.series =
2680 known_coded_font_series[where - known_font_series];
2681 output_font_change(os, oldFont, context.font);
2682 eat_whitespace(p, os, context, false);
2685 else if (is_known(t.cs(), known_font_shapes) &&
2686 context.new_layout_allowed) {
2687 char const * const * where =
2688 is_known(t.cs(), known_font_shapes);
2689 context.check_layout(os);
2690 TeXFont const oldFont = context.font;
2691 context.font.shape =
2692 known_coded_font_shapes[where - known_font_shapes];
2693 output_font_change(os, oldFont, context.font);
2694 eat_whitespace(p, os, context, false);
2696 else if (is_known(t.cs(), known_old_font_families) &&
2697 context.new_layout_allowed) {
2698 char const * const * where =
2699 is_known(t.cs(), known_old_font_families);
2700 context.check_layout(os);
2701 TeXFont const oldFont = context.font;
2702 context.font.init();
2703 context.font.size = oldFont.size;
2704 context.font.family =
2705 known_coded_font_families[where - known_old_font_families];
2706 output_font_change(os, oldFont, context.font);
2707 eat_whitespace(p, os, context, false);
2710 else if (is_known(t.cs(), known_old_font_series) &&
2711 context.new_layout_allowed) {
2712 char const * const * where =
2713 is_known(t.cs(), known_old_font_series);
2714 context.check_layout(os);
2715 TeXFont const oldFont = context.font;
2716 context.font.init();
2717 context.font.size = oldFont.size;
2718 context.font.series =
2719 known_coded_font_series[where - known_old_font_series];
2720 output_font_change(os, oldFont, context.font);
2721 eat_whitespace(p, os, context, false);
2724 else if (is_known(t.cs(), known_old_font_shapes) &&
2725 context.new_layout_allowed) {
2726 char const * const * where =
2727 is_known(t.cs(), known_old_font_shapes);
2728 context.check_layout(os);
2729 TeXFont const oldFont = context.font;
2730 context.font.init();
2731 context.font.size = oldFont.size;
2732 context.font.shape =
2733 known_coded_font_shapes[where - known_old_font_shapes];
2734 output_font_change(os, oldFont, context.font);
2735 eat_whitespace(p, os, context, false);
2738 else if (t.cs() == "selectlanguage") {
2739 context.check_layout(os);
2740 // save the language for the case that a
2741 // \foreignlanguage is used
2743 context.font.language = babel2lyx(p.verbatim_item());
2744 os << "\n\\lang " << context.font.language << "\n";
2747 else if (t.cs() == "foreignlanguage") {
2748 string const lang = babel2lyx(p.verbatim_item());
2749 parse_text_attributes(p, os, FLAG_ITEM, outer,
2751 context.font.language, lang);
2754 else if (t.cs() == "inputencoding") {
2755 // nothing to write here
2756 string const enc = subst(p.verbatim_item(), "\n", " ");
2760 else if (t.cs() == "ldots") {
2761 context.check_layout(os);
2762 os << "\\SpecialChar \\ldots{}\n";
2763 skip_spaces_braces(p);
2766 else if (t.cs() == "lyxarrow") {
2767 context.check_layout(os);
2768 os << "\\SpecialChar \\menuseparator\n";
2769 skip_spaces_braces(p);
2772 else if (t.cs() == "textcompwordmark") {
2773 context.check_layout(os);
2774 os << "\\SpecialChar \\textcompwordmark{}\n";
2775 skip_spaces_braces(p);
2778 else if (t.cs() == "slash") {
2779 context.check_layout(os);
2780 os << "\\SpecialChar \\slash{}\n";
2781 skip_spaces_braces(p);
2784 else if (t.cs() == "nobreakdash" && p.next_token().asInput() == "-") {
2785 context.check_layout(os);
2786 os << "\\SpecialChar \\nobreakdash-\n";
2790 else if (t.cs() == "textquotedbl") {
2791 context.check_layout(os);
2796 else if (t.cs() == "@" && p.next_token().asInput() == ".") {
2797 context.check_layout(os);
2798 os << "\\SpecialChar \\@.\n";
2802 else if (t.cs() == "-") {
2803 context.check_layout(os);
2804 os << "\\SpecialChar \\-\n";
2807 else if (t.cs() == "textasciitilde") {
2808 context.check_layout(os);
2810 skip_spaces_braces(p);
2813 else if (t.cs() == "textasciicircum") {
2814 context.check_layout(os);
2816 skip_spaces_braces(p);
2819 else if (t.cs() == "textbackslash") {
2820 context.check_layout(os);
2821 os << "\n\\backslash\n";
2822 skip_spaces_braces(p);
2825 else if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#"
2826 || t.cs() == "$" || t.cs() == "{" || t.cs() == "}"
2828 context.check_layout(os);
2832 else if (t.cs() == "char") {
2833 context.check_layout(os);
2834 if (p.next_token().character() == '`') {
2836 if (p.next_token().cs() == "\"") {
2841 handle_ert(os, "\\char`", context);
2844 handle_ert(os, "\\char", context);
2848 else if (t.cs() == "verb") {
2849 context.check_layout(os);
2850 char const delimiter = p.next_token().character();
2851 string const arg = p.getArg(delimiter, delimiter);
2853 oss << "\\verb" << delimiter << arg << delimiter;
2854 handle_ert(os, oss.str(), context);
2857 // Problem: \= creates a tabstop inside the tabbing environment
2858 // and else an accent. In the latter case we really would want
2859 // \={o} instead of \= o.
2860 else if (t.cs() == "=" && (flags & FLAG_TABBING))
2861 handle_ert(os, t.asInput(), context);
2863 // accents (see Table 6 in Comprehensive LaTeX Symbol List)
2864 else if (t.cs().size() == 1
2865 && contains("\"'.=^`bcdHkrtuv~", t.cs())) {
2866 context.check_layout(os);
2867 // try to see whether the string is in unicodesymbols
2869 string command = t.asInput() + "{"
2870 + trimSpaceAndEol(p.verbatim_item())
2872 docstring s = encodings.fromLaTeXCommand(from_utf8(command), rem);
2875 cerr << "When parsing " << command
2876 << ", result is " << to_utf8(s)
2877 << "+" << to_utf8(rem) << endl;
2880 // we did not find a non-ert version
2881 handle_ert(os, command, context);
2884 else if (t.cs() == "\\") {
2885 context.check_layout(os);
2887 handle_ert(os, "\\\\" + p.getOpt(), context);
2888 else if (p.next_token().asInput() == "*") {
2890 // getOpt() eats the following space if there
2891 // is no optional argument, but that is OK
2892 // here since it has no effect in the output.
2893 handle_ert(os, "\\\\*" + p.getOpt(), context);
2896 begin_inset(os, "Newline newline");
2901 else if (t.cs() == "newline" ||
2902 (t.cs() == "linebreak" && !p.hasOpt())) {
2903 context.check_layout(os);
2904 begin_inset(os, "Newline ");
2907 skip_spaces_braces(p);
2910 else if (t.cs() == "input" || t.cs() == "include"
2911 || t.cs() == "verbatiminput") {
2912 string name = t.cs();
2913 if (t.cs() == "verbatiminput"
2914 && p.next_token().asInput() == "*")
2915 name += p.get_token().asInput();
2916 context.check_layout(os);
2917 string filename(normalize_filename(p.getArg('{', '}')));
2918 string const path = getMasterFilePath();
2919 // We want to preserve relative / absolute filenames,
2920 // therefore path is only used for testing
2921 if ((t.cs() == "include" || t.cs() == "input") &&
2922 !makeAbsPath(filename, path).exists()) {
2923 // The file extension is probably missing.
2924 // Now try to find it out.
2925 string const tex_name =
2926 find_file(filename, path,
2927 known_tex_extensions);
2928 if (!tex_name.empty())
2929 filename = tex_name;
2931 bool external = false;
2933 if (makeAbsPath(filename, path).exists()) {
2934 string const abstexname =
2935 makeAbsPath(filename, path).absFileName();
2936 string const abslyxname =
2937 changeExtension(abstexname, ".lyx");
2938 string const absfigname =
2939 changeExtension(abstexname, ".fig");
2940 fix_relative_filename(filename);
2941 string const lyxname =
2942 changeExtension(filename, ".lyx");
2944 external = FileName(absfigname).exists();
2945 if (t.cs() == "input") {
2946 string const ext = getExtension(abstexname);
2948 // Combined PS/LaTeX:
2949 // x.eps, x.pstex_t (old xfig)
2950 // x.pstex, x.pstex_t (new xfig, e.g. 3.2.5)
2951 FileName const absepsname(
2952 changeExtension(abstexname, ".eps"));
2953 FileName const abspstexname(
2954 changeExtension(abstexname, ".pstex"));
2955 bool const xfigeps =
2956 (absepsname.exists() ||
2957 abspstexname.exists()) &&
2960 // Combined PDF/LaTeX:
2961 // x.pdf, x.pdftex_t (old xfig)
2962 // x.pdf, x.pdf_t (new xfig, e.g. 3.2.5)
2963 FileName const abspdfname(
2964 changeExtension(abstexname, ".pdf"));
2965 bool const xfigpdf =
2966 abspdfname.exists() &&
2967 (ext == "pdftex_t" || ext == "pdf_t");
2971 // Combined PS/PDF/LaTeX:
2972 // x_pspdftex.eps, x_pspdftex.pdf, x.pspdftex
2973 string const absbase2(
2974 removeExtension(abstexname) + "_pspdftex");
2975 FileName const abseps2name(
2976 addExtension(absbase2, ".eps"));
2977 FileName const abspdf2name(
2978 addExtension(absbase2, ".pdf"));
2979 bool const xfigboth =
2980 abspdf2name.exists() &&
2981 abseps2name.exists() && ext == "pspdftex";
2983 xfig = xfigpdf || xfigeps || xfigboth;
2984 external = external && xfig;
2987 outname = changeExtension(filename, ".fig");
2989 // Don't try to convert, the result
2990 // would be full of ERT.
2992 } else if (t.cs() != "verbatiminput" &&
2993 tex2lyx(abstexname, FileName(abslyxname),
3000 cerr << "Warning: Could not find included file '"
3001 << filename << "'." << endl;
3005 begin_inset(os, "External\n");
3006 os << "\ttemplate XFig\n"
3007 << "\tfilename " << outname << '\n';
3009 begin_command_inset(os, "include", name);
3010 os << "preview false\n"
3011 "filename \"" << outname << "\"\n";
3016 else if (t.cs() == "bibliographystyle") {
3017 // store new bibliographystyle
3018 bibliographystyle = p.verbatim_item();
3019 // If any other command than \bibliography and
3020 // \nocite{*} follows, we need to output the style
3021 // (because it might be used by that command).
3022 // Otherwise, it will automatically be output by LyX.
3025 for (Token t2 = p.get_token(); p.good(); t2 = p.get_token()) {
3026 if (t2.cat() == catBegin)
3028 if (t2.cat() != catEscape)
3030 if (t2.cs() == "nocite") {
3031 if (p.getArg('{', '}') == "*")
3033 } else if (t2.cs() == "bibliography")
3040 "\\bibliographystyle{" + bibliographystyle + '}',
3045 else if (t.cs() == "bibliography") {
3046 context.check_layout(os);
3047 begin_command_inset(os, "bibtex", "bibtex");
3048 if (!btprint.empty()) {
3049 os << "btprint " << '"' << "btPrintAll" << '"' << "\n";
3050 // clear the string because the next BibTeX inset can be without the
3051 // \nocite{*} option
3054 os << "bibfiles " << '"' << p.verbatim_item() << '"' << "\n";
3055 // Do we have a bibliographystyle set?
3056 if (!bibliographystyle.empty())
3057 os << "options " << '"' << bibliographystyle << '"' << "\n";
3061 else if (t.cs() == "parbox")
3062 parse_box(p, os, 0, FLAG_ITEM, outer, context, "", "", t.cs());
3064 else if (t.cs() == "ovalbox" || t.cs() == "Ovalbox" ||
3065 t.cs() == "shadowbox" || t.cs() == "doublebox")
3066 parse_outer_box(p, os, FLAG_ITEM, outer, context, t.cs(), "");
3068 else if (t.cs() == "framebox") {
3069 string special = p.getFullOpt();
3070 special += p.getOpt();
3071 parse_outer_box(p, os, FLAG_ITEM, outer, context, t.cs(), special);
3074 //\makebox() is part of the picture environment and different from \makebox{}
3075 //\makebox{} will be parsed by parse_box when bug 2956 is fixed
3076 else if (t.cs() == "makebox") {
3077 string arg = t.asInput();
3078 if (p.next_token().character() == '(')
3079 //the syntax is: \makebox(x,y)[position]{content}
3080 arg += p.getFullParentheseArg();
3082 //the syntax is: \makebox[width][position]{content}
3083 arg += p.getFullOpt();
3084 handle_ert(os, arg + p.getFullOpt(), context);
3087 else if (t.cs() == "smallskip" ||
3088 t.cs() == "medskip" ||
3089 t.cs() == "bigskip" ||
3090 t.cs() == "vfill") {
3091 context.check_layout(os);
3092 begin_inset(os, "VSpace ");
3095 skip_spaces_braces(p);
3098 else if (is_known(t.cs(), known_spaces)) {
3099 char const * const * where = is_known(t.cs(), known_spaces);
3100 context.check_layout(os);
3101 begin_inset(os, "space ");
3102 os << '\\' << known_coded_spaces[where - known_spaces]
3105 // LaTeX swallows whitespace after all spaces except
3106 // "\\,". We have to do that here, too, because LyX
3107 // adds "{}" which would make the spaces significant.
3109 eat_whitespace(p, os, context, false);
3110 // LyX adds "{}" after all spaces except "\\ " and
3111 // "\\,", so we have to remove "{}".
3112 // "\\,{}" is equivalent to "\\," in LaTeX, so we
3113 // remove the braces after "\\,", too.
3118 else if (t.cs() == "newpage" ||
3119 (t.cs() == "pagebreak" && !p.hasOpt()) ||
3120 t.cs() == "clearpage" ||
3121 t.cs() == "cleardoublepage") {
3122 context.check_layout(os);
3123 begin_inset(os, "Newpage ");
3126 skip_spaces_braces(p);
3129 else if (t.cs() == "DeclareRobustCommand" ||
3130 t.cs() == "DeclareRobustCommandx" ||
3131 t.cs() == "newcommand" ||
3132 t.cs() == "newcommandx" ||
3133 t.cs() == "providecommand" ||
3134 t.cs() == "providecommandx" ||
3135 t.cs() == "renewcommand" ||
3136 t.cs() == "renewcommandx") {
3137 // DeclareRobustCommand, DeclareRobustCommandx,
3138 // providecommand and providecommandx could be handled
3139 // by parse_command(), but we need to call
3140 // add_known_command() here.
3141 string name = t.asInput();
3142 if (p.next_token().asInput() == "*") {
3143 // Starred form. Eat '*'
3147 string const command = p.verbatim_item();
3148 string const opt1 = p.getFullOpt();
3149 string const opt2 = p.getFullOpt();
3150 add_known_command(command, opt1, !opt2.empty());
3151 string const ert = name + '{' + command + '}' +
3153 '{' + p.verbatim_item() + '}';
3155 if (t.cs() == "DeclareRobustCommand" ||
3156 t.cs() == "DeclareRobustCommandx" ||
3157 t.cs() == "providecommand" ||
3158 t.cs() == "providecommandx" ||
3159 name[name.length()-1] == '*')
3160 handle_ert(os, ert, context);
3162 context.check_layout(os);
3163 begin_inset(os, "FormulaMacro");
3169 else if (t.cs() == "let" && p.next_token().asInput() != "*") {
3170 // let could be handled by parse_command(),
3171 // but we need to call add_known_command() here.
3172 string ert = t.asInput();
3175 if (p.next_token().cat() == catBegin) {
3176 name = p.verbatim_item();
3177 ert += '{' + name + '}';
3179 name = p.verbatim_item();
3184 if (p.next_token().cat() == catBegin) {
3185 command = p.verbatim_item();
3186 ert += '{' + command + '}';
3188 command = p.verbatim_item();
3191 // If command is known, make name known too, to parse
3192 // its arguments correctly. For this reason we also
3193 // have commands in syntax.default that are hardcoded.
3194 CommandMap::iterator it = known_commands.find(command);
3195 if (it != known_commands.end())
3196 known_commands[t.asInput()] = it->second;
3197 handle_ert(os, ert, context);
3200 else if (t.cs() == "hspace" || t.cs() == "vspace") {
3201 bool starred = false;
3202 if (p.next_token().asInput() == "*") {
3206 string name = t.asInput();
3207 string const length = p.verbatim_item();
3210 bool valid = splitLatexLength(length, valstring, unit);
3211 bool known_hspace = false;
3212 bool known_vspace = false;
3213 bool known_unit = false;
3216 istringstream iss(valstring);
3219 if (t.cs()[0] == 'h') {
3220 if (unit == "\\fill") {
3225 known_hspace = true;
3228 if (unit == "\\smallskipamount") {
3230 known_vspace = true;
3231 } else if (unit == "\\medskipamount") {
3233 known_vspace = true;
3234 } else if (unit == "\\bigskipamount") {
3236 known_vspace = true;
3237 } else if (unit == "\\fill") {
3239 known_vspace = true;
3243 if (!known_hspace && !known_vspace) {
3244 switch (unitFromString(unit)) {
3265 if (t.cs()[0] == 'h' && (known_unit || known_hspace)) {
3266 // Literal horizontal length or known variable
3267 context.check_layout(os);
3268 begin_inset(os, "space ");
3276 if (known_unit && !known_hspace)
3278 << translate_len(length);
3280 } else if (known_unit || known_vspace) {
3281 // Literal vertical length or known variable
3282 context.check_layout(os);
3283 begin_inset(os, "VSpace ");
3291 // LyX can't handle other length variables in Inset VSpace/space
3296 handle_ert(os, name + '{' + unit + '}', context);
3297 else if (value == -1.0)
3298 handle_ert(os, name + "{-" + unit + '}', context);
3300 handle_ert(os, name + '{' + valstring + unit + '}', context);
3302 handle_ert(os, name + '{' + length + '}', context);
3306 // The single '=' is meant here.
3307 else if ((newinsetlayout = findInsetLayout(context.textclass, t.cs(), true))) {
3309 context.check_layout(os);
3310 begin_inset(os, "Flex ");
3311 os << to_utf8(newinsetlayout->name()) << '\n'
3312 << "status collapsed\n";
3313 parse_text_in_inset(p, os, FLAG_ITEM, false, context, newinsetlayout);
3318 // try to see whether the string is in unicodesymbols
3319 // Only use text mode commands, since we are in text mode here,
3320 // and math commands may be invalid (bug 6797)
3322 docstring s = encodings.fromLaTeXCommand(from_utf8(t.asInput()),
3323 rem, Encodings::TEXT_CMD);
3326 cerr << "When parsing " << t.cs()
3327 << ", result is " << to_utf8(s)
3328 << "+" << to_utf8(rem) << endl;
3329 context.check_layout(os);
3331 skip_spaces_braces(p);
3333 //cerr << "#: " << t << " mode: " << mode << endl;
3334 // heuristic: read up to next non-nested space
3336 string s = t.asInput();
3337 string z = p.verbatim_item();
3338 while (p.good() && z != " " && z.size()) {
3339 //cerr << "read: " << z << endl;
3341 z = p.verbatim_item();
3343 cerr << "found ERT: " << s << endl;
3344 handle_ert(os, s + ' ', context);
3347 string name = t.asInput();
3348 if (p.next_token().asInput() == "*") {
3349 // Starred commands like \vspace*{}
3350 p.get_token(); // Eat '*'
3353 if (!parse_command(name, p, os, outer, context))
3354 handle_ert(os, name, context);
3358 if (flags & FLAG_LEAVE) {
3359 flags &= ~FLAG_LEAVE;