]> git.lyx.org Git - features.git/commitdiff
Correctly terminate a user math macro in certain circumstances
authorEnrico Forestieri <forenr@lyx.org>
Wed, 6 Jan 2021 23:57:36 +0000 (00:57 +0100)
committerEnrico Forestieri <forenr@lyx.org>
Wed, 6 Jan 2021 23:57:36 +0000 (00:57 +0100)
If a macro has only optionals and none is specified and a [ immediately
follows, we have to terminate the macro with {}, otherwise what follows
is taken to be an optional argument.

Fixes #11665.

src/mathed/InsetMathMacro.cpp
src/mathed/MathStream.cpp
src/mathed/MathStream.h

index a21460091fda3e6cbe0f2a9d0000c3b7cff82d5d..d2c35c7e5bea913e1d857fc319871945de87d0db 100644 (file)
@@ -1214,8 +1214,11 @@ void InsetMathMacro::write(TeXMathStream & os) const
        }
 
        // add space if there was no argument
-       if (first)
+       // or add braces if we have optionals but none are present and [ follows
+       if (first) {
                os.pendingSpace(true);
+               os.useBraces(d->optionals_ > 0);
+       }
 
        // write \(no)limits modifiers if relevant
        writeLimits(os);
index 51ac98486795718acdcf3c7c3e75ac52a0444425..6f4da39bf71b097f80089e87e565317462cce925 100644 (file)
@@ -106,6 +106,8 @@ TeXMathStream & operator<<(TeXMathStream & ws, docstring const & s)
        } else if (ws.pendingSpace()) {
                if (isAlphaASCII(s[first]))
                        ws.os() << ' ';
+               else if (s[first] == '[' && ws.useBraces())
+                       ws.os() << "{}";
                else if (s[first] == ' ' && ws.textMode())
                        ws.os() << '\\';
                ws.pendingSpace(false);
@@ -148,9 +150,17 @@ void TeXMathStream::addlines(unsigned int n)
 }
 
 
-void TeXMathStream::pendingSpace(bool how)
+void TeXMathStream::pendingSpace(bool space)
 {
-       pendingspace_ = how;
+       pendingspace_ = space;
+       if (!space)
+               usebraces_ = false;
+}
+
+
+void TeXMathStream::useBraces(bool braces)
+{
+       usebraces_ = braces;
 }
 
 
@@ -226,6 +236,8 @@ TeXMathStream & operator<<(TeXMathStream & ws, char c)
        } else if (ws.pendingSpace()) {
                if (isAlphaASCII(c))
                        ws.os() << ' ';
+               else if (c == '[' && ws.useBraces())
+                       ws.os() << "{}";
                else if (c == ' ' && ws.textMode())
                        ws.os() << '\\';
                ws.pendingSpace(false);
index aa1f71a4616ea5831df08abf1d39932aac60b652..153e7df3c584404a43b68ebae48db36a6ba2edce 100644 (file)
@@ -80,9 +80,13 @@ public:
        /// tell which ulem command type we are inside
        UlemCmdType ulemCmd() const { return ulemcmd_; }
        /// writes space if next thing is isalpha()
-       void pendingSpace(bool how);
+       void pendingSpace(bool space);
        /// writes space if next thing is isalpha()
        bool pendingSpace() const { return pendingspace_; }
+       /// write braces if a space is pending and next char is [
+       void useBraces(bool braces);
+       /// write braces if a space is pending and next char is [
+       bool useBraces() const { return usebraces_; }
        /// tell whether to write the closing brace of \ensuremath
        void pendingBrace(bool brace);
        /// tell whether to write the closing brace of \ensuremath
@@ -124,6 +128,8 @@ private:
        OutputType output_ = wsDefault;
        /// do we have a space pending?
        bool pendingspace_ = false;
+       /// do we have to write braces when a space is pending and [ follows?
+       bool usebraces_ = false;
        /// do we have a brace pending?
        bool pendingbrace_ = false;
        /// are we in text mode when producing latex code?