How can I allow a command to be used without braces around its argument, when the argument has an argument?

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.)

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}$.

• Why does _\v{f} work as a subscript instead of being interpreted as _{\v}{f}? Oct 10, 2011 at 23:29
• With your definition, \v has a pair of braces that will delimit the subscript; I prefer mine, but \newcommand{\v}[1]{{\vec{#1}}} will allow a syntax like _\v{f}. Oct 10, 2011 at 23:41
• I'm sure I just don't understand how expansion works yet, but when I see _\v{f}, I would think that expands to _{...contents of \v...}{f}. When/how does the {f} argument replace the #1 inside of \v? Oct 10, 2011 at 23:46
• The key is that, when TeX scans _, it expands the following token until finding something unexpandable before starting to typeset the subscript; so it really sees _{\vec{f}}. Oct 10, 2011 at 23:51
• When a macro is expanded it needs to look for its arguments and absorbs them. Oct 10, 2011 at 23:59

The simple answer here is going to be 'you cannot'. TeX is not a functional language, so \curl\v{F} is not 'curl of v of F'. \curl will absorb one token or 'balanced text' (braced group) as an argument. So \curl\v{f} ends up with \curl grabbing \v, then all sorts can go wrong.