# ConTeXt : equivalent to LaTeX esvect

When using LaTeX, I used the esvect package, which has the following interesting features :

• very well lengthed vector arrows
• a lot of arrow versions
• a particular attention given to vectors with subscripts

I can't find any equivalent in ConTeXt. I've found this conversation, but the result doesn't suit me, especially with the subscript case. I tried to use directly esvect's code, but it seems to use LaTeX programming utilities (so it is not Plain TeX, and not usable as-is in ConTeXt). I've tried adapting it (the package is about 20 lines of code, so I thought it would be feasible), but since I'm not a TeX programmer at all, I quickly abandoned.

Is there a Plain TeX programmer who would be able to make such a small translation ? Or any other satisfying alternative ?

## EDIT

I understand that I poorly asked the question : my aim is to use the features of the esvect package with ConTeXt. I asked for a Plain TeX version because I (wrongly, as Henri Menke explained in the comments) thought it was usable as-is with ConTeXt, and that it might be easier to produce or reach more programmers. Henri Menke provided a perfectly working Plain TeX version, but since it isn't usable with ConTeXt, it is not what I seeked in the end. Is it possible to have a ConTeXt version of this package ? Since I am neither a TeX programmer nor a metapost user, I don't know what should be the best way to handle it...

## SECOND EDIT

Since @Aditya asked me to explain my problem in a more clear way, I made a comparison (with the Palatino font, since it is the one I use) of what I can acheive with esvect, what is with done basically by LaTeX and by ConTeXt. Below are the codes, then the snapshots, then the details :

%% LaTeX code
\documentclass[11pt]{article}
\usepackage[sc]{mathpazo}
\usepackage{multicol}
\usepackage{amsmath}
\usepackage[g]{esvect}
\begin{document}
\begin{multicols}{2}
esvect package
\begin{equation*}
\vv{F}_{\text{spring}\to M} = -k(x-\ell_{0})\vv*{u}{x}
\end{equation*}
\begin{equation*}
\text{correct } \vv{AB}, \vv{\imath}
\end{equation*}
\begin{equation*}
\vv*{u}{x} \text{ more adapted than } \vv{u}_{x}
\end{equation*}
\begin{equation*}
\end{equation*}

out of the box
\begin{equation*}
\vec{F}_{\text{spring}\to M} = -k(x-\ell_{0})\vec{u}_{x}
\end{equation*}
\begin{equation*}
\text{awful }\vec{AB}, \text{ correct } \vec{\imath}
\end{equation*}
\begin{equation*}
\vv*{u}{x} \text{ better arrow, worse spacing than } \vec{u}_{x}
\end{equation*}
\begin{equation*}
\end{equation*}
\end{multicols}
\end{document}

% ConTeXt code
\usemodule[simplefonts]
\definefontfeature[default][default][onum=yes]
\setupbodyfont[11pt,palatino]

\starttext
\startformula
\vec{F}_{\text{spring}\to M} = -k(x-\ell_{0})\vec{u}_{x}
\stopformula
\startformula
\text{correct } \vec{AB}, \vec{\imath}
\stopformula
\startformula
\text{neither } \vec{u}_{x} \text{ nor } \vec{u_{x}}
\stopformula
\startformula
\stopformula
\stoptext


Below is the result with LaTeX:

And below is the result with ConTeXT

Finally, what I seek in esvect can be summarized as this :

• On one-letter vectors, the arrow is a bit longer on both sides, and I find it much more readable
• On vectors with subscripts, it still makes arrows a bit longer, but also correct the spacing, so that the subscript is already under the arrow tip (I think the correction could be a bit stronger though, but I'm not thinking that this level of detail is important now)
• On multi-letters vectors, it behaves naturally as \overrightarrow, which ConTeXt does out of the box
• Some special cases such as the grad or ìmath are not perfect but acceptable in my opinion
• I prefer esvect's arrow look, but palatino's is fine by me

Maybe I could use TikZ to create my vector commands, but even if I have experience of TikZ I never did this kind of programming (I only drawed pre-defined plots).

• The esvect macro package provides a math font and defines macros that use symbols from that font. To use this in ConTeXt, you will need to define a virtual font where the esvect arrows are stylistic variants. There are some examples of virtual fonts in tex/context/fonts/... subdirectory, but they are for "standard" math fonts. I don't know enough about Opentype Math fonts to create mappings for new symbols. – Aditya Aug 16 '16 at 22:34
• The question of the arrow type is only marginal to me ; the style obtained with the default \vec command is not perfect, but fine by me. My real problem the problem of subscripted vectors, and I suppose (I may be wrong) that the macros don't depend on the arrow style. – A. Licari Aug 19 '16 at 8:59
• What do you mean by "problem of subscripted vectors" – Aditya Aug 20 '16 at 17:34

I managed to convert esvect to Plain TeX. However, I couldn't get it to work in ConTeXt.

\catcode@=11

\font\tenvec=vect10 at 10pt
\font\sevenvec=vect7 at 7pt
\font\fivevec=vect5 at 5pt

%% define new family
\newfam\vecfam
\textfont\vecfam=\tenvec
\scriptfont\vecfam=\sevenvec
\scriptscriptfont\vecfam=\fivevec

%% convert count register to hex
\def\thehex#1{\ifcase#1 0\or 1\or 2\or 3\or 4\or 5\or 6\or 7\or 8\or 9\or
A\or B\or C\or D\or E\or F\fi}

%% define symbols
\mathchardef\fldr="3\thehex\vecfam 12 % adjust here to select another arrow
\mathchardef\montraita="3\thehex\vecfam 20
\mathchardef\montraitd="3\thehex\vecfam 23

%% copied from esvect.sty (with little adjustments)
\def\relbareda{\mathrel{\mathpalette\mathsm@sh\montraita}}
\def\relbaredd{\mathrel{\mathpalette\mathsm@sh\montraitd}}
\def\vv{\futurelet\ifstar\dovv}
\def\dovv{\ifx*\ifstar\expandafter\vvstar\else\expandafter\ESV@vecteur\fi}
\def\vvstar*#1#2{\ESV@vecteur{#1}_{\mkern-1mu\relax#2}}
\def\ESV@vecteur{\mathpalette{\overvect@\vectfill@}}
\def\vectfill@{\traitfill@\relbaredd\relbareda\fldr}
\def\traitfill@#1#2#3#4{%
$\m@th\mkern2mu\relax#4#1\mkern-1.5mu%on met \relbaredd au d\'ebut \cleaders\hbox{$#4\mkern0mu#2\mkern0mu$}\hfill%remplit avec relbareda \mkern-1.5mu#3$%
}
\def\overvect@#1#2#3{\vbox{\ialign{##\crcr%
\noalign{\kern-.7pt\nointerlineskip}#1#2\crcr%
\noalign{\kern-.3pt\nointerlineskip}$\m@th\hfil#2#3\hfil$\crcr}}}

%% We'd need these from plain.tex if we would run in ConTeXt
%\def\m@th{\mathsurround\zeropoint}
%\def\mathsm@sh#1#2{\setbox\z@\hbox{$\m@th #1{#2}$}\finsm@sh}
%\def\finsm@sh{\ht\z@\z@ \dp\z@\z@ \box\z@}

\catcode@=12

%% Test:

Some examples from the manual:

$\vv{E}$, $\vv{AB}$, $\vv{\imath}$ and $\vv{u}$

$\vv*{e}{r}$ and $\vv*{L}{\Delta}$

$\vv{E}_{\vv{u}_{\vv{u}}}$

\bye


• It is probably because I don't understand anything about the Plain TeX / ConTeXt interface, but I thought Plain TeX code was directly usable in ConteXt or LaTeX. – A. Licari Aug 19 '16 at 13:49
• @A.Licari Well, ConTeXt inherently uses Unicode math fonts and all of its math mode setup is tailored towards this. This includes overloading primitves and adjusting font loading. If esvect were available as a Unicode math font you'd surely stand better chances. – Henri Menke Aug 19 '16 at 15:04

I finally used TiKZ to make my vector arrows and reached a result that suits me. This is not perfect but covers most of my usage cases - and I can add specific new cases when needed, now that I have understood the principle. The main problem is the management of superscripts, as @Aditya pointed out : it is neither natural (having a different command is not straightforward) nor perfect (I did not test many letters, and the results seems to depend on the immediate environment when the basic \vv command is used).

However, I wonder if it wouldn't be more ConTeXt-ish to use metapost instead of TikZ, and define these vector arrows as proper mathstackers / accents. My methods somehow feels like a tinkerer thing, more along the lines of Plain TeX philosophy than ConTeXt's (as far as I understand). I would prefer accepting an answer more along ConTeXt's usage that my own TikZ thing - and I would probably learn some more along the way !

\usemodule[simplefonts]
\definefontfeature[default][default][onum=yes]
\setupbodyfont[11pt,palatino,ss]
\usemodule[tikz]
\usetikzlibrary[arrows.meta]
\usetikzlibrary[calc]
\setuppapersize[A5][A5]

% Other stuff
\def\doDdiff[#1][#2]{  % différentielles et dérivées secondes - macro interne
\ifsecondargument {\frac{{\rm d^2} #1}{{\rm d} {#2}^2}}
\else {\rm d^2} \fi
}
\def\ddiff{\dodoubleempty\doDdiff}

% Vectors
\def\doVV[#1][#2]{
\ifsecondargument {\vvtwo{#1}{#2}} \else {\vvone{#1}} \fi
}
\def\vv{\dodoubleempty\doVV}

\def\vvone#1{
\tikz[baseline=(char.base),
-> /.tip = {Straight Barb[length=0.2em,width=0.5ex,slant=0.3]}]{
\def\vup{0.3ex}
\node[inner sep=0pt] (char) {$#1$};
\draw[->] ($(char.north west)+(0,\vup)$) -- ($(char.north east)+(0.1em,\vup)$);
}
}
\def\vvtwo#1#2{
\tikz[baseline=(char.base),
-> /.tip = {Straight Barb[length=0.2em,width=0.5ex,slant=0.3]}]{
\def\vup{0.3ex}
\node[inner sep=0pt, text opacity=0] (char) {$#1$};
\node[inner sep=0pt, anchor=base west] (full) at (char.base west) {$#1_{#2}$};
\draw[->] ($(char.north west)+(0,\vup)$) -- ($(char.north east)+(0.1em,\vup)$);
}
}
\def\vsuper[#1][#2]{
\tikz[baseline=(char.base),
-> /.tip = {Straight Barb[length=0.2em,width=0.5ex,slant=0.3]}]{
\def\vup{0.3ex}
\node[inner sep=0pt, text opacity=0] (char) {$#1$};
\node[inner sep=0pt, anchor=base west] (full) at (char.base west) {$#1^{#2}$};
\draw[->] ($(char.north west)+(0,\vup)$) -- ($(char.north east)+(-0.em,\vup)$);
}
}

\starttext

\startcolumns[2,rule=on]
My version
\startformula
\vv[F]_{\text{spring}\to M} = -k(x-\ell_{0})\vv[u][x]
\stopformula
\startformula
m \ddiff[{\vv[r]}][t] = \vv[F] + m\vv[g] + \vv[R]
\stopformula
\startformula
\vv[AB], \vv[\imath]
\stopformula
\startformula
\vv[u][x], \vv[u][y], \vv[u][z], \vv[u][X], \vv[u][Y], \vv[u][Z], \vv[u][X_{1}],
\vv[u][x_{1}]
\stopformula
\startformula
\vv[L][\Delta], \vv[J][\Delta], \vv[J][xx], \vv[u][x]
\stopformula
\startformula
\vsuper[v][2] \text{ means } \vv[v]\cdot\vv[v]
\stopformula
\startformula
\vv[v]',\vv[v']\text{ no } , \vv[v]' \text{ strangely works}
\stopformula
\startformula
\vsuper[v][\prime],\vsuper[v][\prime 2] \text{ ok}
\stopformula
\column

Out of the box vec
\startformula
\vec{F}_{\text{spring}\to M} = -k(x-\ell_{0})\vec{u}_{x}
\stopformula
\startformula
m \ddiff[\vec{r}][t] = \vec{F} + m\vec{g} + \vec{R}
\stopformula
\startformula
\vec{AB}, \vec{\imath}
\stopformula
\startformula
\vec{u}_{x}, \vec{u}_{y}, \vec{u}_{z}, \vec{u}_{X}, \vec{u}_{Y}, \vec{u}_{Z},     \vec{u}_{X_{1}},  \vec{u}_{x_{1}}
\stopformula
\startformula
\vec{L}_{\Delta}, \vec{J}_{\Delta}, \vec{J}_{xx}, \vec{u}_{x}
\stopformula
\startformula
\vec{v}^{2}  \text{ means } \vec{v}\cdot\vec{v}
\stopformula
\startformula
\vec{v}'
\stopformula
\stopcolumns
\stoptext


• What about when you also want to use superscripts? Is the output that you get with your solution the desired one? – Aditya Aug 24 '16 at 9:30
• I didn't think about this case. There is only one case where I guess I could use it, for the norm of the vector \vv[v]^2 ; in this case the ouput is really bad. I will try to adapt to this case as well and edit this answer. – A. Licari Aug 24 '16 at 10:43
• I edited my answer with the superscript question, it is indeed more complicated that what I thougth. – A. Licari Aug 25 '16 at 7:22
• While searching for a Metapost solution, I've found these two packages which permit changing the arrow heads : ctan.org/pkg/mparrows and tlcontrib.metatex.org/cgi-bin/package.cgi/action=view/id=791. I was wondering how I could try them in my standalone distribution. – A. Licari Aug 28 '16 at 7:05