关于tikz中 scope 环境与 foreach 相关的一个问题

发布于 2022-07-28 19:32:40

我使用scope环境 和 foreach 语法输出了下图: e428e0d7c78df515b04d6a5aeb2e4649.png

我的代码是

\begin{tikzpicture}
\begin{scope}
\foreach \x/\y in {1/A,2/B,3/C,4/D}
{\node[inner sep=0pt](a\x)at(\x,0){$\y$};}
\foreach \x/\y in {a1/a2,a2/a3,a3/a4}
{\draw(\x)--(\y);}
\end{scope}

\begin{scope}[shift={(-1,-.8)}]
\foreach \x/\y in {1/E,2/F,3/G,4/H}
{\node[inner sep=0pt](a\x)at(\x,0){$\y$};}
\foreach \x/\y in {a1/a2,a2/a3,a3/a4}
{\draw(\x)--(\y);}
\end{scope}
\end{tikzpicture}

现在的问题是:我想连接 A 点和 E 点,D 点和 H 点,但现在A 点和 E 点的坐标相同,D 点和 H 点坐标也相同,直接使用draw 命令不行,必须修改第二个 scope 里面的a1,a4名称,变成其他名称。我想问能否不这样操作, 使用什么方法对以上两个 scope 加以区分,然后使得那两对点的坐标不同,可以正常使用draw命令和 foreach 语法。请问该怎么操作?

查看更多

关注者
0
被浏览
691
M
M 2022-07-28
这家伙很懒,什么也没写!

scope 上添加一个 name prefix 属性就可以区别了

\begin{tikzpicture}
    \begin{scope}[name prefix = l1-]
    \foreach \x/\y in {1/A,2/B,3/C,4/D}
    {\node[inner sep=0pt](a\x)at(\x,0){$\y$};}
    \foreach \x/\y in {a1/a2,a2/a3,a3/a4}
    {\draw(\x)--(\y);}
    \end{scope}
    
    \begin{scope}[name prefix = l2-, shift={(-1,-.8)}]
    \foreach \x/\y in {1/E,2/F,3/G,4/H}
    {\node[inner sep=0pt](a\x)at(\x,0){$\y$};}
    \foreach \x/\y in {a1/a2,a2/a3,a3/a4}
    {\draw(\x)--(\y);}
    \end{scope}

    \foreach \x in {1,4}{
        \draw [red] (l1-a\x) -- (l2-a\x);
    }
    
\end{tikzpicture}

fafa420503b07341773401052ce2371d.png

同理,还有 name suffix

1 个回答

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览