路径图片添加自定义锚点

路径图片添加自定义锚点

我想在 TikZ 中创建带有自定义锚点的天线形状。由于我很难使用\pgfdeclareshape,所以我改用path picture

是否可以使命令内部创建的坐标path picture可作为锚点访问?

另外,是否可以围绕中心点旋转整个形状(包括定义的锚点)?

梅威瑟:

\documentclass[border=1pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\tikzset{antenna/.style={draw=none,
    path picture={
        \draw coordinate (feed) at (path picture bounding box.west);
        \draw coordinate (center) at (path picture bounding box.center);
        \draw coordinate (phase center) at (path picture bounding box.east);
        \draw (feed)-- (center);
        \draw (center)-- (path picture bounding box.north east);
        \draw (center)-- (path picture bounding box.south east);
}}}

\begin{document}
    \begin{tikzpicture}
        \node[antenna] (antenna1) {};
        %\draw[->] (antenna1.phase center)-- ++(1, 1);  % I want this to work
    \end{tikzpicture}
\end{document}

答案1

我们可以命名一个坐标antenna1.phase center,但是由于.TikZ 使用 来检测带锚点节点规范,因此我们需要使用显式版本,即(node cs: name=antenna.phase center)然后最好定义假锚点(即您已经使用过的坐标)但只是用-而不是.

path picture-而不是.

我们可以这样做:

\documentclass[border=1pt,tikz]{standalone}
\makeatletter
\tikzset{name prefix node name/.style={name prefix/.expanded=\tikz@fig@name}}
\makeatother
\tikzset{
  antenna/.style={
    draw=none,
    path picture={
      \tikzset{name prefix node name}
      \coordinate (-feed)   at (path picture bounding box.west)
       coordinate (-center) at (path picture bounding box.center)
       coordinate (-phase center) at (path picture bounding box.east);
      \draw (-feed)   -- (-center)
            (-center) -- (path picture bounding box.north east)
            (-center) -- (path picture bounding box.south east);}}}
\begin{document}
\begin{tikzpicture}
\node[antenna] (antenna1) {};
\draw[->] (antenna1-phase center)-- ++(1, 1);% close enough
\end{tikzpicture}
\end{document}

这里设置了我们name prefix所在节点的名称。path picture

append after command为该形状添加了锚点

但请注意,正如您所定义的,它antenna1.phase center只是antenna1.east

也就是说,除非你旋转节点,但这样所有的path picture bounding box锚点也不再正确,因为路径图片边界框不会随之旋转。你需要一个append after commandthen,因为它将具有与源节点相同的变换。

让我们这样做并仅为该节点添加特殊锚点:

\documentclass[border=1pt,tikz]{standalone}
\makeatletter
\tikzset{
  add anchor to node/.code n args={3}{%
    \edef\tikz@temp##1{%  \tikz@pp@name/\tikzlastnode needs to be expanded
      \noexpand\pgfutil@g@addto@macro\expandafter\noexpand\csname pgf@sh@ma@\tikz@pp@name{#1}\endcsname{%
        \def\expandafter\noexpand\csname pgf@anchor@\csname pgf@sh@ns@\tikz@pp@name{#1}\endcsname @#2\endcsname{##1}%
      }}\tikz@temp{#3}},
  add anchor alias to node/.style n args={3}{
    add anchor to node={#1}{#2}{\pgf@sh@reanchor{\csname pgf@sh@ns@\pgfreferencednodename\endcsname}{#3}}},
  add anchor alias to me/.style args={#1 for #2}{append after command={
      [add anchor alias to node={\tikzlastnode}{#1}{#2}]}}}
\makeatother
\tikzset{
  antenna/.style={
    draw=none, add anchor alias to me/.list={phase center for east,feed for west},
    append after command={(\tikzlastnode.center) edge (\tikzlastnode.feed)
      edge (\tikzlastnode.north east) edge (\tikzlastnode.south east)}}}
\begin{document}
\begin{tikzpicture}
\node[antenna, rotate=10] (antenna1) {};
\draw[->] (antenna1.phase center)-- ++(1, 1);
\end{tikzpicture}
\end{document}

图片

相关内容