Correctly terminate a user math macro in certain circumstances

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.
This commit is contained in:
Enrico Forestieri 2021-01-07 00:57:36 +01:00
parent 99e636ae7b
commit fece5d4f37
3 changed files with 25 additions and 4 deletions

View 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);

View 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);

View 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?