我想使用三个连续(非嵌套)的 foreach 循环绘制一些或多或少高级的网格。由于未知原因,第三个循环似乎被忽略了...以下是代码:
\begin{tikzpicture}
\node at (22,3) (F) {$F$};
\foreach \i in {0,...,10}
{
\pgfmathtruncatemacro{\label}{\i}
\node at (20-2*\i,4) (A\label) {$v_{\label}$};
}
\foreach \i in {0,...,9}
{
\pgfmathtruncatemacro{\label}{\i}
\pgfmathtruncatemacro{\labelp}{\i + 1}
\node at (19-2*\i,2) (E\i) {$u_{\label}$}
(A\labelp) edge (E\label)
(E\label) edge (A\label)
(A\labelp) edge (A\label);
}
\foreach \i in {0,...,8}
{
\pgfmathtruncatemacro{\label}{\i}
\pgfmathtruncatemacro{\labelpp}{\i + 2}
(E\label) edge[bend left = 50] (E\labelpp);
}
\end{tikzpicture}
非常感谢您的帮助!
答案1
Harald 已经评论说\draw
第三个循环中缺少一个命令。因此,下一个代码只是想展示一种避免基于值的\pgfmathtruncatemacro
计算的可能性。labels
index
\documentclass[border=2mm,tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\node at (22,3) (F) {$F$};
\foreach \i [count=\label from 0] in {0,...,10}
{
% \pgfmathtruncatemacro{\label}{\i}
\node at (20-2*\i,4) (A\label) {$v_{\label}$};
}
\foreach \i [count=\label from 0, count=\labelp] in {0,...,9}
{
% \pgfmathtruncatemacro{\label}{\i}
% \pgfmathtruncatemacro{\labelp}{\i + 1}
\node at (19-2*\i,2) (E\i) {$u_{\label}$}
(A\labelp) edge (E\label)
(E\label) edge (A\label)
(A\labelp) edge (A\label);
}
\foreach \i [count=\label from 0, evaluate=\label as \labelpp using \label+2] in {0,...,7}
{
% \pgfmathtruncatemacro{\label}{\i}
% \pgfmathtruncatemacro{\labelpp}{\i + 2}
\draw (E\label) edge[bend left=50] (E\labelpp);
}
\end{tikzpicture}
\end{document}
答案2
你想要这样的东西吗?将第三个\foreach
循环的边界设置为 {-1,...,7} 并添加语句\node
:
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node at (22,3) (F) {$F$};
\foreach \i in {0,...,10}
{
\pgfmathtruncatemacro{\label}{\i}
\node at (20-2*\i,4) (A\label) {$v_{\label}$};
}
\foreach \i in {0,...,9}
{
\pgfmathtruncatemacro{\label}{\i}
\pgfmathtruncatemacro{\labelp}{\i + 1}
\node at (19-2*\i,2) (E\i) {$u_{\label}$}
(A\labelp) edge (E\label)
(E\label) edge (A\label)
(A\labelp) edge (A\label);
}
\foreach \i in {-1,...,7}
{
\pgfmathtruncatemacro{\label}{\i}
\pgfmathtruncatemacro{\labelpp}{\i + 2}
\node at (17-2*\i,2) (E\label) {}
(E\label) edge[bend left = 50] (E\labelpp);
}
\end{tikzpicture}
\end{document}