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() != '[')
462 p.get_token(); // eat '['
463 begin_inset(os, "OptArg\n");
464 os << "status collapsed\n\n";
465 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
467 eat_whitespace(p, os, context, false);
471 // This is the code needed to parse required arguments, but
472 // required arguments come into being only much later than the
473 // file format tex2lyx is presently outputting.
474 unsigned int reqargs = 0;
475 while (reqargs < context.layout->reqargs) {
476 eat_whitespace(p, os, context, false);
477 if (p.next_token().character() != '{')
479 p.get_token(); // eat '{'
480 begin_inset(os, "OptArg\n");
481 os << "status collapsed\n\n";
482 parse_text_in_inset(p, os, FLAG_BRACE_LAST, outer, context);
484 eat_whitespace(p, os, context, false);
488 parse_text(p, os, FLAG_ITEM, outer, context);
489 context.check_end_layout(os);
490 if (parent_context.deeper_paragraph) {
491 // We must suppress the "end deeper" because we
492 // suppressed the "begin deeper" above.
493 context.need_end_deeper = false;
495 context.check_end_deeper(os);
496 // We don't need really a new paragraph, but
497 // we must make sure that the next item gets a \begin_layout.
498 parent_context.new_paragraph(os);
503 * Output a space if necessary.
504 * This function gets called for every whitespace token.
506 * We have three cases here:
507 * 1. A space must be suppressed. Example: The lyxcode case below
508 * 2. A space may be suppressed. Example: Spaces before "\par"
509 * 3. A space must not be suppressed. Example: A space between two words
511 * We currently handle only 1. and 3 and from 2. only the case of
512 * spaces before newlines as a side effect.
514 * 2. could be used to suppress as many spaces as possible. This has two effects:
515 * - Reimporting LyX generated LaTeX files changes almost no whitespace
516 * - Superflous whitespace from non LyX generated LaTeX files is removed.
517 * The drawback is that the logic inside the function becomes
518 * complicated, and that is the reason why it is not implemented.
520 void check_space(Parser & p, ostream & os, Context & context)
522 Token const next = p.next_token();
523 Token const curr = p.curr_token();
524 // A space before a single newline and vice versa must be ignored
525 // LyX emits a newline before \end{lyxcode}.
526 // This newline must be ignored,
527 // otherwise LyX will add an additional protected space.
528 if (next.cat() == catSpace ||
529 next.cat() == catNewline ||
530 (next.cs() == "end" && context.layout->free_spacing && curr.cat() == catNewline)) {
533 context.check_layout(os);
539 * Parse all arguments of \p command
541 void parse_arguments(string const & command,
542 vector<ArgumentType> const & template_arguments,
543 Parser & p, ostream & os, bool outer, Context & context)
545 string ert = command;
546 size_t no_arguments = template_arguments.size();
547 for (size_t i = 0; i < no_arguments; ++i) {
548 switch (template_arguments[i]) {
550 // This argument contains regular LaTeX
551 handle_ert(os, ert + '{', context);
552 eat_whitespace(p, os, context, false);
553 parse_text(p, os, FLAG_ITEM, outer, context);
557 // This argument may contain special characters
558 ert += '{' + p.verbatim_item() + '}';
565 handle_ert(os, ert, context);
570 * Check whether \p command is a known command. If yes,
571 * handle the command with all arguments.
572 * \return true if the command was parsed, false otherwise.
574 bool parse_command(string const & command, Parser & p, ostream & os,
575 bool outer, Context & context)
577 if (known_commands.find(command) != known_commands.end()) {
578 parse_arguments(command, known_commands[command], p, os,
586 /// Parses a minipage or parbox
587 void parse_box(Parser & p, ostream & os, unsigned flags, bool outer,
588 Context & parent_context, bool use_parbox)
592 // We need to set the height to the LaTeX default of 1\\totalheight
593 // for the case when no height argument is given
594 string height_value = "1";
595 string height_unit = "in";
596 string height_special = "totalheight";
598 if (p.next_token().asInput() == "[") {
599 position = p.getArg('[', ']');
600 if (position != "t" && position != "c" && position != "b") {
602 cerr << "invalid position for minipage/parbox" << endl;
604 if (p.next_token().asInput() == "[") {
605 latex_height = p.getArg('[', ']');
606 translate_box_len(latex_height, height_value, height_unit, height_special);
608 if (p.next_token().asInput() == "[") {
609 inner_pos = p.getArg('[', ']');
610 if (inner_pos != "c" && inner_pos != "t" &&
611 inner_pos != "b" && inner_pos != "s") {
612 inner_pos = position;
613 cerr << "invalid inner_pos for minipage/parbox"
621 string const latex_width = p.verbatim_item();
622 translate_len(latex_width, width_value, width_unit);
623 if (contains(width_unit, '\\') || contains(height_unit, '\\')) {
624 // LyX can't handle length variables
629 ss << "\\begin{minipage}";
630 if (!position.empty())
631 ss << '[' << position << ']';
632 if (!latex_height.empty())
633 ss << '[' << latex_height << ']';
634 if (!inner_pos.empty())
635 ss << '[' << inner_pos << ']';
636 ss << "{" << latex_width << "}";
639 handle_ert(os, ss.str(), parent_context);
640 parent_context.new_paragraph(os);
641 parse_text_in_inset(p, os, flags, outer, parent_context);
643 handle_ert(os, "}", parent_context);
645 handle_ert(os, "\\end{minipage}", parent_context);
647 // LyX does not like empty positions, so we have
648 // to set them to the LaTeX default values here.
649 if (position.empty())
651 if (inner_pos.empty())
652 inner_pos = position;
653 parent_context.check_layout(os);
654 begin_inset(os, "Box Frameless\n");
655 os << "position \"" << position << "\"\n";
656 os << "hor_pos \"c\"\n";
657 os << "has_inner_box 1\n";
658 os << "inner_pos \"" << inner_pos << "\"\n";
659 os << "use_parbox " << use_parbox << "\n";
660 os << "width \"" << width_value << width_unit << "\"\n";
661 os << "special \"none\"\n";
662 os << "height \"" << height_value << height_unit << "\"\n";
663 os << "height_special \"" << height_special << "\"\n";
664 os << "status open\n\n";
665 parse_text_in_inset(p, os, flags, outer, parent_context);
667 #ifdef PRESERVE_LAYOUT
668 // LyX puts a % after the end of the minipage
669 if (p.next_token().cat() == catNewline && p.next_token().cs().size() > 1) {
671 //handle_comment(os, "%dummy", parent_context);
674 parent_context.new_paragraph(os);
676 else if (p.next_token().cat() == catSpace || p.next_token().cat() == catNewline) {
677 //handle_comment(os, "%dummy", parent_context);
680 // We add a protected space if something real follows
681 if (p.good() && p.next_token().cat() != catComment) {
682 os << "\\InsetSpace ~\n";
690 /// parse an unknown environment
691 void parse_unknown_environment(Parser & p, string const & name, ostream & os,
692 unsigned flags, bool outer,
693 Context & parent_context)
695 if (name == "tabbing")
696 // We need to remember that we have to handle '\=' specially
697 flags |= FLAG_TABBING;
699 // We need to translate font changes and paragraphs inside the
700 // environment to ERT if we have a non standard font.
701 // Otherwise things like
702 // \large\begin{foo}\huge bar\end{foo}
704 bool const specialfont =
705 (parent_context.font != parent_context.normalfont);
706 bool const new_layout_allowed = parent_context.new_layout_allowed;
708 parent_context.new_layout_allowed = false;
709 handle_ert(os, "\\begin{" + name + "}", parent_context);
710 parse_text_snippet(p, os, flags, outer, parent_context);
711 handle_ert(os, "\\end{" + name + "}", parent_context);
713 parent_context.new_layout_allowed = new_layout_allowed;
717 void parse_environment(Parser & p, ostream & os, bool outer,
718 Context & parent_context)
720 Layout const * newlayout;
721 string const name = p.getArg('{', '}');
722 const bool is_starred = suffixIs(name, '*');
723 string const unstarred_name = rtrim(name, "*");
724 active_environments.push_back(name);
726 if (is_math_env(name)) {
727 parent_context.check_layout(os);
728 begin_inset(os, "Formula ");
729 os << "\\begin{" << name << "}";
730 parse_math(p, os, FLAG_END, MATH_MODE);
731 os << "\\end{" << name << "}";
735 else if (name == "tabular" || name == "longtable") {
736 eat_whitespace(p, os, parent_context, false);
737 parent_context.check_layout(os);
738 begin_inset(os, "Tabular ");
739 handle_tabular(p, os, name == "longtable", parent_context);
744 else if (parent_context.textclass.floats().typeExist(unstarred_name)) {
745 eat_whitespace(p, os, parent_context, false);
746 parent_context.check_layout(os);
747 begin_inset(os, "Float " + unstarred_name + "\n");
748 if (p.next_token().asInput() == "[") {
749 os << "placement " << p.getArg('[', ']') << '\n';
751 os << "wide " << convert<string>(is_starred)
752 << "\nsideways false"
753 << "\nstatus open\n\n";
754 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
756 // We don't need really a new paragraph, but
757 // we must make sure that the next item gets a \begin_layout.
758 parent_context.new_paragraph(os);
762 else if (name == "minipage") {
763 eat_whitespace(p, os, parent_context, false);
764 parse_box(p, os, FLAG_END, outer, parent_context, false);
768 else if (name == "comment") {
769 eat_whitespace(p, os, parent_context, false);
770 parent_context.check_layout(os);
771 begin_inset(os, "Note Comment\n");
772 os << "status open\n";
773 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
776 skip_braces(p); // eat {} that might by set by LyX behind comments
779 else if (name == "lyxgreyedout") {
780 eat_whitespace(p, os, parent_context, false);
781 parent_context.check_layout(os);
782 begin_inset(os, "Note Greyedout\n");
783 os << "status open\n";
784 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
789 else if (name == "framed") {
790 eat_whitespace(p, os, parent_context, false);
791 parent_context.check_layout(os);
792 begin_inset(os, "Note Framed\n");
793 os << "status open\n";
794 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
799 else if (name == "shaded") {
800 eat_whitespace(p, os, parent_context, false);
801 parent_context.check_layout(os);
802 begin_inset(os, "Note Shaded\n");
803 os << "status open\n";
804 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
809 else if (!parent_context.new_layout_allowed)
810 parse_unknown_environment(p, name, os, FLAG_END, outer,
813 // Alignment and spacing settings
814 // FIXME (bug xxxx): These settings can span multiple paragraphs and
815 // therefore are totally broken!
816 // Note that \centering, raggedright, and raggedleft cannot be handled, as
817 // they are commands not environments. They are furthermore switches that
818 // can be ended by another switches, but also by commands like \footnote or
819 // \parbox. So the only safe way is to leave them untouched.
820 else if (name == "center" || name == "centering" ||
821 name == "flushleft" || name == "flushright" ||
822 name == "singlespace" || name == "onehalfspace" ||
823 name == "doublespace" || name == "spacing") {
824 eat_whitespace(p, os, parent_context, false);
825 // We must begin a new paragraph if not already done
826 if (! parent_context.atParagraphStart()) {
827 parent_context.check_end_layout(os);
828 parent_context.new_paragraph(os);
830 if (name == "flushleft")
831 parent_context.add_extra_stuff("\\align left\n");
832 else if (name == "flushright")
833 parent_context.add_extra_stuff("\\align right\n");
834 else if (name == "center" || name == "centering")
835 parent_context.add_extra_stuff("\\align center\n");
836 else if (name == "singlespace")
837 parent_context.add_extra_stuff("\\paragraph_spacing single\n");
838 else if (name == "onehalfspace")
839 parent_context.add_extra_stuff("\\paragraph_spacing onehalf\n");
840 else if (name == "doublespace")
841 parent_context.add_extra_stuff("\\paragraph_spacing double\n");
842 else if (name == "spacing")
843 parent_context.add_extra_stuff("\\paragraph_spacing other " + p.verbatim_item() + "\n");
844 parse_text(p, os, FLAG_END, outer, parent_context);
845 // Just in case the environment is empty
846 parent_context.extra_stuff.erase();
847 // We must begin a new paragraph to reset the alignment
848 parent_context.new_paragraph(os);
852 // The single '=' is meant here.
853 else if ((newlayout = findLayout(parent_context.textclass, name)) &&
854 newlayout->isEnvironment()) {
855 eat_whitespace(p, os, parent_context, false);
856 Context context(true, parent_context.textclass, newlayout,
857 parent_context.layout, parent_context.font);
858 if (parent_context.deeper_paragraph) {
859 // We are beginning a nested environment after a
860 // deeper paragraph inside the outer list environment.
861 // Therefore we don't need to output a "begin deeper".
862 context.need_end_deeper = true;
864 parent_context.check_end_layout(os);
865 switch (context.layout->latextype) {
866 case LATEX_LIST_ENVIRONMENT:
867 context.add_par_extra_stuff("\\labelwidthstring "
868 + p.verbatim_item() + '\n');
871 case LATEX_BIB_ENVIRONMENT:
872 p.verbatim_item(); // swallow next arg
878 context.check_deeper(os);
879 parse_text(p, os, FLAG_END, outer, context);
880 context.check_end_layout(os);
881 if (parent_context.deeper_paragraph) {
882 // We must suppress the "end deeper" because we
883 // suppressed the "begin deeper" above.
884 context.need_end_deeper = false;
886 context.check_end_deeper(os);
887 parent_context.new_paragraph(os);
891 else if (name == "appendix") {
892 // This is no good latex style, but it works and is used in some documents...
893 eat_whitespace(p, os, parent_context, false);
894 parent_context.check_end_layout(os);
895 Context context(true, parent_context.textclass, parent_context.layout,
896 parent_context.layout, parent_context.font);
897 context.check_layout(os);
898 os << "\\start_of_appendix\n";
899 parse_text(p, os, FLAG_END, outer, context);
900 context.check_end_layout(os);
904 else if (known_environments.find(name) != known_environments.end()) {
905 vector<ArgumentType> arguments = known_environments[name];
906 // The last "argument" denotes wether we may translate the
907 // environment contents to LyX
908 // The default required if no argument is given makes us
909 // compatible with the reLyXre environment.
910 ArgumentType contents = arguments.empty() ?
913 if (!arguments.empty())
914 arguments.pop_back();
915 // See comment in parse_unknown_environment()
916 bool const specialfont =
917 (parent_context.font != parent_context.normalfont);
918 bool const new_layout_allowed =
919 parent_context.new_layout_allowed;
921 parent_context.new_layout_allowed = false;
922 parse_arguments("\\begin{" + name + "}", arguments, p, os,
923 outer, parent_context);
924 if (contents == verbatim)
925 handle_ert(os, p.verbatimEnvironment(name),
928 parse_text_snippet(p, os, FLAG_END, outer,
930 handle_ert(os, "\\end{" + name + "}", parent_context);
932 parent_context.new_layout_allowed = new_layout_allowed;
936 parse_unknown_environment(p, name, os, FLAG_END, outer,
939 active_environments.pop_back();
943 /// parses a comment and outputs it to \p os.
944 void parse_comment(Parser & p, ostream & os, Token const & t, Context & context)
946 LASSERT(t.cat() == catComment, return);
947 if (!t.cs().empty()) {
948 context.check_layout(os);
949 handle_comment(os, '%' + t.cs(), context);
950 if (p.next_token().cat() == catNewline) {
951 // A newline after a comment line starts a new
953 if (context.new_layout_allowed) {
954 if(!context.atParagraphStart())
955 // Only start a new paragraph if not already
956 // done (we might get called recursively)
957 context.new_paragraph(os);
959 handle_ert(os, "\n", context);
960 eat_whitespace(p, os, context, true);
970 * Reads spaces and comments until the first non-space, non-comment token.
971 * New paragraphs (double newlines or \\par) are handled like simple spaces
972 * if \p eatParagraph is true.
973 * Spaces are skipped, but comments are written to \p os.
975 void eat_whitespace(Parser & p, ostream & os, Context & context,
979 Token const & t = p.get_token();
980 if (t.cat() == catComment)
981 parse_comment(p, os, t, context);
982 else if ((! eatParagraph && p.isParagraph()) ||
983 (t.cat() != catSpace && t.cat() != catNewline)) {
992 * Set a font attribute, parse text and reset the font attribute.
993 * \param attribute Attribute name (e.g. \\family, \\shape etc.)
994 * \param currentvalue Current value of the attribute. Is set to the new
995 * value during parsing.
996 * \param newvalue New value of the attribute
998 void parse_text_attributes(Parser & p, ostream & os, unsigned flags, bool outer,
999 Context & context, string const & attribute,
1000 string & currentvalue, string const & newvalue)
1002 context.check_layout(os);
1003 string const oldvalue = currentvalue;
1004 currentvalue = newvalue;
1005 os << '\n' << attribute << ' ' << newvalue << "\n";
1006 parse_text_snippet(p, os, flags, outer, context);
1007 context.check_layout(os);
1008 os << '\n' << attribute << ' ' << oldvalue << "\n";
1009 currentvalue = oldvalue;
1013 /// get the arguments of a natbib or jurabib citation command
1014 void get_cite_arguments(Parser & p, bool natbibOrder,
1015 string & before, string & after)
1017 // We need to distinguish "" and "[]", so we can't use p.getOpt().
1019 // text before the citation
1021 // text after the citation
1022 after = p.getFullOpt();
1024 if (!after.empty()) {
1025 before = p.getFullOpt();
1026 if (natbibOrder && !before.empty())
1027 swap(before, after);
1032 /// Convert filenames with TeX macros and/or quotes to something LyX
1034 string const normalize_filename(string const & name)
1036 Parser p(trim(name, "\""));
1039 Token const & t = p.get_token();
1040 if (t.cat() != catEscape)
1042 else if (t.cs() == "lyxdot") {
1043 // This is used by LyX for simple dots in relative
1047 } else if (t.cs() == "space") {
1057 /// Convert \p name from TeX convention (relative to master file) to LyX
1058 /// convention (relative to .lyx file) if it is relative
1059 void fix_relative_filename(string & name)
1061 if (FileName::isAbsolute(name))
1064 name = to_utf8(makeRelPath(from_utf8(makeAbsPath(name, getMasterFilePath()).absFileName()),
1065 from_utf8(getParentFilePath())));
1069 /// Parse a NoWeb Scrap section. The initial "<<" is already parsed.
1070 void parse_noweb(Parser & p, ostream & os, Context & context)
1072 // assemble the rest of the keyword
1076 Token const & t = p.get_token();
1077 if (t.asInput() == ">" && p.next_token().asInput() == ">") {
1080 scrap = (p.good() && p.next_token().asInput() == "=");
1082 name += p.get_token().asInput();
1085 name += t.asInput();
1088 if (!scrap || !context.new_layout_allowed ||
1089 !context.textclass.hasLayout(from_ascii("Scrap"))) {
1090 cerr << "Warning: Could not interpret '" << name
1091 << "'. Ignoring it." << endl;
1095 // We use new_paragraph instead of check_end_layout because the stuff
1096 // following the noweb chunk needs to start with a \begin_layout.
1097 // This may create a new paragraph even if there was none in the
1098 // noweb file, but the alternative is an invalid LyX file. Since
1099 // noweb code chunks are implemented with a layout style in LyX they
1100 // always must be in an own paragraph.
1101 context.new_paragraph(os);
1102 Context newcontext(true, context.textclass,
1103 &context.textclass[from_ascii("Scrap")]);
1104 newcontext.check_layout(os);
1107 Token const & t = p.get_token();
1108 // We abuse the parser a bit, because this is no TeX syntax
1110 if (t.cat() == catEscape)
1111 os << subst(t.asInput(), "\\", "\n\\backslash\n");
1113 os << subst(t.asInput(), "\n", "\n\\newline\n");
1114 // The scrap chunk is ended by an @ at the beginning of a line.
1115 // After the @ the line may contain a comment and/or
1116 // whitespace, but nothing else.
1117 if (t.asInput() == "@" && p.prev_token().cat() == catNewline &&
1118 (p.next_token().cat() == catSpace ||
1119 p.next_token().cat() == catNewline ||
1120 p.next_token().cat() == catComment)) {
1121 while (p.good() && p.next_token().cat() == catSpace)
1122 os << p.get_token().asInput();
1123 if (p.next_token().cat() == catComment)
1124 // The comment includes a final '\n'
1125 os << p.get_token().asInput();
1127 if (p.next_token().cat() == catNewline)
1134 newcontext.check_end_layout(os);
1137 } // anonymous namespace
1140 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
1143 Layout const * newlayout = 0;
1144 // Store the latest bibliographystyle (needed for bibtex inset)
1145 string bibliographystyle;
1146 bool const use_natbib = used_packages.find("natbib") != used_packages.end();
1147 bool const use_jurabib = used_packages.find("jurabib") != used_packages.end();
1149 Token const & t = p.get_token();
1152 cerr << "t: " << t << " flags: " << flags << "\n";
1155 if (flags & FLAG_ITEM) {
1156 if (t.cat() == catSpace)
1159 flags &= ~FLAG_ITEM;
1160 if (t.cat() == catBegin) {
1161 // skip the brace and collect everything to the next matching
1163 flags |= FLAG_BRACE_LAST;
1167 // handle only this single token, leave the loop if done
1168 flags |= FLAG_LEAVE;
1171 if (t.character() == ']' && (flags & FLAG_BRACK_LAST))
1173 if (t.character() == '}' && (flags & FLAG_BRACE_LAST))
1179 if (t.cat() == catMath) {
1180 // we are inside some text mode thingy, so opening new math is allowed
1181 context.check_layout(os);
1182 begin_inset(os, "Formula ");
1183 Token const & n = p.get_token();
1184 if (n.cat() == catMath && outer) {
1185 // TeX's $$...$$ syntax for displayed math
1187 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
1189 p.get_token(); // skip the second '$' token
1191 // simple $...$ stuff
1194 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
1200 else if (t.cat() == catSuper || t.cat() == catSub)
1201 cerr << "catcode " << t << " illegal in text mode\n";
1203 // Basic support for english quotes. This should be
1204 // extended to other quotes, but is not so easy (a
1205 // left english quote is the same as a right german
1207 else if (t.asInput() == "`" && p.next_token().asInput() == "`") {
1208 context.check_layout(os);
1209 begin_inset(os, "Quotes ");
1215 else if (t.asInput() == "'" && p.next_token().asInput() == "'") {
1216 context.check_layout(os);
1217 begin_inset(os, "Quotes ");
1224 else if (t.asInput() == ">" && p.next_token().asInput() == ">") {
1225 context.check_layout(os);
1226 begin_inset(os, "Quotes ");
1233 else if (t.asInput() == "<" && p.next_token().asInput() == "<") {
1234 context.check_layout(os);
1235 begin_inset(os, "Quotes ");
1242 else if (t.asInput() == "<"
1243 && p.next_token().asInput() == "<" && noweb_mode) {
1245 parse_noweb(p, os, context);
1248 else if (t.cat() == catSpace || (t.cat() == catNewline && ! p.isParagraph()))
1249 check_space(p, os, context);
1251 else if (t.character() == '[' && noweb_mode &&
1252 p.next_token().character() == '[') {
1253 // These can contain underscores
1255 string const s = p.getFullOpt() + ']';
1256 if (p.next_token().character() == ']')
1259 cerr << "Warning: Inserting missing ']' in '"
1260 << s << "'." << endl;
1261 handle_ert(os, s, context);
1264 else if (t.cat() == catLetter ||
1265 t.cat() == catOther ||
1266 t.cat() == catAlign ||
1267 t.cat() == catParameter) {
1268 // This translates "&" to "\\&" which may be wrong...
1269 context.check_layout(os);
1273 else if (p.isParagraph()) {
1274 if (context.new_layout_allowed)
1275 context.new_paragraph(os);
1277 handle_ert(os, "\\par ", context);
1278 eat_whitespace(p, os, context, true);
1281 else if (t.cat() == catActive) {
1282 context.check_layout(os);
1283 if (t.character() == '~') {
1284 if (context.layout->free_spacing)
1287 os << "\\InsetSpace ~\n";
1292 else if (t.cat() == catBegin &&
1293 p.next_token().cat() == catEnd) {
1295 Token const prev = p.prev_token();
1297 if (p.next_token().character() == '`' ||
1298 (prev.character() == '-' &&
1299 p.next_token().character() == '-'))
1300 ; // ignore it in {}`` or -{}-
1302 handle_ert(os, "{}", context);
1306 else if (t.cat() == catBegin) {
1307 context.check_layout(os);
1308 // special handling of font attribute changes
1309 Token const prev = p.prev_token();
1310 Token const next = p.next_token();
1311 TeXFont const oldFont = context.font;
1312 if (next.character() == '[' ||
1313 next.character() == ']' ||
1314 next.character() == '*') {
1316 if (p.next_token().cat() == catEnd) {
1321 handle_ert(os, "{", context);
1322 parse_text_snippet(p, os,
1325 handle_ert(os, "}", context);
1327 } else if (! context.new_layout_allowed) {
1328 handle_ert(os, "{", context);
1329 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1331 handle_ert(os, "}", context);
1332 } else if (is_known(next.cs(), known_sizes)) {
1333 // next will change the size, so we must
1335 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1337 if (!context.atParagraphStart())
1339 << context.font.size << "\n";
1340 } else if (is_known(next.cs(), known_font_families)) {
1341 // next will change the font family, so we
1342 // must reset it here
1343 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1345 if (!context.atParagraphStart())
1347 << context.font.family << "\n";
1348 } else if (is_known(next.cs(), known_font_series)) {
1349 // next will change the font series, so we
1350 // must reset it here
1351 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1353 if (!context.atParagraphStart())
1355 << context.font.series << "\n";
1356 } else if (is_known(next.cs(), known_font_shapes)) {
1357 // next will change the font shape, so we
1358 // must reset it here
1359 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1361 if (!context.atParagraphStart())
1363 << context.font.shape << "\n";
1364 } else if (is_known(next.cs(), known_old_font_families) ||
1365 is_known(next.cs(), known_old_font_series) ||
1366 is_known(next.cs(), known_old_font_shapes)) {
1367 // next will change the font family, series
1368 // and shape, so we must reset it here
1369 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1371 if (!context.atParagraphStart())
1373 << context.font.family
1375 << context.font.series
1377 << context.font.shape << "\n";
1379 handle_ert(os, "{", context);
1380 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1382 handle_ert(os, "}", context);
1386 else if (t.cat() == catEnd) {
1387 if (flags & FLAG_BRACE_LAST) {
1390 cerr << "stray '}' in text\n";
1391 handle_ert(os, "}", context);
1394 else if (t.cat() == catComment)
1395 parse_comment(p, os, t, context);
1398 // control sequences
1401 else if (t.cs() == "(") {
1402 context.check_layout(os);
1403 begin_inset(os, "Formula");
1405 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
1410 else if (t.cs() == "[") {
1411 context.check_layout(os);
1412 begin_inset(os, "Formula");
1414 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
1419 else if (t.cs() == "begin")
1420 parse_environment(p, os, outer, context);
1422 else if (t.cs() == "end") {
1423 if (flags & FLAG_END) {
1424 // eat environment name
1425 string const name = p.getArg('{', '}');
1426 if (name != active_environment())
1427 cerr << "\\end{" + name + "} does not match \\begin{"
1428 + active_environment() + "}\n";
1431 p.error("found 'end' unexpectedly");
1434 else if (t.cs() == "item") {
1437 bool optarg = false;
1438 if (p.next_token().character() == '[') {
1439 p.get_token(); // eat '['
1440 s = parse_text_snippet(p, FLAG_BRACK_LAST,
1445 context.check_layout(os);
1446 if (context.has_item) {
1447 // An item in an unknown list-like environment
1448 // FIXME: Do this in check_layout()!
1449 context.has_item = false;
1451 handle_ert(os, "\\item", context);
1453 handle_ert(os, "\\item ", context);
1456 if (context.layout->labeltype != LABEL_MANUAL) {
1457 // LyX does not support \item[\mybullet]
1458 // in itemize environments
1459 handle_ert(os, "[", context);
1461 handle_ert(os, "]", context);
1462 } else if (!s.empty()) {
1463 // The space is needed to separate the
1464 // item from the rest of the sentence.
1466 eat_whitespace(p, os, context, false);
1471 else if (t.cs() == "bibitem") {
1473 context.check_layout(os);
1474 begin_inset(os, "LatexCommand ");
1475 os << t.cs() << "\n";
1476 os << "label \"" << p.getOptContent() << "\"\n";
1477 os << "key \"" << p.verbatim_item() << "\"\n";
1481 else if (t.cs() == "def") {
1482 context.check_layout(os);
1483 eat_whitespace(p, os, context, false);
1484 string name = p.get_token().cs();
1485 eat_whitespace(p, os, context, false);
1491 while (p.next_token().cat() != catBegin) {
1492 if (p.next_token().cat() == catParameter) {
1497 // followed by number?
1498 if (p.next_token().cat() == catOther) {
1499 char c = p.getChar();
1501 // number = current arity + 1?
1502 if (c == arity + '0' + 1)
1507 paramtext += p.get_token().cs();
1509 paramtext += p.get_token().cs();
1514 // only output simple (i.e. compatible) macro as FormulaMacros
1515 string ert = "\\def\\" + name + ' ' + paramtext + '{' + p.verbatim_item() + '}';
1517 context.check_layout(os);
1518 begin_inset(os, "FormulaMacro");
1522 handle_ert(os, ert, context);
1525 else if (t.cs() == "noindent") {
1527 context.add_par_extra_stuff("\\noindent\n");
1530 else if (t.cs() == "appendix") {
1531 context.add_par_extra_stuff("\\start_of_appendix\n");
1532 // We need to start a new paragraph. Otherwise the
1533 // appendix in 'bla\appendix\chapter{' would start
1535 context.new_paragraph(os);
1536 // We need to make sure that the paragraph is
1537 // generated even if it is empty. Otherwise the
1538 // appendix in '\par\appendix\par\chapter{' would
1540 context.check_layout(os);
1541 // FIXME: This is a hack to prevent paragraph
1542 // deletion if it is empty. Handle this better!
1544 "%dummy comment inserted by tex2lyx to "
1545 "ensure that this paragraph is not empty",
1547 // Both measures above may generate an additional
1548 // empty paragraph, but that does not hurt, because
1549 // whitespace does not matter here.
1550 eat_whitespace(p, os, context, true);
1553 // Starred section headings
1554 // Must attempt to parse "Section*" before "Section".
1555 else if ((p.next_token().asInput() == "*") &&
1556 context.new_layout_allowed &&
1557 (newlayout = findLayout(context.textclass, t.cs() + '*')) &&
1558 newlayout->isCommand()) {
1559 TeXFont const oldFont = context.font;
1560 // save the current font size
1561 string const size = oldFont.size;
1562 // reset the font size to default, because the
1563 // font size switches don't affect section
1564 // headings and the like
1565 context.font.size = known_coded_sizes[0];
1566 output_font_change(os, oldFont, context.font);
1569 output_command_layout(os, p, outer, context, newlayout);
1570 // set the font size to the original value
1571 context.font.size = size;
1572 output_font_change(os, oldFont, context.font);
1576 // Section headings and the like
1577 else if (context.new_layout_allowed &&
1578 (newlayout = findLayout(context.textclass, t.cs())) &&
1579 newlayout->isCommand()) {
1580 TeXFont const oldFont = context.font;
1581 // save the current font size
1582 string const size = oldFont.size;
1583 // reset the font size to default, because the font size switches don't
1584 // affect section headings and the like
1585 context.font.size = known_coded_sizes[0];
1586 output_font_change(os, oldFont, context.font);
1588 output_command_layout(os, p, outer, context, newlayout);
1589 // set the font size to the original value
1590 context.font.size = size;
1591 output_font_change(os, oldFont, context.font);
1595 else if (t.cs() == "caption") {
1596 // FIXME: this should get some cleanup. All
1597 // the \begin_layout:s are output by the
1600 context.check_layout(os);
1602 begin_inset(os, "Caption\n\n");
1603 os << "\\begin_layout "
1604 << to_utf8(context.textclass.defaultLayout().name())
1606 if (p.next_token().character() == '[') {
1607 p.get_token(); // eat '['
1608 begin_inset(os, "OptArg\n");
1609 os << "status collapsed\n";
1610 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
1612 eat_whitespace(p, os, context, false);
1614 parse_text(p, os, FLAG_ITEM, outer, context);
1615 context.check_end_layout(os);
1616 // We don't need really a new paragraph, but
1617 // we must make sure that the next item gets a \begin_layout.
1618 context.new_paragraph(os);
1621 os << "\\end_layout\n";
1624 else if (t.cs() == "includegraphics") {
1625 bool const clip = p.next_token().asInput() == "*";
1628 map<string, string> opts = split_map(p.getArg('[', ']'));
1630 opts["clip"] = string();
1631 string name = normalize_filename(p.verbatim_item());
1633 string const path = getMasterFilePath();
1634 // We want to preserve relative / absolute filenames,
1635 // therefore path is only used for testing
1636 if (!makeAbsPath(name, path).exists()) {
1637 // The file extension is probably missing.
1638 // Now try to find it out.
1639 string const dvips_name =
1640 find_file(name, path,
1641 known_dvips_graphics_formats);
1642 string const pdftex_name =
1643 find_file(name, path,
1644 known_pdftex_graphics_formats);
1645 if (!dvips_name.empty()) {
1646 if (!pdftex_name.empty()) {
1647 cerr << "This file contains the "
1649 "\"\\includegraphics{"
1651 "However, files\n\""
1652 << dvips_name << "\" and\n\""
1653 << pdftex_name << "\"\n"
1654 "both exist, so I had to make a "
1655 "choice and took the first one.\n"
1656 "Please move the unwanted one "
1657 "someplace else and try again\n"
1658 "if my choice was wrong."
1662 } else if (!pdftex_name.empty())
1666 if (makeAbsPath(name, path).exists())
1667 fix_relative_filename(name);
1669 cerr << "Warning: Could not find graphics file '"
1670 << name << "'." << endl;
1672 context.check_layout(os);
1673 begin_inset(os, "Graphics ");
1674 os << "\n\tfilename " << name << '\n';
1675 if (opts.find("width") != opts.end())
1677 << translate_len(opts["width"]) << '\n';
1678 if (opts.find("height") != opts.end())
1680 << translate_len(opts["height"]) << '\n';
1681 if (opts.find("scale") != opts.end()) {
1682 istringstream iss(opts["scale"]);
1686 os << "\tscale " << val << '\n';
1688 if (opts.find("angle") != opts.end())
1689 os << "\trotateAngle "
1690 << opts["angle"] << '\n';
1691 if (opts.find("origin") != opts.end()) {
1693 string const opt = opts["origin"];
1694 if (opt.find('l') != string::npos) ss << "left";
1695 if (opt.find('r') != string::npos) ss << "right";
1696 if (opt.find('c') != string::npos) ss << "center";
1697 if (opt.find('t') != string::npos) ss << "Top";
1698 if (opt.find('b') != string::npos) ss << "Bottom";
1699 if (opt.find('B') != string::npos) ss << "Baseline";
1700 if (!ss.str().empty())
1701 os << "\trotateOrigin " << ss.str() << '\n';
1703 cerr << "Warning: Ignoring unknown includegraphics origin argument '" << opt << "'\n";
1705 if (opts.find("keepaspectratio") != opts.end())
1706 os << "\tkeepAspectRatio\n";
1707 if (opts.find("clip") != opts.end())
1709 if (opts.find("draft") != opts.end())
1711 if (opts.find("bb") != opts.end())
1712 os << "\tBoundingBox "
1713 << opts["bb"] << '\n';
1714 int numberOfbbOptions = 0;
1715 if (opts.find("bbllx") != opts.end())
1716 numberOfbbOptions++;
1717 if (opts.find("bblly") != opts.end())
1718 numberOfbbOptions++;
1719 if (opts.find("bburx") != opts.end())
1720 numberOfbbOptions++;
1721 if (opts.find("bbury") != opts.end())
1722 numberOfbbOptions++;
1723 if (numberOfbbOptions == 4)
1724 os << "\tBoundingBox "
1725 << opts["bbllx"] << " " << opts["bblly"] << " "
1726 << opts["bburx"] << " " << opts["bbury"] << '\n';
1727 else if (numberOfbbOptions > 0)
1728 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
1729 numberOfbbOptions = 0;
1730 if (opts.find("natwidth") != opts.end())
1731 numberOfbbOptions++;
1732 if (opts.find("natheight") != opts.end())
1733 numberOfbbOptions++;
1734 if (numberOfbbOptions == 2)
1735 os << "\tBoundingBox 0bp 0bp "
1736 << opts["natwidth"] << " " << opts["natheight"] << '\n';
1737 else if (numberOfbbOptions > 0)
1738 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
1739 ostringstream special;
1740 if (opts.find("hiresbb") != opts.end())
1741 special << "hiresbb,";
1742 if (opts.find("trim") != opts.end())
1744 if (opts.find("viewport") != opts.end())
1745 special << "viewport=" << opts["viewport"] << ',';
1746 if (opts.find("totalheight") != opts.end())
1747 special << "totalheight=" << opts["totalheight"] << ',';
1748 if (opts.find("type") != opts.end())
1749 special << "type=" << opts["type"] << ',';
1750 if (opts.find("ext") != opts.end())
1751 special << "ext=" << opts["ext"] << ',';
1752 if (opts.find("read") != opts.end())
1753 special << "read=" << opts["read"] << ',';
1754 if (opts.find("command") != opts.end())
1755 special << "command=" << opts["command"] << ',';
1756 string s_special = special.str();
1757 if (!s_special.empty()) {
1758 // We had special arguments. Remove the trailing ','.
1759 os << "\tspecial " << s_special.substr(0, s_special.size() - 1) << '\n';
1761 // TODO: Handle the unknown settings better.
1762 // Warn about invalid options.
1763 // Check whether some option was given twice.
1767 else if (t.cs() == "footnote" ||
1768 (t.cs() == "thanks" && context.layout->intitle)) {
1770 context.check_layout(os);
1771 begin_inset(os, "Foot\n");
1772 os << "status collapsed\n\n";
1773 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
1777 else if (t.cs() == "marginpar") {
1779 context.check_layout(os);
1780 begin_inset(os, "Marginal\n");
1781 os << "status collapsed\n\n";
1782 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
1786 else if (t.cs() == "ensuremath") {
1788 context.check_layout(os);
1789 string const s = p.verbatim_item();
1790 //FIXME: this never triggers in UTF8
1791 if (s == "\xb1" || s == "\xb3" || s == "\xb2" || s == "\xb5")
1794 handle_ert(os, "\\ensuremath{" + s + "}",
1798 else if (t.cs() == "hfill") {
1799 context.check_layout(os);
1800 os << "\n\\hfill\n";
1805 else if (t.cs() == "makeindex" || t.cs() == "maketitle") {
1806 // FIXME: Somehow prevent title layouts if
1807 // "maketitle" was not found
1809 skip_braces(p); // swallow this
1812 else if (t.cs() == "tableofcontents") {
1814 context.check_layout(os);
1815 begin_inset(os, "LatexCommand ");
1816 os << t.cs() << "\n";
1818 skip_braces(p); // swallow this
1821 else if (t.cs() == "listoffigures") {
1823 context.check_layout(os);
1824 begin_inset(os, "FloatList figure\n");
1826 skip_braces(p); // swallow this
1829 else if (t.cs() == "listoftables") {
1831 context.check_layout(os);
1832 begin_inset(os, "FloatList table\n");
1834 skip_braces(p); // swallow this
1837 else if (t.cs() == "listof") {
1838 p.skip_spaces(true);
1839 string const name = p.get_token().cs();
1840 if (context.textclass.floats().typeExist(name)) {
1841 context.check_layout(os);
1842 begin_inset(os, "FloatList ");
1845 p.get_token(); // swallow second arg
1847 handle_ert(os, "\\listof{" + name + "}", context);
1850 else if (t.cs() == "textrm")
1851 parse_text_attributes(p, os, FLAG_ITEM, outer,
1852 context, "\\family",
1853 context.font.family, "roman");
1855 else if (t.cs() == "textsf")
1856 parse_text_attributes(p, os, FLAG_ITEM, outer,
1857 context, "\\family",
1858 context.font.family, "sans");
1860 else if (t.cs() == "texttt")
1861 parse_text_attributes(p, os, FLAG_ITEM, outer,
1862 context, "\\family",
1863 context.font.family, "typewriter");
1865 else if (t.cs() == "textmd")
1866 parse_text_attributes(p, os, FLAG_ITEM, outer,
1867 context, "\\series",
1868 context.font.series, "medium");
1870 else if (t.cs() == "textbf")
1871 parse_text_attributes(p, os, FLAG_ITEM, outer,
1872 context, "\\series",
1873 context.font.series, "bold");
1875 else if (t.cs() == "textup")
1876 parse_text_attributes(p, os, FLAG_ITEM, outer,
1878 context.font.shape, "up");
1880 else if (t.cs() == "textit")
1881 parse_text_attributes(p, os, FLAG_ITEM, outer,
1883 context.font.shape, "italic");
1885 else if (t.cs() == "textsl")
1886 parse_text_attributes(p, os, FLAG_ITEM, outer,
1888 context.font.shape, "slanted");
1890 else if (t.cs() == "textsc")
1891 parse_text_attributes(p, os, FLAG_ITEM, outer,
1893 context.font.shape, "smallcaps");
1895 else if (t.cs() == "textnormal" || t.cs() == "normalfont") {
1896 context.check_layout(os);
1897 TeXFont oldFont = context.font;
1898 context.font.init();
1899 context.font.size = oldFont.size;
1900 os << "\n\\family " << context.font.family << "\n";
1901 os << "\n\\series " << context.font.series << "\n";
1902 os << "\n\\shape " << context.font.shape << "\n";
1903 if (t.cs() == "textnormal") {
1904 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1905 output_font_change(os, context.font, oldFont);
1906 context.font = oldFont;
1908 eat_whitespace(p, os, context, false);
1911 else if (t.cs() == "textcolor") {
1912 // scheme is \textcolor{color name}{text}
1913 string const color = p.verbatim_item();
1914 // we only support the predefined colors of the color package
1915 if (color == "black" || color == "blue" || color == "cyan"
1916 || color == "green" || color == "magenta" || color == "red"
1917 || color == "white" || color == "yellow") {
1918 context.check_layout(os);
1919 os << "\n\\color " << color << "\n";
1920 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1921 context.check_layout(os);
1922 os << "\n\\color inherit\n";
1924 // for custom defined colors
1925 handle_ert(os, t.asInput() + "{" + color + "}", context);
1928 else if (t.cs() == "underbar") {
1929 // Do NOT handle \underline.
1930 // \underbar cuts through y, g, q, p etc.,
1931 // \underline does not.
1932 context.check_layout(os);
1933 os << "\n\\bar under\n";
1934 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1935 context.check_layout(os);
1936 os << "\n\\bar default\n";
1939 else if (t.cs() == "emph" || t.cs() == "noun") {
1940 context.check_layout(os);
1941 os << "\n\\" << t.cs() << " on\n";
1942 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1943 context.check_layout(os);
1944 os << "\n\\" << t.cs() << " default\n";
1947 else if (t.cs() == "lyxline") {
1948 context.check_layout(os);
1952 else if (is_known(t.cs(), known_ref_commands)) {
1953 context.check_layout(os);
1954 begin_inset(os, "LatexCommand ");
1955 os << t.cs() << "\n";
1956 // LyX cannot handle newlines in a latex command
1957 // FIXME: Move the substitution into parser::getOpt()?
1958 os << subst(p.getOpt(), "\n", " ");
1959 os << "reference " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
1963 else if (use_natbib &&
1964 is_known(t.cs(), known_natbib_commands) &&
1965 ((t.cs() != "citefullauthor" &&
1966 t.cs() != "citeyear" &&
1967 t.cs() != "citeyearpar") ||
1968 p.next_token().asInput() != "*")) {
1969 context.check_layout(os);
1970 string command = t.cs();
1971 if (p.next_token().asInput() == "*") {
1975 if (command == "citefullauthor")
1976 // alternative name for "\\citeauthor*"
1977 command = "citeauthor*";
1979 // text before the citation
1981 // text after the citation
1983 get_cite_arguments(p, true, before, after);
1985 if (command == "cite") {
1986 // \cite without optional argument means
1987 // \citet, \cite with at least one optional
1988 // argument means \citep.
1989 if (before.empty() && after.empty())
1994 if (before.empty() && after == "[]")
1995 // avoid \citet[]{a}
1997 else if (before == "[]" && after == "[]") {
1998 // avoid \citet[][]{a}
2002 // remove the brackets around after and before
2003 if (!after.empty()) {
2005 after.erase(after.length() - 1, 1);
2006 // LyX cannot handle newlines in the parameter
2007 after = subst(after, "\n", " ");
2009 if (!before.empty()) {
2011 before.erase(before.length() - 1, 1);
2012 // LyX cannot handle newlines in the parameter
2013 before = subst(before, "\n", " ");
2015 begin_inset(os, "LatexCommand ");
2016 os << t.cs() << "\n";
2017 os << "after " << '"' << after << '"' << "\n";
2018 os << "before " << '"' << before << '"' << "\n";
2019 os << "key " << '"' << p.verbatim_item() << '"' << "\n";
2023 else if (use_jurabib &&
2024 is_known(t.cs(), known_jurabib_commands)) {
2025 context.check_layout(os);
2026 string const command = t.cs();
2027 char argumentOrder = '\0';
2028 vector<string> const & options = used_packages["jurabib"];
2029 if (find(options.begin(), options.end(),
2030 "natbiborder") != options.end())
2031 argumentOrder = 'n';
2032 else if (find(options.begin(), options.end(),
2033 "jurabiborder") != options.end())
2034 argumentOrder = 'j';
2036 // text before the citation
2038 // text after the citation
2040 get_cite_arguments(p, argumentOrder != 'j', before, after);
2042 string const citation = p.verbatim_item();
2043 if (!before.empty() && argumentOrder == '\0') {
2044 cerr << "Warning: Assuming argument order "
2045 "of jurabib version 0.6 for\n'"
2046 << command << before << after << '{'
2047 << citation << "}'.\n"
2048 "Add 'jurabiborder' to the jurabib "
2049 "package options if you used an\n"
2050 "earlier jurabib version." << endl;
2052 if (!after.empty()) {
2054 after.erase(after.length() - 1, 1);
2056 if (!before.empty()) {
2058 before.erase(before.length() - 1, 1);
2060 begin_inset(os, "LatexCommand ");
2061 os << t.cs() << "\n";
2062 os << "after " << '"' << after << '"' << "\n";
2063 os << "before " << '"' << before << '"' << "\n";
2064 os << "key " << '"' << citation << '"' << "\n";
2068 else if (t.cs() == "cite") {
2069 context.check_layout(os);
2070 // LyX cannot handle newlines in a latex command
2071 string after = subst(p.getOptContent(), "\n", " ");
2072 begin_inset(os, "LatexCommand ");
2073 os << t.cs() << "\n";
2074 os << "after " << '"' << after << '"' << "\n";
2075 os << "key " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
2079 else if (t.cs() == "index") {
2080 context.check_layout(os);
2081 begin_inset(os, "LatexCommand ");
2082 os << t.cs() << "\n";
2083 // LyX cannot handle newlines in a latex command
2084 os << "name " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
2088 else if (t.cs() == "nomenclature") {
2089 context.check_layout(os);
2090 begin_inset(os, "LatexCommand ");
2091 os << t.cs() << "\n";
2092 // LyX cannot handle newlines in a latex command
2093 string prefix = subst(p.getOptContent(), "\n", " ");
2094 if (!prefix.empty())
2095 os << "prefix " << '"' << prefix << '"' << "\n";
2096 os << "symbol " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
2097 os << "description " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
2101 else if (t.cs() == "label") {
2102 context.check_layout(os);
2103 begin_inset(os, "LatexCommand ");
2104 os << t.cs() << "\n";
2105 // LyX cannot handle newlines in a latex command
2106 os << "name " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
2110 else if (t.cs() == "printindex") {
2111 context.check_layout(os);
2112 begin_inset(os, "LatexCommand ");
2113 os << t.cs() << "\n";
2118 else if (t.cs() == "printnomenclature") {
2119 context.check_layout(os);
2120 begin_inset(os, "LatexCommand ");
2121 os << t.cs() << "\n";
2126 else if (t.cs() == "url") {
2127 context.check_layout(os);
2128 begin_inset(os, "LatexCommand ");
2129 os << t.cs() << "\n";
2130 // LyX cannot handle newlines in a latex command
2131 os << "target " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
2135 else if (is_known(t.cs(), known_quotes)) {
2136 char const * const * where = is_known(t.cs(), known_quotes);
2137 context.check_layout(os);
2138 begin_inset(os, "Quotes ");
2139 os << known_coded_quotes[where - known_quotes];
2141 // LyX adds {} after the quote, so we have to eat
2142 // spaces here if there are any before a possible
2144 eat_whitespace(p, os, context, false);
2148 else if (is_known(t.cs(), known_sizes) &&
2149 context.new_layout_allowed) {
2150 char const * const * where = is_known(t.cs(), known_sizes);
2151 context.check_layout(os);
2152 TeXFont const oldFont = context.font;
2153 // the font size index differs by 1, because the known_coded_sizes
2154 // has additionally a "default" entry
2155 context.font.size = known_coded_sizes[where - known_sizes + 1];
2156 output_font_change(os, oldFont, context.font);
2157 eat_whitespace(p, os, context, false);
2160 else if (is_known(t.cs(), known_font_families) &&
2161 context.new_layout_allowed) {
2162 char const * const * where =
2163 is_known(t.cs(), known_font_families);
2164 context.check_layout(os);
2165 TeXFont const oldFont = context.font;
2166 context.font.family =
2167 known_coded_font_families[where - known_font_families];
2168 output_font_change(os, oldFont, context.font);
2169 eat_whitespace(p, os, context, false);
2172 else if (is_known(t.cs(), known_font_series) &&
2173 context.new_layout_allowed) {
2174 char const * const * where =
2175 is_known(t.cs(), known_font_series);
2176 context.check_layout(os);
2177 TeXFont const oldFont = context.font;
2178 context.font.series =
2179 known_coded_font_series[where - known_font_series];
2180 output_font_change(os, oldFont, context.font);
2181 eat_whitespace(p, os, context, false);
2184 else if (is_known(t.cs(), known_font_shapes) &&
2185 context.new_layout_allowed) {
2186 char const * const * where =
2187 is_known(t.cs(), known_font_shapes);
2188 context.check_layout(os);
2189 TeXFont const oldFont = context.font;
2190 context.font.shape =
2191 known_coded_font_shapes[where - known_font_shapes];
2192 output_font_change(os, oldFont, context.font);
2193 eat_whitespace(p, os, context, false);
2195 else if (is_known(t.cs(), known_old_font_families) &&
2196 context.new_layout_allowed) {
2197 char const * const * where =
2198 is_known(t.cs(), known_old_font_families);
2199 context.check_layout(os);
2200 TeXFont const oldFont = context.font;
2201 context.font.init();
2202 context.font.size = oldFont.size;
2203 context.font.family =
2204 known_coded_font_families[where - known_old_font_families];
2205 output_font_change(os, oldFont, context.font);
2206 eat_whitespace(p, os, context, false);
2209 else if (is_known(t.cs(), known_old_font_series) &&
2210 context.new_layout_allowed) {
2211 char const * const * where =
2212 is_known(t.cs(), known_old_font_series);
2213 context.check_layout(os);
2214 TeXFont const oldFont = context.font;
2215 context.font.init();
2216 context.font.size = oldFont.size;
2217 context.font.series =
2218 known_coded_font_series[where - known_old_font_series];
2219 output_font_change(os, oldFont, context.font);
2220 eat_whitespace(p, os, context, false);
2223 else if (is_known(t.cs(), known_old_font_shapes) &&
2224 context.new_layout_allowed) {
2225 char const * const * where =
2226 is_known(t.cs(), known_old_font_shapes);
2227 context.check_layout(os);
2228 TeXFont const oldFont = context.font;
2229 context.font.init();
2230 context.font.size = oldFont.size;
2231 context.font.shape =
2232 known_coded_font_shapes[where - known_old_font_shapes];
2233 output_font_change(os, oldFont, context.font);
2234 eat_whitespace(p, os, context, false);
2237 else if (t.cs() == "selectlanguage") {
2238 context.check_layout(os);
2239 // save the language for the case that a
2240 // \foreignlanguage is used
2242 context.font.language = subst(p.verbatim_item(), "\n", " ");
2243 os << "\\lang " << context.font.language << "\n";
2246 else if (t.cs() == "foreignlanguage") {
2247 context.check_layout(os);
2248 os << "\n\\lang " << subst(p.verbatim_item(), "\n", " ") << "\n";
2249 os << subst(p.verbatim_item(), "\n", " ");
2250 // FIXME: the second argument of selectlanguage
2251 // has to be parsed (like for \textsf, for
2253 // set back to last selectlanguage
2254 os << "\n\\lang " << context.font.language << "\n";
2257 else if (t.cs() == "inputencoding") {
2258 // nothing to write here
2259 string const enc = subst(p.verbatim_item(), "\n", " ");
2263 else if (t.cs() == "LyX" || t.cs() == "TeX"
2264 || t.cs() == "LaTeX") {
2265 context.check_layout(os);
2267 skip_braces(p); // eat {}
2270 else if (t.cs() == "LaTeXe") {
2271 context.check_layout(os);
2273 skip_braces(p); // eat {}
2276 else if (t.cs() == "ldots") {
2277 context.check_layout(os);
2279 os << "\\SpecialChar \\ldots{}\n";
2282 else if (t.cs() == "lyxarrow") {
2283 context.check_layout(os);
2284 os << "\\SpecialChar \\menuseparator\n";
2288 else if (t.cs() == "textcompwordmark") {
2289 context.check_layout(os);
2290 os << "\\SpecialChar \\textcompwordmark{}\n";
2294 else if (t.cs() == "textquotedbl") {
2295 context.check_layout(os);
2300 else if (t.cs() == "@" && p.next_token().asInput() == ".") {
2301 context.check_layout(os);
2302 os << "\\SpecialChar \\@.\n";
2306 else if (t.cs() == "-") {
2307 context.check_layout(os);
2308 os << "\\SpecialChar \\-\n";
2311 else if (t.cs() == "textasciitilde") {
2312 context.check_layout(os);
2317 else if (t.cs() == "textasciicircum") {
2318 context.check_layout(os);
2323 else if (t.cs() == "textbackslash") {
2324 context.check_layout(os);
2325 os << "\n\\backslash\n";
2329 else if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#"
2330 || t.cs() == "$" || t.cs() == "{" || t.cs() == "}"
2332 context.check_layout(os);
2336 else if (t.cs() == "char") {
2337 context.check_layout(os);
2338 if (p.next_token().character() == '`') {
2340 if (p.next_token().cs() == "\"") {
2345 handle_ert(os, "\\char`", context);
2348 handle_ert(os, "\\char", context);
2352 else if (t.cs() == "verb") {
2353 context.check_layout(os);
2354 char const delimiter = p.next_token().character();
2355 string const arg = p.getArg(delimiter, delimiter);
2357 oss << "\\verb" << delimiter << arg << delimiter;
2358 handle_ert(os, oss.str(), context);
2361 // Problem: \= creates a tabstop inside the tabbing environment
2362 // and else an accent. In the latter case we really would want
2363 // \={o} instead of \= o.
2364 else if (t.cs() == "=" && (flags & FLAG_TABBING))
2365 handle_ert(os, t.asInput(), context);
2367 // accents (see Table 6 in Comprehensive LaTeX Symbol List)
2368 else if (t.cs().size() == 1
2369 && contains("\"'.=^`bcdHkrtuv~", t.cs())) {
2370 context.check_layout(os);
2371 // try to see whether the string is in unicodesymbols
2373 string command = t.asInput() + "{"
2374 + trim(p.verbatim_item())
2376 docstring s = encodings.fromLaTeXCommand(from_utf8(command), rem);
2379 cerr << "When parsing " << command
2380 << ", result is " << to_utf8(s)
2381 << "+" << to_utf8(rem) << endl;
2384 // we did not find a non-ert version
2385 handle_ert(os, command, context);
2388 else if (t.cs() == "\\") {
2389 context.check_layout(os);
2390 string const next = p.next_token().asInput();
2392 handle_ert(os, "\\\\" + p.getOpt(), context);
2393 else if (next == "*") {
2395 handle_ert(os, "\\\\*" + p.getOpt(), context);
2398 os << "\n\\newline\n";
2402 else if (t.cs() == "newline") {
2403 context.check_layout(os);
2404 os << "\n\\" << t.cs() << "\n";
2405 skip_braces(p); // eat {}
2408 else if (t.cs() == "input" || t.cs() == "include"
2409 || t.cs() == "verbatiminput") {
2410 string name = '\\' + t.cs();
2411 if (t.cs() == "verbatiminput"
2412 && p.next_token().asInput() == "*")
2413 name += p.get_token().asInput();
2414 context.check_layout(os);
2415 begin_inset(os, "Include ");
2416 string filename(normalize_filename(p.getArg('{', '}')));
2417 string const path = getMasterFilePath();
2418 // We want to preserve relative / absolute filenames,
2419 // therefore path is only used for testing
2420 if ((t.cs() == "include" || t.cs() == "input") &&
2421 !makeAbsPath(filename, path).exists()) {
2422 // The file extension is probably missing.
2423 // Now try to find it out.
2424 string const tex_name =
2425 find_file(filename, path,
2426 known_tex_extensions);
2427 if (!tex_name.empty())
2428 filename = tex_name;
2430 if (makeAbsPath(filename, path).exists()) {
2431 string const abstexname =
2432 makeAbsPath(filename, path).absFileName();
2433 string const abslyxname =
2434 changeExtension(abstexname, ".lyx");
2435 fix_relative_filename(filename);
2436 string const lyxname =
2437 changeExtension(filename, ".lyx");
2438 if (t.cs() != "verbatiminput" &&
2439 tex2lyx(abstexname, FileName(abslyxname),
2441 os << name << '{' << lyxname << "}\n";
2443 os << name << '{' << filename << "}\n";
2446 cerr << "Warning: Could not find included file '"
2447 << filename << "'." << endl;
2448 os << name << '{' << filename << "}\n";
2450 os << "preview false\n";
2454 else if (t.cs() == "bibliographystyle") {
2455 // store new bibliographystyle
2456 bibliographystyle = p.verbatim_item();
2457 // output new bibliographystyle.
2458 // This is only necessary if used in some other macro than \bibliography.
2459 handle_ert(os, "\\bibliographystyle{" + bibliographystyle + "}", context);
2462 else if (t.cs() == "bibliography") {
2463 context.check_layout(os);
2464 begin_inset(os, "LatexCommand ");
2465 os << "bibtex" << "\n";
2466 os << "bibfiles " << '"' << p.verbatim_item() << '"' << "\n";
2467 // Do we have a bibliographystyle set?
2468 if (!bibliographystyle.empty())
2469 os << "options " << '"' << bibliographystyle << '"' << "\n";
2473 else if (t.cs() == "parbox")
2474 parse_box(p, os, FLAG_ITEM, outer, context, true);
2476 //\makebox() is part of the picture environment and different from \makebox{}
2477 //\makebox{} will be parsed by parse_box when bug 2956 is fixed
2478 else if (t.cs() == "makebox") {
2479 string arg = t.asInput();
2480 if (p.next_token().character() == '(')
2481 //the syntax is: \makebox(x,y)[position]{content}
2482 arg += p.getFullParentheseArg();
2484 //the syntax is: \makebox[width][position]{content}
2485 arg += p.getFullOpt();
2486 handle_ert(os, arg + p.getFullOpt(), context);
2489 else if (t.cs() == "smallskip" ||
2490 t.cs() == "medskip" ||
2491 t.cs() == "bigskip" ||
2492 t.cs() == "vfill") {
2493 context.check_layout(os);
2494 begin_inset(os, "VSpace ");
2500 else if (is_known(t.cs(), known_spaces)) {
2501 char const * const * where = is_known(t.cs(), known_spaces);
2502 context.check_layout(os);
2503 os << "\\InsetSpace ";
2504 os << '\\' << known_coded_spaces[where - known_spaces]
2506 // LaTeX swallows whitespace after all spaces except
2507 // "\\,". We have to do that here, too, because LyX
2508 // adds "{}" which would make the spaces significant.
2510 eat_whitespace(p, os, context, false);
2511 // LyX adds "{}" after all spaces except "\\ " and
2512 // "\\,", so we have to remove "{}".
2513 // "\\,{}" is equivalent to "\\," in LaTeX, so we
2514 // remove the braces after "\\,", too.
2519 else if (t.cs() == "newpage" ||
2520 t.cs() == "clearpage" ||
2521 t.cs() == "cleardoublepage") {
2522 context.check_layout(os);
2523 os << "\n\\" << t.cs() << "\n";
2524 skip_braces(p); // eat {}
2527 else if (t.cs() == "newcommand" ||
2528 t.cs() == "providecommand" ||
2529 t.cs() == "renewcommand") {
2530 // these could be handled by parse_command(), but
2531 // we need to call add_known_command() here.
2532 string name = t.asInput();
2533 if (p.next_token().asInput() == "*") {
2534 // Starred form. Eat '*'
2538 string const command = p.verbatim_item();
2539 string const opt1 = p.getOpt();
2540 string const opt2 = p.getFullOpt();
2541 add_known_command(command, opt1, !opt2.empty());
2542 string const ert = name + '{' + command + '}' +
2544 '{' + p.verbatim_item() + '}';
2546 context.check_layout(os);
2547 begin_inset(os, "FormulaMacro");
2552 else if (t.cs() == "vspace") {
2553 bool starred = false;
2554 if (p.next_token().asInput() == "*") {
2558 string const length = p.verbatim_item();
2561 bool valid = splitLatexLength(length, valstring, unit);
2562 bool known_vspace = false;
2563 bool known_unit = false;
2566 istringstream iss(valstring);
2569 if (unit == "\\smallskipamount") {
2571 known_vspace = true;
2572 } else if (unit == "\\medskipamount") {
2574 known_vspace = true;
2575 } else if (unit == "\\bigskipamount") {
2577 known_vspace = true;
2578 } else if (unit == "\\fill") {
2580 known_vspace = true;
2583 if (!known_vspace) {
2584 switch (unitFromString(unit)) {
2605 if (known_unit || known_vspace) {
2606 // Literal length or known variable
2607 context.check_layout(os);
2608 begin_inset(os, "VSpace ");
2616 // LyX can't handle other length variables in Inset VSpace
2617 string name = t.asInput();
2622 handle_ert(os, name + '{' + unit + '}', context);
2623 else if (value == -1.0)
2624 handle_ert(os, name + "{-" + unit + '}', context);
2626 handle_ert(os, name + '{' + valstring + unit + '}', context);
2628 handle_ert(os, name + '{' + length + '}', context);
2633 // try to see whether the string is in unicodesymbols
2635 docstring s = encodings.fromLaTeXCommand(from_utf8(t.asInput()), rem);
2638 cerr << "When parsing " << t.cs()
2639 << ", result is " << to_utf8(s)
2640 << "+" << to_utf8(rem) << endl;
2641 context.check_layout(os);
2644 skip_braces(p); // eat {}
2646 //cerr << "#: " << t << " mode: " << mode << endl;
2647 // heuristic: read up to next non-nested space
2649 string s = t.asInput();
2650 string z = p.verbatim_item();
2651 while (p.good() && z != " " && z.size()) {
2652 //cerr << "read: " << z << endl;
2654 z = p.verbatim_item();
2656 cerr << "found ERT: " << s << endl;
2657 handle_ert(os, s + ' ', context);
2660 string name = t.asInput();
2661 if (p.next_token().asInput() == "*") {
2662 // Starred commands like \vspace*{}
2663 p.get_token(); // Eat '*'
2666 if (!parse_command(name, p, os, outer, context))
2667 handle_ert(os, name, context);
2671 if (flags & FLAG_LEAVE) {
2672 flags &= ~FLAG_LEAVE;