Graphics, Figures & Tablescounter and if

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
Roel
Posts: 4
Joined: Thu Apr 02, 2020 2:50 pm

counter and if

Post by Roel »

Hello

Having the following, working code

Code: Select all

\begin{tikzpicture}[x=.5cm,y=.5cm]
 \foreach \x in {1, 2, 3, 4, 8, 9, 10, 11} {
 	\foreach \y in {1, 2, 3, 4, 5}{
 		\fill [white] (\x,\y) circle (0.3);
		\draw [TheColor] (\x,\y) circle (0.3);
	   }
  }
\end{tikzpicture}
I would like to add a counter and an if-loop to fill some of the circles black. Something like

Code: Select all

\begin{tikzpicture}[x=.5cm,y=.5cm]
counter \i= 0;
 \foreach \x in {1, 2, 3, 4, 8, 9, 10, 11} {
 	\foreach \y in {1, 2, 3, 4, 5}{
 		\fill [white] (\x,\y) circle (0.3);
 		\ifnum \i in {1, 5, 7}{
 			\fill [black] (\x,\y) circle (0.3);
 		}
		\draw [TheColor] (\x,\y) circle (0.3);
		\i ++;
	   }
  }
\end{tikzpicture}
Any ideas?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Bartman
Posts: 369
Joined: Fri Jan 03, 2020 2:39 pm

counter and if

Post by Bartman »

In most cases, the helpers prefer complete examples.

I'm not sure how you want to address the circles. My guess suggests the following:

Code: Select all

\documentclass[tikz]{standalone}

\colorlet{TheColor}{red}% a way to prevent the error message

\newcounter{circleNumber}

\begin{document}
\begin{tikzpicture}[x=.5cm,y=.5cm]
    \foreach \x in {1, 2, 3, 4, 8, 9, 10, 11} {
        \foreach \y in {1, ..., 5}{
            \stepcounter{circleNumber}
            \draw [TheColor] (\x,\y) circle (0.3) 
                coordinate (\thecircleNumber)
            ;
        }
    }
    \foreach \circleNumber in {1, 5, 7}
        \fill (\circleNumber) circle (0.3);
\end{tikzpicture}
\end{document}
Roel
Posts: 4
Joined: Thu Apr 02, 2020 2:50 pm

counter and if

Post by Roel »

Wow, thanks for this answer. It was the answer I needed and you helped me a lot.
Post Reply