8

Imagine the following MWE:

\documentclass{article}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}

\node [draw, circle, minimum width=1cm] at (0,0)  (n1) {n1};
\node [draw, circle, minimum width=1cm] at (3,-1) (n2) {n2};
\node [draw, circle, minimum width=1cm] at (4,0)  (n3) {n3};

\path [in = 270, out = 0] (n1) edge (n3);

\end{tikzpicture}
\end{document}

How can correctly control the path to go around node n2? I know there are various methods, using an auxiliary coordinate, or with controls, but I am not sure what would give a better result.

enter image description here

2
  • 2
    One possible approach for automation of this kind of situation was described by Andrew Stacey in tex.stackexchange.com/a/27996/3954. Apr 6, 2013 at 21:00
  • I was thinking in something that goes smoothly around n2 to reach n3. It does not have to be a perfect path.
    – cacamailg
    Apr 6, 2013 at 21:28

1 Answer 1

8

Below I show two possibilities; one using the .. controls .. syntax, and the other one, using the through point style implemented in Andrew Stacey's answer to Automatically connect nodes without overlapping other nodes or connections. Deciding which one gives better results will depend on several factors (and some of them subjective ones):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\makeatletter
% code by Andrew Stacey: https://tex.stackexchange.com/a/27996/3954
\tikzset{
  through point/.style={
    to path={%
      \pgfextra{%
        \tikz@scan@one@point\pgfutil@firstofone(\tikztostart)\relax
        \pgfmathsetmacro{\pt@sx}{\pgf@x * 0.03514598035}%
        \pgfmathsetmacro{\pt@sy}{\pgf@y * 0.03514598035}%
        \tikz@scan@one@point\pgfutil@firstofone#1\relax
        \pgfmathsetmacro{\pt@ax}{\pgf@x * 0.03514598035 - \pt@sx}%
        \pgfmathsetmacro{\pt@ay}{\pgf@y * 0.03514598035 - \pt@sy}%
        \tikz@scan@one@point\pgfutil@firstofone(\tikztotarget)\relax
        \pgfmathsetmacro{\pt@ex}{\pgf@x * 0.03514598035 - \pt@sx}%
        \pgfmathsetmacro{\pt@ey}{\pgf@y * 0.03514598035 - \pt@sy}%
        \pgfmathsetmacro{\pt@len}{\pt@ex * \pt@ex + \pt@ey * \pt@ey}%
        \pgfmathsetmacro{\pt@t}{(\pt@ax * \pt@ex + \pt@ay * \pt@ey)/\pt@len}%
        \pgfmathsetmacro{\pt@t}{(\pt@t > .5 ? 1 - \pt@t : \pt@t)}%
        \pgfmathsetmacro{\pt@h}{(\pt@ax * \pt@ey - \pt@ay * \pt@ex)/\pt@len}%
        \pgfmathsetmacro{\pt@y}{\pt@h/(3 * \pt@t * (1 - \pt@t))}%
      }
      (\tikztostart) .. controls +(\pt@t * \pt@ex + \pt@y * \pt@ey, \pt@t * \pt@ey - \pt@y * \pt@ex) and +(-\pt@t * \pt@ex + \pt@y * \pt@ey, -\pt@t * \pt@ey - \pt@y * \pt@ex) .. (\tikztotarget)
    }
  }
}

\makeatother


\begin{document}

\begin{tikzpicture}
\node [draw, circle, minimum width=1cm] at (0,0)  (n1) {n1};
\node [draw, circle, minimum width=1cm] at (3,-1) (n2) {n2};
\node [draw, circle, minimum width=1cm] at (4,0)  (n3) {n3};
\path [through point=(n2.east)] (n1) edge (n3);
\begin{scope}[xshift=6cm]
\node [draw, circle, minimum width=1cm] at (0,0)  (n1) {n1};
\node [draw, circle, minimum width=1cm] at (3,-1) (n2) {n2};
\node [draw, circle, minimum width=1cm] at (4,0)  (n3) {n3};
\draw (n1) .. controls ([yshift=-13pt]n2.south west) and ([yshift=-33pt]n2.south east) .. (n3);
\end{scope}
\end{tikzpicture}

\end{document}

enter image description here

1
  • The Andrew's code is in fact very good! I will keep it in mind for further usages. For now, and for the current case, it seems that your second approach does what I need, and is more pleasant to my eyes. Thanks!
    – cacamailg
    Apr 6, 2013 at 23:16

You must log in to answer this question.

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