我想用\foreach
命令绘图,但在下面的例子中,第一个命令tikzpicture
运行正常,第二个命令不运行。
但我觉得第二个应该和第一个一样,为什么不起作用呢?
\documentclass[border=10mm]{standalone}
\usepackage{tikz}
\begin{document}
% This one works
\begin{tikzpicture}
\draw[black] (0,0) \foreach \x/\y in {1/1,1/1.732} {
-- (\x,\y)
} -- cycle;
\end{tikzpicture}
% This one doesn't work
\begin{tikzpicture}
\draw[black] \foreach \x/\y in {0/0,1/1,1/1.732} {
(\x,\y) --
} cycle;
\end{tikzpicture}
\end{document}
答案1
\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\begin{document}
% This one works
\begin{tikzpicture}
\draw[black] (0,0) \foreach \x/\y in {1/1,1/1.732} {
-- (\x,\y)
} -- cycle;
\end{tikzpicture}
% And this also now works
\begin{tikzpicture}
\draw[black] \foreach \x/\y [count=\i]in {1/1, 1/1.732} {%
\ifnum\i=1(0,0)\else\fi -- (\x,\y)
} -- cycle;
\end{tikzpicture}
\end{document}