[Tex/LaTex] Why does a tikz spy specified as a chamfered rectangle show up as a simple rectangle

spytikz-pgf

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

Best Answer

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