3

Trying to use \foreach for node placement and coloring fails. Node label becomes e.g. redredcaptionA where red is the desired color and captionA is the node label text.

Desired result would be a node with captionA as label text in red font color.

MWE

\documentclass[tikz]{standalone}
  \begin{document}
    \begin{tikzpicture}
      \foreach \i/\text/\color in {{1,1}/captionA/{red},{2,2}/captionB/{blue}}
        {\node[text=\color] at (\i) {\text};}
\end{tikzpicture}
\end{document}

I have seen some related posts, where the node definition needs to be in an \edef definition but it's not solving the problem.

1 Answer 1

4

It seems to be because you're (locally) redefining \color, use for example \ccolor as the loop variable instead.

enter image description here

\documentclass[tikz]{standalone}
  \begin{document}
    \begin{tikzpicture}
      \foreach \ii/\ttext/\ccolor in {{1,1}/captionA/{red},{2,2}/captionB/{blue}}
        {\node[text=\ccolor] at (\ii) {\ttext};}
\end{tikzpicture}
\end{document}
1

You must log in to answer this question.

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