13

A noticeable downside to aesthetics in the \vec{} notation is that, for "short" and "tall" characters (a, i, v vs b, k, l), the overset vector notation shows difference in its height from the baseline. Is it possible to define (or redefine) overset characters to show a fixed, consistent height from the baseline (or any other horizontal standard), regardless of the character it is set upon? Below is a quick, crude comparison of the status quo and the objective:

Status Quo: Heights vary

Status Quo: Heights vary.

Objective: Heights are consistent

Objective: Heights are consistent.

ANOTHER EDIT: The customised vector comes from a script written by the user Todd Lehman:

\makeatletter
\newlength\xvec@height%
\newlength\xvec@depth%
\newlength\xvec@width%
\newcommand{\xvec}[2][]{%
  \ifmmode%
    \settoheight{\xvec@height}{$#2$}%
    \settodepth{\xvec@depth}{$#2$}%
    \settowidth{\xvec@width}{$#2$}%
  \else%
    \settoheight{\xvec@height}{#2}%
    \settodepth{\xvec@depth}{#2}%
    \settowidth{\xvec@width}{#2}%
  \fi%
  \def\xvec@arg{#1}%
  \def\xvec@dd{:}%
  \def\xvec@d{.}%
  \raisebox{.2ex}{\raisebox{\xvec@height}{\rlap{%
    \kern.05em%  (Because left edge of drawing is at .05em)
    \begin{tikzpicture}[scale=1]
    \pgfsetroundcap
    \draw (.05em,0)--(\xvec@width-.05em,0);
    \draw (\xvec@width-.05em,0)--(\xvec@width-.15em, .075em);
    \draw (\xvec@width-.05em,0)--(\xvec@width-.15em,-.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}%
  }}}%
  #2%
}
\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}}
  • 1
    How do you get the first picture? Surely not with \vec{a}\cdot\vec{b}. Can you please add you current code? – egreg Mar 11 at 16:59
  • I imported a .tex file that I found somewhere on TEX.SX a few months back, and I liked how it looked. Uploading now...Done! – Paul Kim Mar 11 at 17:05
11
+50

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

  • Would this be setting every vector height to the character A? Thanks for the advice! – Paul Kim Mar 11 at 16:37
  • 1
    The updated version lets you set the height to any character. – Sandy G Mar 17 at 2:17
4

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)--(\xvec@width-.05em,0);
    \draw (\xvec@width-.05em,0)--(\xvec@width-.15em, .075em);
    \draw (\xvec@width-.05em,0)--(\xvec@width-.15em,-.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

4

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.