# Using TikZ spy in 3D

Milo 06/12/2018. 2 answers, 536 views

I am trying to use the spy library to magnify part of a 3D TikZ drawing. Consider this minimal example:

\documentclass[tikz,margin=0.5cm]{standalone}
\usetikzlibrary{3d,spy}

\begin{document}

\begin{tikzpicture}[y={(1cm,-0.5cm)},x={(1cm,0.5cm)}, z={(0cm,1cm)},spy using outlines={rectangle, magnification=4, size=2cm, connect spies}]

\coordinate (O) at (1, -1 -0.5);
\draw[->] (O) -- +(0.5, 0,  0) node [right] {$x$};
\draw[->] (O) -- +(0,  0.5, 0) node [below] {$y$};
\draw[->] (O) -- +(0,  0, 0.5) node [above] {$z$};

\draw [fill=blue!20] (0,0,0) -- (3,0,0) -- (3,2,0) -- (0,2,0) -- cycle;

\begin{scope}[canvas is yx plane at z=0,transform shape,every node/.style={scale=0.2}]
\node [align=center] (Text) at (1,1.5) {Make me\\ big!};
\end{scope}

\spy on (Text) in node at (1,1,3);

\end{tikzpicture}
\end{document}

This works as expected, but I want the small square box to take on the 3D perspective. Like this:

The resulting zoomed in picture would therefore look as if it was face on:

Is this possible?

Jasper Habicht 06/12/2018.

There is the lens option that allows you to apply transformations on the small “spy” picture (see para 68.3 in the TikZ/PGF manual [v 3.0.1a]):

\documentclass[tikz,margin=0.5cm]{standalone}
\usetikzlibrary{3d,spy}

\begin{document}

\begin{tikzpicture}[y={(1cm,-0.5cm)},x={(1cm,0.5cm)}, z={(0cm,1cm)},spy using outlines={rectangle, magnification=4, size=2cm, connect spies}]

\coordinate (O) at (1, -1 -0.5);
\draw[->] (O) -- +(0.5, 0,  0) node [right] {$x$};
\draw[->] (O) -- +(0,  0.5, 0) node [below] {$y$};
\draw[->] (O) -- +(0,  0, 0.5) node [above] {$z$};

\draw [fill=blue!20] (0,0,0) -- (3,0,0) -- (3,2,0) -- (0,2,0) -- cycle;

\begin{scope}[canvas is yx plane at z=0,transform shape,every node/.style={scale=0.2}]
\node [align=center] (Text) at (1,1.5) {Make me\\ big!};
\end{scope}

\spy[lens={xslant=-1,yslant=.5,scale=3}] on (Text) in node at (1,1,3);

\end{tikzpicture}
\end{document}

marmot 06/12/2018.

Very similar to Jasper Habicht's nice answer except that you do not have to adjust the lens transformation by hand. So this works regardless of which vectors you choose for you 3D coordinate system.

\documentclass[tikz,margin=0.5cm]{standalone}
\usetikzlibrary{3d,spy}

\begin{document}

\begin{tikzpicture}[y={(1cm,-0.5cm)},x={(1cm,0.5cm)}, z={(0cm,1cm)},spy using outlines={rectangle, magnification=4, size=2cm, connect spies}]

\coordinate (O) at (1, -1 -0.5);
\draw[->] (O) -- +(0.5, 0,  0) node [right] {$x$};
\draw[->] (O) -- +(0,  0.5, 0) node [below] {$y$};
\draw[->] (O) -- +(0,  0, 0.5) node [above] {$z$};

\draw [fill=blue!20] (0,0,0) -- (3,0,0) -- (3,2,0) -- (0,2,0) -- cycle;

\begin{scope}[canvas is yx plane at z=0,transform shape,every node/.style={scale=0.2}]
\node [align=center] (Text) at (1,1.5) {Make me\\ big!};
\xdef\mytrafo{\mytrafo} % globalize macro
\end{scope}

\spy[lens={/utils/exec={\pgfsettransform{\mytrafo} % reinstall and invert trafo
\pgftransforminvert},scale=3}] on (Text) in node at (1,1,3);

\end{tikzpicture}
\end{document}