4

Changing the out-degree value in the code below doesn't seem to be working. The figure I get always has an out-degree equal to 0. Any ideas why?

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{arrows, positioning}

\begin{document}

\begin{frame}
\begin{figure}
    \centering
    \begin{tikzpicture}
        [
            state/.style={fill=blue!30, circle, minimum size=0.5cm},
            transition/.style={>=stealth'},
        ]

        \node[state, label={-120:$q_1$}] (q1) {};

        \def\n{4}
        \foreach \q in {2,...,\n} {
            \pgfmathsetmacro\pq{\q-1}                       % previous node index
            \node[state, label={-120:$q_\q$}, right=of q\pq] (q\q) {};  % node
            \draw[transition, ->] (q\pq) to (q\q);                  % transition level 1

            % transition level 2
            \edef\tempa{\q}
            \edef\tempb{2}
            \ifx\tempa\tempb
            \else
                \pgfmathsetmacro\ppq{\q-2};     % 2nd previous node
                \edef\new{q\ppq};

                %%%% THIS OUT-DEGREE doesn't work
                \draw[transition, ->] (q\ppq) to[out=45, in=135] (q\q);                 
            \fi
        }

    \end{tikzpicture}
\end{figure}

\end{frame}

\end{document}

At the moment, the code generates this figure:

enter image description here

However, I expect to get something like this: enter image description here

6
  • That looks like 45 degrees to me, what do you expect to see? – Matt Jun 28 '15 at 5:02
  • I think they expected to see the arrow acting as though it is emerging from the center of the blue dot, just like the other side enters its node. – Paul Stiverson Jun 28 '15 at 5:16
  • @Matt Yes I expect to see what Paul described in his comment. I've updated my post to include a picture of what I want my final result to look like. – alguru Jun 28 '15 at 5:26
  • It seems like it is working properly, however when you use (q\ppq) tikz isn't understanding what you're referencing because it knows q2. I don't know how to do this drawing dynamically; is that a requirement? – Paul Stiverson Jun 28 '15 at 5:35
  • @PaulStiverson Yes, the code works properly when I hard-code the node names. I need to do this dynamically because I want to use this concept in a more complicated structure which is too long to hard-code. Thanks for your help though! – alguru Jun 28 '15 at 5:42
6

The problem is that \pgfmathsetmacro\ppq{\q-2}; set \ppq to 2.0 and (q2.0) is not the same as (q2). To avoid this you can use \pgfmathsetmacro\ppq{int(\q-2)}; or even better \pgfmathtruncatemacro\ppq{\q-2}; (as suggested by @percusse in the comment).

Here is an example of code (not exactly the same as yours):

\documentclass[tikz,border=7mm]{standalone}
\usetikzlibrary{arrows, positioning}

\begin{document}
  \begin{tikzpicture}
    [
      state/.style={fill=blue!30, circle, minimum size=0.5cm},
      transition/.style={>=stealth'},
    ]

    \node[state, label={-120:$q_1$}] (q1) {};

    \def\n{4}
    \foreach[evaluate={\pq=int(\q-1);\ppq=int(\q-2)}] \q in {2,...,\n} {
      \node[state, label={-120:$q_\q$}, right=of q\pq] (q\q) {};  % node
      \draw[transition, ->] (q\pq) to (q\q);                      % transition level 1
      \ifnum\q>2\relax
        \draw[transition, ->] (q\ppq) to[out=45, in=135] (q\q);   % transition level 2
      \fi
    }
  \end{tikzpicture}
\end{document}

enter image description here

4
  • or better \pgfmathtruncatemacro. This must be a duplicate of at least five but couldn't find even one :) – percusse Jun 28 '15 at 8:42
  • @percusse I know, but I either was not able to find one of the thousands ... finaly I have one ;) Flagged as duplicate. – Kpym Jun 28 '15 at 10:35
  • Thanks that's exactly what I needed. Sorry for the duplicated. I tried searching for answers before I posted the question but couldn't find any. Must've been using the wrong search terms. – alguru Jun 28 '15 at 14:27
  • @alguru You are welcome. As you can read in the comments, finding the existing answers is not always easy ;) – Kpym Jun 28 '15 at 15:24

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