我如何修复 tikzcd 图表上的标签位置?

我如何修复 tikzcd 图表上的标签位置?

我对交换图上的标签位置感到困惑。

这是我的代码:

\documentclass{article}

\usepackage{tikz-cd}

\begin{document}

\begin{tikzcd}[ampersand replacement = \&]
    0 \arrow[r, "N\lambda", bend left] \& 1 \arrow[r, "(N - 1)\lambda", bend left] \arrow[l, "\lambda", bend left] \& 2 \arrow[r, "(N - 2)\lambda", bend left] \arrow[l, "2\lambda", bend left] \& \cdots \arrow[r, "2\lambda", bend left] \arrow[l, "3\lambda", bend left] \& N-1 \arrow[r, "\lambda", bend left] \arrow[l, "(N-1)\lambda", bend left] \& N \arrow[l, "N\lambda", bend left]
\end{tikzcd}

\begin{tikzcd}[ampersand replacement = \&]
    0 \arrow[r, "N\lambda", bend left] \& 1 \arrow[r, "(N - 1)\lambda", bend left] \arrow[l, "\lambda", bend left] \& 2 \arrow[r, "(N - 2)\lambda", bend left] \arrow[l, "2\lambda", bend left] \& 3 \arrow[r, "2\lambda", bend left] \arrow[l, "3\lambda", bend left] \& 4 \arrow[r, "\lambda", bend left] \arrow[l, "(N-1)\lambda", bend left] \& 5 \arrow[l, "N\lambda", bend left]
\end{tikzcd}

\end{document}

产生

在此处输入图片描述

我希望箭头标签显示在箭头的“中心”,就像在第二个图中一样,但节点标签与第一个图中的一样。我该如何解决这个问题?

注意:我已经看过这个问题这里并且知道我可能可以手动更正每个标签上的间距,但这似乎比必要的更为繁琐,因为我很确定标签默认应该位于箭头的中心。

答案1

这里发生了什么?auto搞砸了。

为什么会发生这种情况?见下文。

可以修复吗?可以,至少可以临时修复。

\documentclass{article}
\usepackage{tikz-cd}
\tikzcdset{ad hoc fix/.style={r'/.style={r,/tikz/above},l'/.style={l,/tikz/below},
every label/.append style={/tikz/auto=false}}}

\begin{document}
\begin{tikzcd}[ampersand replacement=\&,ad hoc fix]
    0 \arrow[r', "N\lambda", bend left] \& 
    1 \arrow[r', "(N - 1)\lambda", bend left] \arrow[l', "\lambda", bend left] \& 
    2 \arrow[r', "(N - 2)\lambda", bend left] \arrow[l', "2\lambda", bend left] \& 
    \cdots \arrow[r', "2\lambda", bend left] \arrow[l', "3\lambda", bend left] \& 
    N-1 \arrow[r', "\lambda", bend left] \arrow[l', "(N-1)\lambda", bend left] \& 
    N \arrow[l', "N\lambda", bend left]
\end{tikzcd}

\begin{tikzcd}[ampersand replacement = \&]
    0 \arrow[r, "N\lambda", bend left] \& 1 \arrow[r, "(N - 1)\lambda", bend left] \arrow[l, "\lambda", bend left] \& 2 \arrow[r, "(N - 2)\lambda", bend left] \arrow[l, "2\lambda", bend left] \& 3 \arrow[r, "2\lambda", bend left] \arrow[l, "3\lambda", bend left] \& 4 \arrow[r, "\lambda", bend left] \arrow[l, "(N-1)\lambda", bend left] \& 5 \arrow[l, "N\lambda", bend left]
\end{tikzcd}
\end{document}

在此处输入图片描述

出现这种情况的原因是箭头的起点和终点的垂直位置不同。这可以从

\documentclass{article}

\usepackage{tikz-cd}

\begin{document}

\begin{tikzcd}[ampersand replacement = \&,every label/.append style={sloped}]
    0 \arrow[r, "N\lambda", bend left] \& 
    1 \arrow[r, "(N - 1)\lambda", bend left] \arrow[l, "\lambda", bend left] \& 
    2 \arrow[r, "(N - 2)\lambda", bend left] \arrow[l, "2\lambda", bend left] \& 
    \cdots \arrow[r, "2\lambda", bend left] \arrow[l, "3\lambda", bend left] \& 
    N-1 \arrow[r,"\lambda", bend left] \arrow[l, "(N-1)\lambda", bend left] \& 
    N \arrow[l, "N\lambda", bend left]
\end{tikzcd}

\end{document}

在此处输入图片描述

答案2

您可以通过增加弯曲角度来减少箭头标签的错位。这样,您可以稍微改变较长节点名称的箭头起点和终点的位置。例如:

\documentclass{article}
\usepackage{tikz-cd}

\begin{document}
    \begin{tikzcd}[sep=large,
                   every arrow/.style={draw,->,bend left=45}, % for shorter diagram code
                   ]
0 \ar[r, "N\lambda"]
    & 1 \ar[r, "(N - 1)\lambda"]
        \ar[l, "\lambda"]
        & 2 \ar[r, "(N - 2)\lambda"]
            \ar[l, "2\lambda"]
            & \cdots \ar[r, "2\lambda"]
                     \ar[l, "3\lambda"]
                & N-1 \ar[r, "\lambda"]
                      \ar[l, "(N-1)\lambda"]
                    & N \ar[l, "N\lambda"]
    \end{tikzcd}
\end{document}

在此处输入图片描述

答案3

看起来像是tikz链库的一个很好的应用程序:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{automata,chains,positioning}
\begin{document}

\begin{tikzpicture}[
auto,
start chain = going right, 
state/.append style = { 
    on chain,
    draw=none,
},
]
\node (s0)[state] {$0$};
\node (s1)[state] {$1$};
\node (s2)[state] {$2$};

\draw[->] (s0) edge[bend left] node {$N\lambda$} (s1)
          (s1) edge[bend left] node {$\lambda$} (s0)
          (s1) edge[bend left] node {$(N-1)\lambda$} (s2)
          (s2) edge[bend left] node {$2\lambda$} (s1)
          ;

\end{tikzpicture}
\end{document}          

在此处输入图片描述

相关内容