2 * \file tex2lyx/text.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Jean-Marc Lasgouttes
10 * Full author contact details are available in file CREDITS.
21 #include "FloatList.h"
25 #include "support/lassert.h"
26 #include "support/convert.h"
27 #include "support/FileName.h"
28 #include "support/filetools.h"
29 #include "support/lstrings.h"
38 using namespace lyx::support;
43 void parse_text_in_inset(Parser & p, ostream & os, unsigned flags, bool outer,
44 Context const & context, InsetLayout const * layout)
46 bool const forcePlainLayout =
47 layout ? layout->forcePlainLayout() : false;
48 Context newcontext(true, context.textclass);
50 newcontext.layout = &context.textclass.plainLayout();
52 newcontext.font = context.font;
53 parse_text(p, os, flags, outer, newcontext);
54 newcontext.check_end_layout(os);
60 void parse_text_in_inset(Parser & p, ostream & os, unsigned flags, bool outer,
61 Context const & context, string const & name)
63 InsetLayout const * layout = 0;
64 DocumentClass::InsetLayouts::const_iterator it =
65 context.textclass.insetLayouts().find(from_ascii(name));
66 if (it != context.textclass.insetLayouts().end())
67 layout = &(it->second);
68 parse_text_in_inset(p, os, flags, outer, context, layout);
71 /// parses a paragraph snippet, useful for example for \\emph{...}
72 void parse_text_snippet(Parser & p, ostream & os, unsigned flags, bool outer,
75 Context newcontext(context);
76 // Don't inherit the paragraph-level extra stuff
77 newcontext.par_extra_stuff.clear();
78 parse_text(p, os, flags, outer, newcontext);
79 // Make sure that we don't create invalid .lyx files
80 context.need_layout = newcontext.need_layout;
81 context.need_end_layout = newcontext.need_end_layout;
86 * Thin wrapper around parse_text_snippet() using a string.
88 * We completely ignore \c context.need_layout and \c context.need_end_layout,
89 * because our return value is not used directly (otherwise the stream version
90 * of parse_text_snippet() could be used). That means that the caller needs
91 * to do layout management manually.
92 * This is intended to parse text that does not create any layout changes.
94 string parse_text_snippet(Parser & p, unsigned flags, const bool outer,
97 Context newcontext(context);
98 newcontext.need_layout = false;
99 newcontext.need_end_layout = false;
100 newcontext.new_layout_allowed = false;
101 // Avoid warning by Context::~Context()
102 newcontext.par_extra_stuff.clear();
104 parse_text_snippet(p, os, flags, outer, newcontext);
109 char const * const known_ref_commands[] = { "ref", "pageref", "vref",
110 "vpageref", "prettyref", "eqref", 0 };
112 char const * const known_coded_ref_commands[] = { "ref", "pageref", "vref",
113 "vpageref", "formatted", "eqref", 0 };
117 * The starred forms are also known except for "citefullauthor",
118 * "citeyear" and "citeyearpar".
120 char const * const known_natbib_commands[] = { "cite", "citet", "citep",
121 "citealt", "citealp", "citeauthor", "citeyear", "citeyearpar",
122 "citefullauthor", "Citet", "Citep", "Citealt", "Citealp", "Citeauthor", 0 };
126 * No starred form other than "cite*" known.
128 char const * const known_jurabib_commands[] = { "cite", "citet", "citep",
129 "citealt", "citealp", "citeauthor", "citeyear", "citeyearpar",
130 // jurabib commands not (yet) supported by LyX:
132 // "footcite", "footcitet", "footcitep", "footcitealt", "footcitealp",
133 // "footciteauthor", "footciteyear", "footciteyearpar",
134 "citefield", "citetitle", 0 };
136 /// LaTeX names for quotes
137 char const * const known_quotes[] = { "dq", "guillemotleft", "flqq", "og",
138 "guillemotright", "frqq", "fg", "glq", "glqq", "textquoteleft", "grq", "grqq",
139 "quotedblbase", "textquotedblleft", "quotesinglbase", "textquoteright", "flq",
140 "guilsinglleft", "frq", "guilsinglright", 0};
142 /// the same as known_quotes with .lyx names
143 char const * const known_coded_quotes[] = { "prd", "ard", "ard", "ard",
144 "ald", "ald", "ald", "gls", "gld", "els", "els", "grd",
145 "gld", "grd", "gls", "ers", "fls",
146 "fls", "frs", "frs", 0};
148 /// LaTeX names for font sizes
149 char const * const known_sizes[] = { "tiny", "scriptsize", "footnotesize",
150 "small", "normalsize", "large", "Large", "LARGE", "huge", "Huge", 0};
152 /// the same as known_sizes with .lyx names
153 char const * const known_coded_sizes[] = { "tiny", "scriptsize", "footnotesize",
154 "small", "normal", "large", "larger", "largest", "huge", "giant", 0};
156 /// LaTeX 2.09 names for font families
157 char const * const known_old_font_families[] = { "rm", "sf", "tt", 0};
159 /// LaTeX names for font families
160 char const * const known_font_families[] = { "rmfamily", "sffamily",
163 /// the same as known_old_font_families and known_font_families with .lyx names
164 char const * const known_coded_font_families[] = { "roman", "sans",
167 /// LaTeX 2.09 names for font series
168 char const * const known_old_font_series[] = { "bf", 0};
170 /// LaTeX names for font series
171 char const * const known_font_series[] = { "bfseries", "mdseries", 0};
173 /// the same as known_old_font_series and known_font_series with .lyx names
174 char const * const known_coded_font_series[] = { "bold", "medium", 0};
176 /// LaTeX 2.09 names for font shapes
177 char const * const known_old_font_shapes[] = { "it", "sl", "sc", 0};
179 /// LaTeX names for font shapes
180 char const * const known_font_shapes[] = { "itshape", "slshape", "scshape",
183 /// the same as known_old_font_shapes and known_font_shapes with .lyx names
184 char const * const known_coded_font_shapes[] = { "italic", "slanted",
185 "smallcaps", "up", 0};
188 * Graphics file extensions known by the dvips driver of the graphics package.
189 * These extensions are used to complete the filename of an included
190 * graphics file if it does not contain an extension.
191 * The order must be the same that latex uses to find a file, because we
192 * will use the first extension that matches.
193 * This is only an approximation for the common cases. If we would want to
194 * do it right in all cases, we would need to know which graphics driver is
195 * used and know the extensions of every driver of the graphics package.
197 char const * const known_dvips_graphics_formats[] = {"eps", "ps", "eps.gz",
198 "ps.gz", "eps.Z", "ps.Z", 0};
201 * Graphics file extensions known by the pdftex driver of the graphics package.
202 * \sa known_dvips_graphics_formats
204 char const * const known_pdftex_graphics_formats[] = {"png", "pdf", "jpg",
208 * Known file extensions for TeX files as used by \\include.
210 char const * const known_tex_extensions[] = {"tex", 0};
212 /// spaces known by InsetSpace
213 char const * const known_spaces[] = { " ", "space", ",",
214 "thinspace", "quad", "qquad", "enspace", "enskip",
215 "negthinspace", "negmedspace", "negthickspace", "textvisiblespace",
216 "hfill", "dotfill", "hrulefill", "leftarrowfill", "rightarrowfill",
217 "upbracefill", "downbracefill", 0};
219 /// the same as known_spaces with .lyx names
220 char const * const known_coded_spaces[] = { "space{}", "space{}",
221 "thinspace{}", "thinspace{}", "quad{}", "qquad{}", "enspace{}", "enskip{}",
222 "negthinspace{}", "negmedspace{}", "negthickspace{}", "textvisiblespace{}",
223 "hfill{}", "dotfill{}", "hrulefill{}", "leftarrowfill{}", "rightarrowfill{}",
224 "upbracefill{}", "downbracefill{}", 0};
226 /// These are translated by LyX to commands like "\\LyX{}", so we have to put
227 /// them in ERT. "LaTeXe" must come before "LaTeX"!
228 char const * const known_phrases[] = {"LyX", "TeX", "LaTeXe", "LaTeX", 0};
229 char const * const known_coded_phrases[] = {"LyX", "TeX", "LaTeX2e", "LaTeX", 0};
230 int const known_phrase_lengths[] = {3, 5, 7, 0};
233 /// splits "x=z, y=b" into a map and an ordered keyword vector
234 void split_map(string const & s, map<string, string> & res, vector<string> & keys)
239 keys.resize(v.size());
240 for (size_t i = 0; i < v.size(); ++i) {
241 size_t const pos = v[i].find('=');
242 string const index = trimSpaceAndEol(v[i].substr(0, pos));
243 string const value = trimSpaceAndEol(v[i].substr(pos + 1, string::npos));
251 * Split a LaTeX length into value and unit.
252 * The latter can be a real unit like "pt", or a latex length variable
253 * like "\textwidth". The unit may contain additional stuff like glue
254 * lengths, but we don't care, because such lengths are ERT anyway.
255 * \returns true if \p value and \p unit are valid.
257 bool splitLatexLength(string const & len, string & value, string & unit)
261 const string::size_type i = len.find_first_not_of(" -+0123456789.,");
262 //'4,5' is a valid LaTeX length number. Change it to '4.5'
263 string const length = subst(len, ',', '.');
264 if (i == string::npos)
267 if (len[0] == '\\') {
268 // We had something like \textwidth without a factor
274 value = trimSpaceAndEol(string(length, 0, i));
278 // 'cM' is a valid LaTeX length unit. Change it to 'cm'
279 if (contains(len, '\\'))
280 unit = trimSpaceAndEol(string(len, i));
282 unit = ascii_lowercase(trimSpaceAndEol(string(len, i)));
287 /// A simple function to translate a latex length to something LyX can
288 /// understand. Not perfect, but rather best-effort.
289 bool translate_len(string const & length, string & valstring, string & unit)
291 if (!splitLatexLength(length, valstring, unit))
293 // LyX uses percent values
295 istringstream iss(valstring);
300 string const percentval = oss.str();
302 if (unit.empty() || unit[0] != '\\')
304 string::size_type const i = unit.find(' ');
305 string const endlen = (i == string::npos) ? string() : string(unit, i);
306 if (unit == "\\textwidth") {
307 valstring = percentval;
308 unit = "text%" + endlen;
309 } else if (unit == "\\columnwidth") {
310 valstring = percentval;
311 unit = "col%" + endlen;
312 } else if (unit == "\\paperwidth") {
313 valstring = percentval;
314 unit = "page%" + endlen;
315 } else if (unit == "\\linewidth") {
316 valstring = percentval;
317 unit = "line%" + endlen;
318 } else if (unit == "\\paperheight") {
319 valstring = percentval;
320 unit = "pheight%" + endlen;
321 } else if (unit == "\\textheight") {
322 valstring = percentval;
323 unit = "theight%" + endlen;
331 string translate_len(string const & length)
335 if (translate_len(length, value, unit))
337 // If the input is invalid, return what we have.
345 * Translates a LaTeX length into \p value, \p unit and
346 * \p special parts suitable for a box inset.
347 * The difference from translate_len() is that a box inset knows about
348 * some special "units" that are stored in \p special.
350 void translate_box_len(string const & length, string & value, string & unit, string & special)
352 if (translate_len(length, value, unit)) {
353 if (unit == "\\height" || unit == "\\depth" ||
354 unit == "\\totalheight" || unit == "\\width") {
355 special = unit.substr(1);
356 // The unit is not used, but LyX requires a dummy setting
369 * Find a file with basename \p name in path \p path and an extension
372 string find_file(string const & name, string const & path,
373 char const * const * extensions)
375 for (char const * const * what = extensions; *what; ++what) {
376 string const trial = addExtension(name, *what);
377 if (makeAbsPath(trial, path).exists())
384 void begin_inset(ostream & os, string const & name)
386 os << "\n\\begin_inset " << name;
390 void begin_command_inset(ostream & os, string const & name,
391 string const & latexname)
393 begin_inset(os, "CommandInset ");
394 os << name << "\nLatexCommand " << latexname << '\n';
398 void end_inset(ostream & os)
400 os << "\n\\end_inset\n\n";
404 bool skip_braces(Parser & p)
406 if (p.next_token().cat() != catBegin)
409 if (p.next_token().cat() == catEnd) {
418 /// replace LaTeX commands in \p s from the unicodesymbols file with their
420 docstring convert_unicodesymbols(docstring s)
423 for (size_t i = 0; i < s.size();) {
430 docstring parsed = encodings.fromLaTeXCommand(s, rem,
431 Encodings::TEXT_CMD);
434 if (s.empty() || s[0] != '\\')
443 /// try to convert \p s to a valid InsetCommand argument
444 string convert_command_inset_arg(string s)
447 // since we don't know the input encoding we can't use from_utf8
448 s = to_utf8(convert_unicodesymbols(from_ascii(s)));
449 // LyX cannot handle newlines in a latex command
450 return subst(s, "\n", " ");
454 void handle_backslash(ostream & os, string const & s)
456 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
458 os << "\n\\backslash\n";
465 void handle_ert(ostream & os, string const & s, Context & context)
467 // We must have a valid layout before outputting the ERT inset.
468 context.check_layout(os);
469 Context newcontext(true, context.textclass);
470 begin_inset(os, "ERT");
471 os << "\nstatus collapsed\n";
472 newcontext.check_layout(os);
473 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
475 os << "\n\\backslash\n";
476 else if (*it == '\n') {
477 newcontext.new_paragraph(os);
478 newcontext.check_layout(os);
482 newcontext.check_end_layout(os);
487 void handle_comment(ostream & os, string const & s, Context & context)
489 // TODO: Handle this better
490 Context newcontext(true, context.textclass);
491 begin_inset(os, "ERT");
492 os << "\nstatus collapsed\n";
493 newcontext.check_layout(os);
494 handle_backslash(os, s);
495 // make sure that our comment is the last thing on the line
496 newcontext.new_paragraph(os);
497 newcontext.check_layout(os);
498 newcontext.check_end_layout(os);
503 Layout const * findLayout(TextClass const & textclass, string const & name, bool command)
505 Layout const * layout = findLayoutWithoutModule(textclass, name, command);
508 if (checkModule(name, command))
509 return findLayoutWithoutModule(textclass, name, command);
514 InsetLayout const * findInsetLayout(TextClass const & textclass, string const & name, bool command)
516 InsetLayout const * insetlayout = findInsetLayoutWithoutModule(textclass, name, command);
519 if (checkModule(name, command))
520 return findInsetLayoutWithoutModule(textclass, name, command);
525 void eat_whitespace(Parser &, ostream &, Context &, bool);
529 * Skips whitespace and braces.
530 * This should be called after a command has been parsed that is not put into
531 * ERT, and where LyX adds "{}" if needed.
533 void skip_spaces_braces(Parser & p, bool keepws = false)
535 /* The following four examples produce the same typeset output and
536 should be handled by this function:
544 // Unfortunately we need to skip comments, too.
545 // We can't use eat_whitespace since writing them after the {}
546 // results in different output in some cases.
547 bool const skipped_spaces = p.skip_spaces(true);
548 bool const skipped_braces = skip_braces(p);
549 if (keepws && skipped_spaces && !skipped_braces)
550 // put back the space (it is better handled by check_space)
551 p.unskip_spaces(true);
555 void output_command_layout(ostream & os, Parser & p, bool outer,
556 Context & parent_context,
557 Layout const * newlayout)
559 TeXFont const oldFont = parent_context.font;
560 // save the current font size
561 string const size = oldFont.size;
562 // reset the font size to default, because the font size switches
563 // don't affect section headings and the like
564 parent_context.font.size = Context::normalfont.size;
565 // we only need to write the font change if we have an open layout
566 if (!parent_context.atParagraphStart())
567 output_font_change(os, oldFont, parent_context.font);
568 parent_context.check_end_layout(os);
569 Context context(true, parent_context.textclass, newlayout,
570 parent_context.layout, parent_context.font);
571 if (parent_context.deeper_paragraph) {
572 // We are beginning a nested environment after a
573 // deeper paragraph inside the outer list environment.
574 // Therefore we don't need to output a "begin deeper".
575 context.need_end_deeper = true;
577 context.check_deeper(os);
578 context.check_layout(os);
579 unsigned int optargs = 0;
580 while (optargs < context.layout->optargs) {
581 eat_whitespace(p, os, context, false);
582 if (p.next_token().cat() == catEscape ||
583 p.next_token().character() != '[')
585 p.get_token(); // eat '['
586 begin_inset(os, "Argument\n");
587 os << "status collapsed\n\n";
588 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
590 eat_whitespace(p, os, context, false);
593 unsigned int reqargs = 0;
594 while (reqargs < context.layout->reqargs) {
595 eat_whitespace(p, os, context, false);
596 if (p.next_token().cat() != catBegin)
598 p.get_token(); // eat '{'
599 begin_inset(os, "Argument\n");
600 os << "status collapsed\n\n";
601 parse_text_in_inset(p, os, FLAG_BRACE_LAST, outer, context);
603 eat_whitespace(p, os, context, false);
606 parse_text(p, os, FLAG_ITEM, outer, context);
607 context.check_end_layout(os);
608 if (parent_context.deeper_paragraph) {
609 // We must suppress the "end deeper" because we
610 // suppressed the "begin deeper" above.
611 context.need_end_deeper = false;
613 context.check_end_deeper(os);
614 // We don't need really a new paragraph, but
615 // we must make sure that the next item gets a \begin_layout.
616 parent_context.new_paragraph(os);
617 // Set the font size to the original value. No need to output it here
618 // (Context::begin_layout() will do that if needed)
619 parent_context.font.size = size;
624 * Output a space if necessary.
625 * This function gets called for every whitespace token.
627 * We have three cases here:
628 * 1. A space must be suppressed. Example: The lyxcode case below
629 * 2. A space may be suppressed. Example: Spaces before "\par"
630 * 3. A space must not be suppressed. Example: A space between two words
632 * We currently handle only 1. and 3 and from 2. only the case of
633 * spaces before newlines as a side effect.
635 * 2. could be used to suppress as many spaces as possible. This has two effects:
636 * - Reimporting LyX generated LaTeX files changes almost no whitespace
637 * - Superflous whitespace from non LyX generated LaTeX files is removed.
638 * The drawback is that the logic inside the function becomes
639 * complicated, and that is the reason why it is not implemented.
641 void check_space(Parser & p, ostream & os, Context & context)
643 Token const next = p.next_token();
644 Token const curr = p.curr_token();
645 // A space before a single newline and vice versa must be ignored
646 // LyX emits a newline before \end{lyxcode}.
647 // This newline must be ignored,
648 // otherwise LyX will add an additional protected space.
649 if (next.cat() == catSpace ||
650 next.cat() == catNewline ||
651 (next.cs() == "end" && context.layout->free_spacing && curr.cat() == catNewline)) {
654 context.check_layout(os);
660 * Parse all arguments of \p command
662 void parse_arguments(string const & command,
663 vector<ArgumentType> const & template_arguments,
664 Parser & p, ostream & os, bool outer, Context & context)
666 string ert = command;
667 size_t no_arguments = template_arguments.size();
668 for (size_t i = 0; i < no_arguments; ++i) {
669 switch (template_arguments[i]) {
671 // This argument contains regular LaTeX
672 handle_ert(os, ert + '{', context);
673 eat_whitespace(p, os, context, false);
674 parse_text(p, os, FLAG_ITEM, outer, context);
678 // This argument consists only of a single item.
679 // The presence of '{' or not must be preserved.
681 if (p.next_token().cat() == catBegin)
682 ert += '{' + p.verbatim_item() + '}';
684 ert += p.verbatim_item();
687 // This argument may contain special characters
688 ert += '{' + p.verbatim_item() + '}';
691 // true because we must not eat whitespace
692 // if an optional arg follows we must not strip the
693 // brackets from this one
694 if (i < no_arguments - 1 &&
695 template_arguments[i+1] == optional)
696 ert += p.getFullOpt(true);
698 ert += p.getOpt(true);
702 handle_ert(os, ert, context);
707 * Check whether \p command is a known command. If yes,
708 * handle the command with all arguments.
709 * \return true if the command was parsed, false otherwise.
711 bool parse_command(string const & command, Parser & p, ostream & os,
712 bool outer, Context & context)
714 if (known_commands.find(command) != known_commands.end()) {
715 parse_arguments(command, known_commands[command], p, os,
723 /// Parses a minipage or parbox
724 void parse_box(Parser & p, ostream & os, unsigned outer_flags,
725 unsigned inner_flags, bool outer, Context & parent_context,
726 string const & outer_type, string const & special,
727 string const & inner_type)
731 string hor_pos = "c";
732 // We need to set the height to the LaTeX default of 1\\totalheight
733 // for the case when no height argument is given
734 string height_value = "1";
735 string height_unit = "in";
736 string height_special = "totalheight";
738 if (!inner_type.empty() && p.hasOpt()) {
739 position = p.getArg('[', ']');
740 if (position != "t" && position != "c" && position != "b") {
741 cerr << "invalid position " << position << " for "
742 << inner_type << endl;
746 latex_height = p.getArg('[', ']');
747 translate_box_len(latex_height, height_value, height_unit, height_special);
750 inner_pos = p.getArg('[', ']');
751 if (inner_pos != "c" && inner_pos != "t" &&
752 inner_pos != "b" && inner_pos != "s") {
753 cerr << "invalid inner_pos "
754 << inner_pos << " for "
755 << inner_type << endl;
756 inner_pos = position;
764 if (inner_type.empty()) {
766 latex_width = "\\columnwidth";
769 latex_width = p2.getArg('[', ']');
770 string const opt = p2.getArg('[', ']');
773 if (hor_pos != "l" && hor_pos != "c" &&
775 cerr << "invalid hor_pos " << hor_pos
776 << " for " << outer_type << endl;
782 latex_width = p.verbatim_item();
783 translate_len(latex_width, width_value, width_unit);
784 // LyX can't handle length variables
785 bool use_ert = contains(width_unit, '\\') || contains(height_unit, '\\');
786 if (!use_ert && !outer_type.empty() && !inner_type.empty()) {
787 // Look whether there is some content after the end of the
788 // inner box, but before the end of the outer box.
789 // If yes, we need to output ERT.
791 if (inner_flags & FLAG_END)
792 p.verbatimEnvironment(inner_type);
796 if ((outer_type == "framed" && p.next_token().asInput() != "\\end") ||
797 (outer_type != "framed" && p.next_token().cat() != catEnd)) {
798 // something is between the end of the inner box and
799 // the end of the outer box, so we need to use ERT.
806 if (!outer_type.empty()) {
807 if (outer_flags & FLAG_END)
808 ss << "\\begin{" << outer_type << '}';
810 ss << '\\' << outer_type << '{';
811 if (!special.empty())
815 if (!inner_type.empty()) {
816 if (inner_flags & FLAG_END)
817 ss << "\\begin{" << inner_type << '}';
819 ss << '\\' << inner_type;
820 if (!position.empty())
821 ss << '[' << position << ']';
822 if (!latex_height.empty())
823 ss << '[' << latex_height << ']';
824 if (!inner_pos.empty())
825 ss << '[' << inner_pos << ']';
826 ss << '{' << latex_width << '}';
827 if (!(inner_flags & FLAG_END))
830 handle_ert(os, ss.str(), parent_context);
831 if (!inner_type.empty()) {
832 parse_text(p, os, inner_flags, outer, parent_context);
833 if (inner_flags & FLAG_END)
834 handle_ert(os, "\\end{" + inner_type + '}',
837 handle_ert(os, "}", parent_context);
839 if (!outer_type.empty()) {
840 parse_text(p, os, outer_flags, outer, parent_context);
841 if (outer_flags & FLAG_END)
842 handle_ert(os, "\\end{" + outer_type + '}',
845 handle_ert(os, "}", parent_context);
848 // LyX does not like empty positions, so we have
849 // to set them to the LaTeX default values here.
850 if (position.empty())
852 if (inner_pos.empty())
853 inner_pos = position;
854 parent_context.check_layout(os);
855 begin_inset(os, "Box ");
856 if (outer_type == "framed")
858 else if (outer_type == "framebox")
860 else if (outer_type == "shadowbox")
862 else if (outer_type == "shaded")
864 else if (outer_type == "doublebox")
866 else if (outer_type.empty())
869 os << outer_type << '\n';
870 os << "position \"" << position << "\"\n";
871 os << "hor_pos \"" << hor_pos << "\"\n";
872 os << "has_inner_box " << !inner_type.empty() << "\n";
873 os << "inner_pos \"" << inner_pos << "\"\n";
874 os << "use_parbox " << (inner_type == "parbox") << '\n';
875 os << "use_makebox 0\n";
876 os << "width \"" << width_value << width_unit << "\"\n";
877 os << "special \"none\"\n";
878 os << "height \"" << height_value << height_unit << "\"\n";
879 os << "height_special \"" << height_special << "\"\n";
880 os << "status open\n\n";
881 Context context(true, parent_context.textclass);
882 context.font = parent_context.font;
884 // If we have no inner box the contens will be read with the outer box
885 if (!inner_type.empty())
886 parse_text(p, os, inner_flags, outer, context);
888 // Ensure that the end of the outer box is parsed correctly:
889 // The opening brace has been eaten by parse_outer_box()
890 if (!outer_type.empty() && (outer_flags & FLAG_ITEM)) {
891 outer_flags &= ~FLAG_ITEM;
892 outer_flags |= FLAG_BRACE_LAST;
895 // Find end of outer box, output contents if inner_type is
896 // empty and output possible comments
897 if (!outer_type.empty()) {
898 // This does not output anything but comments if
899 // inner_type is not empty (see use_ert)
900 parse_text(p, os, outer_flags, outer, context);
903 context.check_end_layout(os);
905 #ifdef PRESERVE_LAYOUT
906 // LyX puts a % after the end of the minipage
907 if (p.next_token().cat() == catNewline && p.next_token().cs().size() > 1) {
909 //handle_comment(os, "%dummy", parent_context);
912 parent_context.new_paragraph(os);
914 else if (p.next_token().cat() == catSpace || p.next_token().cat() == catNewline) {
915 //handle_comment(os, "%dummy", parent_context);
918 // We add a protected space if something real follows
919 if (p.good() && p.next_token().cat() != catComment) {
920 begin_inset(os, "space ~\n");
929 void parse_outer_box(Parser & p, ostream & os, unsigned flags, bool outer,
930 Context & parent_context, string const & outer_type,
931 string const & special)
933 eat_whitespace(p, os, parent_context, false);
934 if (flags & FLAG_ITEM) {
936 if (p.next_token().cat() == catBegin)
939 cerr << "Warning: Ignoring missing '{' after \\"
940 << outer_type << '.' << endl;
941 eat_whitespace(p, os, parent_context, false);
944 unsigned int inner_flags = 0;
945 if (outer_type == "shaded") {
946 // These boxes never have an inner box
948 } else if (p.next_token().asInput() == "\\parbox") {
949 inner = p.get_token().cs();
950 inner_flags = FLAG_ITEM;
951 } else if (p.next_token().asInput() == "\\begin") {
952 // Is this a minipage?
955 inner = p.getArg('{', '}');
957 if (inner == "minipage") {
960 eat_whitespace(p, os, parent_context, false);
961 inner_flags = FLAG_END;
965 if (inner_flags == FLAG_END) {
966 active_environments.push_back(inner);
967 parse_box(p, os, flags, FLAG_END, outer, parent_context,
968 outer_type, special, inner);
969 active_environments.pop_back();
971 parse_box(p, os, flags, inner_flags, outer, parent_context,
972 outer_type, special, inner);
977 void parse_listings(Parser & p, ostream & os, Context & parent_context)
979 parent_context.check_layout(os);
980 begin_inset(os, "listings\n");
981 os << "inline false\n"
982 << "status collapsed\n";
983 Context context(true, parent_context.textclass);
984 context.layout = &parent_context.textclass.plainLayout();
985 context.check_layout(os);
986 string const s = p.verbatimEnvironment("lstlisting");
987 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
989 os << "\n\\backslash\n";
990 else if (*it == '\n') {
991 // avoid adding an empty paragraph at the end
993 context.new_paragraph(os);
994 context.check_layout(os);
999 context.check_end_layout(os);
1004 /// parse an unknown environment
1005 void parse_unknown_environment(Parser & p, string const & name, ostream & os,
1006 unsigned flags, bool outer,
1007 Context & parent_context)
1009 if (name == "tabbing")
1010 // We need to remember that we have to handle '\=' specially
1011 flags |= FLAG_TABBING;
1013 // We need to translate font changes and paragraphs inside the
1014 // environment to ERT if we have a non standard font.
1015 // Otherwise things like
1016 // \large\begin{foo}\huge bar\end{foo}
1018 bool const specialfont =
1019 (parent_context.font != parent_context.normalfont);
1020 bool const new_layout_allowed = parent_context.new_layout_allowed;
1022 parent_context.new_layout_allowed = false;
1023 handle_ert(os, "\\begin{" + name + "}", parent_context);
1024 parse_text_snippet(p, os, flags, outer, parent_context);
1025 handle_ert(os, "\\end{" + name + "}", parent_context);
1027 parent_context.new_layout_allowed = new_layout_allowed;
1031 void parse_environment(Parser & p, ostream & os, bool outer,
1032 string & last_env, Context & parent_context)
1034 Layout const * newlayout;
1035 InsetLayout const * newinsetlayout = 0;
1036 string const name = p.getArg('{', '}');
1037 const bool is_starred = suffixIs(name, '*');
1038 string const unstarred_name = rtrim(name, "*");
1039 active_environments.push_back(name);
1041 if (is_math_env(name)) {
1042 parent_context.check_layout(os);
1043 begin_inset(os, "Formula ");
1044 os << "\\begin{" << name << "}";
1045 parse_math(p, os, FLAG_END, MATH_MODE);
1046 os << "\\end{" << name << "}";
1050 else if (name == "tabular" || name == "longtable") {
1051 eat_whitespace(p, os, parent_context, false);
1052 parent_context.check_layout(os);
1053 begin_inset(os, "Tabular ");
1054 handle_tabular(p, os, name == "longtable", parent_context);
1059 else if (parent_context.textclass.floats().typeExist(unstarred_name)) {
1060 eat_whitespace(p, os, parent_context, false);
1061 parent_context.check_layout(os);
1062 begin_inset(os, "Float " + unstarred_name + "\n");
1064 os << "placement " << p.getArg('[', ']') << '\n';
1065 os << "wide " << convert<string>(is_starred)
1066 << "\nsideways false"
1067 << "\nstatus open\n\n";
1068 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
1070 // We don't need really a new paragraph, but
1071 // we must make sure that the next item gets a \begin_layout.
1072 parent_context.new_paragraph(os);
1076 else if (name == "minipage") {
1077 eat_whitespace(p, os, parent_context, false);
1078 parse_box(p, os, 0, FLAG_END, outer, parent_context, "", "", name);
1082 else if (name == "comment") {
1083 eat_whitespace(p, os, parent_context, false);
1084 parent_context.check_layout(os);
1085 begin_inset(os, "Note Comment\n");
1086 os << "status open\n";
1087 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
1090 skip_braces(p); // eat {} that might by set by LyX behind comments
1093 else if (name == "lyxgreyedout") {
1094 eat_whitespace(p, os, parent_context, false);
1095 parent_context.check_layout(os);
1096 begin_inset(os, "Note Greyedout\n");
1097 os << "status open\n";
1098 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
1103 else if (name == "framed" || name == "shaded") {
1104 eat_whitespace(p, os, parent_context, false);
1105 parse_outer_box(p, os, FLAG_END, outer, parent_context, name, "");
1109 else if (name == "lstlisting") {
1110 eat_whitespace(p, os, parent_context, false);
1111 // FIXME handle listings with parameters
1113 parse_unknown_environment(p, name, os, FLAG_END,
1114 outer, parent_context);
1116 parse_listings(p, os, parent_context);
1120 else if (!parent_context.new_layout_allowed)
1121 parse_unknown_environment(p, name, os, FLAG_END, outer,
1124 // Alignment and spacing settings
1125 // FIXME (bug xxxx): These settings can span multiple paragraphs and
1126 // therefore are totally broken!
1127 // Note that \centering, raggedright, and raggedleft cannot be handled, as
1128 // they are commands not environments. They are furthermore switches that
1129 // can be ended by another switches, but also by commands like \footnote or
1130 // \parbox. So the only safe way is to leave them untouched.
1131 else if (name == "center" || name == "centering" ||
1132 name == "flushleft" || name == "flushright" ||
1133 name == "singlespace" || name == "onehalfspace" ||
1134 name == "doublespace" || name == "spacing") {
1135 eat_whitespace(p, os, parent_context, false);
1136 // We must begin a new paragraph if not already done
1137 if (! parent_context.atParagraphStart()) {
1138 parent_context.check_end_layout(os);
1139 parent_context.new_paragraph(os);
1141 if (name == "flushleft")
1142 parent_context.add_extra_stuff("\\align left\n");
1143 else if (name == "flushright")
1144 parent_context.add_extra_stuff("\\align right\n");
1145 else if (name == "center" || name == "centering")
1146 parent_context.add_extra_stuff("\\align center\n");
1147 else if (name == "singlespace")
1148 parent_context.add_extra_stuff("\\paragraph_spacing single\n");
1149 else if (name == "onehalfspace")
1150 parent_context.add_extra_stuff("\\paragraph_spacing onehalf\n");
1151 else if (name == "doublespace")
1152 parent_context.add_extra_stuff("\\paragraph_spacing double\n");
1153 else if (name == "spacing")
1154 parent_context.add_extra_stuff("\\paragraph_spacing other " + p.verbatim_item() + "\n");
1155 parse_text(p, os, FLAG_END, outer, parent_context);
1156 // Just in case the environment is empty
1157 parent_context.extra_stuff.erase();
1158 // We must begin a new paragraph to reset the alignment
1159 parent_context.new_paragraph(os);
1163 // The single '=' is meant here.
1164 else if ((newlayout = findLayout(parent_context.textclass, name, false))) {
1165 eat_whitespace(p, os, parent_context, false);
1166 Context context(true, parent_context.textclass, newlayout,
1167 parent_context.layout, parent_context.font);
1168 if (parent_context.deeper_paragraph) {
1169 // We are beginning a nested environment after a
1170 // deeper paragraph inside the outer list environment.
1171 // Therefore we don't need to output a "begin deeper".
1172 context.need_end_deeper = true;
1174 parent_context.check_end_layout(os);
1175 if (last_env == name) {
1176 // we need to output a separator since LyX would export
1177 // the two environments as one otherwise (bug 5716)
1178 docstring const sep = from_ascii("--Separator--");
1179 TeX2LyXDocClass const & textclass(parent_context.textclass);
1180 if (textclass.hasLayout(sep)) {
1181 Context newcontext(parent_context);
1182 newcontext.layout = &(textclass[sep]);
1183 newcontext.check_layout(os);
1184 newcontext.check_end_layout(os);
1186 parent_context.check_layout(os);
1187 begin_inset(os, "Note Note\n");
1188 os << "status closed\n";
1189 Context newcontext(true, textclass,
1190 &(textclass.defaultLayout()));
1191 newcontext.check_layout(os);
1192 newcontext.check_end_layout(os);
1194 parent_context.check_end_layout(os);
1197 switch (context.layout->latextype) {
1198 case LATEX_LIST_ENVIRONMENT:
1199 context.add_par_extra_stuff("\\labelwidthstring "
1200 + p.verbatim_item() + '\n');
1203 case LATEX_BIB_ENVIRONMENT:
1204 p.verbatim_item(); // swallow next arg
1210 context.check_deeper(os);
1211 // handle known optional and required arguments
1212 // layouts require all optional arguments before the required ones
1213 // Unfortunately LyX can't handle arguments of list arguments (bug 7468):
1214 // It is impossible to place anything after the environment name,
1215 // but before the first \\item.
1216 if (context.layout->latextype == LATEX_ENVIRONMENT) {
1217 bool need_layout = true;
1218 unsigned int optargs = 0;
1219 while (optargs < context.layout->optargs) {
1220 eat_whitespace(p, os, context, false);
1221 if (p.next_token().cat() == catEscape ||
1222 p.next_token().character() != '[')
1224 p.get_token(); // eat '['
1226 context.check_layout(os);
1227 need_layout = false;
1229 begin_inset(os, "Argument\n");
1230 os << "status collapsed\n\n";
1231 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
1233 eat_whitespace(p, os, context, false);
1236 unsigned int reqargs = 0;
1237 while (reqargs < context.layout->reqargs) {
1238 eat_whitespace(p, os, context, false);
1239 if (p.next_token().cat() != catBegin)
1241 p.get_token(); // eat '{'
1243 context.check_layout(os);
1244 need_layout = false;
1246 begin_inset(os, "Argument\n");
1247 os << "status collapsed\n\n";
1248 parse_text_in_inset(p, os, FLAG_BRACE_LAST, outer, context);
1250 eat_whitespace(p, os, context, false);
1254 parse_text(p, os, FLAG_END, outer, context);
1255 context.check_end_layout(os);
1256 if (parent_context.deeper_paragraph) {
1257 // We must suppress the "end deeper" because we
1258 // suppressed the "begin deeper" above.
1259 context.need_end_deeper = false;
1261 context.check_end_deeper(os);
1262 parent_context.new_paragraph(os);
1266 // The single '=' is meant here.
1267 else if ((newinsetlayout = findInsetLayout(parent_context.textclass, name, false))) {
1268 eat_whitespace(p, os, parent_context, false);
1269 parent_context.check_layout(os);
1270 begin_inset(os, "Flex ");
1271 os << to_utf8(newinsetlayout->name()) << '\n'
1272 << "status collapsed\n";
1273 parse_text_in_inset(p, os, FLAG_END, false, parent_context, newinsetlayout);
1277 else if (name == "appendix") {
1278 // This is no good latex style, but it works and is used in some documents...
1279 eat_whitespace(p, os, parent_context, false);
1280 parent_context.check_end_layout(os);
1281 Context context(true, parent_context.textclass, parent_context.layout,
1282 parent_context.layout, parent_context.font);
1283 context.check_layout(os);
1284 os << "\\start_of_appendix\n";
1285 parse_text(p, os, FLAG_END, outer, context);
1286 context.check_end_layout(os);
1290 else if (known_environments.find(name) != known_environments.end()) {
1291 vector<ArgumentType> arguments = known_environments[name];
1292 // The last "argument" denotes wether we may translate the
1293 // environment contents to LyX
1294 // The default required if no argument is given makes us
1295 // compatible with the reLyXre environment.
1296 ArgumentType contents = arguments.empty() ?
1299 if (!arguments.empty())
1300 arguments.pop_back();
1301 // See comment in parse_unknown_environment()
1302 bool const specialfont =
1303 (parent_context.font != parent_context.normalfont);
1304 bool const new_layout_allowed =
1305 parent_context.new_layout_allowed;
1307 parent_context.new_layout_allowed = false;
1308 parse_arguments("\\begin{" + name + "}", arguments, p, os,
1309 outer, parent_context);
1310 if (contents == verbatim)
1311 handle_ert(os, p.verbatimEnvironment(name),
1314 parse_text_snippet(p, os, FLAG_END, outer,
1316 handle_ert(os, "\\end{" + name + "}", parent_context);
1318 parent_context.new_layout_allowed = new_layout_allowed;
1322 parse_unknown_environment(p, name, os, FLAG_END, outer,
1326 active_environments.pop_back();
1330 /// parses a comment and outputs it to \p os.
1331 void parse_comment(Parser & p, ostream & os, Token const & t, Context & context)
1333 LASSERT(t.cat() == catComment, return);
1334 if (!t.cs().empty()) {
1335 context.check_layout(os);
1336 handle_comment(os, '%' + t.cs(), context);
1337 if (p.next_token().cat() == catNewline) {
1338 // A newline after a comment line starts a new
1340 if (context.new_layout_allowed) {
1341 if(!context.atParagraphStart())
1342 // Only start a new paragraph if not already
1343 // done (we might get called recursively)
1344 context.new_paragraph(os);
1346 handle_ert(os, "\n", context);
1347 eat_whitespace(p, os, context, true);
1350 // "%\n" combination
1357 * Reads spaces and comments until the first non-space, non-comment token.
1358 * New paragraphs (double newlines or \\par) are handled like simple spaces
1359 * if \p eatParagraph is true.
1360 * Spaces are skipped, but comments are written to \p os.
1362 void eat_whitespace(Parser & p, ostream & os, Context & context,
1366 Token const & t = p.get_token();
1367 if (t.cat() == catComment)
1368 parse_comment(p, os, t, context);
1369 else if ((! eatParagraph && p.isParagraph()) ||
1370 (t.cat() != catSpace && t.cat() != catNewline)) {
1379 * Set a font attribute, parse text and reset the font attribute.
1380 * \param attribute Attribute name (e.g. \\family, \\shape etc.)
1381 * \param currentvalue Current value of the attribute. Is set to the new
1382 * value during parsing.
1383 * \param newvalue New value of the attribute
1385 void parse_text_attributes(Parser & p, ostream & os, unsigned flags, bool outer,
1386 Context & context, string const & attribute,
1387 string & currentvalue, string const & newvalue)
1389 context.check_layout(os);
1390 string const oldvalue = currentvalue;
1391 currentvalue = newvalue;
1392 os << '\n' << attribute << ' ' << newvalue << "\n";
1393 parse_text_snippet(p, os, flags, outer, context);
1394 context.check_layout(os);
1395 os << '\n' << attribute << ' ' << oldvalue << "\n";
1396 currentvalue = oldvalue;
1400 /// get the arguments of a natbib or jurabib citation command
1401 void get_cite_arguments(Parser & p, bool natbibOrder,
1402 string & before, string & after)
1404 // We need to distinguish "" and "[]", so we can't use p.getOpt().
1406 // text before the citation
1408 // text after the citation
1409 after = p.getFullOpt();
1411 if (!after.empty()) {
1412 before = p.getFullOpt();
1413 if (natbibOrder && !before.empty())
1414 swap(before, after);
1419 /// Convert filenames with TeX macros and/or quotes to something LyX
1421 string const normalize_filename(string const & name)
1423 Parser p(trim(name, "\""));
1426 Token const & t = p.get_token();
1427 if (t.cat() != catEscape)
1429 else if (t.cs() == "lyxdot") {
1430 // This is used by LyX for simple dots in relative
1434 } else if (t.cs() == "space") {
1444 /// Convert \p name from TeX convention (relative to master file) to LyX
1445 /// convention (relative to .lyx file) if it is relative
1446 void fix_relative_filename(string & name)
1448 if (FileName::isAbsolute(name))
1451 name = to_utf8(makeRelPath(from_utf8(makeAbsPath(name, getMasterFilePath()).absFileName()),
1452 from_utf8(getParentFilePath())));
1456 /// Parse a NoWeb Scrap section. The initial "<<" is already parsed.
1457 void parse_noweb(Parser & p, ostream & os, Context & context)
1459 // assemble the rest of the keyword
1463 Token const & t = p.get_token();
1464 if (t.asInput() == ">" && p.next_token().asInput() == ">") {
1467 scrap = (p.good() && p.next_token().asInput() == "=");
1469 name += p.get_token().asInput();
1472 name += t.asInput();
1475 if (!scrap || !context.new_layout_allowed ||
1476 !context.textclass.hasLayout(from_ascii("Scrap"))) {
1477 cerr << "Warning: Could not interpret '" << name
1478 << "'. Ignoring it." << endl;
1482 // We use new_paragraph instead of check_end_layout because the stuff
1483 // following the noweb chunk needs to start with a \begin_layout.
1484 // This may create a new paragraph even if there was none in the
1485 // noweb file, but the alternative is an invalid LyX file. Since
1486 // noweb code chunks are implemented with a layout style in LyX they
1487 // always must be in an own paragraph.
1488 context.new_paragraph(os);
1489 Context newcontext(true, context.textclass,
1490 &context.textclass[from_ascii("Scrap")]);
1491 newcontext.check_layout(os);
1494 Token const & t = p.get_token();
1495 // We abuse the parser a bit, because this is no TeX syntax
1497 if (t.cat() == catEscape)
1498 os << subst(t.asInput(), "\\", "\n\\backslash\n");
1501 Context tmp(false, context.textclass,
1502 &context.textclass[from_ascii("Scrap")]);
1503 tmp.need_end_layout = true;
1504 tmp.check_layout(oss);
1505 os << subst(t.asInput(), "\n", oss.str());
1507 // The scrap chunk is ended by an @ at the beginning of a line.
1508 // After the @ the line may contain a comment and/or
1509 // whitespace, but nothing else.
1510 if (t.asInput() == "@" && p.prev_token().cat() == catNewline &&
1511 (p.next_token().cat() == catSpace ||
1512 p.next_token().cat() == catNewline ||
1513 p.next_token().cat() == catComment)) {
1514 while (p.good() && p.next_token().cat() == catSpace)
1515 os << p.get_token().asInput();
1516 if (p.next_token().cat() == catComment)
1517 // The comment includes a final '\n'
1518 os << p.get_token().asInput();
1520 if (p.next_token().cat() == catNewline)
1527 newcontext.check_end_layout(os);
1531 /// detects \\def, \\long\\def and \\global\\long\\def with ws and comments
1532 bool is_macro(Parser & p)
1534 Token first = p.curr_token();
1535 if (first.cat() != catEscape || !p.good())
1537 if (first.cs() == "def")
1539 if (first.cs() != "global" && first.cs() != "long")
1541 Token second = p.get_token();
1543 while (p.good() && !p.isParagraph() && (second.cat() == catSpace ||
1544 second.cat() == catNewline || second.cat() == catComment)) {
1545 second = p.get_token();
1548 bool secondvalid = second.cat() == catEscape;
1550 bool thirdvalid = false;
1551 if (p.good() && first.cs() == "global" && secondvalid &&
1552 second.cs() == "long") {
1553 third = p.get_token();
1555 while (p.good() && !p.isParagraph() &&
1556 (third.cat() == catSpace ||
1557 third.cat() == catNewline ||
1558 third.cat() == catComment)) {
1559 third = p.get_token();
1562 thirdvalid = third.cat() == catEscape;
1564 for (int i = 0; i < pos; ++i)
1569 return (first.cs() == "global" || first.cs() == "long") &&
1570 second.cs() == "def";
1571 return first.cs() == "global" && second.cs() == "long" &&
1572 third.cs() == "def";
1576 /// Parse a macro definition (assumes that is_macro() returned true)
1577 void parse_macro(Parser & p, ostream & os, Context & context)
1579 context.check_layout(os);
1580 Token first = p.curr_token();
1583 string command = first.asInput();
1584 if (first.cs() != "def") {
1586 eat_whitespace(p, os, context, false);
1587 second = p.curr_token();
1588 command += second.asInput();
1589 if (second.cs() != "def") {
1591 eat_whitespace(p, os, context, false);
1592 third = p.curr_token();
1593 command += third.asInput();
1596 eat_whitespace(p, os, context, false);
1597 string const name = p.get_token().cs();
1598 eat_whitespace(p, os, context, false);
1604 while (p.next_token().cat() != catBegin) {
1605 if (p.next_token().cat() == catParameter) {
1610 // followed by number?
1611 if (p.next_token().cat() == catOther) {
1612 char c = p.getChar();
1614 // number = current arity + 1?
1615 if (c == arity + '0' + 1)
1620 paramtext += p.get_token().cs();
1622 paramtext += p.get_token().cs();
1627 // only output simple (i.e. compatible) macro as FormulaMacros
1628 string ert = '\\' + name + ' ' + paramtext + '{' + p.verbatim_item() + '}';
1630 context.check_layout(os);
1631 begin_inset(os, "FormulaMacro");
1632 os << "\n\\def" << ert;
1635 handle_ert(os, command + ert, context);
1638 } // anonymous namespace
1641 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
1644 Layout const * newlayout = 0;
1645 InsetLayout const * newinsetlayout = 0;
1646 // Store the latest bibliographystyle and nocite{*} option
1647 // (needed for bibtex inset)
1649 string bibliographystyle;
1650 bool const use_natbib = used_packages.find("natbib") != used_packages.end();
1651 bool const use_jurabib = used_packages.find("jurabib") != used_packages.end();
1654 Token const & t = p.get_token();
1657 debugToken(cerr, t, flags);
1660 if (flags & FLAG_ITEM) {
1661 if (t.cat() == catSpace)
1664 flags &= ~FLAG_ITEM;
1665 if (t.cat() == catBegin) {
1666 // skip the brace and collect everything to the next matching
1668 flags |= FLAG_BRACE_LAST;
1672 // handle only this single token, leave the loop if done
1673 flags |= FLAG_LEAVE;
1676 if (t.cat() != catEscape && t.character() == ']' &&
1677 (flags & FLAG_BRACK_LAST))
1679 if (t.cat() == catEnd && (flags & FLAG_BRACE_LAST))
1682 // If there is anything between \end{env} and \begin{env} we
1683 // don't need to output a separator.
1684 if (t.cat() != catSpace && t.cat() != catNewline &&
1685 t.asInput() != "\\begin")
1691 if (t.cat() == catMath) {
1692 // we are inside some text mode thingy, so opening new math is allowed
1693 context.check_layout(os);
1694 begin_inset(os, "Formula ");
1695 Token const & n = p.get_token();
1696 if (n.cat() == catMath && outer) {
1697 // TeX's $$...$$ syntax for displayed math
1699 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
1701 p.get_token(); // skip the second '$' token
1703 // simple $...$ stuff
1706 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
1712 else if (t.cat() == catSuper || t.cat() == catSub)
1713 cerr << "catcode " << t << " illegal in text mode\n";
1715 // Basic support for english quotes. This should be
1716 // extended to other quotes, but is not so easy (a
1717 // left english quote is the same as a right german
1719 else if (t.asInput() == "`" && p.next_token().asInput() == "`") {
1720 context.check_layout(os);
1721 begin_inset(os, "Quotes ");
1727 else if (t.asInput() == "'" && p.next_token().asInput() == "'") {
1728 context.check_layout(os);
1729 begin_inset(os, "Quotes ");
1736 else if (t.asInput() == ">" && p.next_token().asInput() == ">") {
1737 context.check_layout(os);
1738 begin_inset(os, "Quotes ");
1745 else if (t.asInput() == "<" && p.next_token().asInput() == "<") {
1746 context.check_layout(os);
1747 begin_inset(os, "Quotes ");
1754 else if (t.asInput() == "<"
1755 && p.next_token().asInput() == "<" && noweb_mode) {
1757 parse_noweb(p, os, context);
1760 else if (t.cat() == catSpace || (t.cat() == catNewline && ! p.isParagraph()))
1761 check_space(p, os, context);
1763 else if (t.character() == '[' && noweb_mode &&
1764 p.next_token().character() == '[') {
1765 // These can contain underscores
1767 string const s = p.getFullOpt() + ']';
1768 if (p.next_token().character() == ']')
1771 cerr << "Warning: Inserting missing ']' in '"
1772 << s << "'." << endl;
1773 handle_ert(os, s, context);
1776 else if (t.cat() == catLetter) {
1777 context.check_layout(os);
1778 // Workaround for bug 4752.
1779 // FIXME: This whole code block needs to be removed
1780 // when the bug is fixed and tex2lyx produces
1781 // the updated file format.
1782 // The replacement algorithm in LyX is so stupid that
1783 // it even translates a phrase if it is part of a word.
1784 bool handled = false;
1785 for (int const * l = known_phrase_lengths; *l; ++l) {
1786 string phrase = t.cs();
1787 for (int i = 1; i < *l && p.next_token().isAlnumASCII(); ++i)
1788 phrase += p.get_token().cs();
1789 if (is_known(phrase, known_coded_phrases)) {
1790 handle_ert(os, phrase, context);
1794 for (size_t i = 1; i < phrase.length(); ++i)
1802 else if (t.cat() == catOther ||
1803 t.cat() == catAlign ||
1804 t.cat() == catParameter) {
1805 // This translates "&" to "\\&" which may be wrong...
1806 context.check_layout(os);
1810 else if (p.isParagraph()) {
1811 if (context.new_layout_allowed)
1812 context.new_paragraph(os);
1814 handle_ert(os, "\\par ", context);
1815 eat_whitespace(p, os, context, true);
1818 else if (t.cat() == catActive) {
1819 context.check_layout(os);
1820 if (t.character() == '~') {
1821 if (context.layout->free_spacing)
1824 begin_inset(os, "space ~\n");
1831 else if (t.cat() == catBegin &&
1832 p.next_token().cat() == catEnd) {
1834 Token const prev = p.prev_token();
1836 if (p.next_token().character() == '`' ||
1837 (prev.character() == '-' &&
1838 p.next_token().character() == '-'))
1839 ; // ignore it in {}`` or -{}-
1841 handle_ert(os, "{}", context);
1845 else if (t.cat() == catBegin) {
1846 context.check_layout(os);
1847 // special handling of font attribute changes
1848 Token const prev = p.prev_token();
1849 Token const next = p.next_token();
1850 TeXFont const oldFont = context.font;
1851 if (next.character() == '[' ||
1852 next.character() == ']' ||
1853 next.character() == '*') {
1855 if (p.next_token().cat() == catEnd) {
1860 handle_ert(os, "{", context);
1861 parse_text_snippet(p, os,
1864 handle_ert(os, "}", context);
1866 } else if (! context.new_layout_allowed) {
1867 handle_ert(os, "{", context);
1868 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1870 handle_ert(os, "}", context);
1871 } else if (is_known(next.cs(), known_sizes)) {
1872 // next will change the size, so we must
1874 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1876 if (!context.atParagraphStart())
1878 << context.font.size << "\n";
1879 } else if (is_known(next.cs(), known_font_families)) {
1880 // next will change the font family, so we
1881 // must reset it here
1882 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1884 if (!context.atParagraphStart())
1886 << context.font.family << "\n";
1887 } else if (is_known(next.cs(), known_font_series)) {
1888 // next will change the font series, so we
1889 // must reset it here
1890 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1892 if (!context.atParagraphStart())
1894 << context.font.series << "\n";
1895 } else if (is_known(next.cs(), known_font_shapes)) {
1896 // next will change the font shape, so we
1897 // must reset it here
1898 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1900 if (!context.atParagraphStart())
1902 << context.font.shape << "\n";
1903 } else if (is_known(next.cs(), known_old_font_families) ||
1904 is_known(next.cs(), known_old_font_series) ||
1905 is_known(next.cs(), known_old_font_shapes)) {
1906 // next will change the font family, series
1907 // and shape, so we must reset it here
1908 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1910 if (!context.atParagraphStart())
1912 << context.font.family
1914 << context.font.series
1916 << context.font.shape << "\n";
1918 handle_ert(os, "{", context);
1919 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1921 handle_ert(os, "}", context);
1925 else if (t.cat() == catEnd) {
1926 if (flags & FLAG_BRACE_LAST) {
1929 cerr << "stray '}' in text\n";
1930 handle_ert(os, "}", context);
1933 else if (t.cat() == catComment)
1934 parse_comment(p, os, t, context);
1937 // control sequences
1940 else if (t.cs() == "(") {
1941 context.check_layout(os);
1942 begin_inset(os, "Formula");
1944 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
1949 else if (t.cs() == "[") {
1950 context.check_layout(os);
1951 begin_inset(os, "Formula");
1953 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
1958 else if (t.cs() == "begin")
1959 parse_environment(p, os, outer, last_env, context);
1961 else if (t.cs() == "end") {
1962 if (flags & FLAG_END) {
1963 // eat environment name
1964 string const name = p.getArg('{', '}');
1965 if (name != active_environment())
1966 cerr << "\\end{" + name + "} does not match \\begin{"
1967 + active_environment() + "}\n";
1970 p.error("found 'end' unexpectedly");
1973 else if (t.cs() == "item") {
1976 bool optarg = false;
1977 if (p.next_token().cat() != catEscape &&
1978 p.next_token().character() == '[') {
1979 p.get_token(); // eat '['
1980 s = parse_text_snippet(p, FLAG_BRACK_LAST,
1985 context.check_layout(os);
1986 if (context.has_item) {
1987 // An item in an unknown list-like environment
1988 // FIXME: Do this in check_layout()!
1989 context.has_item = false;
1991 handle_ert(os, "\\item", context);
1993 handle_ert(os, "\\item ", context);
1996 if (context.layout->labeltype != LABEL_MANUAL) {
1997 // LyX does not support \item[\mybullet]
1998 // in itemize environments
1999 handle_ert(os, "[", context);
2001 handle_ert(os, "]", context);
2002 } else if (!s.empty()) {
2003 // The space is needed to separate the
2004 // item from the rest of the sentence.
2006 eat_whitespace(p, os, context, false);
2011 else if (t.cs() == "bibitem") {
2013 context.check_layout(os);
2014 string label = convert_command_inset_arg(p.getArg('[', ']'));
2015 string key = convert_command_inset_arg(p.verbatim_item());
2016 if (contains(label, '\\') || contains(key, '\\')) {
2017 // LyX can't handle LaTeX commands in labels or keys
2018 handle_ert(os, t.asInput() + '[' + label +
2019 "]{" + p.verbatim_item() + '}',
2022 begin_command_inset(os, "bibitem", "bibitem");
2023 os << "label \"" << label << "\"\n"
2024 "key \"" << key << "\"\n";
2029 else if (is_macro(p))
2030 parse_macro(p, os, context);
2032 else if (t.cs() == "noindent") {
2034 context.add_par_extra_stuff("\\noindent\n");
2037 else if (t.cs() == "appendix") {
2038 context.add_par_extra_stuff("\\start_of_appendix\n");
2039 // We need to start a new paragraph. Otherwise the
2040 // appendix in 'bla\appendix\chapter{' would start
2042 context.new_paragraph(os);
2043 // We need to make sure that the paragraph is
2044 // generated even if it is empty. Otherwise the
2045 // appendix in '\par\appendix\par\chapter{' would
2047 context.check_layout(os);
2048 // FIXME: This is a hack to prevent paragraph
2049 // deletion if it is empty. Handle this better!
2051 "%dummy comment inserted by tex2lyx to "
2052 "ensure that this paragraph is not empty",
2054 // Both measures above may generate an additional
2055 // empty paragraph, but that does not hurt, because
2056 // whitespace does not matter here.
2057 eat_whitespace(p, os, context, true);
2060 // Starred section headings
2061 // Must attempt to parse "Section*" before "Section".
2062 else if ((p.next_token().asInput() == "*") &&
2063 context.new_layout_allowed &&
2064 (newlayout = findLayout(context.textclass, t.cs() + '*', true))) {
2067 output_command_layout(os, p, outer, context, newlayout);
2071 // Section headings and the like
2072 else if (context.new_layout_allowed &&
2073 (newlayout = findLayout(context.textclass, t.cs(), true))) {
2075 output_command_layout(os, p, outer, context, newlayout);
2079 else if (t.cs() == "caption") {
2081 context.check_layout(os);
2083 begin_inset(os, "Caption\n\n");
2084 Context newcontext(true, context.textclass);
2085 newcontext.font = context.font;
2086 newcontext.check_layout(os);
2087 if (p.next_token().cat() != catEscape &&
2088 p.next_token().character() == '[') {
2089 p.get_token(); // eat '['
2090 begin_inset(os, "Argument\n");
2091 os << "status collapsed\n";
2092 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
2094 eat_whitespace(p, os, context, false);
2096 parse_text(p, os, FLAG_ITEM, outer, context);
2097 context.check_end_layout(os);
2098 // We don't need really a new paragraph, but
2099 // we must make sure that the next item gets a \begin_layout.
2100 context.new_paragraph(os);
2103 newcontext.check_end_layout(os);
2106 else if (t.cs() == "includegraphics") {
2107 bool const clip = p.next_token().asInput() == "*";
2110 string const arg = p.getArg('[', ']');
2111 map<string, string> opts;
2112 vector<string> keys;
2113 split_map(arg, opts, keys);
2115 opts["clip"] = string();
2116 string name = normalize_filename(p.verbatim_item());
2118 string const path = getMasterFilePath();
2119 // We want to preserve relative / absolute filenames,
2120 // therefore path is only used for testing
2121 if (!makeAbsPath(name, path).exists()) {
2122 // The file extension is probably missing.
2123 // Now try to find it out.
2124 string const dvips_name =
2125 find_file(name, path,
2126 known_dvips_graphics_formats);
2127 string const pdftex_name =
2128 find_file(name, path,
2129 known_pdftex_graphics_formats);
2130 if (!dvips_name.empty()) {
2131 if (!pdftex_name.empty()) {
2132 cerr << "This file contains the "
2134 "\"\\includegraphics{"
2136 "However, files\n\""
2137 << dvips_name << "\" and\n\""
2138 << pdftex_name << "\"\n"
2139 "both exist, so I had to make a "
2140 "choice and took the first one.\n"
2141 "Please move the unwanted one "
2142 "someplace else and try again\n"
2143 "if my choice was wrong."
2147 } else if (!pdftex_name.empty()) {
2153 if (makeAbsPath(name, path).exists())
2154 fix_relative_filename(name);
2156 cerr << "Warning: Could not find graphics file '"
2157 << name << "'." << endl;
2159 context.check_layout(os);
2160 begin_inset(os, "Graphics ");
2161 os << "\n\tfilename " << name << '\n';
2162 if (opts.find("width") != opts.end())
2164 << translate_len(opts["width"]) << '\n';
2165 if (opts.find("height") != opts.end())
2167 << translate_len(opts["height"]) << '\n';
2168 if (opts.find("scale") != opts.end()) {
2169 istringstream iss(opts["scale"]);
2173 os << "\tscale " << val << '\n';
2175 if (opts.find("angle") != opts.end()) {
2176 os << "\trotateAngle "
2177 << opts["angle"] << '\n';
2178 vector<string>::const_iterator a =
2179 find(keys.begin(), keys.end(), "angle");
2180 vector<string>::const_iterator s =
2181 find(keys.begin(), keys.end(), "width");
2182 if (s == keys.end())
2183 s = find(keys.begin(), keys.end(), "height");
2184 if (s == keys.end())
2185 s = find(keys.begin(), keys.end(), "scale");
2186 if (s != keys.end() && distance(s, a) > 0)
2187 os << "\tscaleBeforeRotation\n";
2189 if (opts.find("origin") != opts.end()) {
2191 string const opt = opts["origin"];
2192 if (opt.find('l') != string::npos) ss << "left";
2193 if (opt.find('r') != string::npos) ss << "right";
2194 if (opt.find('c') != string::npos) ss << "center";
2195 if (opt.find('t') != string::npos) ss << "Top";
2196 if (opt.find('b') != string::npos) ss << "Bottom";
2197 if (opt.find('B') != string::npos) ss << "Baseline";
2198 if (!ss.str().empty())
2199 os << "\trotateOrigin " << ss.str() << '\n';
2201 cerr << "Warning: Ignoring unknown includegraphics origin argument '" << opt << "'\n";
2203 if (opts.find("keepaspectratio") != opts.end())
2204 os << "\tkeepAspectRatio\n";
2205 if (opts.find("clip") != opts.end())
2207 if (opts.find("draft") != opts.end())
2209 if (opts.find("bb") != opts.end())
2210 os << "\tBoundingBox "
2211 << opts["bb"] << '\n';
2212 int numberOfbbOptions = 0;
2213 if (opts.find("bbllx") != opts.end())
2214 numberOfbbOptions++;
2215 if (opts.find("bblly") != opts.end())
2216 numberOfbbOptions++;
2217 if (opts.find("bburx") != opts.end())
2218 numberOfbbOptions++;
2219 if (opts.find("bbury") != opts.end())
2220 numberOfbbOptions++;
2221 if (numberOfbbOptions == 4)
2222 os << "\tBoundingBox "
2223 << opts["bbllx"] << " " << opts["bblly"] << " "
2224 << opts["bburx"] << " " << opts["bbury"] << '\n';
2225 else if (numberOfbbOptions > 0)
2226 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
2227 numberOfbbOptions = 0;
2228 if (opts.find("natwidth") != opts.end())
2229 numberOfbbOptions++;
2230 if (opts.find("natheight") != opts.end())
2231 numberOfbbOptions++;
2232 if (numberOfbbOptions == 2)
2233 os << "\tBoundingBox 0bp 0bp "
2234 << opts["natwidth"] << " " << opts["natheight"] << '\n';
2235 else if (numberOfbbOptions > 0)
2236 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
2237 ostringstream special;
2238 if (opts.find("hiresbb") != opts.end())
2239 special << "hiresbb,";
2240 if (opts.find("trim") != opts.end())
2242 if (opts.find("viewport") != opts.end())
2243 special << "viewport=" << opts["viewport"] << ',';
2244 if (opts.find("totalheight") != opts.end())
2245 special << "totalheight=" << opts["totalheight"] << ',';
2246 if (opts.find("type") != opts.end())
2247 special << "type=" << opts["type"] << ',';
2248 if (opts.find("ext") != opts.end())
2249 special << "ext=" << opts["ext"] << ',';
2250 if (opts.find("read") != opts.end())
2251 special << "read=" << opts["read"] << ',';
2252 if (opts.find("command") != opts.end())
2253 special << "command=" << opts["command"] << ',';
2254 string s_special = special.str();
2255 if (!s_special.empty()) {
2256 // We had special arguments. Remove the trailing ','.
2257 os << "\tspecial " << s_special.substr(0, s_special.size() - 1) << '\n';
2259 // TODO: Handle the unknown settings better.
2260 // Warn about invalid options.
2261 // Check whether some option was given twice.
2265 else if (t.cs() == "footnote" ||
2266 (t.cs() == "thanks" && context.layout->intitle)) {
2268 context.check_layout(os);
2269 begin_inset(os, "Foot\n");
2270 os << "status collapsed\n\n";
2271 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
2275 else if (t.cs() == "marginpar") {
2277 context.check_layout(os);
2278 begin_inset(os, "Marginal\n");
2279 os << "status collapsed\n\n";
2280 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
2284 else if (t.cs() == "ensuremath") {
2286 context.check_layout(os);
2287 string const s = p.verbatim_item();
2288 //FIXME: this never triggers in UTF8
2289 if (s == "\xb1" || s == "\xb3" || s == "\xb2" || s == "\xb5")
2292 handle_ert(os, "\\ensuremath{" + s + "}",
2296 else if (t.cs() == "makeindex" || t.cs() == "maketitle") {
2297 // FIXME: Somehow prevent title layouts if
2298 // "maketitle" was not found
2300 skip_spaces_braces(p);
2303 else if (t.cs() == "tableofcontents") {
2304 context.check_layout(os);
2305 begin_command_inset(os, "toc", "tableofcontents");
2307 skip_spaces_braces(p);
2310 else if (t.cs() == "listoffigures") {
2311 context.check_layout(os);
2312 begin_inset(os, "FloatList figure\n");
2314 skip_spaces_braces(p);
2317 else if (t.cs() == "listoftables") {
2318 context.check_layout(os);
2319 begin_inset(os, "FloatList table\n");
2321 skip_spaces_braces(p);
2324 else if (t.cs() == "listof") {
2325 p.skip_spaces(true);
2326 string const name = p.get_token().cs();
2327 if (context.textclass.floats().typeExist(name)) {
2328 context.check_layout(os);
2329 begin_inset(os, "FloatList ");
2332 p.get_token(); // swallow second arg
2334 handle_ert(os, "\\listof{" + name + "}", context);
2337 else if (t.cs() == "textrm")
2338 parse_text_attributes(p, os, FLAG_ITEM, outer,
2339 context, "\\family",
2340 context.font.family, "roman");
2342 else if (t.cs() == "textsf")
2343 parse_text_attributes(p, os, FLAG_ITEM, outer,
2344 context, "\\family",
2345 context.font.family, "sans");
2347 else if (t.cs() == "texttt")
2348 parse_text_attributes(p, os, FLAG_ITEM, outer,
2349 context, "\\family",
2350 context.font.family, "typewriter");
2352 else if (t.cs() == "textmd")
2353 parse_text_attributes(p, os, FLAG_ITEM, outer,
2354 context, "\\series",
2355 context.font.series, "medium");
2357 else if (t.cs() == "textbf")
2358 parse_text_attributes(p, os, FLAG_ITEM, outer,
2359 context, "\\series",
2360 context.font.series, "bold");
2362 else if (t.cs() == "textup")
2363 parse_text_attributes(p, os, FLAG_ITEM, outer,
2365 context.font.shape, "up");
2367 else if (t.cs() == "textit")
2368 parse_text_attributes(p, os, FLAG_ITEM, outer,
2370 context.font.shape, "italic");
2372 else if (t.cs() == "textsl")
2373 parse_text_attributes(p, os, FLAG_ITEM, outer,
2375 context.font.shape, "slanted");
2377 else if (t.cs() == "textsc")
2378 parse_text_attributes(p, os, FLAG_ITEM, outer,
2380 context.font.shape, "smallcaps");
2382 else if (t.cs() == "textnormal" || t.cs() == "normalfont") {
2383 context.check_layout(os);
2384 TeXFont oldFont = context.font;
2385 context.font.init();
2386 context.font.size = oldFont.size;
2387 os << "\n\\family " << context.font.family << "\n";
2388 os << "\n\\series " << context.font.series << "\n";
2389 os << "\n\\shape " << context.font.shape << "\n";
2390 if (t.cs() == "textnormal") {
2391 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
2392 output_font_change(os, context.font, oldFont);
2393 context.font = oldFont;
2395 eat_whitespace(p, os, context, false);
2398 else if (t.cs() == "textcolor") {
2399 // scheme is \textcolor{color name}{text}
2400 string const color = p.verbatim_item();
2401 // we only support the predefined colors of the color package
2402 if (color == "black" || color == "blue" || color == "cyan"
2403 || color == "green" || color == "magenta" || color == "red"
2404 || color == "white" || color == "yellow") {
2405 context.check_layout(os);
2406 os << "\n\\color " << color << "\n";
2407 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
2408 context.check_layout(os);
2409 os << "\n\\color inherit\n";
2411 // for custom defined colors
2412 handle_ert(os, t.asInput() + "{" + color + "}", context);
2415 else if (t.cs() == "underbar" || t.cs() == "uline") {
2416 // \underbar is not 100% correct (LyX outputs \uline
2417 // of ulem.sty). The difference is that \ulem allows
2418 // line breaks, and \underbar does not.
2419 // Do NOT handle \underline.
2420 // \underbar cuts through y, g, q, p etc.,
2421 // \underline does not.
2422 context.check_layout(os);
2423 os << "\n\\bar under\n";
2424 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
2425 context.check_layout(os);
2426 os << "\n\\bar default\n";
2429 else if (t.cs() == "sout") {
2430 context.check_layout(os);
2431 os << "\n\\strikeout on\n";
2432 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
2433 context.check_layout(os);
2434 os << "\n\\strikeout default\n";
2437 else if (t.cs() == "uuline" || t.cs() == "uwave" ||
2438 t.cs() == "emph" || t.cs() == "noun") {
2439 context.check_layout(os);
2440 os << "\n\\" << t.cs() << " on\n";
2441 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
2442 context.check_layout(os);
2443 os << "\n\\" << t.cs() << " default\n";
2446 else if (t.cs() == "lyxline") {
2447 // swallow size argument (it is not used anyway)
2449 if (!context.atParagraphStart()) {
2450 // so our line is in the middle of a paragraph
2451 // we need to add a new line, lest this line
2452 // follow the other content on that line and
2453 // run off the side of the page
2454 // FIXME: This may create an empty paragraph,
2455 // but without that it would not be
2456 // possible to set noindent below.
2457 // Fortunately LaTeX does not care
2458 // about the empty paragraph.
2459 context.new_paragraph(os);
2461 if (h_paragraph_separation == "indent") {
2462 // we need to unindent, lest the line be too long
2463 context.add_par_extra_stuff("\\noindent\n");
2465 context.check_layout(os);
2466 begin_command_inset(os, "line", "rule");
2467 os << "offset \"0.5ex\"\n"
2468 "width \"100line%\"\n"
2473 else if (is_known(t.cs(), known_phrases) ||
2474 (t.cs() == "protect" &&
2475 p.next_token().cat() == catEscape &&
2476 is_known(p.next_token().cs(), known_phrases))) {
2477 // LyX sometimes puts a \protect in front, so we have to ignore it
2478 // FIXME: This needs to be changed when bug 4752 is fixed.
2479 char const * const * where = is_known(
2480 t.cs() == "protect" ? p.get_token().cs() : t.cs(),
2482 context.check_layout(os);
2483 os << known_coded_phrases[where - known_phrases];
2484 skip_spaces_braces(p);
2487 else if (is_known(t.cs(), known_ref_commands)) {
2488 string const opt = p.getOpt();
2490 context.check_layout(os);
2491 char const * const * where = is_known(t.cs(),
2492 known_ref_commands);
2493 begin_command_inset(os, "ref",
2494 known_coded_ref_commands[where - known_ref_commands]);
2495 os << "reference \""
2496 << convert_command_inset_arg(p.verbatim_item())
2500 // LyX does not support optional arguments of ref commands
2501 handle_ert(os, t.asInput() + '[' + opt + "]{" +
2502 p.verbatim_item() + "}", context);
2506 else if (use_natbib &&
2507 is_known(t.cs(), known_natbib_commands) &&
2508 ((t.cs() != "citefullauthor" &&
2509 t.cs() != "citeyear" &&
2510 t.cs() != "citeyearpar") ||
2511 p.next_token().asInput() != "*")) {
2512 context.check_layout(os);
2513 string command = t.cs();
2514 if (p.next_token().asInput() == "*") {
2518 if (command == "citefullauthor")
2519 // alternative name for "\\citeauthor*"
2520 command = "citeauthor*";
2522 // text before the citation
2524 // text after the citation
2526 get_cite_arguments(p, true, before, after);
2528 if (command == "cite") {
2529 // \cite without optional argument means
2530 // \citet, \cite with at least one optional
2531 // argument means \citep.
2532 if (before.empty() && after.empty())
2537 if (before.empty() && after == "[]")
2538 // avoid \citet[]{a}
2540 else if (before == "[]" && after == "[]") {
2541 // avoid \citet[][]{a}
2545 // remove the brackets around after and before
2546 if (!after.empty()) {
2548 after.erase(after.length() - 1, 1);
2549 after = convert_command_inset_arg(after);
2551 if (!before.empty()) {
2553 before.erase(before.length() - 1, 1);
2554 before = convert_command_inset_arg(before);
2556 begin_command_inset(os, "citation", command);
2557 os << "after " << '"' << after << '"' << "\n";
2558 os << "before " << '"' << before << '"' << "\n";
2560 << convert_command_inset_arg(p.verbatim_item())
2565 else if (use_jurabib &&
2566 is_known(t.cs(), known_jurabib_commands) &&
2567 (t.cs() == "cite" || p.next_token().asInput() != "*")) {
2568 context.check_layout(os);
2569 string command = t.cs();
2570 if (p.next_token().asInput() == "*") {
2574 char argumentOrder = '\0';
2575 vector<string> const & options = used_packages["jurabib"];
2576 if (find(options.begin(), options.end(),
2577 "natbiborder") != options.end())
2578 argumentOrder = 'n';
2579 else if (find(options.begin(), options.end(),
2580 "jurabiborder") != options.end())
2581 argumentOrder = 'j';
2583 // text before the citation
2585 // text after the citation
2587 get_cite_arguments(p, argumentOrder != 'j', before, after);
2589 string const citation = p.verbatim_item();
2590 if (!before.empty() && argumentOrder == '\0') {
2591 cerr << "Warning: Assuming argument order "
2592 "of jurabib version 0.6 for\n'"
2593 << command << before << after << '{'
2594 << citation << "}'.\n"
2595 "Add 'jurabiborder' to the jurabib "
2596 "package options if you used an\n"
2597 "earlier jurabib version." << endl;
2599 if (!after.empty()) {
2601 after.erase(after.length() - 1, 1);
2603 if (!before.empty()) {
2605 before.erase(before.length() - 1, 1);
2607 begin_command_inset(os, "citation", command);
2608 os << "after " << '"' << after << '"' << "\n";
2609 os << "before " << '"' << before << '"' << "\n";
2610 os << "key " << '"' << citation << '"' << "\n";
2614 else if (t.cs() == "cite"
2615 || t.cs() == "nocite") {
2616 context.check_layout(os);
2617 string after = convert_command_inset_arg(p.getArg('[', ']'));
2618 string key = convert_command_inset_arg(p.verbatim_item());
2619 // store the case that it is "\nocite{*}" to use it later for
2622 begin_command_inset(os, "citation", t.cs());
2623 os << "after " << '"' << after << '"' << "\n";
2624 os << "key " << '"' << key << '"' << "\n";
2626 } else if (t.cs() == "nocite")
2630 else if (t.cs() == "index") {
2631 context.check_layout(os);
2632 begin_inset(os, "Index idx\n");
2633 os << "status collapsed\n";
2634 parse_text_in_inset(p, os, FLAG_ITEM, false, context, "Index");
2638 else if (t.cs() == "nomenclature") {
2639 context.check_layout(os);
2640 begin_command_inset(os, "nomenclature", "nomenclature");
2641 string prefix = convert_command_inset_arg(p.getArg('[', ']'));
2642 if (!prefix.empty())
2643 os << "prefix " << '"' << prefix << '"' << "\n";
2644 os << "symbol " << '"'
2645 << convert_command_inset_arg(p.verbatim_item());
2646 os << "\"\ndescription \""
2647 << convert_command_inset_arg(p.verbatim_item())
2652 else if (t.cs() == "label") {
2653 context.check_layout(os);
2654 begin_command_inset(os, "label", "label");
2656 << convert_command_inset_arg(p.verbatim_item())
2661 else if (t.cs() == "printindex") {
2662 context.check_layout(os);
2663 begin_command_inset(os, "index_print", "printindex");
2664 os << "type \"idx\"\n";
2666 skip_spaces_braces(p);
2669 else if (t.cs() == "printnomenclature") {
2670 context.check_layout(os);
2671 begin_command_inset(os, "nomencl_print", "printnomenclature");
2673 skip_spaces_braces(p);
2676 else if ((t.cs() == "textsuperscript" || t.cs() == "textsubscript")) {
2677 context.check_layout(os);
2678 begin_inset(os, "script ");
2679 os << t.cs().substr(4) << '\n';
2680 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
2684 else if (is_known(t.cs(), known_quotes)) {
2685 char const * const * where = is_known(t.cs(), known_quotes);
2686 context.check_layout(os);
2687 begin_inset(os, "Quotes ");
2688 os << known_coded_quotes[where - known_quotes];
2690 // LyX adds {} after the quote, so we have to eat
2691 // spaces here if there are any before a possible
2693 eat_whitespace(p, os, context, false);
2697 else if (is_known(t.cs(), known_sizes) &&
2698 context.new_layout_allowed) {
2699 char const * const * where = is_known(t.cs(), known_sizes);
2700 context.check_layout(os);
2701 TeXFont const oldFont = context.font;
2702 context.font.size = known_coded_sizes[where - known_sizes];
2703 output_font_change(os, oldFont, context.font);
2704 eat_whitespace(p, os, context, false);
2707 else if (is_known(t.cs(), known_font_families) &&
2708 context.new_layout_allowed) {
2709 char const * const * where =
2710 is_known(t.cs(), known_font_families);
2711 context.check_layout(os);
2712 TeXFont const oldFont = context.font;
2713 context.font.family =
2714 known_coded_font_families[where - known_font_families];
2715 output_font_change(os, oldFont, context.font);
2716 eat_whitespace(p, os, context, false);
2719 else if (is_known(t.cs(), known_font_series) &&
2720 context.new_layout_allowed) {
2721 char const * const * where =
2722 is_known(t.cs(), known_font_series);
2723 context.check_layout(os);
2724 TeXFont const oldFont = context.font;
2725 context.font.series =
2726 known_coded_font_series[where - known_font_series];
2727 output_font_change(os, oldFont, context.font);
2728 eat_whitespace(p, os, context, false);
2731 else if (is_known(t.cs(), known_font_shapes) &&
2732 context.new_layout_allowed) {
2733 char const * const * where =
2734 is_known(t.cs(), known_font_shapes);
2735 context.check_layout(os);
2736 TeXFont const oldFont = context.font;
2737 context.font.shape =
2738 known_coded_font_shapes[where - known_font_shapes];
2739 output_font_change(os, oldFont, context.font);
2740 eat_whitespace(p, os, context, false);
2742 else if (is_known(t.cs(), known_old_font_families) &&
2743 context.new_layout_allowed) {
2744 char const * const * where =
2745 is_known(t.cs(), known_old_font_families);
2746 context.check_layout(os);
2747 TeXFont const oldFont = context.font;
2748 context.font.init();
2749 context.font.size = oldFont.size;
2750 context.font.family =
2751 known_coded_font_families[where - known_old_font_families];
2752 output_font_change(os, oldFont, context.font);
2753 eat_whitespace(p, os, context, false);
2756 else if (is_known(t.cs(), known_old_font_series) &&
2757 context.new_layout_allowed) {
2758 char const * const * where =
2759 is_known(t.cs(), known_old_font_series);
2760 context.check_layout(os);
2761 TeXFont const oldFont = context.font;
2762 context.font.init();
2763 context.font.size = oldFont.size;
2764 context.font.series =
2765 known_coded_font_series[where - known_old_font_series];
2766 output_font_change(os, oldFont, context.font);
2767 eat_whitespace(p, os, context, false);
2770 else if (is_known(t.cs(), known_old_font_shapes) &&
2771 context.new_layout_allowed) {
2772 char const * const * where =
2773 is_known(t.cs(), known_old_font_shapes);
2774 context.check_layout(os);
2775 TeXFont const oldFont = context.font;
2776 context.font.init();
2777 context.font.size = oldFont.size;
2778 context.font.shape =
2779 known_coded_font_shapes[where - known_old_font_shapes];
2780 output_font_change(os, oldFont, context.font);
2781 eat_whitespace(p, os, context, false);
2784 else if (t.cs() == "selectlanguage") {
2785 context.check_layout(os);
2786 // save the language for the case that a
2787 // \foreignlanguage is used
2789 context.font.language = babel2lyx(p.verbatim_item());
2790 os << "\n\\lang " << context.font.language << "\n";
2793 else if (t.cs() == "foreignlanguage") {
2794 string const lang = babel2lyx(p.verbatim_item());
2795 parse_text_attributes(p, os, FLAG_ITEM, outer,
2797 context.font.language, lang);
2800 else if (t.cs() == "inputencoding") {
2801 // nothing to write here
2802 string const enc = subst(p.verbatim_item(), "\n", " ");
2806 else if (t.cs() == "ldots") {
2807 context.check_layout(os);
2808 os << "\\SpecialChar \\ldots{}\n";
2809 skip_spaces_braces(p);
2812 else if (t.cs() == "lyxarrow") {
2813 context.check_layout(os);
2814 os << "\\SpecialChar \\menuseparator\n";
2815 skip_spaces_braces(p);
2818 else if (t.cs() == "textcompwordmark") {
2819 context.check_layout(os);
2820 os << "\\SpecialChar \\textcompwordmark{}\n";
2821 skip_spaces_braces(p);
2824 else if (t.cs() == "slash") {
2825 context.check_layout(os);
2826 os << "\\SpecialChar \\slash{}\n";
2827 skip_spaces_braces(p);
2830 else if (t.cs() == "nobreakdash" && p.next_token().asInput() == "-") {
2831 context.check_layout(os);
2832 os << "\\SpecialChar \\nobreakdash-\n";
2836 else if (t.cs() == "textquotedbl") {
2837 context.check_layout(os);
2842 else if (t.cs() == "@" && p.next_token().asInput() == ".") {
2843 context.check_layout(os);
2844 os << "\\SpecialChar \\@.\n";
2848 else if (t.cs() == "-") {
2849 context.check_layout(os);
2850 os << "\\SpecialChar \\-\n";
2853 else if (t.cs() == "textasciitilde") {
2854 context.check_layout(os);
2856 skip_spaces_braces(p);
2859 else if (t.cs() == "textasciicircum") {
2860 context.check_layout(os);
2862 skip_spaces_braces(p);
2865 else if (t.cs() == "textbackslash") {
2866 context.check_layout(os);
2867 os << "\n\\backslash\n";
2868 skip_spaces_braces(p);
2871 else if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#"
2872 || t.cs() == "$" || t.cs() == "{" || t.cs() == "}"
2874 context.check_layout(os);
2878 else if (t.cs() == "char") {
2879 context.check_layout(os);
2880 if (p.next_token().character() == '`') {
2882 if (p.next_token().cs() == "\"") {
2887 handle_ert(os, "\\char`", context);
2890 handle_ert(os, "\\char", context);
2894 else if (t.cs() == "verb") {
2895 context.check_layout(os);
2896 char const delimiter = p.next_token().character();
2897 string const arg = p.getArg(delimiter, delimiter);
2899 oss << "\\verb" << delimiter << arg << delimiter;
2900 handle_ert(os, oss.str(), context);
2903 // Problem: \= creates a tabstop inside the tabbing environment
2904 // and else an accent. In the latter case we really would want
2905 // \={o} instead of \= o.
2906 else if (t.cs() == "=" && (flags & FLAG_TABBING))
2907 handle_ert(os, t.asInput(), context);
2909 // accents (see Table 6 in Comprehensive LaTeX Symbol List)
2910 else if (t.cs().size() == 1
2911 && contains("\"'.=^`bcdHkrtuv~", t.cs())) {
2912 context.check_layout(os);
2913 // try to see whether the string is in unicodesymbols
2915 string command = t.asInput() + "{"
2916 + trimSpaceAndEol(p.verbatim_item())
2918 docstring s = encodings.fromLaTeXCommand(from_utf8(command), rem);
2921 cerr << "When parsing " << command
2922 << ", result is " << to_utf8(s)
2923 << "+" << to_utf8(rem) << endl;
2926 // we did not find a non-ert version
2927 handle_ert(os, command, context);
2930 else if (t.cs() == "\\") {
2931 context.check_layout(os);
2933 handle_ert(os, "\\\\" + p.getOpt(), context);
2934 else if (p.next_token().asInput() == "*") {
2936 // getOpt() eats the following space if there
2937 // is no optional argument, but that is OK
2938 // here since it has no effect in the output.
2939 handle_ert(os, "\\\\*" + p.getOpt(), context);
2942 begin_inset(os, "Newline newline");
2947 else if (t.cs() == "newline" ||
2948 (t.cs() == "linebreak" && !p.hasOpt())) {
2949 context.check_layout(os);
2950 begin_inset(os, "Newline ");
2953 skip_spaces_braces(p);
2956 else if (t.cs() == "input" || t.cs() == "include"
2957 || t.cs() == "verbatiminput") {
2958 string name = t.cs();
2959 if (t.cs() == "verbatiminput"
2960 && p.next_token().asInput() == "*")
2961 name += p.get_token().asInput();
2962 context.check_layout(os);
2963 string filename(normalize_filename(p.getArg('{', '}')));
2964 string const path = getMasterFilePath();
2965 // We want to preserve relative / absolute filenames,
2966 // therefore path is only used for testing
2967 if ((t.cs() == "include" || t.cs() == "input") &&
2968 !makeAbsPath(filename, path).exists()) {
2969 // The file extension is probably missing.
2970 // Now try to find it out.
2971 string const tex_name =
2972 find_file(filename, path,
2973 known_tex_extensions);
2974 if (!tex_name.empty())
2975 filename = tex_name;
2977 bool external = false;
2979 if (makeAbsPath(filename, path).exists()) {
2980 string const abstexname =
2981 makeAbsPath(filename, path).absFileName();
2982 string const abslyxname =
2983 changeExtension(abstexname, ".lyx");
2984 string const absfigname =
2985 changeExtension(abstexname, ".fig");
2986 fix_relative_filename(filename);
2987 string const lyxname =
2988 changeExtension(filename, ".lyx");
2990 external = FileName(absfigname).exists();
2991 if (t.cs() == "input") {
2992 string const ext = getExtension(abstexname);
2994 // Combined PS/LaTeX:
2995 // x.eps, x.pstex_t (old xfig)
2996 // x.pstex, x.pstex_t (new xfig, e.g. 3.2.5)
2997 FileName const absepsname(
2998 changeExtension(abstexname, ".eps"));
2999 FileName const abspstexname(
3000 changeExtension(abstexname, ".pstex"));
3001 bool const xfigeps =
3002 (absepsname.exists() ||
3003 abspstexname.exists()) &&
3006 // Combined PDF/LaTeX:
3007 // x.pdf, x.pdftex_t (old xfig)
3008 // x.pdf, x.pdf_t (new xfig, e.g. 3.2.5)
3009 FileName const abspdfname(
3010 changeExtension(abstexname, ".pdf"));
3011 bool const xfigpdf =
3012 abspdfname.exists() &&
3013 (ext == "pdftex_t" || ext == "pdf_t");
3017 // Combined PS/PDF/LaTeX:
3018 // x_pspdftex.eps, x_pspdftex.pdf, x.pspdftex
3019 string const absbase2(
3020 removeExtension(abstexname) + "_pspdftex");
3021 FileName const abseps2name(
3022 addExtension(absbase2, ".eps"));
3023 FileName const abspdf2name(
3024 addExtension(absbase2, ".pdf"));
3025 bool const xfigboth =
3026 abspdf2name.exists() &&
3027 abseps2name.exists() && ext == "pspdftex";
3029 xfig = xfigpdf || xfigeps || xfigboth;
3030 external = external && xfig;
3033 outname = changeExtension(filename, ".fig");
3035 // Don't try to convert, the result
3036 // would be full of ERT.
3038 } else if (t.cs() != "verbatiminput" &&
3039 tex2lyx(abstexname, FileName(abslyxname),
3046 cerr << "Warning: Could not find included file '"
3047 << filename << "'." << endl;
3051 begin_inset(os, "External\n");
3052 os << "\ttemplate XFig\n"
3053 << "\tfilename " << outname << '\n';
3055 begin_command_inset(os, "include", name);
3056 os << "preview false\n"
3057 "filename \"" << outname << "\"\n";
3062 else if (t.cs() == "bibliographystyle") {
3063 // store new bibliographystyle
3064 bibliographystyle = p.verbatim_item();
3065 // If any other command than \bibliography and
3066 // \nocite{*} follows, we need to output the style
3067 // (because it might be used by that command).
3068 // Otherwise, it will automatically be output by LyX.
3071 for (Token t2 = p.get_token(); p.good(); t2 = p.get_token()) {
3072 if (t2.cat() == catBegin)
3074 if (t2.cat() != catEscape)
3076 if (t2.cs() == "nocite") {
3077 if (p.getArg('{', '}') == "*")
3079 } else if (t2.cs() == "bibliography")
3086 "\\bibliographystyle{" + bibliographystyle + '}',
3091 else if (t.cs() == "bibliography") {
3092 context.check_layout(os);
3093 begin_command_inset(os, "bibtex", "bibtex");
3094 if (!btprint.empty()) {
3095 os << "btprint " << '"' << "btPrintAll" << '"' << "\n";
3096 // clear the string because the next BibTeX inset can be without the
3097 // \nocite{*} option
3100 os << "bibfiles " << '"' << p.verbatim_item() << '"' << "\n";
3101 // Do we have a bibliographystyle set?
3102 if (!bibliographystyle.empty())
3103 os << "options " << '"' << bibliographystyle << '"' << "\n";
3107 else if (t.cs() == "parbox")
3108 parse_box(p, os, 0, FLAG_ITEM, outer, context, "", "", t.cs());
3110 else if (t.cs() == "ovalbox" || t.cs() == "Ovalbox" ||
3111 t.cs() == "shadowbox" || t.cs() == "doublebox")
3112 parse_outer_box(p, os, FLAG_ITEM, outer, context, t.cs(), "");
3114 else if (t.cs() == "framebox") {
3115 string special = p.getFullOpt();
3116 special += p.getOpt();
3117 parse_outer_box(p, os, FLAG_ITEM, outer, context, t.cs(), special);
3120 //\makebox() is part of the picture environment and different from \makebox{}
3121 //\makebox{} will be parsed by parse_box when bug 2956 is fixed
3122 else if (t.cs() == "makebox") {
3123 string arg = t.asInput();
3124 if (p.next_token().character() == '(')
3125 //the syntax is: \makebox(x,y)[position]{content}
3126 arg += p.getFullParentheseArg();
3128 //the syntax is: \makebox[width][position]{content}
3129 arg += p.getFullOpt();
3130 handle_ert(os, arg + p.getFullOpt(), context);
3133 else if (t.cs() == "smallskip" ||
3134 t.cs() == "medskip" ||
3135 t.cs() == "bigskip" ||
3136 t.cs() == "vfill") {
3137 context.check_layout(os);
3138 begin_inset(os, "VSpace ");
3141 skip_spaces_braces(p);
3144 else if (is_known(t.cs(), known_spaces)) {
3145 char const * const * where = is_known(t.cs(), known_spaces);
3146 context.check_layout(os);
3147 begin_inset(os, "space ");
3148 os << '\\' << known_coded_spaces[where - known_spaces]
3151 // LaTeX swallows whitespace after all spaces except
3152 // "\\,". We have to do that here, too, because LyX
3153 // adds "{}" which would make the spaces significant.
3155 eat_whitespace(p, os, context, false);
3156 // LyX adds "{}" after all spaces except "\\ " and
3157 // "\\,", so we have to remove "{}".
3158 // "\\,{}" is equivalent to "\\," in LaTeX, so we
3159 // remove the braces after "\\,", too.
3164 else if (t.cs() == "newpage" ||
3165 (t.cs() == "pagebreak" && !p.hasOpt()) ||
3166 t.cs() == "clearpage" ||
3167 t.cs() == "cleardoublepage") {
3168 context.check_layout(os);
3169 begin_inset(os, "Newpage ");
3172 skip_spaces_braces(p);
3175 else if (t.cs() == "DeclareRobustCommand" ||
3176 t.cs() == "DeclareRobustCommandx" ||
3177 t.cs() == "newcommand" ||
3178 t.cs() == "newcommandx" ||
3179 t.cs() == "providecommand" ||
3180 t.cs() == "providecommandx" ||
3181 t.cs() == "renewcommand" ||
3182 t.cs() == "renewcommandx") {
3183 // DeclareRobustCommand, DeclareRobustCommandx,
3184 // providecommand and providecommandx could be handled
3185 // by parse_command(), but we need to call
3186 // add_known_command() here.
3187 string name = t.asInput();
3188 if (p.next_token().asInput() == "*") {
3189 // Starred form. Eat '*'
3193 string const command = p.verbatim_item();
3194 string const opt1 = p.getFullOpt();
3195 string const opt2 = p.getFullOpt();
3196 add_known_command(command, opt1, !opt2.empty());
3197 string const ert = name + '{' + command + '}' +
3199 '{' + p.verbatim_item() + '}';
3201 if (t.cs() == "DeclareRobustCommand" ||
3202 t.cs() == "DeclareRobustCommandx" ||
3203 t.cs() == "providecommand" ||
3204 t.cs() == "providecommandx" ||
3205 name[name.length()-1] == '*')
3206 handle_ert(os, ert, context);
3208 context.check_layout(os);
3209 begin_inset(os, "FormulaMacro");
3215 else if (t.cs() == "let" && p.next_token().asInput() != "*") {
3216 // let could be handled by parse_command(),
3217 // but we need to call add_known_command() here.
3218 string ert = t.asInput();
3221 if (p.next_token().cat() == catBegin) {
3222 name = p.verbatim_item();
3223 ert += '{' + name + '}';
3225 name = p.verbatim_item();
3230 if (p.next_token().cat() == catBegin) {
3231 command = p.verbatim_item();
3232 ert += '{' + command + '}';
3234 command = p.verbatim_item();
3237 // If command is known, make name known too, to parse
3238 // its arguments correctly. For this reason we also
3239 // have commands in syntax.default that are hardcoded.
3240 CommandMap::iterator it = known_commands.find(command);
3241 if (it != known_commands.end())
3242 known_commands[t.asInput()] = it->second;
3243 handle_ert(os, ert, context);
3246 else if (t.cs() == "hspace" || t.cs() == "vspace") {
3247 bool starred = false;
3248 if (p.next_token().asInput() == "*") {
3252 string name = t.asInput();
3253 string const length = p.verbatim_item();
3256 bool valid = splitLatexLength(length, valstring, unit);
3257 bool known_hspace = false;
3258 bool known_vspace = false;
3259 bool known_unit = false;
3262 istringstream iss(valstring);
3265 if (t.cs()[0] == 'h') {
3266 if (unit == "\\fill") {
3271 known_hspace = true;
3274 if (unit == "\\smallskipamount") {
3276 known_vspace = true;
3277 } else if (unit == "\\medskipamount") {
3279 known_vspace = true;
3280 } else if (unit == "\\bigskipamount") {
3282 known_vspace = true;
3283 } else if (unit == "\\fill") {
3285 known_vspace = true;
3289 if (!known_hspace && !known_vspace) {
3290 switch (unitFromString(unit)) {
3311 if (t.cs()[0] == 'h' && (known_unit || known_hspace)) {
3312 // Literal horizontal length or known variable
3313 context.check_layout(os);
3314 begin_inset(os, "space ");
3322 if (known_unit && !known_hspace)
3324 << translate_len(length);
3326 } else if (known_unit || known_vspace) {
3327 // Literal vertical length or known variable
3328 context.check_layout(os);
3329 begin_inset(os, "VSpace ");
3337 // LyX can't handle other length variables in Inset VSpace/space
3342 handle_ert(os, name + '{' + unit + '}', context);
3343 else if (value == -1.0)
3344 handle_ert(os, name + "{-" + unit + '}', context);
3346 handle_ert(os, name + '{' + valstring + unit + '}', context);
3348 handle_ert(os, name + '{' + length + '}', context);
3352 // The single '=' is meant here.
3353 else if ((newinsetlayout = findInsetLayout(context.textclass, t.cs(), true))) {
3355 context.check_layout(os);
3356 begin_inset(os, "Flex ");
3357 os << to_utf8(newinsetlayout->name()) << '\n'
3358 << "status collapsed\n";
3359 parse_text_in_inset(p, os, FLAG_ITEM, false, context, newinsetlayout);
3364 // try to see whether the string is in unicodesymbols
3365 // Only use text mode commands, since we are in text mode here,
3366 // and math commands may be invalid (bug 6797)
3368 docstring s = encodings.fromLaTeXCommand(from_utf8(t.asInput()),
3369 rem, Encodings::TEXT_CMD);
3372 cerr << "When parsing " << t.cs()
3373 << ", result is " << to_utf8(s)
3374 << "+" << to_utf8(rem) << endl;
3375 context.check_layout(os);
3377 skip_spaces_braces(p);
3379 //cerr << "#: " << t << " mode: " << mode << endl;
3380 // heuristic: read up to next non-nested space
3382 string s = t.asInput();
3383 string z = p.verbatim_item();
3384 while (p.good() && z != " " && z.size()) {
3385 //cerr << "read: " << z << endl;
3387 z = p.verbatim_item();
3389 cerr << "found ERT: " << s << endl;
3390 handle_ert(os, s + ' ', context);
3393 string name = t.asInput();
3394 if (p.next_token().asInput() == "*") {
3395 // Starred commands like \vspace*{}
3396 p.get_token(); // Eat '*'
3399 if (!parse_command(name, p, os, outer, context))
3400 handle_ert(os, name, context);
3404 if (flags & FLAG_LEAVE) {
3405 flags &= ~FLAG_LEAVE;