3

I have a nested foreach, where each loop is over the same number range. I want to apply a style dependent on the relation between the numbers (\m and \x in the example code), i.e., whether they are equal, the first is lower than the second, or the second is lower than the first one.

I prefer a solution with tikz styles as long as its not extremely complicated, other solutions are acceptable, too (like using macros).

Example Code

The code could look like this (with the stye conditional style not implemented).

\documentclass{minimal}
\usepackage{tikz} 
\begin{document}

\foreach \m in {1,...,3} {%
\noindent
\begin{tikzpicture}
    \foreach \x in {1,...,3} {%
        \node[conditional style=\x wrt \m] at (\x,0) {\x};
    }
\end{tikzpicture}
\par
}
\end{document}

Expected Result

In the following output, strike through, bold and plain are used as exemplary styles, because SO doesn't allow colored text afaik.

1 2 3

1 2 3

1 2 3

2 Answers 2

5

This was much easier than expected. The following style even allows specifying the conditional styles as arguments.

\documentclass{minimal}
\usepackage{tikz} 
\begin{document}

\foreach \m in {1,...,10} {%
\noindent
\begin{tikzpicture}[%
    compare/.code n args={5}{%
        \ifnum##1<##2
            \pgfkeysalso{##3}
        \else
            \ifnum##1>##2
                \pgfkeysalso{##5}
            \else
                \pgfkeysalso{##4}
            \fi
        \fi}
    ]
    \foreach \x in {1,...,10} {%
        \node[compare={\x}{\m}{blue}{black}{red}] at (\x,0) {\x};
    }
\end{tikzpicture}
\par
}
\end{document}

Output

enter image description here

3
  • I would recommend /.is choice for such stuff if you have the possibility to add more conditions. You also need more % since all of them are in the code block.
    – percusse
    Apr 29, 2017 at 11:01
  • @percusse I don't understand your remark with .is choice. Can you elaborate?
    – ziggystar
    Apr 29, 2017 at 11:08
  • added a quick answer
    – percusse
    Apr 29, 2017 at 11:42
3

You can evaluate a condition and pass it to your own style keys

\documentclass{article}
\usepackage{tikz}
\tikzset{
  mystyle/.is choice,
  mystyle/.default=0,
  mystyle/1/.style=mytrue,
  mystyle/0/.style=myeq,
  mystyle/-1/.style=myfalse,
  myeq/.style={black,draw},
  mytrue/.style=blue,
  myfalse/.style=red
}
\begin{document}
\begin{tikzpicture}
\foreach \m in {1,...,10}{%
  \foreach \x in {1,...,10}{%
    \pgfmathtruncatemacro\mychoice{ifthenelse(\x==\m,0,{ifthenelse(\x>\m,1,-1)})}
    \node[mystyle=\mychoice] at (\x,-\m) {\x};
  }
}
\end{tikzpicture}
\end{document}

enter image description here

Note that minimal class is not meant for users but for LaTeX developers. The actual minimal class for end users is article.

2
  • Your solution is more flexible with regard to the number of choices. But it requires evaluation of the condition (the if) at the use site. My solution places the logic into the style definition.
    – ziggystar
    Apr 29, 2017 at 11:51
  • @ziggystar You can move it to the style definition. I did it for readability.
    – percusse
    Apr 29, 2017 at 11:55

You must log in to answer this question.

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