# Using \IfNoValueTF within a TikZ style specification.

In an answer to Flushing PGF commands and local variable Matthew had suggested using \IfNoValueTF within a TikZ \node specification to determine the text. This solution works great for me.

However, I would also like to able to have these type of conditions within the style without having to define a temporary style. So, in the following, I would like use LabelPointZ to be to determine the style to be used, just as the working LabelPointY determines the text without having an intermediate variable.

\documentclass{articles}
\usepackage{pgfplots}
\usepackage{xparse}

\begin{document}

\NewDocumentCommand{\LabelPointY}{o o m m g}{
node [#2]
{\IfNoValueTF{#5}{$(#3,#4)$}{#5}};
}

\NewDocumentCommand{\LabelPointZ}{o g m m g}{
node [\IfNoValueTF{#2}{color=red,right}{right, #2}]
{\IfNoValueTF{#5}{$(#3,#4)$}{#5}};
}

\begin{tikzpicture}
\begin{axis}
%\LabelPointZ[mark=*,color=red]{0.2}{0.2}
%\LabelPointZ[mark=*,color=red][color=green]{0.3}{0.3}
\LabelPointY[mark=*,color=red][color=blue, below right]{0.5}{0.5} % default label
\LabelPointY[mark=*,color=red][color=blue, below]{1  }{1  }{top}  % given label
\end{axis}
\end{tikzpicture}
\end{document}

-
Please don't use the minimal class for minimal working examples. It is not intended for this even when the name might suggest it. There is no reason to use it and it might cause issues because it only defines a minimal set of macros and has some common lengths set to zero. –  Martin Scharrer May 9 '11 at 13:01
@Martin: The first document linked to in the FAQ answer to meta.tex.stackexchange.com/questions/228/… uses this class, which might make people think it's the appropriate thing to use. Maybe you could incorporate your comment into the FAQ? –  Jake May 9 '11 at 13:33
@Jake: Thanks for pointing this out to me. That document also states quite at the beginning: "You may need to substitute the minimal class with another commonly available class, such as article, report or book." I better add some note to the FAQ. –  Martin Scharrer May 9 '11 at 13:43
@Martin: I have changed to \documentclass[article] as I had misunderstood the purpose of that class. –  Peter Grill May 9 '11 at 17:52

Having some code in the styles, even expandable ones, doesn't work well. Just move the \IfNoValueTF out from the TikZ style:

\NewDocumentCommand{\LabelPointZ}{o o m m g}{%
\IfNoValueTF{#2}{%
node [color=red,right]
}{%
node [right, #2]
}%
{\IfNoValueTF{#5}{$(#3,#4)$}{#5}};
}


This has however the drawback that you have to write some code twice.

Alternatively use the /utils/exec key to execute the \IfNoValueIF switch and use then \tikzset to set the styles. It looks quite funny but works stable.

\NewDocumentCommand{\LabelPointZ}{o o m m g}{
{\IfNoValueTF{#5}{$(#3,#4)$}{#5}};

I have been using a solution similar to your first example. But, can't find out exactly what /utils/exec is, so have posted a follow up question as it might be useful to others as well. –  Peter Grill May 9 '11 at 23:24