如何在 \pgfdeclareshape 内生成多个锚点?

如何在 \pgfdeclareshape 内生成多个锚点?

在使用 定义新形状时\pgfdeclareshape,我无法使用 语句添加许多锚点\foreach。每个锚点都使用三个参数定义。在示例中,参数#1没问题,但程序对#2和有抱怨#3。如何使用 生成这么多具有​​不同键的锚点\foreach

\documentclass[a4paper,landscape]{article}
\usepackage{tikz}
\usetikzlibrary{calc,arrows}
\usepackage{amsmath}
\usepackage[left=1cm,right=1cm]{geometry}
\pagestyle{empty}

\begin{document}
\makeatletter
% Define shapes
\pgfdeclareshape{PE}{
  % Inherit from rectangle
  \inheritsavedanchors[from=rectangle]
  \inheritanchor[from=rectangle]{center}
  \inheritanchorborder[from=rectangle]

  % In the following I want to make many anchors with some loop. For example,
  %\foreach \x/\Y/\z in {een/1/.7,ee/1/0,ees/1/-1}
  %{\anchor{\x}{
  %  \pgf@process{\northeast}%
  %  \pgf@x=\y\pgf@x%
  %  \pgf@y=\z\pgf@y%
  %}}
  %However, while #1 is replaced but #2 and #2 are not. 
  %How can I use \foreach or TeX loop?        

  \anchor{een}{
    \pgf@process{\northeast}%
    \pgf@x=1\pgf@x%
    \pgf@y=.7\pgf@y%
  }
  \anchor{ee}{
    \pgf@process{\northeast}%
    \pgf@x=1\pgf@x%
    \pgf@y=0\pgf@y%
  }
  \anchor{ees}{
    \pgf@process{\northeast}%
    \pgf@x=1\pgf@x%
    \pgf@y=-.7\pgf@y%
  }
    \anchor{se}{
    \pgf@process{\northeast}%
    \pgf@x=1\pgf@x%
    \pgf@y=-1\pgf@y%
  }

  % Draw the rectangle box and the port labels
  \backgroundpath{
    % Rectangle box
    \pgfpathrectanglecorners{\southwest}{\northeast}
  %Add labels
  %The following code doesn't work.
  %\foreach \x/\y in {een,ee,ees}
  %  {
  %  \pgf@anchor@PE@\x
  %  \pgftext[at={\pgfpoint{\pgf@x}{\pgf@y}},x=-\pgfshapeinnerxsep,y=0]{\raisebox{-.75ex}{\y}}
  % }
  %\foreach \x/\y in {anchor@PE@een/een,\pgf@anchor@PE@ee/ee,\pgf@anchor@PE@ees/ees}
  %{
  %  \x
  %  \pgftext[at={\pgfpoint{\pgf@x}{\pgf@y}},x=-\pgfshapeinnerxsep,y=0]{\raisebox{-.75ex}{\y}}
  %}
}
\makeatother

\begin{tikzpicture}[>=stealth']
  \tikzset{PE/port labels/.style={font=\sffamily\scriptsize}}
  \tikzset{every PE node/.style={draw=blue,minimum width=2cm,minimum
    height=3cm,very thick,inner sep=1mm,outer sep=0pt,cap=round}}
  % Place PEs
  \foreach \m in {0,...,3}
    \node [shape=PE] (PE\m) at ($ 4*(\m,0)$) {};
\end{tikzpicture}
\end{document}

答案1

在循环中扩展foreach总是有问题的,所以你需要确保宏被正确扩展(或者其中一些没有扩展,而变量被扩展)

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,arrows}

\makeatletter

\pgfdeclareshape{PE}{

  \inheritsavedanchors[from=rectangle]
  \inheritanchor[from=rectangle]{center}
  \inheritanchorborder[from=rectangle]

  \foreach \x/\z in {een/.7,ee/0,ees/-1}
  {
    \xdef\doanchor{
      \noexpand\anchor{\x}{
      \noexpand\northeast
      \noexpand\pgf@y=\z\noexpand\pgf@y%
      }
    }\doanchor
  }
  \backgroundpath{
    % Rectangle box
    \pgfpathrectanglecorners{\southwest}{\northeast}
  }
}
\makeatother

\begin{document}

\begin{tikzpicture}[>=stealth']
  \tikzset{PE/port labels/.style={font=\sffamily\scriptsize}}
  \tikzset{every PE node/.style={draw=blue,minimum width=2cm,minimum
    height=3cm,very thick,inner sep=1mm,outer sep=0pt,cap=round}}
  % Place PEs
  \foreach \m in {0,...,2}{    \node [shape=PE] (PE\m) at ($ 4*(\m,0.25*\m)$) {};}
    \draw (PE0.een) to[bend right] (PE1.ee) to[bend right] (PE2.ees);
    %\draw (PE0.ees) -|++(3cm,-2cm) -| (PE1.ees);
    \node at (PE2.een)  {a};
    \node at (PE2.ee)   {b};
    \node at (PE2.ees)  {c};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容