3

Consider this code:

\documentclass{article}
\usepackage{tikz}

\usetikzlibrary{calc}
\usetikzlibrary{spy}

\begin{document}

\begin{tikzpicture}
  [spy using outlines={circle, magnification=7, size=3cm, connect spies}]

  \draw[->, very thick] (0,0) -- (5,0);
  \path (2,2) node[fill, rectangle, inner sep=2pt, minimum height=1cm] (n) {};

  \draw[dotted] (n.south west) -- (n.south west |- 0,0);
  \draw[dotted] (n.south east) -- (n.south east |- 0,0);

  \spy on ($(n.south east |- 0,0)!0.5!(n.south west |- 0,0)$) 
    in node at (3,-2);
\end{tikzpicture}

\end{document}

This is the output:

mew

There are two problems:

  1. The dotted lines become a couple of dots. Can I change the style of the line inside the spy in node (leaving the original one unchanged) so that the frequency of the dot symbols is higher and the dotted line appears as such in the spy in node as well?
  2. Can I reference nodes or coordinates inside the spy in nodes in some way? That's because I'd need to draw a distance measurment mark between the zoomed versions of the two dotted lines.
  • I feel that if you want to make such drastic changes, you may not want to use spy at all, but just draw whatever you like in a circle node. –  Jan 26 '20 at 15:15

1 Answers1

4

I feel that if you want to make such drastic changes, you may not want to use spy at all, but just draw whatever you like in a circle node.

\documentclass{article}
\usepackage{tikz}

\usetikzlibrary{calc}
\usetikzlibrary{spy}

\begin{document}

\begin{tikzpicture}
  [spy using outlines={circle, magnification=7, size=3cm, connect spies}]

  \draw[->, very thick] (0,0) -- (5,0);
  \path (2,2) node[fill, rectangle, inner sep=2pt, minimum height=1cm] (n) {};

  \draw[dotted] (n.south west) -- (n.south west |- 0,0);
  \draw[dotted] (n.south east) -- (n.south east |- 0,0);

  \draw  let \p1=($(n.south east)-(n.south west)$)
   in   ($(n.south east |- 0,0)!0.5!(n.south west |- 0,0)$)
   node[circle,draw,inner sep=0pt,minimum size=3cm/7](small){}
    (3,-2)
   node[circle,draw,inner sep=0pt,minimum size=3cm,
   path picture={
   \draw[line width=7*0.8pt] 
   (path picture bounding box.west) -- (path picture bounding box.east);
   \draw[thick,dotted] (-7*\x1/2,0) --  (-7*\x1/2,3) 
    (7*\x1/2,0) --  (7*\x1/2,3); 
   \draw[|<->|] (-7*\x1/2,0.8) -- (7*\x1/2,0.8)
    node[midway,fill=white,minimum size=0pt,rectangle]{$\Delta x$};  }]
   (big){} (small) -- (big);
\end{tikzpicture}

\end{document}

enter image description here

You may use this to annotate the spy node.

\documentclass{article}
\usepackage{tikz}

\usetikzlibrary{calc}
\usetikzlibrary{spy}

\begin{document}

\begin{tikzpicture}
  [spy using outlines={circle, magnification=7, size=3cm, connect spies}]

  \draw[->, very thick] (0,0) -- (5,0);
  \path (2,2) node[fill, rectangle, inner sep=2pt, minimum height=1cm] (n) {};

  \draw[densely dotted] (n.south west) -- (n.south west |- 0,0);
  \draw[densely dotted] (n.south east) -- (n.south east |- 0,0);

  \spy on ($(n.south east |- 0,0)!0.5!(n.south west |- 0,0)$) 
    in node at (3,-2);
  \draw  let \p1=($(n.south east)-(n.south west)$),\n1={7*\x1/2-7*0.2pt}
   in  (3,-2)
   node[circle,draw,inner sep=0pt,minimum size=3cm,
   path picture={
   \draw[|<->|] (-\n1,0.8) -- (\n1,0.8)
    node[midway,fill=white,minimum size=0pt,rectangle]{$\Delta x$};  }]
   (big){}; 
\end{tikzpicture}

\end{document}

enter image description here

  • 1
    Thanks. This is a working solution for this MWE, but this was a shrinked down version of something more complex, where it is hard, if at all possible, to recreate the contents of the spy node by hand. – Nicola Gigante Jan 26 '20 at 15:48
  • @gigabytes spy copies the content of a box. So you cannot easily manipulate its contents. You can overlay stuff in the way described here. –  Jan 26 '20 at 16:39
  • 1
    I see. I suppose the library could be extended to provide at least access to the copies of the nodes. For instance, the magnified copy of a node (n) may be called (spy-n), so that it could be accessed easily. I don't know whether this addition to the library would be easy to obtain. Maybe I should file a feature request on GitHub or wherever the project is hosted. – Nicola Gigante Jan 26 '20 at 16:44
  • @gigabytes I don't think it will be easy to reconstruct the contents in such a way that it becomes a set of nodes. What you can do is to use the code to annotate a spy node, I added an example. –  Jan 26 '20 at 17:22
  • Thanks, I was looking for something like that, but I'm having difficulties understanding what is going on. Can you elaborate a bit on how it works? – Nicola Gigante Jan 26 '20 at 17:29
  • @gigabytes If I know what you do not understand I can try to explain it. –  Jan 26 '20 at 17:31
  • Nevermind. The positions are placed "manually", right? I thought you were somehow computing them somehow. – Nicola Gigante Jan 26 '20 at 17:34
  • @gigabytes They are computed from the other data. It might look "manual" because I subtract half of the scaled line width. –  Jan 26 '20 at 17:52
  • Yes, clear. Thank you :) – Nicola Gigante Jan 26 '20 at 18:05