65

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.

3
  • 4
    To make that clear: you want an extensible \overrightarrow with a smaller arrow tip?
    – mafp
    Commented May 15, 2013 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
    Commented May 15, 2013 at 22:51
  • 2
    This could also be relevant Commented May 16, 2013 at 16:22

3 Answers 3

21

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} \xshlongvec{\Omega M} \]
    \end{document}

a screen capture of the output includes A, ABC, xyz, and ΩM


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

4
  • 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
    Commented Oct 26, 2013 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. Commented Oct 26, 2013 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
    Commented Oct 26, 2013 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, Commented Oct 26, 2013 at 16:26
17

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.

7
  • 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
    Commented May 16, 2013 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
    Commented May 16, 2013 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
    Commented May 17, 2013 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
    Commented May 17, 2013 at 21:13
  • +1 Great solution. I wonder why nobody writes a package for longer arrows that mimick native arrows.
    – Pygmalion
    Commented Oct 22, 2013 at 13:56
4

I really like the esvect package. If you read the documentation (https://www.ctan.org/pkg/esvect), it tells you that you can select the type of arrow that you want. The default option is

\usepackage[d]{esvect}

Although you don't need to add the "d" in the preamble.

If you select option "h", then the arrow looks a little more like the one used in \vec.

1
  • MWE would be useful here to compare with the above.
    – Roly
    Commented Feb 8, 2021 at 20:24

You must log in to answer this question.

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