Using relative coordinates in TikZ to draw joined curves

One solution is as follows. On purpose, I added rows to the diagram, so that you can see one of the benefits of the loops. The output is

diagram

The code is (I hope it is self-explanatory) :

\documentclass{minimal}

\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{calc}

\begin{document}


\begin{tikzpicture}[>=latex']

\pgfmathsetmacro{\rows}{5}

\matrix[matrix of math nodes,column sep=2cm,row sep = 1cm] (m) {
A&B\\
S_2&S_1\\
C&D\\
S_4&S_3\\
E&F\\
};

\foreach \k in {1,...,\rows}{
    \coordinate (c\k) at ($(m-\k-1)!0.5!(m-\k-2)$);
}

\foreach \k [count=\kprev] in {2,...,\rows}{

    \ifodd \k
        \tikzset{dir/.style={<-}}
    \else
        \tikzset{dir/.style={->}}
    \fi

    \coordinate (c) at ($(c\kprev)!0.5!(c\k)$);
    \draw[dir] (m-\kprev-1) to[out=-45,in=180] (c) to[out=0,in=225] (m-\kprev-2);
    \draw[dir] (m-\k-1) to[out=45,in=180] (c) to[out=0,in=135] (m-\k-2);
}   

\end{tikzpicture}

\end{document}

Edit : fixed the code to correct for the arrows


You may try out following approach. Despite it also uses some magic constants, but having worked them out once (well, I did it for you :)), you don't need to alter them anymore. \coeffx and \coeffy are already gracefully set for you, it is left to adjust \nodedistancex and \nodedistancey as you wish.

\documentclass{minimal}

\usepackage{tikz}
\usetikzlibrary{arrows,positioning}

\def \coeffx{0.29}
\def \coeffy{0.91}
\def \nodedistancex{4cm}
\def \nodedistancey{1cm}
\def \offsetx{\nodedistancex*\coeffx}
\def \offsety{\nodedistancey*\coeffy}

\begin{document}

\begin{tikzpicture}[>=latex', node distance=\nodedistancey and \nodedistancex]
 \node (S1) {$S_1$};
 \node[left=of S1] (S2) {$S_2$};
 \node[above=of S2] (A) {$A$}; 
 \node[above=of S1] (B) {$B$};
 \node[below=of S2] (C) {$C$};
 \node[below=of S1] (D) {$D$};

 \draw[->,thick] (S1) .. controls +(-\offsetx,\offsety) and +(\offsetx,\offsety) .. (S2);
 \draw[->,thick] (S2) .. controls +(\offsetx,-\offsety) and +(-\offsetx,-\offsety) .. (S1);
 \draw[->,thick] (A) .. controls +(\offsetx,-\offsety) and +(-\offsetx,-\offsety) .. (B);
 \draw[->,thick] (D) .. controls +(-\offsetx,\offsety) and +(\offsetx,\offsety) .. (C);
\end{tikzpicture}

\end{document}

enter image description hereenter image description here

Tags:

Tikz Pgf