0

I'd like to draw two links between nodes with directions of arrowheads being opposite. Please see the picture below.

I tried something like:

\draw[->] ([yshift=0.1cm]1.east) -- ([yshift=0.1cm]2.west);
\draw[<-] ([yshift=-0.1cm]1.east) -- ([yshift=-0.1cm]2.west);

It works, but every time I draw, I have to know it is east, west, north, or south. Also, I need to write two lines for each pair.

Is there anyway like

\draw[somestyle] (1) -- (2);;

that works for not only horizontal links, but also vertical and diagonal links?

enter image description here

  • do you have a MME? – Black Mild Jun 28 at 20:18
1

This defines a style of this sort. It is based on this answer, and really works in this for circular nodes only.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{positioning,quotes}
\usetikzlibrary{calc}
\tikzset{shifted path/.style args={from #1 to #2 with label #3}{insert path={
let \p1=($(#1.east)-(#1.center)$),
\p2=($(#2.east)-(#2.center)$),\p3=($(#1.center)-(#2.center)$),
\n1={veclen(\x1,\y1)},\n2={veclen(\x2,\y2)},\n3={atan2(\y3,\x3)} in
(#1.{\n3+180+asin(\pgfkeysvalueof{/tikz/shifted path/dist}/\n1)}) edge[/tikz/shifted path/arrows,"#3"] (#2.{\n3-asin(\pgfkeysvalueof{/tikz/shifted path/dist}/\n2)})
}},back and forth/.style={/utils/exec=\pgfkeys{/tikz/shifted path/.cd,#1},
shifted path=from \pgfkeysvalueof{/tikz/shifted path/from} to \pgfkeysvalueof{/tikz/shifted path/to} with label \pgfkeysvalueof{/tikz/shifted path/label 1},
shifted path=from \pgfkeysvalueof{/tikz/shifted path/to} to \pgfkeysvalueof{/tikz/shifted path/from} with label \pgfkeysvalueof{/tikz/shifted path/label 2}},
shifted path/.cd,dist/.initial=3pt,arrows/.style={-stealth},from/.initial=1,to/.initial=2,label 1/.initial={},label 2/.initial={}}
\begin{document}

\begin{tikzpicture}
   \begin{scope}[every node/.style={draw, circle}]
    \path (0,2) node(1){1} (6,2) node(2){2}
     (0,0) node(3){3} (2,0) node(4){4}   (4,0) node(5){6} (6,0) node(6){6};
   \end{scope}
   \begin{scope}[]
   \draw[shifted path/arrows/.style={stealth-},
   back and forth/.list={{from=1,to=2,label 1=3,label 2=1},
    {from=1,to=3,label 1=2,label 2=5},
    {from=2,to=6,label 1=4,label 2=14},
    {from=3,to=4,label 1=8,label 2=12},
    {from=4,to=5,label 1=7,label 2=15},
    {from=5,to=6,label 1=11,label 2=17}}];
   \end{scope}  
\end{tikzpicture}
\end{document}

enter image description here

Please note that this very diagram can be conveniently produced with tikz-cd.

  • Of course this works for diagonal paths, too, e.g. \draw[shifted path/arrows/.style={stealth-}, back and forth/.list={{from=1,to=2,label 1=3,label 2=1}, {from=1,to=3,label 1=2,label 2=5}, {from=2,to=6,label 1=4,label 2=14}, {from=3,to=4,label 1=8,label 2=12}, {from=4,to=5,label 1=7,label 2=15}, {from=5,to=6,label 1=11,label 2=17}, {from=1,to=6,label 1=11,label 2=17}}];. – user121799 Jun 28 at 21:17
  • This is awesome! Thanks! – Chang Jun 28 at 21:23

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.