Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

conditional statements in tikz

5,652 views
Skip to first unread message

Fabrizio Iozzi

unread,
May 27, 2010, 6:07:58 AM5/27/10
to
Hi all,
I'm using tikz and I'm typing a table with the results of two dice. I
would like to print the couple of numbers in one color if the sum if
greater than, say, 6, and in another color otherwise; I wonder it tikz
has a sort of mechanism to get that without typing it manually (or
have a script compose the LaTeX code).
Now I'm using the basic example in the tutorial:
\foreach \x in {1,...,6}
\foreach \y in {1,...,6}
{
\draw (\x,\y) +(-.5,-.5) rectangle ++(.5,.5);
\draw (\x,\y) node{\x,\y};
}
Thanks in advance

Fabrizio

Alan Munn

unread,
May 27, 2010, 3:07:27 PM5/27/10
to
In article
<5bf0bb42-9b68-4f9e...@m33g2000vbi.googlegroups.com>,
Fabrizio Iozzi <fabrizi...@gmail.com> wrote:

There's probably a way to do this directly within tikz; and I'm sure
there's a more elegant way to do this, but the following does what you
want. -Alan


\documentclass{article}
\usepackage{tikz,ifthen,calc}
\newcounter{mysum}

\begin{document}

\begin{tikzpicture}


\foreach \x in {1,...,6}
\foreach \y in {1,...,6}
{

\setcounter{mysum}{\x+\y};


\draw (\x,\y) +(-.5,-.5) rectangle ++(.5,.5);

\ifthenelse{\value{mysum}>6}{\def\mycol{red}}{\def\mycol{blue}}
\draw[color=\mycol](\x,\y) node{\x,\y};
}

\end{tikzpicture}

\end{document}

0 new messages