TransWikia.com

Using node style color in foreach loop messes the node label text up. Why and how to fix?

TeX - LaTeX Asked on July 15, 2021

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.

One Answer

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}

Correct answer by Torbjørn T. on July 15, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP