我有以下代码:
\begin{tikzpicture}[node distance=2cm, auto]
\node (0) {\Large$0$};
\node (1) [right of=0] {\Large$1$};
\node (2) [right of=1] {\Large$2$};
\node (3) [right of=2] {\Large$3$};
\node (4) [right of=3] {\Large$\cdots$};
\draw[->] (1.160) to node {\small$(0,1)$} (0.20);
\draw[->] (1.200) to node [swap] {\small$(1,0)$} (0.340);
\draw[->] (2.160) to node {\small$(0,1)$} (1.20);
\draw[->] (2.200) to node [swap] {\small$(1,0)$} (1.340);
\draw[->] (3.160) to node {\small$(0,1)$} (2.20);
\draw[->] (3.200) to node [swap] {\small$(1,0)$} (2.340);
\draw[->] (4.169) to node {\small$(0,1)$} (3.20);
\draw[->] (4.191) to node [swap] {\small$(1,0)$} (3.340);
\end{tikzpicture}
这给了我以下图片:
它基本上工作正常,但我决定切换所有箭头的方向,它之前给了我这个:
这基本上就是我想要的。
所以我的问题是如何调整边缘的标签位置?
答案1
万一没人回答,我很乐意撤回这个答案。问题是你明确指定了所有角度。因此,在一个方向上工作正常的方法,如果反转方向,将无法正常工作。因此,你可以交换角度,即和160 <-> 200
,20 <-> 340
或者,更简单的是,交换 s 的位置[swap]
。当然,这些程序的结果不同。
\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}[node distance=2cm, auto]
\node (0) {\Large$0$};
\node (1) [right of=0] {\Large$1$};
\node (2) [right of=1] {\Large$2$};
\node (3) [right of=2] {\Large$3$};
\node (4) [right of=3] {\Large$\cdots$};
\draw[->] (1.160) to node [swap] {\small$(0,1)$} (0.20);
\draw[->] (1.200) to node {\small$(1,0)$} (0.340);
\draw[->] (2.160) to node [swap] {\small$(0,1)$} (1.20);
\draw[->] (2.200) to node {\small$(1,0)$} (1.340);
\draw[->] (3.160) to node [swap] {\small$(0,1)$} (2.20);
\draw[->] (3.200) to node {\small$(1,0)$} (2.340);
\draw[->] (4.169) to node [swap] {\small$(0,1)$} (3.20);
\draw[->] (4.191) to node {\small$(1,0)$} (3.340);
\end{tikzpicture}
\end{document}
答案2
另一种解决方案:基于tikz
库的使用chains
,并且positioning
由于所有箭头标签似乎都相同,因此使用循环在节点之间绘制它们:
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{chains, positioning}
\begin{document}
\begin{tikzpicture}[
node distance = 1cm,
start chain = A going right,
num/.style = {font=\Large, on chain=A},
]
\node[num] {$0$}; % node name: A-1
\node[num] {$1$};
\node[num] {$2$};
\node[num] {$3$};
\node[num] {$\cdots$}; % node name: A-5
%
\foreach \i [evaluate=\i as \j using int(\i-1)] in {5,...,2}
{
\draw[->,transform canvas={yshift=+1mm}] (A-\i) -- node[above,font=\small] {$(0,1)$} (A-\j);
\draw[->,transform canvas={yshift=-1mm}] (A-\i) -- node[below,font=\small] {$(1,0)$} (A-\j);
}