# Create a triangular array of red and black circles in TikZ

I would like to create a diagram similar to this using TikZ.

I can draw the circles with nested for-loops, as shown below; but I don't see an easy way to draw them in two different colors. Can someone help me to find an elegant solution?

\documentclass{amsart}
\usepackage{tikz}
\begin{document}
\begin{figure}
\begin{tikzpicture}[xscale=0.3,yscale=0.3]

\foreach \x in {0,...,19}
\foreach \y in {0,...,\x}
\draw [fill=red] (\x - \y / 2, \y * 0.866) circle (0.2);

\end{tikzpicture}
\caption{The 20th triangular number}
\end{figure}

\end{document}

• Welcome! Can you please complete your example so people can copy-paste-compile it? – cfr Jun 25 '17 at 22:42
• The fill color should be red if (y % 5) > (x % 5) and black otherwise. I guess that this can be done using \ifthenelse, but I don't know the TeX macro language yet. – Dave R Jun 26 '17 at 1:53
• Would have helped to know that earlier, possibly. Though I don't know what the % means. (modulo?) – cfr Jun 26 '17 at 1:57
• Added that method, so now I have 2 triangles ... – cfr Jun 26 '17 at 2:02

Not sure it is very elegant ...

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x [evaluate=\x as \xmod using {int(mod(\x,5))}] in {0,...,19}
{
\foreach \y [evaluate=\y as \ymod using {int(mod(\y,5))}, evaluate=\ymod as \k using { \ymod > \xmod ? "black" : "red" }] in {0,...,\x}
\draw [fill=\k] (\x - \y / 2, \y * 0.866) circle (0.2);
}
\end{tikzpicture}
\begin{tikzpicture}
\foreach \i  [evaluate=\i as \imod using {int(mod(\i,5))}] in {1,...,20}
{
\foreach \j [evaluate=\j as \jadj using {int(mod((int((floor(\j/6))+\j)),6))} ] in {1,...,\i}
{
\def\clr{red}
\filldraw [\clr] ( \j - \i/2 , -\i*.866  ) circle (.2);
}
}
\end{tikzpicture}
\end{document}


Will yield two copies of

• Nice! As you may know, rather than using the evaluate=\x as \xmod using... syntax it is also possible to use (still undocumented - I think) evaluate={\xmod=mod(\x,5);\ymod=mod(\y,5);}. Either way, the calculations in the top version can be compressed into one evaluate statement without int: \foreach \x in {0,...,19} \foreach \y [evaluate={\k=mod(\y,5)>mod(\x,5)?"black":"red";}] in {0,...,\x} – Mark Wibrow Jun 27 '17 at 6:01
• @MarkWibrow No, I didn't know you could do that. Thanks. (Probably/possibly because it is undocumented.) I do know you can use ? :. However, I had problems getting it to work in this case and gave up. I find it is hit-and-miss whether it works or not. Presumably, I'm missing some condition, but I've not been able to figure out what. – cfr Jun 27 '17 at 16:10

Stack it!

\documentclass{article}
\usepackage{stackengine,xcolor}
\stackMath
\renewcommand\useanchorwidth{T}
\newcommand\redc{\textcolor{red}{\bullet}}
\newcommand\blac{\textcolor{black}{\bullet}}
\newcommand\redt{\Shortstack{\redc{} \redc\redc{} \redc\redc\redc{} %
\redc\redc\redc\redc{} \redc\redc\redc\redc\redc}}
\newcommand\blat{\Shortstack{\blac\blac\blac\blac{} \blac\blac\blac{} %
\blac\blac{} \blac{} \vphantom{\blac}}}
\setstackgap{S}{-.078ex}
\begin{document}
\LARGE\stackunder[6pt]{%
\stackon{\redt\blat\redt\blat\redt\blat\redt}{%
\stackon{\redt\blat\redt\blat\redt}{%
\stackon{\redt\blat\redt}{%
\redt}}}%
}{\boldmath\normalsize\Longstack{\textsf{\bfseries{}Triangular Numbers}
\color{cyan}T(20)=\color{red}T(5)T(4)\color{cyan}+\color{black}T(4)T(3)}}
\end{document}


With a pic:

\documentclass{amsart}
\usepackage{tikz}
\usepackage{ifthen}
\tikzset{%
pics/blacktri/.style= {code={%
\foreach \y in {0,...,3}{%
\foreach \x in {0,...,\y}{%
\node[draw=black, fill=black] at (\x - \y/2, \y * 0.866) {};
}
}
}}
}

\begin{document}
\begin{figure}
\begin{tikzpicture}[xscale=0.3,yscale=0.3,
every node/.style={circle, minimum size=4pt, inner sep=0pt, outer sep=0pt}]
\foreach \x in {0,...,19}{%
\foreach \y in {0,...,\x}{%
\node[draw=red, fill=red] (P-\x-\y) at (\x - \y/2, \y * 0.866) {};
}
}
\foreach \x in {15,10,5}{%
\foreach \y in {11,6,1}{%
\ifthenelse{\y<\x}{%
\pic[xscale=0.3,yscale=0.3] at (P-\x-\y) {blacktri};
}{}
}
}
\end{tikzpicture}
\caption{The 20th triangular number}
\end{figure}

\end{document}