5

I am following on one of my previous questions regarding the use of foreach loop in tikzpicture:

Here is a simple description with a code that works but with all markers colored in red:

\documentclass{article}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.18}

\begin{document}

\newcommand{\Table}{
    \begin{tikzpicture} 
        \begin{axis}
            \foreach \A / \B/ \C  in {1/100/green, 2/200/red}
                \addplot [
                    mark=diamond*,
                    color=red
                ] 
                coordinates {(\B, \A)};
        \end{axis}
    \end{tikzpicture}
}

\Table

\end{document}

In my case, i would like to make the color a variable in the foreach loop. I tried to adapt a methods that was suggested succesfully for marker label in my previous question. So i changed color=red for

visualization depends on={value \C\as\myC},
color={\myC}

But in that case it does not compile and return a 'undefined control sequence' error. I would be interested to understand the reason behin such problem, and possible way to fix it.

Thanks in advance.

1
  • I should have specified as a command that replacing 'color=red' by 'color={\C}' yield an error "undefined control sequence". This is why i used the 'visualization depends on' trick suggested earlier.
    – Etienne
    Commented Sep 13, 2023 at 14:33

1 Answer 1

7

Here an answer based on This solution here. It worked after adding the commands edef,temp and noexpand

\documentclass{article}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.18}
\begin{document}
\newcommand{\Table}{
    \begin{tikzpicture} 
        \begin{axis}
            \foreach \A / \B/ \C  in {1/100/green, 2/200/red,  3/300/blue} {%
            \edef\temp{%
                \noexpand
                \addplot [
                    mark=diamond*,
                    color=\C,
                ] 
                coordinates {(\B, \A)};
                }\temp
                }   
        \end{axis}
    \end{tikzpicture}
}
\Table
\end{document}

What I get:

enter image description here

5
  • 2
    +1 Do you know why it works? I also wonder (if) there isn't a simpler solution. (That's not a criticism. I even tried reading the documentation but didn't get anywhere.)
    – cfr
    Commented Sep 13, 2023 at 17:43
  • 3
    Disclaimer: I am not savvy with latex macros or pgfplots. From what I can understand from the documentation (page 547 of pgfplots manual), pgfplots does not evaluate the code that you write sequentially, but for some things it waits until it finds the end{axis} command. This produces a "crash", as loops must be evaluated where they are, not at the end of everything. Thus, the code that is added in the answer forces pgfplots to evaluate the loop without waiting. But take this with a grain of salt!
    – Jes
    Commented Sep 13, 2023 at 18:19
  • 3
    In this way, and from my understanding, noexpand tells pgfplots to wait, edef forces it to "go searching" for the value of the color and temp I have no idea, to be honest. I found a little bit of info here: overleaf.com/learn/latex/Articles/…
    – Jes
    Commented Sep 13, 2023 at 18:24
  • The Overleaf link is really useful - thanks! (I have an extremely fuzzy conception of expansion. More like a mental haze than a concept.)
    – cfr
    Commented Sep 13, 2023 at 19:00
  • It is still unclear to me why this works, but it does work! Thanks so much.
    – Etienne
    Commented Sep 14, 2023 at 18:33

You must log in to answer this question.

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