# How to change the line width of the lines connecting spies and the spied region

I was experimenting with the spy library. It worked quite well, except for influencing the line width of the spy connections when using the connect spies keyword, or the width of the outline of the region to be spied on. I tried to insert keywords like ultra thick or line width=3mm everywhere, but it has no effect. The manual only states "Causes the spy-in and the spy-on nodes to be connected by a thin line." for the connect spies keyword, but no options for thick lines. I also looked through every question here on tex.sx concerning the spy library but did not find anything.

So is it even possible to have some options passed to these lines (probably dashed and other things also might be useful), and if, how?

Here is my code so far:

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{spy,calc}

\begin{document}

\begin{tikzpicture}[spy using outlines={circle, size=2cm, connect spies}]
%\includegraphics[scale=1]{test.png}
\node[above right, inner sep=0] at (0,0) {\includegraphics{test.png}};
\draw[very thin, yellow] (0,0) grid (15.25,10);
\spy[magnification=3,red,draw,line width=1mm] on (2,2) in node[ultra thick] at (3,-2);
\spy[magnification=5,green] on (7.1,4.1) in node[line width=4mm] at (8,-2);
\spy[magnification=10,black] on (11.8,2.8) in node[dashed,line width=1mm] at (13,-2);
\end{tikzpicture}

\end{document}


And finally, the original picture if you want to use it for experimenting:

-

The key spy connection path is used to draw the connection between the spy in and spy on nodes. It is executed once both nodes have been created. By using the connect spies style, the spy connection path key is set to \draw[thin] (tikzspyonnode) -- (tikzspyinnode);. This is specified in the TikZ manual on page 465. So in order to influence the path connecting the spy in and spy on nodes we need to specify the spy connection path code. This can simply be done in the options to the \spy commands.

\documentclass{standalone}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{spy,calc}
\begin{document}
\begin{tikzpicture}[spy using outlines={circle, size=2cm}]
\node[above right, inner sep=0] at (0,0) {\includegraphics{test.png}};
\draw[very thin, yellow] (0,0) grid (15.25,10);
\spy[magnification=3,spy connection path={\draw[thick] (tikzspyonnode) -- (tikzspyinnode);}] on (2,2) in node at (3,-2);
\spy[magnification=5,spy connection path={\draw[thick, green] (tikzspyonnode) -- (tikzspyinnode);}] on (7.1,4.1) in node[green] at (8,-2);
\spy[magnification=10,spy connection path={\draw[thick, dashed] (tikzspyonnode) -- (tikzspyinnode);}] on (11.8,2.8) in node[dashed] at (13,-2);
\end{tikzpicture}
\end{document}


Update: To influence the way the on nodes look, we need to define our own spy scope and use keys to change the behaviour. The following example illustrates this:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{spy}
\begin{document}
\tikzset{
my spy on/.style={#1},
my spy/.style={
spy scope={
every spy on node/.style={
draw,
circle,
size=2cm,
my spy on,
},
every spy in node/.style={
draw,
black,
size=2cm,
thick,
circle,
},
#1
}
}
}
\begin{tikzpicture}[my spy]
\node[above right, inner sep=0] at (0,0) {\includegraphics{test.png}};
\draw[very thin, yellow] (0,0) grid (15.25,10);
\spy[magnification=3,spy connection path={\draw[thick, black] (tikzspyonnode) -- (tikzspyinnode);}, my spy on={red, very thick}] on (2,2) in node at (3,-2);
\spy[magnification=5,spy connection path={\draw[thick, green] (tikzspyonnode) -- (tikzspyinnode);}] on (7.1,4.1) in node[green] at (8,-2);
\spy[magnification=10,spy connection path={\draw[thick, dashed,black] (tikzspyonnode) -- (tikzspyinnode);}, my spy on={green, thick}] on (11.8,2.8) in node[dashed] at (13,-2);
\end{tikzpicture}
\end{document}


Now the #1 in the my spy style allows for additional options to be added to the spy scope. The my spy on style is used to set options for the on node. The result looks like this:

-
Very nice and very fast. I just have one nore problem: I can alter the color and style of the spy on node (e.g. red, dashed), but not the thickness. The manual states something about a every spy on node style on page 465, but I don't know how to use it, especially to influence every spy individually. Any ideas? –  Tom Bombadil Jan 26 '12 at 13:16
@TomBombadil: The problem is that the spy using outlines option (discussed on the same page) sets the line width to very thin. So what you would need is to specify your own spy scope. You can then use keys to influence how everything is drawn. I will update the answer in a second. –  Roelof Spijker Jan 26 '12 at 15:17
Yay, now I can do all kinds of weird looking spy glass things. Thanks a lot! –  Tom Bombadil Jan 26 '12 at 16:20