[Tex/LaTex] How to allow a command to be used without braces around its argument, when the argument has an argument

expansionmacros

I've defined a couple simple macros to make my life a little easier:

\newcommand{\curl}[1]{\ensuremath{\nabla\times #1}}
\renewcommand{\v}[1]{{\ensuremath{\vec{#1}}}}

This way, in text mode I can write \v{F} or \curl{\v{F}}, and all is well.

But for simplicity, at least in math mode, I'd like to be able to just write \curl\v{F}, and have it expand to

\ensuremath{\nabla\times {\ensuremath\vec{F}}}

just like \curl{\v{F}}. Since I've defined \curl in a pretty simple manner, this currently looks right. However, I think I'm actually getting a stranger behavior: if I change the definition of \curl to have parentheses, like {(\ensuremath{\nabla\times #1})}, then the vector symbol shows up over the closing parenthesis, as if the \v (but not its argument) is being captured as an argument to \curl.

Strangely, though, simply typing \ensuremath{(\nabla\times {\ensuremath\vec})} results in errors.

What's going on here? Is there a good way to do this, and is it a good idea?

(By the way, the reason I have an extra pair of {} in the definition of \v is that it allows me to use \int_\v{x} rather than \int_{\v{x}}. This much seems to work, but I'm not really sure why.)

Best Answer

You can do that in normal math mode if you define

\newcommand{\curl}{\nabla\times}
\newcommand{\v}{\vec}

Then \curl\v{F} will do exactly what you need but, unfortunately, not for subscripts.

It's possible to make this work for subscripts, actually, but I don't think it's worthy using it (besides it's wrong because it fixes spaces when not used for a subscript):

\makeatletter
\def\curl{\bgroup\@ifnextchar\v\@curlv\@curl}
\def\@curlv#1#2{\nabla\times\vec{#2}\egroup}
\def\@curl#1{\nabla\times #1\egroup}
\makeatother

Now $\int_\curl X$ or $\int_\curl\v{F}$ will work. But only if \v follows \curl, not something like \curl\mathbf{X}. So this is error prone and not recommendable at all.

Your overuse of \ensuremath is wrong: it doesn't add to typing speed being able to write \curl\v{F} instead of $\curl\v{F}$.