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.
1
  • 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.
    – user194703
    Jan 26, 2020 at 15:15

1 Answer 1

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

9
  • 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.
    – gigabytes
    Jan 26, 2020 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.
    – user194703
    Jan 26, 2020 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.
    – gigabytes
    Jan 26, 2020 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.
    – user194703
    Jan 26, 2020 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?
    – gigabytes
    Jan 26, 2020 at 17:29

You must log in to answer this question.

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