3

Is there a way to achieve magnification like the one shown below using Spy library in TikZ? If so, how to achieve it? If not so, what better alternatives are there?

The thick yellow outlines below are not necessary. But I do want to achieve solid boundaries and dashed projection lines.

magnification

My MWE:

\documentclass[border=3mm]{standalone}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{spy}

\begin{document} \begin{tikzpicture}

\begin{scope}[spy using outlines={rectangle,magnification=4, size=2.5cm,connect spies}] \node[rectangle,draw,inner sep=1pt] (image) at (0,0){\includegraphics[width=70px,height=70px]{image1.jpg}}; \spy on (0,0) in node at (0,4); \end{scope} \end{tikzpicture}

\end{document}

produces

my-magnification

UPDATE 1

I managed to get the dashed square projection in the following way:

\documentclass[border=3mm]{standalone}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{spy}

\begin{document} \begin{tikzpicture}

\begin{scope}[spy using outlines={thick,red,rectangle,magnification=4, size=2.5cm,connect spies}] \node[rectangle,draw,inner sep=1pt] (image) at (0,0){\includegraphics[width=70px,height=70px]{image1.jpg}}; \spy[spy connection path={ \draw[densely dashed] (tikzspyinnode.north west) -- (tikzspyonnode.north west); \draw[densely dashed] (tikzspyinnode.south west) -- (tikzspyonnode.south west); \draw[densely dashed] (tikzspyinnode.north east) -- (tikzspyonnode.north east); \draw[densely dashed] (tikzspyinnode.south east) -- (tikzspyonnode.south east); }] on (0,0) in node at (1.5,3.5); \end{scope} \end{tikzpicture}

\end{document}

which produces

my-magnification-2

The only problem is that the dashed lines cross over the magnified image as you can see. How to send them behind?

UPDATE 2

If I use the on background layer as scope option, it did solve for the magnifying part, but then again these lines go behind the actual image:

\documentclass[border=3mm]{standalone}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{spy,backgrounds}

\begin{document} \begin{tikzpicture}

\begin{scope}[spy using outlines={thick,red,rectangle,magnification=4, size=2.5cm,connect spies}] \node[rectangle,draw,inner sep=1pt] (image) at (0,0){\includegraphics[width=70px,height=70px]{image1.jpg}}; \spy[fill=white,spy connection path={ \begin{scope}[on background layer] \draw[densely dashed] (tikzspyinnode.north west) -- (tikzspyonnode.north west); \draw[densely dashed] (tikzspyinnode.south west) -- (tikzspyonnode.south west); \draw[densely dashed] (tikzspyinnode.north east) -- (tikzspyonnode.north east); \draw[densely dashed] (tikzspyinnode.south east) -- (tikzspyonnode.south east); \end{scope} }] on (0,0) in node at (1.5,3.5); \end{scope} \end{tikzpicture}

\end{document}

my-magnification-3

SolidMark
  • 1,389
  • 3
    a MWE would be more than welcome as a starting point. Rewriting code from scratch is always a pain.... :) – JeT Feb 24 '22 at 17:58
  • 1
    Sorry my bad. I thought the basic code is pretty much straightforward and any image would do fine. But here, I have updated them. – SolidMark Feb 24 '22 at 18:18
  • So you're not happy with the single connector and want to get the 2 connectors as in your example. Right ? – JeT Feb 24 '22 at 18:36
  • No, this looks a bit boring. What I want to try is achieving the surface projection. Spy connection paths are dashed, and there should be 4 connections with corresponding points i.e north east to north east corner and so on. The magnified image should be on front layer while the spy connection lines are behind the magnified image. – SolidMark Feb 24 '22 at 18:39
  • Please see the update once more. I almost got what I wanted. But only a slight obstacle there. – SolidMark Feb 24 '22 at 18:48
  • Ah... it's actually another issue then. It's a question of layers ! :) Take a look here. You want the magnified window to appear on the foreground vs the arrows. – JeT Feb 24 '22 at 18:53
  • 2
    For future reference, there are example pictures available out of the box, see \includegraphics[width=3cm]{example-grid-100x100pt} (https://tex.stackexchange.com/questions/231738). This makes MWEs portable. – Dr. Manuel Kuehner Feb 24 '22 at 19:25
  • Will keep that in mind. – SolidMark Feb 24 '22 at 19:26

2 Answers2

5

I would redraw the spy.

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{spy}
\begin{document}
\begin{tikzpicture}
\begin{scope}[spy using outlines={thick, red, rectangle, magnification=4, size=2.5cm}]
\node[rectangle, draw, inner sep=1pt] (image) at (0,0){\includegraphics[width=70px, height=70px]{example-image-duck}};
\spy[spy connection path={
\draw[densely dashed] (tikzspyinnode.north west) -- (tikzspyonnode.north west);
\draw[densely dashed] (tikzspyinnode.south west) -- (tikzspyonnode.south west);
\draw[densely dashed] (tikzspyinnode.north east) -- (tikzspyonnode.north east);
\draw[densely dashed] (tikzspyinnode.south east) -- (tikzspyonnode.south east);
}] on (0,0) in node at (1.5,3.5);
\spy on (0,0) in node at (1.5,3.5);
\end{scope}
\end{tikzpicture}
\end{document}

Duck with extra magnified duck

  • Aha! Nice way to hide the flaw there ha ha! This time connect spies are missing, didn't thought of that. – SolidMark Feb 24 '22 at 21:15
4

An option using intersections:

RESULT:

enter image description here

MWE:

\documentclass[border=3mm,tikz]{standalone}
\usetikzlibrary{spy,intersections}

\begin{document} \begin{tikzpicture}

    \begin{scope}[spy using outlines={thick,red,rectangle,magnification=4, size=2.5cm,connect spies}]
        \node[rectangle,draw,inner sep=1pt] (image) at (0,0){\includegraphics[width=70px,height=70px]{example-grid-100x100pt.jpg}};
        \spy[spy connection path={
            \draw[densely dashed] (tikzspyinnode.north west) -- (tikzspyonnode.north west);
            \draw[densely dashed] (tikzspyinnode.south west) -- (tikzspyonnode.south west);
            \path[name path=Dline] (tikzspyinnode.north east) -- (tikzspyonnode.north east);
            \path[name path=SpyInBottomSide] (tikzspyinnode.south west) -- (tikzspyinnode.south east);
            \path [name intersections={of=Dline and SpyInBottomSide,by=I}];
            \draw[densely dashed,red](I) -- (tikzspyonnode.north east);
            \draw[densely dashed] (tikzspyinnode.south east) -- (tikzspyonnode.south east);
        }] on (0,0) in node at (1.5,3.5);
    \end{scope}
\end{tikzpicture}

\end{document}

J Leon V.
  • 11,533
  • 16
  • 47