2 * \file tex2lyx/text.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Jean-Marc Lasgouttes
10 * Full author contact details are available in file CREDITS.
21 #include "FloatList.h"
25 #include "support/lassert.h"
26 #include "support/convert.h"
27 #include "support/FileName.h"
28 #include "support/filetools.h"
29 #include "support/lstrings.h"
38 using namespace lyx::support;
43 void parse_text_in_inset(Parser & p, ostream & os, unsigned flags, bool outer,
44 Context const & context)
46 Context newcontext(true, context.textclass);
47 newcontext.font = context.font;
48 parse_text(p, os, flags, outer, newcontext);
49 newcontext.check_end_layout(os);
55 /// parses a paragraph snippet, useful for example for \\emph{...}
56 void parse_text_snippet(Parser & p, ostream & os, unsigned flags, bool outer,
59 Context newcontext(context);
60 // Don't inherit the paragraph-level extra stuff
61 newcontext.par_extra_stuff.clear();
62 parse_text(p, os, flags, outer, newcontext);
63 // Make sure that we don't create invalid .lyx files
64 context.need_layout = newcontext.need_layout;
65 context.need_end_layout = newcontext.need_end_layout;
70 * Thin wrapper around parse_text_snippet() using a string.
72 * We completely ignore \c context.need_layout and \c context.need_end_layout,
73 * because our return value is not used directly (otherwise the stream version
74 * of parse_text_snippet() could be used). That means that the caller needs
75 * to do layout management manually.
76 * This is intended to parse text that does not create any layout changes.
78 string parse_text_snippet(Parser & p, unsigned flags, const bool outer,
81 Context newcontext(context);
82 newcontext.need_layout = false;
83 newcontext.need_end_layout = false;
84 newcontext.new_layout_allowed = false;
85 // Avoid warning by Context::~Context()
86 newcontext.par_extra_stuff.clear();
88 parse_text_snippet(p, os, flags, outer, newcontext);
93 char const * const known_ref_commands[] = { "ref", "pageref", "vref",
94 "vpageref", "prettyref", "eqref", 0 };
98 * The starred forms are also known.
100 char const * const known_natbib_commands[] = { "cite", "citet", "citep",
101 "citealt", "citealp", "citeauthor", "citeyear", "citeyearpar",
102 "citefullauthor", "Citet", "Citep", "Citealt", "Citealp", "Citeauthor", 0 };
106 * No starred form other than "cite*" known.
108 char const * const known_jurabib_commands[] = { "cite", "citet", "citep",
109 "citealt", "citealp", "citeauthor", "citeyear", "citeyearpar",
110 // jurabib commands not (yet) supported by LyX:
112 // "footcite", "footcitet", "footcitep", "footcitealt", "footcitealp",
113 // "footciteauthor", "footciteyear", "footciteyearpar",
114 "citefield", "citetitle", "cite*", 0 };
116 /// LaTeX names for quotes
117 char const * const known_quotes[] = { "dq", "guillemotleft", "flqq", "og",
118 "guillemotright", "frqq", "fg", "glq", "glqq", "textquoteleft", "grq", "grqq",
119 "quotedblbase", "textquotedblleft", "quotesinglbase", "textquoteright", "flq",
120 "guilsinglleft", "frq", "guilsinglright", 0};
122 /// the same as known_quotes with .lyx names
123 char const * const known_coded_quotes[] = { "prd", "ard", "ard", "ard",
124 "ald", "ald", "ald", "gls", "gld", "els", "els", "grd",
125 "gld", "grd", "gls", "ers", "fls",
126 "fls", "frs", "frs", 0};
128 /// LaTeX names for font sizes
129 char const * const known_sizes[] = { "tiny", "scriptsize", "footnotesize",
130 "small", "normalsize", "large", "Large", "LARGE", "huge", "Huge", 0};
132 /// the same as known_sizes with .lyx names plus a default entry
133 char const * const known_coded_sizes[] = { "default", "tiny", "scriptsize", "footnotesize",
134 "small", "normal", "large", "larger", "largest", "huge", "giant", 0};
136 /// LaTeX 2.09 names for font families
137 char const * const known_old_font_families[] = { "rm", "sf", "tt", 0};
139 /// LaTeX names for font families
140 char const * const known_font_families[] = { "rmfamily", "sffamily",
143 /// the same as known_old_font_families and known_font_families with .lyx names
144 char const * const known_coded_font_families[] = { "roman", "sans",
147 /// LaTeX 2.09 names for font series
148 char const * const known_old_font_series[] = { "bf", 0};
150 /// LaTeX names for font series
151 char const * const known_font_series[] = { "bfseries", "mdseries", 0};
153 /// the same as known_old_font_series and known_font_series with .lyx names
154 char const * const known_coded_font_series[] = { "bold", "medium", 0};
156 /// LaTeX 2.09 names for font shapes
157 char const * const known_old_font_shapes[] = { "it", "sl", "sc", 0};
159 /// LaTeX names for font shapes
160 char const * const known_font_shapes[] = { "itshape", "slshape", "scshape",
163 /// the same as known_old_font_shapes and known_font_shapes with .lyx names
164 char const * const known_coded_font_shapes[] = { "italic", "slanted",
165 "smallcaps", "up", 0};
168 * Graphics file extensions known by the dvips driver of the graphics package.
169 * These extensions are used to complete the filename of an included
170 * graphics file if it does not contain an extension.
171 * The order must be the same that latex uses to find a file, because we
172 * will use the first extension that matches.
173 * This is only an approximation for the common cases. If we would want to
174 * do it right in all cases, we would need to know which graphics driver is
175 * used and know the extensions of every driver of the graphics package.
177 char const * const known_dvips_graphics_formats[] = {"eps", "ps", "eps.gz",
178 "ps.gz", "eps.Z", "ps.Z", 0};
181 * Graphics file extensions known by the pdftex driver of the graphics package.
182 * \sa known_dvips_graphics_formats
184 char const * const known_pdftex_graphics_formats[] = {"png", "pdf", "jpg",
188 * Known file extensions for TeX files as used by \\include.
190 char const * const known_tex_extensions[] = {"tex", 0};
192 /// spaces known by InsetSpace
193 char const * const known_spaces[] = { " ", "space", ",", "thinspace", "quad",
194 "qquad", "enspace", "enskip", "negthinspace", 0};
196 /// the same as known_spaces with .lyx names
197 char const * const known_coded_spaces[] = { "space{}", "space{}",
198 "thinspace{}", "thinspace{}", "quad{}", "qquad{}", "enspace{}", "enskip{}",
199 "negthinspace{}", 0};
202 /// splits "x=z, y=b" into a map
203 map<string, string> split_map(string const & s)
205 map<string, string> res;
208 for (size_t i = 0; i < v.size(); ++i) {
209 size_t const pos = v[i].find('=');
210 string const index = v[i].substr(0, pos);
211 string const value = v[i].substr(pos + 1, string::npos);
212 res[trim(index)] = trim(value);
219 * Split a LaTeX length into value and unit.
220 * The latter can be a real unit like "pt", or a latex length variable
221 * like "\textwidth". The unit may contain additional stuff like glue
222 * lengths, but we don't care, because such lengths are ERT anyway.
223 * \returns true if \p value and \p unit are valid.
225 bool splitLatexLength(string const & len, string & value, string & unit)
229 const string::size_type i = len.find_first_not_of(" -+0123456789.,");
230 //'4,5' is a valid LaTeX length number. Change it to '4.5'
231 string const length = subst(len, ',', '.');
232 if (i == string::npos)
235 if (len[0] == '\\') {
236 // We had something like \textwidth without a factor
242 value = trim(string(length, 0, i));
246 // 'cM' is a valid LaTeX length unit. Change it to 'cm'
247 if (contains(len, '\\'))
248 unit = trim(string(len, i));
250 unit = ascii_lowercase(trim(string(len, i)));
255 /// A simple function to translate a latex length to something LyX can
256 /// understand. Not perfect, but rather best-effort.
257 bool translate_len(string const & length, string & valstring, string & unit)
259 if (!splitLatexLength(length, valstring, unit))
261 // LyX uses percent values
263 istringstream iss(valstring);
268 string const percentval = oss.str();
270 if (unit.empty() || unit[0] != '\\')
272 string::size_type const i = unit.find(' ');
273 string const endlen = (i == string::npos) ? string() : string(unit, i);
274 if (unit == "\\textwidth") {
275 valstring = percentval;
276 unit = "text%" + endlen;
277 } else if (unit == "\\columnwidth") {
278 valstring = percentval;
279 unit = "col%" + endlen;
280 } else if (unit == "\\paperwidth") {
281 valstring = percentval;
282 unit = "page%" + endlen;
283 } else if (unit == "\\linewidth") {
284 valstring = percentval;
285 unit = "line%" + endlen;
286 } else if (unit == "\\paperheight") {
287 valstring = percentval;
288 unit = "pheight%" + endlen;
289 } else if (unit == "\\textheight") {
290 valstring = percentval;
291 unit = "theight%" + endlen;
299 string translate_len(string const & length)
303 if (translate_len(length, value, unit))
305 // If the input is invalid, return what we have.
313 * Translates a LaTeX length into \p value, \p unit and
314 * \p special parts suitable for a box inset.
315 * The difference from translate_len() is that a box inset knows about
316 * some special "units" that are stored in \p special.
318 void translate_box_len(string const & length, string & value, string & unit, string & special)
320 if (translate_len(length, value, unit)) {
321 if (unit == "\\height" || unit == "\\depth" ||
322 unit == "\\totalheight" || unit == "\\width") {
323 special = unit.substr(1);
324 // The unit is not used, but LyX requires a dummy setting
337 * Find a file with basename \p name in path \p path and an extension
340 string find_file(string const & name, string const & path,
341 char const * const * extensions)
343 for (char const * const * what = extensions; *what; ++what) {
344 string const trial = addExtension(name, *what);
345 if (makeAbsPath(trial, path).exists())
352 void begin_inset(ostream & os, string const & name)
354 os << "\n\\begin_inset " << name;
357 /*// use this void when format 288 is supported
358 void begin_command_inset(ostream & os, string const & name,
359 string const & latexname)
361 os << "\n\\begin_inset CommandInset " << name;
362 os << "\nLatexCommand " << latexname << "\n";
366 void end_inset(ostream & os)
368 os << "\n\\end_inset\n\n";
372 void skip_braces(Parser & p)
374 if (p.next_token().cat() != catBegin)
377 if (p.next_token().cat() == catEnd) {
385 void handle_ert(ostream & os, string const & s, Context & context)
387 // We must have a valid layout before outputting the ERT inset.
388 context.check_layout(os);
389 Context newcontext(true, context.textclass);
390 begin_inset(os, "ERT");
391 os << "\nstatus collapsed\n";
392 newcontext.check_layout(os);
393 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
395 os << "\n\\backslash\n";
396 else if (*it == '\n') {
397 newcontext.new_paragraph(os);
398 newcontext.check_layout(os);
402 newcontext.check_end_layout(os);
407 void handle_comment(ostream & os, string const & s, Context & context)
409 // TODO: Handle this better
410 Context newcontext(true, context.textclass);
411 begin_inset(os, "ERT");
412 os << "\nstatus collapsed\n";
413 newcontext.check_layout(os);
414 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
416 os << "\n\\backslash\n";
420 // make sure that our comment is the last thing on the line
421 newcontext.new_paragraph(os);
422 newcontext.check_layout(os);
423 newcontext.check_end_layout(os);
428 Layout const * findLayout(TextClass const & textclass, string const & name)
430 DocumentClass::const_iterator lit = textclass.begin();
431 DocumentClass::const_iterator len = textclass.end();
432 for (; lit != len; ++lit)
433 if (lit->latexname() == name)
439 void eat_whitespace(Parser &, ostream &, Context &, bool);
442 void output_command_layout(ostream & os, Parser & p, bool outer,
443 Context & parent_context,
444 Layout const * newlayout)
446 parent_context.check_end_layout(os);
447 Context context(true, parent_context.textclass, newlayout,
448 parent_context.layout, parent_context.font);
449 if (parent_context.deeper_paragraph) {
450 // We are beginning a nested environment after a
451 // deeper paragraph inside the outer list environment.
452 // Therefore we don't need to output a "begin deeper".
453 context.need_end_deeper = true;
455 context.check_deeper(os);
456 context.check_layout(os);
457 unsigned int optargs = 0;
458 while (optargs < context.layout->optargs) {
459 eat_whitespace(p, os, context, false);
460 if (p.next_token().character() == '[') {
461 p.get_token(); // eat '['
462 begin_inset(os, "OptArg\n");
463 os << "status collapsed\n\n";
464 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
466 eat_whitespace(p, os, context, false);
470 unsigned int reqargs = 0;
471 while (reqargs < context.layout->reqargs) {
472 eat_whitespace(p, os, context, false);
473 if (p.next_token().character() == '{') {
474 p.get_token(); // eat '['
475 begin_inset(os, "OptArg\n");
476 os << "status collapsed\n\n";
477 parse_text_in_inset(p, os, FLAG_BRACE_LAST, outer, context);
479 eat_whitespace(p, os, context, false);
483 parse_text(p, os, FLAG_ITEM, outer, context);
484 context.check_end_layout(os);
485 if (parent_context.deeper_paragraph) {
486 // We must suppress the "end deeper" because we
487 // suppressed the "begin deeper" above.
488 context.need_end_deeper = false;
490 context.check_end_deeper(os);
491 // We don't need really a new paragraph, but
492 // we must make sure that the next item gets a \begin_layout.
493 parent_context.new_paragraph(os);
498 * Output a space if necessary.
499 * This function gets called for every whitespace token.
501 * We have three cases here:
502 * 1. A space must be suppressed. Example: The lyxcode case below
503 * 2. A space may be suppressed. Example: Spaces before "\par"
504 * 3. A space must not be suppressed. Example: A space between two words
506 * We currently handle only 1. and 3 and from 2. only the case of
507 * spaces before newlines as a side effect.
509 * 2. could be used to suppress as many spaces as possible. This has two effects:
510 * - Reimporting LyX generated LaTeX files changes almost no whitespace
511 * - Superflous whitespace from non LyX generated LaTeX files is removed.
512 * The drawback is that the logic inside the function becomes
513 * complicated, and that is the reason why it is not implemented.
515 void check_space(Parser & p, ostream & os, Context & context)
517 Token const next = p.next_token();
518 Token const curr = p.curr_token();
519 // A space before a single newline and vice versa must be ignored
520 // LyX emits a newline before \end{lyxcode}.
521 // This newline must be ignored,
522 // otherwise LyX will add an additional protected space.
523 if (next.cat() == catSpace ||
524 next.cat() == catNewline ||
525 (next.cs() == "end" && context.layout->free_spacing && curr.cat() == catNewline)) {
528 context.check_layout(os);
534 * Parse all arguments of \p command
536 void parse_arguments(string const & command,
537 vector<ArgumentType> const & template_arguments,
538 Parser & p, ostream & os, bool outer, Context & context)
540 string ert = command;
541 size_t no_arguments = template_arguments.size();
542 for (size_t i = 0; i < no_arguments; ++i) {
543 switch (template_arguments[i]) {
545 // This argument contains regular LaTeX
546 handle_ert(os, ert + '{', context);
547 eat_whitespace(p, os, context, false);
548 parse_text(p, os, FLAG_ITEM, outer, context);
552 // This argument may contain special characters
553 ert += '{' + p.verbatim_item() + '}';
560 handle_ert(os, ert, context);
565 * Check whether \p command is a known command. If yes,
566 * handle the command with all arguments.
567 * \return true if the command was parsed, false otherwise.
569 bool parse_command(string const & command, Parser & p, ostream & os,
570 bool outer, Context & context)
572 if (known_commands.find(command) != known_commands.end()) {
573 parse_arguments(command, known_commands[command], p, os,
581 /// Parses a minipage or parbox
582 void parse_box(Parser & p, ostream & os, unsigned flags, bool outer,
583 Context & parent_context, bool use_parbox)
587 // We need to set the height to the LaTeX default of 1\\totalheight
588 // for the case when no height argument is given
589 string height_value = "1";
590 string height_unit = "in";
591 string height_special = "totalheight";
593 if (p.next_token().asInput() == "[") {
594 position = p.getArg('[', ']');
595 if (position != "t" && position != "c" && position != "b") {
597 cerr << "invalid position for minipage/parbox" << endl;
599 if (p.next_token().asInput() == "[") {
600 latex_height = p.getArg('[', ']');
601 translate_box_len(latex_height, height_value, height_unit, height_special);
603 if (p.next_token().asInput() == "[") {
604 inner_pos = p.getArg('[', ']');
605 if (inner_pos != "c" && inner_pos != "t" &&
606 inner_pos != "b" && inner_pos != "s") {
607 inner_pos = position;
608 cerr << "invalid inner_pos for minipage/parbox"
616 string const latex_width = p.verbatim_item();
617 translate_len(latex_width, width_value, width_unit);
618 if (contains(width_unit, '\\') || contains(height_unit, '\\')) {
619 // LyX can't handle length variables
624 ss << "\\begin{minipage}";
625 if (!position.empty())
626 ss << '[' << position << ']';
627 if (!latex_height.empty())
628 ss << '[' << latex_height << ']';
629 if (!inner_pos.empty())
630 ss << '[' << inner_pos << ']';
631 ss << "{" << latex_width << "}";
634 handle_ert(os, ss.str(), parent_context);
635 parent_context.new_paragraph(os);
636 parse_text_in_inset(p, os, flags, outer, parent_context);
638 handle_ert(os, "}", parent_context);
640 handle_ert(os, "\\end{minipage}", parent_context);
642 // LyX does not like empty positions, so we have
643 // to set them to the LaTeX default values here.
644 if (position.empty())
646 if (inner_pos.empty())
647 inner_pos = position;
648 parent_context.check_layout(os);
649 begin_inset(os, "Box Frameless\n");
650 os << "position \"" << position << "\"\n";
651 os << "hor_pos \"c\"\n";
652 os << "has_inner_box 1\n";
653 os << "inner_pos \"" << inner_pos << "\"\n";
654 os << "use_parbox " << use_parbox << "\n";
655 os << "width \"" << width_value << width_unit << "\"\n";
656 os << "special \"none\"\n";
657 os << "height \"" << height_value << height_unit << "\"\n";
658 os << "height_special \"" << height_special << "\"\n";
659 os << "status open\n\n";
660 parse_text_in_inset(p, os, flags, outer, parent_context);
662 #ifdef PRESERVE_LAYOUT
663 // LyX puts a % after the end of the minipage
664 if (p.next_token().cat() == catNewline && p.next_token().cs().size() > 1) {
666 //handle_comment(os, "%dummy", parent_context);
669 parent_context.new_paragraph(os);
671 else if (p.next_token().cat() == catSpace || p.next_token().cat() == catNewline) {
672 //handle_comment(os, "%dummy", parent_context);
675 // We add a protected space if something real follows
676 if (p.good() && p.next_token().cat() != catComment) {
677 os << "\\InsetSpace ~\n";
685 /// parse an unknown environment
686 void parse_unknown_environment(Parser & p, string const & name, ostream & os,
687 unsigned flags, bool outer,
688 Context & parent_context)
690 if (name == "tabbing")
691 // We need to remember that we have to handle '\=' specially
692 flags |= FLAG_TABBING;
694 // We need to translate font changes and paragraphs inside the
695 // environment to ERT if we have a non standard font.
696 // Otherwise things like
697 // \large\begin{foo}\huge bar\end{foo}
699 bool const specialfont =
700 (parent_context.font != parent_context.normalfont);
701 bool const new_layout_allowed = parent_context.new_layout_allowed;
703 parent_context.new_layout_allowed = false;
704 handle_ert(os, "\\begin{" + name + "}", parent_context);
705 parse_text_snippet(p, os, flags, outer, parent_context);
706 handle_ert(os, "\\end{" + name + "}", parent_context);
708 parent_context.new_layout_allowed = new_layout_allowed;
712 void parse_environment(Parser & p, ostream & os, bool outer,
713 Context & parent_context)
715 Layout const * newlayout;
716 string const name = p.getArg('{', '}');
717 const bool is_starred = suffixIs(name, '*');
718 string const unstarred_name = rtrim(name, "*");
719 active_environments.push_back(name);
721 if (is_math_env(name)) {
722 parent_context.check_layout(os);
723 begin_inset(os, "Formula ");
724 os << "\\begin{" << name << "}";
725 parse_math(p, os, FLAG_END, MATH_MODE);
726 os << "\\end{" << name << "}";
730 else if (name == "tabular" || name == "longtable") {
731 eat_whitespace(p, os, parent_context, false);
732 parent_context.check_layout(os);
733 begin_inset(os, "Tabular ");
734 handle_tabular(p, os, name == "longtable", parent_context);
739 else if (parent_context.textclass.floats().typeExist(unstarred_name)) {
740 eat_whitespace(p, os, parent_context, false);
741 parent_context.check_layout(os);
742 begin_inset(os, "Float " + unstarred_name + "\n");
743 if (p.next_token().asInput() == "[") {
744 os << "placement " << p.getArg('[', ']') << '\n';
746 os << "wide " << convert<string>(is_starred)
747 << "\nsideways false"
748 << "\nstatus open\n\n";
749 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
751 // We don't need really a new paragraph, but
752 // we must make sure that the next item gets a \begin_layout.
753 parent_context.new_paragraph(os);
757 else if (name == "minipage") {
758 eat_whitespace(p, os, parent_context, false);
759 parse_box(p, os, FLAG_END, outer, parent_context, false);
763 else if (name == "comment") {
764 eat_whitespace(p, os, parent_context, false);
765 parent_context.check_layout(os);
766 begin_inset(os, "Note Comment\n");
767 os << "status open\n";
768 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
771 skip_braces(p); // eat {} that might by set by LyX behind comments
774 else if (name == "lyxgreyedout") {
775 eat_whitespace(p, os, parent_context, false);
776 parent_context.check_layout(os);
777 begin_inset(os, "Note Greyedout\n");
778 os << "status open\n";
779 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
784 else if (name == "framed") {
785 eat_whitespace(p, os, parent_context, false);
786 parent_context.check_layout(os);
787 begin_inset(os, "Note Framed\n");
788 os << "status open\n";
789 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
794 else if (name == "shaded") {
795 eat_whitespace(p, os, parent_context, false);
796 parent_context.check_layout(os);
797 begin_inset(os, "Note Shaded\n");
798 os << "status open\n";
799 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
804 else if (!parent_context.new_layout_allowed)
805 parse_unknown_environment(p, name, os, FLAG_END, outer,
808 // Alignment and spacing settings
809 // FIXME (bug xxxx): These settings can span multiple paragraphs and
810 // therefore are totally broken!
811 // Note that \centering, raggedright, and raggedleft cannot be handled, as
812 // they are commands not environments. They are furthermore switches that
813 // can be ended by another switches, but also by commands like \footnote or
814 // \parbox. So the only safe way is to leave them untouched.
815 else if (name == "center" || name == "centering" ||
816 name == "flushleft" || name == "flushright" ||
817 name == "singlespace" || name == "onehalfspace" ||
818 name == "doublespace" || name == "spacing") {
819 eat_whitespace(p, os, parent_context, false);
820 // We must begin a new paragraph if not already done
821 if (! parent_context.atParagraphStart()) {
822 parent_context.check_end_layout(os);
823 parent_context.new_paragraph(os);
825 if (name == "flushleft")
826 parent_context.add_extra_stuff("\\align left\n");
827 else if (name == "flushright")
828 parent_context.add_extra_stuff("\\align right\n");
829 else if (name == "center" || name == "centering")
830 parent_context.add_extra_stuff("\\align center\n");
831 else if (name == "singlespace")
832 parent_context.add_extra_stuff("\\paragraph_spacing single\n");
833 else if (name == "onehalfspace")
834 parent_context.add_extra_stuff("\\paragraph_spacing onehalf\n");
835 else if (name == "doublespace")
836 parent_context.add_extra_stuff("\\paragraph_spacing double\n");
837 else if (name == "spacing")
838 parent_context.add_extra_stuff("\\paragraph_spacing other " + p.verbatim_item() + "\n");
839 parse_text(p, os, FLAG_END, outer, parent_context);
840 // Just in case the environment is empty
841 parent_context.extra_stuff.erase();
842 // We must begin a new paragraph to reset the alignment
843 parent_context.new_paragraph(os);
847 // The single '=' is meant here.
848 else if ((newlayout = findLayout(parent_context.textclass, name)) &&
849 newlayout->isEnvironment()) {
850 eat_whitespace(p, os, parent_context, false);
851 Context context(true, parent_context.textclass, newlayout,
852 parent_context.layout, parent_context.font);
853 if (parent_context.deeper_paragraph) {
854 // We are beginning a nested environment after a
855 // deeper paragraph inside the outer list environment.
856 // Therefore we don't need to output a "begin deeper".
857 context.need_end_deeper = true;
859 parent_context.check_end_layout(os);
860 switch (context.layout->latextype) {
861 case LATEX_LIST_ENVIRONMENT:
862 context.add_par_extra_stuff("\\labelwidthstring "
863 + p.verbatim_item() + '\n');
866 case LATEX_BIB_ENVIRONMENT:
867 p.verbatim_item(); // swallow next arg
873 context.check_deeper(os);
874 parse_text(p, os, FLAG_END, outer, context);
875 context.check_end_layout(os);
876 if (parent_context.deeper_paragraph) {
877 // We must suppress the "end deeper" because we
878 // suppressed the "begin deeper" above.
879 context.need_end_deeper = false;
881 context.check_end_deeper(os);
882 parent_context.new_paragraph(os);
886 else if (name == "appendix") {
887 // This is no good latex style, but it works and is used in some documents...
888 eat_whitespace(p, os, parent_context, false);
889 parent_context.check_end_layout(os);
890 Context context(true, parent_context.textclass, parent_context.layout,
891 parent_context.layout, parent_context.font);
892 context.check_layout(os);
893 os << "\\start_of_appendix\n";
894 parse_text(p, os, FLAG_END, outer, context);
895 context.check_end_layout(os);
899 else if (known_environments.find(name) != known_environments.end()) {
900 vector<ArgumentType> arguments = known_environments[name];
901 // The last "argument" denotes wether we may translate the
902 // environment contents to LyX
903 // The default required if no argument is given makes us
904 // compatible with the reLyXre environment.
905 ArgumentType contents = arguments.empty() ?
908 if (!arguments.empty())
909 arguments.pop_back();
910 // See comment in parse_unknown_environment()
911 bool const specialfont =
912 (parent_context.font != parent_context.normalfont);
913 bool const new_layout_allowed =
914 parent_context.new_layout_allowed;
916 parent_context.new_layout_allowed = false;
917 parse_arguments("\\begin{" + name + "}", arguments, p, os,
918 outer, parent_context);
919 if (contents == verbatim)
920 handle_ert(os, p.verbatimEnvironment(name),
923 parse_text_snippet(p, os, FLAG_END, outer,
925 handle_ert(os, "\\end{" + name + "}", parent_context);
927 parent_context.new_layout_allowed = new_layout_allowed;
931 parse_unknown_environment(p, name, os, FLAG_END, outer,
934 active_environments.pop_back();
938 /// parses a comment and outputs it to \p os.
939 void parse_comment(Parser & p, ostream & os, Token const & t, Context & context)
941 LASSERT(t.cat() == catComment, return);
942 if (!t.cs().empty()) {
943 context.check_layout(os);
944 handle_comment(os, '%' + t.cs(), context);
945 if (p.next_token().cat() == catNewline) {
946 // A newline after a comment line starts a new
948 if (context.new_layout_allowed) {
949 if(!context.atParagraphStart())
950 // Only start a new paragraph if not already
951 // done (we might get called recursively)
952 context.new_paragraph(os);
954 handle_ert(os, "\n", context);
955 eat_whitespace(p, os, context, true);
965 * Reads spaces and comments until the first non-space, non-comment token.
966 * New paragraphs (double newlines or \\par) are handled like simple spaces
967 * if \p eatParagraph is true.
968 * Spaces are skipped, but comments are written to \p os.
970 void eat_whitespace(Parser & p, ostream & os, Context & context,
974 Token const & t = p.get_token();
975 if (t.cat() == catComment)
976 parse_comment(p, os, t, context);
977 else if ((! eatParagraph && p.isParagraph()) ||
978 (t.cat() != catSpace && t.cat() != catNewline)) {
987 * Set a font attribute, parse text and reset the font attribute.
988 * \param attribute Attribute name (e.g. \\family, \\shape etc.)
989 * \param currentvalue Current value of the attribute. Is set to the new
990 * value during parsing.
991 * \param newvalue New value of the attribute
993 void parse_text_attributes(Parser & p, ostream & os, unsigned flags, bool outer,
994 Context & context, string const & attribute,
995 string & currentvalue, string const & newvalue)
997 context.check_layout(os);
998 string const oldvalue = currentvalue;
999 currentvalue = newvalue;
1000 os << '\n' << attribute << ' ' << newvalue << "\n";
1001 parse_text_snippet(p, os, flags, outer, context);
1002 context.check_layout(os);
1003 os << '\n' << attribute << ' ' << oldvalue << "\n";
1004 currentvalue = oldvalue;
1008 /// get the arguments of a natbib or jurabib citation command
1009 void get_cite_arguments(Parser & p, bool natbibOrder,
1010 string & before, string & after)
1012 // We need to distinguish "" and "[]", so we can't use p.getOpt().
1014 // text before the citation
1016 // text after the citation
1017 after = p.getFullOpt();
1019 if (!after.empty()) {
1020 before = p.getFullOpt();
1021 if (natbibOrder && !before.empty())
1022 swap(before, after);
1027 /// Convert filenames with TeX macros and/or quotes to something LyX
1029 string const normalize_filename(string const & name)
1031 Parser p(trim(name, "\""));
1034 Token const & t = p.get_token();
1035 if (t.cat() != catEscape)
1037 else if (t.cs() == "lyxdot") {
1038 // This is used by LyX for simple dots in relative
1042 } else if (t.cs() == "space") {
1052 /// Convert \p name from TeX convention (relative to master file) to LyX
1053 /// convention (relative to .lyx file) if it is relative
1054 void fix_relative_filename(string & name)
1056 if (FileName::isAbsolute(name))
1059 name = to_utf8(makeRelPath(from_utf8(makeAbsPath(name, getMasterFilePath()).absFileName()),
1060 from_utf8(getParentFilePath())));
1064 /// Parse a NoWeb Scrap section. The initial "<<" is already parsed.
1065 void parse_noweb(Parser & p, ostream & os, Context & context)
1067 // assemble the rest of the keyword
1071 Token const & t = p.get_token();
1072 if (t.asInput() == ">" && p.next_token().asInput() == ">") {
1075 scrap = (p.good() && p.next_token().asInput() == "=");
1077 name += p.get_token().asInput();
1080 name += t.asInput();
1083 if (!scrap || !context.new_layout_allowed ||
1084 !context.textclass.hasLayout(from_ascii("Scrap"))) {
1085 cerr << "Warning: Could not interpret '" << name
1086 << "'. Ignoring it." << endl;
1090 // We use new_paragraph instead of check_end_layout because the stuff
1091 // following the noweb chunk needs to start with a \begin_layout.
1092 // This may create a new paragraph even if there was none in the
1093 // noweb file, but the alternative is an invalid LyX file. Since
1094 // noweb code chunks are implemented with a layout style in LyX they
1095 // always must be in an own paragraph.
1096 context.new_paragraph(os);
1097 Context newcontext(true, context.textclass,
1098 &context.textclass[from_ascii("Scrap")]);
1099 newcontext.check_layout(os);
1102 Token const & t = p.get_token();
1103 // We abuse the parser a bit, because this is no TeX syntax
1105 if (t.cat() == catEscape)
1106 os << subst(t.asInput(), "\\", "\n\\backslash\n");
1108 os << subst(t.asInput(), "\n", "\n\\newline\n");
1109 // The scrap chunk is ended by an @ at the beginning of a line.
1110 // After the @ the line may contain a comment and/or
1111 // whitespace, but nothing else.
1112 if (t.asInput() == "@" && p.prev_token().cat() == catNewline &&
1113 (p.next_token().cat() == catSpace ||
1114 p.next_token().cat() == catNewline ||
1115 p.next_token().cat() == catComment)) {
1116 while (p.good() && p.next_token().cat() == catSpace)
1117 os << p.get_token().asInput();
1118 if (p.next_token().cat() == catComment)
1119 // The comment includes a final '\n'
1120 os << p.get_token().asInput();
1122 if (p.next_token().cat() == catNewline)
1129 newcontext.check_end_layout(os);
1132 } // anonymous namespace
1135 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
1138 Layout const * newlayout = 0;
1139 // Store the latest bibliographystyle (needed for bibtex inset)
1140 string bibliographystyle;
1141 bool const use_natbib = used_packages.find("natbib") != used_packages.end();
1142 bool const use_jurabib = used_packages.find("jurabib") != used_packages.end();
1144 Token const & t = p.get_token();
1147 cerr << "t: " << t << " flags: " << flags << "\n";
1150 if (flags & FLAG_ITEM) {
1151 if (t.cat() == catSpace)
1154 flags &= ~FLAG_ITEM;
1155 if (t.cat() == catBegin) {
1156 // skip the brace and collect everything to the next matching
1158 flags |= FLAG_BRACE_LAST;
1162 // handle only this single token, leave the loop if done
1163 flags |= FLAG_LEAVE;
1166 if (t.character() == ']' && (flags & FLAG_BRACK_LAST))
1168 if (t.character() == '}' && (flags & FLAG_BRACE_LAST))
1174 if (t.cat() == catMath) {
1175 // we are inside some text mode thingy, so opening new math is allowed
1176 context.check_layout(os);
1177 begin_inset(os, "Formula ");
1178 Token const & n = p.get_token();
1179 if (n.cat() == catMath && outer) {
1180 // TeX's $$...$$ syntax for displayed math
1182 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
1184 p.get_token(); // skip the second '$' token
1186 // simple $...$ stuff
1189 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
1195 else if (t.cat() == catSuper || t.cat() == catSub)
1196 cerr << "catcode " << t << " illegal in text mode\n";
1198 // Basic support for english quotes. This should be
1199 // extended to other quotes, but is not so easy (a
1200 // left english quote is the same as a right german
1202 else if (t.asInput() == "`" && p.next_token().asInput() == "`") {
1203 context.check_layout(os);
1204 begin_inset(os, "Quotes ");
1210 else if (t.asInput() == "'" && p.next_token().asInput() == "'") {
1211 context.check_layout(os);
1212 begin_inset(os, "Quotes ");
1219 else if (t.asInput() == ">" && p.next_token().asInput() == ">") {
1220 context.check_layout(os);
1221 begin_inset(os, "Quotes ");
1228 else if (t.asInput() == "<" && p.next_token().asInput() == "<") {
1229 context.check_layout(os);
1230 begin_inset(os, "Quotes ");
1237 else if (t.asInput() == "<"
1238 && p.next_token().asInput() == "<" && noweb_mode) {
1240 parse_noweb(p, os, context);
1243 else if (t.cat() == catSpace || (t.cat() == catNewline && ! p.isParagraph()))
1244 check_space(p, os, context);
1246 else if (t.character() == '[' && noweb_mode &&
1247 p.next_token().character() == '[') {
1248 // These can contain underscores
1250 string const s = p.getFullOpt() + ']';
1251 if (p.next_token().character() == ']')
1254 cerr << "Warning: Inserting missing ']' in '"
1255 << s << "'." << endl;
1256 handle_ert(os, s, context);
1259 else if (t.cat() == catLetter ||
1260 t.cat() == catOther ||
1261 t.cat() == catAlign ||
1262 t.cat() == catParameter) {
1263 // This translates "&" to "\\&" which may be wrong...
1264 context.check_layout(os);
1268 else if (p.isParagraph()) {
1269 if (context.new_layout_allowed)
1270 context.new_paragraph(os);
1272 handle_ert(os, "\\par ", context);
1273 eat_whitespace(p, os, context, true);
1276 else if (t.cat() == catActive) {
1277 context.check_layout(os);
1278 if (t.character() == '~') {
1279 if (context.layout->free_spacing)
1282 os << "\\InsetSpace ~\n";
1287 else if (t.cat() == catBegin &&
1288 p.next_token().cat() == catEnd) {
1290 Token const prev = p.prev_token();
1292 if (p.next_token().character() == '`' ||
1293 (prev.character() == '-' &&
1294 p.next_token().character() == '-'))
1295 ; // ignore it in {}`` or -{}-
1297 handle_ert(os, "{}", context);
1301 else if (t.cat() == catBegin) {
1302 context.check_layout(os);
1303 // special handling of font attribute changes
1304 Token const prev = p.prev_token();
1305 Token const next = p.next_token();
1306 TeXFont const oldFont = context.font;
1307 if (next.character() == '[' ||
1308 next.character() == ']' ||
1309 next.character() == '*') {
1311 if (p.next_token().cat() == catEnd) {
1316 handle_ert(os, "{", context);
1317 parse_text_snippet(p, os,
1320 handle_ert(os, "}", context);
1322 } else if (! context.new_layout_allowed) {
1323 handle_ert(os, "{", context);
1324 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1326 handle_ert(os, "}", context);
1327 } else if (is_known(next.cs(), known_sizes)) {
1328 // next will change the size, so we must
1330 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1332 if (!context.atParagraphStart())
1334 << context.font.size << "\n";
1335 } else if (is_known(next.cs(), known_font_families)) {
1336 // next will change the font family, so we
1337 // must reset it here
1338 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1340 if (!context.atParagraphStart())
1342 << context.font.family << "\n";
1343 } else if (is_known(next.cs(), known_font_series)) {
1344 // next will change the font series, so we
1345 // must reset it here
1346 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1348 if (!context.atParagraphStart())
1350 << context.font.series << "\n";
1351 } else if (is_known(next.cs(), known_font_shapes)) {
1352 // next will change the font shape, so we
1353 // must reset it here
1354 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1356 if (!context.atParagraphStart())
1358 << context.font.shape << "\n";
1359 } else if (is_known(next.cs(), known_old_font_families) ||
1360 is_known(next.cs(), known_old_font_series) ||
1361 is_known(next.cs(), known_old_font_shapes)) {
1362 // next will change the font family, series
1363 // and shape, so we must reset it here
1364 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1366 if (!context.atParagraphStart())
1368 << context.font.family
1370 << context.font.series
1372 << context.font.shape << "\n";
1374 handle_ert(os, "{", context);
1375 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1377 handle_ert(os, "}", context);
1381 else if (t.cat() == catEnd) {
1382 if (flags & FLAG_BRACE_LAST) {
1385 cerr << "stray '}' in text\n";
1386 handle_ert(os, "}", context);
1389 else if (t.cat() == catComment)
1390 parse_comment(p, os, t, context);
1393 // control sequences
1396 else if (t.cs() == "(") {
1397 context.check_layout(os);
1398 begin_inset(os, "Formula");
1400 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
1405 else if (t.cs() == "[") {
1406 context.check_layout(os);
1407 begin_inset(os, "Formula");
1409 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
1414 else if (t.cs() == "begin")
1415 parse_environment(p, os, outer, context);
1417 else if (t.cs() == "end") {
1418 if (flags & FLAG_END) {
1419 // eat environment name
1420 string const name = p.getArg('{', '}');
1421 if (name != active_environment())
1422 cerr << "\\end{" + name + "} does not match \\begin{"
1423 + active_environment() + "}\n";
1426 p.error("found 'end' unexpectedly");
1429 else if (t.cs() == "item") {
1432 bool optarg = false;
1433 if (p.next_token().character() == '[') {
1434 p.get_token(); // eat '['
1435 s = parse_text_snippet(p, FLAG_BRACK_LAST,
1440 context.check_layout(os);
1441 if (context.has_item) {
1442 // An item in an unknown list-like environment
1443 // FIXME: Do this in check_layout()!
1444 context.has_item = false;
1446 handle_ert(os, "\\item", context);
1448 handle_ert(os, "\\item ", context);
1451 if (context.layout->labeltype != LABEL_MANUAL) {
1452 // LyX does not support \item[\mybullet]
1453 // in itemize environments
1454 handle_ert(os, "[", context);
1456 handle_ert(os, "]", context);
1457 } else if (!s.empty()) {
1458 // The space is needed to separate the
1459 // item from the rest of the sentence.
1461 eat_whitespace(p, os, context, false);
1466 else if (t.cs() == "bibitem") {
1468 context.check_layout(os);
1469 begin_inset(os, "LatexCommand ");
1470 os << t.cs() << "\n";
1471 os << "label \"" << p.getOptContent() << "\"\n";
1472 os << "key \"" << p.verbatim_item() << "\"\n";
1476 else if (t.cs() == "def") {
1477 context.check_layout(os);
1478 eat_whitespace(p, os, context, false);
1479 string name = p.get_token().cs();
1480 eat_whitespace(p, os, context, false);
1486 while (p.next_token().cat() != catBegin) {
1487 if (p.next_token().cat() == catParameter) {
1492 // followed by number?
1493 if (p.next_token().cat() == catOther) {
1494 char c = p.getChar();
1496 // number = current arity + 1?
1497 if (c == arity + '0' + 1)
1502 paramtext += p.get_token().cs();
1504 paramtext += p.get_token().cs();
1509 // only output simple (i.e. compatible) macro as FormulaMacros
1510 string ert = "\\def\\" + name + ' ' + paramtext + '{' + p.verbatim_item() + '}';
1512 context.check_layout(os);
1513 begin_inset(os, "FormulaMacro");
1517 handle_ert(os, ert, context);
1520 else if (t.cs() == "noindent") {
1522 context.add_par_extra_stuff("\\noindent\n");
1525 else if (t.cs() == "appendix") {
1526 context.add_par_extra_stuff("\\start_of_appendix\n");
1527 // We need to start a new paragraph. Otherwise the
1528 // appendix in 'bla\appendix\chapter{' would start
1530 context.new_paragraph(os);
1531 // We need to make sure that the paragraph is
1532 // generated even if it is empty. Otherwise the
1533 // appendix in '\par\appendix\par\chapter{' would
1535 context.check_layout(os);
1536 // FIXME: This is a hack to prevent paragraph
1537 // deletion if it is empty. Handle this better!
1539 "%dummy comment inserted by tex2lyx to "
1540 "ensure that this paragraph is not empty",
1542 // Both measures above may generate an additional
1543 // empty paragraph, but that does not hurt, because
1544 // whitespace does not matter here.
1545 eat_whitespace(p, os, context, true);
1548 // Starred section headings
1549 // Must attempt to parse "Section*" before "Section".
1550 else if ((p.next_token().asInput() == "*") &&
1551 context.new_layout_allowed &&
1552 (newlayout = findLayout(context.textclass, t.cs() + '*')) &&
1553 newlayout->isCommand()) {
1554 TeXFont const oldFont = context.font;
1555 // save the current font size
1556 string const size = oldFont.size;
1557 // reset the font size to default, because the
1558 // font size switches don't affect section
1559 // headings and the like
1560 context.font.size = known_coded_sizes[0];
1561 output_font_change(os, oldFont, context.font);
1564 output_command_layout(os, p, outer, context, newlayout);
1565 // set the font size to the original value
1566 context.font.size = size;
1567 output_font_change(os, oldFont, context.font);
1571 // Section headings and the like
1572 else if (context.new_layout_allowed &&
1573 (newlayout = findLayout(context.textclass, t.cs())) &&
1574 newlayout->isCommand()) {
1575 TeXFont const oldFont = context.font;
1576 // save the current font size
1577 string const size = oldFont.size;
1578 // reset the font size to default, because the font size switches don't
1579 // affect section headings and the like
1580 context.font.size = known_coded_sizes[0];
1581 output_font_change(os, oldFont, context.font);
1583 output_command_layout(os, p, outer, context, newlayout);
1584 // set the font size to the original value
1585 context.font.size = size;
1586 output_font_change(os, oldFont, context.font);
1590 else if (t.cs() == "caption") {
1591 // FIXME: this should get some cleanup. All
1592 // the \begin_layout:s are output by the
1595 context.check_layout(os);
1597 begin_inset(os, "Caption\n\n");
1598 os << "\\begin_layout "
1599 << to_utf8(context.textclass.defaultLayout().name())
1601 if (p.next_token().character() == '[') {
1602 p.get_token(); // eat '['
1603 begin_inset(os, "OptArg\n");
1604 os << "status collapsed\n";
1605 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
1607 eat_whitespace(p, os, context, false);
1609 parse_text(p, os, FLAG_ITEM, outer, context);
1610 context.check_end_layout(os);
1611 // We don't need really a new paragraph, but
1612 // we must make sure that the next item gets a \begin_layout.
1613 context.new_paragraph(os);
1616 os << "\\end_layout\n";
1619 else if (t.cs() == "includegraphics") {
1620 bool const clip = p.next_token().asInput() == "*";
1623 map<string, string> opts = split_map(p.getArg('[', ']'));
1625 opts["clip"] = string();
1626 string name = normalize_filename(p.verbatim_item());
1628 string const path = getMasterFilePath();
1629 // We want to preserve relative / absolute filenames,
1630 // therefore path is only used for testing
1631 if (!makeAbsPath(name, path).exists()) {
1632 // The file extension is probably missing.
1633 // Now try to find it out.
1634 string const dvips_name =
1635 find_file(name, path,
1636 known_dvips_graphics_formats);
1637 string const pdftex_name =
1638 find_file(name, path,
1639 known_pdftex_graphics_formats);
1640 if (!dvips_name.empty()) {
1641 if (!pdftex_name.empty()) {
1642 cerr << "This file contains the "
1644 "\"\\includegraphics{"
1646 "However, files\n\""
1647 << dvips_name << "\" and\n\""
1648 << pdftex_name << "\"\n"
1649 "both exist, so I had to make a "
1650 "choice and took the first one.\n"
1651 "Please move the unwanted one "
1652 "someplace else and try again\n"
1653 "if my choice was wrong."
1657 } else if (!pdftex_name.empty())
1661 if (makeAbsPath(name, path).exists())
1662 fix_relative_filename(name);
1664 cerr << "Warning: Could not find graphics file '"
1665 << name << "'." << endl;
1667 context.check_layout(os);
1668 begin_inset(os, "Graphics ");
1669 os << "\n\tfilename " << name << '\n';
1670 if (opts.find("width") != opts.end())
1672 << translate_len(opts["width"]) << '\n';
1673 if (opts.find("height") != opts.end())
1675 << translate_len(opts["height"]) << '\n';
1676 if (opts.find("scale") != opts.end()) {
1677 istringstream iss(opts["scale"]);
1681 os << "\tscale " << val << '\n';
1683 if (opts.find("angle") != opts.end())
1684 os << "\trotateAngle "
1685 << opts["angle"] << '\n';
1686 if (opts.find("origin") != opts.end()) {
1688 string const opt = opts["origin"];
1689 if (opt.find('l') != string::npos) ss << "left";
1690 if (opt.find('r') != string::npos) ss << "right";
1691 if (opt.find('c') != string::npos) ss << "center";
1692 if (opt.find('t') != string::npos) ss << "Top";
1693 if (opt.find('b') != string::npos) ss << "Bottom";
1694 if (opt.find('B') != string::npos) ss << "Baseline";
1695 if (!ss.str().empty())
1696 os << "\trotateOrigin " << ss.str() << '\n';
1698 cerr << "Warning: Ignoring unknown includegraphics origin argument '" << opt << "'\n";
1700 if (opts.find("keepaspectratio") != opts.end())
1701 os << "\tkeepAspectRatio\n";
1702 if (opts.find("clip") != opts.end())
1704 if (opts.find("draft") != opts.end())
1706 if (opts.find("bb") != opts.end())
1707 os << "\tBoundingBox "
1708 << opts["bb"] << '\n';
1709 int numberOfbbOptions = 0;
1710 if (opts.find("bbllx") != opts.end())
1711 numberOfbbOptions++;
1712 if (opts.find("bblly") != opts.end())
1713 numberOfbbOptions++;
1714 if (opts.find("bburx") != opts.end())
1715 numberOfbbOptions++;
1716 if (opts.find("bbury") != opts.end())
1717 numberOfbbOptions++;
1718 if (numberOfbbOptions == 4)
1719 os << "\tBoundingBox "
1720 << opts["bbllx"] << " " << opts["bblly"] << " "
1721 << opts["bburx"] << " " << opts["bbury"] << '\n';
1722 else if (numberOfbbOptions > 0)
1723 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
1724 numberOfbbOptions = 0;
1725 if (opts.find("natwidth") != opts.end())
1726 numberOfbbOptions++;
1727 if (opts.find("natheight") != opts.end())
1728 numberOfbbOptions++;
1729 if (numberOfbbOptions == 2)
1730 os << "\tBoundingBox 0bp 0bp "
1731 << opts["natwidth"] << " " << opts["natheight"] << '\n';
1732 else if (numberOfbbOptions > 0)
1733 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
1734 ostringstream special;
1735 if (opts.find("hiresbb") != opts.end())
1736 special << "hiresbb,";
1737 if (opts.find("trim") != opts.end())
1739 if (opts.find("viewport") != opts.end())
1740 special << "viewport=" << opts["viewport"] << ',';
1741 if (opts.find("totalheight") != opts.end())
1742 special << "totalheight=" << opts["totalheight"] << ',';
1743 if (opts.find("type") != opts.end())
1744 special << "type=" << opts["type"] << ',';
1745 if (opts.find("ext") != opts.end())
1746 special << "ext=" << opts["ext"] << ',';
1747 if (opts.find("read") != opts.end())
1748 special << "read=" << opts["read"] << ',';
1749 if (opts.find("command") != opts.end())
1750 special << "command=" << opts["command"] << ',';
1751 string s_special = special.str();
1752 if (!s_special.empty()) {
1753 // We had special arguments. Remove the trailing ','.
1754 os << "\tspecial " << s_special.substr(0, s_special.size() - 1) << '\n';
1756 // TODO: Handle the unknown settings better.
1757 // Warn about invalid options.
1758 // Check whether some option was given twice.
1762 else if (t.cs() == "footnote" ||
1763 (t.cs() == "thanks" && context.layout->intitle)) {
1765 context.check_layout(os);
1766 begin_inset(os, "Foot\n");
1767 os << "status collapsed\n\n";
1768 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
1772 else if (t.cs() == "marginpar") {
1774 context.check_layout(os);
1775 begin_inset(os, "Marginal\n");
1776 os << "status collapsed\n\n";
1777 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
1781 else if (t.cs() == "ensuremath") {
1783 context.check_layout(os);
1784 string const s = p.verbatim_item();
1785 //FIXME: this never triggers in UTF8
1786 if (s == "\xb1" || s == "\xb3" || s == "\xb2" || s == "\xb5")
1789 handle_ert(os, "\\ensuremath{" + s + "}",
1793 else if (t.cs() == "hfill") {
1794 context.check_layout(os);
1795 os << "\n\\hfill\n";
1800 else if (t.cs() == "makeindex" || t.cs() == "maketitle") {
1801 // FIXME: Somehow prevent title layouts if
1802 // "maketitle" was not found
1804 skip_braces(p); // swallow this
1807 else if (t.cs() == "tableofcontents") {
1809 context.check_layout(os);
1810 begin_inset(os, "LatexCommand ");
1811 os << t.cs() << "\n";
1813 skip_braces(p); // swallow this
1816 else if (t.cs() == "listoffigures") {
1818 context.check_layout(os);
1819 begin_inset(os, "FloatList figure\n");
1821 skip_braces(p); // swallow this
1824 else if (t.cs() == "listoftables") {
1826 context.check_layout(os);
1827 begin_inset(os, "FloatList table\n");
1829 skip_braces(p); // swallow this
1832 else if (t.cs() == "listof") {
1833 p.skip_spaces(true);
1834 string const name = p.get_token().cs();
1835 if (context.textclass.floats().typeExist(name)) {
1836 context.check_layout(os);
1837 begin_inset(os, "FloatList ");
1840 p.get_token(); // swallow second arg
1842 handle_ert(os, "\\listof{" + name + "}", context);
1845 else if (t.cs() == "textrm")
1846 parse_text_attributes(p, os, FLAG_ITEM, outer,
1847 context, "\\family",
1848 context.font.family, "roman");
1850 else if (t.cs() == "textsf")
1851 parse_text_attributes(p, os, FLAG_ITEM, outer,
1852 context, "\\family",
1853 context.font.family, "sans");
1855 else if (t.cs() == "texttt")
1856 parse_text_attributes(p, os, FLAG_ITEM, outer,
1857 context, "\\family",
1858 context.font.family, "typewriter");
1860 else if (t.cs() == "textmd")
1861 parse_text_attributes(p, os, FLAG_ITEM, outer,
1862 context, "\\series",
1863 context.font.series, "medium");
1865 else if (t.cs() == "textbf")
1866 parse_text_attributes(p, os, FLAG_ITEM, outer,
1867 context, "\\series",
1868 context.font.series, "bold");
1870 else if (t.cs() == "textup")
1871 parse_text_attributes(p, os, FLAG_ITEM, outer,
1873 context.font.shape, "up");
1875 else if (t.cs() == "textit")
1876 parse_text_attributes(p, os, FLAG_ITEM, outer,
1878 context.font.shape, "italic");
1880 else if (t.cs() == "textsl")
1881 parse_text_attributes(p, os, FLAG_ITEM, outer,
1883 context.font.shape, "slanted");
1885 else if (t.cs() == "textsc")
1886 parse_text_attributes(p, os, FLAG_ITEM, outer,
1888 context.font.shape, "smallcaps");
1890 else if (t.cs() == "textnormal" || t.cs() == "normalfont") {
1891 context.check_layout(os);
1892 TeXFont oldFont = context.font;
1893 context.font.init();
1894 context.font.size = oldFont.size;
1895 os << "\n\\family " << context.font.family << "\n";
1896 os << "\n\\series " << context.font.series << "\n";
1897 os << "\n\\shape " << context.font.shape << "\n";
1898 if (t.cs() == "textnormal") {
1899 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1900 output_font_change(os, context.font, oldFont);
1901 context.font = oldFont;
1903 eat_whitespace(p, os, context, false);
1906 else if (t.cs() == "textcolor") {
1907 // scheme is \textcolor{color name}{text}
1908 string const color = p.verbatim_item();
1909 // we only support the predefined colors of the color package
1910 if (color == "black" || color == "blue" || color == "cyan"
1911 || color == "green" || color == "magenta" || color == "red"
1912 || color == "white" || color == "yellow") {
1913 context.check_layout(os);
1914 os << "\n\\color " << color << "\n";
1915 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1916 context.check_layout(os);
1917 os << "\n\\color inherit\n";
1919 // for custom defined colors
1920 handle_ert(os, t.asInput() + "{" + color + "}", context);
1923 else if (t.cs() == "underbar") {
1924 // Do NOT handle \underline.
1925 // \underbar cuts through y, g, q, p etc.,
1926 // \underline does not.
1927 context.check_layout(os);
1928 os << "\n\\bar under\n";
1929 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1930 context.check_layout(os);
1931 os << "\n\\bar default\n";
1934 else if (t.cs() == "emph" || t.cs() == "noun") {
1935 context.check_layout(os);
1936 os << "\n\\" << t.cs() << " on\n";
1937 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1938 context.check_layout(os);
1939 os << "\n\\" << t.cs() << " default\n";
1942 else if (t.cs() == "lyxline") {
1943 context.check_layout(os);
1947 else if (is_known(t.cs(), known_ref_commands)) {
1948 context.check_layout(os);
1949 begin_inset(os, "LatexCommand ");
1950 os << t.cs() << "\n";
1951 // LyX cannot handle newlines in a latex command
1952 // FIXME: Move the substitution into parser::getOpt()?
1953 os << subst(p.getOpt(), "\n", " ");
1954 os << "reference " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
1958 else if (use_natbib &&
1959 is_known(t.cs(), known_natbib_commands) &&
1960 ((t.cs() != "citefullauthor" &&
1961 t.cs() != "citeyear" &&
1962 t.cs() != "citeyearpar") ||
1963 p.next_token().asInput() != "*")) {
1964 context.check_layout(os);
1965 string command = t.cs();
1966 if (p.next_token().asInput() == "*") {
1970 if (command == "citefullauthor")
1971 // alternative name for "\\citeauthor*"
1972 command = "citeauthor*";
1974 // text before the citation
1976 // text after the citation
1978 get_cite_arguments(p, true, before, after);
1980 if (command == "cite") {
1981 // \cite without optional argument means
1982 // \citet, \cite with at least one optional
1983 // argument means \citep.
1984 if (before.empty() && after.empty())
1989 if (before.empty() && after == "[]")
1990 // avoid \citet[]{a}
1992 else if (before == "[]" && after == "[]") {
1993 // avoid \citet[][]{a}
1997 // remove the brackets around after and before
1998 if (!after.empty()) {
2000 after.erase(after.length() - 1, 1);
2001 // LyX cannot handle newlines in the parameter
2002 after = subst(after, "\n", " ");
2004 if (!before.empty()) {
2006 before.erase(before.length() - 1, 1);
2007 // LyX cannot handle newlines in the parameter
2008 before = subst(before, "\n", " ");
2010 begin_inset(os, "LatexCommand ");
2011 os << t.cs() << "\n";
2012 os << "after " << '"' << after << '"' << "\n";
2013 os << "before " << '"' << before << '"' << "\n";
2014 os << "key " << '"' << p.verbatim_item() << '"' << "\n";
2018 else if (use_jurabib &&
2019 is_known(t.cs(), known_jurabib_commands)) {
2020 context.check_layout(os);
2021 string const command = t.cs();
2022 char argumentOrder = '\0';
2023 vector<string> const & options = used_packages["jurabib"];
2024 if (find(options.begin(), options.end(),
2025 "natbiborder") != options.end())
2026 argumentOrder = 'n';
2027 else if (find(options.begin(), options.end(),
2028 "jurabiborder") != options.end())
2029 argumentOrder = 'j';
2031 // text before the citation
2033 // text after the citation
2035 get_cite_arguments(p, argumentOrder != 'j', before, after);
2037 string const citation = p.verbatim_item();
2038 if (!before.empty() && argumentOrder == '\0') {
2039 cerr << "Warning: Assuming argument order "
2040 "of jurabib version 0.6 for\n'"
2041 << command << before << after << '{'
2042 << citation << "}'.\n"
2043 "Add 'jurabiborder' to the jurabib "
2044 "package options if you used an\n"
2045 "earlier jurabib version." << endl;
2047 if (!after.empty()) {
2049 after.erase(after.length() - 1, 1);
2051 if (!before.empty()) {
2053 before.erase(before.length() - 1, 1);
2055 begin_inset(os, "LatexCommand ");
2056 os << t.cs() << "\n";
2057 os << "after " << '"' << after << '"' << "\n";
2058 os << "before " << '"' << before << '"' << "\n";
2059 os << "key " << '"' << citation << '"' << "\n";
2063 else if (t.cs() == "cite") {
2064 context.check_layout(os);
2065 // LyX cannot handle newlines in a latex command
2066 string after = subst(p.getOptContent(), "\n", " ");
2067 begin_inset(os, "LatexCommand ");
2068 os << t.cs() << "\n";
2069 os << "after " << '"' << after << '"' << "\n";
2070 os << "key " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
2074 else if (t.cs() == "index") {
2075 context.check_layout(os);
2076 begin_inset(os, "LatexCommand ");
2077 os << t.cs() << "\n";
2078 // LyX cannot handle newlines in a latex command
2079 os << "name " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
2083 else if (t.cs() == "nomenclature") {
2084 context.check_layout(os);
2085 begin_inset(os, "LatexCommand ");
2086 os << t.cs() << "\n";
2087 // LyX cannot handle newlines in a latex command
2088 string prefix = subst(p.getOptContent(), "\n", " ");
2089 if (!prefix.empty())
2090 os << "prefix " << '"' << prefix << '"' << "\n";
2091 os << "symbol " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
2092 os << "description " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
2096 else if (t.cs() == "label") {
2097 context.check_layout(os);
2098 begin_inset(os, "LatexCommand ");
2099 os << t.cs() << "\n";
2100 // LyX cannot handle newlines in a latex command
2101 os << "name " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
2105 else if (t.cs() == "printindex") {
2106 context.check_layout(os);
2107 begin_inset(os, "LatexCommand ");
2108 os << t.cs() << "\n";
2113 else if (t.cs() == "printnomenclature") {
2114 context.check_layout(os);
2115 begin_inset(os, "LatexCommand ");
2116 os << t.cs() << "\n";
2121 else if (t.cs() == "url") {
2122 context.check_layout(os);
2123 begin_inset(os, "LatexCommand ");
2124 os << t.cs() << "\n";
2125 // LyX cannot handle newlines in a latex command
2126 os << "target " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
2130 else if (is_known(t.cs(), known_quotes)) {
2131 char const * const * where = is_known(t.cs(), known_quotes);
2132 context.check_layout(os);
2133 begin_inset(os, "Quotes ");
2134 os << known_coded_quotes[where - known_quotes];
2136 // LyX adds {} after the quote, so we have to eat
2137 // spaces here if there are any before a possible
2139 eat_whitespace(p, os, context, false);
2143 else if (is_known(t.cs(), known_sizes) &&
2144 context.new_layout_allowed) {
2145 char const * const * where = is_known(t.cs(), known_sizes);
2146 context.check_layout(os);
2147 TeXFont const oldFont = context.font;
2148 // the font size index differs by 1, because the known_coded_sizes
2149 // has additionally a "default" entry
2150 context.font.size = known_coded_sizes[where - known_sizes + 1];
2151 output_font_change(os, oldFont, context.font);
2152 eat_whitespace(p, os, context, false);
2155 else if (is_known(t.cs(), known_font_families) &&
2156 context.new_layout_allowed) {
2157 char const * const * where =
2158 is_known(t.cs(), known_font_families);
2159 context.check_layout(os);
2160 TeXFont const oldFont = context.font;
2161 context.font.family =
2162 known_coded_font_families[where - known_font_families];
2163 output_font_change(os, oldFont, context.font);
2164 eat_whitespace(p, os, context, false);
2167 else if (is_known(t.cs(), known_font_series) &&
2168 context.new_layout_allowed) {
2169 char const * const * where =
2170 is_known(t.cs(), known_font_series);
2171 context.check_layout(os);
2172 TeXFont const oldFont = context.font;
2173 context.font.series =
2174 known_coded_font_series[where - known_font_series];
2175 output_font_change(os, oldFont, context.font);
2176 eat_whitespace(p, os, context, false);
2179 else if (is_known(t.cs(), known_font_shapes) &&
2180 context.new_layout_allowed) {
2181 char const * const * where =
2182 is_known(t.cs(), known_font_shapes);
2183 context.check_layout(os);
2184 TeXFont const oldFont = context.font;
2185 context.font.shape =
2186 known_coded_font_shapes[where - known_font_shapes];
2187 output_font_change(os, oldFont, context.font);
2188 eat_whitespace(p, os, context, false);
2190 else if (is_known(t.cs(), known_old_font_families) &&
2191 context.new_layout_allowed) {
2192 char const * const * where =
2193 is_known(t.cs(), known_old_font_families);
2194 context.check_layout(os);
2195 TeXFont const oldFont = context.font;
2196 context.font.init();
2197 context.font.size = oldFont.size;
2198 context.font.family =
2199 known_coded_font_families[where - known_old_font_families];
2200 output_font_change(os, oldFont, context.font);
2201 eat_whitespace(p, os, context, false);
2204 else if (is_known(t.cs(), known_old_font_series) &&
2205 context.new_layout_allowed) {
2206 char const * const * where =
2207 is_known(t.cs(), known_old_font_series);
2208 context.check_layout(os);
2209 TeXFont const oldFont = context.font;
2210 context.font.init();
2211 context.font.size = oldFont.size;
2212 context.font.series =
2213 known_coded_font_series[where - known_old_font_series];
2214 output_font_change(os, oldFont, context.font);
2215 eat_whitespace(p, os, context, false);
2218 else if (is_known(t.cs(), known_old_font_shapes) &&
2219 context.new_layout_allowed) {
2220 char const * const * where =
2221 is_known(t.cs(), known_old_font_shapes);
2222 context.check_layout(os);
2223 TeXFont const oldFont = context.font;
2224 context.font.init();
2225 context.font.size = oldFont.size;
2226 context.font.shape =
2227 known_coded_font_shapes[where - known_old_font_shapes];
2228 output_font_change(os, oldFont, context.font);
2229 eat_whitespace(p, os, context, false);
2232 else if (t.cs() == "selectlanguage") {
2233 context.check_layout(os);
2234 // save the language for the case that a
2235 // \foreignlanguage is used
2237 context.font.language = subst(p.verbatim_item(), "\n", " ");
2238 os << "\\lang " << context.font.language << "\n";
2241 else if (t.cs() == "foreignlanguage") {
2242 context.check_layout(os);
2243 os << "\n\\lang " << subst(p.verbatim_item(), "\n", " ") << "\n";
2244 os << subst(p.verbatim_item(), "\n", " ");
2245 // FIXME: the second argument of selectlanguage
2246 // has to be parsed (like for \textsf, for
2248 // set back to last selectlanguage
2249 os << "\n\\lang " << context.font.language << "\n";
2252 else if (t.cs() == "inputencoding") {
2253 // nothing to write here
2254 string const enc = subst(p.verbatim_item(), "\n", " ");
2258 else if (t.cs() == "LyX" || t.cs() == "TeX"
2259 || t.cs() == "LaTeX") {
2260 context.check_layout(os);
2262 skip_braces(p); // eat {}
2265 else if (t.cs() == "LaTeXe") {
2266 context.check_layout(os);
2268 skip_braces(p); // eat {}
2271 else if (t.cs() == "ldots") {
2272 context.check_layout(os);
2274 os << "\\SpecialChar \\ldots{}\n";
2277 else if (t.cs() == "lyxarrow") {
2278 context.check_layout(os);
2279 os << "\\SpecialChar \\menuseparator\n";
2283 else if (t.cs() == "textcompwordmark") {
2284 context.check_layout(os);
2285 os << "\\SpecialChar \\textcompwordmark{}\n";
2289 else if (t.cs() == "textquotedbl") {
2290 context.check_layout(os);
2295 else if (t.cs() == "@" && p.next_token().asInput() == ".") {
2296 context.check_layout(os);
2297 os << "\\SpecialChar \\@.\n";
2301 else if (t.cs() == "-") {
2302 context.check_layout(os);
2303 os << "\\SpecialChar \\-\n";
2306 else if (t.cs() == "textasciitilde") {
2307 context.check_layout(os);
2312 else if (t.cs() == "textasciicircum") {
2313 context.check_layout(os);
2318 else if (t.cs() == "textbackslash") {
2319 context.check_layout(os);
2320 os << "\n\\backslash\n";
2324 else if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#"
2325 || t.cs() == "$" || t.cs() == "{" || t.cs() == "}"
2327 context.check_layout(os);
2331 else if (t.cs() == "char") {
2332 context.check_layout(os);
2333 if (p.next_token().character() == '`') {
2335 if (p.next_token().cs() == "\"") {
2340 handle_ert(os, "\\char`", context);
2343 handle_ert(os, "\\char", context);
2347 else if (t.cs() == "verb") {
2348 context.check_layout(os);
2349 char const delimiter = p.next_token().character();
2350 string const arg = p.getArg(delimiter, delimiter);
2352 oss << "\\verb" << delimiter << arg << delimiter;
2353 handle_ert(os, oss.str(), context);
2356 // Problem: \= creates a tabstop inside the tabbing environment
2357 // and else an accent. In the latter case we really would want
2358 // \={o} instead of \= o.
2359 else if (t.cs() == "=" && (flags & FLAG_TABBING))
2360 handle_ert(os, t.asInput(), context);
2362 // accents (see Table 6 in Comprehensive LaTeX Symbol List)
2363 else if (t.cs().size() == 1
2364 && contains("\"'.=^`bcdHkrtuv~", t.cs())) {
2365 context.check_layout(os);
2366 // try to see whether the string is in unicodesymbols
2368 string command = t.asInput() + "{"
2369 + trim(p.verbatim_item())
2371 docstring s = encodings.fromLaTeXCommand(from_utf8(command), rem);
2374 cerr << "When parsing " << command
2375 << ", result is " << to_utf8(s)
2376 << "+" << to_utf8(rem) << endl;
2379 // we did not find a non-ert version
2380 handle_ert(os, command, context);
2383 else if (t.cs() == "\\") {
2384 context.check_layout(os);
2385 string const next = p.next_token().asInput();
2387 handle_ert(os, "\\\\" + p.getOpt(), context);
2388 else if (next == "*") {
2390 handle_ert(os, "\\\\*" + p.getOpt(), context);
2393 os << "\n\\newline\n";
2397 else if (t.cs() == "newline") {
2398 context.check_layout(os);
2399 os << "\n\\" << t.cs() << "\n";
2400 skip_braces(p); // eat {}
2403 else if (t.cs() == "input" || t.cs() == "include"
2404 || t.cs() == "verbatiminput") {
2405 string name = '\\' + t.cs();
2406 if (t.cs() == "verbatiminput"
2407 && p.next_token().asInput() == "*")
2408 name += p.get_token().asInput();
2409 context.check_layout(os);
2410 begin_inset(os, "Include ");
2411 string filename(normalize_filename(p.getArg('{', '}')));
2412 string const path = getMasterFilePath();
2413 // We want to preserve relative / absolute filenames,
2414 // therefore path is only used for testing
2415 if ((t.cs() == "include" || t.cs() == "input") &&
2416 !makeAbsPath(filename, path).exists()) {
2417 // The file extension is probably missing.
2418 // Now try to find it out.
2419 string const tex_name =
2420 find_file(filename, path,
2421 known_tex_extensions);
2422 if (!tex_name.empty())
2423 filename = tex_name;
2425 if (makeAbsPath(filename, path).exists()) {
2426 string const abstexname =
2427 makeAbsPath(filename, path).absFileName();
2428 string const abslyxname =
2429 changeExtension(abstexname, ".lyx");
2430 fix_relative_filename(filename);
2431 string const lyxname =
2432 changeExtension(filename, ".lyx");
2433 if (t.cs() != "verbatiminput" &&
2434 tex2lyx(abstexname, FileName(abslyxname),
2436 os << name << '{' << lyxname << "}\n";
2438 os << name << '{' << filename << "}\n";
2441 cerr << "Warning: Could not find included file '"
2442 << filename << "'." << endl;
2443 os << name << '{' << filename << "}\n";
2445 os << "preview false\n";
2449 else if (t.cs() == "bibliographystyle") {
2450 // store new bibliographystyle
2451 bibliographystyle = p.verbatim_item();
2452 // output new bibliographystyle.
2453 // This is only necessary if used in some other macro than \bibliography.
2454 handle_ert(os, "\\bibliographystyle{" + bibliographystyle + "}", context);
2457 else if (t.cs() == "bibliography") {
2458 context.check_layout(os);
2459 begin_inset(os, "LatexCommand ");
2460 os << "bibtex" << "\n";
2461 os << "bibfiles " << '"' << p.verbatim_item() << '"' << "\n";
2462 // Do we have a bibliographystyle set?
2463 if (!bibliographystyle.empty())
2464 os << "options " << '"' << bibliographystyle << '"' << "\n";
2468 else if (t.cs() == "parbox")
2469 parse_box(p, os, FLAG_ITEM, outer, context, true);
2471 //\makebox() is part of the picture environment and different from \makebox{}
2472 //\makebox{} will be parsed by parse_box when bug 2956 is fixed
2473 else if (t.cs() == "makebox") {
2474 string arg = t.asInput();
2475 if (p.next_token().character() == '(')
2476 //the syntax is: \makebox(x,y)[position]{content}
2477 arg += p.getFullParentheseArg();
2479 //the syntax is: \makebox[width][position]{content}
2480 arg += p.getFullOpt();
2481 handle_ert(os, arg + p.getFullOpt(), context);
2484 else if (t.cs() == "smallskip" ||
2485 t.cs() == "medskip" ||
2486 t.cs() == "bigskip" ||
2487 t.cs() == "vfill") {
2488 context.check_layout(os);
2489 begin_inset(os, "VSpace ");
2495 else if (is_known(t.cs(), known_spaces)) {
2496 char const * const * where = is_known(t.cs(), known_spaces);
2497 context.check_layout(os);
2498 os << "\\InsetSpace ";
2499 os << '\\' << known_coded_spaces[where - known_spaces]
2501 // LaTeX swallows whitespace after all spaces except
2502 // "\\,". We have to do that here, too, because LyX
2503 // adds "{}" which would make the spaces significant.
2505 eat_whitespace(p, os, context, false);
2506 // LyX adds "{}" after all spaces except "\\ " and
2507 // "\\,", so we have to remove "{}".
2508 // "\\,{}" is equivalent to "\\," in LaTeX, so we
2509 // remove the braces after "\\,", too.
2514 else if (t.cs() == "newpage" ||
2515 t.cs() == "clearpage" ||
2516 t.cs() == "cleardoublepage") {
2517 context.check_layout(os);
2518 os << "\n\\" << t.cs() << "\n";
2519 skip_braces(p); // eat {}
2522 else if (t.cs() == "newcommand" ||
2523 t.cs() == "providecommand" ||
2524 t.cs() == "renewcommand") {
2525 // these could be handled by parse_command(), but
2526 // we need to call add_known_command() here.
2527 string name = t.asInput();
2528 if (p.next_token().asInput() == "*") {
2529 // Starred form. Eat '*'
2533 string const command = p.verbatim_item();
2534 string const opt1 = p.getOpt();
2535 string const opt2 = p.getFullOpt();
2536 add_known_command(command, opt1, !opt2.empty());
2537 string const ert = name + '{' + command + '}' +
2539 '{' + p.verbatim_item() + '}';
2541 context.check_layout(os);
2542 begin_inset(os, "FormulaMacro");
2547 else if (t.cs() == "vspace") {
2548 bool starred = false;
2549 if (p.next_token().asInput() == "*") {
2553 string const length = p.verbatim_item();
2556 bool valid = splitLatexLength(length, valstring, unit);
2557 bool known_vspace = false;
2558 bool known_unit = false;
2561 istringstream iss(valstring);
2564 if (unit == "\\smallskipamount") {
2566 known_vspace = true;
2567 } else if (unit == "\\medskipamount") {
2569 known_vspace = true;
2570 } else if (unit == "\\bigskipamount") {
2572 known_vspace = true;
2573 } else if (unit == "\\fill") {
2575 known_vspace = true;
2578 if (!known_vspace) {
2579 switch (unitFromString(unit)) {
2600 if (known_unit || known_vspace) {
2601 // Literal length or known variable
2602 context.check_layout(os);
2603 begin_inset(os, "VSpace ");
2611 // LyX can't handle other length variables in Inset VSpace
2612 string name = t.asInput();
2617 handle_ert(os, name + '{' + unit + '}', context);
2618 else if (value == -1.0)
2619 handle_ert(os, name + "{-" + unit + '}', context);
2621 handle_ert(os, name + '{' + valstring + unit + '}', context);
2623 handle_ert(os, name + '{' + length + '}', context);
2628 // try to see whether the string is in unicodesymbols
2630 docstring s = encodings.fromLaTeXCommand(from_utf8(t.asInput()), rem);
2633 cerr << "When parsing " << t.cs()
2634 << ", result is " << to_utf8(s)
2635 << "+" << to_utf8(rem) << endl;
2636 context.check_layout(os);
2639 skip_braces(p); // eat {}
2641 //cerr << "#: " << t << " mode: " << mode << endl;
2642 // heuristic: read up to next non-nested space
2644 string s = t.asInput();
2645 string z = p.verbatim_item();
2646 while (p.good() && z != " " && z.size()) {
2647 //cerr << "read: " << z << endl;
2649 z = p.verbatim_item();
2651 cerr << "found ERT: " << s << endl;
2652 handle_ert(os, s + ' ', context);
2655 string name = t.asInput();
2656 if (p.next_token().asInput() == "*") {
2657 // Starred commands like \vspace*{}
2658 p.get_token(); // Eat '*'
2661 if (!parse_command(name, p, os, outer, context))
2662 handle_ert(os, name, context);
2666 if (flags & FLAG_LEAVE) {
2667 flags &= ~FLAG_LEAVE;