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", ",", "thinspace", "quad",
214 "qquad", "enspace", "enskip", "negthinspace", "hfill", "dotfill", "hrulefill",
215 "leftarrowfill", "rightarrowfill", "upbracefill", "downbracefill", 0};
217 /// the same as known_spaces with .lyx names
218 char const * const known_coded_spaces[] = { "space{}", "space{}",
219 "thinspace{}", "thinspace{}", "quad{}", "qquad{}", "enspace{}", "enskip{}",
220 "negthinspace{}", "hfill{}", "dotfill{}", "hrulefill{}", "leftarrowfill{}",
221 "rightarrowfill{}", "upbracefill{}", "downbracefill{}", 0};
223 /// These are translated by LyX to commands like "\\LyX{}", so we have to put
224 /// them in ERT. "LaTeXe" must come before "LaTeX"!
225 char const * const known_phrases[] = {"LyX", "TeX", "LaTeXe", "LaTeX", 0};
226 char const * const known_coded_phrases[] = {"LyX", "TeX", "LaTeX2e", "LaTeX", 0};
227 int const known_phrase_lengths[] = {3, 5, 7, 0};
230 /// splits "x=z, y=b" into a map and an ordered keyword vector
231 void split_map(string const & s, map<string, string> & res, vector<string> & keys)
236 keys.resize(v.size());
237 for (size_t i = 0; i < v.size(); ++i) {
238 size_t const pos = v[i].find('=');
239 string const index = trimSpaceAndEol(v[i].substr(0, pos));
240 string const value = trimSpaceAndEol(v[i].substr(pos + 1, string::npos));
248 * Split a LaTeX length into value and unit.
249 * The latter can be a real unit like "pt", or a latex length variable
250 * like "\textwidth". The unit may contain additional stuff like glue
251 * lengths, but we don't care, because such lengths are ERT anyway.
252 * \returns true if \p value and \p unit are valid.
254 bool splitLatexLength(string const & len, string & value, string & unit)
258 const string::size_type i = len.find_first_not_of(" -+0123456789.,");
259 //'4,5' is a valid LaTeX length number. Change it to '4.5'
260 string const length = subst(len, ',', '.');
261 if (i == string::npos)
264 if (len[0] == '\\') {
265 // We had something like \textwidth without a factor
271 value = trimSpaceAndEol(string(length, 0, i));
275 // 'cM' is a valid LaTeX length unit. Change it to 'cm'
276 if (contains(len, '\\'))
277 unit = trimSpaceAndEol(string(len, i));
279 unit = ascii_lowercase(trimSpaceAndEol(string(len, i)));
284 /// A simple function to translate a latex length to something LyX can
285 /// understand. Not perfect, but rather best-effort.
286 bool translate_len(string const & length, string & valstring, string & unit)
288 if (!splitLatexLength(length, valstring, unit))
290 // LyX uses percent values
292 istringstream iss(valstring);
297 string const percentval = oss.str();
299 if (unit.empty() || unit[0] != '\\')
301 string::size_type const i = unit.find(' ');
302 string const endlen = (i == string::npos) ? string() : string(unit, i);
303 if (unit == "\\textwidth") {
304 valstring = percentval;
305 unit = "text%" + endlen;
306 } else if (unit == "\\columnwidth") {
307 valstring = percentval;
308 unit = "col%" + endlen;
309 } else if (unit == "\\paperwidth") {
310 valstring = percentval;
311 unit = "page%" + endlen;
312 } else if (unit == "\\linewidth") {
313 valstring = percentval;
314 unit = "line%" + endlen;
315 } else if (unit == "\\paperheight") {
316 valstring = percentval;
317 unit = "pheight%" + endlen;
318 } else if (unit == "\\textheight") {
319 valstring = percentval;
320 unit = "theight%" + endlen;
328 string translate_len(string const & length)
332 if (translate_len(length, value, unit))
334 // If the input is invalid, return what we have.
342 * Translates a LaTeX length into \p value, \p unit and
343 * \p special parts suitable for a box inset.
344 * The difference from translate_len() is that a box inset knows about
345 * some special "units" that are stored in \p special.
347 void translate_box_len(string const & length, string & value, string & unit, string & special)
349 if (translate_len(length, value, unit)) {
350 if (unit == "\\height" || unit == "\\depth" ||
351 unit == "\\totalheight" || unit == "\\width") {
352 special = unit.substr(1);
353 // The unit is not used, but LyX requires a dummy setting
366 * Find a file with basename \p name in path \p path and an extension
369 string find_file(string const & name, string const & path,
370 char const * const * extensions)
372 for (char const * const * what = extensions; *what; ++what) {
373 string const trial = addExtension(name, *what);
374 if (makeAbsPath(trial, path).exists())
381 void begin_inset(ostream & os, string const & name)
383 os << "\n\\begin_inset " << name;
387 void begin_command_inset(ostream & os, string const & name,
388 string const & latexname)
390 begin_inset(os, "CommandInset ");
391 os << name << "\nLatexCommand " << latexname << '\n';
395 void end_inset(ostream & os)
397 os << "\n\\end_inset\n\n";
401 bool skip_braces(Parser & p)
403 if (p.next_token().cat() != catBegin)
406 if (p.next_token().cat() == catEnd) {
415 /// replace LaTeX commands in \p s from the unicodesymbols file with their
417 docstring convert_unicodesymbols(docstring s)
420 for (size_t i = 0; i < s.size();) {
427 docstring parsed = encodings.fromLaTeXCommand(s, rem,
428 Encodings::TEXT_CMD);
431 if (s.empty() || s[0] != '\\')
440 /// try to convert \p s to a valid InsetCommand argument
441 string convert_command_inset_arg(string s)
444 // since we don't know the input encoding we can't use from_utf8
445 s = to_utf8(convert_unicodesymbols(from_ascii(s)));
446 // LyX cannot handle newlines in a latex command
447 return subst(s, "\n", " ");
451 void handle_backslash(ostream & os, string const & s)
453 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
455 os << "\n\\backslash\n";
462 void handle_ert(ostream & os, string const & s, Context & context)
464 // We must have a valid layout before outputting the ERT inset.
465 context.check_layout(os);
466 Context newcontext(true, context.textclass);
467 begin_inset(os, "ERT");
468 os << "\nstatus collapsed\n";
469 newcontext.check_layout(os);
470 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
472 os << "\n\\backslash\n";
473 else if (*it == '\n') {
474 newcontext.new_paragraph(os);
475 newcontext.check_layout(os);
479 newcontext.check_end_layout(os);
484 void handle_comment(ostream & os, string const & s, Context & context)
486 // TODO: Handle this better
487 Context newcontext(true, context.textclass);
488 begin_inset(os, "ERT");
489 os << "\nstatus collapsed\n";
490 newcontext.check_layout(os);
491 handle_backslash(os, s);
492 // make sure that our comment is the last thing on the line
493 newcontext.new_paragraph(os);
494 newcontext.check_layout(os);
495 newcontext.check_end_layout(os);
500 Layout const * findLayout(TextClass const & textclass, string const & name, bool command)
502 Layout const * layout = findLayoutWithoutModule(textclass, name, command);
505 if (checkModule(name, command))
506 return findLayoutWithoutModule(textclass, name, command);
511 InsetLayout const * findInsetLayout(TextClass const & textclass, string const & name, bool command)
513 InsetLayout const * insetlayout = findInsetLayoutWithoutModule(textclass, name, command);
516 if (checkModule(name, command))
517 return findInsetLayoutWithoutModule(textclass, name, command);
522 void eat_whitespace(Parser &, ostream &, Context &, bool);
526 * Skips whitespace and braces.
527 * This should be called after a command has been parsed that is not put into
528 * ERT, and where LyX adds "{}" if needed.
530 void skip_spaces_braces(Parser & p, bool keepws = false)
532 /* The following four examples produce the same typeset output and
533 should be handled by this function:
541 // Unfortunately we need to skip comments, too.
542 // We can't use eat_whitespace since writing them after the {}
543 // results in different output in some cases.
544 bool const skipped_spaces = p.skip_spaces(true);
545 bool const skipped_braces = skip_braces(p);
546 if (keepws && skipped_spaces && !skipped_braces)
547 // put back the space (it is better handled by check_space)
548 p.unskip_spaces(true);
552 void output_command_layout(ostream & os, Parser & p, bool outer,
553 Context & parent_context,
554 Layout const * newlayout)
556 TeXFont const oldFont = parent_context.font;
557 // save the current font size
558 string const size = oldFont.size;
559 // reset the font size to default, because the font size switches
560 // don't affect section headings and the like
561 parent_context.font.size = Context::normalfont.size;
562 // we only need to write the font change if we have an open layout
563 if (!parent_context.atParagraphStart())
564 output_font_change(os, oldFont, parent_context.font);
565 parent_context.check_end_layout(os);
566 Context context(true, parent_context.textclass, newlayout,
567 parent_context.layout, parent_context.font);
568 if (parent_context.deeper_paragraph) {
569 // We are beginning a nested environment after a
570 // deeper paragraph inside the outer list environment.
571 // Therefore we don't need to output a "begin deeper".
572 context.need_end_deeper = true;
574 context.check_deeper(os);
575 context.check_layout(os);
576 unsigned int optargs = 0;
577 while (optargs < context.layout->optargs) {
578 eat_whitespace(p, os, context, false);
579 if (p.next_token().cat() == catEscape ||
580 p.next_token().character() != '[')
582 p.get_token(); // eat '['
583 begin_inset(os, "Argument\n");
584 os << "status collapsed\n\n";
585 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
587 eat_whitespace(p, os, context, false);
590 unsigned int reqargs = 0;
591 while (reqargs < context.layout->reqargs) {
592 eat_whitespace(p, os, context, false);
593 if (p.next_token().cat() != catBegin)
595 p.get_token(); // eat '{'
596 begin_inset(os, "Argument\n");
597 os << "status collapsed\n\n";
598 parse_text_in_inset(p, os, FLAG_BRACE_LAST, outer, context);
600 eat_whitespace(p, os, context, false);
603 parse_text(p, os, FLAG_ITEM, outer, context);
604 context.check_end_layout(os);
605 if (parent_context.deeper_paragraph) {
606 // We must suppress the "end deeper" because we
607 // suppressed the "begin deeper" above.
608 context.need_end_deeper = false;
610 context.check_end_deeper(os);
611 // We don't need really a new paragraph, but
612 // we must make sure that the next item gets a \begin_layout.
613 parent_context.new_paragraph(os);
614 // Set the font size to the original value. No need to output it here
615 // (Context::begin_layout() will do that if needed)
616 parent_context.font.size = size;
621 * Output a space if necessary.
622 * This function gets called for every whitespace token.
624 * We have three cases here:
625 * 1. A space must be suppressed. Example: The lyxcode case below
626 * 2. A space may be suppressed. Example: Spaces before "\par"
627 * 3. A space must not be suppressed. Example: A space between two words
629 * We currently handle only 1. and 3 and from 2. only the case of
630 * spaces before newlines as a side effect.
632 * 2. could be used to suppress as many spaces as possible. This has two effects:
633 * - Reimporting LyX generated LaTeX files changes almost no whitespace
634 * - Superflous whitespace from non LyX generated LaTeX files is removed.
635 * The drawback is that the logic inside the function becomes
636 * complicated, and that is the reason why it is not implemented.
638 void check_space(Parser & p, ostream & os, Context & context)
640 Token const next = p.next_token();
641 Token const curr = p.curr_token();
642 // A space before a single newline and vice versa must be ignored
643 // LyX emits a newline before \end{lyxcode}.
644 // This newline must be ignored,
645 // otherwise LyX will add an additional protected space.
646 if (next.cat() == catSpace ||
647 next.cat() == catNewline ||
648 (next.cs() == "end" && context.layout->free_spacing && curr.cat() == catNewline)) {
651 context.check_layout(os);
657 * Parse all arguments of \p command
659 void parse_arguments(string const & command,
660 vector<ArgumentType> const & template_arguments,
661 Parser & p, ostream & os, bool outer, Context & context)
663 string ert = command;
664 size_t no_arguments = template_arguments.size();
665 for (size_t i = 0; i < no_arguments; ++i) {
666 switch (template_arguments[i]) {
668 // This argument contains regular LaTeX
669 handle_ert(os, ert + '{', context);
670 eat_whitespace(p, os, context, false);
671 parse_text(p, os, FLAG_ITEM, outer, context);
675 // This argument consists only of a single item.
676 // The presence of '{' or not must be preserved.
678 if (p.next_token().cat() == catBegin)
679 ert += '{' + p.verbatim_item() + '}';
681 ert += p.verbatim_item();
684 // This argument may contain special characters
685 ert += '{' + p.verbatim_item() + '}';
688 // true because we must not eat whitespace
689 // if an optional arg follows we must not strip the
690 // brackets from this one
691 if (i < no_arguments - 1 &&
692 template_arguments[i+1] == optional)
693 ert += p.getFullOpt(true);
695 ert += p.getOpt(true);
699 handle_ert(os, ert, context);
704 * Check whether \p command is a known command. If yes,
705 * handle the command with all arguments.
706 * \return true if the command was parsed, false otherwise.
708 bool parse_command(string const & command, Parser & p, ostream & os,
709 bool outer, Context & context)
711 if (known_commands.find(command) != known_commands.end()) {
712 parse_arguments(command, known_commands[command], p, os,
720 /// Parses a minipage or parbox
721 void parse_box(Parser & p, ostream & os, unsigned outer_flags,
722 unsigned inner_flags, bool outer, Context & parent_context,
723 string const & outer_type, string const & special,
724 string const & inner_type)
728 string hor_pos = "c";
729 // We need to set the height to the LaTeX default of 1\\totalheight
730 // for the case when no height argument is given
731 string height_value = "1";
732 string height_unit = "in";
733 string height_special = "totalheight";
735 if (!inner_type.empty() && p.hasOpt()) {
736 position = p.getArg('[', ']');
737 if (position != "t" && position != "c" && position != "b") {
738 cerr << "invalid position " << position << " for "
739 << inner_type << endl;
743 latex_height = p.getArg('[', ']');
744 translate_box_len(latex_height, height_value, height_unit, height_special);
747 inner_pos = p.getArg('[', ']');
748 if (inner_pos != "c" && inner_pos != "t" &&
749 inner_pos != "b" && inner_pos != "s") {
750 cerr << "invalid inner_pos "
751 << inner_pos << " for "
752 << inner_type << endl;
753 inner_pos = position;
761 if (inner_type.empty()) {
763 latex_width = "\\columnwidth";
766 latex_width = p2.getArg('[', ']');
767 string const opt = p2.getArg('[', ']');
770 if (hor_pos != "l" && hor_pos != "c" &&
772 cerr << "invalid hor_pos " << hor_pos
773 << " for " << outer_type << endl;
779 latex_width = p.verbatim_item();
780 translate_len(latex_width, width_value, width_unit);
781 // LyX can't handle length variables
782 bool use_ert = contains(width_unit, '\\') || contains(height_unit, '\\');
783 if (!use_ert && !outer_type.empty() && !inner_type.empty()) {
784 // Look whether there is some content after the end of the
785 // inner box, but before the end of the outer box.
786 // If yes, we need to output ERT.
788 if (inner_flags & FLAG_END)
789 p.verbatimEnvironment(inner_type);
793 if ((outer_type == "framed" && p.next_token().asInput() != "\\end") ||
794 (outer_type != "framed" && p.next_token().cat() != catEnd)) {
795 // something is between the end of the inner box and
796 // the end of the outer box, so we need to use ERT.
803 if (!outer_type.empty()) {
804 if (outer_flags & FLAG_END)
805 ss << "\\begin{" << outer_type << '}';
807 ss << '\\' << outer_type << '{';
808 if (!special.empty())
812 if (!inner_type.empty()) {
813 if (inner_flags & FLAG_END)
814 ss << "\\begin{" << inner_type << '}';
816 ss << '\\' << inner_type;
817 if (!position.empty())
818 ss << '[' << position << ']';
819 if (!latex_height.empty())
820 ss << '[' << latex_height << ']';
821 if (!inner_pos.empty())
822 ss << '[' << inner_pos << ']';
823 ss << '{' << latex_width << '}';
824 if (!(inner_flags & FLAG_END))
827 handle_ert(os, ss.str(), parent_context);
828 if (!inner_type.empty()) {
829 parse_text(p, os, inner_flags, outer, parent_context);
830 if (inner_flags & FLAG_END)
831 handle_ert(os, "\\end{" + inner_type + '}',
834 handle_ert(os, "}", parent_context);
836 if (!outer_type.empty()) {
837 parse_text(p, os, outer_flags, outer, parent_context);
838 if (outer_flags & FLAG_END)
839 handle_ert(os, "\\end{" + outer_type + '}',
842 handle_ert(os, "}", parent_context);
845 // LyX does not like empty positions, so we have
846 // to set them to the LaTeX default values here.
847 if (position.empty())
849 if (inner_pos.empty())
850 inner_pos = position;
851 parent_context.check_layout(os);
852 begin_inset(os, "Box ");
853 if (outer_type == "framed")
855 else if (outer_type == "framebox")
857 else if (outer_type == "shadowbox")
859 else if (outer_type == "shaded")
861 else if (outer_type == "doublebox")
863 else if (outer_type.empty())
866 os << outer_type << '\n';
867 os << "position \"" << position << "\"\n";
868 os << "hor_pos \"" << hor_pos << "\"\n";
869 os << "has_inner_box " << !inner_type.empty() << "\n";
870 os << "inner_pos \"" << inner_pos << "\"\n";
871 os << "use_parbox " << (inner_type == "parbox") << '\n';
872 os << "use_makebox 0\n";
873 os << "width \"" << width_value << width_unit << "\"\n";
874 os << "special \"none\"\n";
875 os << "height \"" << height_value << height_unit << "\"\n";
876 os << "height_special \"" << height_special << "\"\n";
877 os << "status open\n\n";
878 Context context(true, parent_context.textclass);
879 context.font = parent_context.font;
881 // If we have no inner box the contens will be read with the outer box
882 if (!inner_type.empty())
883 parse_text(p, os, inner_flags, outer, context);
885 // Ensure that the end of the outer box is parsed correctly:
886 // The opening brace has been eaten by parse_outer_box()
887 if (!outer_type.empty() && (outer_flags & FLAG_ITEM)) {
888 outer_flags &= ~FLAG_ITEM;
889 outer_flags |= FLAG_BRACE_LAST;
892 // Find end of outer box, output contents if inner_type is
893 // empty and output possible comments
894 if (!outer_type.empty()) {
895 // This does not output anything but comments if
896 // inner_type is not empty (see use_ert)
897 parse_text(p, os, outer_flags, outer, context);
900 context.check_end_layout(os);
902 #ifdef PRESERVE_LAYOUT
903 // LyX puts a % after the end of the minipage
904 if (p.next_token().cat() == catNewline && p.next_token().cs().size() > 1) {
906 //handle_comment(os, "%dummy", parent_context);
909 parent_context.new_paragraph(os);
911 else if (p.next_token().cat() == catSpace || p.next_token().cat() == catNewline) {
912 //handle_comment(os, "%dummy", parent_context);
915 // We add a protected space if something real follows
916 if (p.good() && p.next_token().cat() != catComment) {
917 begin_inset(os, "space ~\n");
926 void parse_outer_box(Parser & p, ostream & os, unsigned flags, bool outer,
927 Context & parent_context, string const & outer_type,
928 string const & special)
930 eat_whitespace(p, os, parent_context, false);
931 if (flags & FLAG_ITEM) {
933 if (p.next_token().cat() == catBegin)
936 cerr << "Warning: Ignoring missing '{' after \\"
937 << outer_type << '.' << endl;
938 eat_whitespace(p, os, parent_context, false);
941 unsigned int inner_flags = 0;
942 if (outer_type == "shaded") {
943 // These boxes never have an inner box
945 } else if (p.next_token().asInput() == "\\parbox") {
946 inner = p.get_token().cs();
947 inner_flags = FLAG_ITEM;
948 } else if (p.next_token().asInput() == "\\begin") {
949 // Is this a minipage?
952 inner = p.getArg('{', '}');
954 if (inner == "minipage") {
957 eat_whitespace(p, os, parent_context, false);
958 inner_flags = FLAG_END;
962 if (inner_flags == FLAG_END) {
963 active_environments.push_back(inner);
964 parse_box(p, os, flags, FLAG_END, outer, parent_context,
965 outer_type, special, inner);
966 active_environments.pop_back();
968 parse_box(p, os, flags, inner_flags, outer, parent_context,
969 outer_type, special, inner);
974 void parse_listings(Parser & p, ostream & os, Context & parent_context)
976 parent_context.check_layout(os);
977 begin_inset(os, "listings\n");
978 os << "inline false\n"
979 << "status collapsed\n";
980 Context context(true, parent_context.textclass);
981 context.layout = &parent_context.textclass.plainLayout();
982 context.check_layout(os);
983 string const s = p.verbatimEnvironment("lstlisting");
984 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
986 os << "\n\\backslash\n";
987 else if (*it == '\n') {
988 // avoid adding an empty paragraph at the end
990 context.new_paragraph(os);
991 context.check_layout(os);
996 context.check_end_layout(os);
1001 /// parse an unknown environment
1002 void parse_unknown_environment(Parser & p, string const & name, ostream & os,
1003 unsigned flags, bool outer,
1004 Context & parent_context)
1006 if (name == "tabbing")
1007 // We need to remember that we have to handle '\=' specially
1008 flags |= FLAG_TABBING;
1010 // We need to translate font changes and paragraphs inside the
1011 // environment to ERT if we have a non standard font.
1012 // Otherwise things like
1013 // \large\begin{foo}\huge bar\end{foo}
1015 bool const specialfont =
1016 (parent_context.font != parent_context.normalfont);
1017 bool const new_layout_allowed = parent_context.new_layout_allowed;
1019 parent_context.new_layout_allowed = false;
1020 handle_ert(os, "\\begin{" + name + "}", parent_context);
1021 parse_text_snippet(p, os, flags, outer, parent_context);
1022 handle_ert(os, "\\end{" + name + "}", parent_context);
1024 parent_context.new_layout_allowed = new_layout_allowed;
1028 void parse_environment(Parser & p, ostream & os, bool outer,
1029 string & last_env, Context & parent_context)
1031 Layout const * newlayout;
1032 InsetLayout const * newinsetlayout = 0;
1033 string const name = p.getArg('{', '}');
1034 const bool is_starred = suffixIs(name, '*');
1035 string const unstarred_name = rtrim(name, "*");
1036 active_environments.push_back(name);
1038 if (is_math_env(name)) {
1039 parent_context.check_layout(os);
1040 begin_inset(os, "Formula ");
1041 os << "\\begin{" << name << "}";
1042 parse_math(p, os, FLAG_END, MATH_MODE);
1043 os << "\\end{" << name << "}";
1047 else if (name == "tabular" || name == "longtable") {
1048 eat_whitespace(p, os, parent_context, false);
1049 parent_context.check_layout(os);
1050 begin_inset(os, "Tabular ");
1051 handle_tabular(p, os, name == "longtable", parent_context);
1056 else if (parent_context.textclass.floats().typeExist(unstarred_name)) {
1057 eat_whitespace(p, os, parent_context, false);
1058 parent_context.check_layout(os);
1059 begin_inset(os, "Float " + unstarred_name + "\n");
1061 os << "placement " << p.getArg('[', ']') << '\n';
1062 os << "wide " << convert<string>(is_starred)
1063 << "\nsideways false"
1064 << "\nstatus open\n\n";
1065 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
1067 // We don't need really a new paragraph, but
1068 // we must make sure that the next item gets a \begin_layout.
1069 parent_context.new_paragraph(os);
1073 else if (name == "minipage") {
1074 eat_whitespace(p, os, parent_context, false);
1075 parse_box(p, os, 0, FLAG_END, outer, parent_context, "", "", name);
1079 else if (name == "comment") {
1080 eat_whitespace(p, os, parent_context, false);
1081 parent_context.check_layout(os);
1082 begin_inset(os, "Note Comment\n");
1083 os << "status open\n";
1084 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
1087 skip_braces(p); // eat {} that might by set by LyX behind comments
1090 else if (name == "lyxgreyedout") {
1091 eat_whitespace(p, os, parent_context, false);
1092 parent_context.check_layout(os);
1093 begin_inset(os, "Note Greyedout\n");
1094 os << "status open\n";
1095 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
1100 else if (name == "framed" || name == "shaded") {
1101 eat_whitespace(p, os, parent_context, false);
1102 parse_outer_box(p, os, FLAG_END, outer, parent_context, name, "");
1106 else if (name == "lstlisting") {
1107 eat_whitespace(p, os, parent_context, false);
1108 // FIXME handle listings with parameters
1110 parse_unknown_environment(p, name, os, FLAG_END,
1111 outer, parent_context);
1113 parse_listings(p, os, parent_context);
1117 else if (!parent_context.new_layout_allowed)
1118 parse_unknown_environment(p, name, os, FLAG_END, outer,
1121 // Alignment and spacing settings
1122 // FIXME (bug xxxx): These settings can span multiple paragraphs and
1123 // therefore are totally broken!
1124 // Note that \centering, raggedright, and raggedleft cannot be handled, as
1125 // they are commands not environments. They are furthermore switches that
1126 // can be ended by another switches, but also by commands like \footnote or
1127 // \parbox. So the only safe way is to leave them untouched.
1128 else if (name == "center" || name == "centering" ||
1129 name == "flushleft" || name == "flushright" ||
1130 name == "singlespace" || name == "onehalfspace" ||
1131 name == "doublespace" || name == "spacing") {
1132 eat_whitespace(p, os, parent_context, false);
1133 // We must begin a new paragraph if not already done
1134 if (! parent_context.atParagraphStart()) {
1135 parent_context.check_end_layout(os);
1136 parent_context.new_paragraph(os);
1138 if (name == "flushleft")
1139 parent_context.add_extra_stuff("\\align left\n");
1140 else if (name == "flushright")
1141 parent_context.add_extra_stuff("\\align right\n");
1142 else if (name == "center" || name == "centering")
1143 parent_context.add_extra_stuff("\\align center\n");
1144 else if (name == "singlespace")
1145 parent_context.add_extra_stuff("\\paragraph_spacing single\n");
1146 else if (name == "onehalfspace")
1147 parent_context.add_extra_stuff("\\paragraph_spacing onehalf\n");
1148 else if (name == "doublespace")
1149 parent_context.add_extra_stuff("\\paragraph_spacing double\n");
1150 else if (name == "spacing")
1151 parent_context.add_extra_stuff("\\paragraph_spacing other " + p.verbatim_item() + "\n");
1152 parse_text(p, os, FLAG_END, outer, parent_context);
1153 // Just in case the environment is empty
1154 parent_context.extra_stuff.erase();
1155 // We must begin a new paragraph to reset the alignment
1156 parent_context.new_paragraph(os);
1160 // The single '=' is meant here.
1161 else if ((newlayout = findLayout(parent_context.textclass, name, false))) {
1162 eat_whitespace(p, os, parent_context, false);
1163 Context context(true, parent_context.textclass, newlayout,
1164 parent_context.layout, parent_context.font);
1165 if (parent_context.deeper_paragraph) {
1166 // We are beginning a nested environment after a
1167 // deeper paragraph inside the outer list environment.
1168 // Therefore we don't need to output a "begin deeper".
1169 context.need_end_deeper = true;
1171 parent_context.check_end_layout(os);
1172 if (last_env == name) {
1173 // we need to output a separator since LyX would export
1174 // the two environments as one otherwise (bug 5716)
1175 docstring const sep = from_ascii("--Separator--");
1176 TeX2LyXDocClass const & textclass(parent_context.textclass);
1177 if (textclass.hasLayout(sep)) {
1178 Context newcontext(parent_context);
1179 newcontext.layout = &(textclass[sep]);
1180 newcontext.check_layout(os);
1181 newcontext.check_end_layout(os);
1183 parent_context.check_layout(os);
1184 begin_inset(os, "Note Note\n");
1185 os << "status closed\n";
1186 Context newcontext(true, textclass,
1187 &(textclass.defaultLayout()));
1188 newcontext.check_layout(os);
1189 newcontext.check_end_layout(os);
1191 parent_context.check_end_layout(os);
1194 switch (context.layout->latextype) {
1195 case LATEX_LIST_ENVIRONMENT:
1196 context.add_par_extra_stuff("\\labelwidthstring "
1197 + p.verbatim_item() + '\n');
1200 case LATEX_BIB_ENVIRONMENT:
1201 p.verbatim_item(); // swallow next arg
1207 context.check_deeper(os);
1208 // handle known optional and required arguments
1209 // layouts require all optional arguments before the required ones
1210 // Unfortunately LyX can't handle arguments of list arguments (bug 7468):
1211 // It is impossible to place anything after the environment name,
1212 // but before the first \\item.
1213 if (context.layout->latextype == LATEX_ENVIRONMENT) {
1214 bool need_layout = true;
1215 unsigned int optargs = 0;
1216 while (optargs < context.layout->optargs) {
1217 eat_whitespace(p, os, context, false);
1218 if (p.next_token().cat() == catEscape ||
1219 p.next_token().character() != '[')
1221 p.get_token(); // eat '['
1223 context.check_layout(os);
1224 need_layout = false;
1226 begin_inset(os, "Argument\n");
1227 os << "status collapsed\n\n";
1228 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
1230 eat_whitespace(p, os, context, false);
1233 unsigned int reqargs = 0;
1234 while (reqargs < context.layout->reqargs) {
1235 eat_whitespace(p, os, context, false);
1236 if (p.next_token().cat() != catBegin)
1238 p.get_token(); // eat '{'
1240 context.check_layout(os);
1241 need_layout = false;
1243 begin_inset(os, "Argument\n");
1244 os << "status collapsed\n\n";
1245 parse_text_in_inset(p, os, FLAG_BRACE_LAST, outer, context);
1247 eat_whitespace(p, os, context, false);
1251 parse_text(p, os, FLAG_END, outer, context);
1252 context.check_end_layout(os);
1253 if (parent_context.deeper_paragraph) {
1254 // We must suppress the "end deeper" because we
1255 // suppressed the "begin deeper" above.
1256 context.need_end_deeper = false;
1258 context.check_end_deeper(os);
1259 parent_context.new_paragraph(os);
1263 // The single '=' is meant here.
1264 else if ((newinsetlayout = findInsetLayout(parent_context.textclass, name, false))) {
1265 eat_whitespace(p, os, parent_context, false);
1266 parent_context.check_layout(os);
1267 begin_inset(os, "Flex ");
1268 os << to_utf8(newinsetlayout->name()) << '\n'
1269 << "status collapsed\n";
1270 parse_text_in_inset(p, os, FLAG_END, false, parent_context, newinsetlayout);
1274 else if (name == "appendix") {
1275 // This is no good latex style, but it works and is used in some documents...
1276 eat_whitespace(p, os, parent_context, false);
1277 parent_context.check_end_layout(os);
1278 Context context(true, parent_context.textclass, parent_context.layout,
1279 parent_context.layout, parent_context.font);
1280 context.check_layout(os);
1281 os << "\\start_of_appendix\n";
1282 parse_text(p, os, FLAG_END, outer, context);
1283 context.check_end_layout(os);
1287 else if (known_environments.find(name) != known_environments.end()) {
1288 vector<ArgumentType> arguments = known_environments[name];
1289 // The last "argument" denotes wether we may translate the
1290 // environment contents to LyX
1291 // The default required if no argument is given makes us
1292 // compatible with the reLyXre environment.
1293 ArgumentType contents = arguments.empty() ?
1296 if (!arguments.empty())
1297 arguments.pop_back();
1298 // See comment in parse_unknown_environment()
1299 bool const specialfont =
1300 (parent_context.font != parent_context.normalfont);
1301 bool const new_layout_allowed =
1302 parent_context.new_layout_allowed;
1304 parent_context.new_layout_allowed = false;
1305 parse_arguments("\\begin{" + name + "}", arguments, p, os,
1306 outer, parent_context);
1307 if (contents == verbatim)
1308 handle_ert(os, p.verbatimEnvironment(name),
1311 parse_text_snippet(p, os, FLAG_END, outer,
1313 handle_ert(os, "\\end{" + name + "}", parent_context);
1315 parent_context.new_layout_allowed = new_layout_allowed;
1319 parse_unknown_environment(p, name, os, FLAG_END, outer,
1323 active_environments.pop_back();
1327 /// parses a comment and outputs it to \p os.
1328 void parse_comment(Parser & p, ostream & os, Token const & t, Context & context)
1330 LASSERT(t.cat() == catComment, return);
1331 if (!t.cs().empty()) {
1332 context.check_layout(os);
1333 handle_comment(os, '%' + t.cs(), context);
1334 if (p.next_token().cat() == catNewline) {
1335 // A newline after a comment line starts a new
1337 if (context.new_layout_allowed) {
1338 if(!context.atParagraphStart())
1339 // Only start a new paragraph if not already
1340 // done (we might get called recursively)
1341 context.new_paragraph(os);
1343 handle_ert(os, "\n", context);
1344 eat_whitespace(p, os, context, true);
1347 // "%\n" combination
1354 * Reads spaces and comments until the first non-space, non-comment token.
1355 * New paragraphs (double newlines or \\par) are handled like simple spaces
1356 * if \p eatParagraph is true.
1357 * Spaces are skipped, but comments are written to \p os.
1359 void eat_whitespace(Parser & p, ostream & os, Context & context,
1363 Token const & t = p.get_token();
1364 if (t.cat() == catComment)
1365 parse_comment(p, os, t, context);
1366 else if ((! eatParagraph && p.isParagraph()) ||
1367 (t.cat() != catSpace && t.cat() != catNewline)) {
1376 * Set a font attribute, parse text and reset the font attribute.
1377 * \param attribute Attribute name (e.g. \\family, \\shape etc.)
1378 * \param currentvalue Current value of the attribute. Is set to the new
1379 * value during parsing.
1380 * \param newvalue New value of the attribute
1382 void parse_text_attributes(Parser & p, ostream & os, unsigned flags, bool outer,
1383 Context & context, string const & attribute,
1384 string & currentvalue, string const & newvalue)
1386 context.check_layout(os);
1387 string const oldvalue = currentvalue;
1388 currentvalue = newvalue;
1389 os << '\n' << attribute << ' ' << newvalue << "\n";
1390 parse_text_snippet(p, os, flags, outer, context);
1391 context.check_layout(os);
1392 os << '\n' << attribute << ' ' << oldvalue << "\n";
1393 currentvalue = oldvalue;
1397 /// get the arguments of a natbib or jurabib citation command
1398 void get_cite_arguments(Parser & p, bool natbibOrder,
1399 string & before, string & after)
1401 // We need to distinguish "" and "[]", so we can't use p.getOpt().
1403 // text before the citation
1405 // text after the citation
1406 after = p.getFullOpt();
1408 if (!after.empty()) {
1409 before = p.getFullOpt();
1410 if (natbibOrder && !before.empty())
1411 swap(before, after);
1416 /// Convert filenames with TeX macros and/or quotes to something LyX
1418 string const normalize_filename(string const & name)
1420 Parser p(trim(name, "\""));
1423 Token const & t = p.get_token();
1424 if (t.cat() != catEscape)
1426 else if (t.cs() == "lyxdot") {
1427 // This is used by LyX for simple dots in relative
1431 } else if (t.cs() == "space") {
1441 /// Convert \p name from TeX convention (relative to master file) to LyX
1442 /// convention (relative to .lyx file) if it is relative
1443 void fix_relative_filename(string & name)
1445 if (FileName::isAbsolute(name))
1448 name = to_utf8(makeRelPath(from_utf8(makeAbsPath(name, getMasterFilePath()).absFileName()),
1449 from_utf8(getParentFilePath())));
1453 /// Parse a NoWeb Scrap section. The initial "<<" is already parsed.
1454 void parse_noweb(Parser & p, ostream & os, Context & context)
1456 // assemble the rest of the keyword
1460 Token const & t = p.get_token();
1461 if (t.asInput() == ">" && p.next_token().asInput() == ">") {
1464 scrap = (p.good() && p.next_token().asInput() == "=");
1466 name += p.get_token().asInput();
1469 name += t.asInput();
1472 if (!scrap || !context.new_layout_allowed ||
1473 !context.textclass.hasLayout(from_ascii("Scrap"))) {
1474 cerr << "Warning: Could not interpret '" << name
1475 << "'. Ignoring it." << endl;
1479 // We use new_paragraph instead of check_end_layout because the stuff
1480 // following the noweb chunk needs to start with a \begin_layout.
1481 // This may create a new paragraph even if there was none in the
1482 // noweb file, but the alternative is an invalid LyX file. Since
1483 // noweb code chunks are implemented with a layout style in LyX they
1484 // always must be in an own paragraph.
1485 context.new_paragraph(os);
1486 Context newcontext(true, context.textclass,
1487 &context.textclass[from_ascii("Scrap")]);
1488 newcontext.check_layout(os);
1491 Token const & t = p.get_token();
1492 // We abuse the parser a bit, because this is no TeX syntax
1494 if (t.cat() == catEscape)
1495 os << subst(t.asInput(), "\\", "\n\\backslash\n");
1498 Context tmp(false, context.textclass,
1499 &context.textclass[from_ascii("Scrap")]);
1500 tmp.need_end_layout = true;
1501 tmp.check_layout(oss);
1502 os << subst(t.asInput(), "\n", oss.str());
1504 // The scrap chunk is ended by an @ at the beginning of a line.
1505 // After the @ the line may contain a comment and/or
1506 // whitespace, but nothing else.
1507 if (t.asInput() == "@" && p.prev_token().cat() == catNewline &&
1508 (p.next_token().cat() == catSpace ||
1509 p.next_token().cat() == catNewline ||
1510 p.next_token().cat() == catComment)) {
1511 while (p.good() && p.next_token().cat() == catSpace)
1512 os << p.get_token().asInput();
1513 if (p.next_token().cat() == catComment)
1514 // The comment includes a final '\n'
1515 os << p.get_token().asInput();
1517 if (p.next_token().cat() == catNewline)
1524 newcontext.check_end_layout(os);
1528 /// detects \\def, \\long\\def and \\global\\long\\def with ws and comments
1529 bool is_macro(Parser & p)
1531 Token first = p.curr_token();
1532 if (first.cat() != catEscape || !p.good())
1534 if (first.cs() == "def")
1536 if (first.cs() != "global" && first.cs() != "long")
1538 Token second = p.get_token();
1540 while (p.good() && !p.isParagraph() && (second.cat() == catSpace ||
1541 second.cat() == catNewline || second.cat() == catComment)) {
1542 second = p.get_token();
1545 bool secondvalid = second.cat() == catEscape;
1547 bool thirdvalid = false;
1548 if (p.good() && first.cs() == "global" && secondvalid &&
1549 second.cs() == "long") {
1550 third = p.get_token();
1552 while (p.good() && !p.isParagraph() &&
1553 (third.cat() == catSpace ||
1554 third.cat() == catNewline ||
1555 third.cat() == catComment)) {
1556 third = p.get_token();
1559 thirdvalid = third.cat() == catEscape;
1561 for (int i = 0; i < pos; ++i)
1566 return (first.cs() == "global" || first.cs() == "long") &&
1567 second.cs() == "def";
1568 return first.cs() == "global" && second.cs() == "long" &&
1569 third.cs() == "def";
1573 /// Parse a macro definition (assumes that is_macro() returned true)
1574 void parse_macro(Parser & p, ostream & os, Context & context)
1576 context.check_layout(os);
1577 Token first = p.curr_token();
1580 string command = first.asInput();
1581 if (first.cs() != "def") {
1583 eat_whitespace(p, os, context, false);
1584 second = p.curr_token();
1585 command += second.asInput();
1586 if (second.cs() != "def") {
1588 eat_whitespace(p, os, context, false);
1589 third = p.curr_token();
1590 command += third.asInput();
1593 eat_whitespace(p, os, context, false);
1594 string const name = p.get_token().cs();
1595 eat_whitespace(p, os, context, false);
1601 while (p.next_token().cat() != catBegin) {
1602 if (p.next_token().cat() == catParameter) {
1607 // followed by number?
1608 if (p.next_token().cat() == catOther) {
1609 char c = p.getChar();
1611 // number = current arity + 1?
1612 if (c == arity + '0' + 1)
1617 paramtext += p.get_token().cs();
1619 paramtext += p.get_token().cs();
1624 // only output simple (i.e. compatible) macro as FormulaMacros
1625 string ert = '\\' + name + ' ' + paramtext + '{' + p.verbatim_item() + '}';
1627 context.check_layout(os);
1628 begin_inset(os, "FormulaMacro");
1629 os << "\n\\def" << ert;
1632 handle_ert(os, command + ert, context);
1635 } // anonymous namespace
1638 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
1641 Layout const * newlayout = 0;
1642 InsetLayout const * newinsetlayout = 0;
1643 // Store the latest bibliographystyle and nocite{*} option
1644 // (needed for bibtex inset)
1646 string bibliographystyle;
1647 bool const use_natbib = used_packages.find("natbib") != used_packages.end();
1648 bool const use_jurabib = used_packages.find("jurabib") != used_packages.end();
1651 Token const & t = p.get_token();
1654 debugToken(cerr, t, flags);
1657 if (flags & FLAG_ITEM) {
1658 if (t.cat() == catSpace)
1661 flags &= ~FLAG_ITEM;
1662 if (t.cat() == catBegin) {
1663 // skip the brace and collect everything to the next matching
1665 flags |= FLAG_BRACE_LAST;
1669 // handle only this single token, leave the loop if done
1670 flags |= FLAG_LEAVE;
1673 if (t.cat() != catEscape && t.character() == ']' &&
1674 (flags & FLAG_BRACK_LAST))
1676 if (t.cat() == catEnd && (flags & FLAG_BRACE_LAST))
1679 // If there is anything between \end{env} and \begin{env} we
1680 // don't need to output a separator.
1681 if (t.cat() != catSpace && t.cat() != catNewline &&
1682 t.asInput() != "\\begin")
1688 if (t.cat() == catMath) {
1689 // we are inside some text mode thingy, so opening new math is allowed
1690 context.check_layout(os);
1691 begin_inset(os, "Formula ");
1692 Token const & n = p.get_token();
1693 if (n.cat() == catMath && outer) {
1694 // TeX's $$...$$ syntax for displayed math
1696 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
1698 p.get_token(); // skip the second '$' token
1700 // simple $...$ stuff
1703 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
1709 else if (t.cat() == catSuper || t.cat() == catSub)
1710 cerr << "catcode " << t << " illegal in text mode\n";
1712 // Basic support for english quotes. This should be
1713 // extended to other quotes, but is not so easy (a
1714 // left english quote is the same as a right german
1716 else if (t.asInput() == "`" && p.next_token().asInput() == "`") {
1717 context.check_layout(os);
1718 begin_inset(os, "Quotes ");
1724 else if (t.asInput() == "'" && p.next_token().asInput() == "'") {
1725 context.check_layout(os);
1726 begin_inset(os, "Quotes ");
1733 else if (t.asInput() == ">" && p.next_token().asInput() == ">") {
1734 context.check_layout(os);
1735 begin_inset(os, "Quotes ");
1742 else if (t.asInput() == "<" && p.next_token().asInput() == "<") {
1743 context.check_layout(os);
1744 begin_inset(os, "Quotes ");
1751 else if (t.asInput() == "<"
1752 && p.next_token().asInput() == "<" && noweb_mode) {
1754 parse_noweb(p, os, context);
1757 else if (t.cat() == catSpace || (t.cat() == catNewline && ! p.isParagraph()))
1758 check_space(p, os, context);
1760 else if (t.character() == '[' && noweb_mode &&
1761 p.next_token().character() == '[') {
1762 // These can contain underscores
1764 string const s = p.getFullOpt() + ']';
1765 if (p.next_token().character() == ']')
1768 cerr << "Warning: Inserting missing ']' in '"
1769 << s << "'." << endl;
1770 handle_ert(os, s, context);
1773 else if (t.cat() == catLetter) {
1774 context.check_layout(os);
1775 // Workaround for bug 4752.
1776 // FIXME: This whole code block needs to be removed
1777 // when the bug is fixed and tex2lyx produces
1778 // the updated file format.
1779 // The replacement algorithm in LyX is so stupid that
1780 // it even translates a phrase if it is part of a word.
1781 bool handled = false;
1782 for (int const * l = known_phrase_lengths; *l; ++l) {
1783 string phrase = t.cs();
1784 for (int i = 1; i < *l && p.next_token().isAlnumASCII(); ++i)
1785 phrase += p.get_token().cs();
1786 if (is_known(phrase, known_coded_phrases)) {
1787 handle_ert(os, phrase, context);
1791 for (size_t i = 1; i < phrase.length(); ++i)
1799 else if (t.cat() == catOther ||
1800 t.cat() == catAlign ||
1801 t.cat() == catParameter) {
1802 // This translates "&" to "\\&" which may be wrong...
1803 context.check_layout(os);
1807 else if (p.isParagraph()) {
1808 if (context.new_layout_allowed)
1809 context.new_paragraph(os);
1811 handle_ert(os, "\\par ", context);
1812 eat_whitespace(p, os, context, true);
1815 else if (t.cat() == catActive) {
1816 context.check_layout(os);
1817 if (t.character() == '~') {
1818 if (context.layout->free_spacing)
1821 begin_inset(os, "space ~\n");
1828 else if (t.cat() == catBegin &&
1829 p.next_token().cat() == catEnd) {
1831 Token const prev = p.prev_token();
1833 if (p.next_token().character() == '`' ||
1834 (prev.character() == '-' &&
1835 p.next_token().character() == '-'))
1836 ; // ignore it in {}`` or -{}-
1838 handle_ert(os, "{}", context);
1842 else if (t.cat() == catBegin) {
1843 context.check_layout(os);
1844 // special handling of font attribute changes
1845 Token const prev = p.prev_token();
1846 Token const next = p.next_token();
1847 TeXFont const oldFont = context.font;
1848 if (next.character() == '[' ||
1849 next.character() == ']' ||
1850 next.character() == '*') {
1852 if (p.next_token().cat() == catEnd) {
1857 handle_ert(os, "{", context);
1858 parse_text_snippet(p, os,
1861 handle_ert(os, "}", context);
1863 } else if (! context.new_layout_allowed) {
1864 handle_ert(os, "{", context);
1865 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1867 handle_ert(os, "}", context);
1868 } else if (is_known(next.cs(), known_sizes)) {
1869 // next will change the size, so we must
1871 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1873 if (!context.atParagraphStart())
1875 << context.font.size << "\n";
1876 } else if (is_known(next.cs(), known_font_families)) {
1877 // next will change the font family, so we
1878 // must reset it here
1879 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1881 if (!context.atParagraphStart())
1883 << context.font.family << "\n";
1884 } else if (is_known(next.cs(), known_font_series)) {
1885 // next will change the font series, so we
1886 // must reset it here
1887 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1889 if (!context.atParagraphStart())
1891 << context.font.series << "\n";
1892 } else if (is_known(next.cs(), known_font_shapes)) {
1893 // next will change the font shape, so we
1894 // must reset it here
1895 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1897 if (!context.atParagraphStart())
1899 << context.font.shape << "\n";
1900 } else if (is_known(next.cs(), known_old_font_families) ||
1901 is_known(next.cs(), known_old_font_series) ||
1902 is_known(next.cs(), known_old_font_shapes)) {
1903 // next will change the font family, series
1904 // and shape, so we must reset it here
1905 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1907 if (!context.atParagraphStart())
1909 << context.font.family
1911 << context.font.series
1913 << context.font.shape << "\n";
1915 handle_ert(os, "{", context);
1916 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1918 handle_ert(os, "}", context);
1922 else if (t.cat() == catEnd) {
1923 if (flags & FLAG_BRACE_LAST) {
1926 cerr << "stray '}' in text\n";
1927 handle_ert(os, "}", context);
1930 else if (t.cat() == catComment)
1931 parse_comment(p, os, t, context);
1934 // control sequences
1937 else if (t.cs() == "(") {
1938 context.check_layout(os);
1939 begin_inset(os, "Formula");
1941 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
1946 else if (t.cs() == "[") {
1947 context.check_layout(os);
1948 begin_inset(os, "Formula");
1950 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
1955 else if (t.cs() == "begin")
1956 parse_environment(p, os, outer, last_env, context);
1958 else if (t.cs() == "end") {
1959 if (flags & FLAG_END) {
1960 // eat environment name
1961 string const name = p.getArg('{', '}');
1962 if (name != active_environment())
1963 cerr << "\\end{" + name + "} does not match \\begin{"
1964 + active_environment() + "}\n";
1967 p.error("found 'end' unexpectedly");
1970 else if (t.cs() == "item") {
1973 bool optarg = false;
1974 if (p.next_token().cat() != catEscape &&
1975 p.next_token().character() == '[') {
1976 p.get_token(); // eat '['
1977 s = parse_text_snippet(p, FLAG_BRACK_LAST,
1982 context.check_layout(os);
1983 if (context.has_item) {
1984 // An item in an unknown list-like environment
1985 // FIXME: Do this in check_layout()!
1986 context.has_item = false;
1988 handle_ert(os, "\\item", context);
1990 handle_ert(os, "\\item ", context);
1993 if (context.layout->labeltype != LABEL_MANUAL) {
1994 // LyX does not support \item[\mybullet]
1995 // in itemize environments
1996 handle_ert(os, "[", context);
1998 handle_ert(os, "]", context);
1999 } else if (!s.empty()) {
2000 // The space is needed to separate the
2001 // item from the rest of the sentence.
2003 eat_whitespace(p, os, context, false);
2008 else if (t.cs() == "bibitem") {
2010 context.check_layout(os);
2011 string label = convert_command_inset_arg(p.getArg('[', ']'));
2012 string key = convert_command_inset_arg(p.verbatim_item());
2013 if (contains(label, '\\') || contains(key, '\\')) {
2014 // LyX can't handle LaTeX commands in labels or keys
2015 handle_ert(os, t.asInput() + '[' + label +
2016 "]{" + p.verbatim_item() + '}',
2019 begin_command_inset(os, "bibitem", "bibitem");
2020 os << "label \"" << label << "\"\n"
2021 "key \"" << key << "\"\n";
2026 else if (is_macro(p))
2027 parse_macro(p, os, context);
2029 else if (t.cs() == "noindent") {
2031 context.add_par_extra_stuff("\\noindent\n");
2034 else if (t.cs() == "appendix") {
2035 context.add_par_extra_stuff("\\start_of_appendix\n");
2036 // We need to start a new paragraph. Otherwise the
2037 // appendix in 'bla\appendix\chapter{' would start
2039 context.new_paragraph(os);
2040 // We need to make sure that the paragraph is
2041 // generated even if it is empty. Otherwise the
2042 // appendix in '\par\appendix\par\chapter{' would
2044 context.check_layout(os);
2045 // FIXME: This is a hack to prevent paragraph
2046 // deletion if it is empty. Handle this better!
2048 "%dummy comment inserted by tex2lyx to "
2049 "ensure that this paragraph is not empty",
2051 // Both measures above may generate an additional
2052 // empty paragraph, but that does not hurt, because
2053 // whitespace does not matter here.
2054 eat_whitespace(p, os, context, true);
2057 // Starred section headings
2058 // Must attempt to parse "Section*" before "Section".
2059 else if ((p.next_token().asInput() == "*") &&
2060 context.new_layout_allowed &&
2061 (newlayout = findLayout(context.textclass, t.cs() + '*', true))) {
2064 output_command_layout(os, p, outer, context, newlayout);
2068 // Section headings and the like
2069 else if (context.new_layout_allowed &&
2070 (newlayout = findLayout(context.textclass, t.cs(), true))) {
2072 output_command_layout(os, p, outer, context, newlayout);
2076 else if (t.cs() == "caption") {
2078 context.check_layout(os);
2080 begin_inset(os, "Caption\n\n");
2081 Context newcontext(true, context.textclass);
2082 newcontext.font = context.font;
2083 newcontext.check_layout(os);
2084 if (p.next_token().cat() != catEscape &&
2085 p.next_token().character() == '[') {
2086 p.get_token(); // eat '['
2087 begin_inset(os, "Argument\n");
2088 os << "status collapsed\n";
2089 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
2091 eat_whitespace(p, os, context, false);
2093 parse_text(p, os, FLAG_ITEM, outer, context);
2094 context.check_end_layout(os);
2095 // We don't need really a new paragraph, but
2096 // we must make sure that the next item gets a \begin_layout.
2097 context.new_paragraph(os);
2100 newcontext.check_end_layout(os);
2103 else if (t.cs() == "includegraphics") {
2104 bool const clip = p.next_token().asInput() == "*";
2107 string const arg = p.getArg('[', ']');
2108 map<string, string> opts;
2109 vector<string> keys;
2110 split_map(arg, opts, keys);
2112 opts["clip"] = string();
2113 string name = normalize_filename(p.verbatim_item());
2115 string const path = getMasterFilePath();
2116 // We want to preserve relative / absolute filenames,
2117 // therefore path is only used for testing
2118 if (!makeAbsPath(name, path).exists()) {
2119 // The file extension is probably missing.
2120 // Now try to find it out.
2121 string const dvips_name =
2122 find_file(name, path,
2123 known_dvips_graphics_formats);
2124 string const pdftex_name =
2125 find_file(name, path,
2126 known_pdftex_graphics_formats);
2127 if (!dvips_name.empty()) {
2128 if (!pdftex_name.empty()) {
2129 cerr << "This file contains the "
2131 "\"\\includegraphics{"
2133 "However, files\n\""
2134 << dvips_name << "\" and\n\""
2135 << pdftex_name << "\"\n"
2136 "both exist, so I had to make a "
2137 "choice and took the first one.\n"
2138 "Please move the unwanted one "
2139 "someplace else and try again\n"
2140 "if my choice was wrong."
2144 } else if (!pdftex_name.empty()) {
2150 if (makeAbsPath(name, path).exists())
2151 fix_relative_filename(name);
2153 cerr << "Warning: Could not find graphics file '"
2154 << name << "'." << endl;
2156 context.check_layout(os);
2157 begin_inset(os, "Graphics ");
2158 os << "\n\tfilename " << name << '\n';
2159 if (opts.find("width") != opts.end())
2161 << translate_len(opts["width"]) << '\n';
2162 if (opts.find("height") != opts.end())
2164 << translate_len(opts["height"]) << '\n';
2165 if (opts.find("scale") != opts.end()) {
2166 istringstream iss(opts["scale"]);
2170 os << "\tscale " << val << '\n';
2172 if (opts.find("angle") != opts.end()) {
2173 os << "\trotateAngle "
2174 << opts["angle"] << '\n';
2175 vector<string>::const_iterator a =
2176 find(keys.begin(), keys.end(), "angle");
2177 vector<string>::const_iterator s =
2178 find(keys.begin(), keys.end(), "width");
2179 if (s == keys.end())
2180 s = find(keys.begin(), keys.end(), "height");
2181 if (s == keys.end())
2182 s = find(keys.begin(), keys.end(), "scale");
2183 if (s != keys.end() && distance(s, a) > 0)
2184 os << "\tscaleBeforeRotation\n";
2186 if (opts.find("origin") != opts.end()) {
2188 string const opt = opts["origin"];
2189 if (opt.find('l') != string::npos) ss << "left";
2190 if (opt.find('r') != string::npos) ss << "right";
2191 if (opt.find('c') != string::npos) ss << "center";
2192 if (opt.find('t') != string::npos) ss << "Top";
2193 if (opt.find('b') != string::npos) ss << "Bottom";
2194 if (opt.find('B') != string::npos) ss << "Baseline";
2195 if (!ss.str().empty())
2196 os << "\trotateOrigin " << ss.str() << '\n';
2198 cerr << "Warning: Ignoring unknown includegraphics origin argument '" << opt << "'\n";
2200 if (opts.find("keepaspectratio") != opts.end())
2201 os << "\tkeepAspectRatio\n";
2202 if (opts.find("clip") != opts.end())
2204 if (opts.find("draft") != opts.end())
2206 if (opts.find("bb") != opts.end())
2207 os << "\tBoundingBox "
2208 << opts["bb"] << '\n';
2209 int numberOfbbOptions = 0;
2210 if (opts.find("bbllx") != opts.end())
2211 numberOfbbOptions++;
2212 if (opts.find("bblly") != opts.end())
2213 numberOfbbOptions++;
2214 if (opts.find("bburx") != opts.end())
2215 numberOfbbOptions++;
2216 if (opts.find("bbury") != opts.end())
2217 numberOfbbOptions++;
2218 if (numberOfbbOptions == 4)
2219 os << "\tBoundingBox "
2220 << opts["bbllx"] << " " << opts["bblly"] << " "
2221 << opts["bburx"] << " " << opts["bbury"] << '\n';
2222 else if (numberOfbbOptions > 0)
2223 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
2224 numberOfbbOptions = 0;
2225 if (opts.find("natwidth") != opts.end())
2226 numberOfbbOptions++;
2227 if (opts.find("natheight") != opts.end())
2228 numberOfbbOptions++;
2229 if (numberOfbbOptions == 2)
2230 os << "\tBoundingBox 0bp 0bp "
2231 << opts["natwidth"] << " " << opts["natheight"] << '\n';
2232 else if (numberOfbbOptions > 0)
2233 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
2234 ostringstream special;
2235 if (opts.find("hiresbb") != opts.end())
2236 special << "hiresbb,";
2237 if (opts.find("trim") != opts.end())
2239 if (opts.find("viewport") != opts.end())
2240 special << "viewport=" << opts["viewport"] << ',';
2241 if (opts.find("totalheight") != opts.end())
2242 special << "totalheight=" << opts["totalheight"] << ',';
2243 if (opts.find("type") != opts.end())
2244 special << "type=" << opts["type"] << ',';
2245 if (opts.find("ext") != opts.end())
2246 special << "ext=" << opts["ext"] << ',';
2247 if (opts.find("read") != opts.end())
2248 special << "read=" << opts["read"] << ',';
2249 if (opts.find("command") != opts.end())
2250 special << "command=" << opts["command"] << ',';
2251 string s_special = special.str();
2252 if (!s_special.empty()) {
2253 // We had special arguments. Remove the trailing ','.
2254 os << "\tspecial " << s_special.substr(0, s_special.size() - 1) << '\n';
2256 // TODO: Handle the unknown settings better.
2257 // Warn about invalid options.
2258 // Check whether some option was given twice.
2262 else if (t.cs() == "footnote" ||
2263 (t.cs() == "thanks" && context.layout->intitle)) {
2265 context.check_layout(os);
2266 begin_inset(os, "Foot\n");
2267 os << "status collapsed\n\n";
2268 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
2272 else if (t.cs() == "marginpar") {
2274 context.check_layout(os);
2275 begin_inset(os, "Marginal\n");
2276 os << "status collapsed\n\n";
2277 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
2281 else if (t.cs() == "ensuremath") {
2283 context.check_layout(os);
2284 string const s = p.verbatim_item();
2285 //FIXME: this never triggers in UTF8
2286 if (s == "\xb1" || s == "\xb3" || s == "\xb2" || s == "\xb5")
2289 handle_ert(os, "\\ensuremath{" + s + "}",
2293 else if (t.cs() == "makeindex" || t.cs() == "maketitle") {
2294 // FIXME: Somehow prevent title layouts if
2295 // "maketitle" was not found
2297 skip_spaces_braces(p);
2300 else if (t.cs() == "tableofcontents") {
2301 context.check_layout(os);
2302 begin_command_inset(os, "toc", "tableofcontents");
2304 skip_spaces_braces(p);
2307 else if (t.cs() == "listoffigures") {
2308 context.check_layout(os);
2309 begin_inset(os, "FloatList figure\n");
2311 skip_spaces_braces(p);
2314 else if (t.cs() == "listoftables") {
2315 context.check_layout(os);
2316 begin_inset(os, "FloatList table\n");
2318 skip_spaces_braces(p);
2321 else if (t.cs() == "listof") {
2322 p.skip_spaces(true);
2323 string const name = p.get_token().cs();
2324 if (context.textclass.floats().typeExist(name)) {
2325 context.check_layout(os);
2326 begin_inset(os, "FloatList ");
2329 p.get_token(); // swallow second arg
2331 handle_ert(os, "\\listof{" + name + "}", context);
2334 else if (t.cs() == "textrm")
2335 parse_text_attributes(p, os, FLAG_ITEM, outer,
2336 context, "\\family",
2337 context.font.family, "roman");
2339 else if (t.cs() == "textsf")
2340 parse_text_attributes(p, os, FLAG_ITEM, outer,
2341 context, "\\family",
2342 context.font.family, "sans");
2344 else if (t.cs() == "texttt")
2345 parse_text_attributes(p, os, FLAG_ITEM, outer,
2346 context, "\\family",
2347 context.font.family, "typewriter");
2349 else if (t.cs() == "textmd")
2350 parse_text_attributes(p, os, FLAG_ITEM, outer,
2351 context, "\\series",
2352 context.font.series, "medium");
2354 else if (t.cs() == "textbf")
2355 parse_text_attributes(p, os, FLAG_ITEM, outer,
2356 context, "\\series",
2357 context.font.series, "bold");
2359 else if (t.cs() == "textup")
2360 parse_text_attributes(p, os, FLAG_ITEM, outer,
2362 context.font.shape, "up");
2364 else if (t.cs() == "textit")
2365 parse_text_attributes(p, os, FLAG_ITEM, outer,
2367 context.font.shape, "italic");
2369 else if (t.cs() == "textsl")
2370 parse_text_attributes(p, os, FLAG_ITEM, outer,
2372 context.font.shape, "slanted");
2374 else if (t.cs() == "textsc")
2375 parse_text_attributes(p, os, FLAG_ITEM, outer,
2377 context.font.shape, "smallcaps");
2379 else if (t.cs() == "textnormal" || t.cs() == "normalfont") {
2380 context.check_layout(os);
2381 TeXFont oldFont = context.font;
2382 context.font.init();
2383 context.font.size = oldFont.size;
2384 os << "\n\\family " << context.font.family << "\n";
2385 os << "\n\\series " << context.font.series << "\n";
2386 os << "\n\\shape " << context.font.shape << "\n";
2387 if (t.cs() == "textnormal") {
2388 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
2389 output_font_change(os, context.font, oldFont);
2390 context.font = oldFont;
2392 eat_whitespace(p, os, context, false);
2395 else if (t.cs() == "textcolor") {
2396 // scheme is \textcolor{color name}{text}
2397 string const color = p.verbatim_item();
2398 // we only support the predefined colors of the color package
2399 if (color == "black" || color == "blue" || color == "cyan"
2400 || color == "green" || color == "magenta" || color == "red"
2401 || color == "white" || color == "yellow") {
2402 context.check_layout(os);
2403 os << "\n\\color " << color << "\n";
2404 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
2405 context.check_layout(os);
2406 os << "\n\\color inherit\n";
2408 // for custom defined colors
2409 handle_ert(os, t.asInput() + "{" + color + "}", context);
2412 else if (t.cs() == "underbar" || t.cs() == "uline") {
2413 // \underbar is not 100% correct (LyX outputs \uline
2414 // of ulem.sty). The difference is that \ulem allows
2415 // line breaks, and \underbar does not.
2416 // Do NOT handle \underline.
2417 // \underbar cuts through y, g, q, p etc.,
2418 // \underline does not.
2419 context.check_layout(os);
2420 os << "\n\\bar under\n";
2421 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
2422 context.check_layout(os);
2423 os << "\n\\bar default\n";
2426 else if (t.cs() == "emph" || t.cs() == "noun") {
2427 context.check_layout(os);
2428 os << "\n\\" << t.cs() << " on\n";
2429 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
2430 context.check_layout(os);
2431 os << "\n\\" << t.cs() << " default\n";
2434 else if (t.cs() == "lyxline") {
2435 // swallow size argument (it is not used anyway)
2437 if (!context.atParagraphStart()) {
2438 // so our line is in the middle of a paragraph
2439 // we need to add a new line, lest this line
2440 // follow the other content on that line and
2441 // run off the side of the page
2442 // FIXME: This may create an empty paragraph,
2443 // but without that it would not be
2444 // possible to set noindent below.
2445 // Fortunately LaTeX does not care
2446 // about the empty paragraph.
2447 context.new_paragraph(os);
2449 if (h_paragraph_separation == "indent") {
2450 // we need to unindent, lest the line be too long
2451 context.add_par_extra_stuff("\\noindent\n");
2453 context.check_layout(os);
2454 begin_command_inset(os, "line", "rule");
2455 os << "offset \"0.5ex\"\n"
2456 "width \"100line%\"\n"
2461 else if (is_known(t.cs(), known_phrases) ||
2462 (t.cs() == "protect" &&
2463 p.next_token().cat() == catEscape &&
2464 is_known(p.next_token().cs(), known_phrases))) {
2465 // LyX sometimes puts a \protect in front, so we have to ignore it
2466 // FIXME: This needs to be changed when bug 4752 is fixed.
2467 char const * const * where = is_known(
2468 t.cs() == "protect" ? p.get_token().cs() : t.cs(),
2470 context.check_layout(os);
2471 os << known_coded_phrases[where - known_phrases];
2472 skip_spaces_braces(p);
2475 else if (is_known(t.cs(), known_ref_commands)) {
2476 string const opt = p.getOpt();
2478 context.check_layout(os);
2479 char const * const * where = is_known(t.cs(),
2480 known_ref_commands);
2481 begin_command_inset(os, "ref",
2482 known_coded_ref_commands[where - known_ref_commands]);
2483 os << "reference \""
2484 << convert_command_inset_arg(p.verbatim_item())
2488 // LyX does not support optional arguments of ref commands
2489 handle_ert(os, t.asInput() + '[' + opt + "]{" +
2490 p.verbatim_item() + "}", context);
2494 else if (use_natbib &&
2495 is_known(t.cs(), known_natbib_commands) &&
2496 ((t.cs() != "citefullauthor" &&
2497 t.cs() != "citeyear" &&
2498 t.cs() != "citeyearpar") ||
2499 p.next_token().asInput() != "*")) {
2500 context.check_layout(os);
2501 string command = t.cs();
2502 if (p.next_token().asInput() == "*") {
2506 if (command == "citefullauthor")
2507 // alternative name for "\\citeauthor*"
2508 command = "citeauthor*";
2510 // text before the citation
2512 // text after the citation
2514 get_cite_arguments(p, true, before, after);
2516 if (command == "cite") {
2517 // \cite without optional argument means
2518 // \citet, \cite with at least one optional
2519 // argument means \citep.
2520 if (before.empty() && after.empty())
2525 if (before.empty() && after == "[]")
2526 // avoid \citet[]{a}
2528 else if (before == "[]" && after == "[]") {
2529 // avoid \citet[][]{a}
2533 // remove the brackets around after and before
2534 if (!after.empty()) {
2536 after.erase(after.length() - 1, 1);
2537 after = convert_command_inset_arg(after);
2539 if (!before.empty()) {
2541 before.erase(before.length() - 1, 1);
2542 before = convert_command_inset_arg(before);
2544 begin_command_inset(os, "citation", command);
2545 os << "after " << '"' << after << '"' << "\n";
2546 os << "before " << '"' << before << '"' << "\n";
2548 << convert_command_inset_arg(p.verbatim_item())
2553 else if (use_jurabib &&
2554 is_known(t.cs(), known_jurabib_commands) &&
2555 (t.cs() == "cite" || p.next_token().asInput() != "*")) {
2556 context.check_layout(os);
2557 string command = t.cs();
2558 if (p.next_token().asInput() == "*") {
2562 char argumentOrder = '\0';
2563 vector<string> const & options = used_packages["jurabib"];
2564 if (find(options.begin(), options.end(),
2565 "natbiborder") != options.end())
2566 argumentOrder = 'n';
2567 else if (find(options.begin(), options.end(),
2568 "jurabiborder") != options.end())
2569 argumentOrder = 'j';
2571 // text before the citation
2573 // text after the citation
2575 get_cite_arguments(p, argumentOrder != 'j', before, after);
2577 string const citation = p.verbatim_item();
2578 if (!before.empty() && argumentOrder == '\0') {
2579 cerr << "Warning: Assuming argument order "
2580 "of jurabib version 0.6 for\n'"
2581 << command << before << after << '{'
2582 << citation << "}'.\n"
2583 "Add 'jurabiborder' to the jurabib "
2584 "package options if you used an\n"
2585 "earlier jurabib version." << endl;
2587 if (!after.empty()) {
2589 after.erase(after.length() - 1, 1);
2591 if (!before.empty()) {
2593 before.erase(before.length() - 1, 1);
2595 begin_command_inset(os, "citation", command);
2596 os << "after " << '"' << after << '"' << "\n";
2597 os << "before " << '"' << before << '"' << "\n";
2598 os << "key " << '"' << citation << '"' << "\n";
2602 else if (t.cs() == "cite"
2603 || t.cs() == "nocite") {
2604 context.check_layout(os);
2605 string after = convert_command_inset_arg(p.getArg('[', ']'));
2606 string key = convert_command_inset_arg(p.verbatim_item());
2607 // store the case that it is "\nocite{*}" to use it later for
2610 begin_command_inset(os, "citation", t.cs());
2611 os << "after " << '"' << after << '"' << "\n";
2612 os << "key " << '"' << key << '"' << "\n";
2614 } else if (t.cs() == "nocite")
2618 else if (t.cs() == "index") {
2619 context.check_layout(os);
2620 begin_inset(os, "Index idx\n");
2621 os << "status collapsed\n";
2622 parse_text_in_inset(p, os, FLAG_ITEM, false, context, "Index");
2626 else if (t.cs() == "nomenclature") {
2627 context.check_layout(os);
2628 begin_command_inset(os, "nomenclature", "nomenclature");
2629 string prefix = convert_command_inset_arg(p.getArg('[', ']'));
2630 if (!prefix.empty())
2631 os << "prefix " << '"' << prefix << '"' << "\n";
2632 os << "symbol " << '"'
2633 << convert_command_inset_arg(p.verbatim_item());
2634 os << "\"\ndescription \""
2635 << convert_command_inset_arg(p.verbatim_item())
2640 else if (t.cs() == "label") {
2641 context.check_layout(os);
2642 begin_command_inset(os, "label", "label");
2644 << convert_command_inset_arg(p.verbatim_item())
2649 else if (t.cs() == "printindex") {
2650 context.check_layout(os);
2651 begin_command_inset(os, "index_print", "printindex");
2652 os << "type \"idx\"\n";
2654 skip_spaces_braces(p);
2657 else if (t.cs() == "printnomenclature") {
2658 context.check_layout(os);
2659 begin_command_inset(os, "nomencl_print", "printnomenclature");
2661 skip_spaces_braces(p);
2664 else if ((t.cs() == "textsuperscript" || t.cs() == "textsubscript")) {
2665 context.check_layout(os);
2666 begin_inset(os, "script ");
2667 os << t.cs().substr(4) << '\n';
2668 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
2672 else if (is_known(t.cs(), known_quotes)) {
2673 char const * const * where = is_known(t.cs(), known_quotes);
2674 context.check_layout(os);
2675 begin_inset(os, "Quotes ");
2676 os << known_coded_quotes[where - known_quotes];
2678 // LyX adds {} after the quote, so we have to eat
2679 // spaces here if there are any before a possible
2681 eat_whitespace(p, os, context, false);
2685 else if (is_known(t.cs(), known_sizes) &&
2686 context.new_layout_allowed) {
2687 char const * const * where = is_known(t.cs(), known_sizes);
2688 context.check_layout(os);
2689 TeXFont const oldFont = context.font;
2690 context.font.size = known_coded_sizes[where - known_sizes];
2691 output_font_change(os, oldFont, context.font);
2692 eat_whitespace(p, os, context, false);
2695 else if (is_known(t.cs(), known_font_families) &&
2696 context.new_layout_allowed) {
2697 char const * const * where =
2698 is_known(t.cs(), known_font_families);
2699 context.check_layout(os);
2700 TeXFont const oldFont = context.font;
2701 context.font.family =
2702 known_coded_font_families[where - known_font_families];
2703 output_font_change(os, oldFont, context.font);
2704 eat_whitespace(p, os, context, false);
2707 else if (is_known(t.cs(), known_font_series) &&
2708 context.new_layout_allowed) {
2709 char const * const * where =
2710 is_known(t.cs(), known_font_series);
2711 context.check_layout(os);
2712 TeXFont const oldFont = context.font;
2713 context.font.series =
2714 known_coded_font_series[where - known_font_series];
2715 output_font_change(os, oldFont, context.font);
2716 eat_whitespace(p, os, context, false);
2719 else if (is_known(t.cs(), known_font_shapes) &&
2720 context.new_layout_allowed) {
2721 char const * const * where =
2722 is_known(t.cs(), known_font_shapes);
2723 context.check_layout(os);
2724 TeXFont const oldFont = context.font;
2725 context.font.shape =
2726 known_coded_font_shapes[where - known_font_shapes];
2727 output_font_change(os, oldFont, context.font);
2728 eat_whitespace(p, os, context, false);
2730 else if (is_known(t.cs(), known_old_font_families) &&
2731 context.new_layout_allowed) {
2732 char const * const * where =
2733 is_known(t.cs(), known_old_font_families);
2734 context.check_layout(os);
2735 TeXFont const oldFont = context.font;
2736 context.font.init();
2737 context.font.size = oldFont.size;
2738 context.font.family =
2739 known_coded_font_families[where - known_old_font_families];
2740 output_font_change(os, oldFont, context.font);
2741 eat_whitespace(p, os, context, false);
2744 else if (is_known(t.cs(), known_old_font_series) &&
2745 context.new_layout_allowed) {
2746 char const * const * where =
2747 is_known(t.cs(), known_old_font_series);
2748 context.check_layout(os);
2749 TeXFont const oldFont = context.font;
2750 context.font.init();
2751 context.font.size = oldFont.size;
2752 context.font.series =
2753 known_coded_font_series[where - known_old_font_series];
2754 output_font_change(os, oldFont, context.font);
2755 eat_whitespace(p, os, context, false);
2758 else if (is_known(t.cs(), known_old_font_shapes) &&
2759 context.new_layout_allowed) {
2760 char const * const * where =
2761 is_known(t.cs(), known_old_font_shapes);
2762 context.check_layout(os);
2763 TeXFont const oldFont = context.font;
2764 context.font.init();
2765 context.font.size = oldFont.size;
2766 context.font.shape =
2767 known_coded_font_shapes[where - known_old_font_shapes];
2768 output_font_change(os, oldFont, context.font);
2769 eat_whitespace(p, os, context, false);
2772 else if (t.cs() == "selectlanguage") {
2773 context.check_layout(os);
2774 // save the language for the case that a
2775 // \foreignlanguage is used
2777 context.font.language = babel2lyx(p.verbatim_item());
2778 os << "\n\\lang " << context.font.language << "\n";
2781 else if (t.cs() == "foreignlanguage") {
2782 string const lang = babel2lyx(p.verbatim_item());
2783 parse_text_attributes(p, os, FLAG_ITEM, outer,
2785 context.font.language, lang);
2788 else if (t.cs() == "inputencoding") {
2789 // nothing to write here
2790 string const enc = subst(p.verbatim_item(), "\n", " ");
2794 else if (t.cs() == "ldots") {
2795 context.check_layout(os);
2796 os << "\\SpecialChar \\ldots{}\n";
2797 skip_spaces_braces(p);
2800 else if (t.cs() == "lyxarrow") {
2801 context.check_layout(os);
2802 os << "\\SpecialChar \\menuseparator\n";
2803 skip_spaces_braces(p);
2806 else if (t.cs() == "textcompwordmark") {
2807 context.check_layout(os);
2808 os << "\\SpecialChar \\textcompwordmark{}\n";
2809 skip_spaces_braces(p);
2812 else if (t.cs() == "slash") {
2813 context.check_layout(os);
2814 os << "\\SpecialChar \\slash{}\n";
2815 skip_spaces_braces(p);
2818 else if (t.cs() == "nobreakdash" && p.next_token().asInput() == "-") {
2819 context.check_layout(os);
2820 os << "\\SpecialChar \\nobreakdash-\n";
2824 else if (t.cs() == "textquotedbl") {
2825 context.check_layout(os);
2830 else if (t.cs() == "@" && p.next_token().asInput() == ".") {
2831 context.check_layout(os);
2832 os << "\\SpecialChar \\@.\n";
2836 else if (t.cs() == "-") {
2837 context.check_layout(os);
2838 os << "\\SpecialChar \\-\n";
2841 else if (t.cs() == "textasciitilde") {
2842 context.check_layout(os);
2844 skip_spaces_braces(p);
2847 else if (t.cs() == "textasciicircum") {
2848 context.check_layout(os);
2850 skip_spaces_braces(p);
2853 else if (t.cs() == "textbackslash") {
2854 context.check_layout(os);
2855 os << "\n\\backslash\n";
2856 skip_spaces_braces(p);
2859 else if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#"
2860 || t.cs() == "$" || t.cs() == "{" || t.cs() == "}"
2862 context.check_layout(os);
2866 else if (t.cs() == "char") {
2867 context.check_layout(os);
2868 if (p.next_token().character() == '`') {
2870 if (p.next_token().cs() == "\"") {
2875 handle_ert(os, "\\char`", context);
2878 handle_ert(os, "\\char", context);
2882 else if (t.cs() == "verb") {
2883 context.check_layout(os);
2884 char const delimiter = p.next_token().character();
2885 string const arg = p.getArg(delimiter, delimiter);
2887 oss << "\\verb" << delimiter << arg << delimiter;
2888 handle_ert(os, oss.str(), context);
2891 // Problem: \= creates a tabstop inside the tabbing environment
2892 // and else an accent. In the latter case we really would want
2893 // \={o} instead of \= o.
2894 else if (t.cs() == "=" && (flags & FLAG_TABBING))
2895 handle_ert(os, t.asInput(), context);
2897 // accents (see Table 6 in Comprehensive LaTeX Symbol List)
2898 else if (t.cs().size() == 1
2899 && contains("\"'.=^`bcdHkrtuv~", t.cs())) {
2900 context.check_layout(os);
2901 // try to see whether the string is in unicodesymbols
2903 string command = t.asInput() + "{"
2904 + trimSpaceAndEol(p.verbatim_item())
2906 docstring s = encodings.fromLaTeXCommand(from_utf8(command), rem);
2909 cerr << "When parsing " << command
2910 << ", result is " << to_utf8(s)
2911 << "+" << to_utf8(rem) << endl;
2914 // we did not find a non-ert version
2915 handle_ert(os, command, context);
2918 else if (t.cs() == "\\") {
2919 context.check_layout(os);
2921 handle_ert(os, "\\\\" + p.getOpt(), context);
2922 else if (p.next_token().asInput() == "*") {
2924 // getOpt() eats the following space if there
2925 // is no optional argument, but that is OK
2926 // here since it has no effect in the output.
2927 handle_ert(os, "\\\\*" + p.getOpt(), context);
2930 begin_inset(os, "Newline newline");
2935 else if (t.cs() == "newline" ||
2936 (t.cs() == "linebreak" && !p.hasOpt())) {
2937 context.check_layout(os);
2938 begin_inset(os, "Newline ");
2941 skip_spaces_braces(p);
2944 else if (t.cs() == "input" || t.cs() == "include"
2945 || t.cs() == "verbatiminput") {
2946 string name = t.cs();
2947 if (t.cs() == "verbatiminput"
2948 && p.next_token().asInput() == "*")
2949 name += p.get_token().asInput();
2950 context.check_layout(os);
2951 string filename(normalize_filename(p.getArg('{', '}')));
2952 string const path = getMasterFilePath();
2953 // We want to preserve relative / absolute filenames,
2954 // therefore path is only used for testing
2955 if ((t.cs() == "include" || t.cs() == "input") &&
2956 !makeAbsPath(filename, path).exists()) {
2957 // The file extension is probably missing.
2958 // Now try to find it out.
2959 string const tex_name =
2960 find_file(filename, path,
2961 known_tex_extensions);
2962 if (!tex_name.empty())
2963 filename = tex_name;
2965 bool external = false;
2967 if (makeAbsPath(filename, path).exists()) {
2968 string const abstexname =
2969 makeAbsPath(filename, path).absFileName();
2970 string const abslyxname =
2971 changeExtension(abstexname, ".lyx");
2972 string const absfigname =
2973 changeExtension(abstexname, ".fig");
2974 fix_relative_filename(filename);
2975 string const lyxname =
2976 changeExtension(filename, ".lyx");
2978 external = FileName(absfigname).exists();
2979 if (t.cs() == "input") {
2980 string const ext = getExtension(abstexname);
2982 // Combined PS/LaTeX:
2983 // x.eps, x.pstex_t (old xfig)
2984 // x.pstex, x.pstex_t (new xfig, e.g. 3.2.5)
2985 FileName const absepsname(
2986 changeExtension(abstexname, ".eps"));
2987 FileName const abspstexname(
2988 changeExtension(abstexname, ".pstex"));
2989 bool const xfigeps =
2990 (absepsname.exists() ||
2991 abspstexname.exists()) &&
2994 // Combined PDF/LaTeX:
2995 // x.pdf, x.pdftex_t (old xfig)
2996 // x.pdf, x.pdf_t (new xfig, e.g. 3.2.5)
2997 FileName const abspdfname(
2998 changeExtension(abstexname, ".pdf"));
2999 bool const xfigpdf =
3000 abspdfname.exists() &&
3001 (ext == "pdftex_t" || ext == "pdf_t");
3005 // Combined PS/PDF/LaTeX:
3006 // x_pspdftex.eps, x_pspdftex.pdf, x.pspdftex
3007 string const absbase2(
3008 removeExtension(abstexname) + "_pspdftex");
3009 FileName const abseps2name(
3010 addExtension(absbase2, ".eps"));
3011 FileName const abspdf2name(
3012 addExtension(absbase2, ".pdf"));
3013 bool const xfigboth =
3014 abspdf2name.exists() &&
3015 abseps2name.exists() && ext == "pspdftex";
3017 xfig = xfigpdf || xfigeps || xfigboth;
3018 external = external && xfig;
3021 outname = changeExtension(filename, ".fig");
3023 // Don't try to convert, the result
3024 // would be full of ERT.
3026 } else if (t.cs() != "verbatiminput" &&
3027 tex2lyx(abstexname, FileName(abslyxname),
3034 cerr << "Warning: Could not find included file '"
3035 << filename << "'." << endl;
3039 begin_inset(os, "External\n");
3040 os << "\ttemplate XFig\n"
3041 << "\tfilename " << outname << '\n';
3043 begin_command_inset(os, "include", name);
3044 os << "preview false\n"
3045 "filename \"" << outname << "\"\n";
3050 else if (t.cs() == "bibliographystyle") {
3051 // store new bibliographystyle
3052 bibliographystyle = p.verbatim_item();
3053 // If any other command than \bibliography and
3054 // \nocite{*} follows, we need to output the style
3055 // (because it might be used by that command).
3056 // Otherwise, it will automatically be output by LyX.
3059 for (Token t2 = p.get_token(); p.good(); t2 = p.get_token()) {
3060 if (t2.cat() == catBegin)
3062 if (t2.cat() != catEscape)
3064 if (t2.cs() == "nocite") {
3065 if (p.getArg('{', '}') == "*")
3067 } else if (t2.cs() == "bibliography")
3074 "\\bibliographystyle{" + bibliographystyle + '}',
3079 else if (t.cs() == "bibliography") {
3080 context.check_layout(os);
3081 begin_command_inset(os, "bibtex", "bibtex");
3082 if (!btprint.empty()) {
3083 os << "btprint " << '"' << "btPrintAll" << '"' << "\n";
3084 // clear the string because the next BibTeX inset can be without the
3085 // \nocite{*} option
3088 os << "bibfiles " << '"' << p.verbatim_item() << '"' << "\n";
3089 // Do we have a bibliographystyle set?
3090 if (!bibliographystyle.empty())
3091 os << "options " << '"' << bibliographystyle << '"' << "\n";
3095 else if (t.cs() == "parbox")
3096 parse_box(p, os, 0, FLAG_ITEM, outer, context, "", "", t.cs());
3098 else if (t.cs() == "ovalbox" || t.cs() == "Ovalbox" ||
3099 t.cs() == "shadowbox" || t.cs() == "doublebox")
3100 parse_outer_box(p, os, FLAG_ITEM, outer, context, t.cs(), "");
3102 else if (t.cs() == "framebox") {
3103 string special = p.getFullOpt();
3104 special += p.getOpt();
3105 parse_outer_box(p, os, FLAG_ITEM, outer, context, t.cs(), special);
3108 //\makebox() is part of the picture environment and different from \makebox{}
3109 //\makebox{} will be parsed by parse_box when bug 2956 is fixed
3110 else if (t.cs() == "makebox") {
3111 string arg = t.asInput();
3112 if (p.next_token().character() == '(')
3113 //the syntax is: \makebox(x,y)[position]{content}
3114 arg += p.getFullParentheseArg();
3116 //the syntax is: \makebox[width][position]{content}
3117 arg += p.getFullOpt();
3118 handle_ert(os, arg + p.getFullOpt(), context);
3121 else if (t.cs() == "smallskip" ||
3122 t.cs() == "medskip" ||
3123 t.cs() == "bigskip" ||
3124 t.cs() == "vfill") {
3125 context.check_layout(os);
3126 begin_inset(os, "VSpace ");
3129 skip_spaces_braces(p);
3132 else if (is_known(t.cs(), known_spaces)) {
3133 char const * const * where = is_known(t.cs(), known_spaces);
3134 context.check_layout(os);
3135 begin_inset(os, "space ");
3136 os << '\\' << known_coded_spaces[where - known_spaces]
3139 // LaTeX swallows whitespace after all spaces except
3140 // "\\,". We have to do that here, too, because LyX
3141 // adds "{}" which would make the spaces significant.
3143 eat_whitespace(p, os, context, false);
3144 // LyX adds "{}" after all spaces except "\\ " and
3145 // "\\,", so we have to remove "{}".
3146 // "\\,{}" is equivalent to "\\," in LaTeX, so we
3147 // remove the braces after "\\,", too.
3152 else if (t.cs() == "newpage" ||
3153 (t.cs() == "pagebreak" && !p.hasOpt()) ||
3154 t.cs() == "clearpage" ||
3155 t.cs() == "cleardoublepage") {
3156 context.check_layout(os);
3157 begin_inset(os, "Newpage ");
3160 skip_spaces_braces(p);
3163 else if (t.cs() == "DeclareRobustCommand" ||
3164 t.cs() == "DeclareRobustCommandx" ||
3165 t.cs() == "newcommand" ||
3166 t.cs() == "newcommandx" ||
3167 t.cs() == "providecommand" ||
3168 t.cs() == "providecommandx" ||
3169 t.cs() == "renewcommand" ||
3170 t.cs() == "renewcommandx") {
3171 // DeclareRobustCommand, DeclareRobustCommandx,
3172 // providecommand and providecommandx could be handled
3173 // by parse_command(), but we need to call
3174 // add_known_command() here.
3175 string name = t.asInput();
3176 if (p.next_token().asInput() == "*") {
3177 // Starred form. Eat '*'
3181 string const command = p.verbatim_item();
3182 string const opt1 = p.getFullOpt();
3183 string const opt2 = p.getFullOpt();
3184 add_known_command(command, opt1, !opt2.empty());
3185 string const ert = name + '{' + command + '}' +
3187 '{' + p.verbatim_item() + '}';
3189 if (t.cs() == "DeclareRobustCommand" ||
3190 t.cs() == "DeclareRobustCommandx" ||
3191 t.cs() == "providecommand" ||
3192 t.cs() == "providecommandx" ||
3193 name[name.length()-1] == '*')
3194 handle_ert(os, ert, context);
3196 context.check_layout(os);
3197 begin_inset(os, "FormulaMacro");
3203 else if (t.cs() == "let" && p.next_token().asInput() != "*") {
3204 // let could be handled by parse_command(),
3205 // but we need to call add_known_command() here.
3206 string ert = t.asInput();
3209 if (p.next_token().cat() == catBegin) {
3210 name = p.verbatim_item();
3211 ert += '{' + name + '}';
3213 name = p.verbatim_item();
3218 if (p.next_token().cat() == catBegin) {
3219 command = p.verbatim_item();
3220 ert += '{' + command + '}';
3222 command = p.verbatim_item();
3225 // If command is known, make name known too, to parse
3226 // its arguments correctly. For this reason we also
3227 // have commands in syntax.default that are hardcoded.
3228 CommandMap::iterator it = known_commands.find(command);
3229 if (it != known_commands.end())
3230 known_commands[t.asInput()] = it->second;
3231 handle_ert(os, ert, context);
3234 else if (t.cs() == "hspace" || t.cs() == "vspace") {
3235 bool starred = false;
3236 if (p.next_token().asInput() == "*") {
3240 string name = t.asInput();
3241 string const length = p.verbatim_item();
3244 bool valid = splitLatexLength(length, valstring, unit);
3245 bool known_hspace = false;
3246 bool known_vspace = false;
3247 bool known_unit = false;
3250 istringstream iss(valstring);
3253 if (t.cs()[0] == 'h') {
3254 if (unit == "\\fill") {
3259 known_hspace = true;
3262 if (unit == "\\smallskipamount") {
3264 known_vspace = true;
3265 } else if (unit == "\\medskipamount") {
3267 known_vspace = true;
3268 } else if (unit == "\\bigskipamount") {
3270 known_vspace = true;
3271 } else if (unit == "\\fill") {
3273 known_vspace = true;
3277 if (!known_hspace && !known_vspace) {
3278 switch (unitFromString(unit)) {
3299 if (t.cs()[0] == 'h' && (known_unit || known_hspace)) {
3300 // Literal horizontal length or known variable
3301 context.check_layout(os);
3302 begin_inset(os, "space ");
3310 if (known_unit && !known_hspace)
3312 << translate_len(length);
3314 } else if (known_unit || known_vspace) {
3315 // Literal vertical length or known variable
3316 context.check_layout(os);
3317 begin_inset(os, "VSpace ");
3325 // LyX can't handle other length variables in Inset VSpace/space
3330 handle_ert(os, name + '{' + unit + '}', context);
3331 else if (value == -1.0)
3332 handle_ert(os, name + "{-" + unit + '}', context);
3334 handle_ert(os, name + '{' + valstring + unit + '}', context);
3336 handle_ert(os, name + '{' + length + '}', context);
3340 // The single '=' is meant here.
3341 else if ((newinsetlayout = findInsetLayout(context.textclass, t.cs(), true))) {
3343 context.check_layout(os);
3344 begin_inset(os, "Flex ");
3345 os << to_utf8(newinsetlayout->name()) << '\n'
3346 << "status collapsed\n";
3347 parse_text_in_inset(p, os, FLAG_ITEM, false, context, newinsetlayout);
3352 // try to see whether the string is in unicodesymbols
3353 // Only use text mode commands, since we are in text mode here,
3354 // and math commands may be invalid (bug 6797)
3356 docstring s = encodings.fromLaTeXCommand(from_utf8(t.asInput()),
3357 rem, Encodings::TEXT_CMD);
3360 cerr << "When parsing " << t.cs()
3361 << ", result is " << to_utf8(s)
3362 << "+" << to_utf8(rem) << endl;
3363 context.check_layout(os);
3365 skip_spaces_braces(p);
3367 //cerr << "#: " << t << " mode: " << mode << endl;
3368 // heuristic: read up to next non-nested space
3370 string s = t.asInput();
3371 string z = p.verbatim_item();
3372 while (p.good() && z != " " && z.size()) {
3373 //cerr << "read: " << z << endl;
3375 z = p.verbatim_item();
3377 cerr << "found ERT: " << s << endl;
3378 handle_ert(os, s + ' ', context);
3381 string name = t.asInput();
3382 if (p.next_token().asInput() == "*") {
3383 // Starred commands like \vspace*{}
3384 p.get_token(); // Eat '*'
3387 if (!parse_command(name, p, os, outer, context))
3388 handle_ert(os, name, context);
3392 if (flags & FLAG_LEAVE) {
3393 flags &= ~FLAG_LEAVE;