# In Tikz, how to write equations on the top of foreach arrow?

May you provide some guidance on how to add some descriptions on the arrow of the following two figures? For example,

### In the first figure,

• can we add: Arrow $\vec{21}$ on the top of arrow (better to be (1) parallel or (2) horizontal if possible, but not necessary if it is too difficult for you) from the item 2 to the item 1?

• Similarly, can we add: Arrow $\vec{i j}$ on the top of each arrow from the item i to the item j? Here i=2, and j=1,3,4,5,6.

### In the second figure,

• can we try to add some greek alphabet, $\alpha$, $\beta$, $\gamma$, etc on each of the arrow?

Can we make the above alignment to have the choices of being (1) parallel to the arrow or (2) horizontal to the document? If so, could you advise the command lines for each case? Here is my Minimal Working note MWE:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{matrix}

\usetikzlibrary{shapes,arrows,calc}
\usepackage{multicol}
\usepackage{multirow}
\usepackage{amsmath, amsthm, amssymb,slashed,mathtools,tabu}

\begin{document}

\newline

\;\;\;\;
\begin{tikzpicture}\kern-5mm[>=stealth,->,shorten >=2pt,looseness=.5,auto]
\matrix (M)[matrix of math nodes,row sep=1cm,column sep=8mm]{
1 & 2 & 3\\
4    &  5 & 6 \\
};
\foreach \a/\b in {1-2/2-1,1-2/1-1,1-2/2-2,1-2/2-3, 1-2/1-3}
{\draw[thick,->](M-\a)--(M-\b);}
\end{tikzpicture}

\newline

\begin{tikzpicture}[node distance=2cm, auto]

\def\mypoints{%
(1, 0), (2, 0), (3, 0), (4, 0), (5, 0),
(5, 1), (4, 1), (3, 1), (2, 1), (1, 1)%
};
\path
\foreach \x [count=\xi] in \mypoints {
\x node[circle, fill, inner sep=sqrt(2)*0.025cm] (node\xi) {}
}
\foreach \x [count=\xi, remember=\xi-1 as \xiprev] in \mypoints {
\ifnum\xi>1 %
(node\xiprev) edge[-latex', black!50!white] (node\xi)
\fi
}
;
\end{tikzpicture}

\end{document}


The second figure is from and many thanks to https://tex.stackexchange.com/a/385800/41144

• As I understand it, you want each arrow to have an equation written next to it and that equation to be parallel to the arrow. Is that correct? If not, can you add a freehand drawing of the desired result? Jul 30 '20 at 16:34
• Dear AndreC, the attempt output of the user is good (but I am not sure the file is compilable...) Jul 30 '20 at 17:08
• (maybe you can try to compile of user213378 file and you will see the issue...) thank you! Jul 30 '20 at 17:10
• It can be compiled with asymptote as it gave the code, otherwise, with LaTeX, you have to add \usepackage[inline]{asymptote} and compile successively this way pdflatex->asymptote->pdflatex. Jul 30 '20 at 17:11
• look here and use \begin{asy} Using Asymptote with pdfLaTeX Jul 30 '20 at 17:27

TikZ can probably do it !

While waiting for an TikZ answer, I have an Asymptote answer as following:

Only for compare purpose!

usepackage("esvect");
picture pic1,pic2;

size(pic1,8cm);
pair[] A={(0,0),(-1,0),(1,0),sqrt(2)*dir(-135),(0,-1),sqrt(2)*dir(-45)};
int[] B={2,1,3,4,5,6};
margin m=Margin(2.5,2.5);
for (int i=0; i< A.length; ++i){  label(pic1,"$"+ (string) B[i] + "$",A[i],(0,0)); }
draw(pic1,Label("$\vv{21}$"),A--A,Arrow,m);
draw(pic1,Label("$\vv{23}$",LeftSide),A--A,Arrow,m);
draw(pic1,rotate(degrees(dir(A-A)))*Label("$\vv{24}$"),A--A,Arrow,m);
draw(pic1,rotate(degrees(dir(A-A)))*Label("$\vv{25}$"),A--A,Arrow,m);
draw(pic1,rotate(degrees(dir(A-A)))*Label("$\vv{26}$",LeftSide),A--A,Arrow,m);

size(pic2,10cm);
pair[] A={(0,0),(1,0),(2,0),(3,0),(4,0),(4,1),(3,1),(2,1),(1,1),(0,1)};
string[] alphabet={"$\alpha$", "$\beta$", "$\gamma$"};
alphabet.cyclic=true;
dot(pic2,A);
for (int i=0; i< A.length-1; ++i){
draw(pic2,scale(0.8)*alphabet[i],A[i]--A[i+1],Arrow,Margin(1,1));
}
picture pic;
shipout(bbox(pic,2mm,invisible)); • +1 I think he means alpha, beta, gamma, delta, epsilon, which is the Greek alphabet. Jul 30 '20 at 17:04
• That is not a problem, +1 - I think this is already a great nearly perfect answer! Jul 30 '20 at 17:04
• excuse me, however, how do we use usepackage("esvect"); and why not \usepackage("esvect"); ??? Jul 30 '20 at 17:06
• @wonderich You can see here overleaf.com/6581438472kbmkvdwtwbvk
– user213378
Jul 30 '20 at 17:32
• @wonderich To answer for why do we use usepackage("esvect");, see page 53 of the official documetation.
– user213378
Jul 30 '20 at 17:41

With TikZ:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta,
chains,
matrix,
positioning,
quotes}
\usepackage{esvect}

\begin{document}
\begin{tikzpicture}[
every edge/.style = {draw, thick, -Straight Barb},
every edge quotes/.style = {auto, inner sep=2pt, sloped}
]
\matrix (m) [matrix of math nodes,
nodes={circle, minimum size=1.1em, inner sep=1pt, anchor=center},
column sep=2cm,
row sep=2cm]
{
1 & 2 & 3     \\
4 & 5 & 6     \\
};
\foreach \i/\j/\k in {1/1/1, 4/2/1, 5/2/2, 6/2/3, 3/1/3}
{
\draw   (m-1-2) edge ["$\vv{21}$"] (m-\j-\k);
}
\end{tikzpicture}

\begin{tikzpicture}[
node distance = 2cm and 2 cm,
start chain = going right,
dot/.style = {circle, fill, inner sep=2pt},
every edge/.style = {draw=gray, thick, -Straight Barb},
every edge quotes/.style = {auto, inner sep=2pt, sloped}
]

\foreach \i in {1,...,5}
{
\node (n1\i) [dot, on chain] {};
\node (n2\i) [dot, above=of n1\i] {};
}
\foreach \x [count=\xi,count=\xj from 2] in {\alpha, \beta, \gamma, \alpha}
\ifnum\xj<6     \draw (n1\xi) edge["$\x$"] (n1\xj);   \fi
\foreach \x [count=\xi,count=\xj from 2] in {\gamma, \beta, \alpha,  \gamma}
\ifnum\xj<6     \draw (n2\xj) edge["$\x$"] (n2\xi);   \fi
\draw (n15) edge["$\beta$"] (n25);

\end{tikzpicture}
\end{document}

• thanks! this looks nearly perfect!!! I am learning it. ++1 Aug 1 '20 at 19:14