24

I want to create a figure with TikZ:

Wanted result

I tried first with the following code (remark: Complete MWE on bottom):

\begin{tikzpicture}
\node [block] (A) at (0,0) {\rule{2cm}{2cm}};
\node [block] (B) at (5,5) {\rule{2cm}{2cm}};

\path [draw,red, line width=3] (A) |-( B);
\path [draw,blue, line width=3] (A) |-( B);
\end{tikzpicture}

enter image description here

I created the two nodes A and B and draw the two pathes between them. But the 2nd one overwrites the first.

Then I tried to shift the 2nd path, but the the starting and end point are also shifted:

\begin{tikzpicture}
\node [block] (A) at (0,0) {\rule{2cm}{2cm}};
\node [block] (B) at (5,5) {\rule{2cm}{2cm}};

\path [draw,red, line width=3] (A) |-( B);
\path [draw,blue, line width=3,transform canvas={shift={(-0.2,-0.2)}}] (A) |-( B);  %   shift complete path
\end{tikzpicture}

enter image description here

Then I tried to modify the start and end point with degrees (example now with three lines):

\begin{tikzpicture}
\node [block] (A) at (0,0) {\rule{2cm}{2cm}};
\node [block] (B) at (5,5) {\rule{2cm}{2cm}};

\path [draw,red, line width=3] (A) |-( B);
\path [draw,blue, line width=3] (A.80) |-( B.190);  %set target with angle
\path [draw,green, line width=3] (A.70) |-( B.200); %set target with angle
\end{tikzpicture}

This looks good (see my first image), but it is try and error to get the right degrees. I the node shape changes, the distance between two pathes changes and I have to look for another degree.

\begin{tikzpicture}
\node [block] (A) at (0,0) {\rule{2cm}{5cm}}; %modified node
\node [block] (B) at (5,5) {\rule{2cm}{2cm}};

\path [draw,red, line width=3] (A) |-( B);
\path [draw,blue, line width=3] (A.80) |-( B.190);  %set target with angle
\path [draw,green, line width=3] (A.70) |-( B.200); %set target with angle
\end{tikzpicture}

enter image description here

My last attempt was using an offset for start and end position:

\begin{tikzpicture}
\node [block] (A) at (0,0) {\rule{2cm}{2cm}};
\node [block] (B) at (5,5) {\rule{2cm}{2cm}};

\path [draw,red, line width=3]  (A) |-( B);
\path [draw,blue, line width=3] (A.north)+(0.2,0) |-( B.west)+(0,-0.2);
\end{tikzpicture}

enter image description here

To use the offset I must decide the start orientation (north, west...). And in my example the starting point is ok, but the 2nd part of the path is not shifted.

I also found TikZ parallel Edges between Nodes, but I found now way to use it.

Is there an easier way to create parallel pathes? I don't want to need try and error methods to find the correct positions. My last try with the offset seems to be the logical version, but there is a problem with the end position.

Heres my MWE:

\documentclass[]{scrartcl}

\usepackage{tikz}
% ----------------------------------------------------------------
\begin{document}

\tikzstyle{block} = [draw=black,very thick, rectangle, rounded corners]

\section{Problem: Pathes are overwritten}
\begin{tikzpicture}
\node [block] (A) at (0,0) {\rule{2cm}{2cm}};
\node [block] (B) at (5,5) {\rule{2cm}{2cm}};

\path [draw,red, line width=3] (A) |-( B);
\path [draw,blue, line width=3] (A) |-( B);
\end{tikzpicture}

\section{Shift path}
\begin{tikzpicture}
\node [block] (A) at (0,0) {\rule{2cm}{2cm}};
\node [block] (B) at (5,5) {\rule{2cm}{2cm}};

\path [draw,red, line width=3] (A) |-( B);
\path [draw,blue, line width=3,transform canvas={shift={(-0.2,-0.2)}}] (A) |-( B);  %   shift complete path
\end{tikzpicture}

\section{Set point with degrees}
\begin{tikzpicture}
\node [block] (A) at (0,0) {\rule{2cm}{2cm}};
\node [block] (B) at (5,5) {\rule{2cm}{2cm}};

\path [draw,red, line width=3] (A) |-( B);
\path [draw,blue, line width=3] (A.80) |-( B.190);  %set target with angle
\path [draw,green, line width=3] (A.70) |-( B.200); %set target with angle
\end{tikzpicture}

\subsection{But with other nodes}
\begin{tikzpicture}
\node [block] (A) at (0,0) {\rule{2cm}{5cm}}; %modified node
\node [block] (B) at (5,5) {\rule{2cm}{2cm}};

\path [draw,red, line width=3] (A) |-( B);
\path [draw,blue, line width=3] (A.80) |-( B.190);  %set target with angle
\path [draw,green, line width=3] (A.70) |-( B.200); %set target with angle
\end{tikzpicture}

\section{Use offsets}
\begin{tikzpicture}
\node [block] (A) at (0,0) {\rule{2cm}{2cm}};
\node [block] (B) at (5,5) {\rule{2cm}{2cm}};

\path [draw,red, line width=3]  (A) |-( B);
\path [draw,blue, line width=3] (A.north)+(0.2,0) |- ( B.west)+(0,-0.2);
\end{tikzpicture}

\end{document}
% ----------------------------------------------------------------
5
  • Note that \path … (A.north)+(0.2,0) |-( B.west)+(0,-0.2); does not work because the line will be drawn only to B.west and than a move about 0.2 to the left will be happen. You need to do something like ([xshift=-0.2] B.west) instead. Mar 16, 2013 at 15:03
  • You can use the shorten option to avoid wrong start/end point, when shifting the path. Mar 16, 2013 at 17:13
  • @Qrrbrbirlbel I missed this syntax. I need a \path [draw,blue, line width=3] (A.north)+(0.2,0) |- ([yshift=-5] B.west); to get a good looking result. Do you have an explanation, why I need a value 5 to get a similar y-distance for my 0.2-valuy in x-direction? (If I use a fix value like 2mm there is no difference between x and y). You may make an answer of your comment. There are good chances, that is will be my accepted answer.
    – knut
    Mar 16, 2013 at 19:06
  • @knut The length <len> in [xshift=<len>] and [yshift=<len>] will not get automatically cm appended like in coordinates (but pt), so you need to write yshift=-0.2cm in that regard, sorry. I won’t add an answer, because basically this is the same thing what Bertfried Fauser did in his answer (see also my comments there). Mar 16, 2013 at 19:27
  • Even tough this post is old, I think that this answer is worth mentioning here: tex.stackexchange.com/a/103088/8650 May 28, 2016 at 3:06

3 Answers 3

11

Here are two ways to do this. The first uses an auxiliary node (C), as I was not able to group the calculation of coordinates. The second uses the calc package and does the computation in place. Color and distance are given in the foreach loop as pairs, so you have control where to place all lines.

\documentclass[]{scrartcl}

\usepackage{tikz}
\usetikzlibrary{calc}
% ----------------------------------------------------------------
\begin{document}
\tikzstyle{block} = [draw=black,very thick, rectangle, rounded corners]  
\section{Solution: shift lines with extra coordinate}
\begin{tikzpicture}
\node [block] (A) at (0,0) {\rule{2cm}{2cm}};
\node [block] (B) at (5,5) {\rule{2cm}{2cm}};
\foreach \s/\col in {0/red, 0.2/blue, 0.4/green}{
  \coordinate[yshift=-\s cm] (C) at (B.west);
  \path [draw,\col, line width=2] (A.north)+(\s cm,0) |- (C);
}
\end{tikzpicture}

\section{Solution: shift lines using calc package}
\begin{tikzpicture}
\node [block] (A) at (0,0) {\rule{2cm}{2cm}};
\node [block] (B) at (5,5) {\rule{2cm}{2cm}};
\foreach \s/\col in {0/red, 0.2/blue, 0.4/green}{
  \path [draw,\col, line width=2] (A.north)+(\s cm,0) |- ($(B.west)-(0,\s cm)$);
}
\end{tikzpicture}
\end{document}

If you want the lines centered just use in foreach \foreach \s/\col in {-0.2/red, 0/blue, 0.2/green}{...}

enter image description here

4
  • 1
    Without any auxiliary coordinates, you can do just: \path [draw,\col, line width=2] ([xshift=\s cm] A.north) |- ([yshift=-\s cm] B.west);, though I would use the \s values of -0.2,0,0.2 to “center” the three liens. Mar 16, 2013 at 17:31
  • Does the ([yshift=-\s cm] B.west) compile correctly? I tried such a thing (possibyl wrongly?) and got an error. After my code I said that you can use a symmetric set of parameters. Mar 16, 2013 at 19:00
  • Sorry, I missed the last part after your code segment. Maybe you had one space more in yshift or missed one after \s. \foreach \s/\col in {-0.2/red, 0/blue, 0.2/green} \path [draw,\col, line width=2] ([xshift=\s cm]A.north) |- ([yshift=-\s cm] B.west); works for me without a problem. Mar 16, 2013 at 19:21
  • Ok, thats then a cleaner solution that using spurious addition coordinates, like (C), or to use the (possibly time consuming calc) package. Mar 16, 2013 at 21:53
5

You could use a to path to do this. (I decided to have non-intersecting lines but if you want to intersect them, that should be trivial.)

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

\tikzset{double -|/.style={to path={%
    ($(\tikztostart)+(0,1pt)$) -| ($(\tikztotarget)+(-1pt,0)$)
    ($(\tikztostart)+(0,-1pt)$) -| ($(\tikztotarget)+(1pt,0)$)
}}}
\begin{document}
    \begin{tikzpicture}
       \draw (0,0) to[double -|] (1,1);
    \end{tikzpicture}
\end{document}
5
  • Interesting, I will check it. But I think, this is not the solution for me. In my real image, the paths will be continued to different directions - I will adapt my questions.
    – knut
    Mar 16, 2013 at 14:53
  • 1
    You could just do \draw[double, double distance=1pt] (0,0) -| (1,1); then. (Only caveat: the inner part will be drawn in white.) Mar 16, 2013 at 15:18
  • @Qrrbrbirlbel I had forgotten about that solution. As a matter of fact, I thought of a similar solution but decided not to post it because of the colour that's used to create the "gap". With different backgrounds the double line solution isn't that great.
    – user10274
    Mar 17, 2013 at 10:22
  • Can this answer be used to generate the requested figure? (I am asking, because I do not know enough to see it). Are there only two lines? can the color be set individually? Mar 18, 2013 at 1:50
  • @Hans-PeterE.Kristiansen There are two lines but I don't think that with a to path you can get different colours. Mind you, the solution is very ad hoc. I've been thinking of better ways to do this. I'll update the solution when I have something better.
    – user10274
    Mar 18, 2013 at 5:53
5

I have used postactions to draw two aditional lines, and shortened them to make the endpoints fit.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\tikzstyle{block} = [draw=black,very thick, rectangle, rounded corners]
\tikzstyle{mytriple} = [red, line width=3, postaction={transform canvas={shift={(0.2,-0.2)}}, draw, blue, line width=3, shorten >=0.2cm, shorten <=0.2cm},%
  postaction={transform canvas={shift={(0.4,-0.4)}}, draw, green, line width=3, shorten >=0.4cm, shorten <=0.4cm}]

\begin{tikzpicture}
  \node [block] (A) at (0,0) {\rule{2cm}{2cm}};
  \node [block] (B) at (5,5) {\rule{2cm}{2cm}};
  \draw [mytriple] (A) |- (B);
\end{tikzpicture}

\end{document}

triple

You must log in to answer this question.

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