9

I have issues getting the spy library to work on my current installation. I am running TeXmaker with MikTeX 2.9 (with all packages updated) on Win8.1x64.

I read about the issues others are having with this here and here, but I still don't get it to work using PdfLaTeX with direct pdf output.

This is the MWE I am using:

\documentclass{minimal}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{arrows,calc,spy}

\begin{document}
\begin{tikzpicture}
    % Using Wikipedia's image of the day
    \node[anchor=south west,inner sep=0] (image) at (0,0){\includegraphics[width=0.65\textwidth]{Coiled_Galaxy.jpg}};
        \begin{scope}[x={(image.south east)},y={(image.north west)},
                  spy using outlines={red,circle,magnification=4, size=3cm,connect spies}]

%       \foreach \x in {0,1,...,9} { \node [anchor=north] at (\x/10,0) {0.\x}; }
%       \foreach \y in {0,1,...,9} { \node [anchor=east] at (0,\y/10) {0.\y}; }
        \spy on (0.70,0.95) in node  at (0.5,1.2);
        \end{scope}
\end{tikzpicture}
\\
Pgf Version: \pgfversion

\end{document}

% Log 
% This is pdfTeX, Version 3.14159265-2.6-1.40.16 (MiKTeX 2.9) (preloaded format=pdflatex 2015.7.29)

And this is my result:

MWE_output

EDIT: My mistake was as Ulrike Fischer pointed out, there was no image for spy to work with, nothing to do with my packages.

The fixed solution is:

\documentclass{minimal}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{arrows,calc,spy}

\begin{document}
\begin{tikzpicture}[ spy using outlines={red,circle,magnification=4, size=3cm,connect spies}]
    % Use Wikipedias image of the day
    \node[anchor=south west,inner sep=0] (image) at (0,0){\includegraphics[width=0.65\textwidth]{Coiled_Galaxy.jpg}};
        \begin{scope}[x={(image.south east)},y={(image.north west)}]

        \foreach \x in {0,1,...,9} { \node [anchor=north] at (\x/10,0) {0.\x}; }
        \foreach \y in {0,1,...,9} { \node [anchor=east] at (0,\y/10) {0.\y}; }
        \end{scope}
    \spy on ($0.95*(image.south east)+0.95*(image.north west)$) in node at ([yshift=1cm]image.north);
\end{tikzpicture}
\\
Pgf Version: \pgfversion

\end{document}

Resulting in: Fixed example

  • Your image must be inside the scope. – Ulrike Fischer Jul 29 '15 at 11:48
  • How would one then get the spy to scale with the image then? The principle I am working on here, is based on this – Ash Jul 29 '15 at 11:55
  • Not sure what you mean, but you can always do calculations with the node coordinates, e..g. \spy on ($0.5*(image.south)+0.5*(image.center)$) in node at ([yshift=1cm]image.north); – Ulrike Fischer Jul 29 '15 at 12:16
  • Ah, makes sense! It's fixed now! – Ash Jul 29 '15 at 12:33
6

I'm sure I've seen something very similar to this answer somewhere else, but I can't find it. But anyway, here is an image coordinate system which provides a way to refer to coordinates in the image (actually the node containing the image which should have zero innersep and outersep) using both relative coordinates (from 0 to 1) and exact coordinates (i.e., with units).

In this example I have used universe.jpg from the lecturer package. The image cs will not produce great results if you do something "clever" like rotate or slant the node.

\documentclass[tikz, border=5]{standalone}
\usetikzlibrary{spy}
\tikzdeclarecoordinatesystem{image}{%
  \tikzset{image cs/.cd, #1}%
    \pgfpointdiff%
      {\pgfpointanchor{\graphicname}{south west}}%
      {\pgfpointanchor{\graphicname}{north east}}%
    \pgfgetlastxy\graphicwidth\graphicheight%
    \pgfmathparse{\graphicx}%
    \ifpgfmathunitsdeclared\def\graphicwidth{1}\fi%
    \pgfmathparse{\graphicy}%
    \ifpgfmathunitsdeclared\def\graphicheight{1}\fi%
    \pgfpointadd{\pgfpointanchor{\graphicname}{south west}}%
      {\pgfpoint{(\graphicx)*\graphicwidth}{(\graphicy)*\graphicheight}}%
}
\tikzset{image cs/.cd,
  x/.store in=\graphicx, y/.store in=\graphicy,
  image/.store in=\graphicname
}
\begin{document}
\begin{tikzpicture}
\begin{scope}[spy using outlines={white, ultra thick, 
  circle, magnification=4, size=3cm, connect spies}]
  \node  [inner sep=0, outer sep=0] (universe) 
    {\includegraphics[width=10cm]{universe.jpg}};
\foreach \x in {0,1,...,9}{ 
  \node [below] at (image cs:image=universe, x=\x/10, y=0) {0.\x};
  \node [above] at (image cs:image=universe, x=\x cm, y=1) {\x cm};
}
\foreach \y in {0,1,...,9}{
  \node [left]  at (image cs:image=universe, x=0, y=\y/10) {0.\y};
  \node [right] at (image cs:image=universe, x=1, y=\y cm) {\y cm};
}

\spy on (image cs:image=universe, x=0.69, y=0.88) in node at (-2, 5);
\spy on (image cs:image=universe, x=5cm,  y=5cm)  in node at (-5, 0);
\spy on (image cs:image=universe, x=0.41, y=0.12) in node at ( 5,-5);
\end{scope}
\end{tikzpicture}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.