8

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

I need this. enter image description here

1
  • 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
13

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} {
        \fill [blue] (\x-\n/2,-\y) circle [radius=3pt];
    }
}
\end{tikzpicture}
\end{document}

tikz nested loops with evaluate

0
14

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} 
        \fill [blue] (\x,-\y) circle [radius=0.2];
\end{tikzpicture}
\end{document} 

enter image description here

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

Method 1 (looping)

User defined constants:

\FPset{H}{5}% Height

enter image description here

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pstricks-add}
\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{pstricks-add,tikz}
\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}

enter image description here

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

% 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}
1
  • 1
    Ziz iz not Tikz.
    – user10274
    Jul 25 '12 at 2:31
3

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} {%
        \fill [blue] (\x,-\y) circle [radius=3pt];  
        \fill [blue] (-\x,-\y) circle [radius=3pt];  
            }
    }
\end{tikzpicture}
\end{document}
1
  • 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

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.