# Connect two figures using lines and graphs

I have two figures that have different things in them(they are images using begin{figure}\includegraphics) and I would like to be able to draw some lines, probably using tiks, between them to show some relationships they have. I need the figures to remain where they are at though and no modification to their positions as it would screw the current correct alignment up.

It would be if I wanted to draw a line from some position in side the one picture to another using a mouse. Of course, I want nice lines like I would get from tikz nodes. I'll have to use trial and error to find the correct positions but I sort of want to "overlay" these lines on top of the images.

In fact, if I could just create a tikzpicture and some how set it origin to that of the top corner of the first image and overlay it on top, that would be ideal). (so it won't affect the positions of the images)

Basically it is sort of like annotating images and sets of images I already have to increase explanation without having to modify the images themselves.

Can this be done?

• I need the figures to remain where they are -- using a floating environment this can be a little bit tricky and in and addition, could lead to the connecting line running over text regions of your page – user31729 Feb 5 '17 at 9:55
• @ChristianHupfer That is ok because there is no text. It would be nice if I could somehow mark a figure that exists and then refer to it's coordinates in a tikzpicture. Or get the coordinates of a previous figure/image and use those in a tikzpicture to set the origin. All I need to 1. Have the coordinate system be bound to the image(so if it moves later on the coordinates move with it) and 2. Produce an overlay on it. Essentially use absolute positioning, but relatively to the coordinates.(setup an absolute coordinate system with origin that is locked to one of the images, then relative coord – AbstractDissonance Feb 5 '17 at 10:09
• Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. – user36296 Jan 10 at 19:53

You can add a tikzpicture with the overlay flag at the end of you graphics; this will not occupy any place (be careful with implicit end of line spaces) and then you can add whatever you want.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{graphics}
\usepackage{tikz}
\begin{document}
\begin{figure}
\centering
\includegraphics[width=0.4\linewidth]{example-image-a}
\begin{tikzpicture}[overlay,thick]
\draw [red] (0,0) grid (-5,4);
\draw [blue, ->] (-8, 3) to[bend right] (-2,0.5);
\end{tikzpicture}
\caption{}
\label{fig:}
\end{figure}
\end{document}


• Ok, but is there any way to set the coordinate system to to be fixed on one of the images so if they get shifted I won't have to update the coordinates? (I guess I could just use a coordinate transform, but I'd probably have to change it every time I modify any text in the document(at least before the images)). – AbstractDissonance Feb 5 '17 at 11:18
• like, If one could just get the absolute location that the image is ultimately placed on the page and use that, then I'd have a solution. e.g., insert some text in your answer and it will shift down the images... does your blue line and red grid remain in the same relative location to the images? I do not think they will, which is why I asked the question and is the crux of the problem. – AbstractDissonance Feb 5 '17 at 11:20
• If the overlay works fine because it is part of the figure and will always be positioned the same way unless the images change, then I'm ok and the solution works fine. I am using sub-figs though and I tried to copy and paste your solution. The overlay appears almost entirely off the page and I just see the edge of the red grid... – AbstractDissonance Feb 5 '17 at 11:22
• @AbstractDissonance: You need to run pdflatex twice. The mark made by \tikzmark will remain in the same place relative to the picture if the picture is moved. – Peter Grill Feb 5 '17 at 11:24
• @PeterGrill I'm not sure what you mean by tikzmark. First, that is't used in Rmano's example. Second, I think that can only be used to mark tikz stuff? Not figures? – AbstractDissonance Feb 5 '17 at 11:27

If you put the picture inside a node and name it you can access the positions in it. If you then have [remember picture] as an option to your tikzpicture you can access it from other tikzpictures. In the example below I have put two figure environments with a picture each. In each of then there are two tikzpictures, one with the image and one with the overlay stuff. (If the grid should be in the first or the second doesn't really change anything in this case, but if you for example put tick marks on it the bounding box will change, hence I put it in the overlay part.) With \usetikzlibrary{calc} you can do derivations of the coordinates. Here the green arrow starts in the upper right corner of Figure 1 and ends at coordinate (2,3) from the lower left corner in Figure 2. As long as you keep the two figures on the same page this should work.

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

\begin{document}
\lipsum[2]

\begin{figure}[htb]
\centering
\begin{tikzpicture}[remember picture]
\node[inner sep=0pt,anchor=south west] (A1) {\includegraphics[width=0.4\linewidth]{example-image-a}};
\end{tikzpicture}
%% Overlay part
\begin{tikzpicture}[remember picture,overlay]
\draw[red,thin] (A1.south west) grid (A1.north east);
\end{tikzpicture}
\caption{A first figure}
\label{fig:First}
\end{figure}

\lipsum[2]

\begin{figure}[htb]
\centering
\begin{tikzpicture}[remember picture]
\node[inner sep=0pt,anchor=south west] (A2) {\includegraphics[width=0.4\linewidth]{example-image-b}};
\end{tikzpicture}
%% Overlay part
\begin{tikzpicture}[remember picture,overlay]
\draw[red,thin] (A2.south west) grid (A2.north east);
\draw[green,->,line width=5pt] (A1.north east) to [bend right] ($(A2.south west)+(2,3)$);
\end{tikzpicture}
\caption{A second figure}
\label{fig:Second}
\end{figure}

\lipsum[3]
\end{document}


EDIT

To insert such coordinates in a large number of figures, the editing can be simplified. Instead of putting the graphics inside a tikzpicture a Tikz-coordinate can be set just in front of it. Then the coordinates for the actual picture is of course lost, but you can still set a grid over in an appropriate size. Below is a complement to the example above, containing two figures and one extra tikzfigure that sets the overlay stuff. This last tikzpicture can be put inside one of the figure environment or as here outside, the important is that all three are on the same page.

\begin{figure}[htb]
\centering
\tikz[remember picture,overlay] \coordinate (A3);
\includegraphics[width=0.4\linewidth]{example-image-a}
\caption{A third figure}
\label{fig:Third}
\end{figure}

\begin{figure}[htb]
\centering
\tikz[remember picture,overlay] \coordinate (A4);
\includegraphics[width=0.4\linewidth]{example-image-b}
\caption{A fourth figure}
\label{fig:Fourth}
\end{figure}

\begin{tikzpicture}[remember picture,overlay]
\begin{scope}[shift=(A4)]
\draw[thin,red] (0,0) grid +(6,5);
\end{scope}
\draw[fill=red] (A3) circle (2pt);
\draw[->,blue,thick] (A3) -- ($(A4)+(2,3)$);
\end{tikzpicture}


• It's too much work to redo all the figures and put them in tikzpictures – AbstractDissonance Feb 5 '17 at 13:24
• I think that with a decent editor it is relatively quick. Anyway, to have an easy way to include a coordinate in the figure environment (not the picture) see the edit. – StefanH Feb 5 '17 at 14:01

Below is the modification of @StefanH's solution. The end-points of the line should be located in percentage heights and widths of imported pictures/photos. That is, scaling of these imported pictures should not affect the correctness of the end point positions on both pictures. Moreover, if necessary, surround the connecting line with the white envelope (for the reason of clear visibility of it).

Code:

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

\newcommand\ptrscalef{1}        % width of the line
\newcommand\tipcirclesize{0.8pt}    % size of the ending dot

\begin{document}
\lipsum[2]

\begin{figure}[htb]
\centering
\begin{tikzpicture}[remember picture]
\node[inner sep=0pt] (nodeA) {\includegraphics[width=0.4\linewidth]{fortest.jpg}};
\coordinate (sApicture) at ($(nodeA.south west)!0.6!(nodeA.south east)$);   % 60% of the picture width
\coordinate (nApicture) at (sApicture|-nodeA.north);
\coordinate (cApicture) at ($(sApicture)!0.8!(nApicture)$);         % 80% of the picture height
\end{tikzpicture}
\caption{A first figure}
\end{figure}

\lipsum[2]

\begin{figure}[htb]
\centering
\begin{tikzpicture}[remember picture]
\node[inner sep=0pt] (nodeB) {\includegraphics[width=0.4\linewidth]{fortest.jpg}};
\coordinate (sBpicture) at ($(nodeB.south west)!0.2!(nodeB.south east)$);   % 20% of the picture width
\coordinate (nBpicture) at (sBpicture|-nodeB.north);
\coordinate (cBpicture) at ($(sBpicture)!0.5!(nBpicture)$);         % 50% of the picture height
\end{tikzpicture}
\caption{A second figure}

\begin{tikzpicture}[remember picture,overlay,
% white outline of black line
ptrpath/.style={double=black,color=white,double distance=\ptrscalef*0.6pt,line width=\ptrscalef*0.6pt},
% end-line dot
tip cover/.style={fill=black,circle,inner sep=0,minimum size=2*\tipcirclesize*\ptrscalef}
]
\draw [ptrpath] (cApicture) circle (\tipcirclesize*\ptrscalef) node [tip cover] {}
-- (cBpicture) circle (\tipcirclesize*\ptrscalef) node [tip cover] {};
\end{tikzpicture}
\end{figure}

\lipsum[3]
\end{document}