]> git.lyx.org Git - lyx.git/blobdiff - src/tex2lyx/text.cpp
tex2lyx/text.cpp: \InsetSpace has no begin and end (LyX 1.5 was tolerant to the missi...
[lyx.git] / src / tex2lyx / text.cpp
index 1993098ff64dd19dbd1a92cf25768db5d01411d7..2a38b07e53dfdfe52b530e8875612a6d9ac4031d 100644 (file)
@@ -3,8 +3,9 @@
  * This file is part of LyX, the document processor.
  * Licence details can be found in the file COPYING.
  *
- * \author André Pönitz
+ * \author André Pönitz
  * \author Jean-Marc Lasgouttes
+ * \author Uwe Stöhr
  *
  * Full author contact details are available in file CREDITS.
  */
 #include <config.h>
 
 #include "tex2lyx.h"
+
 #include "Context.h"
-#include "Layout.h"
 #include "FloatList.h"
-#include "lengthcommon.h"
-#include "support/lstrings.h"
+#include "Layout.h"
+#include "Length.h"
+
+#include "support/assert.h"
 #include "support/convert.h"
+#include "support/FileName.h"
 #include "support/filetools.h"
+#include "support/lstrings.h"
 
-#include <boost/filesystem/operations.hpp>
-#include <boost/tuple/tuple.hpp>
-
+#include <algorithm>
 #include <iostream>
 #include <map>
 #include <sstream>
 #include <vector>
 
-using std::cerr;
-using std::endl;
-
-using std::map;
-using std::ostream;
-using std::ostringstream;
-using std::istringstream;
-using std::string;
-using std::vector;
+using namespace std;
+using namespace lyx::support;
 
 namespace lyx {
 
-using support::addExtension;
-using support::changeExtension;
-using support::FileName;
-using support::makeAbsPath;
-using support::makeRelPath;
-using support::rtrim;
-using support::suffixIs;
-using support::contains;
-using support::subst;
-
-namespace fs = boost::filesystem;
-
 
 void parse_text_in_inset(Parser & p, ostream & os, unsigned flags, bool outer,
                Context const & context)
@@ -105,7 +89,7 @@ string parse_text_snippet(Parser & p, unsigned flags, const bool outer,
 }
 
 
-char const * const known_latex_commands[] = { "ref", "cite", "label", "href",
+char const * const known_latex_commands[] = { "ref", "cite", "nocite", "label",
  "index", "printindex", "pageref", "url", "vref", "vpageref", "prettyref",
  "eqref", 0 };
 
@@ -134,20 +118,24 @@ char const * const known_jurabib_commands[] = { "cite", "citet", "citep",
 "citefield", "citetitle", "cite*", 0 };
 
 /// LaTeX names for quotes
-char const * const known_quotes[] = { "glqq", "grqq", "quotedblbase",
-"textquotedblleft", "quotesinglbase", "guilsinglleft", "guilsinglright", 0};
+char const * const known_quotes[] = { "dq", "guillemotleft", "flqq", "og",
+"guillemotright", "frqq", "fg", "glq", "glqq", "textquoteleft", "grq", "grqq",
+"quotedblbase", "textquotedblleft", "quotesinglbase", "textquoteright", "flq",
+"guilsinglleft", "frq", "guilsinglright", 0};
 
 /// the same as known_quotes with .lyx names
-char const * const known_coded_quotes[] = { "gld", "grd", "gld",
-"grd", "gls", "fls", "frs", 0};
+char const * const known_coded_quotes[] = { "prd", "ard", "ard", "ard",
+"ald", "ald", "ald", "gls", "gld", "els", "els", "grd",
+"gld", "grd", "gls", "ers", "fls",
+"fls", "frs", "frs", 0};
 
 /// LaTeX names for font sizes
 char const * const known_sizes[] = { "tiny", "scriptsize", "footnotesize",
 "small", "normalsize", "large", "Large", "LARGE", "huge", "Huge", 0};
 
 /// the same as known_sizes with .lyx names
-char const * const known_coded_sizes[] = { "tiny", "scriptsize", "footnotesize",
-"small", "normal", "large", "larger", "largest",  "huge", "giant", 0};
+char const * const known_coded_sizes[] = { "default", "tiny", "scriptsize", "footnotesize",
+"small", "normal", "large", "larger", "largest", "huge", "giant", 0};
 
 /// LaTeX 2.09 names for font families
 char const * const known_old_font_families[] = { "rm", "sf", "tt", 0};
@@ -263,7 +251,7 @@ bool splitLatexLength(string const & len, string & value, string & unit)
        if (contains(len, '\\'))
                unit = trim(string(len, i));
        else
-               unit = support::ascii_lowercase(trim(string(len, i)));
+               unit = ascii_lowercase(trim(string(len, i)));
        return true;
 }
 
@@ -435,36 +423,37 @@ void handle_comment(ostream & os, string const & s, Context & context)
 }
 
 
-class isLayout : public std::unary_function<LayoutPtr, bool> {
-public:
-       isLayout(string const name) : name_(name) {}
-       bool operator()(LayoutPtr const & ptr) const {
-               return ptr->latexname() == name_;
-       }
-private:
-       string const name_;
-};
-
-
-LayoutPtr findLayout(TextClass const & textclass,
-                        string const & name)
+Layout const * findLayout(TextClass const & textclass, string const & name)
 {
-       TextClass::const_iterator beg = textclass.begin();
-       TextClass::const_iterator end = textclass.end();
-
-       TextClass::const_iterator
-               it = std::find_if(beg, end, isLayout(name));
-
-       return (it == end) ? LayoutPtr() : *it;
+       DocumentClass::const_iterator lit = textclass.begin();
+       DocumentClass::const_iterator len = textclass.end();
+       for (; lit != len; ++lit)
+               if (lit->latexname() == name)
+                       return &*lit;
+       return 0;
 }
 
 
 void eat_whitespace(Parser &, ostream &, Context &, bool);
 
 
+Layout * captionlayout()
+{
+       static Layout * lay = 0;
+       if (!lay) {
+               lay = new Layout;
+               lay->name_ = from_ascii("Caption");
+               lay->latexname_ = "caption";
+               lay->latextype = LATEX_COMMAND;
+               lay->optionalargs = 1;
+       }
+       return lay;
+}
+
+
 void output_command_layout(ostream & os, Parser & p, bool outer,
                           Context & parent_context,
-                          LayoutPtr newlayout)
+                          Layout const * newlayout)
 {
        parent_context.check_end_layout(os);
        Context context(true, parent_context.textclass, newlayout,
@@ -720,7 +709,7 @@ void parse_unknown_environment(Parser & p, string const & name, ostream & os,
 void parse_environment(Parser & p, ostream & os, bool outer,
                       Context & parent_context)
 {
-       LayoutPtr newlayout;
+       Layout const * newlayout;
        string const name = p.getArg('{', '}');
        const bool is_starred = suffixIs(name, '*');
        string const unstarred_name = rtrim(name, "*");
@@ -776,6 +765,7 @@ void parse_environment(Parser & p, ostream & os, bool outer,
                parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
                end_inset(os);
                p.skip_spaces();
+               skip_braces(p); // eat {} that might by set by LyX behind comments
        }
 
        else if (name == "lyxgreyedout") {
@@ -788,27 +778,52 @@ void parse_environment(Parser & p, ostream & os, bool outer,
                p.skip_spaces();
        }
 
+       else if (name == "framed") {
+               eat_whitespace(p, os, parent_context, false);
+               parent_context.check_layout(os);
+               begin_inset(os, "Note Framed\n");
+               os << "status open\n";
+               parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
+               end_inset(os);
+               p.skip_spaces();
+       }
+
+       else if (name == "shaded") {
+               eat_whitespace(p, os, parent_context, false);
+               parent_context.check_layout(os);
+               begin_inset(os, "Note Shaded\n");
+               os << "status open\n";
+               parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
+               end_inset(os);
+               p.skip_spaces();
+       }
+
        else if (!parent_context.new_layout_allowed)
                parse_unknown_environment(p, name, os, FLAG_END, outer,
                                          parent_context);
 
-       // Alignment settings
-       else if (name == "center" || name == "flushleft" || name == "flushright" ||
-                name == "centering" || name == "raggedright" || name == "raggedleft") {
+       // Alignment and spacing settings
+       // FIXME (bug xxxx): These settings can span multiple paragraphs and
+       //                                       therefore are totally broken!
+       // Note that \centering, raggedright, and raggedleft cannot be handled, as
+       // they are commands not environments. They are furthermore switches that
+       // can be ended by another switches, but also by commands like \footnote or
+       // \parbox. So the only safe way is to leave them untouched.
+       else if (name == "center" || name == "flushleft" || name == "flushright") {
                eat_whitespace(p, os, parent_context, false);
                // We must begin a new paragraph if not already done
                if (! parent_context.atParagraphStart()) {
                        parent_context.check_end_layout(os);
                        parent_context.new_paragraph(os);
                }
-               if (name == "flushleft" || name == "raggedright")
+               if (name == "flushleft")
                        parent_context.add_extra_stuff("\\align left\n");
-               else if (name == "flushright" || name == "raggedleft")
+               else if (name == "flushright")
                        parent_context.add_extra_stuff("\\align right\n");
-               else
+               else if (name == "center")
                        parent_context.add_extra_stuff("\\align center\n");
                parse_text(p, os, FLAG_END, outer, parent_context);
-               // Just in case the environment is empty ..
+               // Just in case the environment is empty
                parent_context.extra_stuff.erase();
                // We must begin a new paragraph to reset the alignment
                parent_context.new_paragraph(os);
@@ -816,7 +831,7 @@ void parse_environment(Parser & p, ostream & os, bool outer,
        }
 
        // The single '=' is meant here.
-       else if ((newlayout = findLayout(parent_context.textclass, name)).get() &&
+       else if ((newlayout = findLayout(parent_context.textclass, name)) &&
                  newlayout->isEnvironment()) {
                eat_whitespace(p, os, parent_context, false);
                Context context(true, parent_context.textclass, newlayout,
@@ -909,7 +924,7 @@ void parse_environment(Parser & p, ostream & os, bool outer,
 /// parses a comment and outputs it to \p os.
 void parse_comment(Parser & p, ostream & os, Token const & t, Context & context)
 {
-       BOOST_ASSERT(t.cat() == catComment);
+       LASSERT(t.cat() == catComment, return);
        if (!t.cs().empty()) {
                context.check_layout(os);
                handle_comment(os, '%' + t.cs(), context);
@@ -977,25 +992,26 @@ void parse_text_attributes(Parser & p, ostream & os, unsigned flags, bool outer,
 
 
 /// get the arguments of a natbib or jurabib citation command
-std::pair<string, string> getCiteArguments(Parser & p, bool natbibOrder)
+void get_cite_arguments(Parser & p, bool natbibOrder,
+       string & before, string & after)
 {
        // We need to distinguish "" and "[]", so we can't use p.getOpt().
 
        // text before the citation
-       string before;
+       before.clear();
        // text after the citation
-       string after = p.getFullOpt();
+       after = p.getFullOpt();
 
        if (!after.empty()) {
                before = p.getFullOpt();
                if (natbibOrder && !before.empty())
-                       std::swap(before, after);
+                       swap(before, after);
        }
-       return std::make_pair(before, after);
 }
 
 
-/// Convert filenames with TeX macros and/or quotes to something LyX can understand
+/// Convert filenames with TeX macros and/or quotes to something LyX
+/// can understand
 string const normalize_filename(string const & name)
 {
        Parser p(trim(name, "\""));
@@ -1023,8 +1039,10 @@ string const normalize_filename(string const & name)
 /// convention (relative to .lyx file) if it is relative
 void fix_relative_filename(string & name)
 {
-       if (lyx::support::absolutePath(name))
+       FileName fname(name);
+       if (fname.isAbsolute())
                return;
+
        // FIXME UNICODE encoding of name may be wrong (makeAbsPath expects
        // utf8)
        name = to_utf8(makeRelPath(from_utf8(makeAbsPath(name, getMasterFilePath()).absFilename()),
@@ -1066,7 +1084,7 @@ void parse_noweb(Parser & p, ostream & os, Context & context)
        // always must be in an own paragraph.
        context.new_paragraph(os);
        Context newcontext(true, context.textclass,
-               context.textclass[from_ascii("Scrap")]);
+               &context.textclass[from_ascii("Scrap")]);
        newcontext.check_layout(os);
        os << name;
        while (p.good()) {
@@ -1106,7 +1124,9 @@ void parse_noweb(Parser & p, ostream & os, Context & context)
 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                Context & context)
 {
-       LayoutPtr newlayout;
+       Layout const * newlayout = 0;
+       // store the current selectlanguage to be used after \foreignlanguage
+       string selectlang;
        // Store the latest bibliographystyle (needed for bibtex inset)
        string bibliographystyle;
        bool const use_natbib = used_packages.find("natbib") != used_packages.end();
@@ -1168,8 +1188,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                // extended to other quotes, but is not so easy (a
                // left english quote is the same as a right german
                // quote...)
-               else if (t.asInput() == "`"
-                        && p.next_token().asInput() == "`") {
+               else if (t.asInput() == "`" && p.next_token().asInput() == "`") {
                        context.check_layout(os);
                        begin_inset(os, "Quotes ");
                        os << "eld";
@@ -1177,8 +1196,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        p.get_token();
                        skip_braces(p);
                }
-               else if (t.asInput() == "'"
-                        && p.next_token().asInput() == "'") {
+               else if (t.asInput() == "'" && p.next_token().asInput() == "'") {
                        context.check_layout(os);
                        begin_inset(os, "Quotes ");
                        os << "erd";
@@ -1187,6 +1205,24 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        skip_braces(p);
                }
 
+               else if (t.asInput() == ">" && p.next_token().asInput() == ">") {
+                       context.check_layout(os);
+                       begin_inset(os, "Quotes ");
+                       os << "ald";
+                       end_inset(os);
+                       p.get_token();
+                       skip_braces(p);
+               }
+
+               else if (t.asInput() == "<" && p.next_token().asInput() == "<") {
+                       context.check_layout(os);
+                       begin_inset(os, "Quotes ");
+                       os << "ard";
+                       end_inset(os);
+                       p.get_token();
+                       skip_braces(p);
+               }
+
                else if (t.asInput() == "<"
                         && p.next_token().asInput() == "<" && noweb_mode) {
                        p.get_token();
@@ -1422,8 +1458,12 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        os << "\\bibitem ";
                        os << p.getOpt();
                        os << '{' << p.verbatim_item() << '}' << "\n";
+               } 
+               
+               else if(t.cs() == "global") {
+                       // skip global which can appear in front of e.g. "def"
                }
-
+               
                else if (t.cs() == "def") {
                        context.check_layout(os);
                        eat_whitespace(p, os, context, false);
@@ -1500,9 +1540,8 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                else if ((p.next_token().asInput() == "*") &&
                         context.new_layout_allowed &&
                         // The single '=' is meant here.
-                        (newlayout = findLayout(context.textclass,
-                                                t.cs() + '*')).get() &&
-                        newlayout->isCommand()) {
+                        (newlayout = findLayout(context.textclass, t.cs() + '*')) &&
+                         newlayout->isCommand()) {
                        p.get_token();
                        output_command_layout(os, p, outer, context, newlayout);
                        p.skip_spaces();
@@ -1510,7 +1549,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
 
                // The single '=' is meant here.
                else if (context.new_layout_allowed &&
-                        (newlayout = findLayout(context.textclass, t.cs())).get() &&
+                        (newlayout = findLayout(context.textclass, t.cs())) &&
                         newlayout->isCommand()) {
                        output_command_layout(os, p, outer, context, newlayout);
                        p.skip_spaces();
@@ -1519,9 +1558,9 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                // Special handling for \caption
                // FIXME: remove this when InsetCaption is supported.
                else if (context.new_layout_allowed &&
-                        t.cs() == captionlayout->latexname()) {
+                        t.cs() == captionlayout()->latexname()) {
                        output_command_layout(os, p, outer, context, 
-                                             captionlayout);
+                                             captionlayout());
                        p.skip_spaces();
                }
 
@@ -1630,8 +1669,8 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                                numberOfbbOptions++;
                        if (numberOfbbOptions == 4)
                                os << "\tBoundingBox "
-                                  << opts["bbllx"] << opts["bblly"]
-                                  << opts["bburx"] << opts["bbury"] << '\n';
+                                  << opts["bbllx"] << " " << opts["bblly"] << " "
+                                  << opts["bburx"] << " " << opts["bbury"] << '\n';
                        else if (numberOfbbOptions > 0)
                                cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
                        numberOfbbOptions = 0;
@@ -1641,7 +1680,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                                numberOfbbOptions++;
                        if (numberOfbbOptions == 2)
                                os << "\tBoundingBox 0bp 0bp "
-                                  << opts["natwidth"] << opts["natheight"] << '\n';
+                                  << opts["natwidth"] << " " << opts["natheight"] << '\n';
                        else if (numberOfbbOptions > 0)
                                cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
                        ostringstream special;
@@ -1814,6 +1853,23 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                                eat_whitespace(p, os, context, false);
                }
 
+               else if (t.cs() == "textcolor") {
+                       // scheme is \textcolor{color name}{text}
+                       string const color = p.verbatim_item();
+                       // we only support the predefined colors of the color package
+                       if (color == "black" || color == "blue" || color == "cyan"
+                               || color == "green" || color == "magenta" || color == "red"
+                               || color == "white" || color == "yellow") {
+                                       context.check_layout(os);
+                                       os << "\n\\color " << color << "\n";
+                                       parse_text_snippet(p, os, FLAG_ITEM, outer, context);
+                                       context.check_layout(os);
+                                       os << "\n\\color inherit\n";
+                       } else
+                               // for custom defined colors
+                               handle_ert(os, t.asInput() + "{" + color + "}", context);
+               }
+
                else if (t.cs() == "underbar") {
                        // Do NOT handle \underline.
                        // \underbar cuts through y, g, q, p etc.,
@@ -1833,6 +1889,11 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        os << "\n\\" << t.cs() << " default\n";
                }
 
+               else if (t.cs() == "lyxline") {
+                       context.check_layout(os);
+                       os << "\\lyxline";
+               }
+
                else if (use_natbib &&
                         is_known(t.cs(), known_natbib_commands) &&
                         ((t.cs() != "citefullauthor" &&
@@ -1858,8 +1919,8 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        string before;
                        // text after the citation
                        string after;
+                       get_cite_arguments(p, true, before, after);
 
-                       boost::tie(before, after) = getCiteArguments(p, true);
                        if (command == "\\cite") {
                                // \cite without optional argument means
                                // \citet, \cite with at least one optional
@@ -1889,10 +1950,10 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        string const command = '\\' + t.cs();
                        char argumentOrder = '\0';
                        vector<string> const & options = used_packages["jurabib"];
-                       if (std::find(options.begin(), options.end(),
+                       if (find(options.begin(), options.end(),
                                      "natbiborder") != options.end())
                                argumentOrder = 'n';
-                       else if (std::find(options.begin(), options.end(),
+                       else if (find(options.begin(), options.end(),
                                           "jurabiborder") != options.end())
                                argumentOrder = 'j';
 
@@ -1900,9 +1961,8 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        string before;
                        // text after the citation
                        string after;
+                       get_cite_arguments(p, argumentOrder != 'j', before, after);
 
-                       boost::tie(before, after) =
-                               getCiteArguments(p, argumentOrder != 'j');
                        string const citation = p.verbatim_item();
                        if (!before.empty() && argumentOrder == '\0') {
                                cerr << "Warning: Assuming argument order "
@@ -1952,7 +2012,9 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        char const * const * where = is_known(t.cs(), known_sizes);
                        context.check_layout(os);
                        TeXFont const oldFont = context.font;
-                       context.font.size = known_coded_sizes[where - known_sizes];
+                       // the font size index differs by 1, because the known_coded_sizes
+                       // has additionally a "default" entry
+                       context.font.size = known_coded_sizes[where - known_sizes + 1];
                        output_font_change(os, oldFont, context.font);
                        eat_whitespace(p, os, context, false);
                }
@@ -2034,6 +2096,27 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        eat_whitespace(p, os, context, false);
                }
 
+               else if (t.cs() == "selectlanguage") {
+                       context.check_layout(os);
+                       // save the language for the case that a \foreignlanguage is used 
+                       selectlang = subst(p.verbatim_item(), "\n", " ");
+                       os << "\\lang " << selectlang << "\n";
+                       
+               }
+
+               else if (t.cs() == "foreignlanguage") {
+                       context.check_layout(os);
+                       os << "\n\\lang " << subst(p.verbatim_item(), "\n", " ") << "\n";
+                       os << subst(p.verbatim_item(), "\n", " ");
+                       // set back to last selectlanguage
+                       os << "\n\\lang " << selectlang << "\n";
+               }
+
+               else if (t.cs() == "inputencoding")
+                       // write nothing because this is done by LyX using the "\lang"
+                       // information given by selectlanguage and foreignlanguage
+                       subst(p.verbatim_item(), "\n", " ");
+               
                else if (t.cs() == "LyX" || t.cs() == "TeX"
                         || t.cs() == "LaTeX") {
                        context.check_layout(os);
@@ -2188,6 +2271,39 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        }
                }
 
+               else if (t.cs() == "newline" ||
+                       t.cs() == "linebreak") {
+                       context.check_layout(os);
+                       os << "\n\\" << t.cs() << "\n";
+                       skip_braces(p); // eat {}
+               }
+
+               else if (t.cs() == "href") {
+                       context.check_layout(os);
+                       begin_inset(os, "CommandInset ");
+                       os << t.cs() << "\n";
+                       os << "LatexCommand " << t.cs() << "\n";
+                       bool erase = false;
+                       size_t pos;
+                       // the first argument is "type:target", "type:" is optional
+                       // the second argument the name
+                       string href_target = subst(p.verbatim_item(), "\n", " ");
+                       string href_name = subst(p.verbatim_item(), "\n", " ");
+                       string href_type;
+                       // serach for the ":" to divide type from target
+                       if ((pos = href_target.find(":", 0)) != string::npos){
+                               href_type = href_target;
+                               href_type.erase(pos + 1, href_type.length());
+                               href_target.erase(0, pos + 1);
+                           erase = true;                                                                                       
+                       }
+                       os << "name " << '"' << href_name << '"' << "\n";
+                       os << "target " << '"' << href_target << '"' << "\n";
+                       if(erase)
+                               os << "type " << '"' << href_type << '"' << "\n";
+                       end_inset(os);
+               }
+
                else if (t.cs() == "input" || t.cs() == "include"
                         || t.cs() == "verbatiminput") {
                        string name = '\\' + t.cs();
@@ -2259,6 +2375,19 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
 
                else if (t.cs() == "parbox")
                        parse_box(p, os, FLAG_ITEM, outer, context, true);
+               
+               //\makebox() is part of the picture environment and different from \makebox{}
+               //\makebox{} will be parsed by parse_box when bug 2956 is fixed
+               else if (t.cs() == "makebox") {
+                       string arg = t.asInput();
+                       if (p.next_token().character() == '(')
+                               //the syntax is: \makebox(x,y)[position]{content}
+                               arg += p.getFullParentheseArg();
+                       else
+                               //the syntax is: \makebox[width][position]{content}
+                               arg += p.getFullOpt();
+                       handle_ert(os, arg + p.getFullOpt(), context);
+               }
 
                else if (t.cs() == "smallskip" ||
                         t.cs() == "medskip" ||
@@ -2274,7 +2403,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                else if (is_known(t.cs(), known_spaces)) {
                        char const * const * where = is_known(t.cs(), known_spaces);
                        context.check_layout(os);
-                       begin_inset(os, "InsetSpace ");
+                       os << "\\InsetSpace ";
                        os << '\\' << known_coded_spaces[where - known_spaces]
                           << '\n';
                        // LaTeX swallows whitespace after all spaces except
@@ -2291,17 +2420,18 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                }
 
                else if (t.cs() == "newpage" ||
-                        t.cs() == "clearpage" ||
-                        t.cs() == "cleardoublepage") {
+                       t.cs() == "pagebreak" ||
+                       t.cs() == "clearpage" ||
+                       t.cs() == "cleardoublepage") {
                        context.check_layout(os);
-                       // FIXME: what about \\pagebreak?
                        os << "\n\\" << t.cs() << "\n";
                        skip_braces(p); // eat {}
                }
 
                else if (t.cs() == "newcommand" ||
                         t.cs() == "providecommand" ||
-                        t.cs() == "renewcommand") {
+                        t.cs() == "renewcommand" ||
+                        t.cs() == "newlyxcommand") {
                        // these could be handled by parse_command(), but
                        // we need to call add_known_command() here.
                        string name = t.asInput();
@@ -2312,20 +2442,120 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        }
                        string const command = p.verbatim_item();
                        string const opt1 = p.getOpt();
-                       string const opt2 = p.getFullOpt();
-                       add_known_command(command, opt1, !opt2.empty());
-                       string const ert = name + '{' + command + '}' +
-                                          opt1 + opt2 +
-                                          '{' + p.verbatim_item() + '}';
+                       string optionals;
+                       unsigned optionalsNum = 0;
+                       while (true) {
+                               string const opt = p.getFullOpt();
+                               if (opt.empty())
+                                       break;
+                               optionalsNum++;
+                               optionals += opt;
+                       }
+                       add_known_command(command, opt1, optionalsNum);
+                       string const ert = name + '{' + command + '}' + opt1
+                               + optionals + '{' + p.verbatim_item() + '}';
 
-                       if (opt2.empty()) {
-                               context.check_layout(os);
-                               begin_inset(os, "FormulaMacro");
-                               os << "\n" << ert;
-                               end_inset(os);
-                       } else
-                               // we cannot handle optional argument, so only output ERT
-                               handle_ert(os, ert, context);
+                       context.check_layout(os);
+                       begin_inset(os, "FormulaMacro");
+                       os << "\n" << ert;
+                       end_inset(os);
+               }
+               
+               else if (t.cs() == "newcommandx" ||
+                        t.cs() == "renewcommandx") {
+                       // \newcommandx{\foo}[2][usedefault, addprefix=\global,1=default]{#1,#2}
+
+                       // get command name
+                       string command;
+                       if (p.next_token().cat() == catBegin)
+                               command = p.verbatim_item();
+                       else 
+                               command = "\\" + p.get_token().cs();
+                       
+                       // get arity, we do not check that it fits to the given
+                       // optional parameters here.
+                       string const opt1 = p.getOpt();
+                       
+                       // get options and default values for optional parameters
+                       std::vector<string> optionalValues;
+                       int optionalsNum = 0;
+                       if (p.next_token().character() == '[') {
+                               // skip '['
+                               p.get_token();
+                               
+                               // handle 'opt=value' options, separated by ','.
+                               eat_whitespace(p, os, context, false);
+                               while (p.next_token().character() != ']' && p.good()) {
+                                       char_type nextc = p.next_token().character();
+                                       if (nextc >= '1' && nextc <= '9') {
+                                               // optional value -> get parameter number
+                                               int n = p.getChar() - '0';
+
+                                               // skip '='
+                                               if (p.next_token().character() != '=') {
+                                                       cerr << "'=' expected after numeral option of \\newcommandx" << std::endl;
+                                                       // try to find ] or ,
+                                                       while (p.next_token().character() != ','
+                                                              && p.next_token().character() != ']')
+                                                               p.get_token();
+                                                       continue;
+                                               } else
+                                                       p.get_token();
+                                               
+                                               // get value
+                                               optionalValues.resize(max(size_t(n), optionalValues.size()));
+                                               optionalValues[n - 1].clear();
+                                               while (p.next_token().character() != ']'
+                                                      && p.next_token().character() != ',')
+                                                       optionalValues[n - 1] += p.verbatim_item();
+                                               optionalsNum = max(n, optionalsNum);
+                                       } else if (p.next_token().cat() == catLetter) {
+                                               // we in fact ignore every non-optional
+                                               // parameters
+                                               
+                                               // get option name
+                                               docstring opt;
+                                               while (p.next_token().cat() == catLetter)
+                                                       opt += p.getChar();
+                                               
+                                               // value?
+                                               eat_whitespace(p, os, context, false);
+                                               if (p.next_token().character() == '=') {
+                                                       p.get_token();
+                                                       while (p.next_token().character() != ']'
+                                                              && p.next_token().character() != ',')
+                                                               p.verbatim_item();
+                                               }
+                                       } else
+                                               return;
+                                       
+                                       // skip komma
+                                       eat_whitespace(p, os, context, false);
+                                       if (p.next_token().character() == ',') {
+                                               p.getChar();
+                                               eat_whitespace(p, os, context, false);
+                                       } else if (p.next_token().character() != ']')
+                                               continue;
+                               }
+                               
+                               // skip ']'
+                               p.get_token();
+                       }
+                       
+                       // concat the default values to the optionals string
+                       string optionals;
+                       for (unsigned i = 0; i < optionalValues.size(); ++i)
+                               optionals += "[" + optionalValues[i] + "]";
+                       
+                       // register and output command
+                       add_known_command(command, opt1, optionalsNum);
+                       string const ert = "\\newcommand{" + command + '}' + opt1
+                       + optionals + '{' + p.verbatim_item() + '}';
+                       
+                       context.check_layout(os);
+                       begin_inset(os, "FormulaMacro");
+                       os << "\n" << ert;
+                       end_inset(os);
                }
 
                else if (t.cs() == "vspace") {