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;
// 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";
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);
}
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),
- row_entry_(TexRow::row_none)
+ output_(output), insidemacro_(false), pendingspace_(false),
+ pendingbrace_(false), textmode_(false), locked_(0), ascii_(0),
+ canbreakline_(true), mathsout_(false), ulemcmd_(NONE), line_(0),
+ encoding_(encoding), row_entry_(TexRow::row_none)
{}
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()
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?