Tikz:使用相对定位和 foreach 变量

Tikz:使用相对定位和 foreach 变量

我想生成一张可以自我复制并慢慢淡出的图片。为此,我计划使用 foreach 语句并将图片重新绘制到其右侧。

我如何使用 foreach 变量来定位我复制的图片。

这是一个说明我想要的最小示例。最小示例中的剩余工作是以某种方式用来\x创建名称,以便在重绘期间对节点进行相对寻址。

\begin{tikzpicture}[
  item/.style={draw=black!#1, thick, text=black!#1, minimum width = 60},
  label/.style={text=black!#1, right},
  action/.style={black!#1, ->, thick},
  downfrom/.style={below = 0.5 of {#1.south}, anchor=north},
  rightfrom/.style={right = 0.5 of {#1.north east}, anchor=north west}
]

\foreach \x in {100}
{
    \node [item=\x]                     (first100) {first};
    \node [item=\x, downfrom=first100]  (second100) {second};
    \path[action=\x](first100) edge node [label=\x] {a} (second100.north);
}   

 \foreach \x in {67}
{
    \node [item=\x, rightfrom=first100] (first67) {first};
    \node [item=\x, downfrom=first67]   (second67) {second};
    \path[action=\x](first67) edge node [label=\x] {a} (second67.north);
}   

 \foreach \x in {34}
 {
    \node [item=\x, rightfrom=first67] (first34) {first};
    \node [item=\x, downfrom=first34]  (second34) {second};
    \path[action=\x](first34) edge node [label=\x] {a} (second34.north);
 }  

\end{tikzpicture}

我想写但不知道该如何写:

\begin{tikzpicture}[
  item/.style={draw=black!#1, thick, text=black!#1, minimum width = 60},
  label/.style={text=black!#1, right},
  action/.style={black!#1, ->, thick},
  downfrom/.style={below = 0.5 of {#1.south}, anchor=north},
  rightfrom/.style={right = 0.5 of {#1.north east}, anchor=north west}
]

\foreach \x in {100, 67, 34}
{
 \node ???
 \node ???
 \path ???
}   

\end{tikzpicture}

答案1

像这样吗?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}[
  item/.style={draw=black!#1, thick, text=black!#1, minimum width = 60},
  label/.style={text=black!#1, right},
  action/.style={black!#1, ->, thick, draw},
  downfrom/.style={below = 0.5 of {#1.south}, anchor=north},
  rightfrom/.style={right = 0.5 of {#1.north east}, anchor=north west}
]

\path coordinate(last);
\foreach \x in {100,67,34}
{
  \node[item=\x, rightfrom=last, alias=last](first\x){first};
  \node[item=\x, downfrom=first\x](second\x){second};
  \path[action=\x](first\x) edge node [label=\x] {a} (second\x);
}    
\end{tikzpicture}

\end{document}

答案2

您可以确定绘图范围并每次将其向右移动。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}[
  item/.style={draw=black!#1, thick, text=black!#1, minimum width = 60},
  label/.style={text=black!#1, right},
  action/.style={black!#1, ->, thick},
  downfrom/.style={below = 0.5 of {#1.south}, anchor=north},
  rightfrom/.style={right = 0.5 of {#1.north east}, anchor=north west}
]

\foreach \x[count=\xi from 0] in {100,64,37}
{
\begin{scope}[shift={(3*\xi,0)}]
    \node [item=\x]                     (first\x) {first};
    \node [item=\x, downfrom=first\x]  (second\x) {second};
    \path[action=\x](first\x) edge node [label=\x] {a} (second\x.north);
\end{scope}
}   

\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容