Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}{
  \addplot [#1] coordinates{(#3,#4)} 
    node [#2]
        {\IfNoValueTF{#5}{$(#3,#4)$}{#5}};
}

\NewDocumentCommand{\LabelPointZ}{o g m m g}{
  \addplot [#1] coordinates{(#3,#4)} 
    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}
share|improve this question
2  
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

1 Answer 1

up vote 2 down vote accepted

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}{%
    \addplot [#1] coordinates{(#3,#4)} 
        node [color=red,right]
  }{%
    \addplot [#1] coordinates{(#3,#4)} 
        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}{
  \addplot [#1] coordinates{(#3,#4)} 
    node [/utils/exec={\IfNoValueTF{#2}{\tikzset{color=red,right}}{\tikzset{right, #2}}}]
        {\IfNoValueTF{#5}{$(#3,#4)$}{#5}};
}
share|improve this answer
    
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
    
@Peter: It is a PGF/TikZ key so it is documented in the PGF/TikZ manual. It can be found by a simple search in the PDF. Like I wrote it simply executes the given code. –  Martin Scharrer May 10 '11 at 7:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.