2 * \file tex2lyx/text.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Jean-Marc Lasgouttes
10 * Full author contact details are available in file CREDITS.
21 #include "FloatList.h"
25 #include "support/lassert.h"
26 #include "support/convert.h"
27 #include "support/FileName.h"
28 #include "support/filetools.h"
29 #include "support/lstrings.h"
38 using namespace lyx::support;
43 void parse_text_in_inset(Parser & p, ostream & os, unsigned flags, bool outer,
44 Context const & context)
46 Context newcontext(true, context.textclass);
47 newcontext.font = context.font;
48 parse_text(p, os, flags, outer, newcontext);
49 newcontext.check_end_layout(os);
55 /// parses a paragraph snippet, useful for example for \\emph{...}
56 void parse_text_snippet(Parser & p, ostream & os, unsigned flags, bool outer,
59 Context newcontext(context);
60 // Don't inherit the paragraph-level extra stuff
61 newcontext.par_extra_stuff.clear();
62 parse_text(p, os, flags, outer, newcontext);
63 // Make sure that we don't create invalid .lyx files
64 context.need_layout = newcontext.need_layout;
65 context.need_end_layout = newcontext.need_end_layout;
70 * Thin wrapper around parse_text_snippet() using a string.
72 * We completely ignore \c context.need_layout and \c context.need_end_layout,
73 * because our return value is not used directly (otherwise the stream version
74 * of parse_text_snippet() could be used). That means that the caller needs
75 * to do layout management manually.
76 * This is intended to parse text that does not create any layout changes.
78 string parse_text_snippet(Parser & p, unsigned flags, const bool outer,
81 Context newcontext(context);
82 newcontext.need_layout = false;
83 newcontext.need_end_layout = false;
84 newcontext.new_layout_allowed = false;
85 // Avoid warning by Context::~Context()
86 newcontext.par_extra_stuff.clear();
88 parse_text_snippet(p, os, flags, outer, newcontext);
93 char const * const known_ref_commands[] = { "ref", "pageref", "vref",
94 "vpageref", "prettyref", "eqref", 0 };
98 * The starred forms are also known.
100 char const * const known_natbib_commands[] = { "cite", "citet", "citep",
101 "citealt", "citealp", "citeauthor", "citeyear", "citeyearpar",
102 "citefullauthor", "Citet", "Citep", "Citealt", "Citealp", "Citeauthor", 0 };
106 * No starred form other than "cite*" known.
108 char const * const known_jurabib_commands[] = { "cite", "citet", "citep",
109 "citealt", "citealp", "citeauthor", "citeyear", "citeyearpar",
110 // jurabib commands not (yet) supported by LyX:
112 // "footcite", "footcitet", "footcitep", "footcitealt", "footcitealp",
113 // "footciteauthor", "footciteyear", "footciteyearpar",
114 "citefield", "citetitle", "cite*", 0 };
116 /// LaTeX names for quotes
117 char const * const known_quotes[] = { "dq", "guillemotleft", "flqq", "og",
118 "guillemotright", "frqq", "fg", "glq", "glqq", "textquoteleft", "grq", "grqq",
119 "quotedblbase", "textquotedblleft", "quotesinglbase", "textquoteright", "flq",
120 "guilsinglleft", "frq", "guilsinglright", 0};
122 /// the same as known_quotes with .lyx names
123 char const * const known_coded_quotes[] = { "prd", "ard", "ard", "ard",
124 "ald", "ald", "ald", "gls", "gld", "els", "els", "grd",
125 "gld", "grd", "gls", "ers", "fls",
126 "fls", "frs", "frs", 0};
128 /// LaTeX names for font sizes
129 char const * const known_sizes[] = { "tiny", "scriptsize", "footnotesize",
130 "small", "normalsize", "large", "Large", "LARGE", "huge", "Huge", 0};
132 /// the same as known_sizes with .lyx names plus a default entry
133 char const * const known_coded_sizes[] = { "default", "tiny", "scriptsize", "footnotesize",
134 "small", "normal", "large", "larger", "largest", "huge", "giant", 0};
136 /// LaTeX 2.09 names for font families
137 char const * const known_old_font_families[] = { "rm", "sf", "tt", 0};
139 /// LaTeX names for font families
140 char const * const known_font_families[] = { "rmfamily", "sffamily",
143 /// the same as known_old_font_families and known_font_families with .lyx names
144 char const * const known_coded_font_families[] = { "roman", "sans",
147 /// LaTeX 2.09 names for font series
148 char const * const known_old_font_series[] = { "bf", 0};
150 /// LaTeX names for font series
151 char const * const known_font_series[] = { "bfseries", "mdseries", 0};
153 /// the same as known_old_font_series and known_font_series with .lyx names
154 char const * const known_coded_font_series[] = { "bold", "medium", 0};
156 /// LaTeX 2.09 names for font shapes
157 char const * const known_old_font_shapes[] = { "it", "sl", "sc", 0};
159 /// LaTeX names for font shapes
160 char const * const known_font_shapes[] = { "itshape", "slshape", "scshape",
163 /// the same as known_old_font_shapes and known_font_shapes with .lyx names
164 char const * const known_coded_font_shapes[] = { "italic", "slanted",
165 "smallcaps", "up", 0};
168 * Graphics file extensions known by the dvips driver of the graphics package.
169 * These extensions are used to complete the filename of an included
170 * graphics file if it does not contain an extension.
171 * The order must be the same that latex uses to find a file, because we
172 * will use the first extension that matches.
173 * This is only an approximation for the common cases. If we would want to
174 * do it right in all cases, we would need to know which graphics driver is
175 * used and know the extensions of every driver of the graphics package.
177 char const * const known_dvips_graphics_formats[] = {"eps", "ps", "eps.gz",
178 "ps.gz", "eps.Z", "ps.Z", 0};
181 * Graphics file extensions known by the pdftex driver of the graphics package.
182 * \sa known_dvips_graphics_formats
184 char const * const known_pdftex_graphics_formats[] = {"png", "pdf", "jpg",
188 * Known file extensions for TeX files as used by \\include.
190 char const * const known_tex_extensions[] = {"tex", 0};
192 /// spaces known by InsetSpace
193 char const * const known_spaces[] = { " ", "space", ",", "thinspace", "quad",
194 "qquad", "enspace", "enskip", "negthinspace", 0};
196 /// the same as known_spaces with .lyx names
197 char const * const known_coded_spaces[] = { "space{}", "space{}",
198 "thinspace{}", "thinspace{}", "quad{}", "qquad{}", "enspace{}", "enskip{}",
199 "negthinspace{}", 0};
202 /// splits "x=z, y=b" into a map
203 map<string, string> split_map(string const & s)
205 map<string, string> res;
208 for (size_t i = 0; i < v.size(); ++i) {
209 size_t const pos = v[i].find('=');
210 string const index = v[i].substr(0, pos);
211 string const value = v[i].substr(pos + 1, string::npos);
212 res[trim(index)] = trim(value);
219 * Split a LaTeX length into value and unit.
220 * The latter can be a real unit like "pt", or a latex length variable
221 * like "\textwidth". The unit may contain additional stuff like glue
222 * lengths, but we don't care, because such lengths are ERT anyway.
223 * \returns true if \p value and \p unit are valid.
225 bool splitLatexLength(string const & len, string & value, string & unit)
229 const string::size_type i = len.find_first_not_of(" -+0123456789.,");
230 //'4,5' is a valid LaTeX length number. Change it to '4.5'
231 string const length = subst(len, ',', '.');
232 if (i == string::npos)
235 if (len[0] == '\\') {
236 // We had something like \textwidth without a factor
242 value = trim(string(length, 0, i));
246 // 'cM' is a valid LaTeX length unit. Change it to 'cm'
247 if (contains(len, '\\'))
248 unit = trim(string(len, i));
250 unit = ascii_lowercase(trim(string(len, i)));
255 /// A simple function to translate a latex length to something lyx can
256 /// understand. Not perfect, but rather best-effort.
257 bool translate_len(string const & length, string & valstring, string & unit)
259 if (!splitLatexLength(length, valstring, unit))
261 // LyX uses percent values
263 istringstream iss(valstring);
268 string const percentval = oss.str();
270 if (unit.empty() || unit[0] != '\\')
272 string::size_type const i = unit.find(' ');
273 string const endlen = (i == string::npos) ? string() : string(unit, i);
274 if (unit == "\\textwidth") {
275 valstring = percentval;
276 unit = "text%" + endlen;
277 } else if (unit == "\\columnwidth") {
278 valstring = percentval;
279 unit = "col%" + endlen;
280 } else if (unit == "\\paperwidth") {
281 valstring = percentval;
282 unit = "page%" + endlen;
283 } else if (unit == "\\linewidth") {
284 valstring = percentval;
285 unit = "line%" + endlen;
286 } else if (unit == "\\paperheight") {
287 valstring = percentval;
288 unit = "pheight%" + endlen;
289 } else if (unit == "\\textheight") {
290 valstring = percentval;
291 unit = "theight%" + endlen;
299 string translate_len(string const & length)
303 if (translate_len(length, value, unit))
305 // If the input is invalid, return what we have.
313 * Translates a LaTeX length into \p value, \p unit and
314 * \p special parts suitable for a box inset.
315 * The difference from translate_len() is that a box inset knows about
316 * some special "units" that are stored in \p special.
318 void translate_box_len(string const & length, string & value, string & unit, string & special)
320 if (translate_len(length, value, unit)) {
321 if (unit == "\\height" || unit == "\\depth" ||
322 unit == "\\totalheight" || unit == "\\width") {
323 special = unit.substr(1);
324 // The unit is not used, but LyX requires a dummy setting
337 * Find a file with basename \p name in path \p path and an extension
340 string find_file(string const & name, string const & path,
341 char const * const * extensions)
343 for (char const * const * what = extensions; *what; ++what) {
344 string const trial = addExtension(name, *what);
345 if (makeAbsPath(trial, path).exists())
352 void begin_inset(ostream & os, string const & name)
354 os << "\n\\begin_inset " << name;
357 void begin_Cinset(ostream & os, string const & name)
359 os << "\n\\begin_inset CommandInset " << name;
363 void end_inset(ostream & os)
365 os << "\n\\end_inset\n\n";
368 void LatexCommand(ostream & os, string const & name)
370 os << "\nLatexCommand " << name << "\n";
374 void skip_braces(Parser & p)
376 if (p.next_token().cat() != catBegin)
379 if (p.next_token().cat() == catEnd) {
387 void handle_ert(ostream & os, string const & s, Context & context)
389 // We must have a valid layout before outputting the ERT inset.
390 context.check_layout(os);
391 Context newcontext(true, context.textclass);
392 begin_inset(os, "ERT");
393 os << "\nstatus collapsed\n";
394 newcontext.check_layout(os);
395 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
397 os << "\n\\backslash\n";
398 else if (*it == '\n') {
399 newcontext.new_paragraph(os);
400 newcontext.check_layout(os);
404 newcontext.check_end_layout(os);
409 void handle_comment(ostream & os, string const & s, Context & context)
411 // TODO: Handle this better
412 Context newcontext(true, context.textclass);
413 begin_inset(os, "ERT");
414 os << "\nstatus collapsed\n";
415 newcontext.check_layout(os);
416 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
418 os << "\n\\backslash\n";
422 // make sure that our comment is the last thing on the line
423 newcontext.new_paragraph(os);
424 newcontext.check_layout(os);
425 newcontext.check_end_layout(os);
430 Layout const * findLayout(TextClass const & textclass, string const & name)
432 DocumentClass::const_iterator lit = textclass.begin();
433 DocumentClass::const_iterator len = textclass.end();
434 for (; lit != len; ++lit)
435 if (lit->latexname() == name)
441 void eat_whitespace(Parser &, ostream &, Context &, bool);
444 Layout * captionlayout()
446 static Layout * lay = 0;
449 lay->setName(from_ascii("Caption"));
450 lay->setLatexName("caption");
451 lay->latextype = LATEX_COMMAND;
452 lay->optionalargs = 1;
458 void output_command_layout(ostream & os, Parser & p, bool outer,
459 Context & parent_context,
460 Layout const * newlayout)
462 parent_context.check_end_layout(os);
463 Context context(true, parent_context.textclass, newlayout,
464 parent_context.layout, parent_context.font);
465 if (parent_context.deeper_paragraph) {
466 // We are beginning a nested environment after a
467 // deeper paragraph inside the outer list environment.
468 // Therefore we don't need to output a "begin deeper".
469 context.need_end_deeper = true;
471 context.check_deeper(os);
472 context.check_layout(os);
473 if (context.layout->optionalargs > 0) {
474 eat_whitespace(p, os, context, false);
475 if (p.next_token().character() == '[') {
476 p.get_token(); // eat '['
477 begin_inset(os, "OptArg\n");
478 os << "status collapsed\n\n";
479 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
481 eat_whitespace(p, os, context, false);
484 parse_text(p, os, FLAG_ITEM, outer, context);
485 context.check_end_layout(os);
486 if (parent_context.deeper_paragraph) {
487 // We must suppress the "end deeper" because we
488 // suppressed the "begin deeper" above.
489 context.need_end_deeper = false;
491 context.check_end_deeper(os);
492 // We don't need really a new paragraph, but
493 // we must make sure that the next item gets a \begin_layout.
494 parent_context.new_paragraph(os);
499 * Output a space if necessary.
500 * This function gets called for every whitespace token.
502 * We have three cases here:
503 * 1. A space must be suppressed. Example: The lyxcode case below
504 * 2. A space may be suppressed. Example: Spaces before "\par"
505 * 3. A space must not be suppressed. Example: A space between two words
507 * We currently handle only 1. and 3 and from 2. only the case of
508 * spaces before newlines as a side effect.
510 * 2. could be used to suppress as many spaces as possible. This has two effects:
511 * - Reimporting LyX generated LaTeX files changes almost no whitespace
512 * - Superflous whitespace from non LyX generated LaTeX files is removed.
513 * The drawback is that the logic inside the function becomes
514 * complicated, and that is the reason why it is not implemented.
516 void check_space(Parser & p, ostream & os, Context & context)
518 Token const next = p.next_token();
519 Token const curr = p.curr_token();
520 // A space before a single newline and vice versa must be ignored
521 // LyX emits a newline before \end{lyxcode}.
522 // This newline must be ignored,
523 // otherwise LyX will add an additional protected space.
524 if (next.cat() == catSpace ||
525 next.cat() == catNewline ||
526 (next.cs() == "end" && context.layout->free_spacing && curr.cat() == catNewline)) {
529 context.check_layout(os);
535 * Parse all arguments of \p command
537 void parse_arguments(string const & command,
538 vector<ArgumentType> const & template_arguments,
539 Parser & p, ostream & os, bool outer, Context & context)
541 string ert = command;
542 size_t no_arguments = template_arguments.size();
543 for (size_t i = 0; i < no_arguments; ++i) {
544 switch (template_arguments[i]) {
546 // This argument contains regular LaTeX
547 handle_ert(os, ert + '{', context);
548 eat_whitespace(p, os, context, false);
549 parse_text(p, os, FLAG_ITEM, outer, context);
553 // This argument may contain special characters
554 ert += '{' + p.verbatim_item() + '}';
561 handle_ert(os, ert, context);
566 * Check whether \p command is a known command. If yes,
567 * handle the command with all arguments.
568 * \return true if the command was parsed, false otherwise.
570 bool parse_command(string const & command, Parser & p, ostream & os,
571 bool outer, Context & context)
573 if (known_commands.find(command) != known_commands.end()) {
574 parse_arguments(command, known_commands[command], p, os,
582 /// Parses a minipage or parbox
583 void parse_box(Parser & p, ostream & os, unsigned flags, bool outer,
584 Context & parent_context, bool use_parbox)
588 // We need to set the height to the LaTeX default of 1\\totalheight
589 // for the case when no height argument is given
590 string height_value = "1";
591 string height_unit = "in";
592 string height_special = "totalheight";
594 if (p.next_token().asInput() == "[") {
595 position = p.getArg('[', ']');
596 if (position != "t" && position != "c" && position != "b") {
598 cerr << "invalid position for minipage/parbox" << endl;
600 if (p.next_token().asInput() == "[") {
601 latex_height = p.getArg('[', ']');
602 translate_box_len(latex_height, height_value, height_unit, height_special);
604 if (p.next_token().asInput() == "[") {
605 inner_pos = p.getArg('[', ']');
606 if (inner_pos != "c" && inner_pos != "t" &&
607 inner_pos != "b" && inner_pos != "s") {
608 inner_pos = position;
609 cerr << "invalid inner_pos for minipage/parbox"
617 string const latex_width = p.verbatim_item();
618 translate_len(latex_width, width_value, width_unit);
619 if (contains(width_unit, '\\') || contains(height_unit, '\\')) {
620 // LyX can't handle length variables
625 ss << "\\begin{minipage}";
626 if (!position.empty())
627 ss << '[' << position << ']';
628 if (!latex_height.empty())
629 ss << '[' << latex_height << ']';
630 if (!inner_pos.empty())
631 ss << '[' << inner_pos << ']';
632 ss << "{" << latex_width << "}";
635 handle_ert(os, ss.str(), parent_context);
636 parent_context.new_paragraph(os);
637 parse_text_in_inset(p, os, flags, outer, parent_context);
639 handle_ert(os, "}", parent_context);
641 handle_ert(os, "\\end{minipage}", parent_context);
643 // LyX does not like empty positions, so we have
644 // to set them to the LaTeX default values here.
645 if (position.empty())
647 if (inner_pos.empty())
648 inner_pos = position;
649 parent_context.check_layout(os);
650 begin_inset(os, "Box Frameless\n");
651 os << "position \"" << position << "\"\n";
652 os << "hor_pos \"c\"\n";
653 os << "has_inner_box 1\n";
654 os << "inner_pos \"" << inner_pos << "\"\n";
655 os << "use_parbox " << use_parbox << "\n";
656 os << "width \"" << width_value << width_unit << "\"\n";
657 os << "special \"none\"\n";
658 os << "height \"" << height_value << height_unit << "\"\n";
659 os << "height_special \"" << height_special << "\"\n";
660 os << "status open\n\n";
661 parse_text_in_inset(p, os, flags, outer, parent_context);
663 #ifdef PRESERVE_LAYOUT
664 // lyx puts a % after the end of the minipage
665 if (p.next_token().cat() == catNewline && p.next_token().cs().size() > 1) {
667 //handle_comment(os, "%dummy", parent_context);
670 parent_context.new_paragraph(os);
672 else if (p.next_token().cat() == catSpace || p.next_token().cat() == catNewline) {
673 //handle_comment(os, "%dummy", parent_context);
676 // We add a protected space if something real follows
677 if (p.good() && p.next_token().cat() != catComment) {
678 os << "\\InsetSpace ~\n";
686 /// parse an unknown environment
687 void parse_unknown_environment(Parser & p, string const & name, ostream & os,
688 unsigned flags, bool outer,
689 Context & parent_context)
691 if (name == "tabbing")
692 // We need to remember that we have to handle '\=' specially
693 flags |= FLAG_TABBING;
695 // We need to translate font changes and paragraphs inside the
696 // environment to ERT if we have a non standard font.
697 // Otherwise things like
698 // \large\begin{foo}\huge bar\end{foo}
700 bool const specialfont =
701 (parent_context.font != parent_context.normalfont);
702 bool const new_layout_allowed = parent_context.new_layout_allowed;
704 parent_context.new_layout_allowed = false;
705 handle_ert(os, "\\begin{" + name + "}", parent_context);
706 parse_text_snippet(p, os, flags, outer, parent_context);
707 handle_ert(os, "\\end{" + name + "}", parent_context);
709 parent_context.new_layout_allowed = new_layout_allowed;
713 void parse_environment(Parser & p, ostream & os, bool outer,
714 Context & parent_context)
716 Layout const * newlayout;
717 string const name = p.getArg('{', '}');
718 const bool is_starred = suffixIs(name, '*');
719 string const unstarred_name = rtrim(name, "*");
720 active_environments.push_back(name);
722 if (is_math_env(name)) {
723 parent_context.check_layout(os);
724 begin_inset(os, "Formula ");
725 os << "\\begin{" << name << "}";
726 parse_math(p, os, FLAG_END, MATH_MODE);
727 os << "\\end{" << name << "}";
731 else if (name == "tabular" || name == "longtable") {
732 eat_whitespace(p, os, parent_context, false);
733 parent_context.check_layout(os);
734 begin_inset(os, "Tabular ");
735 handle_tabular(p, os, name == "longtable", parent_context);
740 else if (parent_context.textclass.floats().typeExist(unstarred_name)) {
741 eat_whitespace(p, os, parent_context, false);
742 parent_context.check_layout(os);
743 begin_inset(os, "Float " + unstarred_name + "\n");
744 if (p.next_token().asInput() == "[") {
745 os << "placement " << p.getArg('[', ']') << '\n';
747 os << "wide " << convert<string>(is_starred)
748 << "\nsideways false"
749 << "\nstatus open\n\n";
750 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
752 // We don't need really a new paragraph, but
753 // we must make sure that the next item gets a \begin_layout.
754 parent_context.new_paragraph(os);
758 else if (name == "minipage") {
759 eat_whitespace(p, os, parent_context, false);
760 parse_box(p, os, FLAG_END, outer, parent_context, false);
764 else if (name == "comment") {
765 eat_whitespace(p, os, parent_context, false);
766 parent_context.check_layout(os);
767 begin_inset(os, "Note Comment\n");
768 os << "status open\n";
769 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
772 skip_braces(p); // eat {} that might by set by LyX behind comments
775 else if (name == "lyxgreyedout") {
776 eat_whitespace(p, os, parent_context, false);
777 parent_context.check_layout(os);
778 begin_inset(os, "Note Greyedout\n");
779 os << "status open\n";
780 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
785 else if (name == "framed") {
786 eat_whitespace(p, os, parent_context, false);
787 parent_context.check_layout(os);
788 begin_inset(os, "Note Framed\n");
789 os << "status open\n";
790 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
795 else if (name == "shaded") {
796 eat_whitespace(p, os, parent_context, false);
797 parent_context.check_layout(os);
798 begin_inset(os, "Note Shaded\n");
799 os << "status open\n";
800 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
805 else if (!parent_context.new_layout_allowed)
806 parse_unknown_environment(p, name, os, FLAG_END, outer,
809 // Alignment and spacing settings
810 // FIXME (bug xxxx): These settings can span multiple paragraphs and
811 // therefore are totally broken!
812 // Note that \centering, raggedright, and raggedleft cannot be handled, as
813 // they are commands not environments. They are furthermore switches that
814 // can be ended by another switches, but also by commands like \footnote or
815 // \parbox. So the only safe way is to leave them untouched.
816 else if (name == "center" || name == "centering" ||
817 name == "flushleft" || name == "flushright" ||
818 name == "singlespace" || name == "onehalfspace" ||
819 name == "doublespace" || name == "spacing") {
820 eat_whitespace(p, os, parent_context, false);
821 // We must begin a new paragraph if not already done
822 if (! parent_context.atParagraphStart()) {
823 parent_context.check_end_layout(os);
824 parent_context.new_paragraph(os);
826 if (name == "flushleft")
827 parent_context.add_extra_stuff("\\align left\n");
828 else if (name == "flushright")
829 parent_context.add_extra_stuff("\\align right\n");
830 else if (name == "center" || name == "centering")
831 parent_context.add_extra_stuff("\\align center\n");
832 else if (name == "singlespace")
833 parent_context.add_extra_stuff("\\paragraph_spacing single\n");
834 else if (name == "onehalfspace")
835 parent_context.add_extra_stuff("\\paragraph_spacing onehalf\n");
836 else if (name == "doublespace")
837 parent_context.add_extra_stuff("\\paragraph_spacing double\n");
838 else if (name == "spacing")
839 parent_context.add_extra_stuff("\\paragraph_spacing other " + p.verbatim_item() + "\n");
840 parse_text(p, os, FLAG_END, outer, parent_context);
841 // Just in case the environment is empty
842 parent_context.extra_stuff.erase();
843 // We must begin a new paragraph to reset the alignment
844 parent_context.new_paragraph(os);
848 // The single '=' is meant here.
849 else if ((newlayout = findLayout(parent_context.textclass, name)) &&
850 newlayout->isEnvironment()) {
851 eat_whitespace(p, os, parent_context, false);
852 Context context(true, parent_context.textclass, newlayout,
853 parent_context.layout, parent_context.font);
854 if (parent_context.deeper_paragraph) {
855 // We are beginning a nested environment after a
856 // deeper paragraph inside the outer list environment.
857 // Therefore we don't need to output a "begin deeper".
858 context.need_end_deeper = true;
860 parent_context.check_end_layout(os);
861 switch (context.layout->latextype) {
862 case LATEX_LIST_ENVIRONMENT:
863 context.add_par_extra_stuff("\\labelwidthstring "
864 + p.verbatim_item() + '\n');
867 case LATEX_BIB_ENVIRONMENT:
868 p.verbatim_item(); // swallow next arg
874 context.check_deeper(os);
875 parse_text(p, os, FLAG_END, outer, context);
876 context.check_end_layout(os);
877 if (parent_context.deeper_paragraph) {
878 // We must suppress the "end deeper" because we
879 // suppressed the "begin deeper" above.
880 context.need_end_deeper = false;
882 context.check_end_deeper(os);
883 parent_context.new_paragraph(os);
887 else if (name == "appendix") {
888 // This is no good latex style, but it works and is used in some documents...
889 eat_whitespace(p, os, parent_context, false);
890 parent_context.check_end_layout(os);
891 Context context(true, parent_context.textclass, parent_context.layout,
892 parent_context.layout, parent_context.font);
893 context.check_layout(os);
894 os << "\\start_of_appendix\n";
895 parse_text(p, os, FLAG_END, outer, context);
896 context.check_end_layout(os);
900 else if (known_environments.find(name) != known_environments.end()) {
901 vector<ArgumentType> arguments = known_environments[name];
902 // The last "argument" denotes wether we may translate the
903 // environment contents to LyX
904 // The default required if no argument is given makes us
905 // compatible with the reLyXre environment.
906 ArgumentType contents = arguments.empty() ?
909 if (!arguments.empty())
910 arguments.pop_back();
911 // See comment in parse_unknown_environment()
912 bool const specialfont =
913 (parent_context.font != parent_context.normalfont);
914 bool const new_layout_allowed =
915 parent_context.new_layout_allowed;
917 parent_context.new_layout_allowed = false;
918 parse_arguments("\\begin{" + name + "}", arguments, p, os,
919 outer, parent_context);
920 if (contents == verbatim)
921 handle_ert(os, p.verbatimEnvironment(name),
924 parse_text_snippet(p, os, FLAG_END, outer,
926 handle_ert(os, "\\end{" + name + "}", parent_context);
928 parent_context.new_layout_allowed = new_layout_allowed;
932 parse_unknown_environment(p, name, os, FLAG_END, outer,
935 active_environments.pop_back();
939 /// parses a comment and outputs it to \p os.
940 void parse_comment(Parser & p, ostream & os, Token const & t, Context & context)
942 LASSERT(t.cat() == catComment, return);
943 if (!t.cs().empty()) {
944 context.check_layout(os);
945 handle_comment(os, '%' + t.cs(), context);
946 if (p.next_token().cat() == catNewline) {
947 // A newline after a comment line starts a new
949 if (context.new_layout_allowed) {
950 if(!context.atParagraphStart())
951 // Only start a new paragraph if not already
952 // done (we might get called recursively)
953 context.new_paragraph(os);
955 handle_ert(os, "\n", context);
956 eat_whitespace(p, os, context, true);
966 * Reads spaces and comments until the first non-space, non-comment token.
967 * New paragraphs (double newlines or \\par) are handled like simple spaces
968 * if \p eatParagraph is true.
969 * Spaces are skipped, but comments are written to \p os.
971 void eat_whitespace(Parser & p, ostream & os, Context & context,
975 Token const & t = p.get_token();
976 if (t.cat() == catComment)
977 parse_comment(p, os, t, context);
978 else if ((! eatParagraph && p.isParagraph()) ||
979 (t.cat() != catSpace && t.cat() != catNewline)) {
988 * Set a font attribute, parse text and reset the font attribute.
989 * \param attribute Attribute name (e.g. \\family, \\shape etc.)
990 * \param currentvalue Current value of the attribute. Is set to the new
991 * value during parsing.
992 * \param newvalue New value of the attribute
994 void parse_text_attributes(Parser & p, ostream & os, unsigned flags, bool outer,
995 Context & context, string const & attribute,
996 string & currentvalue, string const & newvalue)
998 context.check_layout(os);
999 string const oldvalue = currentvalue;
1000 currentvalue = newvalue;
1001 os << '\n' << attribute << ' ' << newvalue << "\n";
1002 parse_text_snippet(p, os, flags, outer, context);
1003 context.check_layout(os);
1004 os << '\n' << attribute << ' ' << oldvalue << "\n";
1005 currentvalue = oldvalue;
1009 /// get the arguments of a natbib or jurabib citation command
1010 void get_cite_arguments(Parser & p, bool natbibOrder,
1011 string & before, string & after)
1013 // We need to distinguish "" and "[]", so we can't use p.getOpt().
1015 // text before the citation
1017 // text after the citation
1018 after = p.getFullOpt();
1020 if (!after.empty()) {
1021 before = p.getFullOpt();
1022 if (natbibOrder && !before.empty())
1023 swap(before, after);
1028 /// Convert filenames with TeX macros and/or quotes to something LyX
1030 string const normalize_filename(string const & name)
1032 Parser p(trim(name, "\""));
1035 Token const & t = p.get_token();
1036 if (t.cat() != catEscape)
1038 else if (t.cs() == "lyxdot") {
1039 // This is used by LyX for simple dots in relative
1043 } else if (t.cs() == "space") {
1053 /// Convert \p name from TeX convention (relative to master file) to LyX
1054 /// convention (relative to .lyx file) if it is relative
1055 void fix_relative_filename(string & name)
1057 if (FileName::isAbsolute(name))
1060 name = to_utf8(makeRelPath(from_utf8(makeAbsPath(name, getMasterFilePath()).absFilename()),
1061 from_utf8(getParentFilePath())));
1065 /// Parse a NoWeb Scrap section. The initial "<<" is already parsed.
1066 void parse_noweb(Parser & p, ostream & os, Context & context)
1068 // assemble the rest of the keyword
1072 Token const & t = p.get_token();
1073 if (t.asInput() == ">" && p.next_token().asInput() == ">") {
1076 scrap = (p.good() && p.next_token().asInput() == "=");
1078 name += p.get_token().asInput();
1081 name += t.asInput();
1084 if (!scrap || !context.new_layout_allowed ||
1085 !context.textclass.hasLayout(from_ascii("Scrap"))) {
1086 cerr << "Warning: Could not interpret '" << name
1087 << "'. Ignoring it." << endl;
1091 // We use new_paragraph instead of check_end_layout because the stuff
1092 // following the noweb chunk needs to start with a \begin_layout.
1093 // This may create a new paragraph even if there was none in the
1094 // noweb file, but the alternative is an invalid LyX file. Since
1095 // noweb code chunks are implemented with a layout style in LyX they
1096 // always must be in an own paragraph.
1097 context.new_paragraph(os);
1098 Context newcontext(true, context.textclass,
1099 &context.textclass[from_ascii("Scrap")]);
1100 newcontext.check_layout(os);
1103 Token const & t = p.get_token();
1104 // We abuse the parser a bit, because this is no TeX syntax
1106 if (t.cat() == catEscape)
1107 os << subst(t.asInput(), "\\", "\n\\backslash\n");
1109 os << subst(t.asInput(), "\n", "\n\\newline\n");
1110 // The scrap chunk is ended by an @ at the beginning of a line.
1111 // After the @ the line may contain a comment and/or
1112 // whitespace, but nothing else.
1113 if (t.asInput() == "@" && p.prev_token().cat() == catNewline &&
1114 (p.next_token().cat() == catSpace ||
1115 p.next_token().cat() == catNewline ||
1116 p.next_token().cat() == catComment)) {
1117 while (p.good() && p.next_token().cat() == catSpace)
1118 os << p.get_token().asInput();
1119 if (p.next_token().cat() == catComment)
1120 // The comment includes a final '\n'
1121 os << p.get_token().asInput();
1123 if (p.next_token().cat() == catNewline)
1130 newcontext.check_end_layout(os);
1133 } // anonymous namespace
1136 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
1139 Layout const * newlayout = 0;
1140 // store the current selectlanguage to be used after \foreignlanguage
1142 // Store the latest bibliographystyle (needed for bibtex inset)
1143 string bibliographystyle;
1144 bool const use_natbib = used_packages.find("natbib") != used_packages.end();
1145 bool const use_jurabib = used_packages.find("jurabib") != used_packages.end();
1147 Token const & t = p.get_token();
1150 cerr << "t: " << t << " flags: " << flags << "\n";
1153 if (flags & FLAG_ITEM) {
1154 if (t.cat() == catSpace)
1157 flags &= ~FLAG_ITEM;
1158 if (t.cat() == catBegin) {
1159 // skip the brace and collect everything to the next matching
1161 flags |= FLAG_BRACE_LAST;
1165 // handle only this single token, leave the loop if done
1166 flags |= FLAG_LEAVE;
1169 if (t.character() == ']' && (flags & FLAG_BRACK_LAST))
1175 if (t.cat() == catMath) {
1176 // we are inside some text mode thingy, so opening new math is allowed
1177 context.check_layout(os);
1178 begin_inset(os, "Formula ");
1179 Token const & n = p.get_token();
1180 if (n.cat() == catMath && outer) {
1181 // TeX's $$...$$ syntax for displayed math
1183 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
1185 p.get_token(); // skip the second '$' token
1187 // simple $...$ stuff
1190 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
1196 else if (t.cat() == catSuper || t.cat() == catSub)
1197 cerr << "catcode " << t << " illegal in text mode\n";
1199 // Basic support for english quotes. This should be
1200 // extended to other quotes, but is not so easy (a
1201 // left english quote is the same as a right german
1203 else if (t.asInput() == "`" && p.next_token().asInput() == "`") {
1204 context.check_layout(os);
1205 begin_inset(os, "Quotes ");
1211 else if (t.asInput() == "'" && p.next_token().asInput() == "'") {
1212 context.check_layout(os);
1213 begin_inset(os, "Quotes ");
1220 else if (t.asInput() == ">" && p.next_token().asInput() == ">") {
1221 context.check_layout(os);
1222 begin_inset(os, "Quotes ");
1229 else if (t.asInput() == "<" && p.next_token().asInput() == "<") {
1230 context.check_layout(os);
1231 begin_inset(os, "Quotes ");
1238 else if (t.asInput() == "<"
1239 && p.next_token().asInput() == "<" && noweb_mode) {
1241 parse_noweb(p, os, context);
1244 else if (t.cat() == catSpace || (t.cat() == catNewline && ! p.isParagraph()))
1245 check_space(p, os, context);
1247 else if (t.character() == '[' && noweb_mode &&
1248 p.next_token().character() == '[') {
1249 // These can contain underscores
1251 string const s = p.getFullOpt() + ']';
1252 if (p.next_token().character() == ']')
1255 cerr << "Warning: Inserting missing ']' in '"
1256 << s << "'." << endl;
1257 handle_ert(os, s, context);
1260 else if (t.cat() == catLetter ||
1261 t.cat() == catOther ||
1262 t.cat() == catAlign ||
1263 t.cat() == catParameter) {
1264 // This translates "&" to "\\&" which may be wrong...
1265 context.check_layout(os);
1269 else if (p.isParagraph()) {
1270 if (context.new_layout_allowed)
1271 context.new_paragraph(os);
1273 handle_ert(os, "\\par ", context);
1274 eat_whitespace(p, os, context, true);
1277 else if (t.cat() == catActive) {
1278 context.check_layout(os);
1279 if (t.character() == '~') {
1280 if (context.layout->free_spacing)
1283 os << "\\InsetSpace ~\n";
1288 else if (t.cat() == catBegin &&
1289 p.next_token().cat() == catEnd) {
1291 Token const prev = p.prev_token();
1293 if (p.next_token().character() == '`' ||
1294 (prev.character() == '-' &&
1295 p.next_token().character() == '-'))
1296 ; // ignore it in {}`` or -{}-
1298 handle_ert(os, "{}", context);
1302 else if (t.cat() == catBegin) {
1303 context.check_layout(os);
1304 // special handling of font attribute changes
1305 Token const prev = p.prev_token();
1306 Token const next = p.next_token();
1307 TeXFont const oldFont = context.font;
1308 if (next.character() == '[' ||
1309 next.character() == ']' ||
1310 next.character() == '*') {
1312 if (p.next_token().cat() == catEnd) {
1317 handle_ert(os, "{", context);
1318 parse_text_snippet(p, os,
1321 handle_ert(os, "}", context);
1323 } else if (! context.new_layout_allowed) {
1324 handle_ert(os, "{", context);
1325 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1327 handle_ert(os, "}", context);
1328 } else if (is_known(next.cs(), known_sizes)) {
1329 // next will change the size, so we must
1331 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1333 if (!context.atParagraphStart())
1335 << context.font.size << "\n";
1336 } else if (is_known(next.cs(), known_font_families)) {
1337 // next will change the font family, so we
1338 // must reset it here
1339 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1341 if (!context.atParagraphStart())
1343 << context.font.family << "\n";
1344 } else if (is_known(next.cs(), known_font_series)) {
1345 // next will change the font series, so we
1346 // must reset it here
1347 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1349 if (!context.atParagraphStart())
1351 << context.font.series << "\n";
1352 } else if (is_known(next.cs(), known_font_shapes)) {
1353 // next will change the font shape, so we
1354 // must reset it here
1355 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1357 if (!context.atParagraphStart())
1359 << context.font.shape << "\n";
1360 } else if (is_known(next.cs(), known_old_font_families) ||
1361 is_known(next.cs(), known_old_font_series) ||
1362 is_known(next.cs(), known_old_font_shapes)) {
1363 // next will change the font family, series
1364 // and shape, so we must reset it here
1365 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1367 if (!context.atParagraphStart())
1369 << context.font.family
1371 << context.font.series
1373 << context.font.shape << "\n";
1375 handle_ert(os, "{", context);
1376 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1378 handle_ert(os, "}", context);
1382 else if (t.cat() == catEnd) {
1383 if (flags & FLAG_BRACE_LAST) {
1386 cerr << "stray '}' in text\n";
1387 handle_ert(os, "}", context);
1390 else if (t.cat() == catComment)
1391 parse_comment(p, os, t, context);
1394 // control sequences
1397 else if (t.cs() == "(") {
1398 context.check_layout(os);
1399 begin_inset(os, "Formula");
1401 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
1406 else if (t.cs() == "[") {
1407 context.check_layout(os);
1408 begin_inset(os, "Formula");
1410 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
1415 else if (t.cs() == "begin")
1416 parse_environment(p, os, outer, context);
1418 else if (t.cs() == "end") {
1419 if (flags & FLAG_END) {
1420 // eat environment name
1421 string const name = p.getArg('{', '}');
1422 if (name != active_environment())
1423 cerr << "\\end{" + name + "} does not match \\begin{"
1424 + active_environment() + "}\n";
1427 p.error("found 'end' unexpectedly");
1430 else if (t.cs() == "item") {
1433 bool optarg = false;
1434 if (p.next_token().character() == '[') {
1435 p.get_token(); // eat '['
1436 s = parse_text_snippet(p, FLAG_BRACK_LAST,
1441 context.check_layout(os);
1442 if (context.has_item) {
1443 // An item in an unknown list-like environment
1444 // FIXME: Do this in check_layout()!
1445 context.has_item = false;
1447 handle_ert(os, "\\item", context);
1449 handle_ert(os, "\\item ", context);
1452 if (context.layout->labeltype != LABEL_MANUAL) {
1453 // lyx does not support \item[\mybullet]
1454 // in itemize environments
1455 handle_ert(os, "[", context);
1457 handle_ert(os, "]", context);
1458 } else if (!s.empty()) {
1459 // The space is needed to separate the
1460 // item from the rest of the sentence.
1462 eat_whitespace(p, os, context, false);
1467 else if (t.cs() == "bibitem") {
1469 context.check_layout(os);
1470 begin_Cinset(os, "bibitem");
1471 LatexCommand(os, "bibitem");
1473 os << "key " << '"' << p.verbatim_item() << '"' << "\n";
1477 else if (t.cs() == "def") {
1478 context.check_layout(os);
1479 eat_whitespace(p, os, context, false);
1480 string name = p.get_token().cs();
1481 eat_whitespace(p, os, context, false);
1487 while (p.next_token().cat() != catBegin) {
1488 if (p.next_token().cat() == catParameter) {
1493 // followed by number?
1494 if (p.next_token().cat() == catOther) {
1495 char c = p.getChar();
1497 // number = current arity + 1?
1498 if (c == arity + '0' + 1)
1503 paramtext += p.get_token().asString();
1505 paramtext += p.get_token().asString();
1510 // only output simple (i.e. compatible) macro as FormulaMacros
1511 string ert = "\\def\\" + name + ' ' + paramtext + '{' + p.verbatim_item() + '}';
1513 context.check_layout(os);
1514 begin_inset(os, "FormulaMacro");
1518 handle_ert(os, ert, context);
1521 else if (t.cs() == "noindent") {
1523 context.add_par_extra_stuff("\\noindent\n");
1526 else if (t.cs() == "appendix") {
1527 context.add_par_extra_stuff("\\start_of_appendix\n");
1528 // We need to start a new paragraph. Otherwise the
1529 // appendix in 'bla\appendix\chapter{' would start
1531 context.new_paragraph(os);
1532 // We need to make sure that the paragraph is
1533 // generated even if it is empty. Otherwise the
1534 // appendix in '\par\appendix\par\chapter{' would
1536 context.check_layout(os);
1537 // FIXME: This is a hack to prevent paragraph
1538 // deletion if it is empty. Handle this better!
1540 "%dummy comment inserted by tex2lyx to "
1541 "ensure that this paragraph is not empty",
1543 // Both measures above may generate an additional
1544 // empty paragraph, but that does not hurt, because
1545 // whitespace does not matter here.
1546 eat_whitespace(p, os, context, true);
1549 // Starred section headings
1550 // Must attempt to parse "Section*" before "Section".
1551 else if ((p.next_token().asInput() == "*") &&
1552 context.new_layout_allowed &&
1553 (newlayout = findLayout(context.textclass, t.cs() + '*')) &&
1554 newlayout->isCommand()) {
1555 TeXFont const oldFont = context.font;
1556 // save the current font size
1557 string const size = oldFont.size;
1558 // reset the font size to default, because the
1559 // font size switches don't affect section
1560 // headings and the like
1561 context.font.size = known_coded_sizes[0];
1562 output_font_change(os, oldFont, context.font);
1565 output_command_layout(os, p, outer, context, newlayout);
1566 // set the font size to the original value
1567 context.font.size = size;
1568 output_font_change(os, oldFont, context.font);
1572 // Section headings and the like
1573 else if (context.new_layout_allowed &&
1574 (newlayout = findLayout(context.textclass, t.cs())) &&
1575 newlayout->isCommand()) {
1576 TeXFont const oldFont = context.font;
1577 // save the current font size
1578 string const size = oldFont.size;
1579 // reset the font size to default, because the font size switches don't
1580 // affect section headings and the like
1581 context.font.size = known_coded_sizes[0];
1582 output_font_change(os, oldFont, context.font);
1584 output_command_layout(os, p, outer, context, newlayout);
1585 // set the font size to the original value
1586 context.font.size = size;
1587 output_font_change(os, oldFont, context.font);
1591 // Special handling for \caption
1592 // FIXME: remove this when InsetCaption is supported.
1593 else if (context.new_layout_allowed &&
1594 t.cs() == captionlayout()->latexname()) {
1595 output_command_layout(os, p, outer, context,
1600 else if (t.cs() == "includegraphics") {
1601 bool const clip = p.next_token().asInput() == "*";
1604 map<string, string> opts = split_map(p.getArg('[', ']'));
1606 opts["clip"] = string();
1607 string name = normalize_filename(p.verbatim_item());
1609 string const path = getMasterFilePath();
1610 // We want to preserve relative / absolute filenames,
1611 // therefore path is only used for testing
1612 if (!makeAbsPath(name, path).exists()) {
1613 // The file extension is probably missing.
1614 // Now try to find it out.
1615 string const dvips_name =
1616 find_file(name, path,
1617 known_dvips_graphics_formats);
1618 string const pdftex_name =
1619 find_file(name, path,
1620 known_pdftex_graphics_formats);
1621 if (!dvips_name.empty()) {
1622 if (!pdftex_name.empty()) {
1623 cerr << "This file contains the "
1625 "\"\\includegraphics{"
1627 "However, files\n\""
1628 << dvips_name << "\" and\n\""
1629 << pdftex_name << "\"\n"
1630 "both exist, so I had to make a "
1631 "choice and took the first one.\n"
1632 "Please move the unwanted one "
1633 "someplace else and try again\n"
1634 "if my choice was wrong."
1638 } else if (!pdftex_name.empty())
1642 if (makeAbsPath(name, path).exists())
1643 fix_relative_filename(name);
1645 cerr << "Warning: Could not find graphics file '"
1646 << name << "'." << endl;
1648 context.check_layout(os);
1649 begin_inset(os, "Graphics ");
1650 os << "\n\tfilename " << name << '\n';
1651 if (opts.find("width") != opts.end())
1653 << translate_len(opts["width"]) << '\n';
1654 if (opts.find("height") != opts.end())
1656 << translate_len(opts["height"]) << '\n';
1657 if (opts.find("scale") != opts.end()) {
1658 istringstream iss(opts["scale"]);
1662 os << "\tscale " << val << '\n';
1664 if (opts.find("angle") != opts.end())
1665 os << "\trotateAngle "
1666 << opts["angle"] << '\n';
1667 if (opts.find("origin") != opts.end()) {
1669 string const opt = opts["origin"];
1670 if (opt.find('l') != string::npos) ss << "left";
1671 if (opt.find('r') != string::npos) ss << "right";
1672 if (opt.find('c') != string::npos) ss << "center";
1673 if (opt.find('t') != string::npos) ss << "Top";
1674 if (opt.find('b') != string::npos) ss << "Bottom";
1675 if (opt.find('B') != string::npos) ss << "Baseline";
1676 if (!ss.str().empty())
1677 os << "\trotateOrigin " << ss.str() << '\n';
1679 cerr << "Warning: Ignoring unknown includegraphics origin argument '" << opt << "'\n";
1681 if (opts.find("keepaspectratio") != opts.end())
1682 os << "\tkeepAspectRatio\n";
1683 if (opts.find("clip") != opts.end())
1685 if (opts.find("draft") != opts.end())
1687 if (opts.find("bb") != opts.end())
1688 os << "\tBoundingBox "
1689 << opts["bb"] << '\n';
1690 int numberOfbbOptions = 0;
1691 if (opts.find("bbllx") != opts.end())
1692 numberOfbbOptions++;
1693 if (opts.find("bblly") != opts.end())
1694 numberOfbbOptions++;
1695 if (opts.find("bburx") != opts.end())
1696 numberOfbbOptions++;
1697 if (opts.find("bbury") != opts.end())
1698 numberOfbbOptions++;
1699 if (numberOfbbOptions == 4)
1700 os << "\tBoundingBox "
1701 << opts["bbllx"] << " " << opts["bblly"] << " "
1702 << opts["bburx"] << " " << opts["bbury"] << '\n';
1703 else if (numberOfbbOptions > 0)
1704 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
1705 numberOfbbOptions = 0;
1706 if (opts.find("natwidth") != opts.end())
1707 numberOfbbOptions++;
1708 if (opts.find("natheight") != opts.end())
1709 numberOfbbOptions++;
1710 if (numberOfbbOptions == 2)
1711 os << "\tBoundingBox 0bp 0bp "
1712 << opts["natwidth"] << " " << opts["natheight"] << '\n';
1713 else if (numberOfbbOptions > 0)
1714 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
1715 ostringstream special;
1716 if (opts.find("hiresbb") != opts.end())
1717 special << "hiresbb,";
1718 if (opts.find("trim") != opts.end())
1720 if (opts.find("viewport") != opts.end())
1721 special << "viewport=" << opts["viewport"] << ',';
1722 if (opts.find("totalheight") != opts.end())
1723 special << "totalheight=" << opts["totalheight"] << ',';
1724 if (opts.find("type") != opts.end())
1725 special << "type=" << opts["type"] << ',';
1726 if (opts.find("ext") != opts.end())
1727 special << "ext=" << opts["ext"] << ',';
1728 if (opts.find("read") != opts.end())
1729 special << "read=" << opts["read"] << ',';
1730 if (opts.find("command") != opts.end())
1731 special << "command=" << opts["command"] << ',';
1732 string s_special = special.str();
1733 if (!s_special.empty()) {
1734 // We had special arguments. Remove the trailing ','.
1735 os << "\tspecial " << s_special.substr(0, s_special.size() - 1) << '\n';
1737 // TODO: Handle the unknown settings better.
1738 // Warn about invalid options.
1739 // Check whether some option was given twice.
1743 else if (t.cs() == "footnote" ||
1744 (t.cs() == "thanks" && context.layout->intitle)) {
1746 context.check_layout(os);
1747 begin_inset(os, "Foot\n");
1748 os << "status collapsed\n\n";
1749 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
1753 else if (t.cs() == "marginpar") {
1755 context.check_layout(os);
1756 begin_inset(os, "Marginal\n");
1757 os << "status collapsed\n\n";
1758 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
1762 else if (t.cs() == "ensuremath") {
1764 context.check_layout(os);
1765 string const s = p.verbatim_item();
1766 //FIXME: this never triggers in UTF8
1767 if (s == "\xb1" || s == "\xb3" || s == "\xb2" || s == "\xb5")
1770 handle_ert(os, "\\ensuremath{" + s + "}",
1774 else if (t.cs() == "hfill") {
1775 context.check_layout(os);
1776 os << "\n\\hfill\n";
1781 else if (t.cs() == "makeindex" || t.cs() == "maketitle") {
1782 // FIXME: Somehow prevent title layouts if
1783 // "maketitle" was not found
1785 skip_braces(p); // swallow this
1788 else if (t.cs() == "tableofcontents") {
1790 context.check_layout(os);
1791 begin_Cinset(os, "toc");
1792 LatexCommand(os, "tableofcontents");
1794 skip_braces(p); // swallow this
1797 else if (t.cs() == "listoffigures") {
1799 context.check_layout(os);
1800 begin_inset(os, "FloatList figure\n");
1802 skip_braces(p); // swallow this
1805 else if (t.cs() == "listoftables") {
1807 context.check_layout(os);
1808 begin_inset(os, "FloatList table\n");
1810 skip_braces(p); // swallow this
1813 else if (t.cs() == "listof") {
1814 p.skip_spaces(true);
1815 string const name = p.get_token().asString();
1816 if (context.textclass.floats().typeExist(name)) {
1817 context.check_layout(os);
1818 begin_inset(os, "FloatList ");
1821 p.get_token(); // swallow second arg
1823 handle_ert(os, "\\listof{" + name + "}", context);
1826 else if (t.cs() == "textrm")
1827 parse_text_attributes(p, os, FLAG_ITEM, outer,
1828 context, "\\family",
1829 context.font.family, "roman");
1831 else if (t.cs() == "textsf")
1832 parse_text_attributes(p, os, FLAG_ITEM, outer,
1833 context, "\\family",
1834 context.font.family, "sans");
1836 else if (t.cs() == "texttt")
1837 parse_text_attributes(p, os, FLAG_ITEM, outer,
1838 context, "\\family",
1839 context.font.family, "typewriter");
1841 else if (t.cs() == "textmd")
1842 parse_text_attributes(p, os, FLAG_ITEM, outer,
1843 context, "\\series",
1844 context.font.series, "medium");
1846 else if (t.cs() == "textbf")
1847 parse_text_attributes(p, os, FLAG_ITEM, outer,
1848 context, "\\series",
1849 context.font.series, "bold");
1851 else if (t.cs() == "textup")
1852 parse_text_attributes(p, os, FLAG_ITEM, outer,
1854 context.font.shape, "up");
1856 else if (t.cs() == "textit")
1857 parse_text_attributes(p, os, FLAG_ITEM, outer,
1859 context.font.shape, "italic");
1861 else if (t.cs() == "textsl")
1862 parse_text_attributes(p, os, FLAG_ITEM, outer,
1864 context.font.shape, "slanted");
1866 else if (t.cs() == "textsc")
1867 parse_text_attributes(p, os, FLAG_ITEM, outer,
1869 context.font.shape, "smallcaps");
1871 else if (t.cs() == "textnormal" || t.cs() == "normalfont") {
1872 context.check_layout(os);
1873 TeXFont oldFont = context.font;
1874 context.font.init();
1875 context.font.size = oldFont.size;
1876 os << "\n\\family " << context.font.family << "\n";
1877 os << "\n\\series " << context.font.series << "\n";
1878 os << "\n\\shape " << context.font.shape << "\n";
1879 if (t.cs() == "textnormal") {
1880 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1881 output_font_change(os, context.font, oldFont);
1882 context.font = oldFont;
1884 eat_whitespace(p, os, context, false);
1887 else if (t.cs() == "textcolor") {
1888 // scheme is \textcolor{color name}{text}
1889 string const color = p.verbatim_item();
1890 // we only support the predefined colors of the color package
1891 if (color == "black" || color == "blue" || color == "cyan"
1892 || color == "green" || color == "magenta" || color == "red"
1893 || color == "white" || color == "yellow") {
1894 context.check_layout(os);
1895 os << "\n\\color " << color << "\n";
1896 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1897 context.check_layout(os);
1898 os << "\n\\color inherit\n";
1900 // for custom defined colors
1901 handle_ert(os, t.asInput() + "{" + color + "}", context);
1904 else if (t.cs() == "underbar") {
1905 // Do NOT handle \underline.
1906 // \underbar cuts through y, g, q, p etc.,
1907 // \underline does not.
1908 context.check_layout(os);
1909 os << "\n\\bar under\n";
1910 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1911 context.check_layout(os);
1912 os << "\n\\bar default\n";
1915 else if (t.cs() == "emph" || t.cs() == "noun") {
1916 context.check_layout(os);
1917 os << "\n\\" << t.cs() << " on\n";
1918 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1919 context.check_layout(os);
1920 os << "\n\\" << t.cs() << " default\n";
1923 else if (t.cs() == "lyxline") {
1924 context.check_layout(os);
1928 else if (is_known(t.cs(), known_ref_commands)) {
1929 context.check_layout(os);
1930 begin_Cinset(os, "ref");
1931 LatexCommand(os, t.cs());
1932 // lyx cannot handle newlines in a latex command
1933 // FIXME: Move the substitution into parser::getOpt()?
1934 os << subst(p.getOpt(), "\n", " ");
1935 os << "reference " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
1939 else if (use_natbib &&
1940 is_known(t.cs(), known_natbib_commands) &&
1941 ((t.cs() != "citefullauthor" &&
1942 t.cs() != "citeyear" &&
1943 t.cs() != "citeyearpar") ||
1944 p.next_token().asInput() != "*")) {
1945 context.check_layout(os);
1946 string command = t.cs();
1947 if (p.next_token().asInput() == "*") {
1951 if (command == "citefullauthor")
1952 // alternative name for "\\citeauthor*"
1953 command = "citeauthor*";
1955 // text before the citation
1957 // text after the citation
1959 get_cite_arguments(p, true, before, after);
1961 if (command == "cite") {
1962 // \cite without optional argument means
1963 // \citet, \cite with at least one optional
1964 // argument means \citep.
1965 if (before.empty() && after.empty())
1970 if (before.empty() && after == "[]")
1971 // avoid \citet[]{a}
1973 else if (before == "[]" && after == "[]") {
1974 // avoid \citet[][]{a}
1978 // remove the brackets around after and before
1979 if (!after.empty()) {
1981 after.erase(after.length() - 1, 1);
1983 if (!before.empty()) {
1985 before.erase(before.length() - 1, 1);
1987 begin_Cinset(os, "citation");
1988 LatexCommand(os, command);
1989 os << "after " << '"' << after << '"' << "\n";
1990 os << "before " << '"' << before << '"' << "\n";
1991 os << "key " << '"' << p.verbatim_item() << '"' << "\n";
1995 else if (use_jurabib &&
1996 is_known(t.cs(), known_jurabib_commands)) {
1997 context.check_layout(os);
1998 string const command = t.cs();
1999 char argumentOrder = '\0';
2000 vector<string> const & options = used_packages["jurabib"];
2001 if (find(options.begin(), options.end(),
2002 "natbiborder") != options.end())
2003 argumentOrder = 'n';
2004 else if (find(options.begin(), options.end(),
2005 "jurabiborder") != options.end())
2006 argumentOrder = 'j';
2008 // text before the citation
2010 // text after the citation
2012 get_cite_arguments(p, argumentOrder != 'j', before, after);
2014 string const citation = p.verbatim_item();
2015 if (!before.empty() && argumentOrder == '\0') {
2016 cerr << "Warning: Assuming argument order "
2017 "of jurabib version 0.6 for\n'"
2018 << command << before << after << '{'
2019 << citation << "}'.\n"
2020 "Add 'jurabiborder' to the jurabib "
2021 "package options if you used an\n"
2022 "earlier jurabib version." << endl;
2024 if (!after.empty()) {
2026 after.erase(after.length() - 1, 1);
2028 if (!before.empty()) {
2030 before.erase(before.length() - 1, 1);
2032 begin_Cinset(os, "citation");
2033 LatexCommand(os, command);
2034 os << "after " << '"' << after << '"' << "\n";
2035 os << "before " << '"' << before << '"' << "\n";
2036 os << "key " << '"' << citation << '"' << "\n";
2040 else if (t.cs() == "cite") {
2041 context.check_layout(os);
2042 // lyx cannot handle newlines in a latex command
2043 string after = subst(p.getOpt(), "\n", " ");
2044 if (!after.empty()) {
2046 after.erase(after.length() - 1, 1);
2048 begin_Cinset(os, "citation");
2049 LatexCommand(os, t.cs());
2050 os << "after " << '"' << after << '"' << "\n";
2051 os << "key " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
2055 else if (t.cs() == "index") {
2056 context.check_layout(os);
2057 begin_inset(os, "LatexCommand ");
2058 os << t.cs() << "\n";;
2059 // lyx cannot handle newlines in a latex command
2060 os << "name " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
2064 else if (t.cs() == "label") {
2065 context.check_layout(os);
2066 begin_Cinset(os, t.cs());
2067 LatexCommand(os, t.cs());
2068 // lyx cannot handle newlines in a latex command
2069 os << "name " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
2073 else if (t.cs() == "printindex") {
2074 context.check_layout(os);
2075 begin_Cinset(os, "index_print");
2076 LatexCommand(os, t.cs());
2081 else if (t.cs() == "url") {
2082 context.check_layout(os);
2083 begin_inset(os, "LatexCommand ");
2084 os << t.cs() << "\n";;
2085 // lyx cannot handle newlines in a latex command
2086 os << "target " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
2090 else if (is_known(t.cs(), known_quotes)) {
2091 char const * const * where = is_known(t.cs(), known_quotes);
2092 context.check_layout(os);
2093 begin_inset(os, "Quotes ");
2094 os << known_coded_quotes[where - known_quotes];
2096 // LyX adds {} after the quote, so we have to eat
2097 // spaces here if there are any before a possible
2099 eat_whitespace(p, os, context, false);
2103 else if (is_known(t.cs(), known_sizes) &&
2104 context.new_layout_allowed) {
2105 char const * const * where = is_known(t.cs(), known_sizes);
2106 context.check_layout(os);
2107 TeXFont const oldFont = context.font;
2108 // the font size index differs by 1, because the known_coded_sizes
2109 // has additionally a "default" entry
2110 context.font.size = known_coded_sizes[where - known_sizes + 1];
2111 output_font_change(os, oldFont, context.font);
2112 eat_whitespace(p, os, context, false);
2115 else if (is_known(t.cs(), known_font_families) &&
2116 context.new_layout_allowed) {
2117 char const * const * where =
2118 is_known(t.cs(), known_font_families);
2119 context.check_layout(os);
2120 TeXFont const oldFont = context.font;
2121 context.font.family =
2122 known_coded_font_families[where - known_font_families];
2123 output_font_change(os, oldFont, context.font);
2124 eat_whitespace(p, os, context, false);
2127 else if (is_known(t.cs(), known_font_series) &&
2128 context.new_layout_allowed) {
2129 char const * const * where =
2130 is_known(t.cs(), known_font_series);
2131 context.check_layout(os);
2132 TeXFont const oldFont = context.font;
2133 context.font.series =
2134 known_coded_font_series[where - known_font_series];
2135 output_font_change(os, oldFont, context.font);
2136 eat_whitespace(p, os, context, false);
2139 else if (is_known(t.cs(), known_font_shapes) &&
2140 context.new_layout_allowed) {
2141 char const * const * where =
2142 is_known(t.cs(), known_font_shapes);
2143 context.check_layout(os);
2144 TeXFont const oldFont = context.font;
2145 context.font.shape =
2146 known_coded_font_shapes[where - known_font_shapes];
2147 output_font_change(os, oldFont, context.font);
2148 eat_whitespace(p, os, context, false);
2150 else if (is_known(t.cs(), known_old_font_families) &&
2151 context.new_layout_allowed) {
2152 char const * const * where =
2153 is_known(t.cs(), known_old_font_families);
2154 context.check_layout(os);
2155 TeXFont const oldFont = context.font;
2156 context.font.init();
2157 context.font.size = oldFont.size;
2158 context.font.family =
2159 known_coded_font_families[where - known_old_font_families];
2160 output_font_change(os, oldFont, context.font);
2161 eat_whitespace(p, os, context, false);
2164 else if (is_known(t.cs(), known_old_font_series) &&
2165 context.new_layout_allowed) {
2166 char const * const * where =
2167 is_known(t.cs(), known_old_font_series);
2168 context.check_layout(os);
2169 TeXFont const oldFont = context.font;
2170 context.font.init();
2171 context.font.size = oldFont.size;
2172 context.font.series =
2173 known_coded_font_series[where - known_old_font_series];
2174 output_font_change(os, oldFont, context.font);
2175 eat_whitespace(p, os, context, false);
2178 else if (is_known(t.cs(), known_old_font_shapes) &&
2179 context.new_layout_allowed) {
2180 char const * const * where =
2181 is_known(t.cs(), known_old_font_shapes);
2182 context.check_layout(os);
2183 TeXFont const oldFont = context.font;
2184 context.font.init();
2185 context.font.size = oldFont.size;
2186 context.font.shape =
2187 known_coded_font_shapes[where - known_old_font_shapes];
2188 output_font_change(os, oldFont, context.font);
2189 eat_whitespace(p, os, context, false);
2192 else if (t.cs() == "selectlanguage") {
2193 context.check_layout(os);
2194 // save the language for the case that a
2195 // \foreignlanguage is used
2197 //FIXME: this is wrong, the language should
2198 // be saved in the context. (JMarc)
2199 selectlang = subst(p.verbatim_item(), "\n", " ");
2200 os << "\\lang " << selectlang << "\n";
2203 else if (t.cs() == "foreignlanguage") {
2204 context.check_layout(os);
2205 os << "\n\\lang " << subst(p.verbatim_item(), "\n", " ") << "\n";
2206 os << subst(p.verbatim_item(), "\n", " ");
2207 // FIXME: the second argument of selectlanguage
2208 // has to be parsed (like for \textsf, for
2210 // set back to last selectlanguage
2211 os << "\n\\lang " << selectlang << "\n";
2214 else if (t.cs() == "inputencoding") {
2215 // nothing to write here
2216 string const enc = subst(p.verbatim_item(), "\n", " ");
2219 else if (t.cs() == "LyX" || t.cs() == "TeX"
2220 || t.cs() == "LaTeX") {
2221 context.check_layout(os);
2223 skip_braces(p); // eat {}
2226 else if (t.cs() == "LaTeXe") {
2227 context.check_layout(os);
2229 skip_braces(p); // eat {}
2232 else if (t.cs() == "ldots") {
2233 context.check_layout(os);
2235 os << "\\SpecialChar \\ldots{}\n";
2238 else if (t.cs() == "lyxarrow") {
2239 context.check_layout(os);
2240 os << "\\SpecialChar \\menuseparator\n";
2244 else if (t.cs() == "textcompwordmark") {
2245 context.check_layout(os);
2246 os << "\\SpecialChar \\textcompwordmark{}\n";
2250 else if (t.cs() == "@" && p.next_token().asInput() == ".") {
2251 context.check_layout(os);
2252 os << "\\SpecialChar \\@.\n";
2256 else if (t.cs() == "-") {
2257 context.check_layout(os);
2258 os << "\\SpecialChar \\-\n";
2261 else if (t.cs() == "textasciitilde") {
2262 context.check_layout(os);
2267 else if (t.cs() == "textasciicircum") {
2268 context.check_layout(os);
2273 else if (t.cs() == "textbackslash") {
2274 context.check_layout(os);
2275 os << "\n\\backslash\n";
2279 else if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#"
2280 || t.cs() == "$" || t.cs() == "{" || t.cs() == "}"
2282 context.check_layout(os);
2286 else if (t.cs() == "char") {
2287 context.check_layout(os);
2288 if (p.next_token().character() == '`') {
2290 if (p.next_token().cs() == "\"") {
2295 handle_ert(os, "\\char`", context);
2298 handle_ert(os, "\\char", context);
2302 else if (t.cs() == "verb") {
2303 context.check_layout(os);
2304 char const delimiter = p.next_token().character();
2305 string const arg = p.getArg(delimiter, delimiter);
2307 oss << "\\verb" << delimiter << arg << delimiter;
2308 handle_ert(os, oss.str(), context);
2311 // Problem: \= creates a tabstop inside the tabbing environment
2312 // and else an accent. In the latter case we really would want
2313 // \={o} instead of \= o.
2314 else if (t.cs() == "=" && (flags & FLAG_TABBING))
2315 handle_ert(os, t.asInput(), context);
2317 // accents (see Table 6 in Comprehensive LaTeX Symbol List)
2318 else if (t.cs().size() == 1
2319 && contains("\"'.=^`bcdHkrtuv~", t.cs())) {
2320 context.check_layout(os);
2321 // try to see whether the string is in unicodesymbols
2323 string command = t.asInput() + "{"
2324 + trim(p.verbatim_item())
2326 docstring s = encodings.fromLaTeXCommand(from_utf8(command), rem);
2329 cerr << "When parsing " << command
2330 << ", result is " << to_utf8(s)
2331 << "+" << to_utf8(rem) << endl;
2334 // we did not find a non-ert version
2335 handle_ert(os, command, context);
2338 else if (t.cs() == "\\") {
2339 context.check_layout(os);
2340 string const next = p.next_token().asInput();
2342 handle_ert(os, "\\\\" + p.getOpt(), context);
2343 else if (next == "*") {
2345 handle_ert(os, "\\\\*" + p.getOpt(), context);
2348 os << "\n\\newline\n";
2352 else if (t.cs() == "newline" ||
2353 t.cs() == "linebreak") {
2354 context.check_layout(os);
2355 os << "\n\\" << t.cs() << "\n";
2356 skip_braces(p); // eat {}
2359 else if (t.cs() == "input" || t.cs() == "include"
2360 || t.cs() == "verbatiminput") {
2361 string name = '\\' + t.cs();
2362 if (t.cs() == "verbatiminput"
2363 && p.next_token().asInput() == "*")
2364 name += p.get_token().asInput();
2365 context.check_layout(os);
2366 begin_inset(os, "Include ");
2367 string filename(normalize_filename(p.getArg('{', '}')));
2368 string const path = getMasterFilePath();
2369 // We want to preserve relative / absolute filenames,
2370 // therefore path is only used for testing
2371 if ((t.cs() == "include" || t.cs() == "input") &&
2372 !makeAbsPath(filename, path).exists()) {
2373 // The file extension is probably missing.
2374 // Now try to find it out.
2375 string const tex_name =
2376 find_file(filename, path,
2377 known_tex_extensions);
2378 if (!tex_name.empty())
2379 filename = tex_name;
2381 if (makeAbsPath(filename, path).exists()) {
2382 string const abstexname =
2383 makeAbsPath(filename, path).absFilename();
2384 string const abslyxname =
2385 changeExtension(abstexname, ".lyx");
2386 fix_relative_filename(filename);
2387 string const lyxname =
2388 changeExtension(filename, ".lyx");
2389 if (t.cs() != "verbatiminput" &&
2390 tex2lyx(abstexname, FileName(abslyxname),
2392 os << name << '{' << lyxname << "}\n";
2394 os << name << '{' << filename << "}\n";
2397 cerr << "Warning: Could not find included file '"
2398 << filename << "'." << endl;
2399 os << name << '{' << filename << "}\n";
2401 os << "preview false\n";
2405 else if (t.cs() == "bibliographystyle") {
2406 // store new bibliographystyle
2407 bibliographystyle = p.verbatim_item();
2408 // output new bibliographystyle.
2409 // This is only necessary if used in some other macro than \bibliography.
2410 handle_ert(os, "\\bibliographystyle{" + bibliographystyle + "}", context);
2413 else if (t.cs() == "bibliography") {
2414 context.check_layout(os);
2415 begin_Cinset(os, "bibtex");
2416 LatexCommand(os, "bibtex");
2417 os << "bibfiles " << '"' << p.verbatim_item() << '"' << "\n";
2418 // Do we have a bibliographystyle set?
2419 if (!bibliographystyle.empty())
2420 os << "options " << '"' << bibliographystyle << '"' << "\n";
2424 else if (t.cs() == "parbox")
2425 parse_box(p, os, FLAG_ITEM, outer, context, true);
2427 //\makebox() is part of the picture environment and different from \makebox{}
2428 //\makebox{} will be parsed by parse_box when bug 2956 is fixed
2429 else if (t.cs() == "makebox") {
2430 string arg = t.asInput();
2431 if (p.next_token().character() == '(')
2432 //the syntax is: \makebox(x,y)[position]{content}
2433 arg += p.getFullParentheseArg();
2435 //the syntax is: \makebox[width][position]{content}
2436 arg += p.getFullOpt();
2437 handle_ert(os, arg + p.getFullOpt(), context);
2440 else if (t.cs() == "smallskip" ||
2441 t.cs() == "medskip" ||
2442 t.cs() == "bigskip" ||
2443 t.cs() == "vfill") {
2444 context.check_layout(os);
2445 begin_inset(os, "VSpace ");
2451 else if (is_known(t.cs(), known_spaces)) {
2452 char const * const * where = is_known(t.cs(), known_spaces);
2453 context.check_layout(os);
2454 os << "\\InsetSpace ";
2455 os << '\\' << known_coded_spaces[where - known_spaces]
2457 // LaTeX swallows whitespace after all spaces except
2458 // "\\,". We have to do that here, too, because LyX
2459 // adds "{}" which would make the spaces significant.
2461 eat_whitespace(p, os, context, false);
2462 // LyX adds "{}" after all spaces except "\\ " and
2463 // "\\,", so we have to remove "{}".
2464 // "\\,{}" is equivalent to "\\," in LaTeX, so we
2465 // remove the braces after "\\,", too.
2470 else if (t.cs() == "newpage" ||
2471 t.cs() == "pagebreak" ||
2472 t.cs() == "clearpage" ||
2473 t.cs() == "cleardoublepage") {
2474 context.check_layout(os);
2475 os << "\n\\" << t.cs() << "\n";
2476 skip_braces(p); // eat {}
2479 else if (t.cs() == "newcommand" ||
2480 t.cs() == "providecommand" ||
2481 t.cs() == "renewcommand") {
2482 // these could be handled by parse_command(), but
2483 // we need to call add_known_command() here.
2484 string name = t.asInput();
2485 if (p.next_token().asInput() == "*") {
2486 // Starred form. Eat '*'
2490 string const command = p.verbatim_item();
2491 string const opt1 = p.getOpt();
2492 string const opt2 = p.getFullOpt();
2493 add_known_command(command, opt1, !opt2.empty());
2494 string const ert = name + '{' + command + '}' +
2496 '{' + p.verbatim_item() + '}';
2498 context.check_layout(os);
2499 begin_inset(os, "FormulaMacro");
2504 else if (t.cs() == "vspace") {
2505 bool starred = false;
2506 if (p.next_token().asInput() == "*") {
2510 string const length = p.verbatim_item();
2513 bool valid = splitLatexLength(length, valstring, unit);
2514 bool known_vspace = false;
2515 bool known_unit = false;
2518 istringstream iss(valstring);
2521 if (unit == "\\smallskipamount") {
2523 known_vspace = true;
2524 } else if (unit == "\\medskipamount") {
2526 known_vspace = true;
2527 } else if (unit == "\\bigskipamount") {
2529 known_vspace = true;
2530 } else if (unit == "\\fill") {
2532 known_vspace = true;
2535 if (!known_vspace) {
2536 switch (unitFromString(unit)) {
2557 if (known_unit || known_vspace) {
2558 // Literal length or known variable
2559 context.check_layout(os);
2560 begin_inset(os, "VSpace ");
2568 // LyX can't handle other length variables in Inset VSpace
2569 string name = t.asInput();
2574 handle_ert(os, name + '{' + unit + '}', context);
2575 else if (value == -1.0)
2576 handle_ert(os, name + "{-" + unit + '}', context);
2578 handle_ert(os, name + '{' + valstring + unit + '}', context);
2580 handle_ert(os, name + '{' + length + '}', context);
2585 // try to see whether the string is in unicodesymbols
2587 docstring s = encodings.fromLaTeXCommand(from_utf8(t.asInput()), rem);
2590 cerr << "When parsing " << t.cs()
2591 << ", result is " << to_utf8(s)
2592 << "+" << to_utf8(rem) << endl;
2593 context.check_layout(os);
2596 skip_braces(p); // eat {}
2598 //cerr << "#: " << t << " mode: " << mode << endl;
2599 // heuristic: read up to next non-nested space
2601 string s = t.asInput();
2602 string z = p.verbatim_item();
2603 while (p.good() && z != " " && z.size()) {
2604 //cerr << "read: " << z << endl;
2606 z = p.verbatim_item();
2608 cerr << "found ERT: " << s << endl;
2609 handle_ert(os, s + ' ', context);
2612 string name = t.asInput();
2613 if (p.next_token().asInput() == "*") {
2614 // Starred commands like \vspace*{}
2615 p.get_token(); // Eat '*'
2618 if (!parse_command(name, p, os, outer, context))
2619 handle_ert(os, name, context);
2623 if (flags & FLAG_LEAVE) {
2624 flags &= ~FLAG_LEAVE;