# How to create a triangle of dots in TikZ using the foreach command?

How to create a triangle of dots in TikZ using the foreach command?

I need this.

-
Could you accept Altermundus' answer instead of mine? It's a much more elegant way to achieve this. –  Jake Jul 25 '12 at 6:18

You can use the option [evaluate = <variable> as <macro> using <expression>] to calculate a new value based on a counter, which can then be used in an inner counter:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\foreach [evaluate = \y as \n using \y*2-1] \y in {1,...,5} {
\foreach \x in {1,...,\n} {
}
}
\end{tikzpicture}
\end{document}


-
Thanks Jake!!!! –  Regis da Silva Jul 22 '11 at 5:51

The simplest solution that respects the question of the OP (a triangle of dots with Tikz) seems to be :

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\foreach  \y in {0,...,4}
\foreach \x in {-\y,...,\y}
\end{tikzpicture}
\end{document}


-
Yes, this is a much more elegant way to do it! –  Jake Jul 25 '12 at 6:17

Jake's answer is very neat, and I was happy to learn about evaluate, but you do not need it. The following does the trick using more elementary code:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \y in {0,...,4} {%
\foreach \x in {0,...,\y} {%
}
}
\end{tikzpicture}
\end{document}

-
You're right, there's always more than one way to achieve a goal. I hope you don't mind if I point out two things that could be perceived as drawbacks with your approach: 1) You have to repeat the \fill ... line, which could become a bit annoying if you want to change the colour, shape or radius of the points. 2) The dots in the middle column are drawn twice, which can lead to undesirable results if you use transparency. –  Jake Jul 26 '11 at 23:31

## Method 1 (looping)

User defined constants:

\FPset{H}{5}% Height


\documentclass[pstricks,border=12pt]{standalone}
\usepackage[nomessages]{fp}

% user defined constants
\FPset{H}{5}% Height

% internal used constants
\FPeval{Size}{H-1}

\begin{document}

\begin{pspicture}(-\Size,-\Size)(\Size,0)
\multido{\iY=0+1}{\H}{%
\FPeval{N}{round(2*\iY{}+1:0)}%
\multirput(-\iY,-\iY)(1,0){\N}{\pscircle*[linecolor=blue](0,0){5pt}}}
\end{pspicture}

\end{document}


## Method 2 (looping with TikZ foreach)

\documentclass[pstricks,border=12pt]{standalone}
\usepackage[nomessages]{fp}

% user defined constants
\FPset{H}{5}% Height

% internal used constants
\FPeval{Size}{H-1}

\begin{document}

\begin{pspicture}(-\Size,-\Size)(\Size,0)
\foreach  \y in {0,...,\H}
\foreach \x in {-\y,...,\y}
{\pscircle*[linecolor=blue](\x,-\y){5pt}}
\end{pspicture}

\end{document}


## Method 3 (clipping and looping)

User defined data:

\FPset{N}{6}


\documentclass[border=-0.5cm,pstricks]{standalone}%a negative length is set to border to trim the unwanted white spaces!

% user defined data
\FPset{N}{6}

% internal used data
\FPeval{H}{round(N-1:0)}
\FPeval{W}{round(2*H+1:0)}

\pstVerb{/Left {\N\space 0.25 2 sqrt mul \H\space add neg} def}

\begin{document}

\begin{pspicture}(-\N,-\N)(\N,1)
\psclip{\pspolygon[linestyle=none](0,0.5)(!Left)(!Left 2 1 roll neg exch)}
\multirput(0,0)(0,-1){\N}{\multirput(-\H,0)(1,0){\W}{\pscircle*(0,0){5pt}}}
\endpsclip
\end{pspicture}

\end{document}

-
Ziz iz not Tikz. –  Marc van Dongen Jul 25 '12 at 2:31