6

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?

  • 1
    offtopic: You should not use \documentclass{minimal}. – Roland Oct 27 '21 at 00:04
  • Roland, I use it exclusively «for testing or for showing a minimal working example», just as the picked answer in that question suggests… ¯_(ツ)_/¯ – Mariano Suárez-Álvarez Oct 27 '21 at 00:21
  • 2
    You cannot change the color of individual circles in a path, however, you can change the colors of individual circle nodes instead. –  Oct 27 '21 at 00: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 '21 at 00: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. – Mariano Suárez-Álvarez Oct 27 '21 at 00:28
  • That is why I wrote "offtopic" and that minimal can lead to problems. But If you don't want to take good advice, that's your business. – Roland Oct 27 '21 at 00:34
  • Here another question that explains the minimal class issue in detail. – Roland Oct 27 '21 at 01:43

3 Answers3

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

Zarko
  • 296,517
  • I think OP wants a conditional statement in his foreach loop. – Roland Oct 27 '21 at 01: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. – Mariano Suárez-Álvarez Oct 27 '21 at 02: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

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

polyn
  • 5,614