5

I have an overview image (let's call it A) of the task. The task is divided into two. I have the detailed description of each sub-tasks (let's call it B and C).

I am trying to put them together using TikZ. I am trying to generate something similar to this- enter image description here

See below the code snippet-

\documentclass{standalone}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows, positioning}

\tikzset{
  box/.style  = {draw, rectangle, minimum width=2cm, minimum height=2cm, line width=0.5mm},
  line/.style = {line width=0.5mm},
}

\begin{document}
\begin{tikzpicture}[node distance=2cm]
  \node (a) {\includegraphics[width=4cm]{image-a}};
  \node (b) [box, draw=green, fill=green!20, below of=a, yshift=-2cm] {\includegraphics[width=10cm]{image-b}};
  \node (c) [box, draw=orange, fill=orange!20, below of=b, yshift=-2cm] {\includegraphics[width=10cm]{image-c}};

  \coordinate[left of=a, xshift=-2cm] (t1);
  \coordinate[right of=a, xshift=2cm] (t2);

  \draw [line, draw=green] (a) -- (t1);
  \draw [line, draw=green] (t1) -- (b);

  \draw [line, draw=orange] (a) -- (t2);
  \draw [line, draw=orange] (t2) -- (c);
\end{tikzpicture}
\end{document}

The code produces the following output- enter image description here

Following are the issues with generated diagram-

  • The positioning of the lines. The inclined lines aren't preferred. Better to use vertical lines.
  • The orange line is going through image B, which doesn't look nice. It would be wise to keep it back.
  • Lots of manual positioning is done in the code in order to place these images.

How do I get the diagram similar to the first diagram? I would definitely prefer less manual positioning.

ravi
  • 1,618
  • Related: https://tex.stackexchange.com/questions/202649/automatically-include-a-sequence-of-images/202657#202657 –  Mar 06 '18 at 09:53
  • @Andrew: Thank you very much. I checked the reference you have provided. Unfortunately it is different from my question. In my question (a) I want to place photos (b) connect them with lines in certain fashion. Hope it help. – ravi Mar 06 '18 at 10:02
  • Note that your code doesn't actually work, those images are called example-image-a, not `image-a. You should also have a look at Difference between "right of=" and "right=of" in PGF/TikZ – Torbjørn T. Mar 06 '18 at 10:13
  • @TorbjørnT. : I already have image-a.pdf which is exactly same as example-image-a. I am sorry that this information wasn't provided. However the code works. – ravi Mar 06 '18 at 10:22

1 Answers1

6

For the positioning, you're loading the positioning library, but you're not using its features. Use e.g. below=of not below of=. The latter is deprecated, and with that the distance is measured between node centers. With below=of the distance is measured between node borders. See Difference between "right of=" and "right=of" in PGF/TikZ

For the lines, you can make use of perpendicular coordinates and path specifications, see TikZ: What EXACTLY does the the |- notation for arrows do? for explanation of that.

To place the red path behind the line, you can use the backgrounds library, and place the path on background layer.

If you want to avoid defining the t1 and t2 coordinates, you can replace the code for drawing the lines found below with this:

\draw [line, draw=green] (a) -| ([xshift=-1cm]a.west |- b.north);

\scoped[on background layer]
  \draw [line, draw=orange] (a) -| ([xshift=1cm]a.east |- c.north);

enter image description here

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{
  backgrounds, % <-- added
  positioning
}

\tikzset{
  box/.style  = {draw, rectangle, minimum width=2cm, minimum height=2cm, line width=0.5mm},
  line/.style = {line width=0.5mm},
}

\begin{document}
\begin{tikzpicture}[node distance=1cm] % changed 2cm to 1cm
  \node (a) {\includegraphics[width=4cm]{example-image-a}};

  \node (b) [box, draw=green,
             fill=green!20, below=of a % use below=of
             ] {\includegraphics[width=10cm,height=3cm]{example-image-b}};
  \node (c) [box, draw=orange, 
             fill=orange!20, below=of b] % below=of here as well
             {\includegraphics[width=10cm,height=3cm]{example-image-c}};

  % left=of/right=of, not left of=/right of=
  \coordinate[left=of a] (t1);
  \coordinate[right=of a] (t2);


  \draw [line, draw=green] (a) -| (t1 |- b.north);

\scoped[on background layer] % means that the following path is placed on the background layer
  \draw [line, draw=orange] (a) -| (t2 |- c.north);
\end{tikzpicture}
\end{document}
Torbjørn T.
  • 206,688
  • Thank you very much. One quick question. Do I really need point t1 and t2? Since you are using -| notation, so I think it would automatically find the intermediate point. – ravi Mar 06 '18 at 10:26
  • @RaviJoshi Sounds like you haven't quite understood -|, it is t1/t2 that defines the horizontal position of the corners of those paths, you need them for that. But you can remove them if you do something like \draw [line, draw=green] (a) -| ([xshift=-1cm]a.west |- b.north);. – Torbjørn T. Mar 06 '18 at 10:31
  • Very nice answer. +1 sincerely. – Sebastiano Mar 06 '18 at 10:43