TikZ 中节点名称的模算术?
假设我想在 TikZ 中构建一个循环图,其节点数量可变,标记为 v0、v1、...、v_n-1,代表我选择的 n 个节点(比如 5)。
我希望能够使用 TikZ 中的 \foreach 命令迭代地 (1) 定义节点,以及 (2) 连接节点。比方说,我希望能够将 v_i 连接到 v_(i+1),将 v_i 连接到 v_{i+2}。
\foreach \i in {0, ..., 4} {\path (\i*72:3) node (v\i) {};}
完美地构造了节点。但是当我想画一些线时,
\foreach \i \in {0, ..., 3} { \draw (v\i) -- (v{\i+1}); }
????
不起作用。 也不行
{0/1, ..., 3/4} 中的 \foreach \i / \j { \draw (v\i) -- (v{\j}); 我收到错误“
没有这样的形状 v0”或类似的错误。
我知道这一定很容易做到,但我不知道如何做。有什么建议吗?
作为后续,如果能够使用单个 \foreach
命令连接 v_i 和 v_{i+4} 或其他东西,并让 TikZ/pgf 为我执行模块化算术,而无需不必担心溢出。
Say I want to construct a circulant graph in TikZ, with a variable number of nodes, labelled v0, v1, ..., v_n-1 for some n of my choice (say, 5).
I'd like to be able to use the \foreach command in TikZ to iteratively (1) define the nodes, and (2) connect the nodes. I'd like to be able to connect v_i to v_(i+1) and v_i to v_{i+2}, say.
\foreach \i in {0, ..., 4}
{\path (\i*72:3) node (v\i) {};}
constructs the nodes perfectly. But then when I want to draw some lines,
\foreach \i \in {0, ..., 3}
????
{ \draw (v\i) -- (v{\i+1}); }
doesn't work. Nor does
\foreach \i / \j in {0/1, ..., 3/4}
{ \draw (v\i) -- (v{\j}); }
I get an error "no such shape v0" or something like that.
I know this must be totally easy to do, but I can't figure out how. Any suggestions?
As a followup, it would be nice to be able to connect v_i and v_{i+4} or something, with a single \foreach
command, and have TikZ/pgf do the modular arithmetic for me without having to worry about spilling over.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这对我有用:
语法
{0/1,...,5/6}< /code> 对我不起作用。但我承认,我花了一些时间试图找到一个好的解决方案,但我仍然不完全满意。
This worked for me:
The syntax
{0/1,...,5/6}
did not work for me. But I admit, I spent a bit of time trying to find a good solution, and I'm still not totally satisfied.另一个解决方案是通过 Steve Tjoa 编辑代码实现的:
Another solution, achieved editing the code by Steve Tjoa: