2

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?

1
  • 1
    Welcome to TeX.SE!
    – Mensch
    Commented Dec 6, 2021 at 12:11

1 Answer 1

5

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

2
  • Thank you very much, that's what I was looking for!
    – PolyPhys
    Commented Dec 6, 2021 at 13:17
  • 2
    @PolyPhys I added an improved version
    – egreg
    Commented Dec 6, 2021 at 13:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .