5

The following code

\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}

produces the following: enter image description here Why is the red color specification for the fill of soem of the circles being ignored?

7
  • 1
    offtopic: You should not use \documentclass{minimal}.
    – Roland
    Oct 27 at 0:04
  • Roland, I use it exclusively «for testing or for showing a minimal working example», just as the picked answer in that question suggests… ¯_(ツ)_/¯ Oct 27 at 0:21
  • 2
    You cannot change the color of individual circles in a path, however, you can change the colors of individual circle nodes instead.
    – user255043
    Oct 27 at 0:21
  • @Mariano Suárez-Álvarez Yes but what these answer also say is that minimal do not load any macros. This could have easily caused your problem. In this particular case you were lucky, but for future MWE you should use article or standalone which is also common best practice.
    – Roland
    Oct 27 at 0:26
  • Dear Roland, I know that it is not minimal that caused the problem, since the file I posted in the question is the result of distilling the problem out of a complete tex file which compiles to some 200 pages and has a prelude of some 200 lines. Oct 27 at 0:28
4

Probably I don't understand your problem, but desired image I would draw in two loops, one for black circles, and another for the red ones:

\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}

enter image description here

2
  • I think OP wants a conditional statement in his foreach loop.
    – Roland
    Oct 27 at 1:41
  • The intention was, really, not to have to compute the lists of coordinates that you computed. Of course, one can do that programmatically, but the code I put in the OP completely avoids, and the second loop. Oct 27 at 2:28
3

Ok. Using ABC's suggestion of using circular nodes instead of circles, this becomes

\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}

and produces enter image description here

1
  • It is ok to accept your own answer - accept it! Oct 27 at 9:07
3

without nodes

\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}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.