0

Using a simplified version of an older answer as MWE:

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

\begin{document} \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} \end{document}

This works as intended. But, according to the manual, I should also be able to just specify the macro the save instead of assigning it a new name. So:

                \addplot[
                 mark=diamond*,
                 visualization depends on={value \C},
                 nodes near coords={\C}
                ]

However, this fails with the following error:

Package pgfplots Error: Sorry, `visualization depends on=value <
\macro>' expected a defined control sequence name instead of `\C '. Please make
 sure `\C ' is a properly defined macro or use the `visualization depends on=va
lue <content> \as<\macro>' syntax instead.

Why does this version fail? It looks like a space slipped into the macro name. Is this a bug?

TexMax
  • 93

1 Answers1

0

visualization depends on is used when the visualization of a point depends on more than just point meta. As your \addplot only has one point, it is not really applicable here although it does work. - see my code with and without visualization depends on.

What does not work is your loop - see section 8.1 in the manual about \pgfplotsinvokeforeach. -and comment by @Qrrbrbirlbel in your linked question

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).

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.18}

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

Graph with a single point and label

  • As your \addplot only has one point, it is not really applicable here

    It is as point meta cannot store textual information.

    What does not work is your loop

    That is exactly the point why I'm using visualization depends on. It stores the macro content that is already out of scope. Simply removing the loop does not only change the output but also avoids the original problem, I'm afraid. (My non-reduced code absolutely needs the loop, too.)

    – TexMax Nov 15 '23 at 00:04
  • True point meta does not store text, but the original macro does. When you only have one point in \addplot you could use the original macro except that it does not work in a loop - see my answer and the original linked question/answer. I can not explain further. – hpekristiansen Nov 15 '23 at 03:47