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.
20 #include "FloatList.h"
24 #include "support/lassert.h"
25 #include "support/convert.h"
26 #include "support/FileName.h"
27 #include "support/filetools.h"
28 #include "support/lstrings.h"
37 using namespace lyx::support;
42 void parse_text_in_inset(Parser & p, ostream & os, unsigned flags, bool outer,
43 Context const & context)
45 Context newcontext(true, context.textclass);
46 newcontext.font = context.font;
47 parse_text(p, os, flags, outer, newcontext);
48 newcontext.check_end_layout(os);
54 /// parses a paragraph snippet, useful for example for \\emph{...}
55 void parse_text_snippet(Parser & p, ostream & os, unsigned flags, bool outer,
58 Context newcontext(context);
59 // Don't inherit the paragraph-level extra stuff
60 newcontext.par_extra_stuff.clear();
61 parse_text(p, os, flags, outer, newcontext);
62 // Make sure that we don't create invalid .lyx files
63 context.need_layout = newcontext.need_layout;
64 context.need_end_layout = newcontext.need_end_layout;
69 * Thin wrapper around parse_text_snippet() using a string.
71 * We completely ignore \c context.need_layout and \c context.need_end_layout,
72 * because our return value is not used directly (otherwise the stream version
73 * of parse_text_snippet() could be used). That means that the caller needs
74 * to do layout management manually.
75 * This is intended to parse text that does not create any layout changes.
77 string parse_text_snippet(Parser & p, unsigned flags, const bool outer,
80 Context newcontext(context);
81 newcontext.need_layout = false;
82 newcontext.need_end_layout = false;
83 newcontext.new_layout_allowed = false;
84 // Avoid warning by Context::~Context()
85 newcontext.par_extra_stuff.clear();
87 parse_text_snippet(p, os, flags, outer, newcontext);
92 char const * const known_latex_commands[] = { "ref", "cite", "label",
93 "index", "printindex", "pageref", "url", "vref", "vpageref", "prettyref",
98 * We can't put these into known_latex_commands because the argument order
99 * is reversed in lyx if there are 2 arguments.
100 * The starred forms are also known.
102 char const * const known_natbib_commands[] = { "cite", "citet", "citep",
103 "citealt", "citealp", "citeauthor", "citeyear", "citeyearpar",
104 "citefullauthor", "Citet", "Citep", "Citealt", "Citealp", "Citeauthor", 0 };
108 * We can't put these into known_latex_commands because the argument order
109 * is reversed in lyx if there are 2 arguments.
110 * No starred form other than "cite*" known.
112 char const * const known_jurabib_commands[] = { "cite", "citet", "citep",
113 "citealt", "citealp", "citeauthor", "citeyear", "citeyearpar",
114 // jurabib commands not (yet) supported by LyX:
116 // "footcite", "footcitet", "footcitep", "footcitealt", "footcitealp",
117 // "footciteauthor", "footciteyear", "footciteyearpar",
118 "citefield", "citetitle", "cite*", 0 };
120 /// LaTeX names for quotes
121 char const * const known_quotes[] = { "dq", "guillemotleft", "flqq", "og",
122 "guillemotright", "frqq", "fg", "glq", "glqq", "textquoteleft", "grq", "grqq",
123 "quotedblbase", "textquotedblleft", "quotesinglbase", "textquoteright", "flq",
124 "guilsinglleft", "frq", "guilsinglright", 0};
126 /// the same as known_quotes with .lyx names
127 char const * const known_coded_quotes[] = { "prd", "ard", "ard", "ard",
128 "ald", "ald", "ald", "gls", "gld", "els", "els", "grd",
129 "gld", "grd", "gls", "ers", "fls",
130 "fls", "frs", "frs", 0};
132 /// LaTeX names for font sizes
133 char const * const known_sizes[] = { "tiny", "scriptsize", "footnotesize",
134 "small", "normalsize", "large", "Large", "LARGE", "huge", "Huge", 0};
136 /// the same as known_sizes with .lyx names plus a default entry
137 char const * const known_coded_sizes[] = { "default", "tiny", "scriptsize", "footnotesize",
138 "small", "normal", "large", "larger", "largest", "huge", "giant", 0};
140 /// LaTeX 2.09 names for font families
141 char const * const known_old_font_families[] = { "rm", "sf", "tt", 0};
143 /// LaTeX names for font families
144 char const * const known_font_families[] = { "rmfamily", "sffamily",
147 /// the same as known_old_font_families and known_font_families with .lyx names
148 char const * const known_coded_font_families[] = { "roman", "sans",
151 /// LaTeX 2.09 names for font series
152 char const * const known_old_font_series[] = { "bf", 0};
154 /// LaTeX names for font series
155 char const * const known_font_series[] = { "bfseries", "mdseries", 0};
157 /// the same as known_old_font_series and known_font_series with .lyx names
158 char const * const known_coded_font_series[] = { "bold", "medium", 0};
160 /// LaTeX 2.09 names for font shapes
161 char const * const known_old_font_shapes[] = { "it", "sl", "sc", 0};
163 /// LaTeX names for font shapes
164 char const * const known_font_shapes[] = { "itshape", "slshape", "scshape",
167 /// the same as known_old_font_shapes and known_font_shapes with .lyx names
168 char const * const known_coded_font_shapes[] = { "italic", "slanted",
169 "smallcaps", "up", 0};
172 * Graphics file extensions known by the dvips driver of the graphics package.
173 * These extensions are used to complete the filename of an included
174 * graphics file if it does not contain an extension.
175 * The order must be the same that latex uses to find a file, because we
176 * will use the first extension that matches.
177 * This is only an approximation for the common cases. If we would want to
178 * do it right in all cases, we would need to know which graphics driver is
179 * used and know the extensions of every driver of the graphics package.
181 char const * const known_dvips_graphics_formats[] = {"eps", "ps", "eps.gz",
182 "ps.gz", "eps.Z", "ps.Z", 0};
185 * Graphics file extensions known by the pdftex driver of the graphics package.
186 * \sa known_dvips_graphics_formats
188 char const * const known_pdftex_graphics_formats[] = {"png", "pdf", "jpg",
192 * Known file extensions for TeX files as used by \\include.
194 char const * const known_tex_extensions[] = {"tex", 0};
196 /// spaces known by InsetSpace
197 char const * const known_spaces[] = { " ", "space", ",", "thinspace", "quad",
198 "qquad", "enspace", "enskip", "negthinspace", 0};
200 /// the same as known_spaces with .lyx names
201 char const * const known_coded_spaces[] = { "space{}", "space{}",
202 "thinspace{}", "thinspace{}", "quad{}", "qquad{}", "enspace{}", "enskip{}",
203 "negthinspace{}", 0};
206 /// splits "x=z, y=b" into a map
207 map<string, string> split_map(string const & s)
209 map<string, string> res;
212 for (size_t i = 0; i < v.size(); ++i) {
213 size_t const pos = v[i].find('=');
214 string const index = v[i].substr(0, pos);
215 string const value = v[i].substr(pos + 1, string::npos);
216 res[trim(index)] = trim(value);
223 * Split a LaTeX length into value and unit.
224 * The latter can be a real unit like "pt", or a latex length variable
225 * like "\textwidth". The unit may contain additional stuff like glue
226 * lengths, but we don't care, because such lengths are ERT anyway.
227 * \returns true if \p value and \p unit are valid.
229 bool splitLatexLength(string const & len, string & value, string & unit)
233 const string::size_type i = len.find_first_not_of(" -+0123456789.,");
234 //'4,5' is a valid LaTeX length number. Change it to '4.5'
235 string const length = subst(len, ',', '.');
236 if (i == string::npos)
239 if (len[0] == '\\') {
240 // We had something like \textwidth without a factor
246 value = trim(string(length, 0, i));
250 // 'cM' is a valid LaTeX length unit. Change it to 'cm'
251 if (contains(len, '\\'))
252 unit = trim(string(len, i));
254 unit = ascii_lowercase(trim(string(len, i)));
259 /// A simple function to translate a latex length to something lyx can
260 /// understand. Not perfect, but rather best-effort.
261 bool translate_len(string const & length, string & valstring, string & unit)
263 if (!splitLatexLength(length, valstring, unit))
265 // LyX uses percent values
267 istringstream iss(valstring);
272 string const percentval = oss.str();
274 if (unit.empty() || unit[0] != '\\')
276 string::size_type const i = unit.find(' ');
277 string const endlen = (i == string::npos) ? string() : string(unit, i);
278 if (unit == "\\textwidth") {
279 valstring = percentval;
280 unit = "text%" + endlen;
281 } else if (unit == "\\columnwidth") {
282 valstring = percentval;
283 unit = "col%" + endlen;
284 } else if (unit == "\\paperwidth") {
285 valstring = percentval;
286 unit = "page%" + endlen;
287 } else if (unit == "\\linewidth") {
288 valstring = percentval;
289 unit = "line%" + endlen;
290 } else if (unit == "\\paperheight") {
291 valstring = percentval;
292 unit = "pheight%" + endlen;
293 } else if (unit == "\\textheight") {
294 valstring = percentval;
295 unit = "theight%" + endlen;
303 string translate_len(string const & length)
307 if (translate_len(length, value, unit))
309 // If the input is invalid, return what we have.
317 * Translates a LaTeX length into \p value, \p unit and
318 * \p special parts suitable for a box inset.
319 * The difference from translate_len() is that a box inset knows about
320 * some special "units" that are stored in \p special.
322 void translate_box_len(string const & length, string & value, string & unit, string & special)
324 if (translate_len(length, value, unit)) {
325 if (unit == "\\height" || unit == "\\depth" ||
326 unit == "\\totalheight" || unit == "\\width") {
327 special = unit.substr(1);
328 // The unit is not used, but LyX requires a dummy setting
341 * Find a file with basename \p name in path \p path and an extension
344 string find_file(string const & name, string const & path,
345 char const * const * extensions)
347 // FIXME UNICODE encoding of name and path may be wrong (makeAbsPath
349 for (char const * const * what = extensions; *what; ++what) {
350 string const trial = addExtension(name, *what);
351 if (makeAbsPath(trial, path).exists())
358 void begin_inset(ostream & os, string const & name)
360 os << "\n\\begin_inset " << name;
364 void end_inset(ostream & os)
366 os << "\n\\end_inset\n\n";
370 void skip_braces(Parser & p)
372 if (p.next_token().cat() != catBegin)
375 if (p.next_token().cat() == catEnd) {
383 void handle_ert(ostream & os, string const & s, Context & context)
385 // We must have a valid layout before outputting the ERT inset.
386 context.check_layout(os);
387 Context newcontext(true, context.textclass);
388 begin_inset(os, "ERT");
389 os << "\nstatus collapsed\n";
390 newcontext.check_layout(os);
391 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
393 os << "\n\\backslash\n";
394 else if (*it == '\n') {
395 newcontext.new_paragraph(os);
396 newcontext.check_layout(os);
400 newcontext.check_end_layout(os);
405 void handle_comment(ostream & os, string const & s, Context & context)
407 // TODO: Handle this better
408 Context newcontext(true, context.textclass);
409 begin_inset(os, "ERT");
410 os << "\nstatus collapsed\n";
411 newcontext.check_layout(os);
412 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
414 os << "\n\\backslash\n";
418 // make sure that our comment is the last thing on the line
419 newcontext.new_paragraph(os);
420 newcontext.check_layout(os);
421 newcontext.check_end_layout(os);
426 Layout const * findLayout(TextClass const & textclass, string const & name)
428 DocumentClass::const_iterator lit = textclass.begin();
429 DocumentClass::const_iterator len = textclass.end();
430 for (; lit != len; ++lit)
431 if (lit->latexname() == name)
437 void eat_whitespace(Parser &, ostream &, Context &, bool);
440 Layout * captionlayout()
442 static Layout * lay = 0;
445 lay->name_ = from_ascii("Caption");
446 lay->latexname_ = "caption";
447 lay->latextype = LATEX_COMMAND;
448 lay->optionalargs = 1;
454 void output_command_layout(ostream & os, Parser & p, bool outer,
455 Context & parent_context,
456 Layout const * newlayout)
458 parent_context.check_end_layout(os);
459 Context context(true, parent_context.textclass, newlayout,
460 parent_context.layout, parent_context.font);
461 if (parent_context.deeper_paragraph) {
462 // We are beginning a nested environment after a
463 // deeper paragraph inside the outer list environment.
464 // Therefore we don't need to output a "begin deeper".
465 context.need_end_deeper = true;
467 context.check_deeper(os);
468 context.check_layout(os);
469 if (context.layout->optionalargs > 0) {
470 eat_whitespace(p, os, context, false);
471 if (p.next_token().character() == '[') {
472 p.get_token(); // eat '['
473 begin_inset(os, "OptArg\n");
474 os << "status collapsed\n\n";
475 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
477 eat_whitespace(p, os, context, false);
480 parse_text(p, os, FLAG_ITEM, outer, context);
481 context.check_end_layout(os);
482 if (parent_context.deeper_paragraph) {
483 // We must suppress the "end deeper" because we
484 // suppressed the "begin deeper" above.
485 context.need_end_deeper = false;
487 context.check_end_deeper(os);
488 // We don't need really a new paragraph, but
489 // we must make sure that the next item gets a \begin_layout.
490 parent_context.new_paragraph(os);
495 * Output a space if necessary.
496 * This function gets called for every whitespace token.
498 * We have three cases here:
499 * 1. A space must be suppressed. Example: The lyxcode case below
500 * 2. A space may be suppressed. Example: Spaces before "\par"
501 * 3. A space must not be suppressed. Example: A space between two words
503 * We currently handle only 1. and 3 and from 2. only the case of
504 * spaces before newlines as a side effect.
506 * 2. could be used to suppress as many spaces as possible. This has two effects:
507 * - Reimporting LyX generated LaTeX files changes almost no whitespace
508 * - Superflous whitespace from non LyX generated LaTeX files is removed.
509 * The drawback is that the logic inside the function becomes
510 * complicated, and that is the reason why it is not implemented.
512 void check_space(Parser const & p, ostream & os, Context & context)
514 Token const next = p.next_token();
515 Token const curr = p.curr_token();
516 // A space before a single newline and vice versa must be ignored
517 // LyX emits a newline before \end{lyxcode}.
518 // This newline must be ignored,
519 // otherwise LyX will add an additional protected space.
520 if (next.cat() == catSpace ||
521 next.cat() == catNewline ||
522 (next.cs() == "end" && context.layout->free_spacing && curr.cat() == catNewline)) {
525 context.check_layout(os);
531 * Parse all arguments of \p command
533 void parse_arguments(string const & command,
534 vector<ArgumentType> const & template_arguments,
535 Parser & p, ostream & os, bool outer, Context & context)
537 string ert = command;
538 size_t no_arguments = template_arguments.size();
539 for (size_t i = 0; i < no_arguments; ++i) {
540 switch (template_arguments[i]) {
542 // This argument contains regular LaTeX
543 handle_ert(os, ert + '{', context);
544 eat_whitespace(p, os, context, false);
545 parse_text(p, os, FLAG_ITEM, outer, context);
549 // This argument may contain special characters
550 ert += '{' + p.verbatim_item() + '}';
557 handle_ert(os, ert, context);
562 * Check whether \p command is a known command. If yes,
563 * handle the command with all arguments.
564 * \return true if the command was parsed, false otherwise.
566 bool parse_command(string const & command, Parser & p, ostream & os,
567 bool outer, Context & context)
569 if (known_commands.find(command) != known_commands.end()) {
570 parse_arguments(command, known_commands[command], p, os,
578 /// Parses a minipage or parbox
579 void parse_box(Parser & p, ostream & os, unsigned flags, bool outer,
580 Context & parent_context, bool use_parbox)
584 // We need to set the height to the LaTeX default of 1\\totalheight
585 // for the case when no height argument is given
586 string height_value = "1";
587 string height_unit = "in";
588 string height_special = "totalheight";
590 if (p.next_token().asInput() == "[") {
591 position = p.getArg('[', ']');
592 if (position != "t" && position != "c" && position != "b") {
594 cerr << "invalid position for minipage/parbox" << endl;
596 if (p.next_token().asInput() == "[") {
597 latex_height = p.getArg('[', ']');
598 translate_box_len(latex_height, height_value, height_unit, height_special);
600 if (p.next_token().asInput() == "[") {
601 inner_pos = p.getArg('[', ']');
602 if (inner_pos != "c" && inner_pos != "t" &&
603 inner_pos != "b" && inner_pos != "s") {
604 inner_pos = position;
605 cerr << "invalid inner_pos for minipage/parbox"
613 string const latex_width = p.verbatim_item();
614 translate_len(latex_width, width_value, width_unit);
615 if (contains(width_unit, '\\') || contains(height_unit, '\\')) {
616 // LyX can't handle length variables
621 ss << "\\begin{minipage}";
622 if (!position.empty())
623 ss << '[' << position << ']';
624 if (!latex_height.empty())
625 ss << '[' << latex_height << ']';
626 if (!inner_pos.empty())
627 ss << '[' << inner_pos << ']';
628 ss << "{" << latex_width << "}";
631 handle_ert(os, ss.str(), parent_context);
632 parent_context.new_paragraph(os);
633 parse_text_in_inset(p, os, flags, outer, parent_context);
635 handle_ert(os, "}", parent_context);
637 handle_ert(os, "\\end{minipage}", parent_context);
639 // LyX does not like empty positions, so we have
640 // to set them to the LaTeX default values here.
641 if (position.empty())
643 if (inner_pos.empty())
644 inner_pos = position;
645 parent_context.check_layout(os);
646 begin_inset(os, "Box Frameless\n");
647 os << "position \"" << position << "\"\n";
648 os << "hor_pos \"c\"\n";
649 os << "has_inner_box 1\n";
650 os << "inner_pos \"" << inner_pos << "\"\n";
651 os << "use_parbox " << use_parbox << "\n";
652 os << "width \"" << width_value << width_unit << "\"\n";
653 os << "special \"none\"\n";
654 os << "height \"" << height_value << height_unit << "\"\n";
655 os << "height_special \"" << height_special << "\"\n";
656 os << "status open\n\n";
657 parse_text_in_inset(p, os, flags, outer, parent_context);
659 #ifdef PRESERVE_LAYOUT
660 // lyx puts a % after the end of the minipage
661 if (p.next_token().cat() == catNewline && p.next_token().cs().size() > 1) {
663 //handle_comment(os, "%dummy", parent_context);
666 parent_context.new_paragraph(os);
668 else if (p.next_token().cat() == catSpace || p.next_token().cat() == catNewline) {
669 //handle_comment(os, "%dummy", parent_context);
672 // We add a protected space if something real follows
673 if (p.good() && p.next_token().cat() != catComment) {
674 os << "\\InsetSpace ~\n";
682 /// parse an unknown environment
683 void parse_unknown_environment(Parser & p, string const & name, ostream & os,
684 unsigned flags, bool outer,
685 Context & parent_context)
687 if (name == "tabbing")
688 // We need to remember that we have to handle '\=' specially
689 flags |= FLAG_TABBING;
691 // We need to translate font changes and paragraphs inside the
692 // environment to ERT if we have a non standard font.
693 // Otherwise things like
694 // \large\begin{foo}\huge bar\end{foo}
696 bool const specialfont =
697 (parent_context.font != parent_context.normalfont);
698 bool const new_layout_allowed = parent_context.new_layout_allowed;
700 parent_context.new_layout_allowed = false;
701 handle_ert(os, "\\begin{" + name + "}", parent_context);
702 parse_text_snippet(p, os, flags, outer, parent_context);
703 handle_ert(os, "\\end{" + name + "}", parent_context);
705 parent_context.new_layout_allowed = new_layout_allowed;
709 void parse_environment(Parser & p, ostream & os, bool outer,
710 Context & parent_context)
712 Layout const * newlayout;
713 string const name = p.getArg('{', '}');
714 const bool is_starred = suffixIs(name, '*');
715 string const unstarred_name = rtrim(name, "*");
716 active_environments.push_back(name);
718 if (is_math_env(name)) {
719 parent_context.check_layout(os);
720 begin_inset(os, "Formula ");
721 os << "\\begin{" << name << "}";
722 parse_math(p, os, FLAG_END, MATH_MODE);
723 os << "\\end{" << name << "}";
727 else if (name == "tabular" || name == "longtable") {
728 eat_whitespace(p, os, parent_context, false);
729 parent_context.check_layout(os);
730 begin_inset(os, "Tabular ");
731 handle_tabular(p, os, name == "longtable", parent_context);
736 else if (parent_context.textclass.floats().typeExist(unstarred_name)) {
737 eat_whitespace(p, os, parent_context, false);
738 parent_context.check_layout(os);
739 begin_inset(os, "Float " + unstarred_name + "\n");
740 if (p.next_token().asInput() == "[") {
741 os << "placement " << p.getArg('[', ']') << '\n';
743 os << "wide " << convert<string>(is_starred)
744 << "\nsideways false"
745 << "\nstatus open\n\n";
746 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
748 // We don't need really a new paragraph, but
749 // we must make sure that the next item gets a \begin_layout.
750 parent_context.new_paragraph(os);
754 else if (name == "minipage") {
755 eat_whitespace(p, os, parent_context, false);
756 parse_box(p, os, FLAG_END, outer, parent_context, false);
760 else if (name == "comment") {
761 eat_whitespace(p, os, parent_context, false);
762 parent_context.check_layout(os);
763 begin_inset(os, "Note Comment\n");
764 os << "status open\n";
765 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
768 skip_braces(p); // eat {} that might by set by LyX behind comments
771 else if (name == "lyxgreyedout") {
772 eat_whitespace(p, os, parent_context, false);
773 parent_context.check_layout(os);
774 begin_inset(os, "Note Greyedout\n");
775 os << "status open\n";
776 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
781 else if (name == "framed") {
782 eat_whitespace(p, os, parent_context, false);
783 parent_context.check_layout(os);
784 begin_inset(os, "Note Framed\n");
785 os << "status open\n";
786 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
791 else if (name == "shaded") {
792 eat_whitespace(p, os, parent_context, false);
793 parent_context.check_layout(os);
794 begin_inset(os, "Note Shaded\n");
795 os << "status open\n";
796 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
801 else if (!parent_context.new_layout_allowed)
802 parse_unknown_environment(p, name, os, FLAG_END, outer,
805 // Alignment and spacing settings
806 // FIXME (bug xxxx): These settings can span multiple paragraphs and
807 // therefore are totally broken!
808 // Note that \centering, raggedright, and raggedleft cannot be handled, as
809 // they are commands not environments. They are furthermore switches that
810 // can be ended by another switches, but also by commands like \footnote or
811 // \parbox. So the only safe way is to leave them untouched.
812 else if (name == "center" || name == "flushleft" || name == "flushright") {
813 eat_whitespace(p, os, parent_context, false);
814 // We must begin a new paragraph if not already done
815 if (! parent_context.atParagraphStart()) {
816 parent_context.check_end_layout(os);
817 parent_context.new_paragraph(os);
819 if (name == "flushleft")
820 parent_context.add_extra_stuff("\\align left\n");
821 else if (name == "flushright")
822 parent_context.add_extra_stuff("\\align right\n");
823 else if (name == "center")
824 parent_context.add_extra_stuff("\\align center\n");
825 parse_text(p, os, FLAG_END, outer, parent_context);
826 // Just in case the environment is empty
827 parent_context.extra_stuff.erase();
828 // We must begin a new paragraph to reset the alignment
829 parent_context.new_paragraph(os);
833 // The single '=' is meant here.
834 else if ((newlayout = findLayout(parent_context.textclass, name)) &&
835 newlayout->isEnvironment()) {
836 eat_whitespace(p, os, parent_context, false);
837 Context context(true, parent_context.textclass, newlayout,
838 parent_context.layout, parent_context.font);
839 if (parent_context.deeper_paragraph) {
840 // We are beginning a nested environment after a
841 // deeper paragraph inside the outer list environment.
842 // Therefore we don't need to output a "begin deeper".
843 context.need_end_deeper = true;
845 parent_context.check_end_layout(os);
846 switch (context.layout->latextype) {
847 case LATEX_LIST_ENVIRONMENT:
848 context.add_par_extra_stuff("\\labelwidthstring "
849 + p.verbatim_item() + '\n');
852 case LATEX_BIB_ENVIRONMENT:
853 p.verbatim_item(); // swallow next arg
859 context.check_deeper(os);
860 parse_text(p, os, FLAG_END, outer, context);
861 context.check_end_layout(os);
862 if (parent_context.deeper_paragraph) {
863 // We must suppress the "end deeper" because we
864 // suppressed the "begin deeper" above.
865 context.need_end_deeper = false;
867 context.check_end_deeper(os);
868 parent_context.new_paragraph(os);
872 else if (name == "appendix") {
873 // This is no good latex style, but it works and is used in some documents...
874 eat_whitespace(p, os, parent_context, false);
875 parent_context.check_end_layout(os);
876 Context context(true, parent_context.textclass, parent_context.layout,
877 parent_context.layout, parent_context.font);
878 context.check_layout(os);
879 os << "\\start_of_appendix\n";
880 parse_text(p, os, FLAG_END, outer, context);
881 context.check_end_layout(os);
885 else if (known_environments.find(name) != known_environments.end()) {
886 vector<ArgumentType> arguments = known_environments[name];
887 // The last "argument" denotes wether we may translate the
888 // environment contents to LyX
889 // The default required if no argument is given makes us
890 // compatible with the reLyXre environment.
891 ArgumentType contents = arguments.empty() ?
894 if (!arguments.empty())
895 arguments.pop_back();
896 // See comment in parse_unknown_environment()
897 bool const specialfont =
898 (parent_context.font != parent_context.normalfont);
899 bool const new_layout_allowed =
900 parent_context.new_layout_allowed;
902 parent_context.new_layout_allowed = false;
903 parse_arguments("\\begin{" + name + "}", arguments, p, os,
904 outer, parent_context);
905 if (contents == verbatim)
906 handle_ert(os, p.verbatimEnvironment(name),
909 parse_text_snippet(p, os, FLAG_END, outer,
911 handle_ert(os, "\\end{" + name + "}", parent_context);
913 parent_context.new_layout_allowed = new_layout_allowed;
917 parse_unknown_environment(p, name, os, FLAG_END, outer,
920 active_environments.pop_back();
924 /// parses a comment and outputs it to \p os.
925 void parse_comment(Parser & p, ostream & os, Token const & t, Context & context)
927 LASSERT(t.cat() == catComment, return);
928 if (!t.cs().empty()) {
929 context.check_layout(os);
930 handle_comment(os, '%' + t.cs(), context);
931 if (p.next_token().cat() == catNewline) {
932 // A newline after a comment line starts a new
934 if (context.new_layout_allowed) {
935 if(!context.atParagraphStart())
936 // Only start a new paragraph if not already
937 // done (we might get called recursively)
938 context.new_paragraph(os);
940 handle_ert(os, "\n", context);
941 eat_whitespace(p, os, context, true);
951 * Reads spaces and comments until the first non-space, non-comment token.
952 * New paragraphs (double newlines or \\par) are handled like simple spaces
953 * if \p eatParagraph is true.
954 * Spaces are skipped, but comments are written to \p os.
956 void eat_whitespace(Parser & p, ostream & os, Context & context,
960 Token const & t = p.get_token();
961 if (t.cat() == catComment)
962 parse_comment(p, os, t, context);
963 else if ((! eatParagraph && p.isParagraph()) ||
964 (t.cat() != catSpace && t.cat() != catNewline)) {
973 * Set a font attribute, parse text and reset the font attribute.
974 * \param attribute Attribute name (e.g. \\family, \\shape etc.)
975 * \param currentvalue Current value of the attribute. Is set to the new
976 * value during parsing.
977 * \param newvalue New value of the attribute
979 void parse_text_attributes(Parser & p, ostream & os, unsigned flags, bool outer,
980 Context & context, string const & attribute,
981 string & currentvalue, string const & newvalue)
983 context.check_layout(os);
984 string const oldvalue = currentvalue;
985 currentvalue = newvalue;
986 os << '\n' << attribute << ' ' << newvalue << "\n";
987 parse_text_snippet(p, os, flags, outer, context);
988 context.check_layout(os);
989 os << '\n' << attribute << ' ' << oldvalue << "\n";
990 currentvalue = oldvalue;
994 /// get the arguments of a natbib or jurabib citation command
995 void get_cite_arguments(Parser & p, bool natbibOrder,
996 string & before, string & after)
998 // We need to distinguish "" and "[]", so we can't use p.getOpt().
1000 // text before the citation
1002 // text after the citation
1003 after = p.getFullOpt();
1005 if (!after.empty()) {
1006 before = p.getFullOpt();
1007 if (natbibOrder && !before.empty())
1008 swap(before, after);
1013 /// Convert filenames with TeX macros and/or quotes to something LyX
1015 string const normalize_filename(string const & name)
1017 Parser p(trim(name, "\""));
1020 Token const & t = p.get_token();
1021 if (t.cat() != catEscape)
1023 else if (t.cs() == "lyxdot") {
1024 // This is used by LyX for simple dots in relative
1028 } else if (t.cs() == "space") {
1038 /// Convert \p name from TeX convention (relative to master file) to LyX
1039 /// convention (relative to .lyx file) if it is relative
1040 void fix_relative_filename(string & name)
1042 FileName fname(name);
1043 if (fname.isAbsolute())
1046 // FIXME UNICODE encoding of name may be wrong (makeAbsPath expects
1048 name = to_utf8(makeRelPath(from_utf8(makeAbsPath(name, getMasterFilePath()).absFilename()),
1049 from_utf8(getParentFilePath())));
1053 /// Parse a NoWeb Scrap section. The initial "<<" is already parsed.
1054 void parse_noweb(Parser & p, ostream & os, Context & context)
1056 // assemble the rest of the keyword
1060 Token const & t = p.get_token();
1061 if (t.asInput() == ">" && p.next_token().asInput() == ">") {
1064 scrap = (p.good() && p.next_token().asInput() == "=");
1066 name += p.get_token().asInput();
1069 name += t.asInput();
1072 if (!scrap || !context.new_layout_allowed ||
1073 !context.textclass.hasLayout(from_ascii("Scrap"))) {
1074 cerr << "Warning: Could not interpret '" << name
1075 << "'. Ignoring it." << endl;
1079 // We use new_paragraph instead of check_end_layout because the stuff
1080 // following the noweb chunk needs to start with a \begin_layout.
1081 // This may create a new paragraph even if there was none in the
1082 // noweb file, but the alternative is an invalid LyX file. Since
1083 // noweb code chunks are implemented with a layout style in LyX they
1084 // always must be in an own paragraph.
1085 context.new_paragraph(os);
1086 Context newcontext(true, context.textclass,
1087 &context.textclass[from_ascii("Scrap")]);
1088 newcontext.check_layout(os);
1091 Token const & t = p.get_token();
1092 // We abuse the parser a bit, because this is no TeX syntax
1094 if (t.cat() == catEscape)
1095 os << subst(t.asInput(), "\\", "\n\\backslash\n");
1097 os << subst(t.asInput(), "\n", "\n\\newline\n");
1098 // The scrap chunk is ended by an @ at the beginning of a line.
1099 // After the @ the line may contain a comment and/or
1100 // whitespace, but nothing else.
1101 if (t.asInput() == "@" && p.prev_token().cat() == catNewline &&
1102 (p.next_token().cat() == catSpace ||
1103 p.next_token().cat() == catNewline ||
1104 p.next_token().cat() == catComment)) {
1105 while (p.good() && p.next_token().cat() == catSpace)
1106 os << p.get_token().asInput();
1107 if (p.next_token().cat() == catComment)
1108 // The comment includes a final '\n'
1109 os << p.get_token().asInput();
1111 if (p.next_token().cat() == catNewline)
1118 newcontext.check_end_layout(os);
1121 } // anonymous namespace
1124 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
1127 Layout const * newlayout = 0;
1128 // store the current selectlanguage to be used after \foreignlanguage
1130 // Store the latest bibliographystyle (needed for bibtex inset)
1131 string bibliographystyle;
1132 bool const use_natbib = used_packages.find("natbib") != used_packages.end();
1133 bool const use_jurabib = used_packages.find("jurabib") != used_packages.end();
1135 Token const & t = p.get_token();
1138 cerr << "t: " << t << " flags: " << flags << "\n";
1141 if (flags & FLAG_ITEM) {
1142 if (t.cat() == catSpace)
1145 flags &= ~FLAG_ITEM;
1146 if (t.cat() == catBegin) {
1147 // skip the brace and collect everything to the next matching
1149 flags |= FLAG_BRACE_LAST;
1153 // handle only this single token, leave the loop if done
1154 flags |= FLAG_LEAVE;
1157 if (t.character() == ']' && (flags & FLAG_BRACK_LAST))
1163 if (t.cat() == catMath) {
1164 // we are inside some text mode thingy, so opening new math is allowed
1165 context.check_layout(os);
1166 begin_inset(os, "Formula ");
1167 Token const & n = p.get_token();
1168 if (n.cat() == catMath && outer) {
1169 // TeX's $$...$$ syntax for displayed math
1171 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
1173 p.get_token(); // skip the second '$' token
1175 // simple $...$ stuff
1178 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
1184 else if (t.cat() == catSuper || t.cat() == catSub)
1185 cerr << "catcode " << t << " illegal in text mode\n";
1187 // Basic support for english quotes. This should be
1188 // extended to other quotes, but is not so easy (a
1189 // left english quote is the same as a right german
1191 else if (t.asInput() == "`" && p.next_token().asInput() == "`") {
1192 context.check_layout(os);
1193 begin_inset(os, "Quotes ");
1199 else if (t.asInput() == "'" && p.next_token().asInput() == "'") {
1200 context.check_layout(os);
1201 begin_inset(os, "Quotes ");
1208 else if (t.asInput() == ">" && p.next_token().asInput() == ">") {
1209 context.check_layout(os);
1210 begin_inset(os, "Quotes ");
1217 else if (t.asInput() == "<" && p.next_token().asInput() == "<") {
1218 context.check_layout(os);
1219 begin_inset(os, "Quotes ");
1226 else if (t.asInput() == "<"
1227 && p.next_token().asInput() == "<" && noweb_mode) {
1229 parse_noweb(p, os, context);
1232 else if (t.cat() == catSpace || (t.cat() == catNewline && ! p.isParagraph()))
1233 check_space(p, os, context);
1235 else if (t.character() == '[' && noweb_mode &&
1236 p.next_token().character() == '[') {
1237 // These can contain underscores
1239 string const s = p.getFullOpt() + ']';
1240 if (p.next_token().character() == ']')
1243 cerr << "Warning: Inserting missing ']' in '"
1244 << s << "'." << endl;
1245 handle_ert(os, s, context);
1248 else if (t.cat() == catLetter ||
1249 t.cat() == catOther ||
1250 t.cat() == catAlign ||
1251 t.cat() == catParameter) {
1252 // This translates "&" to "\\&" which may be wrong...
1253 context.check_layout(os);
1254 os << t.character();
1257 else if (p.isParagraph()) {
1258 if (context.new_layout_allowed)
1259 context.new_paragraph(os);
1261 handle_ert(os, "\\par ", context);
1262 eat_whitespace(p, os, context, true);
1265 else if (t.cat() == catActive) {
1266 context.check_layout(os);
1267 if (t.character() == '~') {
1268 if (context.layout->free_spacing)
1271 os << "\\InsetSpace ~\n";
1273 os << t.character();
1276 else if (t.cat() == catBegin &&
1277 p.next_token().cat() == catEnd) {
1279 Token const prev = p.prev_token();
1281 if (p.next_token().character() == '`' ||
1282 (prev.character() == '-' &&
1283 p.next_token().character() == '-'))
1284 ; // ignore it in {}`` or -{}-
1286 handle_ert(os, "{}", context);
1290 else if (t.cat() == catBegin) {
1291 context.check_layout(os);
1292 // special handling of font attribute changes
1293 Token const prev = p.prev_token();
1294 Token const next = p.next_token();
1295 TeXFont const oldFont = context.font;
1296 if (next.character() == '[' ||
1297 next.character() == ']' ||
1298 next.character() == '*') {
1300 if (p.next_token().cat() == catEnd) {
1301 os << next.character();
1305 handle_ert(os, "{", context);
1306 parse_text_snippet(p, os,
1309 handle_ert(os, "}", context);
1311 } else if (! context.new_layout_allowed) {
1312 handle_ert(os, "{", context);
1313 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1315 handle_ert(os, "}", context);
1316 } else if (is_known(next.cs(), known_sizes)) {
1317 // next will change the size, so we must
1319 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1321 if (!context.atParagraphStart())
1323 << context.font.size << "\n";
1324 } else if (is_known(next.cs(), known_font_families)) {
1325 // next will change the font family, so we
1326 // must reset it here
1327 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1329 if (!context.atParagraphStart())
1331 << context.font.family << "\n";
1332 } else if (is_known(next.cs(), known_font_series)) {
1333 // next will change the font series, so we
1334 // must reset it here
1335 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1337 if (!context.atParagraphStart())
1339 << context.font.series << "\n";
1340 } else if (is_known(next.cs(), known_font_shapes)) {
1341 // next will change the font shape, so we
1342 // must reset it here
1343 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1345 if (!context.atParagraphStart())
1347 << context.font.shape << "\n";
1348 } else if (is_known(next.cs(), known_old_font_families) ||
1349 is_known(next.cs(), known_old_font_series) ||
1350 is_known(next.cs(), known_old_font_shapes)) {
1351 // next will change the font family, series
1352 // and shape, so we must reset it here
1353 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1355 if (!context.atParagraphStart())
1357 << context.font.family
1359 << context.font.series
1361 << context.font.shape << "\n";
1363 handle_ert(os, "{", context);
1364 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1366 handle_ert(os, "}", context);
1370 else if (t.cat() == catEnd) {
1371 if (flags & FLAG_BRACE_LAST) {
1374 cerr << "stray '}' in text\n";
1375 handle_ert(os, "}", context);
1378 else if (t.cat() == catComment)
1379 parse_comment(p, os, t, context);
1382 // control sequences
1385 else if (t.cs() == "(") {
1386 context.check_layout(os);
1387 begin_inset(os, "Formula");
1389 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
1394 else if (t.cs() == "[") {
1395 context.check_layout(os);
1396 begin_inset(os, "Formula");
1398 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
1403 else if (t.cs() == "begin")
1404 parse_environment(p, os, outer, context);
1406 else if (t.cs() == "end") {
1407 if (flags & FLAG_END) {
1408 // eat environment name
1409 string const name = p.getArg('{', '}');
1410 if (name != active_environment())
1411 cerr << "\\end{" + name + "} does not match \\begin{"
1412 + active_environment() + "}\n";
1415 p.error("found 'end' unexpectedly");
1418 else if (t.cs() == "item") {
1421 bool optarg = false;
1422 if (p.next_token().character() == '[') {
1423 p.get_token(); // eat '['
1424 s = parse_text_snippet(p, FLAG_BRACK_LAST,
1429 context.check_layout(os);
1430 if (context.has_item) {
1431 // An item in an unknown list-like environment
1432 // FIXME: Do this in check_layout()!
1433 context.has_item = false;
1435 handle_ert(os, "\\item", context);
1437 handle_ert(os, "\\item ", context);
1440 if (context.layout->labeltype != LABEL_MANUAL) {
1441 // lyx does not support \item[\mybullet]
1442 // in itemize environments
1443 handle_ert(os, "[", context);
1445 handle_ert(os, "]", context);
1446 } else if (!s.empty()) {
1447 // The space is needed to separate the
1448 // item from the rest of the sentence.
1450 eat_whitespace(p, os, context, false);
1455 else if (t.cs() == "bibitem") {
1457 context.check_layout(os);
1460 os << '{' << p.verbatim_item() << '}' << "\n";
1463 else if (t.cs() == "def") {
1464 context.check_layout(os);
1465 eat_whitespace(p, os, context, false);
1466 string name = p.get_token().cs();
1467 eat_whitespace(p, os, context, false);
1473 while (p.next_token().cat() != catBegin) {
1474 if (p.next_token().cat() == catParameter) {
1479 // followed by number?
1480 if (p.next_token().cat() == catOther) {
1481 char c = p.getChar();
1483 // number = current arity + 1?
1484 if (c == arity + '0' + 1)
1489 paramtext += p.get_token().asString();
1491 paramtext += p.get_token().asString();
1496 // only output simple (i.e. compatible) macro as FormulaMacros
1497 string ert = "\\def\\" + name + ' ' + paramtext + '{' + p.verbatim_item() + '}';
1499 context.check_layout(os);
1500 begin_inset(os, "FormulaMacro");
1504 handle_ert(os, ert, context);
1507 else if (t.cs() == "noindent") {
1509 context.add_par_extra_stuff("\\noindent\n");
1512 else if (t.cs() == "appendix") {
1513 context.add_par_extra_stuff("\\start_of_appendix\n");
1514 // We need to start a new paragraph. Otherwise the
1515 // appendix in 'bla\appendix\chapter{' would start
1517 context.new_paragraph(os);
1518 // We need to make sure that the paragraph is
1519 // generated even if it is empty. Otherwise the
1520 // appendix in '\par\appendix\par\chapter{' would
1522 context.check_layout(os);
1523 // FIXME: This is a hack to prevent paragraph
1524 // deletion if it is empty. Handle this better!
1526 "%dummy comment inserted by tex2lyx to "
1527 "ensure that this paragraph is not empty",
1529 // Both measures above may generate an additional
1530 // empty paragraph, but that does not hurt, because
1531 // whitespace does not matter here.
1532 eat_whitespace(p, os, context, true);
1535 // Starred section headings
1536 // Must attempt to parse "Section*" before "Section".
1537 else if ((p.next_token().asInput() == "*") &&
1538 context.new_layout_allowed &&
1539 (newlayout = findLayout(context.textclass, t.cs() + '*')) &&
1540 newlayout->isCommand()) {
1541 TeXFont const oldFont = context.font;
1542 // save the current font size
1543 string const size = oldFont.size;
1544 // reset the font size to default, because the font size switches don't
1545 // affect section headings and the like
1546 context.font.size = known_coded_sizes[0];
1547 output_font_change(os, oldFont, context.font);
1550 output_command_layout(os, p, outer, context, newlayout);
1551 // set the font size to the original value
1552 context.font.size = size;
1553 output_font_change(os, oldFont, context.font);
1557 // Section headings and the like
1558 else if (context.new_layout_allowed &&
1559 (newlayout = findLayout(context.textclass, t.cs())) &&
1560 newlayout->isCommand()) {
1561 TeXFont const oldFont = context.font;
1562 // save the current font size
1563 string const size = oldFont.size;
1564 // reset the font size to default, because the font size switches don't
1565 // affect section headings and the like
1566 context.font.size = known_coded_sizes[0];
1567 output_font_change(os, oldFont, context.font);
1569 output_command_layout(os, p, outer, context, newlayout);
1570 // set the font size to the original value
1571 context.font.size = size;
1572 output_font_change(os, oldFont, context.font);
1576 // Special handling for \caption
1577 // FIXME: remove this when InsetCaption is supported.
1578 else if (context.new_layout_allowed &&
1579 t.cs() == captionlayout()->latexname()) {
1580 output_command_layout(os, p, outer, context,
1585 else if (t.cs() == "includegraphics") {
1586 bool const clip = p.next_token().asInput() == "*";
1589 map<string, string> opts = split_map(p.getArg('[', ']'));
1591 opts["clip"] = string();
1592 string name = normalize_filename(p.verbatim_item());
1594 string const path = getMasterFilePath();
1595 // We want to preserve relative / absolute filenames,
1596 // therefore path is only used for testing
1597 // FIXME UNICODE encoding of name and path may be
1598 // wrong (makeAbsPath expects utf8)
1599 if (!makeAbsPath(name, path).exists()) {
1600 // The file extension is probably missing.
1601 // Now try to find it out.
1602 string const dvips_name =
1603 find_file(name, path,
1604 known_dvips_graphics_formats);
1605 string const pdftex_name =
1606 find_file(name, path,
1607 known_pdftex_graphics_formats);
1608 if (!dvips_name.empty()) {
1609 if (!pdftex_name.empty()) {
1610 cerr << "This file contains the "
1612 "\"\\includegraphics{"
1614 "However, files\n\""
1615 << dvips_name << "\" and\n\""
1616 << pdftex_name << "\"\n"
1617 "both exist, so I had to make a "
1618 "choice and took the first one.\n"
1619 "Please move the unwanted one "
1620 "someplace else and try again\n"
1621 "if my choice was wrong."
1625 } else if (!pdftex_name.empty())
1629 // FIXME UNICODE encoding of name and path may be
1630 // wrong (makeAbsPath expects utf8)
1631 if (makeAbsPath(name, path).exists())
1632 fix_relative_filename(name);
1634 cerr << "Warning: Could not find graphics file '"
1635 << name << "'." << endl;
1637 context.check_layout(os);
1638 begin_inset(os, "Graphics ");
1639 os << "\n\tfilename " << name << '\n';
1640 if (opts.find("width") != opts.end())
1642 << translate_len(opts["width"]) << '\n';
1643 if (opts.find("height") != opts.end())
1645 << translate_len(opts["height"]) << '\n';
1646 if (opts.find("scale") != opts.end()) {
1647 istringstream iss(opts["scale"]);
1651 os << "\tscale " << val << '\n';
1653 if (opts.find("angle") != opts.end())
1654 os << "\trotateAngle "
1655 << opts["angle"] << '\n';
1656 if (opts.find("origin") != opts.end()) {
1658 string const opt = opts["origin"];
1659 if (opt.find('l') != string::npos) ss << "left";
1660 if (opt.find('r') != string::npos) ss << "right";
1661 if (opt.find('c') != string::npos) ss << "center";
1662 if (opt.find('t') != string::npos) ss << "Top";
1663 if (opt.find('b') != string::npos) ss << "Bottom";
1664 if (opt.find('B') != string::npos) ss << "Baseline";
1665 if (!ss.str().empty())
1666 os << "\trotateOrigin " << ss.str() << '\n';
1668 cerr << "Warning: Ignoring unknown includegraphics origin argument '" << opt << "'\n";
1670 if (opts.find("keepaspectratio") != opts.end())
1671 os << "\tkeepAspectRatio\n";
1672 if (opts.find("clip") != opts.end())
1674 if (opts.find("draft") != opts.end())
1676 if (opts.find("bb") != opts.end())
1677 os << "\tBoundingBox "
1678 << opts["bb"] << '\n';
1679 int numberOfbbOptions = 0;
1680 if (opts.find("bbllx") != opts.end())
1681 numberOfbbOptions++;
1682 if (opts.find("bblly") != opts.end())
1683 numberOfbbOptions++;
1684 if (opts.find("bburx") != opts.end())
1685 numberOfbbOptions++;
1686 if (opts.find("bbury") != opts.end())
1687 numberOfbbOptions++;
1688 if (numberOfbbOptions == 4)
1689 os << "\tBoundingBox "
1690 << opts["bbllx"] << " " << opts["bblly"] << " "
1691 << opts["bburx"] << " " << opts["bbury"] << '\n';
1692 else if (numberOfbbOptions > 0)
1693 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
1694 numberOfbbOptions = 0;
1695 if (opts.find("natwidth") != opts.end())
1696 numberOfbbOptions++;
1697 if (opts.find("natheight") != opts.end())
1698 numberOfbbOptions++;
1699 if (numberOfbbOptions == 2)
1700 os << "\tBoundingBox 0bp 0bp "
1701 << opts["natwidth"] << " " << opts["natheight"] << '\n';
1702 else if (numberOfbbOptions > 0)
1703 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
1704 ostringstream special;
1705 if (opts.find("hiresbb") != opts.end())
1706 special << "hiresbb,";
1707 if (opts.find("trim") != opts.end())
1709 if (opts.find("viewport") != opts.end())
1710 special << "viewport=" << opts["viewport"] << ',';
1711 if (opts.find("totalheight") != opts.end())
1712 special << "totalheight=" << opts["totalheight"] << ',';
1713 if (opts.find("type") != opts.end())
1714 special << "type=" << opts["type"] << ',';
1715 if (opts.find("ext") != opts.end())
1716 special << "ext=" << opts["ext"] << ',';
1717 if (opts.find("read") != opts.end())
1718 special << "read=" << opts["read"] << ',';
1719 if (opts.find("command") != opts.end())
1720 special << "command=" << opts["command"] << ',';
1721 string s_special = special.str();
1722 if (!s_special.empty()) {
1723 // We had special arguments. Remove the trailing ','.
1724 os << "\tspecial " << s_special.substr(0, s_special.size() - 1) << '\n';
1726 // TODO: Handle the unknown settings better.
1727 // Warn about invalid options.
1728 // Check whether some option was given twice.
1732 else if (t.cs() == "footnote" ||
1733 (t.cs() == "thanks" && context.layout->intitle)) {
1735 context.check_layout(os);
1736 begin_inset(os, "Foot\n");
1737 os << "status collapsed\n\n";
1738 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
1742 else if (t.cs() == "marginpar") {
1744 context.check_layout(os);
1745 begin_inset(os, "Marginal\n");
1746 os << "status collapsed\n\n";
1747 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
1751 else if (t.cs() == "ensuremath") {
1753 context.check_layout(os);
1754 string const s = p.verbatim_item();
1755 if (s == "\xb1" || s == "\xb3" || s == "\xb2" || s == "\xb5")
1758 handle_ert(os, "\\ensuremath{" + s + "}",
1762 else if (t.cs() == "hfill") {
1763 context.check_layout(os);
1764 os << "\n\\hfill\n";
1769 else if (t.cs() == "makeindex" || t.cs() == "maketitle") {
1770 // FIXME: Somehow prevent title layouts if
1771 // "maketitle" was not found
1773 skip_braces(p); // swallow this
1776 else if (t.cs() == "tableofcontents") {
1778 context.check_layout(os);
1779 begin_inset(os, "LatexCommand \\tableofcontents\n");
1781 skip_braces(p); // swallow this
1784 else if (t.cs() == "listoffigures") {
1786 context.check_layout(os);
1787 begin_inset(os, "FloatList figure\n");
1789 skip_braces(p); // swallow this
1792 else if (t.cs() == "listoftables") {
1794 context.check_layout(os);
1795 begin_inset(os, "FloatList table\n");
1797 skip_braces(p); // swallow this
1800 else if (t.cs() == "listof") {
1801 p.skip_spaces(true);
1802 string const name = p.get_token().asString();
1803 if (context.textclass.floats().typeExist(name)) {
1804 context.check_layout(os);
1805 begin_inset(os, "FloatList ");
1808 p.get_token(); // swallow second arg
1810 handle_ert(os, "\\listof{" + name + "}", context);
1813 else if (t.cs() == "textrm")
1814 parse_text_attributes(p, os, FLAG_ITEM, outer,
1815 context, "\\family",
1816 context.font.family, "roman");
1818 else if (t.cs() == "textsf")
1819 parse_text_attributes(p, os, FLAG_ITEM, outer,
1820 context, "\\family",
1821 context.font.family, "sans");
1823 else if (t.cs() == "texttt")
1824 parse_text_attributes(p, os, FLAG_ITEM, outer,
1825 context, "\\family",
1826 context.font.family, "typewriter");
1828 else if (t.cs() == "textmd")
1829 parse_text_attributes(p, os, FLAG_ITEM, outer,
1830 context, "\\series",
1831 context.font.series, "medium");
1833 else if (t.cs() == "textbf")
1834 parse_text_attributes(p, os, FLAG_ITEM, outer,
1835 context, "\\series",
1836 context.font.series, "bold");
1838 else if (t.cs() == "textup")
1839 parse_text_attributes(p, os, FLAG_ITEM, outer,
1841 context.font.shape, "up");
1843 else if (t.cs() == "textit")
1844 parse_text_attributes(p, os, FLAG_ITEM, outer,
1846 context.font.shape, "italic");
1848 else if (t.cs() == "textsl")
1849 parse_text_attributes(p, os, FLAG_ITEM, outer,
1851 context.font.shape, "slanted");
1853 else if (t.cs() == "textsc")
1854 parse_text_attributes(p, os, FLAG_ITEM, outer,
1856 context.font.shape, "smallcaps");
1858 else if (t.cs() == "textnormal" || t.cs() == "normalfont") {
1859 context.check_layout(os);
1860 TeXFont oldFont = context.font;
1861 context.font.init();
1862 context.font.size = oldFont.size;
1863 os << "\n\\family " << context.font.family << "\n";
1864 os << "\n\\series " << context.font.series << "\n";
1865 os << "\n\\shape " << context.font.shape << "\n";
1866 if (t.cs() == "textnormal") {
1867 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1868 output_font_change(os, context.font, oldFont);
1869 context.font = oldFont;
1871 eat_whitespace(p, os, context, false);
1874 else if (t.cs() == "textcolor") {
1875 // scheme is \textcolor{color name}{text}
1876 string const color = p.verbatim_item();
1877 // we only support the predefined colors of the color package
1878 if (color == "black" || color == "blue" || color == "cyan"
1879 || color == "green" || color == "magenta" || color == "red"
1880 || color == "white" || color == "yellow") {
1881 context.check_layout(os);
1882 os << "\n\\color " << color << "\n";
1883 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1884 context.check_layout(os);
1885 os << "\n\\color inherit\n";
1887 // for custom defined colors
1888 handle_ert(os, t.asInput() + "{" + color + "}", context);
1891 else if (t.cs() == "underbar") {
1892 // Do NOT handle \underline.
1893 // \underbar cuts through y, g, q, p etc.,
1894 // \underline does not.
1895 context.check_layout(os);
1896 os << "\n\\bar under\n";
1897 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1898 context.check_layout(os);
1899 os << "\n\\bar default\n";
1902 else if (t.cs() == "emph" || t.cs() == "noun") {
1903 context.check_layout(os);
1904 os << "\n\\" << t.cs() << " on\n";
1905 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1906 context.check_layout(os);
1907 os << "\n\\" << t.cs() << " default\n";
1910 else if (t.cs() == "lyxline") {
1911 context.check_layout(os);
1915 else if (use_natbib &&
1916 is_known(t.cs(), known_natbib_commands) &&
1917 ((t.cs() != "citefullauthor" &&
1918 t.cs() != "citeyear" &&
1919 t.cs() != "citeyearpar") ||
1920 p.next_token().asInput() != "*")) {
1921 context.check_layout(os);
1923 // \citet[before][after]{a} \citet[after][before]{a}
1924 // \citet[before][]{a} \citet[][before]{a}
1925 // \citet[after]{a} \citet[after]{a}
1926 // \citet{a} \citet{a}
1927 string command = '\\' + t.cs();
1928 if (p.next_token().asInput() == "*") {
1932 if (command == "\\citefullauthor")
1933 // alternative name for "\\citeauthor*"
1934 command = "\\citeauthor*";
1936 // text before the citation
1938 // text after the citation
1940 get_cite_arguments(p, true, before, after);
1942 if (command == "\\cite") {
1943 // \cite without optional argument means
1944 // \citet, \cite with at least one optional
1945 // argument means \citep.
1946 if (before.empty() && after.empty())
1947 command = "\\citet";
1949 command = "\\citep";
1951 if (before.empty() && after == "[]")
1952 // avoid \citet[]{a}
1954 else if (before == "[]" && after == "[]") {
1955 // avoid \citet[][]{a}
1959 begin_inset(os, "LatexCommand ");
1960 os << command << after << before
1961 << '{' << p.verbatim_item() << "}\n";
1965 else if (use_jurabib &&
1966 is_known(t.cs(), known_jurabib_commands)) {
1967 context.check_layout(os);
1968 string const command = '\\' + t.cs();
1969 char argumentOrder = '\0';
1970 vector<string> const & options = used_packages["jurabib"];
1971 if (find(options.begin(), options.end(),
1972 "natbiborder") != options.end())
1973 argumentOrder = 'n';
1974 else if (find(options.begin(), options.end(),
1975 "jurabiborder") != options.end())
1976 argumentOrder = 'j';
1978 // text before the citation
1980 // text after the citation
1982 get_cite_arguments(p, argumentOrder != 'j', before, after);
1984 string const citation = p.verbatim_item();
1985 if (!before.empty() && argumentOrder == '\0') {
1986 cerr << "Warning: Assuming argument order "
1987 "of jurabib version 0.6 for\n'"
1988 << command << before << after << '{'
1989 << citation << "}'.\n"
1990 "Add 'jurabiborder' to the jurabib "
1991 "package options if you used an\n"
1992 "earlier jurabib version." << endl;
1994 begin_inset(os, "LatexCommand ");
1995 os << command << after << before
1996 << '{' << citation << "}\n";
2000 else if (is_known(t.cs(), known_latex_commands)) {
2001 // This needs to be after the check for natbib and
2002 // jurabib commands, because "cite" has different
2003 // arguments with natbib and jurabib.
2004 context.check_layout(os);
2005 begin_inset(os, "LatexCommand ");
2006 os << '\\' << t.cs();
2007 // lyx cannot handle newlines in a latex command
2008 // FIXME: Move the substitution into parser::getOpt()?
2009 os << subst(p.getOpt(), "\n", " ");
2010 os << subst(p.getOpt(), "\n", " ");
2011 os << '{' << subst(p.verbatim_item(), "\n", " ") << "}\n";
2015 else if (is_known(t.cs(), known_quotes)) {
2016 char const * const * where = is_known(t.cs(), known_quotes);
2017 context.check_layout(os);
2018 begin_inset(os, "Quotes ");
2019 os << known_coded_quotes[where - known_quotes];
2021 // LyX adds {} after the quote, so we have to eat
2022 // spaces here if there are any before a possible
2024 eat_whitespace(p, os, context, false);
2028 else if (is_known(t.cs(), known_sizes) &&
2029 context.new_layout_allowed) {
2030 char const * const * where = is_known(t.cs(), known_sizes);
2031 context.check_layout(os);
2032 TeXFont const oldFont = context.font;
2033 // the font size index differs by 1, because the known_coded_sizes
2034 // has additionally a "default" entry
2035 context.font.size = known_coded_sizes[where - known_sizes + 1];
2036 output_font_change(os, oldFont, context.font);
2037 eat_whitespace(p, os, context, false);
2040 else if (is_known(t.cs(), known_font_families) &&
2041 context.new_layout_allowed) {
2042 char const * const * where =
2043 is_known(t.cs(), known_font_families);
2044 context.check_layout(os);
2045 TeXFont const oldFont = context.font;
2046 context.font.family =
2047 known_coded_font_families[where - known_font_families];
2048 output_font_change(os, oldFont, context.font);
2049 eat_whitespace(p, os, context, false);
2052 else if (is_known(t.cs(), known_font_series) &&
2053 context.new_layout_allowed) {
2054 char const * const * where =
2055 is_known(t.cs(), known_font_series);
2056 context.check_layout(os);
2057 TeXFont const oldFont = context.font;
2058 context.font.series =
2059 known_coded_font_series[where - known_font_series];
2060 output_font_change(os, oldFont, context.font);
2061 eat_whitespace(p, os, context, false);
2064 else if (is_known(t.cs(), known_font_shapes) &&
2065 context.new_layout_allowed) {
2066 char const * const * where =
2067 is_known(t.cs(), known_font_shapes);
2068 context.check_layout(os);
2069 TeXFont const oldFont = context.font;
2070 context.font.shape =
2071 known_coded_font_shapes[where - known_font_shapes];
2072 output_font_change(os, oldFont, context.font);
2073 eat_whitespace(p, os, context, false);
2075 else if (is_known(t.cs(), known_old_font_families) &&
2076 context.new_layout_allowed) {
2077 char const * const * where =
2078 is_known(t.cs(), known_old_font_families);
2079 context.check_layout(os);
2080 TeXFont const oldFont = context.font;
2081 context.font.init();
2082 context.font.size = oldFont.size;
2083 context.font.family =
2084 known_coded_font_families[where - known_old_font_families];
2085 output_font_change(os, oldFont, context.font);
2086 eat_whitespace(p, os, context, false);
2089 else if (is_known(t.cs(), known_old_font_series) &&
2090 context.new_layout_allowed) {
2091 char const * const * where =
2092 is_known(t.cs(), known_old_font_series);
2093 context.check_layout(os);
2094 TeXFont const oldFont = context.font;
2095 context.font.init();
2096 context.font.size = oldFont.size;
2097 context.font.series =
2098 known_coded_font_series[where - known_old_font_series];
2099 output_font_change(os, oldFont, context.font);
2100 eat_whitespace(p, os, context, false);
2103 else if (is_known(t.cs(), known_old_font_shapes) &&
2104 context.new_layout_allowed) {
2105 char const * const * where =
2106 is_known(t.cs(), known_old_font_shapes);
2107 context.check_layout(os);
2108 TeXFont const oldFont = context.font;
2109 context.font.init();
2110 context.font.size = oldFont.size;
2111 context.font.shape =
2112 known_coded_font_shapes[where - known_old_font_shapes];
2113 output_font_change(os, oldFont, context.font);
2114 eat_whitespace(p, os, context, false);
2117 else if (t.cs() == "selectlanguage") {
2118 context.check_layout(os);
2119 // save the language for the case that a \foreignlanguage is used
2120 selectlang = subst(p.verbatim_item(), "\n", " ");
2121 os << "\\lang " << selectlang << "\n";
2125 else if (t.cs() == "foreignlanguage") {
2126 context.check_layout(os);
2127 os << "\n\\lang " << subst(p.verbatim_item(), "\n", " ") << "\n";
2128 os << subst(p.verbatim_item(), "\n", " ");
2129 // set back to last selectlanguage
2130 os << "\n\\lang " << selectlang << "\n";
2133 else if (t.cs() == "inputencoding")
2134 // write nothing because this is done by LyX using the "\lang"
2135 // information given by selectlanguage and foreignlanguage
2136 subst(p.verbatim_item(), "\n", " ");
2138 else if (t.cs() == "LyX" || t.cs() == "TeX"
2139 || t.cs() == "LaTeX") {
2140 context.check_layout(os);
2142 skip_braces(p); // eat {}
2145 else if (t.cs() == "LaTeXe") {
2146 context.check_layout(os);
2148 skip_braces(p); // eat {}
2151 else if (t.cs() == "ldots") {
2152 context.check_layout(os);
2154 os << "\\SpecialChar \\ldots{}\n";
2157 else if (t.cs() == "lyxarrow") {
2158 context.check_layout(os);
2159 os << "\\SpecialChar \\menuseparator\n";
2163 else if (t.cs() == "textcompwordmark") {
2164 context.check_layout(os);
2165 os << "\\SpecialChar \\textcompwordmark{}\n";
2169 else if (t.cs() == "@" && p.next_token().asInput() == ".") {
2170 context.check_layout(os);
2171 os << "\\SpecialChar \\@.\n";
2175 else if (t.cs() == "-") {
2176 context.check_layout(os);
2177 os << "\\SpecialChar \\-\n";
2180 else if (t.cs() == "textasciitilde") {
2181 context.check_layout(os);
2186 else if (t.cs() == "textasciicircum") {
2187 context.check_layout(os);
2192 else if (t.cs() == "textbackslash") {
2193 context.check_layout(os);
2194 os << "\n\\backslash\n";
2198 else if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#"
2199 || t.cs() == "$" || t.cs() == "{" || t.cs() == "}"
2201 context.check_layout(os);
2205 else if (t.cs() == "char") {
2206 context.check_layout(os);
2207 if (p.next_token().character() == '`') {
2209 if (p.next_token().cs() == "\"") {
2214 handle_ert(os, "\\char`", context);
2217 handle_ert(os, "\\char", context);
2221 else if (t.cs() == "verb") {
2222 context.check_layout(os);
2223 char const delimiter = p.next_token().character();
2224 string const arg = p.getArg(delimiter, delimiter);
2226 oss << "\\verb" << delimiter << arg << delimiter;
2227 handle_ert(os, oss.str(), context);
2230 else if (t.cs() == "\"") {
2231 context.check_layout(os);
2232 string const name = p.verbatim_item();
2233 if (name == "a") os << '\xe4';
2234 else if (name == "o") os << '\xf6';
2235 else if (name == "u") os << '\xfc';
2236 else if (name == "A") os << '\xc4';
2237 else if (name == "O") os << '\xd6';
2238 else if (name == "U") os << '\xdc';
2239 else handle_ert(os, "\"{" + name + "}", context);
2242 // Problem: \= creates a tabstop inside the tabbing environment
2243 // and else an accent. In the latter case we really would want
2244 // \={o} instead of \= o.
2245 else if (t.cs() == "=" && (flags & FLAG_TABBING))
2246 handle_ert(os, t.asInput(), context);
2248 else if (t.cs() == "H" || t.cs() == "c" || t.cs() == "^"
2249 || t.cs() == "'" || t.cs() == "`"
2250 || t.cs() == "~" || t.cs() == "." || t.cs() == "=") {
2251 // we need the trim as the LyX parser chokes on such spaces
2252 // The argument of InsetLatexAccent is parsed as a
2253 // subset of LaTeX, so don't parse anything here,
2254 // but use the raw argument.
2255 // Otherwise we would convert \~{\i} wrongly.
2256 // This will of course not translate \~{\ss} to \~{ß},
2257 // but that does at least compile and does only look
2258 // strange on screen.
2259 context.check_layout(os);
2260 os << "\\i \\" << t.cs() << "{"
2261 << trim(p.verbatim_item(), " ")
2265 else if (t.cs() == "ss") {
2266 context.check_layout(os);
2268 skip_braces(p); // eat {}
2271 else if (t.cs() == "i" || t.cs() == "j" || t.cs() == "l" ||
2273 context.check_layout(os);
2274 os << "\\i \\" << t.cs() << "{}\n";
2275 skip_braces(p); // eat {}
2278 else if (t.cs() == "\\") {
2279 context.check_layout(os);
2280 string const next = p.next_token().asInput();
2282 handle_ert(os, "\\\\" + p.getOpt(), context);
2283 else if (next == "*") {
2285 handle_ert(os, "\\\\*" + p.getOpt(), context);
2288 os << "\n\\newline\n";
2292 else if (t.cs() == "newline" ||
2293 t.cs() == "linebreak") {
2294 context.check_layout(os);
2295 os << "\n\\" << t.cs() << "\n";
2296 skip_braces(p); // eat {}
2299 else if (t.cs() == "input" || t.cs() == "include"
2300 || t.cs() == "verbatiminput") {
2301 string name = '\\' + t.cs();
2302 if (t.cs() == "verbatiminput"
2303 && p.next_token().asInput() == "*")
2304 name += p.get_token().asInput();
2305 context.check_layout(os);
2306 begin_inset(os, "Include ");
2307 string filename(normalize_filename(p.getArg('{', '}')));
2308 string const path = getMasterFilePath();
2309 // We want to preserve relative / absolute filenames,
2310 // therefore path is only used for testing
2311 // FIXME UNICODE encoding of filename and path may be
2312 // wrong (makeAbsPath expects utf8)
2313 if ((t.cs() == "include" || t.cs() == "input") &&
2314 !makeAbsPath(filename, path).exists()) {
2315 // The file extension is probably missing.
2316 // Now try to find it out.
2317 string const tex_name =
2318 find_file(filename, path,
2319 known_tex_extensions);
2320 if (!tex_name.empty())
2321 filename = tex_name;
2323 // FIXME UNICODE encoding of filename and path may be
2324 // wrong (makeAbsPath expects utf8)
2325 if (makeAbsPath(filename, path).exists()) {
2326 string const abstexname =
2327 makeAbsPath(filename, path).absFilename();
2328 string const abslyxname =
2329 changeExtension(abstexname, ".lyx");
2330 fix_relative_filename(filename);
2331 string const lyxname =
2332 changeExtension(filename, ".lyx");
2333 if (t.cs() != "verbatiminput" &&
2334 tex2lyx(abstexname, FileName(abslyxname))) {
2335 os << name << '{' << lyxname << "}\n";
2337 os << name << '{' << filename << "}\n";
2340 cerr << "Warning: Could not find included file '"
2341 << filename << "'." << endl;
2342 os << name << '{' << filename << "}\n";
2344 os << "preview false\n";
2348 else if (t.cs() == "bibliographystyle") {
2349 // store new bibliographystyle
2350 bibliographystyle = p.verbatim_item();
2351 // output new bibliographystyle.
2352 // This is only necessary if used in some other macro than \bibliography.
2353 handle_ert(os, "\\bibliographystyle{" + bibliographystyle + "}", context);
2356 else if (t.cs() == "bibliography") {
2357 context.check_layout(os);
2358 begin_inset(os, "LatexCommand ");
2360 // Do we have a bibliographystyle set?
2361 if (!bibliographystyle.empty()) {
2362 os << '[' << bibliographystyle << ']';
2364 os << '{' << p.verbatim_item() << "}\n";
2368 else if (t.cs() == "parbox")
2369 parse_box(p, os, FLAG_ITEM, outer, context, true);
2371 //\makebox() is part of the picture environment and different from \makebox{}
2372 //\makebox{} will be parsed by parse_box when bug 2956 is fixed
2373 else if (t.cs() == "makebox") {
2374 string arg = t.asInput();
2375 if (p.next_token().character() == '(')
2376 //the syntax is: \makebox(x,y)[position]{content}
2377 arg += p.getFullParentheseArg();
2379 //the syntax is: \makebox[width][position]{content}
2380 arg += p.getFullOpt();
2381 handle_ert(os, arg + p.getFullOpt(), context);
2384 else if (t.cs() == "smallskip" ||
2385 t.cs() == "medskip" ||
2386 t.cs() == "bigskip" ||
2387 t.cs() == "vfill") {
2388 context.check_layout(os);
2389 begin_inset(os, "VSpace ");
2395 else if (is_known(t.cs(), known_spaces)) {
2396 char const * const * where = is_known(t.cs(), known_spaces);
2397 context.check_layout(os);
2398 os << "\\InsetSpace ";
2399 os << '\\' << known_coded_spaces[where - known_spaces]
2401 // LaTeX swallows whitespace after all spaces except
2402 // "\\,". We have to do that here, too, because LyX
2403 // adds "{}" which would make the spaces significant.
2405 eat_whitespace(p, os, context, false);
2406 // LyX adds "{}" after all spaces except "\\ " and
2407 // "\\,", so we have to remove "{}".
2408 // "\\,{}" is equivalent to "\\," in LaTeX, so we
2409 // remove the braces after "\\,", too.
2414 else if (t.cs() == "newpage" ||
2415 t.cs() == "pagebreak" ||
2416 t.cs() == "clearpage" ||
2417 t.cs() == "cleardoublepage") {
2418 context.check_layout(os);
2419 os << "\n\\" << t.cs() << "\n";
2420 skip_braces(p); // eat {}
2423 else if (t.cs() == "newcommand" ||
2424 t.cs() == "providecommand" ||
2425 t.cs() == "renewcommand") {
2426 // these could be handled by parse_command(), but
2427 // we need to call add_known_command() here.
2428 string name = t.asInput();
2429 if (p.next_token().asInput() == "*") {
2430 // Starred form. Eat '*'
2434 string const command = p.verbatim_item();
2435 string const opt1 = p.getOpt();
2436 string const opt2 = p.getFullOpt();
2437 add_known_command(command, opt1, !opt2.empty());
2438 string const ert = name + '{' + command + '}' +
2440 '{' + p.verbatim_item() + '}';
2442 context.check_layout(os);
2443 begin_inset(os, "FormulaMacro");
2448 else if (t.cs() == "vspace") {
2449 bool starred = false;
2450 if (p.next_token().asInput() == "*") {
2454 string const length = p.verbatim_item();
2457 bool valid = splitLatexLength(length, valstring, unit);
2458 bool known_vspace = false;
2459 bool known_unit = false;
2462 istringstream iss(valstring);
2465 if (unit == "\\smallskipamount") {
2467 known_vspace = true;
2468 } else if (unit == "\\medskipamount") {
2470 known_vspace = true;
2471 } else if (unit == "\\bigskipamount") {
2473 known_vspace = true;
2474 } else if (unit == "\\fill") {
2476 known_vspace = true;
2479 if (!known_vspace) {
2480 switch (unitFromString(unit)) {
2501 if (known_unit || known_vspace) {
2502 // Literal length or known variable
2503 context.check_layout(os);
2504 begin_inset(os, "VSpace ");
2512 // LyX can't handle other length variables in Inset VSpace
2513 string name = t.asInput();
2518 handle_ert(os, name + '{' + unit + '}', context);
2519 else if (value == -1.0)
2520 handle_ert(os, name + "{-" + unit + '}', context);
2522 handle_ert(os, name + '{' + valstring + unit + '}', context);
2524 handle_ert(os, name + '{' + length + '}', context);
2529 //cerr << "#: " << t << " mode: " << mode << endl;
2530 // heuristic: read up to next non-nested space
2532 string s = t.asInput();
2533 string z = p.verbatim_item();
2534 while (p.good() && z != " " && z.size()) {
2535 //cerr << "read: " << z << endl;
2537 z = p.verbatim_item();
2539 cerr << "found ERT: " << s << endl;
2540 handle_ert(os, s + ' ', context);
2542 string name = t.asInput();
2543 if (p.next_token().asInput() == "*") {
2544 // Starred commands like \vspace*{}
2545 p.get_token(); // Eat '*'
2548 if (! parse_command(name, p, os, outer, context))
2549 handle_ert(os, name, context);
2552 if (flags & FLAG_LEAVE) {
2553 flags &= ~FLAG_LEAVE;