# How can I draw a line to the vertices of an image in tikz

Basically what I am trying to achieve is the following: - Place 2 images horizontally side by side - Inside the left image (image_1) draw a small red rectangle - Connect the vertices of that rectangle with the vertices from the second image (image_2) that is to the right of image_1. This should be done by drawing a line with the same width as the small rectangle inside image_1

Optional: Using the same line width that was used for the small rectangle, draw a rectangle around image_2. That is, connect all vertices of image_2 with a line

The behind this is to show a zoom in image_1.

This is what I have so far: The small rectangle inside image_1

\begin{tikzpicture}

\node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[width=0.55\textwidth]{image_1.jpg}};
\begin{scope}[x={(image.south east)},y={(image.north west)}]
\coordinate (A) at (0.65,0.65);
\coordinate (B) at (0.78,0.65);
\coordinate (C) at (0.78,0.75);
\coordinate (D) at (0.65,0.75);
\draw[red,ultra thick] (A) -- (B) -- (C) --(D) -- cycle;
\end{scope}

\end{tikzpicture}


Can someone help?

• have you seen this tex.stackexchange.com/questions/25414 Dec 2 '14 at 18:05
• yeah but my second image is different from my first image. and i would like to have those lines Dec 2 '14 at 18:35

Something like this?

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{mwe}

\begin{document}
\begin{tikzpicture}

\node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[width=0.55\textwidth]{example-image}};
\node[anchor=south west,inner sep=0,right=5mm of image, draw=red, ultra thick] (image-b) {\includegraphics[width=0.55\textwidth]{example-image-a}};
\begin{scope}[x={(image.south east)},y={(image.north west)}]
\coordinate (A) at (0.65,0.65);
\coordinate (B) at (0.78,0.65);
\coordinate (C) at (0.78,0.75);
\coordinate (D) at (0.65,0.75);
\draw[red,ultra thick] (A) -- (B) -- (C) --(D) -- cycle;
\foreach \i/\j in {A/south west, B/south east, C/north east, D/north west}
\draw[red, ultra thick] (\i) -- (image-b.\j);
\end{scope}

\end{tikzpicture}
\end{document}


2nd version

To stop lines connecting corners at second image border, we need to find the intersection point between lines (B)--(image-b.south east) and (image-b.south west)--(image-b.north west) (and equivalent from C). This can be done with intersections cs.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,intersections}
\usepackage{mwe}

\begin{document}
\begin{tikzpicture}

\node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[width=0.55\textwidth]{example-image}};
\node[anchor=south west,inner sep=0,right=5mm of image, draw=red, ultra thick] (image-b) {\includegraphics[width=0.55\textwidth]{example-image-a}};
\begin{scope}[x={(image.south east)},y={(image.north west)}]
\coordinate (A) at (0.65,0.65);
\coordinate (B) at (0.78,0.65);
\coordinate (C) at (0.78,0.75);
\coordinate (D) at (0.65,0.75);
\draw[red,ultra thick] (A) -- (B) -- (C) --(D) -- cycle;
\end{scope}
\draw[red, ultra thick] (A) -- (image-b.south west);
\coordinate (aux) at  (intersection cs:first line={(B)--(image-b.south east)}, second line={(image-b.south west)--(image-b.north west)});
\draw[red, ultra thick] (B) -- (aux);
\draw[red, dashed, ultra thick] (aux) -- (image-b.south east);
\coordinate (aux) at -(intersection cs:first line={(C)--(image-b.north east)}, second line={(image-b.south west)--(image-b.north west)});
\draw[red, ultra thick] (C) -- (aux);
\draw[red,dashed,ultra thick] (aux)--(image-b.north east);
\draw[red, ultra thick] (D) -- (image-b.north west);

\end{tikzpicture}
\end{document}


• Great that's what I was looking for! By chance, do you know how to bring the second image to the front? Dec 2 '14 at 19:55
• @user2426316 To the front? Do you want lines stopping at second image border? Dec 2 '14 at 20:05
• Yes, that would be perfect Dec 2 '14 at 20:15
• @user2426316 I've updated my answer. Dec 2 '14 at 20:29