41

Instead of changing to \overrightarrow{}, which uses a usual extensible arrow and puts it above the argument, I would like to get an extensible \vec{} symbol. My intention is to obtain a new command \vect{}, for instance, which is exactly the same as \overrightarrow{} but the head is the \vec symbol (and the extensible part is done with the aproppiate “\relbars”, which I don't know).

In my case I'm using kpfonts package.

\documentclass{scrartcl}
\usepackage{kpfonts}
\newcommand*{\vv}[1]{\vec{\mkern0mu#1}}
\begin{document}
\[
    \vec u,\ \vv u,\ \vv{OA},\ \vv{BC},\ \overrightarrow{AB}
\]
\end{document}

I defined the \vv{} command because the usual \vec gives you a misaligned arrow.

enter image description here

EDIT: As @GonzaloMedina pointed, there exists the esvect package. But, in my opinion, it doesn't provide any arrow with a head matching the style of kpfonts.

  • 2
    To make that clear: you want an extensible \overrightarrow with a smaller arrow tip? – mafp May 15 '13 at 22:13
  • @mafp More or less. That would be a cool step forward. But the last intention is not to get an smaller arrow tip but the \vec arrow tip. – Manuel May 15 '13 at 22:51
  • 2
    This could also be relevant – Peater de Xel May 16 '13 at 16:22
17

Here I introduce \xshlongvec that takes an optional and mandatory argument. The mandatory is the text over which to lay the \vec. The optional is a number which represents the rightward kern (in points) applied to the vec arrow, since as the OP points out, the kerning of \vec is not the best.

In this MWE, I use the kpfonts mentioned by the OP. I first give the unaltered \vec followed by two instances of \xshlongvec. The one flaw that I note is that the left end of the arrow is not slanted like \vec, but is square as the result of being created by a \rule.

Thanks to Phillipe for pointing out a deficiency in the original answer, since repaired.

\documentclass[12pt]{scrartcl}
\usepackage{kpfonts}
\usepackage{stackengine}
\usepackage{calc}
\newlength\shlength
\newcommand\xshlongvec[2][0]{\setlength\shlength{#1pt}%
  \stackengine{-5.6pt}{$#2$}{\smash{$\kern\shlength%
    \stackengine{7.55pt}{$\mathchar"017E$}%
      {\rule{\widthof{$#2$}}{.57pt}\kern.4pt}{O}{r}{F}{F}{L}\kern-\shlength$}}%
      {O}{c}{F}{T}{S}}
\begin{document}
\centering
\[ \vec{A} \quad \xshlongvec[1]{ABC} \quad \xshlongvec[1]{xyz} \]
\end{document}

enter image description here


This EDITED solution below works across math styles, by using the scalerel package.

\documentclass[12pt]{scrartcl}
\usepackage{kpfonts}
\usepackage{stackengine,scalerel}
\usepackage{calc}
\newlength\shlength
\newcommand\xshlongvec[2][0]{\ThisStyle{\setlength\shlength{#1\LMpt}%
  \stackengine{-5.6\LMpt}{$\SavedStyle#2$}{\smash{$\kern\shlength%
    \stackengine{\dimexpr 1.3pt+6.25\LMpt}{$\SavedStyle\mathchar"017E$}%
      {\rule{\widthof{$\SavedStyle#2$}}{\dimexpr.1pt+.5\LMpt}\kern.4\LMpt}{O}{r}{F}{F}{L}\kern-\shlength$}}%
      {O}{c}{F}{T}{S}}}
\begin{document}
\centering
\[ \vec{A} \quad \xshlongvec[1]{ABC} \quad \xshlongvec[1]{xyz} \quad \xshlongvec{\Omega M} \]
\[ \scriptstyle \vec{A} \quad \xshlongvec[1]{ABC} \quad \xshlongvec[1]{xyz} \quad \xshlongvec{\Omega M}\]
\[ \scriptscriptstyle \vec{A} \quad \xshlongvec[1]{ABC} \quad \xshlongvec[1]{xyz} \quad \xshlongvec{\Omega M} \]
\end{document}

enter image description here

  • I can't check it right now since I don't have MacTeX installed and I don't have enough time now. But it looks perfect. By the way, a question I have: what is the correct way of working: \vec{A} or \vec{\mkern0mu A}. Is that a bug? – Manuel Oct 26 '13 at 14:58
  • @Manuel I'm not sure I understand your question. In the definition of \xshlongvec, I have the letters pt which forces the optional argument number to be converted to a length in pts. That is later paired with a \kern. If you removed the letters pt from the definition, then you would instead attach the kerning units to your optional argument. If you wanted those kerning units to be in mus then the \kern would have to be changed to a \mkern. – Steven B. Segletes Oct 26 '13 at 15:04
  • Sorry, I explained quite bad. I'm not talking about \xshlongvec, as I said, I have no option to try and see it now. I'm referring to the \vvcommand I defined in the question: I defined the \vv{} command because the usual \vec gives you a misaligned arrow. Is THAT a bug, or is it just me that see things “wrong”? – Manuel Oct 26 '13 at 15:45
  • @Manuel I don't know for sure if its a "bug", but I agree with you that \vec does not always put the arrow where I want it. I think the problem is compounded when you change fonts, because (I'm believing) that the arrow adjustment done by \vec is not font-dependent. Thus, I'm guessing that \vec will do even worse with a changed font than with the standard font. For this reason, I too put the kerning code into \xshlongvec, and left it as an optional argument, – Steven B. Segletes Oct 26 '13 at 16:26
13

The \vec command uses \mathchar"017E as the arrow, while \overrightarrow uses \mathchar"3221. Both look like scaled versions of each other to me. So we can replicate \overrightarrow with the arrow tip of \vec. I propose two versions, a LaTeX one, and one following the amsmath route. For the amsmath version I give another variant that scales the minus horizontally to make it thinner.

\documentclass{article}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{kpfonts}
\newcommand*{\vv}[1]{\vec{\mkern0mu#1}}


\makeatletter
\newcommand{\vect}[1]{%
  \vbox{\m@th \ialign {##\crcr
  \vectfill\crcr\noalign{\kern-\p@ \nointerlineskip}
  $\hfil\displaystyle{#1}\hfil$\crcr}}}
\def\vectfill{%
  $\m@th\smash-\mkern-7mu%
  \cleaders\hbox{$\mkern-2mu\smash-\mkern-2mu$}\hfill
  \mkern-7mu\raisebox{-3.81pt}[\p@][\p@]{$\mathord\mathchar"017E$}$}

\newcommand{\amsvect}{%
  \mathpalette {\overarrow@\vectfill@}}
\def\vectfill@{\arrowfill@\relbar\relbar{\raisebox{-3.81pt}[\p@][\p@]{$\mathord\mathchar"017E$}}}

\newcommand{\amsvectb}{%
  \mathpalette {\overarrow@\vectfillb@}}
\newcommand{\vecbar}{%
  \scalebox{0.8}{$\relbar$}}
\def\vectfillb@{\arrowfill@\vecbar\vecbar{\raisebox{-4.35pt}[\p@][\p@]{$\mathord\mathchar"017E$}}}
\makeatother

\begin{document}
\begin{align}
  \mathchar"017E, \mathchar"3221\\
  \vec u,\ \vv u,\ \vv{OA},\ \vv{BC},\ \overrightarrow{AB} \\
  \vect{u}, \vect{OA}, \vect{BC}, \vect{AB}\\
  \amsvect{u}, \amsvect{OA}, \amsvect{BC}, \amsvect{AB}\\
  \amsvectb{u}, \amsvectb{OA}, \amsvectb{BC}, \amsvectb{AB}
\end{align}
\end{document}

sample output

Note that this kind of building extensible accents with minus signs and short arrows shows the typical LaTeX weakness that those accents render oddly on certain resolutions. The raise parameter -3.81pt is the best approximation I found to mitigate this problem. You might want to fiddle with it to get better results. Also, the third variant with scaled minus has yet another parameter (the 0.8 scaling factor) that probably needs fine-tuning.

The vertical space between the arrows and the letters is governed by the use of \overarrow@ from amsmath, so the space you get is the same as for \overrightarrow. If we want it a little lower, we make a new version of \overarrow@ and use that in \amsvect and \amsvectb:

\makeatletter
\def\my@overarrow@#1#2#3{\vbox{\ialign{##\crcr #1#2\crcr \noalign{\kern-\p@\nointerlineskip}$\m@th \hfil #2#3\hfil $\crcr}}}

\newcommand{\amsvect}{%
  \mathpalette {\my@overarrow@\vectfill@}}
\def\vectfill@{\arrowfill@\relbar\relbar{\raisebox{-3.81pt}[\p@][\p@]{$\mathord\mathchar"017E$}}}

\newcommand{\amsvectb}{%
  \mathpalette {\my@overarrow@\vectfillb@}}
\newcommand{\vecbar}{%
  \scalebox{0.8}{$\relbar$}}
\def\vectfillb@{\arrowfill@\vecbar\vecbar{\raisebox{-4.35pt}[\p@][\p@]{$\mathord\mathchar"017E$}}}
\makeatother

The magic number here is the \kern-\p@ in \my@overarrow, which pushes the arrow one point deeper. Again, this is open for fine tuning. Note that this is the same value that is already used in \vect.

  • Well, that's an improvement, but as you say (1) that hand approximation is not perfect and (2) the \relbars used in both solutions are thicker than the tail of the \vec arrow. Those two were my main objectives (correct thickness of the relbars and correct positioning). I suspect the latter (positioning) is far more difficult. Thank you for your improvementent. – Manuel May 16 '13 at 18:53
  • @Manuel Ad (2): I added a way to reduce the thickness of the minus signs. But the hand tuning problem stays, of course. – mafp May 16 '13 at 23:29
  • 1
    After trying a little I ended with -4.5pt which I think adapts better (in the \amsvectb). By the way, I would like the \amsvectb to be lower (a little nearer to the letters of the argument). After that, if nobody cames up with a better solution I accept your answer. – Manuel May 17 '13 at 20:44
  • @Manuel OK, if this fits better. The real test is probably a high DPI printout, but currently I have no working printer around here. – mafp May 17 '13 at 21:13
  • +1 Great solution. I wonder why nobody writes a package for longer arrows that mimick native arrows. – Pygmalion Oct 22 '13 at 13:56

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.