# Using the “foreach” option in TikZ to make a table

To show that the probability of rolling two die and getting a sum of 7 is 1/6, I wanted to make a table using "foreach" option in TikZ. The following code gives me a table of all the possible rolls.

The rectangles of adjacent pairs overlap. This makes for an unseemly appearance. How can I add horizontal space to separate the rectangles?

Using this code, can I replace the rectangles containing the six pairs of numbers that total 7 with a circle or ellipse?

How can I make the first number blue and the second number green?

\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{calc,angles,shapes,positioning,intersections,quotes,decorations.markings}

\begin{document}

\begin{tikzpicture}
\node foreach \x in {1,...,6} foreach \y in{1,...,6} [draw] at (\x-6,6-\y)
{(\x, \, \y)};
\end{tikzpicture}

\end{document}


## 3 Answers

% arara: pdflatex

\documentclass{amsart}
\usepackage{tikz}
\usepackage{xcolor}

\begin{document}
\begin{tikzpicture}[x=1.5cm,y=1.5cm]
\node foreach \x in {1,...,6} foreach \y in{1,...,6} [draw, circle] at (\x-6,6-\y)
{(\textcolor{blue}{\x}, \, \textcolor{green}{\y})};
\end{tikzpicture}
\end{document}


% arara: pdflatex

\documentclass{amsart}
\usepackage{tikz}
\usepackage{xcolor}

\begin{document}
\begin{tikzpicture}[x=1.8cm,y=1cm]
\foreach \x in {1,...,6} {%
\foreach \y in {1,...,6} {%
\draw (\x-6,6-\y) ellipse (.8cm and .3cm) node {$(\textcolor{blue}{\x},\textcolor{green}{\y})$};
}
}
\end{tikzpicture}
\end{document}


Edit:

Sorry, I got you wrong in the first place. Here you are:

% arara: pdflatex

\documentclass{amsart}
\usepackage{tikz}
\usepackage{xcolor}
\usepackage{xifthen}

\begin{document}
\begin{tikzpicture}[x=1.8cm,y=1cm]
\foreach \x in {1,...,6} {%
\foreach \y in {1,...,6} {%
\pgfmathtruncatemacro{\test}{\x+\y};
\ifthenelse{\test=7}
{\draw (\x-6,6-\y) ellipse (.8cm and .3cm) node {$(\textcolor{blue}{\x},\textcolor{green}{\y})$};}
{\draw (\x-6,6-\y) node [draw] {$(\textcolor{blue}{\x},\textcolor{green}{\y})$};}
}
}
\end{tikzpicture}
\end{document}


• I only want the pair of numbers that total 7 to be enclosed by a circle or ellipse. The others pairs should be enclosed by rectangles ... or not enclosed. – Adelyn Apr 16 '15 at 16:52
• What does the option [x=1.5cm,y=1.5cm] instruct TikZ to do? A reference to the manual would be helpful. – Adelyn Apr 16 '15 at 16:53
• @Adelyn Sorry, I got you wrong. I made an edit at the end of my answer. The option you are asking for is redefining the size of the coordinate system of your graphic. I believe, the default is 1cm. You have given unitless coordinates such as (\x-6,6-\y). Therefore, I just told the tikzpicture to interpret this as ((\x-6)*1.5 cm,(6-\y)*1.5 cm). – LaRiFaRi Apr 16 '15 at 21:44
• Yes, this is the display that I wanted. Thanks. This involves some programming! Right now, I am learning the usual command for TikZ. Please clarify one feature of your revised code. Are you saying that the units of the coordinate system are, by default, in centimeters? If this is the case, the command \draw (0,0) -- (1,1) would draw a line segment diagonally that is $\sqrt{2}$ centimeters in length. Is that correct? By typing [x=1.8cm,y=1cm] immediately after declaring a TikZ environment, you are saying to scale both directions by a factor of 1.8. Is that correct? – Adelyn Apr 16 '15 at 22:21
• @Adelyn The default coordinates of TikZ do not have any unit. But there is some default of course. Just do \draw (0,0) -- (1,0), print it, and measure if it is a centimeter. Or you search the manual for this information. But let's say, these are just "tikz-units". If they are too small for you, you can redefine them. x=3.14in, y=10000pt; whatever you want. – LaRiFaRi Apr 17 '15 at 7:14

You can also use a matrix and empty cell directives

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix,shapes.geometric}
% shorthand for less typing
\def\mcr{\pgfmatrixcurrentrow}\def\mcc{\pgfmatrixcurrentcolumn}
\begin{document}
\begin{tikzpicture}
\matrix (a) [execute at empty cell={\ifnum7=\numexpr\mcc+\mcr\relax
\node[ellipse,draw,inner sep=1pt]
{(\textcolor{blue}{\the\mcc},\textcolor{green}{\the\mcr})};
\else
\node[draw,inner sep=1pt]{(\textcolor{blue}{\the\mcc},\textcolor{green}{\the\mcr})};
\fi}]{&&&&&\\&&&&&\\&&&&&\\&&&&&\\&&&&&\\&&&&&\\};
\end{tikzpicture}
\end{document}


• As I told LaRiFaRi, this is some programming that I did not intend to get involved. I am learning the basic commands of TikZ. You did give me code that gives me the display that I wanted ... but, for now, I would manually input 36 different nodes. – Adelyn Apr 16 '15 at 22:27
• @Adelyn No problem. But what is above is basically this sentence: If the matrix entry is empty, sum it's row and column numbers and check if it is 7 and either do this or that. So no black magic is involved. – percusse Apr 17 '15 at 10:38
• I have not seen most of the commands in your code: \matrix, \pgfmatrixcurrentrow, \pgfmatrixcurrentcolumn, execute at empty cell=, and \fi. If I plan to use TikZ to make tables, I will refer to your code. I would have to get more familiar with TikZ commands. It will take awhile. Thanks. – Adelyn Apr 17 '15 at 13:07

An attempt with MetaPost, as sober as possible. To be processed with LuaLaTeX.

\documentclass[border=2mm]{standalone}
\usepackage{luamplib}
\mplibtextextlabel{enable}
\begin{document}
\begin{mplibcode}
u := 1.25cm; v := -cm; pair pos; path box; picture dices;
beginfig(1);
for i = 1 upto 6:
for j = 1 upto 6:
pos := ((i+1)*u, (j+1)*v);
dices :=  thelabel(decimal i & ", " & decimal j, pos);
box := bbox dices;
if i+j <> 7: draw box;
else:
d1 := xpart(lrcorner box - llcorner box) + labeloffset;
d2 := ypart(urcorner box - lrcorner box) + labeloffset;
draw fullcircle xscaled d1 yscaled d2 shifted  pos;
fi
draw dices;
endfor
endfor
endfig;
\end{mplibcode}
\end{document}