我有以下代码-
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,automata,calc,shapes, positioning}
\begin{document}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,node distance=3.0cm,thick]
\tikzstyle{every state}=[fill=white,draw=black,text=black,minimum size=45pt]
\node[state] (A) {$A$};
\node[state] (B) [right of=A] {$B$};
\node[state] (C) [right of=B] {$C$};
\path (A) [loop above] edge (A);
\path (A) edge (B)
(B) edge (C)
(A) edge [bend left=45] node [above, xshift=-2mm,yshift=-3.5mm,very near start] {$1$} (B)
(A) edge [bend left=45] (C);
% Hard-coded the token location since i don't know about it.
\fill[black] (0.00,1.75) circle (2pt);
\end{tikzpicture}
\end{document}
这将生成如下图像 -
但是,我想在边缘添加一些文字。因此,我希望我的输出是这样的 -
我如何在 Tikz 中实现这一点?
谢谢!Raj
答案1
每个 tikz 节点都有 360 个锚点,使用符号 表示nodename.angle
,是angle
0 到 360 之间的整数。这些锚点位于节点形状与给定角度的半径的交点处。您可以使用这些锚点设置圆弧的起点,也可以使用 表示标签的起点$1$
。
更新:此外,关于您“手动”放置的点,还有一点改进。以极坐标形式给出该点相对于节点的坐标更加方便A
。该点位于 以北 9 毫米处A.north
,因此您可以将其坐标指定为(A.north) +(90:9.1mm)
。该9.1mm
部分仍然是手动估计的,但使坐标相对可以避免在移动节点时重新计算它们A
。
仅引用相关部分:
\path (A) [loop above] edge (A);
\path (A) edge (B)
(B) edge (C)
(A.20) edge [bend left=30] (B)
(A.22) node[above] {$1$}
(A.50) edge [bend left=45] (C)
(A.52) node[above] {$1$};
\fill[black] (A.north) +(90:9.1mm) circle (2pt);
生成:
第二次更新
在边中添加标记的正确方法是使用修饰markings
。语法有点丑陋,因为修饰必须添加到 中postaction
,但使用下面提供的示例,您可以轻松地将标记添加到图中的任何边。
\documentclass{article}
\usepackage{tikz}
\begin{document}
\usetikzlibrary{arrows,automata,decorations.markings}
\tikzset{ % Use this style and postaction={decorate} to put a token in an edge
put token/.style={
decoration={markings, mark=at position 0.5 with {\fill[red] circle(2pt); }},
},
}
\begin{tikzpicture}[node distance=3.0cm,thick]
\tikzset{
every state/.append style={fill=white,draw=black,text=black,minimum size=45pt},
every edge/.append style={->,>=stealth',shorten >=1pt}
}
\node[state] (A) {$A$};
\node[state] (B) [right of=A] {$B$};
\node[state] (C) [right of=B] {$C$};
\path (A) edge[loop above, put token, postaction={decorate}] (A);
\path (A) edge (B)
(B) edge (C)
(A.20) edge [bend left=30] (B)
(A.22) node[above] {$1$}
(A.50) edge [bend left=45] (C)
(A.52) node[above] {$1$};
\end{tikzpicture}
\end{document}
答案2
这是最终的代码 -
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,automata,calc,shapes, positioning}
\begin{document}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,node distance=3.0cm,thick]
\tikzstyle{every state}=[fill=white,draw=black,text=black,minimum size=45pt]
\node[state] (A) {$A$};
\node[state] (B) [right of=A] {$B$};
\node[state] (C) [right of=B] {$C$};
\path (A) [loop above] edge (A);
\path (A) edge (B)
(B) edge (C)
(A) edge [bend left=30] node [above, xshift=-1mm,yshift=-0.85mm,very near start] {$1$} (B)
(A) edge [bend left=50] node [above, xshift=-5mm,yshift=-3.5mm,very near start] {$1$} (C);
% Hard-coded the token location since i don't know about it.
\fill[black] (0.00,1.75) circle (2pt);
\end{tikzpicture}
\end{document}