# math accent before starred macro, with unicode-math or without amsmath

I have a macro \makebold which takes a star and a shortcut \aaa. I'm trying to use \aaa after \bar to produce a barred, bold letter.

\documentclass{standalone}
\usepackage{xparse, unicode-math}

\DeclareDocumentCommand \makebold { s m }{%
\mathbf{#2}%
}
\newcommand{\aaa}{\makebold*{b}}

\begin{document}Test $\bar\aaa$.\end{document}


produces the following:

! Missing { inserted.
\l__xparse_processor_int
l.16 Test $\bar\aaa$.


It works if I remove the s option to DeclareDocumentCommand. I further tried,

\documentclass{standalone}
\usepackage{amsmath}      % <- removing this line produces error
\usepackage{xparse}

\DeclareDocumentCommand \makebold { s m }{%
\mathbf{#2}%
}
\newcommand{\aaa}{\makebold*{b}}

\begin{document}Test $\bar\aaa$.\end{document}


This works. I remove the line \usepackage{amsmath}, it does not. What's going on here? How can I modify the two macros so they work with unicode-math or without amsmath?

• Using \bar x is wrong and it should be \bar{x}; if you are always consistent with this usage, you'd input \bar{\aaa} and the problem would disappear. Mar 10 '17 at 9:46
• @egreg, do you say this because of the precise nature of \bar or would \foo x (where foo is defined to take one argument) be equally wrong? Is \mathbf x wrong also?
– Mass
Mar 10 '17 at 13:43
• \mathbf x is conceptually wrong; it works, but promotes bad coding. Mar 10 '17 at 14:02

The problem in all your cases is that you don't use braces around the argument of \bar.

When TeX expands tokens, without the amsmath package being loaded \bar expands to \mathaccent "7016\relax. After that TeX expects a single character or a subformula in braces. In you case the braces are missing, so \aaa is expanded and the first token \l__xparse_processor_int of that expansion (coming from \makebold) causes problems.

However, if you load the amsmath package, \bar is redefined such that more care is taken when the token after it is expanded.

So the most simple solution is to just use braces: \bar{\aaa}. If you really don't want that, for whaterver reason, you can also redefine \bar to automatically add braces around its argument (and keep your fingers crossed that this doesn't break at other places/with other packages):

\let\oldbar\bar
\renewcommand\bar[1]{\oldbar{#1}}

• You also need to allow for the Unicode issue: \mathbf is the wrong command with unicode-math here, \symbf is needed. Mar 10 '17 at 7:11
• Just for further understanding of expansion, is there any feature that can instead wrap \aaa to cause \bar\aaa to be expanded as \mathaccent "7016\relax{\makebold a}, with the braces?
– Mass
Mar 10 '17 at 14:06
• @Mass If you mean to expand without the star, you have to redefine \aaa to \makebold a. Otherwise this is what already happens with the last to lines Mar 10 '17 at 14:44
• @JosephWright Ah, indeed, I hadn't thought to sanity-check whether \bar{\mathbf{a}} worked with unicode-math (it does not).
– Mass
Mar 10 '17 at 16:46