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 if (context.layout->optionalargs > 0) {
458 eat_whitespace(p, os, context, false);
459 if (p.next_token().character() == '[') {
460 p.get_token(); // eat '['
461 begin_inset(os, "OptArg\n");
462 os << "status collapsed\n\n";
463 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
465 eat_whitespace(p, os, context, false);
468 parse_text(p, os, FLAG_ITEM, outer, context);
469 context.check_end_layout(os);
470 if (parent_context.deeper_paragraph) {
471 // We must suppress the "end deeper" because we
472 // suppressed the "begin deeper" above.
473 context.need_end_deeper = false;
475 context.check_end_deeper(os);
476 // We don't need really a new paragraph, but
477 // we must make sure that the next item gets a \begin_layout.
478 parent_context.new_paragraph(os);
483 * Output a space if necessary.
484 * This function gets called for every whitespace token.
486 * We have three cases here:
487 * 1. A space must be suppressed. Example: The lyxcode case below
488 * 2. A space may be suppressed. Example: Spaces before "\par"
489 * 3. A space must not be suppressed. Example: A space between two words
491 * We currently handle only 1. and 3 and from 2. only the case of
492 * spaces before newlines as a side effect.
494 * 2. could be used to suppress as many spaces as possible. This has two effects:
495 * - Reimporting LyX generated LaTeX files changes almost no whitespace
496 * - Superflous whitespace from non LyX generated LaTeX files is removed.
497 * The drawback is that the logic inside the function becomes
498 * complicated, and that is the reason why it is not implemented.
500 void check_space(Parser & p, ostream & os, Context & context)
502 Token const next = p.next_token();
503 Token const curr = p.curr_token();
504 // A space before a single newline and vice versa must be ignored
505 // LyX emits a newline before \end{lyxcode}.
506 // This newline must be ignored,
507 // otherwise LyX will add an additional protected space.
508 if (next.cat() == catSpace ||
509 next.cat() == catNewline ||
510 (next.cs() == "end" && context.layout->free_spacing && curr.cat() == catNewline)) {
513 context.check_layout(os);
519 * Parse all arguments of \p command
521 void parse_arguments(string const & command,
522 vector<ArgumentType> const & template_arguments,
523 Parser & p, ostream & os, bool outer, Context & context)
525 string ert = command;
526 size_t no_arguments = template_arguments.size();
527 for (size_t i = 0; i < no_arguments; ++i) {
528 switch (template_arguments[i]) {
530 // This argument contains regular LaTeX
531 handle_ert(os, ert + '{', context);
532 eat_whitespace(p, os, context, false);
533 parse_text(p, os, FLAG_ITEM, outer, context);
537 // This argument may contain special characters
538 ert += '{' + p.verbatim_item() + '}';
545 handle_ert(os, ert, context);
550 * Check whether \p command is a known command. If yes,
551 * handle the command with all arguments.
552 * \return true if the command was parsed, false otherwise.
554 bool parse_command(string const & command, Parser & p, ostream & os,
555 bool outer, Context & context)
557 if (known_commands.find(command) != known_commands.end()) {
558 parse_arguments(command, known_commands[command], p, os,
566 /// Parses a minipage or parbox
567 void parse_box(Parser & p, ostream & os, unsigned flags, bool outer,
568 Context & parent_context, bool use_parbox)
572 // We need to set the height to the LaTeX default of 1\\totalheight
573 // for the case when no height argument is given
574 string height_value = "1";
575 string height_unit = "in";
576 string height_special = "totalheight";
578 if (p.next_token().asInput() == "[") {
579 position = p.getArg('[', ']');
580 if (position != "t" && position != "c" && position != "b") {
582 cerr << "invalid position for minipage/parbox" << endl;
584 if (p.next_token().asInput() == "[") {
585 latex_height = p.getArg('[', ']');
586 translate_box_len(latex_height, height_value, height_unit, height_special);
588 if (p.next_token().asInput() == "[") {
589 inner_pos = p.getArg('[', ']');
590 if (inner_pos != "c" && inner_pos != "t" &&
591 inner_pos != "b" && inner_pos != "s") {
592 inner_pos = position;
593 cerr << "invalid inner_pos for minipage/parbox"
601 string const latex_width = p.verbatim_item();
602 translate_len(latex_width, width_value, width_unit);
603 if (contains(width_unit, '\\') || contains(height_unit, '\\')) {
604 // LyX can't handle length variables
609 ss << "\\begin{minipage}";
610 if (!position.empty())
611 ss << '[' << position << ']';
612 if (!latex_height.empty())
613 ss << '[' << latex_height << ']';
614 if (!inner_pos.empty())
615 ss << '[' << inner_pos << ']';
616 ss << "{" << latex_width << "}";
619 handle_ert(os, ss.str(), parent_context);
620 parent_context.new_paragraph(os);
621 parse_text_in_inset(p, os, flags, outer, parent_context);
623 handle_ert(os, "}", parent_context);
625 handle_ert(os, "\\end{minipage}", parent_context);
627 // LyX does not like empty positions, so we have
628 // to set them to the LaTeX default values here.
629 if (position.empty())
631 if (inner_pos.empty())
632 inner_pos = position;
633 parent_context.check_layout(os);
634 begin_inset(os, "Box Frameless\n");
635 os << "position \"" << position << "\"\n";
636 os << "hor_pos \"c\"\n";
637 os << "has_inner_box 1\n";
638 os << "inner_pos \"" << inner_pos << "\"\n";
639 os << "use_parbox " << use_parbox << "\n";
640 os << "width \"" << width_value << width_unit << "\"\n";
641 os << "special \"none\"\n";
642 os << "height \"" << height_value << height_unit << "\"\n";
643 os << "height_special \"" << height_special << "\"\n";
644 os << "status open\n\n";
645 parse_text_in_inset(p, os, flags, outer, parent_context);
647 #ifdef PRESERVE_LAYOUT
648 // LyX puts a % after the end of the minipage
649 if (p.next_token().cat() == catNewline && p.next_token().cs().size() > 1) {
651 //handle_comment(os, "%dummy", parent_context);
654 parent_context.new_paragraph(os);
656 else if (p.next_token().cat() == catSpace || p.next_token().cat() == catNewline) {
657 //handle_comment(os, "%dummy", parent_context);
660 // We add a protected space if something real follows
661 if (p.good() && p.next_token().cat() != catComment) {
662 os << "\\InsetSpace ~\n";
670 /// parse an unknown environment
671 void parse_unknown_environment(Parser & p, string const & name, ostream & os,
672 unsigned flags, bool outer,
673 Context & parent_context)
675 if (name == "tabbing")
676 // We need to remember that we have to handle '\=' specially
677 flags |= FLAG_TABBING;
679 // We need to translate font changes and paragraphs inside the
680 // environment to ERT if we have a non standard font.
681 // Otherwise things like
682 // \large\begin{foo}\huge bar\end{foo}
684 bool const specialfont =
685 (parent_context.font != parent_context.normalfont);
686 bool const new_layout_allowed = parent_context.new_layout_allowed;
688 parent_context.new_layout_allowed = false;
689 handle_ert(os, "\\begin{" + name + "}", parent_context);
690 parse_text_snippet(p, os, flags, outer, parent_context);
691 handle_ert(os, "\\end{" + name + "}", parent_context);
693 parent_context.new_layout_allowed = new_layout_allowed;
697 void parse_environment(Parser & p, ostream & os, bool outer,
698 Context & parent_context)
700 Layout const * newlayout;
701 string const name = p.getArg('{', '}');
702 const bool is_starred = suffixIs(name, '*');
703 string const unstarred_name = rtrim(name, "*");
704 active_environments.push_back(name);
706 if (is_math_env(name)) {
707 parent_context.check_layout(os);
708 begin_inset(os, "Formula ");
709 os << "\\begin{" << name << "}";
710 parse_math(p, os, FLAG_END, MATH_MODE);
711 os << "\\end{" << name << "}";
715 else if (name == "tabular" || name == "longtable") {
716 eat_whitespace(p, os, parent_context, false);
717 parent_context.check_layout(os);
718 begin_inset(os, "Tabular ");
719 handle_tabular(p, os, name == "longtable", parent_context);
724 else if (parent_context.textclass.floats().typeExist(unstarred_name)) {
725 eat_whitespace(p, os, parent_context, false);
726 parent_context.check_layout(os);
727 begin_inset(os, "Float " + unstarred_name + "\n");
728 if (p.next_token().asInput() == "[") {
729 os << "placement " << p.getArg('[', ']') << '\n';
731 os << "wide " << convert<string>(is_starred)
732 << "\nsideways false"
733 << "\nstatus open\n\n";
734 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
736 // We don't need really a new paragraph, but
737 // we must make sure that the next item gets a \begin_layout.
738 parent_context.new_paragraph(os);
742 else if (name == "minipage") {
743 eat_whitespace(p, os, parent_context, false);
744 parse_box(p, os, FLAG_END, outer, parent_context, false);
748 else if (name == "comment") {
749 eat_whitespace(p, os, parent_context, false);
750 parent_context.check_layout(os);
751 begin_inset(os, "Note Comment\n");
752 os << "status open\n";
753 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
756 skip_braces(p); // eat {} that might by set by LyX behind comments
759 else if (name == "lyxgreyedout") {
760 eat_whitespace(p, os, parent_context, false);
761 parent_context.check_layout(os);
762 begin_inset(os, "Note Greyedout\n");
763 os << "status open\n";
764 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
769 else if (name == "framed") {
770 eat_whitespace(p, os, parent_context, false);
771 parent_context.check_layout(os);
772 begin_inset(os, "Note Framed\n");
773 os << "status open\n";
774 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
779 else if (name == "shaded") {
780 eat_whitespace(p, os, parent_context, false);
781 parent_context.check_layout(os);
782 begin_inset(os, "Note Shaded\n");
783 os << "status open\n";
784 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
789 else if (!parent_context.new_layout_allowed)
790 parse_unknown_environment(p, name, os, FLAG_END, outer,
793 // Alignment and spacing settings
794 // FIXME (bug xxxx): These settings can span multiple paragraphs and
795 // therefore are totally broken!
796 // Note that \centering, raggedright, and raggedleft cannot be handled, as
797 // they are commands not environments. They are furthermore switches that
798 // can be ended by another switches, but also by commands like \footnote or
799 // \parbox. So the only safe way is to leave them untouched.
800 else if (name == "center" || name == "centering" ||
801 name == "flushleft" || name == "flushright" ||
802 name == "singlespace" || name == "onehalfspace" ||
803 name == "doublespace" || name == "spacing") {
804 eat_whitespace(p, os, parent_context, false);
805 // We must begin a new paragraph if not already done
806 if (! parent_context.atParagraphStart()) {
807 parent_context.check_end_layout(os);
808 parent_context.new_paragraph(os);
810 if (name == "flushleft")
811 parent_context.add_extra_stuff("\\align left\n");
812 else if (name == "flushright")
813 parent_context.add_extra_stuff("\\align right\n");
814 else if (name == "center" || name == "centering")
815 parent_context.add_extra_stuff("\\align center\n");
816 else if (name == "singlespace")
817 parent_context.add_extra_stuff("\\paragraph_spacing single\n");
818 else if (name == "onehalfspace")
819 parent_context.add_extra_stuff("\\paragraph_spacing onehalf\n");
820 else if (name == "doublespace")
821 parent_context.add_extra_stuff("\\paragraph_spacing double\n");
822 else if (name == "spacing")
823 parent_context.add_extra_stuff("\\paragraph_spacing other " + p.verbatim_item() + "\n");
824 parse_text(p, os, FLAG_END, outer, parent_context);
825 // Just in case the environment is empty
826 parent_context.extra_stuff.erase();
827 // We must begin a new paragraph to reset the alignment
828 parent_context.new_paragraph(os);
832 // The single '=' is meant here.
833 else if ((newlayout = findLayout(parent_context.textclass, name)) &&
834 newlayout->isEnvironment()) {
835 eat_whitespace(p, os, parent_context, false);
836 Context context(true, parent_context.textclass, newlayout,
837 parent_context.layout, parent_context.font);
838 if (parent_context.deeper_paragraph) {
839 // We are beginning a nested environment after a
840 // deeper paragraph inside the outer list environment.
841 // Therefore we don't need to output a "begin deeper".
842 context.need_end_deeper = true;
844 parent_context.check_end_layout(os);
845 switch (context.layout->latextype) {
846 case LATEX_LIST_ENVIRONMENT:
847 context.add_par_extra_stuff("\\labelwidthstring "
848 + p.verbatim_item() + '\n');
851 case LATEX_BIB_ENVIRONMENT:
852 p.verbatim_item(); // swallow next arg
858 context.check_deeper(os);
859 parse_text(p, os, FLAG_END, outer, context);
860 context.check_end_layout(os);
861 if (parent_context.deeper_paragraph) {
862 // We must suppress the "end deeper" because we
863 // suppressed the "begin deeper" above.
864 context.need_end_deeper = false;
866 context.check_end_deeper(os);
867 parent_context.new_paragraph(os);
871 else if (name == "appendix") {
872 // This is no good latex style, but it works and is used in some documents...
873 eat_whitespace(p, os, parent_context, false);
874 parent_context.check_end_layout(os);
875 Context context(true, parent_context.textclass, parent_context.layout,
876 parent_context.layout, parent_context.font);
877 context.check_layout(os);
878 os << "\\start_of_appendix\n";
879 parse_text(p, os, FLAG_END, outer, context);
880 context.check_end_layout(os);
884 else if (known_environments.find(name) != known_environments.end()) {
885 vector<ArgumentType> arguments = known_environments[name];
886 // The last "argument" denotes wether we may translate the
887 // environment contents to LyX
888 // The default required if no argument is given makes us
889 // compatible with the reLyXre environment.
890 ArgumentType contents = arguments.empty() ?
893 if (!arguments.empty())
894 arguments.pop_back();
895 // See comment in parse_unknown_environment()
896 bool const specialfont =
897 (parent_context.font != parent_context.normalfont);
898 bool const new_layout_allowed =
899 parent_context.new_layout_allowed;
901 parent_context.new_layout_allowed = false;
902 parse_arguments("\\begin{" + name + "}", arguments, p, os,
903 outer, parent_context);
904 if (contents == verbatim)
905 handle_ert(os, p.verbatimEnvironment(name),
908 parse_text_snippet(p, os, FLAG_END, outer,
910 handle_ert(os, "\\end{" + name + "}", parent_context);
912 parent_context.new_layout_allowed = new_layout_allowed;
916 parse_unknown_environment(p, name, os, FLAG_END, outer,
919 active_environments.pop_back();
923 /// parses a comment and outputs it to \p os.
924 void parse_comment(Parser & p, ostream & os, Token const & t, Context & context)
926 LASSERT(t.cat() == catComment, return);
927 if (!t.cs().empty()) {
928 context.check_layout(os);
929 handle_comment(os, '%' + t.cs(), context);
930 if (p.next_token().cat() == catNewline) {
931 // A newline after a comment line starts a new
933 if (context.new_layout_allowed) {
934 if(!context.atParagraphStart())
935 // Only start a new paragraph if not already
936 // done (we might get called recursively)
937 context.new_paragraph(os);
939 handle_ert(os, "\n", context);
940 eat_whitespace(p, os, context, true);
950 * Reads spaces and comments until the first non-space, non-comment token.
951 * New paragraphs (double newlines or \\par) are handled like simple spaces
952 * if \p eatParagraph is true.
953 * Spaces are skipped, but comments are written to \p os.
955 void eat_whitespace(Parser & p, ostream & os, Context & context,
959 Token const & t = p.get_token();
960 if (t.cat() == catComment)
961 parse_comment(p, os, t, context);
962 else if ((! eatParagraph && p.isParagraph()) ||
963 (t.cat() != catSpace && t.cat() != catNewline)) {
972 * Set a font attribute, parse text and reset the font attribute.
973 * \param attribute Attribute name (e.g. \\family, \\shape etc.)
974 * \param currentvalue Current value of the attribute. Is set to the new
975 * value during parsing.
976 * \param newvalue New value of the attribute
978 void parse_text_attributes(Parser & p, ostream & os, unsigned flags, bool outer,
979 Context & context, string const & attribute,
980 string & currentvalue, string const & newvalue)
982 context.check_layout(os);
983 string const oldvalue = currentvalue;
984 currentvalue = newvalue;
985 os << '\n' << attribute << ' ' << newvalue << "\n";
986 parse_text_snippet(p, os, flags, outer, context);
987 context.check_layout(os);
988 os << '\n' << attribute << ' ' << oldvalue << "\n";
989 currentvalue = oldvalue;
993 /// get the arguments of a natbib or jurabib citation command
994 void get_cite_arguments(Parser & p, bool natbibOrder,
995 string & before, string & after)
997 // We need to distinguish "" and "[]", so we can't use p.getOpt().
999 // text before the citation
1001 // text after the citation
1002 after = p.getFullOpt();
1004 if (!after.empty()) {
1005 before = p.getFullOpt();
1006 if (natbibOrder && !before.empty())
1007 swap(before, after);
1012 /// Convert filenames with TeX macros and/or quotes to something LyX
1014 string const normalize_filename(string const & name)
1016 Parser p(trim(name, "\""));
1019 Token const & t = p.get_token();
1020 if (t.cat() != catEscape)
1022 else if (t.cs() == "lyxdot") {
1023 // This is used by LyX for simple dots in relative
1027 } else if (t.cs() == "space") {
1037 /// Convert \p name from TeX convention (relative to master file) to LyX
1038 /// convention (relative to .lyx file) if it is relative
1039 void fix_relative_filename(string & name)
1041 if (FileName::isAbsolute(name))
1044 name = to_utf8(makeRelPath(from_utf8(makeAbsPath(name, getMasterFilePath()).absFilename()),
1045 from_utf8(getParentFilePath())));
1049 /// Parse a NoWeb Scrap section. The initial "<<" is already parsed.
1050 void parse_noweb(Parser & p, ostream & os, Context & context)
1052 // assemble the rest of the keyword
1056 Token const & t = p.get_token();
1057 if (t.asInput() == ">" && p.next_token().asInput() == ">") {
1060 scrap = (p.good() && p.next_token().asInput() == "=");
1062 name += p.get_token().asInput();
1065 name += t.asInput();
1068 if (!scrap || !context.new_layout_allowed ||
1069 !context.textclass.hasLayout(from_ascii("Scrap"))) {
1070 cerr << "Warning: Could not interpret '" << name
1071 << "'. Ignoring it." << endl;
1075 // We use new_paragraph instead of check_end_layout because the stuff
1076 // following the noweb chunk needs to start with a \begin_layout.
1077 // This may create a new paragraph even if there was none in the
1078 // noweb file, but the alternative is an invalid LyX file. Since
1079 // noweb code chunks are implemented with a layout style in LyX they
1080 // always must be in an own paragraph.
1081 context.new_paragraph(os);
1082 Context newcontext(true, context.textclass,
1083 &context.textclass[from_ascii("Scrap")]);
1084 newcontext.check_layout(os);
1087 Token const & t = p.get_token();
1088 // We abuse the parser a bit, because this is no TeX syntax
1090 if (t.cat() == catEscape)
1091 os << subst(t.asInput(), "\\", "\n\\backslash\n");
1093 os << subst(t.asInput(), "\n", "\n\\newline\n");
1094 // The scrap chunk is ended by an @ at the beginning of a line.
1095 // After the @ the line may contain a comment and/or
1096 // whitespace, but nothing else.
1097 if (t.asInput() == "@" && p.prev_token().cat() == catNewline &&
1098 (p.next_token().cat() == catSpace ||
1099 p.next_token().cat() == catNewline ||
1100 p.next_token().cat() == catComment)) {
1101 while (p.good() && p.next_token().cat() == catSpace)
1102 os << p.get_token().asInput();
1103 if (p.next_token().cat() == catComment)
1104 // The comment includes a final '\n'
1105 os << p.get_token().asInput();
1107 if (p.next_token().cat() == catNewline)
1114 newcontext.check_end_layout(os);
1117 } // anonymous namespace
1120 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
1123 Layout const * newlayout = 0;
1124 // store the current selectlanguage to be used after \foreignlanguage
1126 // Store the latest bibliographystyle (needed for bibtex inset)
1127 string bibliographystyle;
1128 bool const use_natbib = used_packages.find("natbib") != used_packages.end();
1129 bool const use_jurabib = used_packages.find("jurabib") != used_packages.end();
1131 Token const & t = p.get_token();
1134 cerr << "t: " << t << " flags: " << flags << "\n";
1137 if (flags & FLAG_ITEM) {
1138 if (t.cat() == catSpace)
1141 flags &= ~FLAG_ITEM;
1142 if (t.cat() == catBegin) {
1143 // skip the brace and collect everything to the next matching
1145 flags |= FLAG_BRACE_LAST;
1149 // handle only this single token, leave the loop if done
1150 flags |= FLAG_LEAVE;
1153 if (t.character() == ']' && (flags & FLAG_BRACK_LAST))
1159 if (t.cat() == catMath) {
1160 // we are inside some text mode thingy, so opening new math is allowed
1161 context.check_layout(os);
1162 begin_inset(os, "Formula ");
1163 Token const & n = p.get_token();
1164 if (n.cat() == catMath && outer) {
1165 // TeX's $$...$$ syntax for displayed math
1167 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
1169 p.get_token(); // skip the second '$' token
1171 // simple $...$ stuff
1174 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
1180 else if (t.cat() == catSuper || t.cat() == catSub)
1181 cerr << "catcode " << t << " illegal in text mode\n";
1183 // Basic support for english quotes. This should be
1184 // extended to other quotes, but is not so easy (a
1185 // left english quote is the same as a right german
1187 else if (t.asInput() == "`" && p.next_token().asInput() == "`") {
1188 context.check_layout(os);
1189 begin_inset(os, "Quotes ");
1195 else if (t.asInput() == "'" && p.next_token().asInput() == "'") {
1196 context.check_layout(os);
1197 begin_inset(os, "Quotes ");
1204 else if (t.asInput() == ">" && p.next_token().asInput() == ">") {
1205 context.check_layout(os);
1206 begin_inset(os, "Quotes ");
1213 else if (t.asInput() == "<" && p.next_token().asInput() == "<") {
1214 context.check_layout(os);
1215 begin_inset(os, "Quotes ");
1222 else if (t.asInput() == "<"
1223 && p.next_token().asInput() == "<" && noweb_mode) {
1225 parse_noweb(p, os, context);
1228 else if (t.cat() == catSpace || (t.cat() == catNewline && ! p.isParagraph()))
1229 check_space(p, os, context);
1231 else if (t.character() == '[' && noweb_mode &&
1232 p.next_token().character() == '[') {
1233 // These can contain underscores
1235 string const s = p.getFullOpt() + ']';
1236 if (p.next_token().character() == ']')
1239 cerr << "Warning: Inserting missing ']' in '"
1240 << s << "'." << endl;
1241 handle_ert(os, s, context);
1244 else if (t.cat() == catLetter ||
1245 t.cat() == catOther ||
1246 t.cat() == catAlign ||
1247 t.cat() == catParameter) {
1248 // This translates "&" to "\\&" which may be wrong...
1249 context.check_layout(os);
1253 else if (p.isParagraph()) {
1254 if (context.new_layout_allowed)
1255 context.new_paragraph(os);
1257 handle_ert(os, "\\par ", context);
1258 eat_whitespace(p, os, context, true);
1261 else if (t.cat() == catActive) {
1262 context.check_layout(os);
1263 if (t.character() == '~') {
1264 if (context.layout->free_spacing)
1267 os << "\\InsetSpace ~\n";
1272 else if (t.cat() == catBegin &&
1273 p.next_token().cat() == catEnd) {
1275 Token const prev = p.prev_token();
1277 if (p.next_token().character() == '`' ||
1278 (prev.character() == '-' &&
1279 p.next_token().character() == '-'))
1280 ; // ignore it in {}`` or -{}-
1282 handle_ert(os, "{}", context);
1286 else if (t.cat() == catBegin) {
1287 context.check_layout(os);
1288 // special handling of font attribute changes
1289 Token const prev = p.prev_token();
1290 Token const next = p.next_token();
1291 TeXFont const oldFont = context.font;
1292 if (next.character() == '[' ||
1293 next.character() == ']' ||
1294 next.character() == '*') {
1296 if (p.next_token().cat() == catEnd) {
1301 handle_ert(os, "{", context);
1302 parse_text_snippet(p, os,
1305 handle_ert(os, "}", context);
1307 } else if (! context.new_layout_allowed) {
1308 handle_ert(os, "{", context);
1309 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1311 handle_ert(os, "}", context);
1312 } else if (is_known(next.cs(), known_sizes)) {
1313 // next will change the size, so we must
1315 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1317 if (!context.atParagraphStart())
1319 << context.font.size << "\n";
1320 } else if (is_known(next.cs(), known_font_families)) {
1321 // next will change the font family, so we
1322 // must reset it here
1323 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1325 if (!context.atParagraphStart())
1327 << context.font.family << "\n";
1328 } else if (is_known(next.cs(), known_font_series)) {
1329 // next will change the font series, so we
1330 // must reset it here
1331 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1333 if (!context.atParagraphStart())
1335 << context.font.series << "\n";
1336 } else if (is_known(next.cs(), known_font_shapes)) {
1337 // next will change the font shape, so we
1338 // must reset it here
1339 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1341 if (!context.atParagraphStart())
1343 << context.font.shape << "\n";
1344 } else if (is_known(next.cs(), known_old_font_families) ||
1345 is_known(next.cs(), known_old_font_series) ||
1346 is_known(next.cs(), known_old_font_shapes)) {
1347 // next will change the font family, series
1348 // and shape, so we must reset it here
1349 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1351 if (!context.atParagraphStart())
1353 << context.font.family
1355 << context.font.series
1357 << context.font.shape << "\n";
1359 handle_ert(os, "{", context);
1360 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1362 handle_ert(os, "}", context);
1366 else if (t.cat() == catEnd) {
1367 if (flags & FLAG_BRACE_LAST) {
1370 cerr << "stray '}' in text\n";
1371 handle_ert(os, "}", context);
1374 else if (t.cat() == catComment)
1375 parse_comment(p, os, t, context);
1378 // control sequences
1381 else if (t.cs() == "(") {
1382 context.check_layout(os);
1383 begin_inset(os, "Formula");
1385 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
1390 else if (t.cs() == "[") {
1391 context.check_layout(os);
1392 begin_inset(os, "Formula");
1394 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
1399 else if (t.cs() == "begin")
1400 parse_environment(p, os, outer, context);
1402 else if (t.cs() == "end") {
1403 if (flags & FLAG_END) {
1404 // eat environment name
1405 string const name = p.getArg('{', '}');
1406 if (name != active_environment())
1407 cerr << "\\end{" + name + "} does not match \\begin{"
1408 + active_environment() + "}\n";
1411 p.error("found 'end' unexpectedly");
1414 else if (t.cs() == "item") {
1417 bool optarg = false;
1418 if (p.next_token().character() == '[') {
1419 p.get_token(); // eat '['
1420 s = parse_text_snippet(p, FLAG_BRACK_LAST,
1425 context.check_layout(os);
1426 if (context.has_item) {
1427 // An item in an unknown list-like environment
1428 // FIXME: Do this in check_layout()!
1429 context.has_item = false;
1431 handle_ert(os, "\\item", context);
1433 handle_ert(os, "\\item ", context);
1436 if (context.layout->labeltype != LABEL_MANUAL) {
1437 // LyX does not support \item[\mybullet]
1438 // in itemize environments
1439 handle_ert(os, "[", context);
1441 handle_ert(os, "]", context);
1442 } else if (!s.empty()) {
1443 // The space is needed to separate the
1444 // item from the rest of the sentence.
1446 eat_whitespace(p, os, context, false);
1451 else if (t.cs() == "bibitem") {
1453 context.check_layout(os);
1454 begin_inset(os, "LatexCommand ");
1455 os << t.cs() << "\n";
1457 os << "key " << '"' << p.verbatim_item() << '"' << "\n";
1461 else if (t.cs() == "def") {
1462 context.check_layout(os);
1463 eat_whitespace(p, os, context, false);
1464 string name = p.get_token().cs();
1465 eat_whitespace(p, os, context, false);
1471 while (p.next_token().cat() != catBegin) {
1472 if (p.next_token().cat() == catParameter) {
1477 // followed by number?
1478 if (p.next_token().cat() == catOther) {
1479 char c = p.getChar();
1481 // number = current arity + 1?
1482 if (c == arity + '0' + 1)
1487 paramtext += p.get_token().asString();
1489 paramtext += p.get_token().asString();
1494 // only output simple (i.e. compatible) macro as FormulaMacros
1495 string ert = "\\def\\" + name + ' ' + paramtext + '{' + p.verbatim_item() + '}';
1497 context.check_layout(os);
1498 begin_inset(os, "FormulaMacro");
1502 handle_ert(os, ert, context);
1505 else if (t.cs() == "noindent") {
1507 context.add_par_extra_stuff("\\noindent\n");
1510 else if (t.cs() == "appendix") {
1511 context.add_par_extra_stuff("\\start_of_appendix\n");
1512 // We need to start a new paragraph. Otherwise the
1513 // appendix in 'bla\appendix\chapter{' would start
1515 context.new_paragraph(os);
1516 // We need to make sure that the paragraph is
1517 // generated even if it is empty. Otherwise the
1518 // appendix in '\par\appendix\par\chapter{' would
1520 context.check_layout(os);
1521 // FIXME: This is a hack to prevent paragraph
1522 // deletion if it is empty. Handle this better!
1524 "%dummy comment inserted by tex2lyx to "
1525 "ensure that this paragraph is not empty",
1527 // Both measures above may generate an additional
1528 // empty paragraph, but that does not hurt, because
1529 // whitespace does not matter here.
1530 eat_whitespace(p, os, context, true);
1533 // Starred section headings
1534 // Must attempt to parse "Section*" before "Section".
1535 else if ((p.next_token().asInput() == "*") &&
1536 context.new_layout_allowed &&
1537 (newlayout = findLayout(context.textclass, t.cs() + '*')) &&
1538 newlayout->isCommand()) {
1539 TeXFont const oldFont = context.font;
1540 // save the current font size
1541 string const size = oldFont.size;
1542 // reset the font size to default, because the
1543 // font size switches don't affect section
1544 // headings and the like
1545 context.font.size = known_coded_sizes[0];
1546 output_font_change(os, oldFont, context.font);
1549 output_command_layout(os, p, outer, context, newlayout);
1550 // set the font size to the original value
1551 context.font.size = size;
1552 output_font_change(os, oldFont, context.font);
1556 // Section headings and the like
1557 else if (context.new_layout_allowed &&
1558 (newlayout = findLayout(context.textclass, t.cs())) &&
1559 newlayout->isCommand()) {
1560 TeXFont const oldFont = context.font;
1561 // save the current font size
1562 string const size = oldFont.size;
1563 // reset the font size to default, because the font size switches don't
1564 // affect section headings and the like
1565 context.font.size = known_coded_sizes[0];
1566 output_font_change(os, oldFont, context.font);
1568 output_command_layout(os, p, outer, context, newlayout);
1569 // set the font size to the original value
1570 context.font.size = size;
1571 output_font_change(os, oldFont, context.font);
1575 else if (t.cs() == "caption") {
1577 context.check_layout(os);
1579 begin_inset(os, "Caption\n\n");
1580 os << "\\begin_layout Standard";
1581 if (p.next_token().character() == '[') {
1582 p.get_token(); // eat '['
1583 begin_inset(os, "OptArg\n");
1584 os << "status collapsed\n";
1585 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
1587 eat_whitespace(p, os, context, false);
1589 parse_text(p, os, FLAG_ITEM, outer, context);
1590 context.check_end_layout(os);
1591 // We don't need really a new paragraph, but
1592 // we must make sure that the next item gets a \begin_layout.
1593 context.new_paragraph(os);
1596 os << "\\end_layout\n";
1599 else if (t.cs() == "includegraphics") {
1600 bool const clip = p.next_token().asInput() == "*";
1603 map<string, string> opts = split_map(p.getArg('[', ']'));
1605 opts["clip"] = string();
1606 string name = normalize_filename(p.verbatim_item());
1608 string const path = getMasterFilePath();
1609 // We want to preserve relative / absolute filenames,
1610 // therefore path is only used for testing
1611 if (!makeAbsPath(name, path).exists()) {
1612 // The file extension is probably missing.
1613 // Now try to find it out.
1614 string const dvips_name =
1615 find_file(name, path,
1616 known_dvips_graphics_formats);
1617 string const pdftex_name =
1618 find_file(name, path,
1619 known_pdftex_graphics_formats);
1620 if (!dvips_name.empty()) {
1621 if (!pdftex_name.empty()) {
1622 cerr << "This file contains the "
1624 "\"\\includegraphics{"
1626 "However, files\n\""
1627 << dvips_name << "\" and\n\""
1628 << pdftex_name << "\"\n"
1629 "both exist, so I had to make a "
1630 "choice and took the first one.\n"
1631 "Please move the unwanted one "
1632 "someplace else and try again\n"
1633 "if my choice was wrong."
1637 } else if (!pdftex_name.empty())
1641 if (makeAbsPath(name, path).exists())
1642 fix_relative_filename(name);
1644 cerr << "Warning: Could not find graphics file '"
1645 << name << "'." << endl;
1647 context.check_layout(os);
1648 begin_inset(os, "Graphics ");
1649 os << "\n\tfilename " << name << '\n';
1650 if (opts.find("width") != opts.end())
1652 << translate_len(opts["width"]) << '\n';
1653 if (opts.find("height") != opts.end())
1655 << translate_len(opts["height"]) << '\n';
1656 if (opts.find("scale") != opts.end()) {
1657 istringstream iss(opts["scale"]);
1661 os << "\tscale " << val << '\n';
1663 if (opts.find("angle") != opts.end())
1664 os << "\trotateAngle "
1665 << opts["angle"] << '\n';
1666 if (opts.find("origin") != opts.end()) {
1668 string const opt = opts["origin"];
1669 if (opt.find('l') != string::npos) ss << "left";
1670 if (opt.find('r') != string::npos) ss << "right";
1671 if (opt.find('c') != string::npos) ss << "center";
1672 if (opt.find('t') != string::npos) ss << "Top";
1673 if (opt.find('b') != string::npos) ss << "Bottom";
1674 if (opt.find('B') != string::npos) ss << "Baseline";
1675 if (!ss.str().empty())
1676 os << "\trotateOrigin " << ss.str() << '\n';
1678 cerr << "Warning: Ignoring unknown includegraphics origin argument '" << opt << "'\n";
1680 if (opts.find("keepaspectratio") != opts.end())
1681 os << "\tkeepAspectRatio\n";
1682 if (opts.find("clip") != opts.end())
1684 if (opts.find("draft") != opts.end())
1686 if (opts.find("bb") != opts.end())
1687 os << "\tBoundingBox "
1688 << opts["bb"] << '\n';
1689 int numberOfbbOptions = 0;
1690 if (opts.find("bbllx") != opts.end())
1691 numberOfbbOptions++;
1692 if (opts.find("bblly") != opts.end())
1693 numberOfbbOptions++;
1694 if (opts.find("bburx") != opts.end())
1695 numberOfbbOptions++;
1696 if (opts.find("bbury") != opts.end())
1697 numberOfbbOptions++;
1698 if (numberOfbbOptions == 4)
1699 os << "\tBoundingBox "
1700 << opts["bbllx"] << " " << opts["bblly"] << " "
1701 << opts["bburx"] << " " << opts["bbury"] << '\n';
1702 else if (numberOfbbOptions > 0)
1703 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
1704 numberOfbbOptions = 0;
1705 if (opts.find("natwidth") != opts.end())
1706 numberOfbbOptions++;
1707 if (opts.find("natheight") != opts.end())
1708 numberOfbbOptions++;
1709 if (numberOfbbOptions == 2)
1710 os << "\tBoundingBox 0bp 0bp "
1711 << opts["natwidth"] << " " << opts["natheight"] << '\n';
1712 else if (numberOfbbOptions > 0)
1713 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
1714 ostringstream special;
1715 if (opts.find("hiresbb") != opts.end())
1716 special << "hiresbb,";
1717 if (opts.find("trim") != opts.end())
1719 if (opts.find("viewport") != opts.end())
1720 special << "viewport=" << opts["viewport"] << ',';
1721 if (opts.find("totalheight") != opts.end())
1722 special << "totalheight=" << opts["totalheight"] << ',';
1723 if (opts.find("type") != opts.end())
1724 special << "type=" << opts["type"] << ',';
1725 if (opts.find("ext") != opts.end())
1726 special << "ext=" << opts["ext"] << ',';
1727 if (opts.find("read") != opts.end())
1728 special << "read=" << opts["read"] << ',';
1729 if (opts.find("command") != opts.end())
1730 special << "command=" << opts["command"] << ',';
1731 string s_special = special.str();
1732 if (!s_special.empty()) {
1733 // We had special arguments. Remove the trailing ','.
1734 os << "\tspecial " << s_special.substr(0, s_special.size() - 1) << '\n';
1736 // TODO: Handle the unknown settings better.
1737 // Warn about invalid options.
1738 // Check whether some option was given twice.
1742 else if (t.cs() == "footnote" ||
1743 (t.cs() == "thanks" && context.layout->intitle)) {
1745 context.check_layout(os);
1746 begin_inset(os, "Foot\n");
1747 os << "status collapsed\n\n";
1748 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
1752 else if (t.cs() == "marginpar") {
1754 context.check_layout(os);
1755 begin_inset(os, "Marginal\n");
1756 os << "status collapsed\n\n";
1757 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
1761 else if (t.cs() == "ensuremath") {
1763 context.check_layout(os);
1764 string const s = p.verbatim_item();
1765 //FIXME: this never triggers in UTF8
1766 if (s == "\xb1" || s == "\xb3" || s == "\xb2" || s == "\xb5")
1769 handle_ert(os, "\\ensuremath{" + s + "}",
1773 else if (t.cs() == "hfill") {
1774 context.check_layout(os);
1775 os << "\n\\hfill\n";
1780 else if (t.cs() == "makeindex" || t.cs() == "maketitle") {
1781 // FIXME: Somehow prevent title layouts if
1782 // "maketitle" was not found
1784 skip_braces(p); // swallow this
1787 else if (t.cs() == "tableofcontents") {
1789 context.check_layout(os);
1790 begin_inset(os, "LatexCommand ");
1791 os << t.cs() << "\n";
1793 skip_braces(p); // swallow this
1796 else if (t.cs() == "listoffigures") {
1798 context.check_layout(os);
1799 begin_inset(os, "FloatList figure\n");
1801 skip_braces(p); // swallow this
1804 else if (t.cs() == "listoftables") {
1806 context.check_layout(os);
1807 begin_inset(os, "FloatList table\n");
1809 skip_braces(p); // swallow this
1812 else if (t.cs() == "listof") {
1813 p.skip_spaces(true);
1814 string const name = p.get_token().asString();
1815 if (context.textclass.floats().typeExist(name)) {
1816 context.check_layout(os);
1817 begin_inset(os, "FloatList ");
1820 p.get_token(); // swallow second arg
1822 handle_ert(os, "\\listof{" + name + "}", context);
1825 else if (t.cs() == "textrm")
1826 parse_text_attributes(p, os, FLAG_ITEM, outer,
1827 context, "\\family",
1828 context.font.family, "roman");
1830 else if (t.cs() == "textsf")
1831 parse_text_attributes(p, os, FLAG_ITEM, outer,
1832 context, "\\family",
1833 context.font.family, "sans");
1835 else if (t.cs() == "texttt")
1836 parse_text_attributes(p, os, FLAG_ITEM, outer,
1837 context, "\\family",
1838 context.font.family, "typewriter");
1840 else if (t.cs() == "textmd")
1841 parse_text_attributes(p, os, FLAG_ITEM, outer,
1842 context, "\\series",
1843 context.font.series, "medium");
1845 else if (t.cs() == "textbf")
1846 parse_text_attributes(p, os, FLAG_ITEM, outer,
1847 context, "\\series",
1848 context.font.series, "bold");
1850 else if (t.cs() == "textup")
1851 parse_text_attributes(p, os, FLAG_ITEM, outer,
1853 context.font.shape, "up");
1855 else if (t.cs() == "textit")
1856 parse_text_attributes(p, os, FLAG_ITEM, outer,
1858 context.font.shape, "italic");
1860 else if (t.cs() == "textsl")
1861 parse_text_attributes(p, os, FLAG_ITEM, outer,
1863 context.font.shape, "slanted");
1865 else if (t.cs() == "textsc")
1866 parse_text_attributes(p, os, FLAG_ITEM, outer,
1868 context.font.shape, "smallcaps");
1870 else if (t.cs() == "textnormal" || t.cs() == "normalfont") {
1871 context.check_layout(os);
1872 TeXFont oldFont = context.font;
1873 context.font.init();
1874 context.font.size = oldFont.size;
1875 os << "\n\\family " << context.font.family << "\n";
1876 os << "\n\\series " << context.font.series << "\n";
1877 os << "\n\\shape " << context.font.shape << "\n";
1878 if (t.cs() == "textnormal") {
1879 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1880 output_font_change(os, context.font, oldFont);
1881 context.font = oldFont;
1883 eat_whitespace(p, os, context, false);
1886 else if (t.cs() == "textcolor") {
1887 // scheme is \textcolor{color name}{text}
1888 string const color = p.verbatim_item();
1889 // we only support the predefined colors of the color package
1890 if (color == "black" || color == "blue" || color == "cyan"
1891 || color == "green" || color == "magenta" || color == "red"
1892 || color == "white" || color == "yellow") {
1893 context.check_layout(os);
1894 os << "\n\\color " << color << "\n";
1895 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1896 context.check_layout(os);
1897 os << "\n\\color inherit\n";
1899 // for custom defined colors
1900 handle_ert(os, t.asInput() + "{" + color + "}", context);
1903 else if (t.cs() == "underbar") {
1904 // Do NOT handle \underline.
1905 // \underbar cuts through y, g, q, p etc.,
1906 // \underline does not.
1907 context.check_layout(os);
1908 os << "\n\\bar under\n";
1909 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1910 context.check_layout(os);
1911 os << "\n\\bar default\n";
1914 else if (t.cs() == "emph" || t.cs() == "noun") {
1915 context.check_layout(os);
1916 os << "\n\\" << t.cs() << " on\n";
1917 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1918 context.check_layout(os);
1919 os << "\n\\" << t.cs() << " default\n";
1922 else if (t.cs() == "lyxline") {
1923 context.check_layout(os);
1927 else if (is_known(t.cs(), known_ref_commands)) {
1928 context.check_layout(os);
1929 begin_inset(os, "LatexCommand ");
1930 os << t.cs() << "\n";
1931 // LyX cannot handle newlines in a latex command
1932 // FIXME: Move the substitution into parser::getOpt()?
1933 os << subst(p.getOpt(), "\n", " ");
1934 os << "reference " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
1938 else if (use_natbib &&
1939 is_known(t.cs(), known_natbib_commands) &&
1940 ((t.cs() != "citefullauthor" &&
1941 t.cs() != "citeyear" &&
1942 t.cs() != "citeyearpar") ||
1943 p.next_token().asInput() != "*")) {
1944 context.check_layout(os);
1945 string command = t.cs();
1946 if (p.next_token().asInput() == "*") {
1950 if (command == "citefullauthor")
1951 // alternative name for "\\citeauthor*"
1952 command = "citeauthor*";
1954 // text before the citation
1956 // text after the citation
1958 get_cite_arguments(p, true, before, after);
1960 if (command == "cite") {
1961 // \cite without optional argument means
1962 // \citet, \cite with at least one optional
1963 // argument means \citep.
1964 if (before.empty() && after.empty())
1969 if (before.empty() && after == "[]")
1970 // avoid \citet[]{a}
1972 else if (before == "[]" && after == "[]") {
1973 // avoid \citet[][]{a}
1977 // remove the brackets around after and before
1978 if (!after.empty()) {
1980 after.erase(after.length() - 1, 1);
1982 if (!before.empty()) {
1984 before.erase(before.length() - 1, 1);
1986 begin_inset(os, "LatexCommand ");
1987 os << t.cs() << "\n";
1988 os << "after " << '"' << after << '"' << "\n";
1989 os << "before " << '"' << before << '"' << "\n";
1990 os << "key " << '"' << p.verbatim_item() << '"' << "\n";
1994 else if (use_jurabib &&
1995 is_known(t.cs(), known_jurabib_commands)) {
1996 context.check_layout(os);
1997 string const command = t.cs();
1998 char argumentOrder = '\0';
1999 vector<string> const & options = used_packages["jurabib"];
2000 if (find(options.begin(), options.end(),
2001 "natbiborder") != options.end())
2002 argumentOrder = 'n';
2003 else if (find(options.begin(), options.end(),
2004 "jurabiborder") != options.end())
2005 argumentOrder = 'j';
2007 // text before the citation
2009 // text after the citation
2011 get_cite_arguments(p, argumentOrder != 'j', before, after);
2013 string const citation = p.verbatim_item();
2014 if (!before.empty() && argumentOrder == '\0') {
2015 cerr << "Warning: Assuming argument order "
2016 "of jurabib version 0.6 for\n'"
2017 << command << before << after << '{'
2018 << citation << "}'.\n"
2019 "Add 'jurabiborder' to the jurabib "
2020 "package options if you used an\n"
2021 "earlier jurabib version." << endl;
2023 if (!after.empty()) {
2025 after.erase(after.length() - 1, 1);
2027 if (!before.empty()) {
2029 before.erase(before.length() - 1, 1);
2031 begin_inset(os, "LatexCommand ");
2032 os << t.cs() << "\n";
2033 os << "after " << '"' << after << '"' << "\n";
2034 os << "before " << '"' << before << '"' << "\n";
2035 os << "key " << '"' << citation << '"' << "\n";
2039 else if (t.cs() == "cite") {
2040 context.check_layout(os);
2041 // LyX cannot handle newlines in a latex command
2042 string after = subst(p.getOptContent(), "\n", " ");
2043 begin_inset(os, "LatexCommand ");
2044 os << t.cs() << "\n";
2045 os << "after " << '"' << after << '"' << "\n";
2046 os << "key " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
2050 else if (t.cs() == "index") {
2051 context.check_layout(os);
2052 begin_inset(os, "LatexCommand ");
2053 os << t.cs() << "\n";
2054 // LyX cannot handle newlines in a latex command
2055 os << "name " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
2059 else if (t.cs() == "nomenclature") {
2060 context.check_layout(os);
2061 begin_inset(os, "LatexCommand ");
2062 os << t.cs() << "\n";
2063 // LyX cannot handle newlines in a latex command
2064 string prefix = subst(p.getOptContent(), "\n", " ");
2065 if (!prefix.empty())
2066 os << "prefix " << '"' << prefix << '"' << "\n";
2067 os << "symbol " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
2068 os << "description " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
2072 else if (t.cs() == "label") {
2073 context.check_layout(os);
2074 begin_inset(os, "LatexCommand ");
2075 os << t.cs() << "\n";
2076 // LyX cannot handle newlines in a latex command
2077 os << "name " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
2081 else if (t.cs() == "printindex") {
2082 context.check_layout(os);
2083 begin_inset(os, "LatexCommand ");
2084 os << t.cs() << "\n";
2089 else if (t.cs() == "printnomenclature") {
2090 context.check_layout(os);
2091 begin_inset(os, "LatexCommand ");
2092 os << t.cs() << "\n";
2097 else if (t.cs() == "url") {
2098 context.check_layout(os);
2099 begin_inset(os, "LatexCommand ");
2100 os << t.cs() << "\n";
2101 // LyX cannot handle newlines in a latex command
2102 os << "target " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
2106 else if (is_known(t.cs(), known_quotes)) {
2107 char const * const * where = is_known(t.cs(), known_quotes);
2108 context.check_layout(os);
2109 begin_inset(os, "Quotes ");
2110 os << known_coded_quotes[where - known_quotes];
2112 // LyX adds {} after the quote, so we have to eat
2113 // spaces here if there are any before a possible
2115 eat_whitespace(p, os, context, false);
2119 else if (is_known(t.cs(), known_sizes) &&
2120 context.new_layout_allowed) {
2121 char const * const * where = is_known(t.cs(), known_sizes);
2122 context.check_layout(os);
2123 TeXFont const oldFont = context.font;
2124 // the font size index differs by 1, because the known_coded_sizes
2125 // has additionally a "default" entry
2126 context.font.size = known_coded_sizes[where - known_sizes + 1];
2127 output_font_change(os, oldFont, context.font);
2128 eat_whitespace(p, os, context, false);
2131 else if (is_known(t.cs(), known_font_families) &&
2132 context.new_layout_allowed) {
2133 char const * const * where =
2134 is_known(t.cs(), known_font_families);
2135 context.check_layout(os);
2136 TeXFont const oldFont = context.font;
2137 context.font.family =
2138 known_coded_font_families[where - known_font_families];
2139 output_font_change(os, oldFont, context.font);
2140 eat_whitespace(p, os, context, false);
2143 else if (is_known(t.cs(), known_font_series) &&
2144 context.new_layout_allowed) {
2145 char const * const * where =
2146 is_known(t.cs(), known_font_series);
2147 context.check_layout(os);
2148 TeXFont const oldFont = context.font;
2149 context.font.series =
2150 known_coded_font_series[where - known_font_series];
2151 output_font_change(os, oldFont, context.font);
2152 eat_whitespace(p, os, context, false);
2155 else if (is_known(t.cs(), known_font_shapes) &&
2156 context.new_layout_allowed) {
2157 char const * const * where =
2158 is_known(t.cs(), known_font_shapes);
2159 context.check_layout(os);
2160 TeXFont const oldFont = context.font;
2161 context.font.shape =
2162 known_coded_font_shapes[where - known_font_shapes];
2163 output_font_change(os, oldFont, context.font);
2164 eat_whitespace(p, os, context, false);
2166 else if (is_known(t.cs(), known_old_font_families) &&
2167 context.new_layout_allowed) {
2168 char const * const * where =
2169 is_known(t.cs(), known_old_font_families);
2170 context.check_layout(os);
2171 TeXFont const oldFont = context.font;
2172 context.font.init();
2173 context.font.size = oldFont.size;
2174 context.font.family =
2175 known_coded_font_families[where - known_old_font_families];
2176 output_font_change(os, oldFont, context.font);
2177 eat_whitespace(p, os, context, false);
2180 else if (is_known(t.cs(), known_old_font_series) &&
2181 context.new_layout_allowed) {
2182 char const * const * where =
2183 is_known(t.cs(), known_old_font_series);
2184 context.check_layout(os);
2185 TeXFont const oldFont = context.font;
2186 context.font.init();
2187 context.font.size = oldFont.size;
2188 context.font.series =
2189 known_coded_font_series[where - known_old_font_series];
2190 output_font_change(os, oldFont, context.font);
2191 eat_whitespace(p, os, context, false);
2194 else if (is_known(t.cs(), known_old_font_shapes) &&
2195 context.new_layout_allowed) {
2196 char const * const * where =
2197 is_known(t.cs(), known_old_font_shapes);
2198 context.check_layout(os);
2199 TeXFont const oldFont = context.font;
2200 context.font.init();
2201 context.font.size = oldFont.size;
2202 context.font.shape =
2203 known_coded_font_shapes[where - known_old_font_shapes];
2204 output_font_change(os, oldFont, context.font);
2205 eat_whitespace(p, os, context, false);
2208 else if (t.cs() == "selectlanguage") {
2209 context.check_layout(os);
2210 // save the language for the case that a
2211 // \foreignlanguage is used
2213 //FIXME: this is wrong, the language should
2214 // be saved in the context. (JMarc)
2215 selectlang = subst(p.verbatim_item(), "\n", " ");
2216 os << "\\lang " << selectlang << "\n";
2219 else if (t.cs() == "foreignlanguage") {
2220 context.check_layout(os);
2221 os << "\n\\lang " << subst(p.verbatim_item(), "\n", " ") << "\n";
2222 os << subst(p.verbatim_item(), "\n", " ");
2223 // FIXME: the second argument of selectlanguage
2224 // has to be parsed (like for \textsf, for
2226 // set back to last selectlanguage
2227 os << "\n\\lang " << selectlang << "\n";
2230 else if (t.cs() == "inputencoding") {
2231 // nothing to write here
2232 string const enc = subst(p.verbatim_item(), "\n", " ");
2236 else if (t.cs() == "LyX" || t.cs() == "TeX"
2237 || t.cs() == "LaTeX") {
2238 context.check_layout(os);
2240 skip_braces(p); // eat {}
2243 else if (t.cs() == "LaTeXe") {
2244 context.check_layout(os);
2246 skip_braces(p); // eat {}
2249 else if (t.cs() == "ldots") {
2250 context.check_layout(os);
2252 os << "\\SpecialChar \\ldots{}\n";
2255 else if (t.cs() == "lyxarrow") {
2256 context.check_layout(os);
2257 os << "\\SpecialChar \\menuseparator\n";
2261 else if (t.cs() == "textcompwordmark") {
2262 context.check_layout(os);
2263 os << "\\SpecialChar \\textcompwordmark{}\n";
2267 else if (t.cs() == "@" && p.next_token().asInput() == ".") {
2268 context.check_layout(os);
2269 os << "\\SpecialChar \\@.\n";
2273 else if (t.cs() == "-") {
2274 context.check_layout(os);
2275 os << "\\SpecialChar \\-\n";
2278 else if (t.cs() == "textasciitilde") {
2279 context.check_layout(os);
2284 else if (t.cs() == "textasciicircum") {
2285 context.check_layout(os);
2290 else if (t.cs() == "textbackslash") {
2291 context.check_layout(os);
2292 os << "\n\\backslash\n";
2296 else if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#"
2297 || t.cs() == "$" || t.cs() == "{" || t.cs() == "}"
2299 context.check_layout(os);
2303 else if (t.cs() == "char") {
2304 context.check_layout(os);
2305 if (p.next_token().character() == '`') {
2307 if (p.next_token().cs() == "\"") {
2312 handle_ert(os, "\\char`", context);
2315 handle_ert(os, "\\char", context);
2319 else if (t.cs() == "verb") {
2320 context.check_layout(os);
2321 char const delimiter = p.next_token().character();
2322 string const arg = p.getArg(delimiter, delimiter);
2324 oss << "\\verb" << delimiter << arg << delimiter;
2325 handle_ert(os, oss.str(), context);
2328 // Problem: \= creates a tabstop inside the tabbing environment
2329 // and else an accent. In the latter case we really would want
2330 // \={o} instead of \= o.
2331 else if (t.cs() == "=" && (flags & FLAG_TABBING))
2332 handle_ert(os, t.asInput(), context);
2334 // accents (see Table 6 in Comprehensive LaTeX Symbol List)
2335 else if (t.cs().size() == 1
2336 && contains("\"'.=^`bcdHkrtuv~", t.cs())) {
2337 context.check_layout(os);
2338 // try to see whether the string is in unicodesymbols
2340 string command = t.asInput() + "{"
2341 + trim(p.verbatim_item())
2343 docstring s = encodings.fromLaTeXCommand(from_utf8(command), rem);
2346 cerr << "When parsing " << command
2347 << ", result is " << to_utf8(s)
2348 << "+" << to_utf8(rem) << endl;
2351 // we did not find a non-ert version
2352 handle_ert(os, command, context);
2355 else if (t.cs() == "\\") {
2356 context.check_layout(os);
2357 string const next = p.next_token().asInput();
2359 handle_ert(os, "\\\\" + p.getOpt(), context);
2360 else if (next == "*") {
2362 handle_ert(os, "\\\\*" + p.getOpt(), context);
2365 os << "\n\\newline\n";
2369 else if (t.cs() == "newline") {
2370 context.check_layout(os);
2371 os << "\n\\" << t.cs() << "\n";
2372 skip_braces(p); // eat {}
2375 else if (t.cs() == "input" || t.cs() == "include"
2376 || t.cs() == "verbatiminput") {
2377 string name = '\\' + t.cs();
2378 if (t.cs() == "verbatiminput"
2379 && p.next_token().asInput() == "*")
2380 name += p.get_token().asInput();
2381 context.check_layout(os);
2382 begin_inset(os, "Include ");
2383 string filename(normalize_filename(p.getArg('{', '}')));
2384 string const path = getMasterFilePath();
2385 // We want to preserve relative / absolute filenames,
2386 // therefore path is only used for testing
2387 if ((t.cs() == "include" || t.cs() == "input") &&
2388 !makeAbsPath(filename, path).exists()) {
2389 // The file extension is probably missing.
2390 // Now try to find it out.
2391 string const tex_name =
2392 find_file(filename, path,
2393 known_tex_extensions);
2394 if (!tex_name.empty())
2395 filename = tex_name;
2397 if (makeAbsPath(filename, path).exists()) {
2398 string const abstexname =
2399 makeAbsPath(filename, path).absFilename();
2400 string const abslyxname =
2401 changeExtension(abstexname, ".lyx");
2402 fix_relative_filename(filename);
2403 string const lyxname =
2404 changeExtension(filename, ".lyx");
2405 if (t.cs() != "verbatiminput" &&
2406 tex2lyx(abstexname, FileName(abslyxname),
2408 os << name << '{' << lyxname << "}\n";
2410 os << name << '{' << filename << "}\n";
2413 cerr << "Warning: Could not find included file '"
2414 << filename << "'." << endl;
2415 os << name << '{' << filename << "}\n";
2417 os << "preview false\n";
2421 else if (t.cs() == "bibliographystyle") {
2422 // store new bibliographystyle
2423 bibliographystyle = p.verbatim_item();
2424 // output new bibliographystyle.
2425 // This is only necessary if used in some other macro than \bibliography.
2426 handle_ert(os, "\\bibliographystyle{" + bibliographystyle + "}", context);
2429 else if (t.cs() == "bibliography") {
2430 context.check_layout(os);
2431 begin_inset(os, "LatexCommand ");
2432 os << "bibtex" << "\n";
2433 os << "bibfiles " << '"' << p.verbatim_item() << '"' << "\n";
2434 // Do we have a bibliographystyle set?
2435 if (!bibliographystyle.empty())
2436 os << "options " << '"' << bibliographystyle << '"' << "\n";
2440 else if (t.cs() == "parbox")
2441 parse_box(p, os, FLAG_ITEM, outer, context, true);
2443 //\makebox() is part of the picture environment and different from \makebox{}
2444 //\makebox{} will be parsed by parse_box when bug 2956 is fixed
2445 else if (t.cs() == "makebox") {
2446 string arg = t.asInput();
2447 if (p.next_token().character() == '(')
2448 //the syntax is: \makebox(x,y)[position]{content}
2449 arg += p.getFullParentheseArg();
2451 //the syntax is: \makebox[width][position]{content}
2452 arg += p.getFullOpt();
2453 handle_ert(os, arg + p.getFullOpt(), context);
2456 else if (t.cs() == "smallskip" ||
2457 t.cs() == "medskip" ||
2458 t.cs() == "bigskip" ||
2459 t.cs() == "vfill") {
2460 context.check_layout(os);
2461 begin_inset(os, "VSpace ");
2467 else if (is_known(t.cs(), known_spaces)) {
2468 char const * const * where = is_known(t.cs(), known_spaces);
2469 context.check_layout(os);
2470 os << "\\InsetSpace ";
2471 os << '\\' << known_coded_spaces[where - known_spaces]
2473 // LaTeX swallows whitespace after all spaces except
2474 // "\\,". We have to do that here, too, because LyX
2475 // adds "{}" which would make the spaces significant.
2477 eat_whitespace(p, os, context, false);
2478 // LyX adds "{}" after all spaces except "\\ " and
2479 // "\\,", so we have to remove "{}".
2480 // "\\,{}" is equivalent to "\\," in LaTeX, so we
2481 // remove the braces after "\\,", too.
2486 else if (t.cs() == "newpage" ||
2487 t.cs() == "clearpage" ||
2488 t.cs() == "cleardoublepage") {
2489 context.check_layout(os);
2490 os << "\n\\" << t.cs() << "\n";
2491 skip_braces(p); // eat {}
2494 else if (t.cs() == "newcommand" ||
2495 t.cs() == "providecommand" ||
2496 t.cs() == "renewcommand") {
2497 // these could be handled by parse_command(), but
2498 // we need to call add_known_command() here.
2499 string name = t.asInput();
2500 if (p.next_token().asInput() == "*") {
2501 // Starred form. Eat '*'
2505 string const command = p.verbatim_item();
2506 string const opt1 = p.getOpt();
2507 string const opt2 = p.getFullOpt();
2508 add_known_command(command, opt1, !opt2.empty());
2509 string const ert = name + '{' + command + '}' +
2511 '{' + p.verbatim_item() + '}';
2513 context.check_layout(os);
2514 begin_inset(os, "FormulaMacro");
2519 else if (t.cs() == "vspace") {
2520 bool starred = false;
2521 if (p.next_token().asInput() == "*") {
2525 string const length = p.verbatim_item();
2528 bool valid = splitLatexLength(length, valstring, unit);
2529 bool known_vspace = false;
2530 bool known_unit = false;
2533 istringstream iss(valstring);
2536 if (unit == "\\smallskipamount") {
2538 known_vspace = true;
2539 } else if (unit == "\\medskipamount") {
2541 known_vspace = true;
2542 } else if (unit == "\\bigskipamount") {
2544 known_vspace = true;
2545 } else if (unit == "\\fill") {
2547 known_vspace = true;
2550 if (!known_vspace) {
2551 switch (unitFromString(unit)) {
2572 if (known_unit || known_vspace) {
2573 // Literal length or known variable
2574 context.check_layout(os);
2575 begin_inset(os, "VSpace ");
2583 // LyX can't handle other length variables in Inset VSpace
2584 string name = t.asInput();
2589 handle_ert(os, name + '{' + unit + '}', context);
2590 else if (value == -1.0)
2591 handle_ert(os, name + "{-" + unit + '}', context);
2593 handle_ert(os, name + '{' + valstring + unit + '}', context);
2595 handle_ert(os, name + '{' + length + '}', context);
2600 // try to see whether the string is in unicodesymbols
2602 docstring s = encodings.fromLaTeXCommand(from_utf8(t.asInput()), rem);
2605 cerr << "When parsing " << t.cs()
2606 << ", result is " << to_utf8(s)
2607 << "+" << to_utf8(rem) << endl;
2608 context.check_layout(os);
2611 skip_braces(p); // eat {}
2613 //cerr << "#: " << t << " mode: " << mode << endl;
2614 // heuristic: read up to next non-nested space
2616 string s = t.asInput();
2617 string z = p.verbatim_item();
2618 while (p.good() && z != " " && z.size()) {
2619 //cerr << "read: " << z << endl;
2621 z = p.verbatim_item();
2623 cerr << "found ERT: " << s << endl;
2624 handle_ert(os, s + ' ', context);
2627 string name = t.asInput();
2628 if (p.next_token().asInput() == "*") {
2629 // Starred commands like \vspace*{}
2630 p.get_token(); // Eat '*'
2633 if (!parse_command(name, p, os, outer, context))
2634 handle_ert(os, name, context);
2638 if (flags & FLAG_LEAVE) {
2639 flags &= ~FLAG_LEAVE;