]> git.lyx.org Git - features.git/commitdiff
Fix display and output of math macros with optional arguments
authorEnrico Forestieri <forenr@lyx.org>
Sun, 4 Dec 2016 21:52:22 +0000 (22:52 +0100)
committerEnrico Forestieri <forenr@lyx.org>
Sun, 4 Dec 2016 21:52:22 +0000 (22:52 +0100)
This is a long standing issue, present since the new math macros
inception in version 1.6. It manifests as a display issue when a
macro with optional arguments appears in the optional argument of
another macro. In this case the display is messed up and it is
difficult, if not impossible, changing the arguments as they do not
appear on screen as related to a specific macro instance. It also
manifests as latex errors when compiling, even if the latex output
is formally correct, due to limitations of the xargs package used
to output the macros. Most probably, both aspects have the same
root cause, as simply enclosing in braces the macro and its
parameters solves both issues. However, when reloading a document,
lyx strips the outer braces enclosing a macro argument, thus
frustrating this possible workaround.

This commit solves the display issue by correctly accounting for
macros with optional arguments nested in the argument of another
macro, and circumvents the xargs package limitations causing errors
by enclosing in braces the macros with optional arguments appearing
in the argument of an outer macro when they are output. This means
that when loading an old document with such macros and saving it
again, the macro representation is updated and will have these
additional braces. However, as such braces are stripped by lyx on
loading, there is no risk that they accumulate.

See also this thread:
http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg197828.html

src/mathed/MathData.cpp
src/mathed/MathMacro.cpp
src/mathed/MathStream.cpp
src/mathed/MathStream.h
status.22x

index 7e302c8ac7b8655f860fb307f8bde1812a69b79e..0701244321ccade5a38139a93c26a942b7f5480a 100644 (file)
@@ -713,12 +713,15 @@ void MathData::collectOptionalParameters(Cursor * cur,
                if (operator[](pos)->getChar() != '[')
                        break;
 
-               // found possible optional argument, look for "]"
+               // found possible optional argument, look for pairing "]"
+               int count = 1;
                size_t right = pos + 1;
                for (; right < size(); ++right) {
                        MathAtom & cell = operator[](right);
 
-                       if (cell->getChar() == ']')
+                       if (cell->getChar() == '[')
+                               ++count;
+                       else if (cell->getChar() == ']' && --count == 0)
                                // found right end
                                break;
 
index 9fe6e15cc46f3728ad9629dfaa7388357d5d479b..06d5a2c36b8b7aa129813781ac2ff9751322b79c 100644 (file)
@@ -938,6 +938,15 @@ void MathMacro::write(WriteStream & os) const
        // we should be ok to continue even if this fails.
        LATTEST(d->macro_);
 
+       // We may already be in the argument of a macro
+       bool const inside_macro = os.insideMacro();
+       os.insideMacro(true);
+
+       // Enclose in braces to avoid latex errors with xargs if we have
+       // optional arguments and are in the optional argument of a macro
+       if (d->optionals_ && inside_macro)
+               os << '{';
+
        // Always protect macros in a fragile environment
        if (os.fragile())
                os << "\\protect";
@@ -976,9 +985,13 @@ void MathMacro::write(WriteStream & os) const
                first = false;
        }
 
-       // add space if there was no argument
-       if (first)
+       // Close the opened brace or add space if there was no argument
+       if (d->optionals_ && inside_macro)
+               os << '}';
+       else if (first)
                os.pendingSpace(true);
+
+       os.insideMacro(inside_macro);
 }
 
 
index 7ea6ab75f813e0e194b8ee51dd0f17f4a5818dab..64e48667cd8c7ac3dd8e4b85e0d55e80e20bda93 100644 (file)
@@ -125,17 +125,19 @@ WriteStream & operator<<(WriteStream & ws, docstring const & s)
 WriteStream::WriteStream(otexrowstream & os, bool fragile, bool latex,
                                                 OutputType output, Encoding const * encoding)
        : os_(os), fragile_(fragile), firstitem_(false), latex_(latex),
-         output_(output), pendingspace_(false), pendingbrace_(false),
-         textmode_(false), locked_(0), ascii_(0), canbreakline_(true),
-         mathsout_(false), ulemcmd_(NONE), line_(0), encoding_(encoding)
+         output_(output), insidemacro_(false), pendingspace_(false),
+         pendingbrace_(false), textmode_(false), locked_(0), ascii_(0),
+         canbreakline_(true), mathsout_(false), ulemcmd_(NONE), line_(0),
+         encoding_(encoding)
 {}
 
 
 WriteStream::WriteStream(otexrowstream & os)
        : os_(os), fragile_(false), firstitem_(false), latex_(false),
-         output_(wsDefault), pendingspace_(false), pendingbrace_(false),
-         textmode_(false), locked_(0), ascii_(0), canbreakline_(true),
-         line_(0), encoding_(0) 
+         output_(wsDefault), insidemacro_(false), pendingspace_(false),
+         pendingbrace_(false), textmode_(false), locked_(0), ascii_(0),
+         canbreakline_(true), mathsout_(false), ulemcmd_(NONE), line_(0),
+         encoding_(0)
 {}
 
 
index b2a7992f3d848f083076c767ea55d825abd6f4ab..977947b7d3a2f6bb8b6c8c647de1e55b192f8b23 100644 (file)
@@ -80,6 +80,10 @@ public:
        void ulemCmd(UlemCmdType ulemcmd) { ulemcmd_ = ulemcmd; }
        /// tell which ulem command type we are inside
        UlemCmdType ulemCmd() const { return ulemcmd_; }
+       /// record whether we are in the argument of a math macro
+       void insideMacro(bool insidemacro) { insidemacro_ = insidemacro; }
+       /// tell whether we are in the argument of a math macro
+       bool insideMacro() const { return insidemacro_; }
        /// writes space if next thing is isalpha()
        void pendingSpace(bool how);
        /// writes space if next thing is isalpha()
@@ -122,6 +126,8 @@ private:
        int latex_;
        /// output type (default, source preview, instant preview)?
        OutputType output_;
+       /// are we in the argument of a math macro?
+       bool insidemacro_;
        /// do we have a space pending?
        bool pendingspace_;
        /// do we have a brace pending?
index 3bc26b21826752c5ba020d08cd5c3c5cbc1a2891..5ae18f127bf93654d7994dad30729d61d27f8671 100644 (file)
@@ -147,6 +147,9 @@ What's new
 - Do not use the clipboard stack when transforming selection into an
   inset (bug 6570).
 
+- Fix display and output of math macros with optional arguments appearing
+  in the optional argument of another macro.
+
 * INTERNALS