Tilde below letter + subscript creates unwanted spacing

I use a tilde below a letter as a symbol for a vector. Redefining the \vec-command delivers a good output, but subscripts are a pain. I'm using the following code:

\documentclass[10pt,a4paper]{article}
\usepackage{amsmath}
\begin{document}
\renewcommand{\vec}[1]{{\underset{\sim}{#1}}}
$\vec v = \vec{e}_x + 2 \vec{e}_y + 3\vec{e}_z$
\end{document}


This produces the following output:

The space between the symbol and the tilde is too big and the subscript for the unit vectors is too low.

I'd like this better:

I already tried multiple variations, e.g. {\vec{e}}_x, but the problem persists.

Any ideas?

You maybe want to use accents:

\documentclass{article}
\usepackage{amsmath}
\usepackage{accents}

\newcommand{\ut}[1]{\underaccent{\tilde}{#1}}
\renewcommand{\vec}[1]{\ut{#1}}

\begin{document}

$\vec v = \vec{e}_x + 2 \vec{e}_y + 3\vec{e}_z$

\end{document}


• @jaytar On the other hand, there are better ways for denoting vectors, for instance with \mathbf or \bm. – egreg Aug 29 '17 at 9:59
• I guess that depends on personal preference. I also like bold font for vectors, but I have to use the tilde notation. This is especially helpful for tensors of different dimensions, so a one-dimensional tensor is a tilde, a two-dimensional tensor has two tildes etc. This doesn't work for bold as vector notation. – jaytar Aug 29 '17 at 10:02

A stackengine solution. The underset gap can be adjusted with the 1st argument to \stackengine.

\documentclass[10pt,a4paper]{article}
\usepackage{amsmath,stackengine}
\renewcommand{\vec}[1]{%
\smash{\ensurestackMath{\stackengine{1pt}{#1}{\scriptscriptstyle\sim}{U}{c}{F}{F}{S}}}
\vphantom{#1}
}
\begin{document}
$\vec v = \vec{e}_x + 2 \vec{e}_y + 3\vec{e}_z$
\end{document}


Try the same using undertilde package.

The code is as follows. It works for me perfectly.

\documentclass[10pt,a4paper]{article}
\usepackage{amsmath}
\usepackage{undertilde}
\begin{document}
$\utilde{v} = \utilde{e}_{x} + 2 \utilde{e}_{y} + 3\utilde{e}_{z}$
\end{document}


• Perfectly may be a bit of a stretch, considering the tilde collides with the subset x. – Joey Aug 29 '17 at 13:33