9

I'd like to use a tikz spy shaped like a chamfered rectangle. The code runs without complaining, but the result is a plain rectangle. "rounded rectangle" works as expected, as does chamfered-rectangle-shaped non-spy nodes. How could I fix this?

The code below should demonstrate my problem. Unfortunately I don't have enough rep to add a picture..

pdflatex Version 3.1415926-2.4-1.40.13 (TeX Live 2012/Debian) I had the same issue in Windows yesterday, I believe that was Tex Live 2012 as well. tikz 2010/10/13 v2.10 (rcs-revision 1.76)

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{spy,decorations.fractals,shapes.misc}

\begin{document}

\tikz\node[draw, circle] {circle};
\begin{tikzpicture}
  [spy using outlines={circle, magnification=3, size=1cm, connect spies}]
  \draw [decoration=Koch curve type 1]
    decorate{ decorate{ decorate{ (0,0) -- (2,0) }}};
  \spy [red] on (1.6,0.3) in node at (3,1);
\end{tikzpicture}

\tikz\node[draw, rounded rectangle] {rounded rectangle};
\begin{tikzpicture}
  [spy using outlines={rounded rectangle, magnification=3, width=2cm, height=1cm, connect spies}]
  \draw [decoration=Koch curve type 1]
    decorate{ decorate{ decorate{ (0,0) -- (2,0) }}};
  \spy [red] on (1.6,0.3) in node at (3,1);
\end{tikzpicture}

\tikz\node[draw, chamfered rectangle] {chamfered rectangle};
\begin{tikzpicture}
  [spy using outlines={chamfered rectangle, magnification=3, width=2cm, height=1cm, connect spies}]
  \draw [decoration=Koch curve type 1]
    decorate{ decorate{ decorate{ (0,0) -- (2,0) }}};
  \spy [red] on (1.6,0.3) in node at (3,1);
\end{tikzpicture}

\end{document}

Here's what it produces for me:

enter image description here

  • Add the picture and remove the initial ! which makes it a link. Then we can add it for you. – percusse Aug 15 '13 at 14:35
  • 1
    @Roel Your code produces a chamfered rectangle here, as expected, both with pdfTeX 3.1415926-2.4-1.40.13 (TeX Live 2012) and pdfTeX 3.1415926-2.5-1.40.14 (TeX Live 2013). You and I use the same TikZ version. – jub0bs Aug 15 '13 at 14:46
  • I added the image. The chamfered rectangle works as a plain node shape, but not as a spy. Is there anything intermediate I could check to see where my problem comes from? – Roel Aug 16 '13 at 7:06
  • To counter Jubobs: using pdfTeX 3.1415926-2.5-1.40.14 (TeX Live 2013), with the same version of TikZ as you I can reproduce your issue. – simont Aug 27 '13 at 11:38
  • @simont It seems to me that the chamfered rectangle uses the text box to define its shape (as every node), but in the case of the spy nodes the text is empty and so is the box. Using the keys text width and text height on the spy-in node will move the magnified content around. – Qrrbrbirlbel Aug 27 '13 at 12:47
7
+50

The chamfered rectangle shape uses the height, depth and width of the node’s text box as well as the inner separator to compute its path (as every other shape).

This can be simulated with a normal node with inner sep set to zero (the spy nodes use this too):

\tikz
  \node[draw, chamfered rectangle, minimum width=2cm, minimum height=1cm, inner sep=0pt] {};

So, the next idea would be to include an inner sep again in the every spy on node as well as the every spy in node.

My other solution provides a specific fix for the chamfered rectangle which will use the minimum height and minimum width as a replacement for the text box.

The yellow box shows in both solutions where the 2cm × 1cm will be measured.

Code A (inner sep)

\documentclass[tikz,convert=false]{standalone}
\usetikzlibrary{spy,decorations.fractals,shapes.misc}
\begin{document}
\begin{tikzpicture}[
  spy using outlines={
    shape=chamfered rectangle,
    every spy on node/.append style={inner sep=+.3333em},
    every spy in node/.append style={inner sep=+.3333em},
    magnification=3,
    width=2cm,
    height=1cm,
    connect spies
  }]
  \draw [decoration=Koch curve type 1]
    decorate{ decorate{ decorate{ (0,0) -- (2,0) }}};
  \spy[red] on (1.6,0.3) in node at (3,1);
  \node[draw=green, thick, minimum width=2cm/3, minimum height=1cm/3] at (1.6,.3) {};
\end{tikzpicture}
\end{document}

Code B (specific fix)

\documentclass[tikz,convert=false]{standalone}
\usepackage{etoolbox}
\usetikzlibrary{spy,decorations.fractals,shapes.misc}
\makeatletter
\newif\iftikz@lib@spy@active
\expandafter\patchcmd\csname pgfk@/tikz/spy scope/.@cmd\endcsname
  {\copy\tikz@lib@spybox\tikz@lib@spy@collection}
  {\copy\tikz@lib@spybox\tikz@lib@spy@activetrue\tikz@lib@spy@collection}{}{}
\expandafter\patchcmd\csname pgf@sh@s@chamfered rectangle\endcsname{%
  \pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/inner xsep}}%
  \advance\pgf@xa.5\wd\pgfnodeparttextbox%
  \pgfmathsetlength\pgf@ya{\pgfkeysvalueof{/pgf/inner ysep}}%
  \advance\pgf@ya.5\ht\pgfnodeparttextbox%
  \advance\pgf@ya.5\dp\pgfnodeparttextbox%
}{%
  \pgfmathsetlength\pgf@xa{\pgfkeysvalueof{/pgf/inner xsep}}%
  \pgfmathsetlength\pgf@ya{\pgfkeysvalueof{/pgf/inner ysep}}%
  \iftikz@lib@spy@active
    \pgfmathaddtolength\pgf@xa{.5*(\pgfkeysvalueof{/pgf/minimum width}-2*(\pgfkeysvalueof{/pgf/chamfered rectangle xsep}))}%
    \pgfmathaddtolength\pgf@ya{.5*(\pgfkeysvalueof{/pgf/minimum height}-2*(\pgfkeysvalueof{/pgf/chamfered rectangle ysep}))}%
  \else
    \advance\pgf@xa.5\wd\pgfnodeparttextbox%
    \advance\pgf@ya.5\ht\pgfnodeparttextbox%
    \advance\pgf@ya.5\dp\pgfnodeparttextbox%
  \fi}{}{}
\makeatother
\begin{document}
\begin{tikzpicture}[
  spy using outlines={
    shape=chamfered rectangle,
    magnification=3,
    width=2cm,
    height=1cm,
    connect spies
  }]
  \draw [decoration=Koch curve type 1]
    decorate{ decorate{ decorate{ (0,0) -- (2,0) }}};
  \spy[red] on (1.6,0.3) in node at (3,1);
  \node[draw=green, thick, minimum width=2cm/3, minimum height=1cm/3] at (1.6,.3) {};
\end{tikzpicture}
\end{document}

Output (both)

enter image description here

  • 1
    Hm, red/green is not a good choice, colorwise, isn’t it? – Qrrbrbirlbel Aug 27 '13 at 14:14
  • That certainly expands my knowledge - many thanks. – simont Aug 28 '13 at 1:23
  • I'll read that over calmly the weekend. I'm impressed :) – Roel Aug 29 '13 at 20:33

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.