在 tikz 中使用 scale 和 pgfonlayer 进行间谍活动

在 tikz 中使用 scale 和 pgfonlayer 进行间谍活动

当我在 中使用spy和时,放大镜似乎放大的不是我指定的区域。不过,小指针位于正确的位置。如果我设置,放大倍数似乎位于正确的位置。scaletikzpicturescale=1.0

另外,如果我使用pgfonlayer它在背景中创建绘图,如何将其放大spy

这是一个最小的工作示例:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{spy, backgrounds}
\begin{document}

\begin{figure} 

\begin{tikzpicture} [spy using outlines={circle, magnification=4, size=1cm, connect spies},
scale = 1.4,transform shape]
\draw[help lines] (0,0) grid (4,4);
\draw (0,0) -- (3,3) -- (3,0);
\begin{pgfonlayer}{background}
\draw[red] (2.9,0) -- (2.9,4);
\end{pgfonlayer}
\spy [black] on (3,3) in node [left] at (5,5);
\end{tikzpicture}

\end{figure}
\end{document}

请注意,我在放大镜中看到的是三角形的一个角以及垂直红线的一部分(在背景层中)。

答案1

针对你的问题,我有两个建议:

  • 我没有扩大你的间谍规模,而是尝试了magnificationsize选项;
  • 您还可以通过在任何后续内容之前排版来在背景中打印某些内容。

这个最小的例子强调了上述建议:

\documentclass{article}
\usepackage{tikz}% http://ctan.org/pkg/pgf
\usetikzlibrary{spy, backgrounds}
\begin{document}

\begin{tikzpicture} [spy using outlines={circle, magnification=8, size=2cm, connect spies, transform shape}]
  \draw[red] (2.9,0) -- (2.9,4);
  \draw[help lines] (0,0) grid (4,4);
  \draw (0,0) -- (3,3) -- (3,0);
  %\begin{pgfonlayer}{background}
  %  \draw[red] (2.9,0) -- (2.9,4);
  %\end{pgfonlayer}
  \spy [black] on (3,3) in node [left] at (6,5.5);
\end{tikzpicture}
\end{document}

tikz 中的间谍

答案2

我刚刚发现自己想要将图层与间谍库一起使用,因此发现自己处于您描述的情况,即只有主图层上的内容才会出现在节点中spy in。重新排序图层对我来说不是一个选择,所以我查看了间谍库,看看我是否能弄清楚发生了什么。

简而言之,图层的工作方式是将每个图层保存在一个框中,然后在最后按所需顺序堆叠这些框。间谍库实际上的工作原理非常相似:它将绘图保存在一个框中,以便可以将其(带剪辑)复制到节点中。它们不能一起工作,因为当东西保存到其图层框中时,它不会保存到间谍框中。我想到的解决方案是确保间谍库使用所有图层。现在,这在范围方面变得有点复杂,所以在下面我只对整个图片使用它。

这可能不是最终的解决方案,但它是一个可行的解决方案。当我使用它时,我可能会对其进行一些调整。将以下内容保存为tikzlibraryspyall.code.tex并使用\usetikzlibrary{spyall}

% Spy all styles

\tikzset{
  spy all using outlines/.style={
    spy picture={
      every spy all on node/.style={very thin,draw},
      every spy all in node/.style={thick,draw},
      #1
    }
  },
  spy all using overlays/.style={
    spy picture={
      every spy all on node/.style={fill,fill opacity=0.2,text opacity=1},
      every spy all in node/.style={fill,fill opacity=0.2,text opacity=1},
      #1
    }
  },
  connect spies/.style={
    spy connection path={\draw[thin] (tikzspyonnode) -- (tikzspyinnode);}
  }
}

%\newbox\tikz@lib@spybox

\let\tikz@lib@spyall@collection=\pgfutil@empty%

\tikzset{spy picture/.style={
    size/.style={minimum size=##1},
    height/.style={minimum height=##1},
    width/.style={minimum width=##1},
    execute at begin picture={%
      \let\tikz@lib@spyall@save=\tikz@lib@spyall@collection%
      \let\spyall=\tikz@lib@spyall@parse%
      \tikz@insert@spy@layer
      \pgfonlayer{spy}%
    },
    execute at end picture={%
      \endpgfonlayer
      {%
        \tikz@lib@spyall@collection%
      }%
      \global\let\tikz@lib@spyall@collection=\tikz@lib@spyall@save%
    },%
    tikz@lib@spyall@style/.style={#1},
    tikz@lib@reset@gs
  },
  lens/.store in=\tikz@lib@spy@lens,
  lens=,
  magnification/.style={lens={scale=#1}},
  spy connection path/.store in=\tikz@lib@spy@path,
  spy connection path=
}

\tikzset{
  tikz@lib@reset@gs/.style={black,thin,solid,opaque,line cap=butt,line join=miter}
}

\def\tikz@lib@spyall@parse{%
  \pgfutil@ifnextchar[{\tikz@lib@spyall@parse@opt}{\tikz@lib@spyall@parse@opt[]}%]
}
\def\tikz@lib@spyall@parse@opt[#1]{
  \pgfutil@ifnextchar x{\tikz@lib@spyall@parse@opta[#1]}{\tikz@lib@spyall@parse@opta[#1]}%]
}
\def\tikz@lib@spyall@parse@opta[#1]on#2in node#3;{%
  \pgfutil@g@addto@macro\tikz@lib@spyall@collection{\tikz@lib@spyall@do{#1}{#2}{#3}}%
}

\def\tikz@lib@spyall@do#1#2#3{%
  \scope[tikz@lib@spyall@style,#1]
    \node [alias=tikzspyonnode,inner sep=0pt,outer sep=0pt,every spy all on node/.try,
    /utils/exec={
      {%
        \let\tikz@transform=\relax
        \pgftransformreset%
        \expandafter\tikzset\expandafter{\tikz@lib@spy@lens}
        \pgftransforminvert%
        \pgfgettransformentries\a\b\c\d\e\f%
        \global\let\pgf@lib@svg@a=\a%
        \global\let\pgf@lib@svg@b=\b%
        \global\let\pgf@lib@svg@c=\c%
        \global\let\pgf@lib@svg@d=\d%
      }%
      \tikz@addtransform{%
        \tikz@scan@one@point\pgftransformshift#2%
        \pgftransformcm{\pgf@lib@svg@a}{\pgf@lib@svg@b}{\pgf@lib@svg@c}{\pgf@lib@svg@d}{\pgfpointorigin}%
      }
    }]{};
    \node [alias=tikzspyinnode,inner sep=0pt,outer sep=0pt,at={#2},every spy all in node/.try,
    path picture={\node[anchor=center,tikz@lib@reset@gs]{\nullfont%
        \pgfpicture\relax\pgfsetbaseline{default}\pgfsettrimleft{default}\pgfsettrimright{default}%
        \pgftransformreset%
        \let\tikz@transform=\relax%
        \expandafter\tikzset\expandafter{\tikz@lib@spy@lens}%
        \pgflowlevelsynccm%
        \tikz@scan@one@point\tikz@lib@spyall@shift#2%
        \pgflowlevelsynccm%
        \pgf@dosavedlayers
        \endpgfpicture};}]#3{};
    \tikz@lib@spy@path
  \endscope
}

\def\tikz@lib@spyall@shift#1{%
  \pgf@process{#1}%
  \pgf@x=-\pgf@x%
  \pgf@y=-\pgf@y%
  \pgftransformshift{}%
}

\pgfdeclarelayer{spy}


\def\tikz@insert@spy@layer{%
    \expandafter\tikz@insert@spy@@layer\pgf@layerlist\relax
}
\def\tikz@insert@spy@@layer#1main#2\relax{%
    \def\pgf@layerlist{#1spy#2,main}%
}


\def\pgf@dosavedlayers{%
  \expandafter\pgf@dosavedlayer\pgf@layerlist,,\relax%
}
\def\pgf@dosavedlayer#1,#2,\relax{%
  \def\pgf@test{#1}%
  \ifx\pgf@test\pgf@maintext%
    \copy\pgf@layerbox@main
  \else%
    \expandafter\copy\csname pgf@layerboxsaved@#1\endcsname
  \fi%
  \def\pgf@test{#2}%
  \ifx\pgf@test\pgfutil@empty%
  \else%
    \pgf@dosavedlayer#2,\relax%
  \fi%
}
\newbox\pgf@layerboxsaved@main
\def\pgf@dosavelayer#1,#2,\relax{%
  \def\pgf@test{#1}%
  \ifx\pgf@test\pgf@maintext%
    \setbox\pgf@layerboxsaved@main=\box\pgf@layerbox@main
  \else%
    \setbox\csname pgf@layerboxsaved@#1\endcsname=\box\csname pgf@layerbox@#1\endcsname%
  \fi%
  \def\pgf@test{#2}%
  \ifx\pgf@test\pgfutil@empty%
  \else%
    \pgf@dosavelayer#2,\relax%
  \fi%
}


\endinput

这是测试文档。真正的文档是很多更复杂。

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{spyall}

\pgfdeclarelayer{back}
\pgfdeclarelayer{front}
\pgfsetlayers{back,main,front}

\begin{document}


\begin{tikzpicture}[spy all using outlines={circle,magnification=4,size=2cm, connect spies}]
\begin{pgfonlayer}{front}
\draw[ultra thick,black] (.5,0) -- (.5,1);
\end{pgfonlayer}
\draw[ultra thick,blue] (0,0) -- (1,1);
\begin{pgfonlayer}{back}
\draw[ultra thick,green] (1,0) -- (0,1);
\end{pgfonlayer}
\spyall [red] on (.5,.5) in node at (.5,3);
\draw[ultra thick,cyan] (0,.5) -- (1,.5);
\end{tikzpicture}

\end{document}

层层侦察

相关内容