1

Into a tikzpicture, I'd like to apply different styles to nodes depending on their values. For example, in a list of integers, I'd like to color the node in red or in blue.

This is only a test for a bigger project, but an answer on this should help anyway.

The following code is working:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgffor}
\usepackage{ifthen}

\begin{document}

\begin{tikzpicture}
    \pgfmathdeclarerandomlist{nums}{{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}}
    \foreach \x in {1,...,10}
        {
        \pgfmathrandomitem{\choice}{nums}
        \ifthenelse{\choice<5}
            {
            \node[circle, fill=blue!50] at (\x,0) {\choice};
            }
            {
            \node[circle,fill=red!50] at (\x,0) {\choice};
            }
        }
\end{tikzpicture}

\end{document}

enter image description here

But, what I'd like to be able to do is creating a node style, depending on the value of the node. This way, I could add more cases and only use the \node line once. I tried the simpliest way to achieve that, but it doesn't work:

\begin{tikzpicture}
    \pgfmathdeclarerandomlist{nums}{{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}}
    \foreach \x in {1,...,10}
        {
        \pgfmathrandomitem{\choice}{nums}
        \def\clr{\ifthenelse{\choice<5}{blue!50}{red!50}}
        \node[circle, fill=\clr] at (\x,0) {\choice};
        }
\end{tikzpicture}

In fine, What I'd like to have is a series of ifthenelse into a tikzstyle. This didn't work either. Seems that \ifthenelse does not comply with every TikZ structure...

Thanks in advance if you know a way to help me with this.

1 Answer 1

3

Pgf/Tikz has its own if-then-else constructs, among othe mathematical operators. See section 95.2 Syntax for Mathematical Expressions: Operators of the manual.

What you proposed in you second snippet can actuallay be achieved with the following script.

\documentclass{article}
\usepackage{tikz}
\usepackage{pgffor}
\usepackage{ifthen}

\tikzset{
    conditionalcolor/.style={circle,fill=#1}
}

\begin{document}

\begin{tikzpicture}
    \pgfmathdeclarerandomlist{nums}{{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}}
    \foreach \x in {1,...,10}{
            \pgfmathrandomitem{\choice}{nums}
            \pgfmathsetmacro{\col}{ifthenelse(\choice<5,"blue!50","red!50")}
            \node[conditionalcolor=\col] at (\x,0) {\choice};
        }
\end{tikzpicture}

\end{document}

EDIT Here is a code where the random number is generated in an interval of integers.

\documentclass{article}
\usepackage{tikz}

\tikzset{
    conditionalcolor/.style={circle,fill=#1}
}

\begin{document}
\begin{tikzpicture}
    \foreach \x in {1,...,10}{
            \pgfmathtruncatemacro{\choice}{random(0,9)}
            \pgfmathsetmacro{\col}{ifthenelse(\choice<5,"blue!50","red!50")}
            \node[conditionalcolor=\col] at (\x,0) {\choice};
        }
\end{tikzpicture}

\end{document}
8
  • off-topic: I expect that after each compilation the result will be different, but it is not. Why?
    – Zarko
    Apr 25, 2020 at 17:17
  • @Zarko, \choice is selected randomly in the list, so it should be different
    – BambOo
    Apr 25, 2020 at 17:23
  • But it is not. At first compiling selection is random, but after then it is frozen. I test with TikZ 3.1.5b. Interestingly, If I repeat images, each images has different selection of numbers, but after first compilation they are not changes.
    – Zarko
    Apr 25, 2020 at 17:41
  • @Zarko There seems to be a new feature in our new LaTeX installations. This has nothing to do with the specifics of the above code. If I add \pgfmathtruncatemacro{\irnd}{int(100*rnd)} \typeout{\irnd} I get always the same number on my new TeXLive installation. I am pretty sure this did not happen on TeXLive2019, where I had to use \pgfmathsetseed to "freeze" my random numbers.
    – user194703
    Apr 25, 2020 at 17:44
  • 1
    You can copy this from the preamble of the tikzlings package: \ifluatex \let\pdfrandomseed\randomseed \fi \ifxetex \pgfmathsetseed{\time} \else \pgfmathsetseed{\number\pdfrandomseed} \fi
    – user194703
    Apr 25, 2020 at 18:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .