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

8
  • 3
    a MWE would be more than welcome as a starting point. Rewriting code from scratch is always a pain.... :)
    – JeT
    Commented Feb 24, 2022 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
    Commented Feb 24, 2022 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
    Commented Feb 24, 2022 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
    Commented Feb 24, 2022 at 18:39
  • 2
    For future reference, there are example pictures available out of the box, see \includegraphics[width=3cm]{example-grid-100x100pt} (tex.stackexchange.com/questions/231738). This makes MWEs portable. Commented Feb 24, 2022 at 19:25

2 Answers 2

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}
2
  • This one's elegant! Thanks a lot! I wish there were easier options though to apply in general cases.
    – SolidMark
    Commented Feb 24, 2022 at 20:32
  • 1
    @J Leon V. I'm your click on the 10k :) nice !
    – JeT
    Commented Feb 24, 2022 at 20:38
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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .