Defining a new \Vec command to use with the esvect package

packagesrenewcommandvector

The esvect package uses the command \vv{v} to write an arrowhead above the letter in its argument (i.e., a vector), and the command \vv*{v}{n} to write a vector v with a subscript n. I would like to define a function \Vec so that \Vec{v} gives me \vv*{v}{}, while \Vec{v}_n gives \vv*{v}{n}.

In effect, I would like to use the esvect package in the same way as I would use the ordinary \vec command. Is this possible?

Best Answer

You can use the e (embellishment) argument specifier for \NewDocumentCommand:

\documentclass{article}
\usepackage{esvect}

\NewDocumentCommand{\Vec}{me{_}}{%
  \IfNoValueTF{#2}{\vv{#1}}{\vv*{#1}{#2}}%
}

\begin{document}

With the standard \verb|\vv|
\[
\vv{v}+\vv*{v}{n}
\]

With the new \verb|\Vec|
\[
\Vec{v}+\Vec{v}_n
\]

\end{document}

enter image description here

Even better, you can avoid the small hole that's left by \vv*:

\documentclass{article}
\usepackage{amsmath}
\usepackage{esvect}

\RenewDocumentCommand{\Vec}{me{_}}{%
  \IfNoValueTF{#2}{\vv{#1}}{\vv*{#1}{\mspace{-2mu}#2}}%
}

\begin{document}

\begin{gather*}
\Vec{v}+\Vec{v}_n \\
\Vec{x}+\Vec{x}_n \\
\Vec{p}+\Vec{p}_n \\
\Vec{y}+\Vec{y}_n \\
\end{gather*}

\end{document}

Note that you need \RenewDocumentCommand{\Vec} if amsmath is loaded (with amsmath, \Vec is an alias for \vec).

enter image description here