我想使用 TIKZ 绘制一个圆圈,而不是手动放置节点并连接节点,我想使用循环,就像任何理智的人都会做的那样。现在,当我写这些时,传入的边缘似乎会稍微指向节点的某个点,而不是指向其核心。
在示例中,我添加了一个手动写出路径的版本。我快疯了 :D 有人知道这里发生了什么吗?也欢迎任何风格批评!
\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{automata}
\begin{document}
\begin{tikzpicture}[nodes=state]
\def \number {8}
\def \radius {2cm}
\def \degree {360/\number}
\foreach \s in {1,...,\number}
{
\node at ({\degree * (\s -1)}:\radius) (\s) {$u_\s$};
}
\foreach \s in {1,...,\number}
{
\pgfmathsetmacro\result{Mod( (\s), \number)+1}
\path (\s) edge[bend right = 15] (\result);
}
\end{tikzpicture}
\begin{tikzpicture}[nodes=state]
\def \number {8}
\def \radius {2cm}
\def \degree {360/\number}
\foreach \s in {1,...,\number}
{
\node at ({\degree * (\s -1)}:\radius) (\s) {$u_\s$};
}
\path (1) edge[bend right = 15] (2)
% edge (5)
(2) edge[bend right = 15] (3)
% edge (6)
(3) edge[bend right = 15] (4)
% edge (7)
(4) edge[bend right = 15] (5)
% edge (8)
(5) edge[bend right = 15] (6)
(6) edge[bend right = 15] (7)
(7) edge[bend right = 15] (8)
(8) edge[bend right = 15] (1)
;
\end{tikzpicture}
\end{document}
答案1
你需要
\pgfmathtruncatemacro\result{Mod( (\s), \number)+1}
放下.0
,这将被解释为节点锚点(东锚点)。或者evaluate
与 一起使用int
。
\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{automata}
\begin{document}
\begin{tikzpicture}[nodes=state]
\def \number {8}
\def \radius {2cm}
\def \degree {360/\number}
\foreach \s in {1,...,\number}
{
\node at ({\degree * (\s -1)}:\radius) (\s) {$u_\s$};
}
\foreach \s [evaluate=\s as \result using {int(Mod( (\s), \number)+1)}]in {1,...,\number}
{
\path (\s) edge[bend right = 15] (\result);
}
\end{tikzpicture}
\end{document}
答案2
如果问题只是将圆圈排列在圆上,这可以做得相对简单:
\documentclass[tikz, margin=10pt]{standalone}
\begin{document}
\begin{tikzpicture}[
C/.style = {circle, draw, fill=white, minimum size=7mm,
inner sep=0pt, outer sep=0pt}
]
\def \number {8}
\def \radius {2cm}
\def \degree {360/\number}
% circle
\draw (0,0) circle[radius=\radius];
% nodes on circle
\foreach \i [count=\j from 0] in {1,...,\number}
\node (n\i) [C] at (\j*\degree:\radius) {$u_\i$};
\end{tikzpicture}
\end{document}
或者我错过或者误解了什么?