]> git.lyx.org Git - features.git/blobdiff - src/mathed/MathMacro.cpp
Fix display and output of math macros with optional arguments
[features.git] / src / mathed / MathMacro.cpp
index 34b5160a5f84dc90ff13109ef26643457ca5a754..afc40a4910e9764df40ada1a4dae062c9baa4744 100644 (file)
@@ -1068,6 +1068,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";
@@ -1106,9 +1115,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);
 }