6

I am puzzled by something in tikzpicture. Here is the problematic code that i shrinked to its minimum:

\documentclass{article}
\usepackage{tikz}
\usepackage{xcolor}
\usepackage{pgfplotstable}
\usepackage{pgfplots}

\begin{document}

\newcommand{\Table}{ \begin{tikzpicture} \begin{axis} \foreach \A / \B/ \C in {1/100/g, 2/200/h} \addplot [ mark=diamond*, nodes near coords={g}] coordinates {(\B, \A)}; \end{axis}

\end{tikzpicture} }

\Table

\end{document}

This code works well and adds the label g next to the markers. But i would like this label to be a variable in the foreach loop. So i replace nodes near coords={g} by nodes near coords={\C}

But then I got an 'undefined control sequence' error. Could someone explain why is this the case? And also possible way to fix it?

Thanks in advance.

Qrrbrbirlbel
  • 119,821
Etienne
  • 115
  • 2
    Welcome to TeX.SX! Unrelated: pgfplotstable loads pgfplots, pgfplots loads tikz and tikz loads xcolor. So, it is sufficient to just load the pgfplotstable package. But you should add \pgfplotsset{compat=1.18} as noted in a warning you should get. – Jasper Habicht Sep 12 '23 at 13:17
  • 2
    The problem here is that PGFPlots doesn't “execute” the \addplots directly (or at least not all of it) but caches things for later. At that point \C isn't either defined anymore (because \foreach groups its body) or \C is just the last item (when a loop is used that doesn't group its body). – Qrrbrbirlbel Sep 12 '23 at 14:37
  • Thanks for your comments. Could you elaborate or direct me to documentation for what "groups its body" means? Thanks in advance. – Etienne Sep 13 '23 at 08:53

1 Answers1

5

Adaptations

  • The node is drawn later and then \C will be unknown. Therefore, you can use the option visualization depends on:
    visualization depends on={value \C\as\myC},
    nodes near coords={\myC}
    
  • Implemented suggestions from Jasper Habicht's comment

Code

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

\begin{document}

\newcommand{\Table}{ \begin{tikzpicture} \begin{axis} \foreach \A / \B / \C in {1/100/g, 2/200/h} \addplot[ mark=diamond*, visualization depends on={value \C\as\myC}, nodes near coords={\myC} ] coordinates {(\B, \A)}; \end{axis} \end{tikzpicture} }

\Table

\end{document}

Result

enter image description here

dexteritas
  • 9,161
  • Works perfectly. Thanks a lot. – Etienne Sep 13 '23 at 08:51
  • Actually, I would have a follow up question as this methods does not appear to work for colors of the marker. I tried to go back at my initial code that has a few more features and just adding the colors variable with the same methods seems to cause a problem. Should i create a separate post or is there a way to create follow up questions? – Etienne Sep 13 '23 at 09:13
  • 1
    It would be best to ask a new question, with the code you tried and provide a link to this question/answer for reference. – dexteritas Sep 13 '23 at 10:07
  • Thanks, this done here. – Etienne Sep 13 '23 at 10:39