3

I can't overcome this little nasty issue. Using \ifnum I set labels position around the coordinates in my pgfplot. This works fine but :

I am only able to test if variable \coordindex is greater or smaller than a certain value. I want to be able to test if the index is equal to a number. Tex gives me this error, in case I use \ifnum\coordindex=0 at first.

Missing = inserted for \ifnum. \end{axis}

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.10}

\pgfplotsset{
    name nodes near coords/.style={
        every node near coord/.append style={
            anchor=center,                      % 
            name=#1\coordindex,                 % naming of npdes with running index
            alias=#1last,
        },
    },
    name nodes near coords/.default=coordnode
}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[%
        width=\textwidth,
        height=.3\textheight,
        scale mode = scale uniformly,
        scale only axis,
        xmin=-200,
        xmax= 250,
        ymin=- 50,
        ymax= 400,
        axis x line = middle,
        axis y line = left,
        grid = none,
        xtick = {-200, -100, ...,300},
        ytick = {-100, 0, ...,400},
        minor tick num = 1,
        ytick align = outside,
        extra x ticks={0},
        extra x tick style={grid=major},
        xlabel={x / mm},
        ylabel={y / mm},
    ]

        \addplot+[
            color=orange,
            ultra thick,
            shape=circle,
            nodes near coords={},
            every node near coord/.append style={
               label={
                   [black!80, label distance=-1ex]
                   \ifnum\coordindex<1
                       5
                   \else
%                      \ifnum\coordindex=3
%                          -135
%                      \else
                           180-\coordindex*45
%                      \fi
                   \fi
                   :$p_{\coordindex}$
                }
            },
            name nodes near coords=p
        ]
        table{%
            0           0     
            -79.9393    236.8749 
            143.0014    350.0007
            143.0014    300.0000
            200.0008    300.0000
        };  
    \end{axis}
\end{tikzpicture}
\end{document}

I also tried with the ifthen package but had no luck. Please does anybody have a quick solution to this? What am I doing wrong? According to the document this shouldn't arise.

Maybe one of you also has a hint on how to print the ylabel the same way the xlabel is set; just at the top of and/or beside the y label.

  • 1
    I'd say that \newcommand\equals{=} in the preamble and \ifnum\coordindex\equals 0 should work. At least no error is raised. See tex.stackexchange.com/questions/214995/… – egreg Dec 18 '14 at 14:10
  • You have to do the math somewhere else not where TikZ is looking for an anchor to place the label. – percusse Dec 18 '14 at 14:13
  • @egreg This does it. ThumbsUp. Thanks so much for the wuick hint and the link, which explains what i actually going on. Didn't know about this. – goeck Dec 18 '14 at 14:21
2

This is similar to Why does \ifnum not work inside of TikZ style definitions?, but the situation is different.

When processing label={...}, PGF looks for =; a workaround is thus hiding it:

        every node near coord/.append style={
           label={
               [black!80, label distance=-1ex]
               \ifnum\coordindex<1
                   5
               \else
                  \ifnum\coordindex\equals 3
                      -135
                  \else
                       180-\coordindex*45
                  \fi
               \fi
               :$p_{\coordindex}$
            }
        },

where \equals is defined in the preamble by

\newcommand{\equals}{=}

Another possibility:

        every node near coord/.append style={
           label={
               [black!80, label distance=-1ex]
               \numbercompare{\coordindex<1}
                 {5}
                 {\numbercompare{\coordindex=3}{-135}{180-\coordindex*45}}%
               :$p_{\coordindex}$
            }
        },

which requires

\usepackage{expl3}

\ExplSyntaxOn
\cs_set_eq:NN \numbercompare \int_compare:nTF
\ExplSyntaxOff
  • Maybe there is even a more elegant way to handle the discrimination than in this manner. The solution of the error is simple and elegant though! :-) – goeck Dec 18 '14 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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