Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

I need this. enter image description here

share|improve this question
    
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
add comment

4 Answers

up vote 12 down vote accepted

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

share|improve this answer
    
Thanks Jake!!!! –  Regis da Silva Jul 22 '11 at 5:51
add comment

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

share|improve this answer
    
Yes, this is a much more elegant way to do it! –  Jake Jul 25 '12 at 6:17
add comment

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}
share|improve this answer
    
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
add comment

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}
share|improve this answer
1  
Ziz iz not Tikz. –  Marc van Dongen Jul 25 '12 at 2:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.