\foreach 和 \draw 冲突?

\foreach 和 \draw 冲突?

在下面的代码中:

\documentclass{article}
\usepackage{tkz-berge}


\begin{document}
\def\kob{circle (3pt)} % kółko białe


\begin{tikzpicture}
\tikzset{VertexStyle/.style = {
shape = circle,
fill = black,
inner sep = 0pt,
outer sep = 0pt,
minimum size = 0pt,
draw}}
  \SetVertexNoLabel
\grPath[Math,prefix=p,RA=1,RS=0]{3}
\begin{scope}[xshift=-0.5 cm]
\grPath[Math,prefix=q,RA=1,RS=1]{4}
\Edges(q0,p0,q1,p1,q2,p2,q3)
\draw[fill=white] (q0)\kob;
\draw[fill=white] (q1)\kob;
\draw[fill=white] (p1)\kob;
\draw[fill=white] (p2)\kob;
\draw[fill=white] (q3)\kob;
\end{scope}

\end{tikzpicture}

\end{document}

我只想简单写

\foreach \k in (q0,q1,p1,p2,q3) {\draw[fill=white] (\k)\kob;}

但它会产生语法错误。是否有可能使用\draw指定的一组(非数值)节点?

答案1

您必须使用括号传递列表{ }

\documentclass{article}
\usepackage{tkz-berge}
\def\kob{circle (3pt)} % kółko białe

\begin{document}

\begin{tikzpicture}
\tikzset{VertexStyle/.style = {
  shape = circle,
  fill = black,
  inner sep = 0pt,
  outer sep = 0pt,
  minimum size = 0pt,
  draw}
}
\SetVertexNoLabel
\grPath[Math,prefix=p,RA=1,RS=0]{3}
\begin{scope}[xshift=-0.5 cm]
\grPath[Math,prefix=q,RA=1,RS=1]{4}
\Edges(q0,p0,q1,p1,q2,p2,q3)
%\draw[fill=white] (q0)\kob;
%\draw[fill=white] (q1)\kob;
%\draw[fill=white] (p1)\kob;
%\draw[fill=white] (p2)\kob;
%\draw[fill=white] (q3)\kob;
\foreach \k in {q0,q1,p1,p2,q3} {
   \draw[fill=white] (\k)\kob;
}
\end{scope}

\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

这不是对您的问题的直接回答,而是一个简短(模糊)的代码,可以在纯 TikZ 中产生相同的结果。

\documentclass[tikz,border=7mm]{standalone}
\begin{document}
  \tikz[x=.5cm,thick,v/.style={fill=white,draw,circle,inner sep=2pt}]
     \draw(2,0)--(6,0)(7,1)foreach[count=\i,evaluate={\j=mod(\i,2);}]~in{v,,v,v,,v,v}{--(\i,\j)node[~]{}};
\end{document}

在此处输入图片描述

相关内容