Setting fixed height (in all situations and characters) for overset notations

Here's a quick low-tech solution:

enter image description here

\documentclass{article}

\newcommand{\myvec}[1]{\vec{\vphantom{A}#1}}

\begin{document}

$\myvec{a}\cdot\myvec{b}$

\end{document}

This will adjust automatically for for sub/superscripts but has an issue (see below) if the vector symbol is above a character taller than A.

A slightly more complicated version using \ooalign will force the vector to be at a height as if it is above A even if it's placed over something taller.

enter image description here

The top line is the simple version above, where the third vector is out of allignment since A^2 is taller than A. The second line uses the code

\newcommand{\myvec}[1]{\ooalign{\hfil$\vec{\vphantom{A}}$\hfil\cr\hfil$#1$\hfil\cr}}

but will not adjust to subscript size changes. If you want, you can fix that using \mathchoice:

\newcommand{\myvec}[1]{\mathchoice
{\ooalign{\hfil$\vec{\vphantom{A}}$\hfil\cr\hfil$#1$\hfil\cr}}
{\ooalign{\hfil$\vec{\vphantom{A}}$\hfil\cr\hfil$#1$\hfil\cr}}
{\ooalign{\hfil$\vec{\vphantom{\scriptstyle A}}$\hfil\cr\hfil$\scriptstyle #1$\hfil\cr}}
{\ooalign{\hfil$\vec{\vphantom{\scriptscriptstyle A}}$\hfil\cr\hfil$\scriptscriptstyle #1$\hfil\cr}}
}

Note that I would avoid renewing the \vec command since the high arrows look odd to me when above short letters. Compare \myvec{a}\cdot\myvec{a} with \vec{a}\cdot\vec{a}.

Update:

As a final option (my favorite), one could define \myvec with 2 parameters: one optional. The required parameter is the printed letter with the arrow, the second, optional parameter is a character that sets the height of the arrow.

enter image description here

So \myvec[t]{a} would give you a vector over the a at the height of the t. Omitting the optional argument gives you the same result as \vec. The line above is produced from the code:

$\myvec{a}\cdot\vec{a}\quad\myvec[t]{a}\cdot\vec{t}\quad\myvec[j]{a}\cdot\vec{j}\quad\myvec[b]{a}\cdot\vec{b}\quad\myvec[A]{a}\cdot\vec{A}$

The code for \myvec is

\newcommand{\myvec}[2][]{\vec{\vphantom{#1}#2}}

and it will adjust for sub/superscripts as in $A_{\myvec[t]{a}\cdot\vec{t}}$.


The idea is to add a fixed character when you measure the height, I chose T. In case the object is higher, the arrow will adapt, as the (admittedly contrived) example shows.

The code for the arrow has been enhanced to allow for using it also in subscripts and superscripts.

\documentclass{article}
\usepackage{tikz}

\makeatletter
\newlength\xvec@height
\newlength\xvec@depth
\newlength\xvec@width
\def\xvec@dd{:}%
\def\xvec@d{.}%
\newcommand{\xvec}[2][]{{\mathpalette\xvec@{{#1}{#2}}}}
\newcommand{\xvec@}[2]{\xvec@@#1#2}
\newcommand{\xvec@@}[3]{%
  \settoheight{\xvec@height}{$\m@th#1 T#3$}%
  \settodepth{\xvec@depth}{$\m@th#1#3$}%
  \settowidth{\xvec@width}{$\m@th#1#3$}%
  \def\xvec@arg{#2}%
  \raisebox{.2ex}{\raisebox{\xvec@height}{\rlap{%
    \kern.05em%  (Because left edge of drawing is at .05em)
    \begin{tikzpicture}[scale=1]
    \pgfsetroundcap
    \draw (.05em,0)--(\[email protected],0);
    \draw (\[email protected],0)--(\[email protected], .075em);
    \draw (\[email protected],0)--(\[email protected],-.075em);
    \ifx\xvec@arg\xvec@d
      \fill(\xvec@width*.45,.5ex) circle (.5pt);
    \else\ifx\xvec@arg\xvec@dd
      \fill(\xvec@width*.30,.5ex) circle (.5pt);
      \fill(\xvec@width*.65,.5ex) circle (.5pt);
    \fi\fi
    \end{tikzpicture}%
  }}}%
  #3%
}
\makeatother

\let\stdvec\vec
\renewcommand{\vec}[1]{\xvec[]{#1}}

% --- Define \dvec and \ddvec for dotted and double-dotted vectors.
\newcommand{\dvec}[1]{\xvec[.]{#1}}
\newcommand{\ddvec}[1]{\xvec[:]{#1}}

\begin{document}

$\vec{a}\cdot\vec{b}$
$\dvec{a}\cdot\dvec{b}$
$\ddvec{a}\cdot\ddvec{b}$
$\dvec{\sum\limits_{k=1}^n a_k}$

$X_{\vec{a}+\vec{b}}$

\end{document}

enter image description here


Here is my approach:

The basic idea is to save the height of every vecs argument and then add a \rule of 0 width but with the height of the taller argument found inside every \vec with its real argument before really printed. (the expansion is takin place inside a \savebox and thus before really used the expression with all its \vecs the maximum height needed to be added is already calculated and been added.)

Extra features

-- a command for \vec (named \myVec) that is using the last height that have been found (actually the command that replaces the original \vec inside the basic command of my approach -named \mySHVecs-)

-- a pair of commands to save and restore the current value of the Maximum Height stored in order to be reusable (as many times as needed)

\documentclass{article}
\usepackage{amsmath}

\let\oldvec\vec
\makeatletter
\newsavebox\myVec@TempBox %Box for each vector
\newsavebox\myVec@Box %Box for all vectors
\def\max@Height{0pt}
\newcounter{myVecDepth}



% Comand For SameHeightVectors (mySHVecs):
\newcommand\mySHVecs[1]{%
\xdef\max@Height{0pt}
\def\calcMaxVec##1{%
\savebox\myVec@TempBox{\hbox{\ensuremath{##1}}}\ifdim\dimexpr\ht\myVec@TempBox>\max@Height\xdef\max@Height{\the\ht\myVec@TempBox}\fi}
\let\vec\calcMaxVec\savebox\myVec@Box{\vbox{\ensuremath{#1}}}\let\vec\myVec#1\let\vec\oldvec}




% The changed command of vec that uses the maximum height inside the above environment that can be used outside also to use the last maximum height available
\newcommand\myVec[1]{\oldvec{\rule{0pt}{\max@Height}\ensuremath{#1}}}
% Save and restore current height with optional argument to can be used for several saves/restores
\newcommand\saveVecH[1][\tempheight]{\xdef#1{\max@Height}}
\newcommand\useVecH[1][\tempheight]{\xdef\max@Height{#1}}
\makeatother




%User Command that makes the vectors Same Height
%\newcommand\mySHVecs[1]{\begin{SetVecsToHeight}\ensuremath{#1}\end{SetVecsToHeight}}


\begin{document}

Simple command for some variables:

\vspace{0.5cm}


\[\mySHVecs{\vec{a}\cdot\vec{b}\cdot\vec{F_x^2}}\]

\vspace{1cm}

Demonstration of mixing commands \verb|\mySHVecs|, \verb|\oldvec| and \verb|\myVec|:

\vspace{0.5cm}

$\mySHVecs{\vec{a}\cdot\vec{b}\cdot\vec{F_x^2}}-\dfrac{\oldvec{a}}{b}\cdot \myVec{S}=\myVec{0}$

\vspace{1cm}

Demonstration of saved and reused value of height:

\vspace{0.5cm}

\[
\mySHVecs{\vec{a}\cdot\vec{\dfrac{b}{3}}}\saveVecH+\cdot X_{\mySHVecs{\vec{a}+\oldvec{b}}}\cdot\mySHVecs{\useVecH\vec{F_1}}
\]

\end{document}

Output:

enter image description here