更改下面代码中的出度值似乎不起作用。我得到的数值的出度始终等于 0。知道为什么吗?
\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}
目前,代码生成以下图形:
然而,我期望得到这样的结果:
答案1
问题是\pgfmathsetmacro\ppq{\q-2};
设置\ppq
为2.0
和(q2.0)
与不一样(q2)
。为了避免这种情况,您可以使用\pgfmathsetmacro\ppq{int(\q-2)};
或甚至更好的方法\pgfmathtruncatemacro\ppq{\q-2};
(如@percusse在评论中所建议的那样)。
以下是代码示例(与您的代码不完全相同):
\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}