为什么指定为倒角矩形的 tikz 间谍会显示为简单矩形?

为什么指定为倒角矩形的 tikz 间谍会显示为简单矩形?

我想使用形状像倒角矩形的 tikz 间谍。代码运行时没有抱怨,但结果是一个普通的矩形。“圆角矩形”按预期工作,倒角矩形形状的非间谍节点也是如此。我该如何解决这个问题?

下面的代码应该可以演示我的问题。不幸的是,我没有足够的代表来添加图片。

pdflatex 版本 3.1415926-2.4-1.40.13(TeX Live 2012/Debian)我昨天在 Windows 中遇到了同样的问题,我相信那也是 Tex Live 2012。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}

以下是它为我提供的结果:

在此处输入图片描述

答案1

chamfered rectangle形状使用节点文本框的高度、深度和宽度以及内部分隔符来计算其路径(与其他所有形状一样)。

这可以通过设置为零的普通节点来模拟inner sep(间谍节点也使用它):

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

因此,下一个想法是inner sepevery spy on node以及中再次包含every spy in node

我的另一个解决方案为提供了一个特定的修复,它将chamfered rectangle使用minimum heightminimum width作为文本框的替代。

黄色框显示了两个解决方案中的2cm × 1cm测量位置。

代码 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}

代码 B(具体修复)

\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\[email protected]\wd\pgfnodeparttextbox%
  \pgfmathsetlength\pgf@ya{\pgfkeysvalueof{/pgf/inner ysep}}%
  \advance\[email protected]\ht\pgfnodeparttextbox%
  \advance\[email protected]\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\[email protected]\wd\pgfnodeparttextbox%
    \advance\[email protected]\ht\pgfnodeparttextbox%
    \advance\[email protected]\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}

输出(两者)

在此处输入图片描述

相关内容