圆圈未填充正确的颜色

圆圈未填充正确的颜色

下面的代码

\documentclass{minimal}
\usepackage{tikz}
\begin{document}
  \[
  \begin{tikzpicture}[
        every circle/.style={radius=2pt}
        ]
  \filldraw[draw=black] (0,0)
    \foreach \i in {3,0,1,0,2,2,1,4}
      {
      \ifnum \i > 0
      \foreach \j in { 1, ..., \i }
         { ++(0.5,0) circle[fill=black] }
      \fi
       ++(0.5,0) circle[fill=red]
      }
    \foreach \j in { 1, ..., 3 }
       { ++(0.5,0) circle[fill=black] }
    ;
  \end{tikzpicture}
  \]
\end{document}

产生以下内容: 在此处输入图片描述 为什么red某些圆圈的填充颜色规范被忽略了?

答案1

可能我不明白你的问题,但所需的图像我会画成两个循环,一个用于黑色圆圈,另一个用于红色圆圈:

\documentclass[border=3.141592, varwidth]{standalone}
\usepackage{tikz}

\begin{document}
    \[
\begin{tikzpicture}[
  C/.style = {circle, fill=#1, minimum size=5pt},
C/.default = black
                    ]
\foreach \i in {0,1,2, 5, 8,9, 11,12, 14, 16,17,18,19, 21,22,23}
{
\node[C] at (\i/2,0) {};
}
\foreach \i in {3,4, 6,7, 10, 13, 15, 20}
{
\node[C=red] at (\i/2,0) {};
}
\end{tikzpicture}
   \]
\end{document}

在此处输入图片描述

答案2

好的。使用 ABC 的建议,使用圆形节点代替圆形,这将变成

\documentclass{minimal}
\usepackage{tikz}
\begin{document}
  \[
  \begin{tikzpicture}[
        every node/.style={minimum size=5pt}
        ]
  \path (0,0)
    \foreach \i in {3,0,1,0,2,2,1,4}
      {
      \ifnum \i > 0
      \foreach \j in { 1, ..., \i }
         { ++(0.5,0) node[circle, fill=black] {} }
      \fi
       ++(0.5,0) node[circle, fill=red] {}
      }
    \foreach \j in { 1, ..., 3 }
       { ++(0.5,0) node[circle, fill=black] {} }
    ;
  \end{tikzpicture}
  \]
\end{document}

并产生 在此处输入图片描述

答案3

无节点

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    \pgfmathsetlengthmacro{\R}{4.5pt}
    \coordinate (X) at (0,0);
    \foreach \i in {3,0,1,0,2,2,1,4}
    {
        \ifnum \i > 0
        \foreach \j in { 1, ..., \i }
        { \fill (X) ++(0.5*\j,0) coordinate (Y) circle[radius=\R]; }
        \fi
        \fill[red] (Y) ++(0.5,0) coordinate (X) circle[radius=\R];
        \ifnum \i = 0
        \fill[red] (X) ++(0.5,0) coordinate (X) circle[radius=\R];
        \fi
    }
    \foreach \j in { 1, ..., 3 }
    { \fill (X) ++(0.5,0) coordinate (X) circle[radius=\R]; }
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容